You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 23df2d1
Browse filesBrowse the repository at this point in the historyBrowse files
bpo-32107 - Improve MAC address calculation and fix test_uuid.py (#4600)
``uuid.getnode()`` now preferentially returns universally administered MAC addresses if available, over locally administered MAC addresses. This makes a better guarantee for global uniqueness of UUIDs returned from ``uuid.uuid1()``. If only locally administered MAC addresses are available, the first such one found is returned.
Also improve internal code style by being explicit about ``return None`` rather than falling off the end of the function.
Improve the test robustness.
"""Get the hardware address on Windows using NetBIOS calls.
465
498
See http://support.microsoft.com/kb/118623 for details."""
466
499
importwin32wnet, netbios
500
+
first_local_mac=None
467
501
ncb=netbios.NCB()
468
502
ncb.Command=netbios.NCBENUM
469
503
ncb.Buffer=adapters=netbios.LANA_ENUM()
470
504
adapters._pack()
471
505
ifwin32wnet.Netbios(ncb) !=0:
472
-
return
506
+
returnNone
473
507
adapters._unpack()
474
508
foriinrange(adapters.length):
475
509
ncb.Reset()
@@ -488,7 +522,11 @@ def _netbios_getnode():
488
522
bytes=status.adapter_address[:6]
489
523
iflen(bytes) !=6:
490
524
continue
491
-
returnint.from_bytes(bytes, 'big')
525
+
mac=int.from_bytes(bytes, 'big')
526
+
if_is_universal(mac):
527
+
returnmac
528
+
first_local_mac=first_local_macormac
529
+
returnfirst_local_macorNone
492
530
493
531
494
532
_generate_time_safe=_UuidCreate=None
@@ -601,9 +639,19 @@ def _windll_getnode():
601
639
returnUUID(bytes=bytes_(_buffer.raw)).node
602
640
603
641
def_random_getnode():
604
-
"""Get a random node ID, with eighth bit set as suggested by RFC 4122."""
642
+
"""Get a random node ID."""
643
+
# RFC 4122, $4.1.6 says "For systems with no IEEE address, a randomly or
644
+
# pseudo-randomly generated value may be used; see Section 4.5. The
645
+
# multicast bit must be set in such addresses, in order that they will
646
+
# never conflict with addresses obtained from network cards."
647
+
#
648
+
# The "multicast bit" of a MAC address is defined to be "the least
649
+
# significant bit of the first octet". This works out to be the 41st bit
650
+
# counting from 1 being the least significant bit, or 1<<40.
651
+
#
652
+
# See https://en.wikipedia.org/wiki/MAC_address#Unicast_vs._multicast
0 commit comments