From 05cf46a1d856443ed677879c1597b41a97e51b20 Mon Sep 17 00:00:00 2001 From: lkk7 Date: Sat, 15 Aug 2026 22:01:16 +0200 Subject: [PATCH] gh-155869: Persist dbm.dumb offsets after reorganization --- Lib/dbm/dumb.py | 1 + Lib/test/test_dbm_dumb.py | 10 ++++++++++ .../2026-08-15-21-55-31.gh-issue-155869.yRUnQW.rst | 2 ++ 3 files changed, 13 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-08-15-21-55-31.gh-issue-155869.yRUnQW.rst 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.