@@ -7744,6 +7744,104 @@ cfunc_noargs(PyTypeObject *t, const char *name)
77447744 return NULL ;
77457745}
77467746
7747+ PyDoc_STRVAR (libmpdec_version_info__doc__ ,
7748+ "decimal.libmpdec_version_info\n\
7749+ \n\
7750+ libmpdec version information as a named tuple." );
7751+
7752+ static PyStructSequence_Field libmpdec_version_info_fields [] = {
7753+ {"major" , "Major release number" },
7754+ {"minor" , "Minor release number" },
7755+ {"micro" , "Micro release number" },
7756+ {0 }
7757+ };
7758+
7759+ static PyStructSequence_Desc libmpdec_version_info_desc = {
7760+ "decimal.libmpdec_version_info" , /* name */
7761+ libmpdec_version_info__doc__ , /* doc */
7762+ libmpdec_version_info_fields , /* fields */
7763+ 3
7764+ };
7765+
7766+ static PyObject *
7767+ make_libmpdec_version_info (PyTypeObject * type , int major , int minor , int micro )
7768+ {
7769+ PyObject * version ;
7770+ int pos = 0 ;
7771+
7772+ version = PyStructSequence_New (type );
7773+ if (version == NULL ) {
7774+ return NULL ;
7775+ }
7776+
7777+ #define SetItem (VALUE ) \
7778+ PyStructSequence_SET_ITEM(version, pos++, VALUE); \
7779+ if (PyErr_Occurred()) { \
7780+ Py_DECREF(version); \
7781+ return NULL; \
7782+ }
7783+
7784+ SetItem (PyLong_FromLong (major ))
7785+ SetItem (PyLong_FromLong (minor ))
7786+ SetItem (PyLong_FromLong (micro ))
7787+ #undef SetItem
7788+
7789+ return version ;
7790+ }
7791+
7792+ static PyObject *
7793+ parse_libmpdec_version_info (PyTypeObject * type , const char * version )
7794+ {
7795+ int major , minor , micro ;
7796+ if (sscanf (version , "%d.%d.%d" , & major , & minor , & micro ) != 3 ) {
7797+ PyErr_Format (PyExc_RuntimeError ,
7798+ "unexpected libmpdec version string %s" , version );
7799+ return NULL ;
7800+ }
7801+ return make_libmpdec_version_info (type , major , minor , micro );
7802+ }
7803+
7804+ static int
7805+ add_version_constants (PyObject * m )
7806+ {
7807+ const char * version = mpd_version ();
7808+ if (PyModule_AddStringConstant (m , "LIBMPDEC_VERSION" , MPD_VERSION ) < 0 ) {
7809+ return -1 ;
7810+ }
7811+ PyObject * obj = PyUnicode_FromString (version );
7812+ if (obj == NULL ) {
7813+ return -1 ;
7814+ }
7815+ if (PyModule_AddObjectRef (m , "libmpdec_version" , obj ) < 0 ||
7816+ PyModule_AddObjectRef (m , "__libmpdec_version__" , obj ) < 0 )
7817+ {
7818+ Py_DECREF (obj );
7819+ return -1 ;
7820+ }
7821+ Py_DECREF (obj );
7822+ PyTypeObject * version_type ;
7823+ version_type = PyStructSequence_NewType (& libmpdec_version_info_desc );
7824+ if (version_type == NULL ) {
7825+ return -1 ;
7826+ }
7827+ if (PyModule_Add (m , "LIBMPDEC_VERSION_INFO" ,
7828+ make_libmpdec_version_info (version_type , MPD_MAJOR_VERSION ,
7829+ MPD_MINOR_VERSION ,
7830+ MPD_MICRO_VERSION )) < 0 )
7831+ {
7832+ Py_DECREF (version_type );
7833+ return -1 ;
7834+ }
7835+ if (PyModule_Add (m , "libmpdec_version_info" ,
7836+ parse_libmpdec_version_info (version_type , version )) < 0 )
7837+ {
7838+ Py_DECREF (version_type );
7839+ return -1 ;
7840+ }
7841+ Py_DECREF (version_type );
7842+ return 0 ;
7843+ }
7844+
77477845static int minalloc_is_set = 0 ;
77487846
77497847static int
@@ -7992,7 +8090,7 @@ _decimal_exec(PyObject *m)
79928090
79938091 /* Add specification version number */
79948092 CHECK_INT (PyModule_AddStringConstant (m , "SPEC_VERSION" , MPD_SPEC_VERSION ));
7995- CHECK_INT (PyModule_AddStringConstant ( m , "__libmpdec_version__" , mpd_version () ));
8093+ CHECK_INT (add_version_constants ( m ));
79968094
79978095 return 0 ;
79988096
0 commit comments