11#ifndef LIBRT_INTERNAL_H
22#define LIBRT_INTERNAL_H
33
4- #define LIBRT_INTERNAL_ABI_VERSION 1
5- #define LIBRT_INTERNAL_API_LEN 19
4+ // ABI version -- only an exact match is compatible. This will only be changed in
5+ // very exceptional cases (likely never) due to strict backward compatibility
6+ // requirements.
7+ #define LIBRT_INTERNAL_ABI_VERSION 2
8+
9+ // API version -- more recent versions must maintain backward compatibility, i.e.
10+ // we can add new features but not remove or change existing features (unless
11+ // ABI version is changed, but see the comment above).
12+ #define LIBRT_INTERNAL_API_VERSION 0
13+
14+ // Number of functions in the capsule API. If you add a new function, also increase
15+ // LIBRT_INTERNAL_API_VERSION.
16+ #define LIBRT_INTERNAL_API_LEN 20
617
718#ifdef LIBRT_INTERNAL_MODULE
819
@@ -27,6 +38,7 @@ static PyObject *read_bytes_internal(PyObject *data);
2738static uint8_t cache_version_internal (void );
2839static PyTypeObject * ReadBuffer_type_internal (void );
2940static PyTypeObject * WriteBuffer_type_internal (void );
41+ static int NativeInternal_API_Version (void );
3042
3143#else
3244
@@ -51,6 +63,7 @@ static void *NativeInternal_API[LIBRT_INTERNAL_API_LEN];
5163#define cache_version_internal (*(uint8_t (*)(void)) NativeInternal_API[16])
5264#define ReadBuffer_type_internal (*(PyTypeObject* (*)(void)) NativeInternal_API[17])
5365#define WriteBuffer_type_internal (*(PyTypeObject* (*)(void)) NativeInternal_API[18])
66+ #define NativeInternal_API_Version (*(int (*)(void)) NativeInternal_API[19])
5467
5568static int
5669import_librt_internal (void )
@@ -64,7 +77,22 @@ import_librt_internal(void)
6477 return -1 ;
6578 memcpy (NativeInternal_API , capsule , sizeof (NativeInternal_API ));
6679 if (NativeInternal_ABI_Version () != LIBRT_INTERNAL_ABI_VERSION ) {
67- PyErr_SetString (PyExc_ValueError , "ABI version conflict for librt.internal" );
80+ char err [128 ];
81+ snprintf (err , sizeof (err ), "ABI version conflict for librt.internal, expected %d, found %d" ,
82+ LIBRT_INTERNAL_ABI_VERSION ,
83+ NativeInternal_ABI_Version ()
84+ );
85+ PyErr_SetString (PyExc_ValueError , err );
86+ return -1 ;
87+ }
88+ if (NativeInternal_API_Version () < LIBRT_INTERNAL_API_VERSION ) {
89+ char err [128 ];
90+ snprintf (err , sizeof (err ),
91+ "API version conflict for librt.internal, expected %d or newer, found %d (hint: upgrade librt)" ,
92+ LIBRT_INTERNAL_API_VERSION ,
93+ NativeInternal_API_Version ()
94+ );
95+ PyErr_SetString (PyExc_ValueError , err );
6896 return -1 ;
6997 }
7098 return 0 ;
0 commit comments