Skip to content

Unable to read sqlite3 database keys in read-only directory with Python 3.13 with shelve #135386

Closed

Description

@jmcarcell

Bug report

Bug description:

In Python 3.13, sqlite is the new default for creating databases (https://www.python.org/downloads/release/python-3130/). However, it seems that it is not possible to get the keys of the database if the database file is in a read-only folder.

The setup:

mkdir PythonTest
cd PythonTest
python -c "import shelve; shelve.open('db.db', 'c')"
cd ..
chmod 555 PythonTest

Then running

import shelve

with shelve.open('PythonTest/db.db', 'r') as db:
    print(list(db.keys()))

will fail with

Traceback (most recent call last):
  File "/usr/lib/python3.13/dbm/sqlite3.py", line 79, in _execute
    return closing(self._cx.execute(*args, **kwargs))
                   ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
sqlite3.OperationalError: attempt to write a readonly database

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/read.py", line 5, in <module>
    print(list(db.keys()))
          ~~~~^^^^^^^^^^^
  File "<frozen _collections_abc>", line 854, in __len__
  File "/usr/lib/python3.13/shelve.py", line 99, in __len__
    return len(self.dict)
  File "/usr/lib/python3.13/dbm/sqlite3.py", line 84, in __len__
    with self._execute(GET_SIZE) as cu:
         ~~~~~~~~~~~~~^^^^^^^^^^
  File "/usr/lib/python3.13/dbm/sqlite3.py", line 81, in _execute
    raise error(str(exc))
dbm.sqlite3.error: attempt to write a readonly database

which I believe to be unexpected since the database was opened with the r flag, for read-only (see https://docs.python.org/3/library/dbm.html#dbm.open).

CPython versions tested on:

3.13.2 and 3.13.3

Operating systems tested on:

Linux (Ubuntu 24, Arch Linux)

Linked PRs

Activity

  1. vstinner commented on Jun 11, 2025

    @vstinner
    Member
  2. GeneralK1ng commented on Jun 16, 2025

    @GeneralK1ng
    Contributor

    Hi,
    Thanks for reporting this issue! I have worked on a fix that adds the immutable=1 flag when opening a read-only SQLite DB to avoid WAL/SHM file errors.
    I am preparing a PR #135566 and will submit it soon for review.
    Looking forward to feedback from the maintainers and community.

    Thanks!

  3. added 3 commits that reference this issue on Jun 17, 2025
  4. added a commit that references this issue on Aug 22, 2025
  5. added 2 commits that reference this issue on Aug 22, 2025
  6. added
    3.13only security fixes
    3.14bugs and security fixes
    3.15bugs and security fixes
    on Aug 22, 2025
  7. added 4 commits that reference this issue on Aug 22, 2025
  8. added a commit that references this issue on Aug 22, 2025
  9. added a commit that references this issue on Aug 22, 2025
  10. added a commit that references this issue on Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixes3.14bugs and security fixes3.15bugs and security fixesextension-modulesC modules in the Modules dirtopic-sqlite3type-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions