blob: 89499143a61b2b9b61374b2ee9ad7d4d8f086e47 [file] [log] [blame]
Antoine Pitroua106aec2017-09-28 23:03:06 +02001#define PY_SSIZE_T_CLEAN
2
3#include "Python.h"
Michael Felt0d3ccb42017-12-30 22:39:20 +01004#ifdef HAVE_UUID_UUID_H
Antoine Pitroua106aec2017-09-28 23:03:06 +02005#include <uuid/uuid.h>
Michael Felt0d3ccb42017-12-30 22:39:20 +01006#endif
7#ifdef HAVE_UUID_H
8#include <uuid.h>
9#endif
Antoine Pitroua106aec2017-09-28 23:03:06 +020010
11
12static PyObject *
13py_uuid_generate_time_safe(void)
14{
Michael Felt0d3ccb42017-12-30 22:39:20 +010015 uuid_t uuid;
Berker Peksag9a10ff42017-11-08 23:09:16 +030016#ifdef HAVE_UUID_GENERATE_TIME_SAFE
Antoine Pitroua106aec2017-09-28 23:03:06 +020017 int res;
18
Michael Felt0d3ccb42017-12-30 22:39:20 +010019 res = uuid_generate_time_safe(uuid);
20 return Py_BuildValue("y#i", (const char *) uuid, sizeof(uuid), res);
Miss Islington (bot)5734f412018-05-24 16:22:59 -070021#elif defined(HAVE_UUID_CREATE)
David Carlierb4ebaa72018-01-09 19:38:07 +000022 uint32_t status;
Michael Felt0d3ccb42017-12-30 22:39:20 +010023 uuid_create(&uuid, &status);
Miss Islington (bot)5734f412018-05-24 16:22:59 -070024# 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
Michael Felt0d3ccb42017-12-30 22:39:20 +010029 return Py_BuildValue("y#i", (const char *) &uuid, sizeof(uuid), (int) status);
Miss Islington (bot)5734f412018-05-24 16:22:59 -070030# endif
Victor Stinner4337a0d2017-10-02 07:57:59 -070031#else
Michael Felt0d3ccb42017-12-30 22:39:20 +010032 uuid_generate_time(uuid);
33 return Py_BuildValue("y#O", (const char *) uuid, sizeof(uuid), Py_None);
Victor Stinner4337a0d2017-10-02 07:57:59 -070034#endif
Antoine Pitroua106aec2017-09-28 23:03:06 +020035}
36
37
38static PyMethodDef uuid_methods[] = {
39 {"generate_time_safe", (PyCFunction) py_uuid_generate_time_safe, METH_NOARGS, NULL},
40 {NULL, NULL, 0, NULL} /* sentinel */
41};
42
43static struct PyModuleDef uuidmodule = {
44 PyModuleDef_HEAD_INIT,
45 .m_name = "_uuid",
46 .m_size = -1,
47 .m_methods = uuid_methods,
48};
49
50PyMODINIT_FUNC
51PyInit__uuid(void)
52{
Victor Stinner4337a0d2017-10-02 07:57:59 -070053 PyObject *mod;
Antoine Pitroua106aec2017-09-28 23:03:06 +020054 assert(sizeof(uuid_t) == 16);
Berker Peksag9a10ff42017-11-08 23:09:16 +030055#ifdef HAVE_UUID_GENERATE_TIME_SAFE
Victor Stinner4337a0d2017-10-02 07:57:59 -070056 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) {
Miss Islington (bot)5734f412018-05-24 16:22:59 -070066 Py_DECREF(mod);
Victor Stinner4337a0d2017-10-02 07:57:59 -070067 return NULL;
68 }
69
70 return mod;
Antoine Pitroua106aec2017-09-28 23:03:06 +020071}