@@ -555,68 +555,15 @@ 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 ()
566
+
620
567
621
568
622
569
_generate_time_safe = _UuidCreate = None
@@ -650,83 +597,25 @@ def _load_system_functions():
650
597
# the test can be adjusted when a later version is fixed.
651
598
pass
652
599
elif _uuid is not None :
653
- _generate_time_safe = _uuid .generate_time_safe
600
+ _generate_time_safe = getattr (_uuid , "generate_time_safe" , None )
601
+ _UuidCreate = getattr (_uuid , "UuidCreate" , None )
654
602
_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 )
714
603
715
604
716
605
def _unix_getnode ():
717
606
"""Get the hardware address on Unix using the _uuid extension module
718
607
or ctypes."""
719
608
_load_system_functions ()
720
- uuid_time , _ = _generate_time_safe ()
721
- return UUID (bytes = uuid_time ).node
609
+ if _generate_time_safe :
610
+ uuid_time , _ = _generate_time_safe ()
611
+ return UUID (bytes = uuid_time ).node
722
612
723
613
def _windll_getnode ():
724
- """Get the hardware address on Windows using ctypes."""
725
- import ctypes
614
+ """Get the hardware address on Windows using UuidCreateSequential."""
726
615
_load_system_functions ()
727
- _buffer = ctypes . create_string_buffer ( 16 )
728
- if _UuidCreate ( _buffer ) == 0 :
729
- return UUID (bytes = bytes_ ( _buffer . raw ) ).node
616
+ if _UuidCreate :
617
+ uuid_bytes = _UuidCreate ()
618
+ return UUID (bytes_le = uuid_bytes ).node
730
619
731
620
def _random_getnode ():
732
621
"""Get a random node ID."""
@@ -755,7 +644,8 @@ def _random_getnode():
755
644
elif _DARWIN :
756
645
_OS_GETTERS = [_ifconfig_getnode , _arp_getnode , _netstat_getnode ]
757
646
elif _WINDOWS :
758
- _OS_GETTERS = [_netbios_getnode , _ipconfig_getnode ]
647
+ # bpo-40201: _windll_getnode will always succeed, so these are not needed
648
+ _OS_GETTERS = []
759
649
elif _AIX :
760
650
_OS_GETTERS = [_netstat_getnode ]
761
651
else :
0 commit comments