aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib-python/3/uuid.py')
-rw-r--r--lib-python/3/uuid.py49
1 files changed, 30 insertions, 19 deletions
diff --git a/lib-python/3/uuid.py b/lib-python/3/uuid.py
index 2aab4231d0..3e70a912c4 100644
--- a/lib-python/3/uuid.py
+++ b/lib-python/3/uuid.py
@@ -327,26 +327,35 @@ class UUID(object):
return int((self.int >> 76) & 0xf)
def _find_mac(command, args, hw_identifiers, get_index):
- import os
- for dir in ['', '/sbin/', '/usr/sbin']:
- executable = os.path.join(dir, command)
- if not os.path.exists(executable):
- continue
+ import os, shutil
+ executable = shutil.which(command)
+ if executable is None:
+ path = os.pathsep.join(('/sbin', '/usr/sbin'))
+ executable = shutil.which(command, path=path)
+ if executable is None:
+ return None
- try:
- # LC_ALL to get English output, 2>/dev/null to
- # prevent output on stderr
- cmd = 'LC_ALL=C %s %s 2>/dev/null' % (executable, args)
- with os.popen(cmd) as pipe:
- for line in pipe:
- words = line.lower().split()
- for i in range(len(words)):
- if words[i] in hw_identifiers:
+ try:
+ # LC_ALL to ensure English output, 2>/dev/null to
+ # prevent output on stderr
+ cmd = 'LC_ALL=C %s %s 2>/dev/null' % (executable, args)
+ with os.popen(cmd) as pipe:
+ for line in pipe:
+ words = line.lower().split()
+ for i in range(len(words)):
+ if words[i] in hw_identifiers:
+ try:
return int(
words[get_index(i)].replace(':', ''), 16)
- except IOError:
- continue
- return None
+ except (ValueError, IndexError):
+ # Virtual interfaces, such as those provided by
+ # VPNs, do not have a colon-delimited MAC address
+ # as expected, but a 16-byte HWAddr separated by
+ # dashes. These should be ignored in favor of a
+ # real MAC address
+ pass
+ except IOError:
+ pass
def _ifconfig_getnode():
"""Get the hardware address on Unix by running ifconfig."""
@@ -421,7 +430,7 @@ def _netbios_getnode():
if win32wnet.Netbios(ncb) != 0:
continue
status._unpack()
- bytes = map(ord, status.adapter_address)
+ bytes = status.adapter_address
return ((bytes[0]<<40) + (bytes[1]<<32) + (bytes[2]<<24) +
(bytes[3]<<16) + (bytes[4]<<8) + bytes[5])
@@ -444,6 +453,8 @@ try:
_uuid_generate_random = lib.uuid_generate_random
if hasattr(lib, 'uuid_generate_time'):
_uuid_generate_time = lib.uuid_generate_time
+ if _uuid_generate_random is not None:
+ break # found everything we were looking for
# The uuid_generate_* functions are broken on MacOS X 10.5, as noted
# in issue #8621 the function generates the same sequence of values
@@ -455,7 +466,7 @@ try:
import sys
if sys.platform == 'darwin':
import os
- if int(os.uname()[2].split('.')[0]) >= 9:
+ if int(os.uname().release.split('.')[0]) >= 9:
_uuid_generate_random = _uuid_generate_time = None
# On Windows prior to 2000, UuidCreate gives a UUID containing the