Debian Codegraph @

mypyc/lib-rt/time/librt_time_api.c

raw
1 #include "librt_time_api.h"
2
3 void *LibRTTime_API[LIBRT_TIME_API_LEN] = {0};
4
5 int
6 import_librt_time(void)
7 {
8 PyObject *mod = PyImport_ImportModule("librt.time");
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.time._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_TIME_ABI_VERSION) {
20 char err[128];
21 snprintf(err, sizeof(err), "ABI version conflict for librt.time, expected %d, found %d",
22 LIBRT_TIME_ABI_VERSION,
23 abi_version()
24 );
25 PyErr_SetString(PyExc_ValueError, err);
26 return -1;
27 }
28 if (api_version() < LIBRT_TIME_API_VERSION) {
29 char err[128];
30 snprintf(err, sizeof(err),
31 "API version conflict for librt.time, expected %d or newer, found %d (hint: upgrade librt)",
32 LIBRT_TIME_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_TIME_API_LEN
40 // entries, so this copy is safe.
41 memcpy(LibRTTime_API, capsule, sizeof(LibRTTime_API));
42 return 0;
43 }