]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #11508: Fixed uuid.getnode() and uuid.uuid1() on environment with
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 26 Nov 2013 20:47:16 +0000 (22:47 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 26 Nov 2013 20:47:16 +0000 (22:47 +0200)
virtual interface.  Original patch by Kent Frazier.

Lib/test/test_uuid.py
Lib/uuid.py
Misc/ACKS
Misc/NEWS

index 97ad6d0c2fb2e8914d9caa937bb531c7a6d7cb17..0a47a91a73bba7e9afcb6ea59ee011bb92c7fbb3 100644 (file)
@@ -1,5 +1,7 @@
 import unittest
+from test import support
 import builtins
+import io
 import os
 import uuid
 
@@ -356,6 +358,25 @@ class TestUUID(unittest.TestCase):
 
         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.StringIO(data)
+
+        with 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
index 9226fd4ebbeb7c687091a67575bc7ce84b864cd3..d5c3160fdfdf375763bd39278daef7ba973cd033 100644 (file)
@@ -327,8 +327,16 @@ def _find_mac(command, args, hw_identifiers, get_index):
                     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
index 5b0f8b045b98eaa31777ef3858a2c6f7add37988..2ad044aad99b290f8b5479f384753dc94cc6110d 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -394,6 +394,7 @@ John Fouhy
 Andrew Francis
 Stefan Franke
 Martin Franklin
+Kent Frazier
 Bruce Frederiksen
 Robin Friedrich
 Bradley Froehle
index 84217e7b2188c5446f901012f82fc93bb6c31333..07f5b8fd3336e24ef0c47742fa91d82776c21de3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -16,6 +16,9 @@ Core and Builtins
 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.
 
 - Issue #19545: Avoid chained exceptions while passing stray % to