changeset 87593:72951ffbdc76 2.7

Issue #11508: Fixed uuid.getnode() and uuid.uuid1() on environment with virtual interface. Original patch by Kent Frazier.
author Serhiy Storchaka <storchaka@gmail.com>
date Tue, 26 Nov 2013 22:47:05 +0200
parents c85305a54e6d
children cbf30e9aa586
files Lib/test/test_uuid.py Lib/uuid.py Misc/ACKS Misc/NEWS
diffstat 4 files changed, 34 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/Lib/test/test_uuid.py	Tue Nov 26 21:25:15 2013 +0200
+++ b/Lib/test/test_uuid.py	Tue Nov 26 22:47:05 2013 +0200
@@ -1,5 +1,6 @@
 import unittest
 from test import test_support
+import io
 import os
 import uuid
 
@@ -346,6 +347,25 @@
 
         self.assertEqual(node1, node2)
 
+    def test_find_mac(self):
+        data = '''\
+
+fake hwaddr
+cscotun0  Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
+eth0      Link encap:Ethernet  HWaddr 12:34:56:78:90:ab
+'''
+        def mock_popen(cmd):
+            return io.BytesIO(data)
+
+        with test_support.swap_attr(os, 'popen', mock_popen):
+            mac = uuid._find_mac(
+                command='ifconfig',
+                args='',
+                hw_identifiers=['hwaddr'],
+                get_index=lambda x: x + 1,
+            )
+            self.assertEqual(mac, 0x1234567890ab)
+
     @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
     def test_uuid1(self):
         equal = self.assertEqual
--- a/Lib/uuid.py	Tue Nov 26 21:25:15 2013 +0200
+++ b/Lib/uuid.py	Tue Nov 26 22:47:05 2013 +0200
@@ -307,8 +307,16 @@
                     words = line.lower().split()
                     for i in range(len(words)):
                         if words[i] in hw_identifiers:
-                            return int(
-                                words[get_index(i)].replace(':', ''), 16)
+                            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
--- a/Misc/ACKS	Tue Nov 26 21:25:15 2013 +0200
+++ b/Misc/ACKS	Tue Nov 26 22:47:05 2013 +0200
@@ -324,6 +324,7 @@
 John Fouhy
 Stefan Franke
 Martin Franklin
+Kent Frazier
 Bruce Frederiksen
 Robin Friedrich
 Bradley Froehle
--- a/Misc/NEWS	Tue Nov 26 21:25:15 2013 +0200
+++ b/Misc/NEWS	Tue Nov 26 22:47:05 2013 +0200
@@ -15,6 +15,9 @@
 Library
 -------
 
+- Issue #11508: Fixed uuid.getnode() and uuid.uuid1() on environment with
+  virtual interface.  Original patch by Kent Frazier.
+
 - Issue #11489: JSON decoder now accepts lone surrogates.
 
 - Fix test.test_support.bind_port() to not cause an error when Python was