Previous commit
Next commit
gh-135386: Fix dbm.sqlite3 readonly open error by using immutable=1
  • Loading branch information
blurb-it[bot] authored and GeneralK1ng committedJun 17, 2025
commit 3e3a3ba6c6f1a8ea747e08b3d1ad6b098366d4b6
Original file line numberDiff line numberDiff line change
Expand Up@@ -69,6 +69,7 @@ def __init__(self, path, /, *, flag, mode):
self._cx = sqlite3.connect(uri, autocommit=True, uri=True)
except sqlite3.Error as exc:
raise error(str(exc))

self._readonly = (flag == "ro")
# This is an optimization only; it's ok if it fails.
if not self._readonly:
Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
import os
import sys
import unittest
from contextlib import closing
Expand DownExpand Up@@ -89,6 +90,22 @@ def test_readonly_keys(self):
def test_readonly_iter(self):
self.assertEqual([k for k in self.db], [b"key1", b"key2"])

def test_readonly_open_without_wal_shm(self):
wal_path = self.filename + "-wal"
shm_path = self.filename + "-shm"

for suffix in wal_path, shm_path:
try:
os.remove(suffix)
except FileNotFoundError:
pass

os.chmod(self.filename, 0o444)

with dbm_sqlite3.open(self.filename, "r") as db:
self.assertEqual(db[b"key1"], b"value1")
self.assertEqual(db[b"key2"], b"value2")


class ReadWrite(_SQLiteDbmTests):

Expand Down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
Fix :exc:`sqlite3.OperationalError` error when using :func:`dbm.open` with a read-only file object.
Loading