2828 static const char which_dbm [] = "GNU gdbm" ;
2929#elif defined(USE_NDBM )
3030 #include <ndbm.h>
31+ #ifdef _GDBM_H_
32+ /* ndbm.h is the GDBM compatibility interface. */
3133 static const char which_dbm [] = "GNU gdbm" ;
34+ #else
35+ static const char which_dbm [] = "ndbm" ;
36+ #endif
3237#elif defined(USE_BERKDB )
3338 #ifndef DB_DBM_HSEARCH
3439 #define DB_DBM_HSEARCH 1
@@ -634,6 +639,136 @@ static PyMethodDef dbmmodule_methods[] = {
634639 { 0 , 0 },
635640};
636641
642+ #if defined(GDBM_VERSION_MAJOR ) || defined(DB_VERSION_MAJOR )
643+ static PyStructSequence_Field version_info_fields [] = {
644+ {"major" , "Major release number" },
645+ {"minor" , "Minor release number" },
646+ {"patch" , "Patch release number" },
647+ {0 }
648+ };
649+
650+ static PyObject *
651+ make_version_info (PyTypeObject * type , int major , int minor , int patch )
652+ {
653+ PyObject * version ;
654+ int pos = 0 ;
655+
656+ version = PyStructSequence_New (type );
657+ if (version == NULL ) {
658+ return NULL ;
659+ }
660+
661+ #define SetItem (VALUE ) \
662+ PyStructSequence_SET_ITEM(version, pos++, VALUE); \
663+ if (PyErr_Occurred()) { \
664+ Py_DECREF(version); \
665+ return NULL; \
666+ }
667+
668+ SetItem (PyLong_FromLong (major ))
669+ SetItem (PyLong_FromLong (minor ))
670+ SetItem (PyLong_FromLong (patch ))
671+ #undef SetItem
672+
673+ return version ;
674+ }
675+ #endif
676+
677+ #if defined(GDBM_VERSION_MAJOR )
678+ PyDoc_STRVAR (gdbm_version_info__doc__ ,
679+ "_dbm.gdbm_version_info\n\
680+ \n\
681+ GDBM version information as a named tuple." );
682+
683+ static PyStructSequence_Desc gdbm_version_info_desc = {
684+ "_dbm.gdbm_version_info" , /* name */
685+ gdbm_version_info__doc__ , /* doc */
686+ version_info_fields , /* fields */
687+ 3
688+ };
689+
690+ static int
691+ add_version_constants (PyObject * module )
692+ {
693+ if (PyModule_AddStringConstant (module , "gdbm_version" , gdbm_version ) < 0 ) {
694+ return -1 ;
695+ }
696+ PyTypeObject * version_type ;
697+ version_type = PyStructSequence_NewType (& gdbm_version_info_desc );
698+ if (version_type == NULL ) {
699+ return -1 ;
700+ }
701+ if (PyModule_Add (module , "GDBM_VERSION_INFO" ,
702+ make_version_info (version_type , GDBM_VERSION_MAJOR ,
703+ GDBM_VERSION_MINOR , GDBM_VERSION_PATCH )) < 0 )
704+ {
705+ Py_DECREF (version_type );
706+ return -1 ;
707+ }
708+ if (PyModule_Add (module , "gdbm_version_info" ,
709+ make_version_info (version_type , gdbm_version_number [0 ],
710+ gdbm_version_number [1 ],
711+ gdbm_version_number [2 ])) < 0 )
712+ {
713+ Py_DECREF (version_type );
714+ return -1 ;
715+ }
716+ Py_DECREF (version_type );
717+ return 0 ;
718+ }
719+ #elif defined(DB_VERSION_MAJOR )
720+ PyDoc_STRVAR (bdb_version_info__doc__ ,
721+ "_dbm.bdb_version_info\n\
722+ \n\
723+ Berkeley DB version information as a named tuple." );
724+
725+ static PyStructSequence_Desc bdb_version_info_desc = {
726+ "_dbm.bdb_version_info" , /* name */
727+ bdb_version_info__doc__ , /* doc */
728+ version_info_fields , /* fields */
729+ 3
730+ };
731+
732+ static int
733+ add_version_constants (PyObject * module )
734+ {
735+ int major , minor , patch ;
736+ const char * version = db_version (& major , & minor , & patch );
737+ if (PyModule_AddStringConstant (module , "BDB_VERSION" , DB_VERSION_STRING ) < 0 ) {
738+ return -1 ;
739+ }
740+ if (PyModule_AddStringConstant (module , "bdb_version" , version ) < 0 ) {
741+ return -1 ;
742+ }
743+ PyTypeObject * version_type ;
744+ version_type = PyStructSequence_NewType (& bdb_version_info_desc );
745+ if (version_type == NULL ) {
746+ return -1 ;
747+ }
748+ if (PyModule_Add (module , "BDB_VERSION_INFO" ,
749+ make_version_info (version_type , DB_VERSION_MAJOR ,
750+ DB_VERSION_MINOR , DB_VERSION_PATCH )) < 0 )
751+ {
752+ Py_DECREF (version_type );
753+ return -1 ;
754+ }
755+ if (PyModule_Add (module , "bdb_version_info" ,
756+ make_version_info (version_type , major , minor , patch )) < 0 )
757+ {
758+ Py_DECREF (version_type );
759+ return -1 ;
760+ }
761+ Py_DECREF (version_type );
762+ return 0 ;
763+ }
764+ #else
765+ static int
766+ add_version_constants (PyObject * module )
767+ {
768+ return 0 ;
769+ }
770+ #endif
771+
637772static int
638773_dbm_exec (PyObject * module )
639774{
@@ -650,6 +785,9 @@ _dbm_exec(PyObject *module)
650785 if (PyModule_AddStringConstant (module , "library" , which_dbm ) < 0 ) {
651786 return -1 ;
652787 }
788+ if (add_version_constants (module ) < 0 ) {
789+ return -1 ;
790+ }
653791 if (PyModule_AddType (module , (PyTypeObject * )state -> dbm_error ) < 0 ) {
654792 return -1 ;
655793 }
0 commit comments