Skip to content

Commit b08495b

Browse files
committed
python#17198: Fix a NameError in the dbm module. Patch by Valentina Mukhamedzhanova.
1 parent 331c3fd commit b08495b

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

Lib/dbm/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class error(Exception):
4444

4545
error = (error, IOError)
4646

47+
try:
48+
from dbm import ndbm
49+
except ImportError:
50+
ndbm = None
51+
4752

4853
def open(file, flag='r', mode=0o666):
4954
"""Open or create database at path given by *file*.

Lib/test/test_dbm.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
# Skip tests if dbm module doesn't exist.
1010
dbm = test.support.import_module('dbm')
1111

12+
try:
13+
from dbm import ndbm
14+
except ImportError:
15+
ndbm = None
16+
1217
_fname = test.support.TESTFN
1318

1419
#
@@ -130,7 +135,7 @@ def test_whichdb(self):
130135
delete_files()
131136
f = module.open(_fname, 'c')
132137
f.close()
133-
self.assertEqual(name, dbm.whichdb(_fname))
138+
self.assertEqual(name, self.dbm.whichdb(_fname))
134139
# Now add a key
135140
f = module.open(_fname, 'w')
136141
f[b"1"] = b"1"
@@ -139,7 +144,15 @@ def test_whichdb(self):
139144
# and read it
140145
self.assertTrue(f[b"1"] == b"1")
141146
f.close()
142-
self.assertEqual(name, dbm.whichdb(_fname))
147+
self.assertEqual(name, self.dbm.whichdb(_fname))
148+
149+
@unittest.skipUnless(ndbm, reason='Test requires ndbm')
150+
def test_whichdb_ndbm(self):
151+
# Issue 17198: check that ndbm which is referenced in whichdb is defined
152+
db_file = '{}_ndbm.db'.format(_fname)
153+
with open(db_file, 'w'):
154+
self.addCleanup(test.support.unlink, db_file)
155+
self.assertIsNone(self.dbm.whichdb(db_file[:-3]))
143156

144157
def tearDown(self):
145158
delete_files()
@@ -149,6 +162,7 @@ def setUp(self):
149162
self.filename = test.support.TESTFN
150163
self.d = dbm.open(self.filename, 'c')
151164
self.d.close()
165+
self.dbm = test.support.import_fresh_module('dbm')
152166

153167
def test_keys(self):
154168
self.d = dbm.open(self.filename, 'c')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ Core and Builtins
4141
Library
4242
-------
4343

44+
- Issue #17198: Fix a NameError in the dbm module. Patch by Valentina
45+
Mukhamedzhanova.
46+
4447
- Issue #18013: Fix cgi.FieldStorage to parse the W3C sample form.
4548

4649
- Issue #18347: ElementTree's html serializer now preserves the case of

0 commit comments

Comments
 (0)