diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-10-21 22:33:10 +0200 |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-10-21 22:33:10 +0200 |
commit | 6d074197bca8d762b0eff3be255d42b25f1f22b1 (patch) | |
tree | 896173f3eb75e274da8cca58167af2a64a45a790 /Lib/test/test_uuid.py | |
parent | a3c8d4956292d4647887aaeffdd15b018fa70c34 (diff) | |
download | cpython-6d074197bca8d762b0eff3be255d42b25f1f22b1.tar.gz |
Issue #22637: avoid using a shell in uuid
Replace os.popen() with subprocess.Popen() in the uuid module.
Diffstat (limited to 'Lib/test/test_uuid.py')
-rw-r--r-- | Lib/test/test_uuid.py | 38 |
1 files changed, 19 insertions, 19 deletions
diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py index 72648089b7..115e66c216 100644 --- a/Lib/test/test_uuid.py +++ b/Lib/test/test_uuid.py @@ -1,9 +1,10 @@ -import unittest +import unittest.mock from test import support import builtins import io import os import shutil +import subprocess import uuid def importable(name): @@ -361,28 +362,27 @@ class TestUUID(unittest.TestCase): @unittest.skipUnless(os.name == 'posix', 'requires Posix') def test_find_mac(self): - data = '''\ - + 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) - - if shutil.which('ifconfig') is None: - path = os.pathsep.join(('/sbin', '/usr/sbin')) - if shutil.which('ifconfig', path=path) is None: - self.skipTest('requires ifconfig') - - 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) + + popen = unittest.mock.MagicMock() + popen.stdout = io.BytesIO(data.encode()) + + with unittest.mock.patch.object(shutil, 'which', + return_value='/sbin/ifconfig'): + with unittest.mock.patch.object(subprocess, 'Popen', + return_value=popen): + mac = uuid._find_mac( + command='ifconfig', + arg='', + hw_identifiers=[b'hwaddr'], + get_index=lambda x: x + 1, + ) + + self.assertEqual(mac, 0x1234567890ab) @unittest.skipUnless(importable('ctypes'), 'requires ctypes') def test_uuid1(self): |