From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Joseph Turner Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] Fix persist-reset Date: Fri, 14 Jun 2024 13:04:39 -0700 Message-ID: <87msnnjnoo.fsf@ushin.org> References: <87o785pq6j.fsf@ushin.org> <9d5ec904-d4a4-438b-8862-7ef737b1b6b5@alphapapa.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="21940"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Emacs Devel Mailing List To: Adam Porter Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Jun 15 07:47:29 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1sIMG8-0005Uo-3t for ged-emacs-devel@m.gmane-mx.org; Sat, 15 Jun 2024 07:47:28 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sIMF9-0008OX-Mw; Sat, 15 Jun 2024 01:46:27 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sIDAL-0006AJ-Fw for emacs-devel@gnu.org; Fri, 14 Jun 2024 16:04:53 -0400 Original-Received: from out-186.mta0.migadu.com ([91.218.175.186]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sIDAI-0007Mb-Kt for emacs-devel@gnu.org; Fri, 14 Jun 2024 16:04:53 -0400 X-Envelope-To: adam@alphapapa.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ushin.org; s=key1; t=1718395482; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Ts0hx/LBqPxX7cTvYeYpxcpk8vXXabG9PJS8TElM+p4=; b=HdXgthBQqlGvj+UvowcCTmdo4Xvmrn1OvWWdL4l5xJqraybF+orA6iJPkJdFSM9yK55+lq LSsk/BLae3z+Bp6tgMs4tpI82eru/U/ofe645cMT5CCjc/g/3MQSuiIz3gx1yKvmcCfcyK cSEms6sW598/sw2tu74PNI+OeTNkRI0nYTSbPvH1NA+H31ASKt0fGObE7v3MsTBaSaqVW6 NgQPr+B1QHdt8KJFPrNfbok1Y6NoVeUT4j0XRiI1v5mH0TOmEcUObhAZwYqhSq6kvZxJ/l zbwlHK64j4G1fguzaV8BSZCmbVarYnCAQgTyZ5wkMcSVIOKOcjjM5JhAttKyqA== X-Envelope-To: emacs-devel@gnu.org X-Envelope-To: adam.porter@47ap.net X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. In-Reply-To: <9d5ec904-d4a4-438b-8862-7ef737b1b6b5@alphapapa.net> (Adam Porter's message of "Thu, 13 Jun 2024 21:47:13 -0500") X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=91.218.175.186; envelope-from=joseph@ushin.org; helo=out-186.mta0.migadu.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Sat, 15 Jun 2024 01:46:26 -0400 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:320097 Archived-At: --=-=-= Content-Type: text/plain Adam Porter writes: > Hi Joseph, > > On 6/12/24 20:49, Joseph Turner wrote: >> Previously, persist-reset set the value of SYM to its default without >> copying it, which caused subsequent modifications to the value of SYM >> to erroneously modify the default value. > > The only suggestion I can think of would be, in the test, to perhaps > bind to a variable the initial hash table set as the default value, so > that you could also compare against that with EQ to ensure that the > original table is not being reused. Good idea! Please see new patchset. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Copy-default-when-resetting-with-persist-reset.patch >From 39b1a0a1d1ae25130a270a115ae8241af4ed6d75 Mon Sep 17 00:00:00 2001 From: Joseph Turner Date: Wed, 12 Jun 2024 18:45:32 -0700 Subject: [PATCH 1/2] Copy default when resetting with persist-reset Previously, persist-reset set the value of SYM to its default without copying it, which caused subsequent modifications to the value of SYM to erroneously modify the default value. Co-authored-by: Adam Porter --- persist.el | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/persist.el b/persist.el index 6cc94b4db3..df7f3836c5 100644 --- a/persist.el +++ b/persist.el @@ -122,9 +122,7 @@ (defun persist-symbol (symbol &optional initvalue) (let ((initvalue (or initvalue (symbol-value symbol)))) (add-to-list 'persist--symbols symbol) (put symbol 'persist t) - (if (hash-table-p initvalue) - (put symbol 'persist-default (copy-hash-table initvalue)) - (put symbol 'persist-default (persist-copy-tree initvalue t))))) + (put symbol 'persist-default (persist-copy initvalue)))) (defun persist--persistant-p (symbol) "Return non-nil if SYMBOL is a persistent variable." @@ -164,8 +162,8 @@ (defun persist-default (symbol) (get symbol 'persist-default)) (defun persist-reset (symbol) - "Reset the value of SYMBOL to the default." - (set symbol (persist-default symbol))) + "Set the value of SYMBOL to a copy of the default." + (set symbol (persist-copy (persist-default symbol)))) (defun persist-load (symbol) "Load the saved value of SYMBOL." @@ -241,5 +239,11 @@ (defun persist-copy-tree (tree &optional vectors-and-records) tree) tree))) +(defun persist-copy (obj) + "Return copy of OBJ." + (if (hash-table-p obj) + (copy-hash-table obj) + (persist-copy-tree obj t))) + (provide 'persist) ;;; persist.el ends here -- 2.41.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0002-Test-persist-reset.patch >From b30f854d149d7cbe550716d3cfdbeb170a6ca1ab Mon Sep 17 00:00:00 2001 From: Joseph Turner Date: Wed, 12 Jun 2024 18:45:36 -0700 Subject: [PATCH 2/2] Test persist-reset Co-authored-by: Adam Porter --- test/persist-tests.el | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/persist-tests.el b/test/persist-tests.el index 62d8501493..4439fa3e07 100644 --- a/test/persist-tests.el +++ b/test/persist-tests.el @@ -134,3 +134,20 @@ (ert-deftest test-persist-location () (should-error (persist-save 'fred))) (delete-directory "./persist-defined-location" t))) + +(ert-deftest test-persist-reset () + "Symbol should be reset to a copy of the default." + (let ((initial-value (make-hash-table))) + (with-local-temp-persist + (persist-defvar persist--test-reset-variable initial-value "docstring") + (should-not (eq persist--test-reset-variable + (persist-default 'persist--test-reset-variable))) + (should-not (eq persist--test-reset-variable initial-value)) + (should-not (eq initial-value + (persist-default 'persist--test-reset-variable))) + (persist-reset 'persist--test-reset-variable) + (should-not (eq persist--test-reset-variable + (persist-default 'persist--test-reset-variable))) + (should-not (eq persist--test-reset-variable initial-value)) + (should-not (eq initial-value + (persist-default 'persist--test-reset-variable)))))) -- 2.41.0 --=-=-=--