Skip to content

Commit 32c8cca

Browse files
gh-144069: Fix cleanup in _dbm deallocation
1 parent 8bdd53c commit 32c8cca

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

Lib/test/test_dbm.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ def test_error(self):
7171
def test_anydbm_not_existing(self):
7272
self.assertRaises(dbm.error, dbm.open, _fname)
7373

74+
def test_open_nonexistent_directory(self):
75+
missing_dir = os.path.join(dirname + "_does_not_exist", "test.db")
76+
with self.assertRaises(OSError):
77+
dbm.open(missing_dir, "c")
78+
7479
def test_anydbm_creation(self):
7580
f = dbm.open(_fname, 'c')
7681
self.assertEqual(list(f.keys()), [])
@@ -244,10 +249,6 @@ def setUp(self):
244249
self.addCleanup(cleaunup_test_dir)
245250
setup_test_dir()
246251

247-
def test_open_nonexistent_directory(self):
248-
missing_dir = os.path.join(dirname + "_does_not_exist", "test.db")
249-
with self.assertRaises(OSError):
250-
dbm.open(missing_dir, "c")
251252

252253
class WhichDBTestCase(unittest.TestCase):
253254
def test_whichdb(self):

Modules/_dbmmodule.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@ newdbmobject(_dbm_state *state, const char *file, int flags, int mode)
9191
/* See issue #19296 */
9292
if ( (dp->di_dbm = dbm_open((char *)file, flags, mode)) == NULL ) {
9393
PyErr_SetFromErrnoWithFilename(state->dbm_error, file);
94-
if (dp->di_dbm != NULL) {
95-
dbm_close(dp->di_dbm);
96-
dp->di_dbm = NULL;
97-
}
9894
Py_DECREF(dp);
9995
return NULL;
10096
}
@@ -110,6 +106,7 @@ dbm_dealloc(PyObject *self)
110106
PyObject_GC_UnTrack(dp);
111107
if (dp->di_dbm) {
112108
dbm_close(dp->di_dbm);
109+
dp->di_dbm = NULL;
113110
}
114111
PyTypeObject *tp = Py_TYPE(dp);
115112
tp->tp_free(dp);

0 commit comments

Comments
 (0)