@@ -439,6 +439,87 @@ add_keyword_tuple(PyObject *module)
439439#endif
440440}
441441
442+ PyDoc_STRVAR (sqlite_version_info__doc__ ,
443+ "_sqlite3.sqlite_version_info\n\
444+ \n\
445+ SQLite version information as a named tuple." );
446+
447+ static PyStructSequence_Field sqlite_version_info_fields [] = {
448+ {"major" , "Major release number" },
449+ {"minor" , "Minor release number" },
450+ {"patch" , "Patch release number" },
451+ {0 }
452+ };
453+
454+ static PyStructSequence_Desc sqlite_version_info_desc = {
455+ "_sqlite3.sqlite_version_info" , /* name */
456+ sqlite_version_info__doc__ , /* doc */
457+ sqlite_version_info_fields , /* fields */
458+ 3
459+ };
460+
461+ static PyObject *
462+ make_sqlite_version_info (PyTypeObject * type , int number )
463+ {
464+ PyObject * version ;
465+ int pos = 0 ;
466+ int major = number / 1000000 ;
467+ int minor = (number % 1000000 ) / 1000 ;
468+ int patch = number % 1000 ;
469+
470+ version = PyStructSequence_New (type );
471+ if (version == NULL ) {
472+ return NULL ;
473+ }
474+
475+ #define SetItem (VALUE ) \
476+ PyStructSequence_SET_ITEM(version, pos++, VALUE); \
477+ if (PyErr_Occurred()) { \
478+ Py_DECREF(version); \
479+ return NULL; \
480+ }
481+
482+ SetItem (PyLong_FromLong (major ))
483+ SetItem (PyLong_FromLong (minor ))
484+ SetItem (PyLong_FromLong (patch ))
485+ #undef SetItem
486+
487+ return version ;
488+ }
489+
490+ static int
491+ add_version_constants (PyObject * module )
492+ {
493+ if (PyModule_AddStringMacro (module , SQLITE_VERSION ) < 0 ) {
494+ return -1 ;
495+ }
496+ if (PyModule_AddStringConstant (module , "sqlite_version" ,
497+ sqlite3_libversion ()) < 0 )
498+ {
499+ return -1 ;
500+ }
501+ PyTypeObject * version_type ;
502+ version_type = PyStructSequence_NewType (& sqlite_version_info_desc );
503+ if (version_type == NULL ) {
504+ return -1 ;
505+ }
506+ if (PyModule_Add (module , "SQLITE_VERSION_INFO" ,
507+ make_sqlite_version_info (version_type , SQLITE_VERSION_NUMBER )) < 0 )
508+ {
509+ Py_DECREF (version_type );
510+ return -1 ;
511+ }
512+ if (PyModule_Add (module , "sqlite_version_info" ,
513+ make_sqlite_version_info (version_type ,
514+ sqlite3_libversion_number ())) < 0 )
515+ {
516+ Py_DECREF (version_type );
517+ return -1 ;
518+ }
519+ Py_DECREF (version_type );
520+ return 0 ;
521+ }
522+
442523static int
443524add_integer_constants (PyObject * module ) {
444525#define ADD_INT (ival ) \
@@ -741,7 +822,7 @@ module_exec(PyObject *module)
741822 goto error ;
742823 }
743824
744- if (PyModule_AddStringConstant (module , "sqlite_version" , sqlite3_libversion ()) ) {
825+ if (add_version_constants (module ) < 0 ) {
745826 goto error ;
746827 }
747828
0 commit comments