Debian Codegraph @

mypyc/lib-rt/internal/librt_internal_api.c

raw
1 #include "librt_internal_api.h"
2
3 void *NativeInternal_API[LIBRT_INTERNAL_API_LEN] = {0};
4
5 int
6 import_librt_internal(void)
7 {
8 PyObject *mod = PyImport_ImportModule("librt.internal");
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.internal._C_API", 0);
13 if (capsule == NULL)
14 return -1;
15
16 // Each librt capsule gives at least 20 entries. Validate version before copying the table.
17 int (*abi_version)(void) = (int (*)(void))capsule[13];
18 if (abi_version() != LIBRT_INTERNAL_ABI_VERSION) {
19 char err[128];
20 snprintf(err, sizeof(err), "ABI version conflict for librt.internal, expected %d, found %d",
21 LIBRT_INTERNAL_ABI_VERSION,
22 abi_version()
23 );
24 PyErr_SetString(PyExc_ValueError, err);
25 return -1;
26 }
27 int (*api_version)(void) = (int (*)(void))capsule[19];
28 if (api_version() < LIBRT_INTERNAL_API_VERSION) {
29 char err[128];
30 snprintf(err, sizeof(err),
31 "API version conflict for librt.internal, expected %d or newer, found %d (hint: upgrade librt)",
32 LIBRT_INTERNAL_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_INTERNAL_API_LEN
40 // entries, so this copy is safe.
41 memcpy(NativeInternal_API, capsule, sizeof(NativeInternal_API));
42 return 0;
43 }