Skip to content

Commit 3d7596b

Browse files
Restore journaling and simplify tests.
1 parent a28c0cc commit 3d7596b

File tree

2 files changed

+11
-58
lines changed

2 files changed

+11
-58
lines changed

Lib/dbm/sqlite3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __init__(self, path, /, *, flag, mode):
7272
# This is an optimization only; it's ok if it fails.
7373
if flag != "ro":
7474
with suppress(sqlite3.OperationalError):
75-
self._cx.execute("PRAGMA journal_mode = OFF")
75+
self._cx.execute("PRAGMA journal_mode = wal")
7676

7777
if flag == "rwc":
7878
self._execute(BUILD_TABLE)

Lib/test/test_dbm_sqlite3.py

Lines changed: 10 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -91,86 +91,39 @@ def test_readonly_keys(self):
9191
def test_readonly_iter(self):
9292
self.assertEqual([k for k in self.db], [b"key1", b"key2"])
9393

94-
class Immutable(unittest.TestCase):
95-
def setUp(self):
96-
self.filename = os_helper.TESTFN
97-
98-
db = dbm_sqlite3.open(self.filename, "c")
99-
db[b"key"] = b"value"
100-
db.close()
101-
102-
self.db = dbm_sqlite3.open(self.filename, "r")
103-
104-
def tearDown(self):
105-
self.db.close()
106-
for suffix in "", "-wal", "-shm":
107-
os_helper.unlink(self.filename + suffix)
108-
109-
def test_readonly_open_without_wal_shm(self):
110-
wal_path = self.filename + "-wal"
111-
shm_path = self.filename + "-shm"
112-
113-
self.assertFalse(os.path.exists(wal_path))
114-
self.assertFalse(os.path.exists(shm_path))
115-
116-
self.assertEqual(self.db[b"key"], b"value")
117-
11894

11995
class ReadOnlyFilesystem(unittest.TestCase):
12096

12197
def setUp(self):
12298
self.test_dir = os_helper.TESTFN
99+
self.addCleanup(os_helper.rmtree, self.test_dir)
123100
os.mkdir(self.test_dir)
124101
self.db_path = os.path.join(self.test_dir, "test.db")
125102

126103
db = dbm_sqlite3.open(self.db_path, "c")
127104
db[b"key"] = b"value"
128105
db.close()
129106

130-
def tearDown(self):
131-
os.chmod(self.db_path, stat.S_IWRITE)
132-
os.chmod(self.test_dir, stat.S_IWRITE | stat.S_IEXEC | stat.S_IREAD)
133-
os_helper.rmtree(self.test_dir)
134-
135-
def test_open_readonly_dir_success_ro(self):
136-
files = os.listdir(self.test_dir)
137-
self.assertEqual(sorted(files), ["test.db"])
138-
139-
os.chmod(self.test_dir, stat.S_IREAD | stat.S_IEXEC)
140-
with dbm_sqlite3.open(self.db_path, "r") as db:
141-
self.assertEqual(db[b"key"], b"value")
142-
143-
def test_open_readonly_file_success(self):
107+
def test_readonly_file_read(self):
144108
os.chmod(self.db_path, stat.S_IREAD)
145109
with dbm_sqlite3.open(self.db_path, "r") as db:
146110
self.assertEqual(db[b"key"], b"value")
147111

148-
def test_open_readonly_file_fail_rw(self):
112+
def test_readonly_file_write(self):
149113
os.chmod(self.db_path, stat.S_IREAD)
150114
with dbm_sqlite3.open(self.db_path, "w") as db:
151-
with self.assertRaises(OSError):
115+
with self.assertRaises(dbm_sqlite3.error):
152116
db[b"newkey"] = b"newvalue"
153-
@unittest.skipUnless(sys.platform == "darwin", "SQLite fallback behavior differs on non-macOS")
154-
def test_open_readonly_dir_fail_rw_missing_wal_shm(self):
155-
for suffix in ("-wal", "-shm"):
156-
os_helper.unlink(self.db_path + suffix)
157117

118+
def test_readonly_dir_read(self):
158119
os.chmod(self.test_dir, stat.S_IREAD | stat.S_IEXEC)
120+
with dbm_sqlite3.open(self.db_path, "r") as db:
121+
self.assertEqual(db[b"key"], b"value")
159122

160-
with self.assertRaises(OSError):
161-
with dbm_sqlite3.open(self.db_path, "w") as db:
162-
db[b"newkey"] = b"newvalue"
163-
164-
@unittest.skipUnless(sys.platform == "darwin", "SQLite fallback behavior differs on non-macOS")
165-
def test_open_readonly_dir_fail_rw_with_writable_db(self):
166-
os.chmod(self.db_path, stat.S_IREAD | stat.S_IWRITE)
167-
for suffix in ("-wal", "-shm"):
168-
os_helper.unlink(self.db_path + suffix)
169-
123+
def test_readonly_dir_write(self):
170124
os.chmod(self.test_dir, stat.S_IREAD | stat.S_IEXEC)
171-
172-
with self.assertRaises(OSError):
173-
with dbm_sqlite3.open(self.db_path, "w") as db:
125+
with dbm_sqlite3.open(self.db_path, "w") as db:
126+
with self.assertRaises(dbm_sqlite3.error):
174127
db[b"newkey"] = b"newvalue"
175128

176129

0 commit comments

Comments
 (0)