Skip to content
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
14 changes: 9 additions & 5 deletions Doc/library/shelve.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ lots of shared sub-objects. The keys are ordinary strings.
determine which accessed entries are mutable, nor which ones were actually
mutated).

The optional *mode* parameter controls the file mode (permissions) when creating
a new shelf. It is passed to :func:`dbm.open` and defaults to ``0o666`` (octal),
which means read and write permissions for everyone, subject to the current umask.
This parameter has no effect if the database already exists.

By default, :mod:`!shelve` uses :func:`pickle.dumps` and :func:`pickle.loads`
for serializing and deserializing. This can be changed by supplying
*serializer* and *deserializer*, respectively.
Expand Down Expand Up @@ -209,19 +214,19 @@ Restrictions


.. class:: DbfilenameShelf(filename, flag='c', protocol=None, \
writeback=False, *, serializer=None, \
deserializer=None)
writeback=False, mode=0o666, *, \
serializer=None, deserializer=None)

A subclass of :class:`Shelf` which accepts a *filename* instead of a dict-like
object. The underlying file will be opened using :func:`dbm.open`. By
default, the file will be created and opened for both read and write. The
optional *flag* parameter has the same interpretation as for the
:func:`.open` function. The optional *protocol*, *writeback*, *serializer*
:func:`.open` function. The optional *mode*, *protocol*, *writeback*, *serializer*
and *deserializer* parameters have the same interpretation as in
:func:`~shelve.open`.

.. versionchanged:: 3.15
Added the *serializer* and *deserializer* parameters.
Added the *mode*, *serializer* and *deserializer* parameters.


.. _shelve-example:
Expand Down Expand Up @@ -284,4 +289,3 @@ Exceptions

Module :mod:`pickle`
Object serialization used by :mod:`!shelve`.

3 changes: 2 additions & 1 deletion Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,8 @@ shelve
* Add support for custom serialization and deserialization functions
in the :mod:`shelve` module.
(Contributed by Furkan Onder in :gh:`99631`.)

* Add suport for custom mode in :func:`shelve.open`.
(Contributed by Guilherme Crocetti in :gh:`113093`.)

socket
------
Expand Down
16 changes: 9 additions & 7 deletions Lib/shelve.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ class DbfilenameShelf(Shelf):
See the module's __doc__ string for an overview of the interface.
"""

def __init__(self, filename, flag='c', protocol=None, writeback=False, *,
serializer=None, deserializer=None):
def __init__(self, filename, flag='c', protocol=None, writeback=False,
mode=0o666, *, serializer=None, deserializer=None):
import dbm
Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback,
Shelf.__init__(self, dbm.open(filename, flag, mode), protocol, writeback,
serializer=serializer, deserializer=deserializer)

def clear(self):
Expand All @@ -248,19 +248,21 @@ def clear(self):
self.cache.clear()
self.dict.clear()

def open(filename, flag='c', protocol=None, writeback=False, *,
serializer=None, deserializer=None):
def open(filename, flag='c', protocol=None, writeback=False,
mode=0o666, *, serializer=None, deserializer=None):
"""Open a persistent dictionary for reading and writing.

The filename parameter is the base filename for the underlying
database. As a side-effect, an extension may be added to the
filename and more than one file may be created. The optional flag
parameter has the same interpretation as the flag parameter of
dbm.open(). The optional protocol parameter specifies the
version of the pickle protocol.
version of the pickle protocol. The optional mode parameter is
passed to dbm.open() and controls the file mode when creating a
new shelf, set to 0666 by default.

See the module's __doc__ string for an overview of the interface.
"""

return DbfilenameShelf(filename, flag, protocol, writeback,
return DbfilenameShelf(filename, flag, protocol, writeback, mode,
serializer=serializer, deserializer=deserializer)
7 changes: 7 additions & 0 deletions Lib/test/test_shelve.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import array
import unittest
from unittest import mock
import dbm
import shelve
import pickle
Expand Down Expand Up @@ -47,6 +48,12 @@ class TestCase(unittest.TestCase):
dirname = os_helper.TESTFN
fn = os.path.join(os_helper.TESTFN, "shelftemp.db")

@mock.patch("dbm.open", autospec=True)
def test_open_calls_dbm_as_expected(self, dbm_open):
shelf_open_mode = 0o433
shelve.open(filename=self.fn, mode=shelf_open_mode)
dbm_open.assert_called_once_with(self.fn, 'c', shelf_open_mode)

def test_close(self):
d1 = {}
s = shelve.Shelf(d1, protocol=2, writeback=False)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add parameter *mode* in :func:`shelve.open`.
Contributed by Guilherme Crocetti.
Loading