]> granicus.if.org Git - python/commitdiff
#19855: uuid.get_node now looks on the PATH for executables on unix.
authorR David Murray <rdmurray@bitdance.com>
Wed, 18 Dec 2013 02:27:56 +0000 (21:27 -0500)
committerR David Murray <rdmurray@bitdance.com>
Wed, 18 Dec 2013 02:27:56 +0000 (21:27 -0500)
Patch by Serhiy Storchaka.

Lib/uuid.py
Misc/NEWS

index 4cd519417c59fbb70619f8b6d5a86abb908149d1..2a70ce69dd34777f2fed3ee4bc27a6b7dcd3309f 100644 (file)
@@ -293,33 +293,38 @@ class UUID(object):
 
 def _find_mac(command, args, hw_identifiers, get_index):
     import os
-    for dir in ['', '/sbin/', '/usr/sbin']:
+    path = os.environ.get("PATH", os.defpath).split(os.pathsep)
+    path.extend(('/sbin', '/usr/sbin'))
+    for dir in path:
         executable = os.path.join(dir, command)
-        if not os.path.exists(executable):
-            continue
+        if (os.path.exists(executable) and
+            os.access(executable, os.F_OK | os.X_OK) and
+            not os.path.isdir(executable)):
+            break
+    else:
+        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:
-                                return int(
-                                    words[get_index(i)].replace(':', ''), 16)
-                            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:
-            continue
-    return None
+    try:
+        # LC_MESSAGES to get English output, 2>/dev/null to
+        # prevent output on stderr
+        cmd = 'LC_MESSAGES=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 (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."""
index d4a1206d490c99ea9e89073c42b170488531acea..ec5a483c00401e998907d08efc7eb9ee605f8360 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,10 @@ Core and Builtins
 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.
   Original patch by Simon Sapin.