2017-09-28 23:03:06 +02:00
|
|
|
#define PY_SSIZE_T_CLEAN
|
|
|
|
|
|
|
|
#include "Python.h"
|
|
|
|
#include <uuid/uuid.h>
|
|
|
|
|
|
|
|
|
2017-10-02 16:57:59 +02:00
|
|
|
/* bpo-11063: libuuid on macOS doesn't provide uuid_generate_time_safe(),
|
|
|
|
only uuid_generate_time(). */
|
|
|
|
#ifndef __APPLE__
|
|
|
|
# define HAVE_TIME_SAFE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2017-09-28 23:03:06 +02:00
|
|
|
static PyObject *
|
|
|
|
py_uuid_generate_time_safe(void)
|
|
|
|
{
|
2017-10-02 16:57:59 +02:00
|
|
|
#ifdef HAVE_TIME_SAFE
|
2017-09-28 23:03:06 +02:00
|
|
|
uuid_t out;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
res = uuid_generate_time_safe(out);
|
|
|
|
return Py_BuildValue("y#i", (const char *) out, sizeof(out), res);
|
2017-10-02 16:57:59 +02:00
|
|
|
#else
|
|
|
|
uuid_t out;
|
|
|
|
uuid_generate_time(out);
|
|
|
|
return Py_BuildValue("y#O", (const char *) out, sizeof(out), Py_None);
|
|
|
|
#endif
|
2017-09-28 23:03:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyMethodDef uuid_methods[] = {
|
|
|
|
{"generate_time_safe", (PyCFunction) py_uuid_generate_time_safe, METH_NOARGS, NULL},
|
|
|
|
{NULL, NULL, 0, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct PyModuleDef uuidmodule = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
.m_name = "_uuid",
|
|
|
|
.m_size = -1,
|
|
|
|
.m_methods = uuid_methods,
|
|
|
|
};
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit__uuid(void)
|
|
|
|
{
|
2017-10-02 16:57:59 +02:00
|
|
|
PyObject *mod;
|
2017-09-28 23:03:06 +02:00
|
|
|
assert(sizeof(uuid_t) == 16);
|
2017-10-02 16:57:59 +02:00
|
|
|
#ifdef HAVE_TIME_SAFE
|
|
|
|
int has_uuid_generate_time_safe = 1;
|
|
|
|
#else
|
|
|
|
int has_uuid_generate_time_safe = 0;
|
|
|
|
#endif
|
|
|
|
mod = PyModule_Create(&uuidmodule);
|
|
|
|
if (mod == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (PyModule_AddIntConstant(mod, "has_uuid_generate_time_safe",
|
|
|
|
has_uuid_generate_time_safe) < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mod;
|
2017-09-28 23:03:06 +02:00
|
|
|
}
|