[Python-checkins] bpo-34248: Add filename to error raised in {gnu, ndbm}.open() (GH-8590)

Berker Peksag webhook-mailer at python.org
Thu Sep 27 15:54:38 EDT 2018


https://github.com/python/cpython/commit/9df346bf98069a87de14a3c2f69009d800994c63
commit: 9df346bf98069a87de14a3c2f69009d800994c63
branch: master
author: Zsolt Cserna <cserna.zsolt at gmail.com>
committer: Berker Peksag <berker.peksag at gmail.com>
date: 2018-09-27T22:54:34+03:00
summary:

bpo-34248: Add filename to error raised in {gnu,ndbm}.open() (GH-8590)

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.

files:
A Misc/NEWS.d/next/Library/2018-07-31-23-00-09.bpo-34248.5U6wwc.rst
M Lib/test/test_dbm_gnu.py
M Lib/test/test_dbm_ndbm.py
M Modules/_dbmmodule.c
M Modules/_gdbmmodule.c

diff --git a/Lib/test/test_dbm_gnu.py b/Lib/test/test_dbm_gnu.py
index c96eff5a319e..16b7fe6fac17 100644
--- a/Lib/test/test_dbm_gnu.py
+++ b/Lib/test/test_dbm_gnu.py
@@ -144,6 +144,13 @@ def test_nonascii_filename(self):
             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()
diff --git a/Lib/test/test_dbm_ndbm.py b/Lib/test/test_dbm_ndbm.py
index 49b88f5cccef..bd411da652e6 100644
--- a/Lib/test/test_dbm_ndbm.py
+++ b/Lib/test/test_dbm_ndbm.py
@@ -105,6 +105,12 @@ def test_nonascii_filename(self):
             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
index 000000000000..55215f60550b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-07-31-23-00-09.bpo-34248.5U6wwc.rst
@@ -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.
diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c
index 65761d83d380..081184a0b322 100644
--- a/Modules/_dbmmodule.c
+++ b/Modules/_dbmmodule.c
@@ -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;
     }
diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c
index 10560040e446..ceb744b99ba8 100644
--- a/Modules/_gdbmmodule.c
+++ b/Modules/_gdbmmodule.c
@@ -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);



More information about the Python-checkins mailing list