Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56507c7

Browse files
Issue #11508: Fixed uuid.getnode() and uuid.uuid1() on environment with
virtual interface. Original patch by Kent Frazier.
1 parent a04f4e0 commit 56507c7

4 files changed

Lines changed: 35 additions & 2 deletions

File tree

Lib/test/test_uuid.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import unittest
2+
from test import support
23
import builtins
4+
import io
35
import os
46
import uuid
57

@@ -356,6 +358,25 @@ def test_getnode(self):
356358

357359
self.assertEqual(node1, node2)
358360

361+
def test_find_mac(self):
362+
data = '''\
363+
364+
fake hwaddr
365+
cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
366+
eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab
367+
'''
368+
def mock_popen(cmd):
369+
return io.StringIO(data)
370+
371+
with support.swap_attr(os, 'popen', mock_popen):
372+
mac = uuid._find_mac(
373+
command='ifconfig',
374+
args='',
375+
hw_identifiers=['hwaddr'],
376+
get_index=lambda x: x + 1,
377+
)
378+
self.assertEqual(mac, 0x1234567890ab)
379+
359380
@unittest.skipUnless(importable('ctypes'), 'requires ctypes')
360381
def test_uuid1(self):
361382
equal = self.assertEqual

Lib/uuid.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,16 @@ def _find_mac(command, args, hw_identifiers, get_index):
327327
words = line.lower().split()
328328
for i in range(len(words)):
329329
if words[i] in hw_identifiers:
330-
return int(
331-
words[get_index(i)].replace(':', ''), 16)
330+
try:
331+
return int(
332+
words[get_index(i)].replace(':', ''), 16)
333+
except (ValueError, IndexError):
334+
# Virtual interfaces, such as those provided by
335+
# VPNs, do not have a colon-delimited MAC address
336+
# as expected, but a 16-byte HWAddr separated by
337+
# dashes. These should be ignored in favor of a
338+
# real MAC address
339+
pass
332340
except IOError:
333341
continue
334342
return None

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ John Fouhy
394394
Andrew Francis
395395
Stefan Franke
396396
Martin Franklin
397+
Kent Frazier
397398
Bruce Frederiksen
398399
Robin Friedrich
399400
Bradley Froehle

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Core and Builtins
1616
Library
1717
-------
1818

19+
- Issue #11508: Fixed uuid.getnode() and uuid.uuid1() on environment with
20+
virtual interface. Original patch by Kent Frazier.
21+
1922
- Issue #11489: JSON decoder now accepts lone surrogates.
2023

2124
- Issue #19545: Avoid chained exceptions while passing stray % to

0 commit comments

Comments
 (0)