| 1 |
#include "librt_strings_api.h" |
| 2 |
|
| 3 |
void *LibRTStrings_API[LIBRT_STRINGS_API_LEN] = {0}; |
| 4 |
|
| 5 |
int |
| 6 |
import_librt_strings(void) |
| 7 |
{ |
| 8 |
PyObject *mod = PyImport_ImportModule("librt.strings"); |
| 9 |
if (mod == NULL) |
| 10 |
return -1; |
| 11 |
Py_DECREF(mod); // we import just for the side effect of making the below work. |
| 12 |
void **capsule = (void **)PyCapsule_Import("librt.strings._C_API", 0); |
| 13 |
if (capsule == NULL) |
| 14 |
return -1; |
| 15 |
|
| 16 |
// Only after version validation succeeds can we safely copy the full table. |
| 17 |
int (*abi_version)(void) = (int (*)(void))capsule[0]; |
| 18 |
int (*api_version)(void) = (int (*)(void))capsule[1]; |
| 19 |
if (abi_version() != LIBRT_STRINGS_ABI_VERSION) { |
| 20 |
char err[128]; |
| 21 |
snprintf(err, sizeof(err), "ABI version conflict for librt.strings, expected %d, found %d", |
| 22 |
LIBRT_STRINGS_ABI_VERSION, |
| 23 |
abi_version() |
| 24 |
); |
| 25 |
PyErr_SetString(PyExc_ValueError, err); |
| 26 |
return -1; |
| 27 |
} |
| 28 |
if (api_version() < LIBRT_STRINGS_API_VERSION) { |
| 29 |
char err[128]; |
| 30 |
snprintf(err, sizeof(err), |
| 31 |
"API version conflict for librt.strings, expected %d or newer, found %d (hint: upgrade librt)", |
| 32 |
LIBRT_STRINGS_API_VERSION, |
| 33 |
api_version() |
| 34 |
); |
| 35 |
PyErr_SetString(PyExc_ValueError, err); |
| 36 |
return -1; |
| 37 |
} |
| 38 |
// Provider API version is >= our expected version, which (by the API |
| 39 |
// compatibility contract) means it has at least LIBRT_STRINGS_API_LEN |
| 40 |
// entries, so this copy is safe. |
| 41 |
memcpy(LibRTStrings_API, capsule, sizeof(LibRTStrings_API)); |
| 42 |
return 0; |
| 43 |
} |