- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy path_uuidmodule.c
130 lines (113 loc) · 3.32 KB
/
_uuidmodule.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Python UUID module that wraps libuuid or Windows rpcrt4.dll.
* DCE compatible Universally Unique Identifier library.
*/
// Need limited C API version 3.13 for Py_mod_gil
#include"pyconfig.h"// Py_GIL_DISABLED
#ifndefPy_GIL_DISABLED
# definePy_LIMITED_API 0x030d0000
#endif
#include"Python.h"
#if defined(HAVE_UUID_H)
// AIX, FreeBSD, libuuid with pkgconf
#include<uuid.h>
#elif defined(HAVE_UUID_UUID_H)
// libuuid without pkgconf
#include<uuid/uuid.h>
#endif
#ifdefMS_WINDOWS
#include<rpc.h>
#endif
#ifndefMS_WINDOWS
staticPyObject*
py_uuid_generate_time_safe(PyObject*Py_UNUSED(context),
PyObject*Py_UNUSED(ignored))
{
uuid_tuuid;
#ifdefHAVE_UUID_GENERATE_TIME_SAFE
intres;
res=uuid_generate_time_safe(uuid);
returnPy_BuildValue("y#i", (constchar*) uuid, sizeof(uuid), res);
#elif defined(HAVE_UUID_CREATE)
uint32_tstatus;
uuid_create(&uuid, &status);
# if defined(HAVE_UUID_ENC_BE)
unsigned charbuf[sizeof(uuid)];
uuid_enc_be(buf, &uuid);
returnPy_BuildValue("y#i", buf, sizeof(uuid), (int) status);
# else
returnPy_BuildValue("y#i", (constchar*) &uuid, sizeof(uuid), (int) status);
# endif/* HAVE_UUID_CREATE */
#else/* HAVE_UUID_GENERATE_TIME_SAFE */
uuid_generate_time(uuid);
returnPy_BuildValue("y#O", (constchar*) uuid, sizeof(uuid), Py_None);
#endif/* HAVE_UUID_GENERATE_TIME_SAFE */
}
#else/* MS_WINDOWS */
staticPyObject*
py_UuidCreate(PyObject*Py_UNUSED(context),
PyObject*Py_UNUSED(ignored))
{
UUIDuuid;
RPC_STATUSres;
Py_BEGIN_ALLOW_THREADS
res=UuidCreateSequential(&uuid);
Py_END_ALLOW_THREADS
switch (res) {
caseRPC_S_OK:
caseRPC_S_UUID_LOCAL_ONLY:
caseRPC_S_UUID_NO_ADDRESS:
/*
All success codes, but the latter two indicate that the UUID is random
rather than based on the MAC address. If the OS can't figure this out,
neither can we, so we'll take it anyway.
*/
returnPy_BuildValue("y#", (constchar*)&uuid, sizeof(uuid));
}
PyErr_SetFromWindowsErr(res);
returnNULL;
}
#endif/* MS_WINDOWS */
staticint
uuid_exec(PyObject*module) {
assert(sizeof(uuid_t) ==16);
#if defined(MS_WINDOWS)
inthas_uuid_generate_time_safe=0;
#elif defined(HAVE_UUID_GENERATE_TIME_SAFE)
inthas_uuid_generate_time_safe=1;
#else
inthas_uuid_generate_time_safe=0;
#endif
if (PyModule_AddIntConstant(module, "has_uuid_generate_time_safe",
has_uuid_generate_time_safe) <0) {
return-1;
}
return0;
}
staticPyMethodDefuuid_methods[] = {
#if defined(HAVE_UUID_UUID_H) || defined(HAVE_UUID_H)
{"generate_time_safe", py_uuid_generate_time_safe, METH_NOARGS, NULL},
#endif
#if defined(MS_WINDOWS)
{"UuidCreate", py_UuidCreate, METH_NOARGS, NULL},
#endif
{NULL, NULL, 0, NULL} /* sentinel */
};
staticPyModuleDef_Slotuuid_slots[] = {
{Py_mod_exec, uuid_exec},
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
{0, NULL}
};
staticstructPyModuleDefuuidmodule= {
PyModuleDef_HEAD_INIT,
.m_name="_uuid",
.m_size=0,
.m_methods=uuid_methods,
.m_slots=uuid_slots,
};
PyMODINIT_FUNC
PyInit__uuid(void)
{
returnPyModuleDef_Init(&uuidmodule);
}