0
0
mirror of https://github.com/python/cpython.git synced 2024-11-24 17:47:13 +01:00
cpython/Modules/_uuidmodule.c
Michael Felt 0d3ccb4395 bpo-32399: Starting with AIX6.1 there is support in libc.a for uuid (RFC4122) (#4974)
Starting with AIX6.1 there is support in libc.a for uuid (RFC4122)
This patch provides the changes needed for this integration with the OS.

On AIX the base function is uuid_create() rather than uuid_generate_time()
The AIX uuid_t typedef is more aligned to the UUID field based definition
while the Linux typedef that is more aligned with UUID bytes
(or perhaps UUID bytes_le) definitions.
2017-12-30 22:39:20 +01:00

68 lines
1.5 KiB
C

#define PY_SSIZE_T_CLEAN
#include "Python.h"
#ifdef HAVE_UUID_UUID_H
#include <uuid/uuid.h>
#endif
#ifdef HAVE_UUID_H
#include <uuid.h>
#endif
static PyObject *
py_uuid_generate_time_safe(void)
{
uuid_t uuid;
#ifdef HAVE_UUID_GENERATE_TIME_SAFE
int res;
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);
#else
uuid_generate_time(uuid);
return Py_BuildValue("y#O", (const char *) uuid, sizeof(uuid), Py_None);
#endif
}
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)
{
PyObject *mod;
assert(sizeof(uuid_t) == 16);
#ifdef HAVE_UUID_GENERATE_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;
}