[Python-checkins] cpython: Issue #17177: stop using imp for compileall.

brett.cannon python-checkins at python.org
Sat Jun 15 05:04:12 CEST 2013


http://hg.python.org/cpython/rev/cc27d50bd91a
changeset: 84140:cc27d50bd91a
user: Brett Cannon <brett at python.org>
date: Fri Jun 14 23:04:02 2013 -0400
summary:
 Issue #17177: stop using imp for compileall.
files:
 Lib/compileall.py | 11 +++--
 Lib/test/test_compileall.py | 50 +++++++++++-------------
 2 files changed, 29 insertions(+), 32 deletions(-)
diff --git a/Lib/compileall.py b/Lib/compileall.py
--- a/Lib/compileall.py
+++ b/Lib/compileall.py
@@ -13,7 +13,7 @@
 import os
 import sys
 import errno
-import imp
+import importlib.util
 import py_compile
 import struct
 
@@ -91,17 +91,18 @@
 cfile = fullname + ('c' if __debug__ else 'o')
 else:
 if optimize >= 0:
- cfile = imp.cache_from_source(fullname,
- debug_override=not optimize)
+ cfile = importlib.util.cache_from_source(
+ fullname, debug_override=not optimize)
 else:
- cfile = imp.cache_from_source(fullname)
+ cfile = importlib.util.cache_from_source(fullname)
 cache_dir = os.path.dirname(cfile)
 head, tail = name[:-3], name[-3:]
 if tail == '.py':
 if not force:
 try:
 mtime = int(os.stat(fullname).st_mtime)
- expect = struct.pack('<4sl', imp.get_magic(), mtime)
+ expect = struct.pack('<4sl', importlib.util.MAGIC_NUMBER,
+ mtime)
 with open(cfile, 'rb') as chandle:
 actual = chandle.read(8)
 if expect == actual:
diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py
--- a/Lib/test/test_compileall.py
+++ b/Lib/test/test_compileall.py
@@ -1,11 +1,12 @@
 import sys
 import compileall
-import imp
+import importlib.util
 import os
 import py_compile
 import shutil
 import struct
 import subprocess
+import sys
 import tempfile
 import time
 import unittest
@@ -18,11 +19,11 @@
 def setUp(self):
 self.directory = tempfile.mkdtemp()
 self.source_path = os.path.join(self.directory, '_test.py')
- self.bc_path = imp.cache_from_source(self.source_path)
+ self.bc_path = importlib.util.cache_from_source(self.source_path)
 with open(self.source_path, 'w') as file:
 file.write('x = 123\n')
 self.source_path2 = os.path.join(self.directory, '_test2.py')
- self.bc_path2 = imp.cache_from_source(self.source_path2)
+ self.bc_path2 = importlib.util.cache_from_source(self.source_path2)
 shutil.copyfile(self.source_path, self.source_path2)
 self.subdirectory = os.path.join(self.directory, '_subdir')
 os.mkdir(self.subdirectory)
@@ -36,7 +37,7 @@
 with open(self.bc_path, 'rb') as file:
 data = file.read(8)
 mtime = int(os.stat(self.source_path).st_mtime)
- compare = struct.pack('<4sl', imp.get_magic(), mtime)
+ compare = struct.pack('<4sl', importlib.util.MAGIC_NUMBER, mtime)
 return data, compare
 
 def recreation_check(self, metadata):
@@ -57,7 +58,8 @@
 
 def test_mtime(self):
 # Test a change in mtime leads to a new .pyc.
- self.recreation_check(struct.pack('<4sl', imp.get_magic(), 1))
+ self.recreation_check(struct.pack('<4sl', importlib.util.MAGIC_NUMBER,
+ 1))
 
 def test_magic_number(self):
 # Test a change in mtime leads to a new .pyc.
@@ -97,14 +99,14 @@
 # interpreter's creates the correct file names
 optimize = 1 if __debug__ else 0
 compileall.compile_dir(self.directory, quiet=True, optimize=optimize)
- cached = imp.cache_from_source(self.source_path,
- debug_override=not optimize)
+ cached = importlib.util.cache_from_source(self.source_path,
+ debug_override=not optimize)
 self.assertTrue(os.path.isfile(cached))
- cached2 = imp.cache_from_source(self.source_path2,
- debug_override=not optimize)
+ cached2 = importlib.util.cache_from_source(self.source_path2,
+ debug_override=not optimize)
 self.assertTrue(os.path.isfile(cached2))
- cached3 = imp.cache_from_source(self.source_path3,
- debug_override=not optimize)
+ cached3 = importlib.util.cache_from_source(self.source_path3,
+ debug_override=not optimize)
 self.assertTrue(os.path.isfile(cached3))
 
 
@@ -152,10 +154,12 @@
 return rc, out, err
 
 def assertCompiled(self, fn):
- self.assertTrue(os.path.exists(imp.cache_from_source(fn)))
+ path = importlib.util.cache_from_source(fn)
+ self.assertTrue(os.path.exists(path))
 
 def assertNotCompiled(self, fn):
- self.assertFalse(os.path.exists(imp.cache_from_source(fn)))
+ path = importlib.util.cache_from_source(fn)
+ self.assertFalse(os.path.exists(path))
 
 def setUp(self):
 self.addCleanup(self._cleanup)
@@ -190,8 +194,8 @@
 ['-m', 'compileall', '-q', self.pkgdir]))
 # Verify the __pycache__ directory contents.
 self.assertTrue(os.path.exists(self.pkgdir_cachedir))
- expected = sorted(base.format(imp.get_tag(), ext) for base in
- ('__init__.{}.{}', 'bar.{}.{}'))
+ expected = sorted(base.format(sys.implementation.cache_tag, ext)
+ for base in ('__init__.{}.{}', 'bar.{}.{}'))
 self.assertEqual(sorted(os.listdir(self.pkgdir_cachedir)), expected)
 # Make sure there are no .pyc files in the source directory.
 self.assertFalse([fn for fn in os.listdir(self.pkgdir)
@@ -224,7 +228,7 @@
 
 def test_force(self):
 self.assertRunOK('-q', self.pkgdir)
- pycpath = imp.cache_from_source(self.barfn)
+ pycpath = importlib.util.cache_from_source(self.barfn)
 # set atime/mtime backward to avoid file timestamp resolution issues
 os.utime(pycpath, (time.time()-60,)*2)
 mtime = os.stat(pycpath).st_mtime
@@ -288,7 +292,7 @@
 bazfn = script_helper.make_script(self.pkgdir, 'baz', 'raise Exception')
 self.assertRunOK('-q', '-d', 'dinsdale', self.pkgdir)
 fn = script_helper.make_script(self.pkgdir, 'bing', 'import baz')
- pyc = imp.cache_from_source(bazfn)
+ pyc = importlib.util.cache_from_source(bazfn)
 os.rename(pyc, os.path.join(self.pkgdir, 'baz.pyc'))
 os.remove(bazfn)
 rc, out, err = script_helper.assert_python_failure(fn)
@@ -299,7 +303,7 @@
 '-i', os.path.join(self.directory, 'nosuchfile'), self.pkgdir)
 self.assertRegex(out, b'rror.*nosuchfile')
 self.assertNotRegex(err, b'Traceback')
- self.assertFalse(os.path.exists(imp.cache_from_source(
+ self.assertFalse(os.path.exists(importlib.util.cache_from_source(
 self.pkgdir_cachedir)))
 
 def test_include_file_with_arg(self):
@@ -356,13 +360,5 @@
 self.assertRegex(out, b"Can't list 'badfilename'")
 
 
-def test_main():
- support.run_unittest(
- CommandLineTests,
- CompileallTests,
- EncodingTest,
- )
-
-
 if __name__ == "__main__":
- test_main()
+ unittest.main()
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /