Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Lib/dbm/sqlite3.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,23 @@ def __init__(self, path, /, *, flag, mode):
# We use the URI format when opening the database.
uri = _normalize_uri(path)
uri = f"{uri}?mode={flag}"
if flag == "ro":
# Add immutable=1 to allow read-only SQLite access even if wal/shm missing
uri += "&immutable=1"
Comment thread
furkanonder marked this conversation as resolved.

try:
self._cx = sqlite3.connect(uri, autocommit=True, uri=True)
except sqlite3.Error as exc:
raise error(str(exc))

self._readonly = (flag == "ro")
Comment thread
GeneralK1ng marked this conversation as resolved.
Outdated
# This is an optimization only; it's ok if it fails.
with suppress(sqlite3.OperationalError):
self._cx.execute("PRAGMA journal_mode = wal")
if flag != "ro":
with suppress(sqlite3.OperationalError):
self._cx.execute("PRAGMA journal_mode = OFF")

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

def _execute(self, *args, **kwargs):
if not self._cx:
Expand Down
25 changes: 25 additions & 0 deletions Lib/test/test_dbm_sqlite3.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import stat
import os
import sys
import unittest
from contextlib import closing
Expand Down Expand Up @@ -89,6 +91,29 @@ 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"
Comment thread
furkanonder marked this conversation as resolved.
Outdated
shm_path = self.filename + "-shm"
Comment thread
furkanonder marked this conversation as resolved.
Outdated

for suffix in wal_path, shm_path:
os_helper.unlink(suffix)

try:
self.db.close()
except Exception:
pass

os.chmod(self.filename, stat.S_IREAD)

db = dbm_sqlite3.open(self.filename, "r")
try:
self.assertEqual(db[b"key1"], b"value1")
self.assertEqual(db[b"key2"], b"value2")
finally:
db.close()

Comment thread
GeneralK1ng marked this conversation as resolved.
Outdated
os.chmod(self.filename, stat.S_IWRITE)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC correctly, this change makes it so that the WAL/SHM files are no longer created in read-only mode? In that case, we could simplify the test to simply open the database in read-only mode and check that the auxiliary files are not written. I'm thinking something like this:

class Immutable(unittest.TestCase):
    def setUp(self):
        self.filename = os_helper.TESTFN
        self.db = dbm_sqlite3.open(self.filename, "r")

    def tearDown(self):
        self.db.close()

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

        self.assertFalse(os.path.exists(wal_path))
        self.assertFalse(os.path.exists(shm_path))

Or does this also affect the database file itself? i.e. does passing immutable=1 allow opening read-only files whereas otherwise it would be impossible?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I’ve replaced the original test with your suggested version in a new Immutable test class. It’s much cleaner and focused.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or does this also affect the database file itself? i.e. does passing immutable=1 allow opening read-only files whereas otherwise it would be impossible?

Yes, exactly — without immutable=1, even in mode=ro, SQLite may still attempt operations that require write access (like writing lock files or checking for journal/WAL state), and will fail if the database file is read-only or in a read-only directory.

Passing immutable=1 tells SQLite to treat the database as entirely static and read-only, which avoids those operations and allows the file to be opened successfully even with 0444 permissions or from a read-only mount.



class ReadWrite(_SQLiteDbmTests):

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