Skip to content

Commit 7cc1d76

Browse files
committed
pythongh-113308: Deprecate some internal parts of uuid module
1 parent de0b4f9 commit 7cc1d76

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

Doc/whatsnew/3.13.rst

+5
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,11 @@ Pending Removal in Python 3.15
924924
They will be removed in Python 3.15.
925925
(Contributed by Victor Stinner in :gh:`105096`.)
926926

927+
* :mod:`uuid`: Deprecate some internal protected parts:
928+
``_has_uuid_generate_time_safe``, ``_netbios_getnode``, ``_ipconfig_getnode``,
929+
and ``_load_system_functions``. They will be removed in Python 3.15.
930+
(Contributed by Nikita Sobolev in :gh:`113308`.)
931+
927932
Pending Removal in Python 3.16
928933
------------------------------
929934

Lib/test/test_uuid.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io
99
import os
1010
import pickle
11+
import re
1112
import sys
1213
import weakref
1314
from unittest import mock
@@ -530,7 +531,11 @@ def test_uuid1(self):
530531
@support.requires_mac_ver(10, 5)
531532
@unittest.skipUnless(os.name == 'posix', 'POSIX-only test')
532533
def test_uuid1_safe(self):
533-
if not self.uuid._has_uuid_generate_time_safe:
534+
msg = re.escape("'_has_uuid_generate_time_safe' is deprecated and "
535+
"slated for removal in Python 3.15")
536+
with self.assertWarnsRegex(DeprecationWarning, msg):
537+
has_uuid_generate_time_safe = self.uuid._has_uuid_generate_time_safe
538+
if not has_uuid_generate_time_safe:
534539
self.skipTest('requires uuid_generate_time_safe(3)')
535540

536541
u = self.uuid.uuid1()
@@ -546,7 +551,6 @@ def mock_generate_time_safe(self, safe_value):
546551
"""
547552
if os.name != 'posix':
548553
self.skipTest('POSIX-only test')
549-
self.uuid._load_system_functions()
550554
f = self.uuid._generate_time_safe
551555
if f is None:
552556
self.skipTest('need uuid._generate_time_safe')
@@ -581,7 +585,19 @@ def test_uuid1_bogus_return_value(self):
581585
self.assertEqual(u.is_safe, self.uuid.SafeUUID.unknown)
582586

583587
def test_uuid1_time(self):
584-
with mock.patch.object(self.uuid, '_has_uuid_generate_time_safe', False), \
588+
import contextlib
589+
590+
@contextlib.contextmanager
591+
def patch_has_uuid_generate_time_safe(value):
592+
msg = re.escape("'_has_uuid_generate_time_safe' is deprecated and "
593+
"slated for removal in Python 3.15")
594+
with self.assertWarnsRegex(DeprecationWarning, msg):
595+
with mock.patch.object(
596+
self.uuid, '_has_uuid_generate_time_safe', value,
597+
) as patched:
598+
yield patched
599+
600+
with patch_has_uuid_generate_time_safe(False), \
585601
mock.patch.object(self.uuid, '_generate_time_safe', None), \
586602
mock.patch.object(self.uuid, '_last_timestamp', None), \
587603
mock.patch.object(self.uuid, 'getnode', return_value=93328246233727), \
@@ -590,7 +606,7 @@ def test_uuid1_time(self):
590606
u = self.uuid.uuid1()
591607
self.assertEqual(u, self.uuid.UUID('a7a55b92-01fc-11e9-94c5-54e1acf6da7f'))
592608

593-
with mock.patch.object(self.uuid, '_has_uuid_generate_time_safe', False), \
609+
with patch_has_uuid_generate_time_safe(False), \
594610
mock.patch.object(self.uuid, '_generate_time_safe', None), \
595611
mock.patch.object(self.uuid, '_last_timestamp', None), \
596612
mock.patch('time.time_ns', return_value=1545052026752910643):

Lib/uuid.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
import os
4848
import sys
49+
import warnings
4950

5051
from enum import Enum, _simple_enum
5152

@@ -566,11 +567,13 @@ def _netstat_getnode():
566567

567568
def _ipconfig_getnode():
568569
"""[DEPRECATED] Get the hardware address on Windows."""
570+
warnings._deprecated("_ipconfig_getnode", remove=(3, 15))
569571
# bpo-40501: UuidCreateSequential() is now the only supported approach
570572
return _windll_getnode()
571573

572574
def _netbios_getnode():
573575
"""[DEPRECATED] Get the hardware address on Windows."""
576+
warnings._deprecated("_netbios_getnode", remove=(3, 15))
574577
# bpo-40501: UuidCreateSequential() is now the only supported approach
575578
return _windll_getnode()
576579

@@ -580,16 +583,28 @@ def _netbios_getnode():
580583
import _uuid
581584
_generate_time_safe = getattr(_uuid, "generate_time_safe", None)
582585
_UuidCreate = getattr(_uuid, "UuidCreate", None)
583-
_has_uuid_generate_time_safe = _uuid.has_uuid_generate_time_safe
584586
except ImportError:
585587
_uuid = None
586588
_generate_time_safe = None
587589
_UuidCreate = None
588-
_has_uuid_generate_time_safe = None
590+
591+
592+
def __getattr__(attr):
593+
if attr == "_has_uuid_generate_time_safe":
594+
try:
595+
import _uuid
596+
except ImportError:
597+
_has_uuid_generate_time_safe = None
598+
else:
599+
_has_uuid_generate_time_safe = _uuid.has_uuid_generate_time_safe
600+
warnings._deprecated("_has_uuid_generate_time_safe", remove=(3, 15))
601+
return _has_uuid_generate_time_safe
602+
raise AttributeError(f"module {__name__!r} has no attribute {attr!r}")
589603

590604

591605
def _load_system_functions():
592606
"""[DEPRECATED] Platform-specific functions loaded at import time"""
607+
warnings._deprecated("_load_system_functions", remove=(3, 15))
593608

594609

595610
def _unix_getnode():
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:mod:`uuid`: Deprecate some internal protected parts:
2+
``_has_uuid_generate_time_safe``, ``_netbios_getnode``,
3+
``_ipconfig_getnode``, and ``_load_system_functions``. They will be removed
4+
in Python 3.15.

0 commit comments

Comments
 (0)