@@ -6386,6 +6386,89 @@ _ctypes_add_objects(PyObject *mod)
63866386}
63876387
63886388
6389+ #ifdef FFI_VERSION_NUMBER
6390+ PyDoc_STRVAR (libffi_version_info__doc__ ,
6391+ "ctypes.libffi_version_info\n\
6392+ \n\
6393+ libffi version information as a named tuple." );
6394+
6395+ static PyStructSequence_Field libffi_version_info_fields [] = {
6396+ {"major" , "Major release number" },
6397+ {"minor" , "Minor release number" },
6398+ {"patch" , "Patch release number" },
6399+ {0 }
6400+ };
6401+
6402+ static PyStructSequence_Desc libffi_version_info_desc = {
6403+ "ctypes.libffi_version_info" , /* name */
6404+ libffi_version_info__doc__ , /* doc */
6405+ libffi_version_info_fields , /* fields */
6406+ 3
6407+ };
6408+
6409+ static PyObject *
6410+ make_libffi_version_info (PyTypeObject * type , unsigned long number )
6411+ {
6412+ PyObject * version ;
6413+ int pos = 0 ;
6414+ unsigned long major = number / 10000 ;
6415+ unsigned long minor = (number % 10000 ) / 100 ;
6416+ unsigned long patch = number % 100 ;
6417+
6418+ version = PyStructSequence_New (type );
6419+ if (version == NULL ) {
6420+ return NULL ;
6421+ }
6422+
6423+ #define SetItem (VALUE ) \
6424+ PyStructSequence_SET_ITEM(version, pos++, VALUE); \
6425+ if (PyErr_Occurred()) { \
6426+ Py_DECREF(version); \
6427+ return NULL; \
6428+ }
6429+
6430+ SetItem (PyLong_FromUnsignedLong (major ))
6431+ SetItem (PyLong_FromUnsignedLong (minor ))
6432+ SetItem (PyLong_FromUnsignedLong (patch ))
6433+ #undef SetItem
6434+
6435+ return version ;
6436+ }
6437+
6438+ static int
6439+ _ctypes_add_version_constants (PyObject * mod )
6440+ {
6441+ if (PyModule_AddStringConstant (mod , "LIBFFI_VERSION" ,
6442+ FFI_VERSION_STRING ) < 0 ) {
6443+ return -1 ;
6444+ }
6445+ if (PyModule_AddStringConstant (mod , "libffi_version" ,
6446+ ffi_get_version ()) < 0 ) {
6447+ return -1 ;
6448+ }
6449+ PyTypeObject * version_type ;
6450+ version_type = PyStructSequence_NewType (& libffi_version_info_desc );
6451+ if (version_type == NULL ) {
6452+ return -1 ;
6453+ }
6454+ if (PyModule_Add (mod , "LIBFFI_VERSION_INFO" ,
6455+ make_libffi_version_info (version_type , FFI_VERSION_NUMBER )) < 0 )
6456+ {
6457+ Py_DECREF (version_type );
6458+ return -1 ;
6459+ }
6460+ if (PyModule_Add (mod , "libffi_version_info" ,
6461+ make_libffi_version_info (version_type ,
6462+ ffi_get_version_number ())) < 0 )
6463+ {
6464+ Py_DECREF (version_type );
6465+ return -1 ;
6466+ }
6467+ Py_DECREF (version_type );
6468+ return 0 ;
6469+ }
6470+ #endif
6471+
63896472static int
63906473_ctypes_mod_exec (PyObject * mod )
63916474{
@@ -6440,6 +6523,11 @@ _ctypes_mod_exec(PyObject *mod)
64406523 if (_ctypes_add_objects (mod ) < 0 ) {
64416524 return -1 ;
64426525 }
6526+ #ifdef FFI_VERSION_NUMBER
6527+ if (_ctypes_add_version_constants (mod ) < 0 ) {
6528+ return -1 ;
6529+ }
6530+ #endif
64436531 return 0 ;
64446532}
64456533
0 commit comments