@@ -7756,6 +7756,104 @@ cfunc_noargs(PyTypeObject *t, const char *name)
77567756 return NULL ;
77577757}
77587758
7759+ PyDoc_STRVAR (libmpdec_version_info__doc__ ,
7760+ "decimal.libmpdec_version_info\n\
7761+ \n\
7762+ libmpdec version information as a named tuple." );
7763+
7764+ static PyStructSequence_Field libmpdec_version_info_fields [] = {
7765+ {"major" , "Major release number" },
7766+ {"minor" , "Minor release number" },
7767+ {"micro" , "Micro release number" },
7768+ {0 }
7769+ };
7770+
7771+ static PyStructSequence_Desc libmpdec_version_info_desc = {
7772+ "decimal.libmpdec_version_info" , /* name */
7773+ libmpdec_version_info__doc__ , /* doc */
7774+ libmpdec_version_info_fields , /* fields */
7775+ 3
7776+ };
7777+
7778+ static PyObject *
7779+ make_libmpdec_version_info (PyTypeObject * type , int major , int minor , int micro )
7780+ {
7781+ PyObject * version ;
7782+ int pos = 0 ;
7783+
7784+ version = PyStructSequence_New (type );
7785+ if (version == NULL ) {
7786+ return NULL ;
7787+ }
7788+
7789+ #define SetItem (VALUE ) \
7790+ PyStructSequence_SET_ITEM(version, pos++, VALUE); \
7791+ if (PyErr_Occurred()) { \
7792+ Py_DECREF(version); \
7793+ return NULL; \
7794+ }
7795+
7796+ SetItem (PyLong_FromLong (major ))
7797+ SetItem (PyLong_FromLong (minor ))
7798+ SetItem (PyLong_FromLong (micro ))
7799+ #undef SetItem
7800+
7801+ return version ;
7802+ }
7803+
7804+ static PyObject *
7805+ parse_libmpdec_version_info (PyTypeObject * type , const char * version )
7806+ {
7807+ int major , minor , micro ;
7808+ if (sscanf (version , "%d.%d.%d" , & major , & minor , & micro ) != 3 ) {
7809+ PyErr_Format (PyExc_RuntimeError ,
7810+ "unexpected libmpdec version string %s" , version );
7811+ return NULL ;
7812+ }
7813+ return make_libmpdec_version_info (type , major , minor , micro );
7814+ }
7815+
7816+ static int
7817+ add_version_constants (PyObject * m )
7818+ {
7819+ const char * version = mpd_version ();
7820+ if (PyModule_AddStringConstant (m , "LIBMPDEC_VERSION" , MPD_VERSION ) < 0 ) {
7821+ return -1 ;
7822+ }
7823+ PyObject * obj = PyUnicode_FromString (version );
7824+ if (obj == NULL ) {
7825+ return -1 ;
7826+ }
7827+ if (PyModule_AddObjectRef (m , "libmpdec_version" , obj ) < 0 ||
7828+ PyModule_AddObjectRef (m , "__libmpdec_version__" , obj ) < 0 )
7829+ {
7830+ Py_DECREF (obj );
7831+ return -1 ;
7832+ }
7833+ Py_DECREF (obj );
7834+ PyTypeObject * version_type ;
7835+ version_type = PyStructSequence_NewType (& libmpdec_version_info_desc );
7836+ if (version_type == NULL ) {
7837+ return -1 ;
7838+ }
7839+ if (PyModule_Add (m , "LIBMPDEC_VERSION_INFO" ,
7840+ make_libmpdec_version_info (version_type , MPD_MAJOR_VERSION ,
7841+ MPD_MINOR_VERSION ,
7842+ MPD_MICRO_VERSION )) < 0 )
7843+ {
7844+ Py_DECREF (version_type );
7845+ return -1 ;
7846+ }
7847+ if (PyModule_Add (m , "libmpdec_version_info" ,
7848+ parse_libmpdec_version_info (version_type , version )) < 0 )
7849+ {
7850+ Py_DECREF (version_type );
7851+ return -1 ;
7852+ }
7853+ Py_DECREF (version_type );
7854+ return 0 ;
7855+ }
7856+
77597857static int minalloc_is_set = 0 ;
77607858
77617859static int
@@ -8004,7 +8102,7 @@ _decimal_exec(PyObject *m)
80048102
80058103 /* Add specification version number */
80068104 CHECK_INT (PyModule_AddStringConstant (m , "SPEC_VERSION" , MPD_SPEC_VERSION ));
8007- CHECK_INT (PyModule_AddStringConstant ( m , "__libmpdec_version__" , mpd_version () ));
8105+ CHECK_INT (add_version_constants ( m ));
80088106
80098107 return 0 ;
80108108
0 commit comments