summaryrefslogtreecommitdiffstats
path: root/Lib/dbm
diff options
context:
space:
mode:
authorHenry-Joseph Audéoud <h.audeoud@gmail.com>2021-09-10 12:26:16 (GMT)
committerGitHub <noreply@github.com>2021-09-10 12:26:16 (GMT)
commit707137b8637feef37b2e06a851fdca9d1b945861 (patch)
treeeedd9bec5c0b98b244438e120ee12d8143906c19 /Lib/dbm
parent62fa613f6a6e872723505ee9d56242c31a654a9d (diff)
downloadcpython-707137b8637feef37b2e06a851fdca9d1b945861.zip
cpython-707137b8637feef37b2e06a851fdca9d1b945861.tar.gz
cpython-707137b8637feef37b2e06a851fdca9d1b945861.tar.bz2
bpo-40563: Support pathlike objects on dbm/shelve (GH-21849)
Co-authored-by: Hakan Çelik <hakancelik96@outlook.com>
Diffstat (limited to 'Lib/dbm')
-rw-r--r--Lib/dbm/__init__.py13
-rw-r--r--Lib/dbm/dumb.py7
2 files changed, 11 insertions, 9 deletions
diff --git a/Lib/dbm/__init__.py b/Lib/dbm/__init__.py
index f65da52..8055d37 100644
--- a/Lib/dbm/__init__.py
+++ b/Lib/dbm/__init__.py
@@ -109,17 +109,18 @@ def whichdb(filename):
"""
# Check for ndbm first -- this has a .pag and a .dir file
+ filename = os.fsencode(filename)
try:
- f = io.open(filename + ".pag", "rb")
+ f = io.open(filename + b".pag", "rb")
f.close()
- f = io.open(filename + ".dir", "rb")
+ f = io.open(filename + b".dir", "rb")
f.close()
return "dbm.ndbm"
except OSError:
# some dbm emulations based on Berkeley DB generate a .db file
# some do not, but they should be caught by the bsd checks
try:
- f = io.open(filename + ".db", "rb")
+ f = io.open(filename + b".db", "rb")
f.close()
# guarantee we can actually open the file using dbm
# kind of overkill, but since we are dealing with emulations
@@ -134,12 +135,12 @@ def whichdb(filename):
# Check for dumbdbm next -- this has a .dir and a .dat file
try:
# First check for presence of files
- os.stat(filename + ".dat")
- size = os.stat(filename + ".dir").st_size
+ os.stat(filename + b".dat")
+ size = os.stat(filename + b".dir").st_size
# dumbdbm files with no keys are empty
if size == 0:
return "dbm.dumb"
- f = io.open(filename + ".dir", "rb")
+ f = io.open(filename + b".dir", "rb")
try:
if f.read(1) in (b"'", b'"'):
return "dbm.dumb"
diff --git a/Lib/dbm/dumb.py b/Lib/dbm/dumb.py
index 864ad37..754624c 100644
--- a/Lib/dbm/dumb.py
+++ b/Lib/dbm/dumb.py
@@ -46,6 +46,7 @@ class _Database(collections.abc.MutableMapping):
_io = _io # for _commit()
def __init__(self, filebasename, mode, flag='c'):
+ filebasename = self._os.fsencode(filebasename)
self._mode = mode
self._readonly = (flag == 'r')
@@ -54,14 +55,14 @@ class _Database(collections.abc.MutableMapping):
# where key is the string key, pos is the offset into the dat
# file of the associated value's first byte, and siz is the number
# of bytes in the associated value.
- self._dirfile = filebasename + '.dir'
+ self._dirfile = filebasename + b'.dir'
# The data file is a binary file pointed into by the directory
# file, and holds the values associated with keys. Each value
# begins at a _BLOCKSIZE-aligned byte offset, and is a raw
# binary 8-bit string value.
- self._datfile = filebasename + '.dat'
- self._bakfile = filebasename + '.bak'
+ self._datfile = filebasename + b'.dat'
+ self._bakfile = filebasename + b'.bak'
# The index is an in-memory dict, mirroring the directory file.
self._index = None # maps keys to (pos, siz) pairs