blob: 3be6c848ad64575c3a9147cebea18c39cff8fd37 [file] [log] [blame]
ziheng6ffd9b02019-06-25 01:59:51 +08001/*
2 * Python UUID module that wraps libuuid -
3 * DCE compatible Universally Unique Identifier library.
4 */
5
Antoine Pitroua106aec2017-09-28 23:03:06 +02006#define PY_SSIZE_T_CLEAN
7
8#include "Python.h"
Michael Felt0d3ccb42017-12-30 22:39:20 +01009#ifdef HAVE_UUID_UUID_H
Antoine Pitroua106aec2017-09-28 23:03:06 +020010#include <uuid/uuid.h>
ziheng6ffd9b02019-06-25 01:59:51 +080011#elif defined(HAVE_UUID_H)
Michael Felt0d3ccb42017-12-30 22:39:20 +010012#include <uuid.h>
13#endif
Antoine Pitroua106aec2017-09-28 23:03:06 +020014
Antoine Pitroua106aec2017-09-28 23:03:06 +020015static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +053016py_uuid_generate_time_safe(PyObject *Py_UNUSED(context),
17 PyObject *Py_UNUSED(ignored))
Antoine Pitroua106aec2017-09-28 23:03:06 +020018{
Michael Felt0d3ccb42017-12-30 22:39:20 +010019 uuid_t uuid;
Berker Peksag9a10ff42017-11-08 23:09:16 +030020#ifdef HAVE_UUID_GENERATE_TIME_SAFE
Antoine Pitroua106aec2017-09-28 23:03:06 +020021 int res;
22
Michael Felt0d3ccb42017-12-30 22:39:20 +010023 res = uuid_generate_time_safe(uuid);
24 return Py_BuildValue("y#i", (const char *) uuid, sizeof(uuid), res);
Serhiy Storchaka17d88302018-05-25 01:45:09 +030025#elif defined(HAVE_UUID_CREATE)
David Carlierb4ebaa72018-01-09 19:38:07 +000026 uint32_t status;
Michael Felt0d3ccb42017-12-30 22:39:20 +010027 uuid_create(&uuid, &status);
Serhiy Storchaka17d88302018-05-25 01:45:09 +030028# if defined(HAVE_UUID_ENC_BE)
29 unsigned char buf[sizeof(uuid)];
30 uuid_enc_be(buf, &uuid);
31 return Py_BuildValue("y#i", buf, sizeof(uuid), (int) status);
32# else
Michael Felt0d3ccb42017-12-30 22:39:20 +010033 return Py_BuildValue("y#i", (const char *) &uuid, sizeof(uuid), (int) status);
Serhiy Storchaka17d88302018-05-25 01:45:09 +030034# endif
Victor Stinner4337a0d2017-10-02 07:57:59 -070035#else
Michael Felt0d3ccb42017-12-30 22:39:20 +010036 uuid_generate_time(uuid);
37 return Py_BuildValue("y#O", (const char *) uuid, sizeof(uuid), Py_None);
Victor Stinner4337a0d2017-10-02 07:57:59 -070038#endif
Antoine Pitroua106aec2017-09-28 23:03:06 +020039}
40
Dong-hee Na1cb763b2020-03-31 21:43:47 +090041static int
42uuid_exec(PyObject *module) {
Antoine Pitroua106aec2017-09-28 23:03:06 +020043 assert(sizeof(uuid_t) == 16);
Berker Peksag9a10ff42017-11-08 23:09:16 +030044#ifdef HAVE_UUID_GENERATE_TIME_SAFE
Victor Stinner4337a0d2017-10-02 07:57:59 -070045 int has_uuid_generate_time_safe = 1;
46#else
47 int has_uuid_generate_time_safe = 0;
48#endif
Dong-hee Na1cb763b2020-03-31 21:43:47 +090049 if (PyModule_AddIntConstant(module, "has_uuid_generate_time_safe",
Victor Stinner4337a0d2017-10-02 07:57:59 -070050 has_uuid_generate_time_safe) < 0) {
Dong-hee Na1cb763b2020-03-31 21:43:47 +090051 return -1;
Victor Stinner4337a0d2017-10-02 07:57:59 -070052 }
Dong-hee Na1cb763b2020-03-31 21:43:47 +090053 return 0;
54}
Victor Stinner4337a0d2017-10-02 07:57:59 -070055
Dong-hee Na1cb763b2020-03-31 21:43:47 +090056static PyMethodDef uuid_methods[] = {
57 {"generate_time_safe", py_uuid_generate_time_safe, METH_NOARGS, NULL},
58 {NULL, NULL, 0, NULL} /* sentinel */
59};
60
61static PyModuleDef_Slot uuid_slots[] = {
62 {Py_mod_exec, uuid_exec},
63 {0, NULL}
64};
65
66static struct PyModuleDef uuidmodule = {
67 PyModuleDef_HEAD_INIT,
68 .m_name = "_uuid",
69 .m_size = 0,
70 .m_methods = uuid_methods,
71 .m_slots = uuid_slots,
72};
73
74PyMODINIT_FUNC
75PyInit__uuid(void)
76{
77 return PyModuleDef_Init(&uuidmodule);
Antoine Pitroua106aec2017-09-28 23:03:06 +020078}