• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define PY_SSIZE_T_CLEAN
2 
3 #include "Python.h"
4 #ifdef HAVE_UUID_UUID_H
5 #include <uuid/uuid.h>
6 #endif
7 #ifdef HAVE_UUID_H
8 #include <uuid.h>
9 #endif
10 
11 
12 static PyObject *
py_uuid_generate_time_safe(void)13 py_uuid_generate_time_safe(void)
14 {
15     uuid_t uuid;
16 #ifdef HAVE_UUID_GENERATE_TIME_SAFE
17     int res;
18 
19     res = uuid_generate_time_safe(uuid);
20     return Py_BuildValue("y#i", (const char *) uuid, sizeof(uuid), res);
21 #elif defined(HAVE_UUID_CREATE)
22     uint32_t status;
23     uuid_create(&uuid, &status);
24 # if defined(HAVE_UUID_ENC_BE)
25     unsigned char buf[sizeof(uuid)];
26     uuid_enc_be(buf, &uuid);
27     return Py_BuildValue("y#i", buf, sizeof(uuid), (int) status);
28 # else
29     return Py_BuildValue("y#i", (const char *) &uuid, sizeof(uuid), (int) status);
30 # endif
31 #else
32     uuid_generate_time(uuid);
33     return Py_BuildValue("y#O", (const char *) uuid, sizeof(uuid), Py_None);
34 #endif
35 }
36 
37 
38 static PyMethodDef uuid_methods[] = {
39     {"generate_time_safe", (PyCFunction) py_uuid_generate_time_safe, METH_NOARGS, NULL},
40     {NULL, NULL, 0, NULL}           /* sentinel */
41 };
42 
43 static struct PyModuleDef uuidmodule = {
44     PyModuleDef_HEAD_INIT,
45     .m_name = "_uuid",
46     .m_size = -1,
47     .m_methods = uuid_methods,
48 };
49 
50 PyMODINIT_FUNC
PyInit__uuid(void)51 PyInit__uuid(void)
52 {
53     PyObject *mod;
54     assert(sizeof(uuid_t) == 16);
55 #ifdef HAVE_UUID_GENERATE_TIME_SAFE
56     int has_uuid_generate_time_safe = 1;
57 #else
58     int has_uuid_generate_time_safe = 0;
59 #endif
60     mod = PyModule_Create(&uuidmodule);
61     if (mod == NULL) {
62         return NULL;
63     }
64     if (PyModule_AddIntConstant(mod, "has_uuid_generate_time_safe",
65                                 has_uuid_generate_time_safe) < 0) {
66         Py_DECREF(mod);
67         return NULL;
68     }
69 
70     return mod;
71 }
72