]> granicus.if.org Git - python/commitdiff
#17198: Fix a NameError in the dbm module. Patch by Valentina Mukhamedzhanova.
authorEzio Melotti <ezio.melotti@gmail.com>
Sun, 7 Jul 2013 11:15:08 +0000 (13:15 +0200)
committerEzio Melotti <ezio.melotti@gmail.com>
Sun, 7 Jul 2013 11:15:08 +0000 (13:15 +0200)
Lib/dbm/__init__.py
Lib/test/test_dbm.py
Misc/NEWS

index 813a29d70922814b20a93ed7cc68df7673d12a30..a783fde464b30fceb0e79d173a127577c56d88a5 100644 (file)
@@ -44,6 +44,11 @@ _modules = {}
 
 error = (error, IOError)
 
+try:
+    from dbm import ndbm
+except ImportError:
+    ndbm = None
+
 
 def open(file, flag='r', mode=0o666):
     """Open or create database at path given by *file*.
index 752e0b77a6b858545a5d193176ea8b6fd301b0cb..243a0af10319523e7fe02bfb4404ba7e58f04dc5 100644 (file)
@@ -9,6 +9,11 @@ import test.support
 # Skip tests if dbm module doesn't exist.
 dbm = test.support.import_module('dbm')
 
+try:
+    from dbm import ndbm
+except ImportError:
+    ndbm = None
+
 _fname = test.support.TESTFN
 
 #
@@ -130,7 +135,7 @@ class WhichDBTestCase(unittest.TestCase):
             delete_files()
             f = module.open(_fname, 'c')
             f.close()
-            self.assertEqual(name, dbm.whichdb(_fname))
+            self.assertEqual(name, self.dbm.whichdb(_fname))
             # Now add a key
             f = module.open(_fname, 'w')
             f[b"1"] = b"1"
@@ -139,7 +144,15 @@ class WhichDBTestCase(unittest.TestCase):
             # and read it
             self.assertTrue(f[b"1"] == b"1")
             f.close()
-            self.assertEqual(name, dbm.whichdb(_fname))
+            self.assertEqual(name, self.dbm.whichdb(_fname))
+
+    @unittest.skipUnless(ndbm, reason='Test requires ndbm')
+    def test_whichdb_ndbm(self):
+        # Issue 17198: check that ndbm which is referenced in whichdb is defined
+        db_file = '{}_ndbm.db'.format(_fname)
+        with open(db_file, 'w'):
+            self.addCleanup(test.support.unlink, db_file)
+        self.assertIsNone(self.dbm.whichdb(db_file[:-3]))
 
     def tearDown(self):
         delete_files()
@@ -149,6 +162,7 @@ class WhichDBTestCase(unittest.TestCase):
         self.filename = test.support.TESTFN
         self.d = dbm.open(self.filename, 'c')
         self.d.close()
+        self.dbm = test.support.import_fresh_module('dbm')
 
     def test_keys(self):
         self.d = dbm.open(self.filename, 'c')
index b0bc75f2989cabe6d32b6eaa4b41e6c108eeb8e3..63d49693d7d788a7aa411482747a48110366a6bf 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -41,6 +41,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #17198: Fix a NameError in the dbm module.  Patch by Valentina
+  Mukhamedzhanova.
+
 - Issue #18013: Fix cgi.FieldStorage to parse the W3C sample form.
 
 - Issue #18347: ElementTree's html serializer now preserves the case of