Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Description
Bug report
Bug description:
According to the docs,uuid.getnode() is meant to return a number based on the MAC address of the network interface. However, if Python is built withlibuuid, this does not match the observed behavior. Instead,getnode() often produces a random number.
Versions of Python obtained through python.org,brew, andpyenv don't appear to display this bug. But versions obtained throughuv andpython-build-standalone do.
The key difference seems to be which branch of thistry block insideuuid.py is executed:
# Import optional C extension at toplevel, to help disabling it when testingtry:import_uuid_generate_time_safe=getattr(_uuid,"generate_time_safe",None)_UuidCreate=getattr(_uuid,"UuidCreate",None)_has_uuid_generate_time_safe=_uuid.has_uuid_generate_time_safeexceptImportError:_uuid=None_generate_time_safe=None_UuidCreate=None_has_uuid_generate_time_safe=None
When the top branch executes,getnode() produces a random number.
When the bottom branch executes,getnode() produces a number tied to the MAC address of the network interface.
Steps to reproduce:
Case 1: working as intended
Using a version of Python compiled with these flags...
HAVE_UUID_CREATE = "0" HAVE_UUID_ENC_BE = "0" HAVE_UUID_GENERATE_TIME_SAFE = "1" HAVE_UUID_H = "1" HAVE_UUID_UUID_H = "1"...we get this behavior:
$python -c"import uuid; print(uuid.getnode())"some number X$python -c"import uuid; print(uuid.getnode())"the same number X
Case 2: buggy behavior
Using a version of Python compiled with these flags...
HAVE_UUID_CREATE = "0" HAVE_UUID_ENC_BE = "0" HAVE_UUID_GENERATE_TIME_SAFE = "0" HAVE_UUID_H = "0" HAVE_UUID_UUID_H = "1"...we get this behavior:
$python -c"import uuid; print(uuid.getnode())"some number X$python -c"import uuid; print(uuid.getnode())"some other number Y!!!
CPython versions tested on:
3.13
Operating systems tested on:
macOS, Linux
Linked PRs
- gh-132710: only use stable
_uuid.generate_time_safe()to deduce MAC address #132901 - [3.14] gh-132710: only use stable
_uuid.generate_time_safe()to deduce MAC address (GH-132901) #134697 - [3.13] gh-132710: only use stable
_uuid.generate_time_safe()to deduce MAC address (GH-132901) #134704 [to be merged in August 2025] - gh-132710: add missing NEWS entry for GH-132901 #134705
- [3.14] gh-132710: add missing NEWS entry for GH-134697 (GH-134705) #134707
- [3.13] gh-132710: add missing NEWS entry for GH-134704 (GH-134705) #134709 [included in[3.13] gh-132710: only use stable
_uuid.generate_time_safe()to deduce MAC address (GH-132901) #134704]