#19855: uuid.get_node now looks on the PATH for executables on unix.

Patch by Serhiy Storchaka.
This commit is contained in:
R David Murray 2013-12-17 21:13:16 -05:00
parent 320b39158e
commit 4be1e24933
2 changed files with 32 additions and 27 deletions

View File

@ -312,34 +312,35 @@ class UUID(object):
return int((self.int >> 76) & 0xf) return int((self.int >> 76) & 0xf)
def _find_mac(command, args, hw_identifiers, get_index): def _find_mac(command, args, hw_identifiers, get_index):
import os import os, shutil
for dir in ['', '/sbin/', '/usr/sbin']: executable = shutil.which(command)
executable = os.path.join(dir, command) if executable is None:
if not os.path.exists(executable): path = os.pathsep.join(('/sbin', '/usr/sbin'))
continue executable = shutil.which(command, path=path)
if executable is None:
return None
try: try:
# LC_ALL to get English output, 2>/dev/null to # LC_MESSAGES to get English output, 2>/dev/null to
# prevent output on stderr # prevent output on stderr
cmd = 'LC_ALL=C %s %s 2>/dev/null' % (executable, args) cmd = 'LC_MESSAGES=C %s %s 2>/dev/null' % (executable, args)
with os.popen(cmd) as pipe: with os.popen(cmd) as pipe:
for line in pipe: for line in pipe:
words = line.lower().split() words = line.lower().split()
for i in range(len(words)): for i in range(len(words)):
if words[i] in hw_identifiers: if words[i] in hw_identifiers:
try: try:
return int( return int(
words[get_index(i)].replace(':', ''), 16) words[get_index(i)].replace(':', ''), 16)
except (ValueError, IndexError): except (ValueError, IndexError):
# Virtual interfaces, such as those provided by # Virtual interfaces, such as those provided by
# VPNs, do not have a colon-delimited MAC address # VPNs, do not have a colon-delimited MAC address
# as expected, but a 16-byte HWAddr separated by # as expected, but a 16-byte HWAddr separated by
# dashes. These should be ignored in favor of a # dashes. These should be ignored in favor of a
# real MAC address # real MAC address
pass pass
except IOError: except IOError:
continue pass
return None
def _ifconfig_getnode(): def _ifconfig_getnode():
"""Get the hardware address on Unix by running ifconfig.""" """Get the hardware address on Unix by running ifconfig."""

View File

@ -29,6 +29,10 @@ Core and Builtins
Library Library
------- -------
- Issue #19855: uuid.getnode() on Unix now looks on the PATH for the
executables used to find the mac address, with /sbin and /usr/sbin as
fallbacks.
- Issue #20007: HTTPResponse.read(0) no more prematurely closes connection. - Issue #20007: HTTPResponse.read(0) no more prematurely closes connection.
Original patch by Simon Sapin. Original patch by Simon Sapin.