]> granicus.if.org Git - python/commitdiff
bpo-33901: Add _gdbm._GDBM_VERSION (GH-7794)
authorVictor Stinner <vstinner@redhat.com>
Tue, 19 Jun 2018 21:29:22 +0000 (23:29 +0200)
committerGitHub <noreply@github.com>
Tue, 19 Jun 2018 21:29:22 +0000 (23:29 +0200)
* 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()

Lib/test/pythoninfo.py
Lib/test/test_dbm_gnu.py
Modules/_gdbmmodule.c

index 9242a36bedd68b2447d40da3b5f6eec51cf1b1ab..3958764a6ae94173b8adbc9ee7d763682f994790 100644 (file)
@@ -525,6 +525,15 @@ def collect_cc(info_add):
     info_add('CC.version', text)
 
 
+def collect_gdbm(info_add):
+    try:
+        import _gdbm
+    except ImportError:
+        return
+
+    info_add('gdbm.GDBM_VERSION', '.'.join(map(str, _gdbm._GDBM_VERSION)))
+
+
 def collect_info(info):
     error = False
     info_add = info.add
@@ -552,6 +561,7 @@ def collect_info(info):
         collect_testcapi,
         collect_resource,
         collect_cc,
+        collect_gdbm,
 
         # Collecting from tests should be last as they have side effects.
         collect_test_socket,
index 379601ad86deb283a0c97a5298bb55f096708a77..49f91705b5b8ec4190720538d1aa236d2f81cb23 100644 (file)
@@ -8,6 +8,17 @@ from test.support import TESTFN, TESTFN_NONASCII, unlink
 filename = TESTFN
 
 class TestGdbm(unittest.TestCase):
+    @staticmethod
+    def setUpClass():
+        if support.verbose:
+            try:
+                import _gdbm
+                version = _gdbm._GDBM_VERSION
+            except (ImportError, AttributeError):
+                pass
+            else:
+                print(f"gdbm version: {version}")
+
     def setUp(self):
         self.g = None
 
index 9996d8c26fb7d106cf8bfd0dece4687d7af254fb..a68cf029429ab5d9c62b4bf26ea00787489a1b40 100644 (file)
@@ -654,20 +654,43 @@ static struct PyModuleDef _gdbmmodule = {
 
 PyMODINIT_FUNC
 PyInit__gdbm(void) {
-    PyObject *m, *d, *s;
+    PyObject *m;
 
     if (PyType_Ready(&Dbmtype) < 0)
             return NULL;
     m = PyModule_Create(&_gdbmmodule);
-    if (m == NULL)
+    if (m == NULL) {
         return NULL;
-    d = PyModule_GetDict(m);
+    }
+
     DbmError = PyErr_NewException("_gdbm.error", PyExc_OSError, NULL);
-    if (DbmError != NULL) {
-        PyDict_SetItemString(d, "error", DbmError);
-        s = PyUnicode_FromString(dbmmodule_open_flags);
-        PyDict_SetItemString(d, "open_flags", s);
-        Py_DECREF(s);
+    if (DbmError == NULL) {
+        goto error;
+    }
+    Py_INCREF(DbmError);
+    if (PyModule_AddObject(m, "error", DbmError) < 0) {
+        Py_DECREF(DbmError);
+        goto error;
+    }
+
+    if (PyModule_AddStringConstant(m, "open_flags",
+                                   dbmmodule_open_flags) < 0) {
+        goto error;
     }
+
+    PyObject *obj = Py_BuildValue("iii", GDBM_VERSION_MAJOR,
+                                  GDBM_VERSION_MINOR, GDBM_VERSION_PATCH);
+    if (obj == NULL) {
+        goto error;
+    }
+    if (PyModule_AddObject(m, "_GDBM_VERSION", obj) < 0) {
+        Py_DECREF(obj);
+        goto error;
+    }
+
     return m;
+
+error:
+    Py_DECREF(m);
+    return NULL;
 }