Commit 9df346bf authored by Zsolt Cserna's avatar Zsolt Cserna Committed by Berker Peksag

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.
parent 59ee5b12
......@@ -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()
......@@ -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__':
......
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.
......@@ -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;
}
......
......@@ -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);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment