Skip to content

bpo-32493: Fix uuid.uuid1() on FreeBSD. #7098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions Lib/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ def _netbios_getnode():

_generate_time_safe = _UuidCreate = None
_has_uuid_generate_time_safe = None
_little_endian = None

# Import optional C extension at toplevel, to help disabling it when testing
try:
Expand All @@ -545,7 +546,7 @@ def _load_system_functions():
"""
Try to load platform-specific functions for generating uuids.
"""
global _generate_time_safe, _UuidCreate, _has_uuid_generate_time_safe
global _generate_time_safe, _UuidCreate, _has_uuid_generate_time_safe, _little_endian

if _has_uuid_generate_time_safe is not None:
return
Expand All @@ -564,6 +565,7 @@ def _load_system_functions():
elif _uuid is not None:
_generate_time_safe = _uuid.generate_time_safe
_has_uuid_generate_time_safe = _uuid.has_uuid_generate_time_safe
_little_endian = _uuid.little_endian
return

try:
Expand Down Expand Up @@ -592,6 +594,7 @@ def _generate_time_safe():
res = _uuid_generate_time_safe(_buffer)
return bytes(_buffer.raw), res
_has_uuid_generate_time_safe = True
_little_endian = False
break

elif hasattr(lib, 'uuid_generate_time'): # pragma: nocover
Expand All @@ -602,6 +605,7 @@ def _generate_time_safe():
_buffer = ctypes.create_string_buffer(16)
_uuid_generate_time(_buffer)
return bytes(_buffer.raw), None
_little_endian = False
break

# On Windows prior to 2000, UuidCreate gives a UUID containing the
Expand Down Expand Up @@ -630,7 +634,10 @@ def _unix_getnode():
or ctypes."""
_load_system_functions()
uuid_time, _ = _generate_time_safe()
return UUID(bytes=uuid_time).node
if _little_endian:
return UUID(bytes_le=uuid_time).node
else:
return UUID(bytes=uuid_time).node

def _windll_getnode():
"""Get the hardware address on Windows using ctypes."""
Expand Down Expand Up @@ -707,7 +714,10 @@ def uuid1(node=None, clock_seq=None):
is_safe = SafeUUID(safely_generated)
except ValueError:
is_safe = SafeUUID.unknown
return UUID(bytes=uuid_time, is_safe=is_safe)
if _little_endian:
return UUID(bytes_le=uuid_time, is_safe=is_safe)
else:
return UUID(bytes=uuid_time, is_safe=is_safe)

global _last_timestamp
import time
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed :func:`uuid.uuid1` on FreeBSD.
13 changes: 12 additions & 1 deletion Modules/_uuidmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ py_uuid_generate_time_safe(PyObject *Py_UNUSED(context),

res = uuid_generate_time_safe(uuid);
return Py_BuildValue("y#i", (const char *) uuid, sizeof(uuid), res);
#elif HAVE_UUID_CREATE
#elif defined(HAVE_UUID_CREATE)
uint32_t status;
uuid_create(&uuid, &status);
return Py_BuildValue("y#i", (const char *) &uuid, sizeof(uuid), (int) status);
Expand Down Expand Up @@ -51,13 +51,24 @@ PyInit__uuid(void)
int has_uuid_generate_time_safe = 1;
#else
int has_uuid_generate_time_safe = 0;
#endif
/* uuid_create() creates bytes with platform-depending endianess */
#if !defined(HAVE_UUID_GENERATE_TIME_SAFE) && defined(HAVE_UUID_CREATE)
int little_endian = PY_LITTLE_ENDIAN;
#else
int little_endian = 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) {
Py_DECREF(mod);
return NULL;
}
if (PyModule_AddIntConstant(mod, "little_endian", little_endian) < 0) {
Py_DECREF(mod);
return NULL;
}

Expand Down