mirror of
https://github.com/python/cpython.git
synced 2025-12-23 09:19:18 +00:00
[3.14] gh-132710: only use stable _uuid.generate_time_safe() to deduce MAC address (GH-132901) (#134697)
(cherry picked from commit 3bffada467)
This commit is contained in:
parent
c8379c7edf
commit
072f50fc12
6 changed files with 357 additions and 28 deletions
|
|
@ -14,6 +14,7 @@ from unittest import mock
|
|||
|
||||
from test import support
|
||||
from test.support import import_helper
|
||||
from test.support.script_helper import assert_python_ok
|
||||
|
||||
py_uuid = import_helper.import_fresh_module('uuid', blocked=['_uuid'])
|
||||
c_uuid = import_helper.import_fresh_module('uuid', fresh=['_uuid'])
|
||||
|
|
@ -1217,10 +1218,37 @@ class BaseTestUUID:
|
|||
class TestUUIDWithoutExtModule(BaseTestUUID, unittest.TestCase):
|
||||
uuid = py_uuid
|
||||
|
||||
|
||||
@unittest.skipUnless(c_uuid, 'requires the C _uuid module')
|
||||
class TestUUIDWithExtModule(BaseTestUUID, unittest.TestCase):
|
||||
uuid = c_uuid
|
||||
|
||||
def check_has_stable_libuuid_extractable_node(self):
|
||||
if not self.uuid._has_stable_extractable_node:
|
||||
self.skipTest("libuuid cannot deduce MAC address")
|
||||
|
||||
@unittest.skipUnless(os.name == 'posix', 'POSIX only')
|
||||
def test_unix_getnode_from_libuuid(self):
|
||||
self.check_has_stable_libuuid_extractable_node()
|
||||
script = 'import uuid; print(uuid._unix_getnode())'
|
||||
_, n_a, _ = assert_python_ok('-c', script)
|
||||
_, n_b, _ = assert_python_ok('-c', script)
|
||||
n_a, n_b = n_a.decode().strip(), n_b.decode().strip()
|
||||
self.assertTrue(n_a.isdigit())
|
||||
self.assertTrue(n_b.isdigit())
|
||||
self.assertEqual(n_a, n_b)
|
||||
|
||||
@unittest.skipUnless(os.name == 'nt', 'Windows only')
|
||||
def test_windows_getnode_from_libuuid(self):
|
||||
self.check_has_stable_libuuid_extractable_node()
|
||||
script = 'import uuid; print(uuid._windll_getnode())'
|
||||
_, n_a, _ = assert_python_ok('-c', script)
|
||||
_, n_b, _ = assert_python_ok('-c', script)
|
||||
n_a, n_b = n_a.decode().strip(), n_b.decode().strip()
|
||||
self.assertTrue(n_a.isdigit())
|
||||
self.assertTrue(n_b.isdigit())
|
||||
self.assertEqual(n_a, n_b)
|
||||
|
||||
|
||||
class BaseTestInternals:
|
||||
_uuid = py_uuid
|
||||
|
|
|
|||
|
|
@ -633,22 +633,24 @@ def _netstat_getnode():
|
|||
try:
|
||||
import _uuid
|
||||
_generate_time_safe = getattr(_uuid, "generate_time_safe", None)
|
||||
_has_stable_extractable_node = _uuid.has_stable_extractable_node
|
||||
_UuidCreate = getattr(_uuid, "UuidCreate", None)
|
||||
except ImportError:
|
||||
_uuid = None
|
||||
_generate_time_safe = None
|
||||
_has_stable_extractable_node = False
|
||||
_UuidCreate = None
|
||||
|
||||
|
||||
def _unix_getnode():
|
||||
"""Get the hardware address on Unix using the _uuid extension module."""
|
||||
if _generate_time_safe:
|
||||
if _generate_time_safe and _has_stable_extractable_node:
|
||||
uuid_time, _ = _generate_time_safe()
|
||||
return UUID(bytes=uuid_time).node
|
||||
|
||||
def _windll_getnode():
|
||||
"""Get the hardware address on Windows using the _uuid extension module."""
|
||||
if _UuidCreate:
|
||||
if _UuidCreate and _has_stable_extractable_node:
|
||||
uuid_bytes = _UuidCreate()
|
||||
return UUID(bytes_le=uuid_bytes).node
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue