@@ -555,178 +555,44 @@ def _netstat_getnode():
555
555
return _find_mac_under_heading ('netstat' , '-ian' , b'Address' )
556
556
557
557
def _ipconfig_getnode ():
558
- """Get the hardware address on Windows by running ipconfig.exe."""
559
- import os , re , subprocess
560
- first_local_mac = None
561
- dirs = ['' , r'c:\windows\system32' , r'c:\winnt\system32' ]
562
- try :
563
- import ctypes
564
- buffer = ctypes .create_string_buffer (300 )
565
- ctypes .windll .kernel32 .GetSystemDirectoryA (buffer , 300 )
566
- dirs .insert (0 , buffer .value .decode ('mbcs' ))
567
- except :
568
- pass
569
- for dir in dirs :
570
- try :
571
- proc = subprocess .Popen ([os .path .join (dir , 'ipconfig' ), '/all' ],
572
- stdout = subprocess .PIPE ,
573
- encoding = "oem" )
574
- except OSError :
575
- continue
576
- with proc :
577
- for line in proc .stdout :
578
- value = line .split (':' )[- 1 ].strip ().lower ()
579
- if re .fullmatch ('(?:[0-9a-f][0-9a-f]-){5}[0-9a-f][0-9a-f]' , value ):
580
- mac = int (value .replace ('-' , '' ), 16 )
581
- if _is_universal (mac ):
582
- return mac
583
- first_local_mac = first_local_mac or mac
584
- return first_local_mac or None
558
+ """[DEPRECATED] Get the hardware address on Windows."""
559
+ # bpo-40501: UuidCreateSequential() is now the only supported approach
560
+ return _windll_getnode ()
585
561
586
562
def _netbios_getnode ():
587
- """Get the hardware address on Windows using NetBIOS calls.
588
- See http://support.microsoft.com/kb/118623 for details."""
589
- import win32wnet , netbios
590
- first_local_mac = None
591
- ncb = netbios .NCB ()
592
- ncb .Command = netbios .NCBENUM
593
- ncb .Buffer = adapters = netbios .LANA_ENUM ()
594
- adapters ._pack ()
595
- if win32wnet .Netbios (ncb ) != 0 :
596
- return None
597
- adapters ._unpack ()
598
- for i in range (adapters .length ):
599
- ncb .Reset ()
600
- ncb .Command = netbios .NCBRESET
601
- ncb .Lana_num = ord (adapters .lana [i ])
602
- if win32wnet .Netbios (ncb ) != 0 :
603
- continue
604
- ncb .Reset ()
605
- ncb .Command = netbios .NCBASTAT
606
- ncb .Lana_num = ord (adapters .lana [i ])
607
- ncb .Callname = '*' .ljust (16 )
608
- ncb .Buffer = status = netbios .ADAPTER_STATUS ()
609
- if win32wnet .Netbios (ncb ) != 0 :
610
- continue
611
- status ._unpack ()
612
- bytes = status .adapter_address [:6 ]
613
- if len (bytes ) != 6 :
614
- continue
615
- mac = int .from_bytes (bytes , 'big' )
616
- if _is_universal (mac ):
617
- return mac
618
- first_local_mac = first_local_mac or mac
619
- return first_local_mac or None
563
+ """[DEPRECATED] Get the hardware address on Windows."""
564
+ # bpo-40501: UuidCreateSequential() is now the only supported approach
565
+ return _windll_getnode ()
620
566
621
567
622
- _generate_time_safe = _UuidCreate = None
623
- _has_uuid_generate_time_safe = None
624
-
625
568
# Import optional C extension at toplevel, to help disabling it when testing
626
569
try :
627
570
import _uuid
571
+ _generate_time_safe = getattr (_uuid , "generate_time_safe" , None )
572
+ _UuidCreate = getattr (_uuid , "UuidCreate" , None )
573
+ _has_uuid_generate_time_safe = _uuid .has_uuid_generate_time_safe
628
574
except ImportError :
629
575
_uuid = None
576
+ _generate_time_safe = None
577
+ _UuidCreate = None
578
+ _has_uuid_generate_time_safe = None
630
579
631
580
632
581
def _load_system_functions ():
633
- """
634
- Try to load platform-specific functions for generating uuids.
635
- """
636
- global _generate_time_safe , _UuidCreate , _has_uuid_generate_time_safe
637
-
638
- if _has_uuid_generate_time_safe is not None :
639
- return
640
-
641
- _has_uuid_generate_time_safe = False
642
-
643
- if sys .platform == "darwin" and int (os .uname ().release .split ('.' )[0 ]) < 9 :
644
- # The uuid_generate_* functions are broken on MacOS X 10.5, as noted
645
- # in issue #8621 the function generates the same sequence of values
646
- # in the parent process and all children created using fork (unless
647
- # those children use exec as well).
648
- #
649
- # Assume that the uuid_generate functions are broken from 10.5 onward,
650
- # the test can be adjusted when a later version is fixed.
651
- pass
652
- elif _uuid is not None :
653
- _generate_time_safe = _uuid .generate_time_safe
654
- _has_uuid_generate_time_safe = _uuid .has_uuid_generate_time_safe
655
- return
656
-
657
- try :
658
- # If we couldn't find an extension module, try ctypes to find
659
- # system routines for UUID generation.
660
- # Thanks to Thomas Heller for ctypes and for his help with its use here.
661
- import ctypes
662
- import ctypes .util
663
-
664
- # The uuid_generate_* routines are provided by libuuid on at least
665
- # Linux and FreeBSD, and provided by libc on Mac OS X.
666
- _libnames = ['uuid' ]
667
- if not sys .platform .startswith ('win' ):
668
- _libnames .append ('c' )
669
- for libname in _libnames :
670
- try :
671
- lib = ctypes .CDLL (ctypes .util .find_library (libname ))
672
- except Exception : # pragma: nocover
673
- continue
674
- # Try to find the safe variety first.
675
- if hasattr (lib , 'uuid_generate_time_safe' ):
676
- _uuid_generate_time_safe = lib .uuid_generate_time_safe
677
- # int uuid_generate_time_safe(uuid_t out);
678
- def _generate_time_safe ():
679
- _buffer = ctypes .create_string_buffer (16 )
680
- res = _uuid_generate_time_safe (_buffer )
681
- return bytes (_buffer .raw ), res
682
- _has_uuid_generate_time_safe = True
683
- break
684
-
685
- elif hasattr (lib , 'uuid_generate_time' ): # pragma: nocover
686
- _uuid_generate_time = lib .uuid_generate_time
687
- # void uuid_generate_time(uuid_t out);
688
- _uuid_generate_time .restype = None
689
- def _generate_time_safe ():
690
- _buffer = ctypes .create_string_buffer (16 )
691
- _uuid_generate_time (_buffer )
692
- return bytes (_buffer .raw ), None
693
- break
694
-
695
- # On Windows prior to 2000, UuidCreate gives a UUID containing the
696
- # hardware address. On Windows 2000 and later, UuidCreate makes a
697
- # random UUID and UuidCreateSequential gives a UUID containing the
698
- # hardware address. These routines are provided by the RPC runtime.
699
- # NOTE: at least on Tim's WinXP Pro SP2 desktop box, while the last
700
- # 6 bytes returned by UuidCreateSequential are fixed, they don't appear
701
- # to bear any relationship to the MAC address of any network device
702
- # on the box.
703
- try :
704
- lib = ctypes .windll .rpcrt4
705
- except :
706
- lib = None
707
- _UuidCreate = getattr (lib , 'UuidCreateSequential' ,
708
- getattr (lib , 'UuidCreate' , None ))
709
-
710
- except Exception as exc :
711
- import warnings
712
- warnings .warn (f"Could not find fallback ctypes uuid functions: { exc } " ,
713
- ImportWarning )
582
+ """[DEPRECATED] Platform-specific functions loaded at import time"""
714
583
715
584
716
585
def _unix_getnode ():
717
- """Get the hardware address on Unix using the _uuid extension module
718
- or ctypes."""
719
- _load_system_functions ()
720
- uuid_time , _ = _generate_time_safe ()
721
- return UUID (bytes = uuid_time ).node
586
+ """Get the hardware address on Unix using the _uuid extension module."""
587
+ if _generate_time_safe :
588
+ uuid_time , _ = _generate_time_safe ()
589
+ return UUID (bytes = uuid_time ).node
722
590
723
591
def _windll_getnode ():
724
- """Get the hardware address on Windows using ctypes."""
725
- import ctypes
726
- _load_system_functions ()
727
- _buffer = ctypes .create_string_buffer (16 )
728
- if _UuidCreate (_buffer ) == 0 :
729
- return UUID (bytes = bytes_ (_buffer .raw )).node
592
+ """Get the hardware address on Windows using the _uuid extension module."""
593
+ if _UuidCreate :
594
+ uuid_bytes = _UuidCreate ()
595
+ return UUID (bytes_le = uuid_bytes ).node
730
596
731
597
def _random_getnode ():
732
598
"""Get a random node ID."""
@@ -755,7 +621,8 @@ def _random_getnode():
755
621
elif _DARWIN :
756
622
_OS_GETTERS = [_ifconfig_getnode , _arp_getnode , _netstat_getnode ]
757
623
elif _WINDOWS :
758
- _OS_GETTERS = [_netbios_getnode , _ipconfig_getnode ]
624
+ # bpo-40201: _windll_getnode will always succeed, so these are not needed
625
+ _OS_GETTERS = []
759
626
elif _AIX :
760
627
_OS_GETTERS = [_netstat_getnode ]
761
628
else :
@@ -802,7 +669,6 @@ def uuid1(node=None, clock_seq=None):
802
669
803
670
# When the system provides a version-1 UUID generator, use it (but don't
804
671
# use UuidCreate here because its UUIDs don't conform to RFC 4122).
805
- _load_system_functions ()
806
672
if _generate_time_safe is not None and node is clock_seq is None :
807
673
uuid_time , safely_generated = _generate_time_safe ()
808
674
try :
0 commit comments