gh-155869: Fix data loss in dbm.dumb.reorganize() (GH-155872)

serhiy-storchaka <[email protected]>
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/e3287f631f3c88ed80191aa222e7fc4ba91edd17
commit: e3287f631f3c88ed80191aa222e7fc4ba91edd17
branch: main
author: Łukasz <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-08-15T20:47:06Z
summary:

gh-155869: Fix data loss in dbm.dumb.reorganize() (GH-155872)

files:
A Misc/NEWS.d/next/Library/2026-08-15-21-55-31.gh-issue-155869.yRUnQW.rst
M Lib/dbm/dumb.py
M Lib/test/test_dbm_dumb.py

diff --git a/Lib/dbm/dumb.py b/Lib/dbm/dumb.py
index c1c38da5101a572..a080f4e865508b0 100644
--- a/Lib/dbm/dumb.py
+++ b/Lib/dbm/dumb.py
@@ -311,6 +311,7 @@ def reorganize(self):
                 reorganize_pos += blocks_occupied * _BLOCKSIZE
 
             f.truncate(reorganize_pos)
+        self._modified = True
         # Commit changes to index, which were not in-place.
         self._commit()
 
diff --git a/Lib/test/test_dbm_dumb.py b/Lib/test/test_dbm_dumb.py
index 672f9092207cf62..d977a81876df651 100644
--- a/Lib/test/test_dbm_dumb.py
+++ b/Lib/test/test_dbm_dumb.py
@@ -114,6 +114,16 @@ def test_write_write_read(self):
         with contextlib.closing(dumbdbm.open(_fname)) as f:
             self.assertEqual(f[b'1'], b'hello2')
 
+    def test_reorganize_persists_changed_offsets(self):
+        with dumbdbm.open(_fname, 'n') as f:
+            f[b'deleted'] = b'x'
+            f[b'retained'] = b'value'
+            del f[b'deleted']
+            f.reorganize()
+
+        with dumbdbm.open(_fname, 'r') as f:
+            self.assertEqual(f[b'retained'], b'value')
+
     def test_str_read(self):
         self.init_db()
         with contextlib.closing(dumbdbm.open(_fname, 'r')) as f:
diff --git a/Misc/NEWS.d/next/Library/2026-08-15-21-55-31.gh-issue-155869.yRUnQW.rst b/Misc/NEWS.d/next/Library/2026-08-15-21-55-31.gh-issue-155869.yRUnQW.rst
new file mode 100644
index 000000000000000..19f76c176af0629
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-08-15-21-55-31.gh-issue-155869.yRUnQW.rst
@@ -0,0 +1,2 @@
+Fix :meth:`!reorganize` in :mod:`dbm.dumb` failing to persist updated value
+offsets, which could cause data loss after reopening the database.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.