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

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

index 9de3d789c5e2952107c28ebe1ab8d2f1b70a20a0..55b939f0f63e63cd5d388c9a9b18b63b2f8899b0 100644 (file)
@@ -1,5 +1,6 @@
 import unittest
 from test import test_support
+import io
 import os
 import uuid
 
@@ -346,6 +347,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.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
index cb4e5f050f1bdd90cd2ef91458a648c71a9a001a..4cd519417c59fbb70619f8b6d5a86abb908149d1 100644 (file)
@@ -307,8 +307,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 e276960db78e75b7610c8a082b581780433b08f0..5cdc3c2ce4150bf1f367166dcbcca932a26d1257 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -324,6 +324,7 @@ Doug Fort
 John Fouhy
 Stefan Franke
 Martin Franklin
+Kent Frazier
 Bruce Frederiksen
 Robin Friedrich
 Bradley Froehle
index 6815aa22e86b9c926176b5859763bece3f1edeef..57e3c35bc11c8f0deb578073786c0d0e2b567425 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,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.
 
 - Fix test.test_support.bind_port() to not cause an error when Python was