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

Skip to content

Commit 9529fbf

Browse files
committed
Issue #17177: Stop using imp in a bunch of tests
1 parent 8a2a902 commit 9529fbf

7 files changed

Lines changed: 17 additions & 15 deletions

File tree

Lib/test/script_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import shutil
1313
import zipfile
1414

15-
from imp import source_from_cache
15+
from importlib.util import source_from_cache
1616
from test.support import make_legacy_pyc, strip_python_stderr
1717

1818
# Executing the interpreter in a subprocess

Lib/test/support.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
import warnings
1616
import unittest
1717
import importlib
18+
import importlib.util
1819
import collections.abc
1920
import re
2021
import subprocess
21-
import imp
2222
import time
2323
import sysconfig
2424
import fnmatch
@@ -316,7 +316,7 @@ def make_legacy_pyc(source):
316316
does not need to exist, however the PEP 3147 pyc file must exist.
317317
:return: The file system path to the legacy pyc file.
318318
"""
319-
pyc_file = imp.cache_from_source(source)
319+
pyc_file = importlib.util.cache_from_source(source)
320320
up_one = os.path.dirname(os.path.abspath(source))
321321
legacy_pyc = os.path.join(up_one, source + ('c' if __debug__ else 'o'))
322322
os.rename(pyc_file, legacy_pyc)
@@ -335,8 +335,8 @@ def forget(modname):
335335
# combinations of PEP 3147 and legacy pyc and pyo files.
336336
unlink(source + 'c')
337337
unlink(source + 'o')
338-
unlink(imp.cache_from_source(source, debug_override=True))
339-
unlink(imp.cache_from_source(source, debug_override=False))
338+
unlink(importlib.util.cache_from_source(source, debug_override=True))
339+
unlink(importlib.util.cache_from_source(source, debug_override=False))
340340

341341
# On some platforms, should not run gui test even if it is allowed
342342
# in `use_resources'.

Lib/test/test_pdb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# A test suite for pdb; not very comprehensive at the moment.
22

33
import doctest
4-
import imp
54
import pdb
65
import sys
6+
import types
77
import unittest
88
import subprocess
99
import textwrap
@@ -464,7 +464,7 @@ def test_pdb_skip_modules():
464464

465465

466466
# Module for testing skipping of module that makes a callback
467-
mod = imp.new_module('module_to_skip')
467+
mod = types.ModuleType('module_to_skip')
468468
exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__)
469469

470470

Lib/test/test_pkgimport.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import tempfile
77
import unittest
88

9-
from imp import cache_from_source
9+
from importlib.util import cache_from_source
1010
from test.support import run_unittest, create_empty_file
1111

1212
class TestImport(unittest.TestCase):

Lib/test/test_pkgutil.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from test.support import run_unittest, unload, check_warnings
22
import unittest
33
import sys
4-
import imp
54
import importlib
65
import pkgutil
76
import os
87
import os.path
98
import tempfile
9+
import types
1010
import shutil
1111
import zipfile
1212

@@ -105,7 +105,7 @@ class PkgutilPEP302Tests(unittest.TestCase):
105105
class MyTestLoader(object):
106106
def load_module(self, fullname):
107107
# Create an empty module
108-
mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
108+
mod = sys.modules.setdefault(fullname, types.ModuleType(fullname))
109109
mod.__file__ = "<%s>" % self.__class__.__name__
110110
mod.__loader__ = self
111111
# Make it a package

Lib/test/test_reprlib.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
Nick Mathewson
44
"""
55

6-
import imp
76
import sys
87
import os
98
import shutil
109
import importlib
10+
import importlib.util
1111
import unittest
1212

1313
from test.support import run_unittest, create_empty_file, verbose
@@ -241,7 +241,8 @@ def _check_path_limitations(self, module_name):
241241
source_path_len += 2 * (len(self.longname) + 1)
242242
# a path separator + `module_name` + ".py"
243243
source_path_len += len(module_name) + 1 + len(".py")
244-
cached_path_len = source_path_len + len(imp.cache_from_source("x.py")) - len("x.py")
244+
cached_path_len = (source_path_len +
245+
len(importlib.util.cache_from_source("x.py")) - len("x.py"))
245246
if os.name == 'nt' and cached_path_len >= 258:
246247
# Under Windows, the max path len is 260 including C's terminating
247248
# NUL character.

Lib/test/test_zipimport.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
import os
33
import marshal
4-
import imp
4+
import importlib.util
55
import struct
66
import time
77
import unittest
@@ -34,7 +34,8 @@ def make_pyc(co, mtime, size):
3434
mtime = int(mtime)
3535
else:
3636
mtime = int(-0x100000000 + int(mtime))
37-
pyc = imp.get_magic() + struct.pack("<ii", int(mtime), size & 0xFFFFFFFF) + data
37+
pyc = (importlib.util.MAGIC_NUMBER +
38+
struct.pack("<ii", int(mtime), size & 0xFFFFFFFF) + data)
3839
return pyc
3940

4041
def module_path_to_dotted_name(path):
@@ -49,7 +50,7 @@ def module_path_to_dotted_name(path):
4950
TESTPACK2 = "ziptestpackage2"
5051
TEMP_ZIP = os.path.abspath("junk95142.zip")
5152

52-
pyc_file = imp.cache_from_source(TESTMOD + '.py')
53+
pyc_file = importlib.util.cache_from_source(TESTMOD + '.py')
5354
pyc_ext = ('.pyc' if __debug__ else '.pyo')
5455

5556

0 commit comments

Comments
 (0)