Debian Codegraph @

mypyc/lib-rt/random/librt_random_api.c

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