]> granicus.if.org Git - python/commitdiff
bpo-34248: Add filename to error raised in {gnu,ndbm}.open() (GH-8590)
authorZsolt Cserna <cserna.zsolt@gmail.com>
Thu, 27 Sep 2018 19:54:34 +0000 (21:54 +0200)
committerBerker Peksag <berker.peksag@gmail.com>
Thu, 27 Sep 2018 19:54:34 +0000 (22:54 +0300)
Report the filename to the exception when raising {gdbm,dbm.ndbm}.error in
dbm.gnu.open() and dbm.ndbm.open() functions, so it gets printed when the
exception is raised, and can also be obtained by the filename attribute of the
exception object.

Lib/test/test_dbm_gnu.py
Lib/test/test_dbm_ndbm.py
Misc/NEWS.d/next/Library/2018-07-31-23-00-09.bpo-34248.5U6wwc.rst [new file with mode: 0644]
Modules/_dbmmodule.c
Modules/_gdbmmodule.c

index c96eff5a319e64fbc4af94de6388f78f74ff88c4..16b7fe6fac1760e5bbd47d2ecbdc392354f3f600 100644 (file)
@@ -144,6 +144,13 @@ class TestGdbm(unittest.TestCase):
             self.assertTrue(b'key' in db)
             self.assertEqual(db[b'key'], b'value')
 
+    def test_nonexisting_file(self):
+        nonexisting_file = 'nonexisting-file'
+        with self.assertRaises(gdbm.error) as cm:
+            gdbm.open(nonexisting_file)
+        self.assertIn(nonexisting_file, str(cm.exception))
+        self.assertEqual(cm.exception.filename, nonexisting_file)
+
 
 if __name__ == '__main__':
     unittest.main()
index 49b88f5cccefc8ead78ff876a0c6f1de23042172..bd411da652e66eb88de5cbd616f03a4944582e9f 100644 (file)
@@ -105,6 +105,12 @@ class DbmTestCase(unittest.TestCase):
             self.assertTrue(b'key' in db)
             self.assertEqual(db[b'key'], b'value')
 
+    def test_nonexisting_file(self):
+        nonexisting_file = 'nonexisting-file'
+        with self.assertRaises(dbm.ndbm.error) as cm:
+            dbm.ndbm.open(nonexisting_file)
+        self.assertIn(nonexisting_file, str(cm.exception))
+        self.assertEqual(cm.exception.filename, nonexisting_file)
 
 
 if __name__ == '__main__':
diff --git a/Misc/NEWS.d/next/Library/2018-07-31-23-00-09.bpo-34248.5U6wwc.rst b/Misc/NEWS.d/next/Library/2018-07-31-23-00-09.bpo-34248.5U6wwc.rst
new file mode 100644 (file)
index 0000000..55215f6
--- /dev/null
@@ -0,0 +1,3 @@
+Report filename in the exception raised when the database file cannot be opened
+by :func:`dbm.gnu.open` and :func:`dbm.ndbm.open` due to OS-related error.
+Patch by Zsolt Cserna.
index 65761d83d380f71ef344ff136331b6a767c6dba9..081184a0b3224d522c216193fcd3312ccfbe6194 100644 (file)
@@ -62,7 +62,7 @@ newdbmobject(const char *file, int flags, int mode)
     dp->di_size = -1;
     /* See issue #19296 */
     if ( (dp->di_dbm = dbm_open((char *)file, flags, mode)) == 0 ) {
-        PyErr_SetFromErrno(DbmError);
+        PyErr_SetFromErrnoWithFilename(DbmError, file);
         Py_DECREF(dp);
         return NULL;
     }
index 10560040e446b3b5a39aaef9d7af3b16e2d5d80e..ceb744b99ba80c45569ca1d21cfde9009b697c55 100644 (file)
@@ -75,7 +75,7 @@ newdbmobject(const char *file, int flags, int mode)
     errno = 0;
     if ((dp->di_dbm = gdbm_open((char *)file, 0, flags, mode, NULL)) == 0) {
         if (errno != 0)
-            PyErr_SetFromErrno(DbmError);
+            PyErr_SetFromErrnoWithFilename(DbmError, file);
         else
             PyErr_SetString(DbmError, gdbm_strerror(gdbm_errno));
         Py_DECREF(dp);