Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-135386: Fix "unable to open database file" errors on readonly DB #135566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 11 additions & 5 deletions Lib/dbm/sqlite3.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,25 @@ 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 = f"{uri}?mode={flag}&immutable=1"
else:
uri = f"{uri}?mode={flag}"
Comment on lines +62 to +66
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if flag == "ro":
# Add immutable=1 to allow read-only SQLite access even if wal/shm missing
uri = f"{uri}?mode={flag}&immutable=1"
else:
uri = f"{uri}?mode={flag}"
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"


try:
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.
with suppress(sqlite3.OperationalError):
self._cx.execute("PRAGMA journal_mode = wal")
if not self._readonly:
Copy link
Member

Choose a reason for hiding this comment

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

self._readonly is not used anywhere else, is it? You could just use flag != "ro" directly:

Suggested change
if not self._readonly:
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
18 changes: 18 additions & 0 deletions Lib/test/test_dbm_sqlite3.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sys
import unittest
from contextlib import closing
Expand Down Expand Up @@ -89,6 +90,23 @@ def test_readonly_keys(self):
def test_readonly_iter(self):
self.assertEqual([k for k in self.db], [b"key1", b"key2"])

@unittest.skipIf(sys.platform.startswith("win"), "incompatible with Windows file locking")
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 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