From 6d9d30da6a02a259c6d3df21c9e7f2aa5b3fd820 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 26 Nov 2013 22:47:05 +0200 Subject: [PATCH] Issue #11508: Fixed uuid.getnode() and uuid.uuid1() on environment with virtual interface. Original patch by Kent Frazier. --- Lib/test/test_uuid.py | 20 ++++++++++++++++++++ Lib/uuid.py | 12 ++++++++++-- Misc/ACKS | 1 + Misc/NEWS | 3 +++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py index 9de3d789c5..55b939f0f6 100644 --- a/Lib/test/test_uuid.py +++ b/Lib/test/test_uuid.py @@ -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 diff --git a/Lib/uuid.py b/Lib/uuid.py index cb4e5f050f..4cd519417c 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -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 diff --git a/Misc/ACKS b/Misc/ACKS index e276960db7..5cdc3c2ce4 100644 --- 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 diff --git a/Misc/NEWS b/Misc/NEWS index 6815aa22e8..57e3c35bc1 100644 --- 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 -- 2.50.1