2017-09-28 23:03:06 +02:00
|
|
|
#define PY_SSIZE_T_CLEAN
|
|
|
|
|
|
|
|
#include "Python.h"
|
2017-12-30 22:39:20 +01:00
|
|
|
#ifdef HAVE_UUID_UUID_H
|
2017-09-28 23:03:06 +02:00
|
|
|
#include <uuid/uuid.h>
|
2017-12-30 22:39:20 +01:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UUID_H
|
|
|
|
#include <uuid.h>
|
|
|
|
#endif
|
2017-09-28 23:03:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
py_uuid_generate_time_safe(void)
|
|
|
|
{
|
2017-12-30 22:39:20 +01:00
|
|
|
uuid_t uuid;
|
2017-11-08 21:09:16 +01:00
|
|
|
#ifdef HAVE_UUID_GENERATE_TIME_SAFE
|
2017-09-28 23:03:06 +02:00
|
|
|
int res;
|
|
|
|
|
2017-12-30 22:39:20 +01:00
|
|
|
res = uuid_generate_time_safe(uuid);
|
|
|
|
return Py_BuildValue("y#i", (const char *) uuid, sizeof(uuid), res);
|
|
|
|
#elif HAVE_UUID_CREATE
|
|
|
|
/*
|
|
|
|
* AIX support for uuid - RFC4122
|
|
|
|
*/
|
|
|
|
unsigned32 status;
|
|
|
|
uuid_create(&uuid, &status);
|
|
|
|
return Py_BuildValue("y#i", (const char *) &uuid, sizeof(uuid), (int) status);
|
2017-10-02 16:57:59 +02:00
|
|
|
#else
|
2017-12-30 22:39:20 +01:00
|
|
|
uuid_generate_time(uuid);
|
|
|
|
return Py_BuildValue("y#O", (const char *) uuid, sizeof(uuid), Py_None);
|
2017-10-02 16:57:59 +02:00
|
|
|
#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-11-08 21:09:16 +01:00
|
|
|
#ifdef HAVE_UUID_GENERATE_TIME_SAFE
|
2017-10-02 16:57:59 +02:00
|
|
|
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
|
|
|
}
|