mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
Issue #23731: Implement PEP 488.
The concept of .pyo files no longer exists. Now .pyc files have an optional `opt-` tag which specifies if any extra optimizations beyond the peepholer were applied.
This commit is contained in:
parent
a63cc21234
commit
f299abdafa
56 changed files with 4731 additions and 4621 deletions
|
@ -111,7 +111,6 @@ class ImportTests(unittest.TestCase):
|
|||
del sys.path[0]
|
||||
support.unlink(temp_mod_name + '.py')
|
||||
support.unlink(temp_mod_name + '.pyc')
|
||||
support.unlink(temp_mod_name + '.pyo')
|
||||
|
||||
def test_issue5604(self):
|
||||
# Test cannot cover imp.load_compiled function.
|
||||
|
@ -194,7 +193,7 @@ class ImportTests(unittest.TestCase):
|
|||
self.assertEqual(package.b, 2)
|
||||
finally:
|
||||
del sys.path[0]
|
||||
for ext in ('.py', '.pyc', '.pyo'):
|
||||
for ext in ('.py', '.pyc'):
|
||||
support.unlink(temp_mod_name + ext)
|
||||
support.unlink(init_file_name + ext)
|
||||
support.rmtree(test_package_name)
|
||||
|
@ -346,56 +345,6 @@ class PEP3147Tests(unittest.TestCase):
|
|||
'qux.{}.pyc'.format(self.tag))
|
||||
self.assertEqual(imp.cache_from_source(path, True), expect)
|
||||
|
||||
def test_cache_from_source_no_cache_tag(self):
|
||||
# Non cache tag means NotImplementedError.
|
||||
with support.swap_attr(sys.implementation, 'cache_tag', None):
|
||||
with self.assertRaises(NotImplementedError):
|
||||
imp.cache_from_source('whatever.py')
|
||||
|
||||
def test_cache_from_source_no_dot(self):
|
||||
# Directory with a dot, filename without dot.
|
||||
path = os.path.join('foo.bar', 'file')
|
||||
expect = os.path.join('foo.bar', '__pycache__',
|
||||
'file{}.pyc'.format(self.tag))
|
||||
self.assertEqual(imp.cache_from_source(path, True), expect)
|
||||
|
||||
def test_cache_from_source_optimized(self):
|
||||
# Given the path to a .py file, return the path to its PEP 3147
|
||||
# defined .pyo file (i.e. under __pycache__).
|
||||
path = os.path.join('foo', 'bar', 'baz', 'qux.py')
|
||||
expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
|
||||
'qux.{}.pyo'.format(self.tag))
|
||||
self.assertEqual(imp.cache_from_source(path, False), expect)
|
||||
|
||||
def test_cache_from_source_cwd(self):
|
||||
path = 'foo.py'
|
||||
expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag))
|
||||
self.assertEqual(imp.cache_from_source(path, True), expect)
|
||||
|
||||
def test_cache_from_source_override(self):
|
||||
# When debug_override is not None, it can be any true-ish or false-ish
|
||||
# value.
|
||||
path = os.path.join('foo', 'bar', 'baz.py')
|
||||
partial_expect = os.path.join('foo', 'bar', '__pycache__',
|
||||
'baz.{}.py'.format(self.tag))
|
||||
self.assertEqual(imp.cache_from_source(path, []), partial_expect + 'o')
|
||||
self.assertEqual(imp.cache_from_source(path, [17]),
|
||||
partial_expect + 'c')
|
||||
# However if the bool-ishness can't be determined, the exception
|
||||
# propagates.
|
||||
class Bearish:
|
||||
def __bool__(self): raise RuntimeError
|
||||
with self.assertRaises(RuntimeError):
|
||||
imp.cache_from_source('/foo/bar/baz.py', Bearish())
|
||||
|
||||
@unittest.skipUnless(os.sep == '\\' and os.altsep == '/',
|
||||
'test meaningful only where os.altsep is defined')
|
||||
def test_sep_altsep_and_sep_cache_from_source(self):
|
||||
# Windows path and PEP 3147 where sep is right of altsep.
|
||||
self.assertEqual(
|
||||
imp.cache_from_source('\\foo\\bar\\baz/qux.py', True),
|
||||
'\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag))
|
||||
|
||||
@unittest.skipUnless(sys.implementation.cache_tag is not None,
|
||||
'requires sys.implementation.cache_tag to not be '
|
||||
'None')
|
||||
|
@ -407,68 +356,6 @@ class PEP3147Tests(unittest.TestCase):
|
|||
expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
|
||||
self.assertEqual(imp.source_from_cache(path), expect)
|
||||
|
||||
def test_source_from_cache_no_cache_tag(self):
|
||||
# If sys.implementation.cache_tag is None, raise NotImplementedError.
|
||||
path = os.path.join('blah', '__pycache__', 'whatever.pyc')
|
||||
with support.swap_attr(sys.implementation, 'cache_tag', None):
|
||||
with self.assertRaises(NotImplementedError):
|
||||
imp.source_from_cache(path)
|
||||
|
||||
def test_source_from_cache_bad_path(self):
|
||||
# When the path to a pyc file is not in PEP 3147 format, a ValueError
|
||||
# is raised.
|
||||
self.assertRaises(
|
||||
ValueError, imp.source_from_cache, '/foo/bar/bazqux.pyc')
|
||||
|
||||
def test_source_from_cache_no_slash(self):
|
||||
# No slashes at all in path -> ValueError
|
||||
self.assertRaises(
|
||||
ValueError, imp.source_from_cache, 'foo.cpython-32.pyc')
|
||||
|
||||
def test_source_from_cache_too_few_dots(self):
|
||||
# Too few dots in final path component -> ValueError
|
||||
self.assertRaises(
|
||||
ValueError, imp.source_from_cache, '__pycache__/foo.pyc')
|
||||
|
||||
def test_source_from_cache_too_many_dots(self):
|
||||
# Too many dots in final path component -> ValueError
|
||||
self.assertRaises(
|
||||
ValueError, imp.source_from_cache,
|
||||
'__pycache__/foo.cpython-32.foo.pyc')
|
||||
|
||||
def test_source_from_cache_no__pycache__(self):
|
||||
# Another problem with the path -> ValueError
|
||||
self.assertRaises(
|
||||
ValueError, imp.source_from_cache,
|
||||
'/foo/bar/foo.cpython-32.foo.pyc')
|
||||
|
||||
def test_package___file__(self):
|
||||
try:
|
||||
m = __import__('pep3147')
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
self.fail("pep3147 module already exists: %r" % (m,))
|
||||
# Test that a package's __file__ points to the right source directory.
|
||||
os.mkdir('pep3147')
|
||||
sys.path.insert(0, os.curdir)
|
||||
def cleanup():
|
||||
if sys.path[0] == os.curdir:
|
||||
del sys.path[0]
|
||||
shutil.rmtree('pep3147')
|
||||
self.addCleanup(cleanup)
|
||||
# Touch the __init__.py file.
|
||||
support.create_empty_file('pep3147/__init__.py')
|
||||
importlib.invalidate_caches()
|
||||
expected___file__ = os.sep.join(('.', 'pep3147', '__init__.py'))
|
||||
m = __import__('pep3147')
|
||||
self.assertEqual(m.__file__, expected___file__, (m.__file__, m.__path__, sys.path, sys.path_importer_cache))
|
||||
# Ensure we load the pyc file.
|
||||
support.unload('pep3147')
|
||||
m = __import__('pep3147')
|
||||
support.unload('pep3147')
|
||||
self.assertEqual(m.__file__, expected___file__, (m.__file__, m.__path__, sys.path, sys.path_importer_cache))
|
||||
|
||||
|
||||
class NullImporterTests(unittest.TestCase):
|
||||
@unittest.skipIf(support.TESTFN_UNENCODABLE is None,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue