Skip to content

Commit 00f9edb

Browse files
authored
bpo-33901: Add _gdbm._GDBM_VERSION (GH-7794)
* Fix also PyInit__gdbm() to catch errors. * test.pythoninfo: add gdbm.version * test_dbm_gnu now logs GDBM_VERSION when run in verbose mode. * pythoninfo: rename function to collect_gdbm()
1 parent 06fe77a commit 00f9edb

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

‎Lib/test/pythoninfo.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,15 @@ def collect_cc(info_add):
525525
info_add('CC.version', text)
526526

527527

528+
def collect_gdbm(info_add):
529+
try:
530+
import _gdbm
531+
except ImportError:
532+
return
533+
534+
info_add('gdbm.GDBM_VERSION', '.'.join(map(str, _gdbm._GDBM_VERSION)))
535+
536+
528537
def collect_info(info):
529538
error = False
530539
info_add = info.add
@@ -552,6 +561,7 @@ def collect_info(info):
552561
collect_testcapi,
553562
collect_resource,
554563
collect_cc,
564+
collect_gdbm,
555565

556566
# Collecting from tests should be last as they have side effects.
557567
collect_test_socket,

‎Lib/test/test_dbm_gnu.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88
filename = TESTFN
99

1010
class TestGdbm(unittest.TestCase):
11+
@staticmethod
12+
def setUpClass():
13+
if support.verbose:
14+
try:
15+
import _gdbm
16+
version = _gdbm._GDBM_VERSION
17+
except (ImportError, AttributeError):
18+
pass
19+
else:
20+
print(f"gdbm version: {version}")
21+
1122
def setUp(self):
1223
self.g = None
1324

‎Modules/_gdbmmodule.c‎

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -654,20 +654,43 @@ static struct PyModuleDef _gdbmmodule = {
654654

655655
PyMODINIT_FUNC
656656
PyInit__gdbm(void) {
657-
PyObject *m, *d, *s;
657+
PyObject *m;
658658

659659
if (PyType_Ready(&Dbmtype) < 0)
660660
return NULL;
661661
m = PyModule_Create(&_gdbmmodule);
662-
if (m == NULL)
662+
if (m == NULL) {
663663
return NULL;
664-
d = PyModule_GetDict(m);
664+
}
665+
665666
DbmError = PyErr_NewException("_gdbm.error", PyExc_OSError, NULL);
666-
if (DbmError != NULL) {
667-
PyDict_SetItemString(d, "error", DbmError);
668-
s = PyUnicode_FromString(dbmmodule_open_flags);
669-
PyDict_SetItemString(d, "open_flags", s);
670-
Py_DECREF(s);
667+
if (DbmError == NULL) {
668+
goto error;
669+
}
670+
Py_INCREF(DbmError);
671+
if (PyModule_AddObject(m, "error", DbmError) < 0) {
672+
Py_DECREF(DbmError);
673+
goto error;
674+
}
675+
676+
if (PyModule_AddStringConstant(m, "open_flags",
677+
dbmmodule_open_flags) < 0) {
678+
goto error;
671679
}
680+
681+
PyObject *obj = Py_BuildValue("iii", GDBM_VERSION_MAJOR,
682+
GDBM_VERSION_MINOR, GDBM_VERSION_PATCH);
683+
if (obj == NULL) {
684+
goto error;
685+
}
686+
if (PyModule_AddObject(m, "_GDBM_VERSION", obj) < 0) {
687+
Py_DECREF(obj);
688+
goto error;
689+
}
690+
672691
return m;
692+
693+
error:
694+
Py_DECREF(m);
695+
return NULL;
673696
}

0 commit comments

Comments
 (0)