Skip to content

uuid.getnode() is not tied to MAC address when using libuuid #132710

Open
@sfc-gh-tteixeira

Description

@sfc-gh-tteixeira

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 with libuuid, this does not match the observed behavior. Instead, getnode() often produces a random number.

Versions of Python obtained through python.org, brew, and pyenv don't appear to display this bug. But versions obtained through uv and python-build-standalone do.

The key difference seems to be which branch of this try block inside uuid.py is executed:

# Import optional C extension at toplevel, to help disabling it when testing
try:
    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_safe
except ImportError:
    _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

Metadata

Metadata

Assignees

Labels

extension-modulesC modules in the Modules dirstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions