Skip to content

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

Open
@jmcarcell

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions