blob: 1b37511c2286785c3205b02c5b8cae1cdcf6b376 [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);
21#elif HAVE_UUID_CREATE
22/*
23 * AIX support for uuid - RFC4122
24 */
25 unsigned32 status;
26 uuid_create(&uuid, &status);
27 return Py_BuildValue("y#i", (const char *) &uuid, sizeof(uuid), (int) status);
Victor Stinner4337a0d2017-10-02 07:57:59 -070028#else
Michael Felt0d3ccb42017-12-30 22:39:20 +010029 uuid_generate_time(uuid);
30 return Py_BuildValue("y#O", (const char *) uuid, sizeof(uuid), Py_None);
Victor Stinner4337a0d2017-10-02 07:57:59 -070031#endif
Antoine Pitroua106aec2017-09-28 23:03:06 +020032}
33
34
35static PyMethodDef uuid_methods[] = {
36 {"generate_time_safe", (PyCFunction) py_uuid_generate_time_safe, METH_NOARGS, NULL},
37 {NULL, NULL, 0, NULL} /* sentinel */
38};
39
40static struct PyModuleDef uuidmodule = {
41 PyModuleDef_HEAD_INIT,
42 .m_name = "_uuid",
43 .m_size = -1,
44 .m_methods = uuid_methods,
45};
46
47PyMODINIT_FUNC
48PyInit__uuid(void)
49{
Victor Stinner4337a0d2017-10-02 07:57:59 -070050 PyObject *mod;
Antoine Pitroua106aec2017-09-28 23:03:06 +020051 assert(sizeof(uuid_t) == 16);
Berker Peksag9a10ff42017-11-08 23:09:16 +030052#ifdef HAVE_UUID_GENERATE_TIME_SAFE
Victor Stinner4337a0d2017-10-02 07:57:59 -070053 int has_uuid_generate_time_safe = 1;
54#else
55 int has_uuid_generate_time_safe = 0;
56#endif
57 mod = PyModule_Create(&uuidmodule);
58 if (mod == NULL) {
59 return NULL;
60 }
61 if (PyModule_AddIntConstant(mod, "has_uuid_generate_time_safe",
62 has_uuid_generate_time_safe) < 0) {
63 return NULL;
64 }
65
66 return mod;
Antoine Pitroua106aec2017-09-28 23:03:06 +020067}