diff options
Diffstat (limited to 'Lib/test/test_imp.py')
-rw-r--r-- | Lib/test/test_imp.py | 141 |
1 files changed, 91 insertions, 50 deletions
diff --git a/Lib/test/test_imp.py b/Lib/test/test_imp.py index 551ad1b..a660278 100644 --- a/Lib/test/test_imp.py +++ b/Lib/test/test_imp.py @@ -1,11 +1,12 @@ import imp +import importlib import os import os.path import shutil import sys -import unittest from test import support -import importlib +import unittest +import warnings class LockTests(unittest.TestCase): @@ -58,6 +59,10 @@ class ImportTests(unittest.TestCase): with imp.find_module('module_' + mod, self.test_path)[0] as fd: self.assertEqual(fd.encoding, encoding) + path = [os.path.dirname(__file__)] + with self.assertRaises(SyntaxError): + imp.find_module('badsyntax_pep3120', path) + def test_issue1267(self): for mod, encoding, _ in self.test_strings: fp, filename, info = imp.find_module('module_' + mod, @@ -150,18 +155,24 @@ class ImportTests(unittest.TestCase): mod = imp.load_module(temp_mod_name, file, filename, info) self.assertEqual(mod.a, 1) - mod = imp.load_source(temp_mod_name, temp_mod_name + '.py') + with warnings.catch_warnings(): + warnings.simplefilter('ignore') + mod = imp.load_source(temp_mod_name, temp_mod_name + '.py') self.assertEqual(mod.a, 1) - mod = imp.load_compiled( - temp_mod_name, imp.cache_from_source(temp_mod_name + '.py')) + with warnings.catch_warnings(): + warnings.simplefilter('ignore') + mod = imp.load_compiled( + temp_mod_name, imp.cache_from_source(temp_mod_name + '.py')) self.assertEqual(mod.a, 1) if not os.path.exists(test_package_name): os.mkdir(test_package_name) with open(init_file_name, 'w') as file: file.write('b = 2\n') - package = imp.load_package(test_package_name, test_package_name) + with warnings.catch_warnings(): + warnings.simplefilter('ignore') + package = imp.load_package(test_package_name, test_package_name) self.assertEqual(package.b, 2) finally: del sys.path[0] @@ -175,6 +186,17 @@ class ImportTests(unittest.TestCase): self.assertRaises(SyntaxError, imp.find_module, "badsyntax_pep3120", [path]) + def test_load_dynamic_ImportError_path(self): + # Issue #1559549 added `name` and `path` attributes to ImportError + # in order to provide better detail. Issue #10854 implemented those + # attributes on import failures of extensions on Windows. + path = 'bogus file path' + name = 'extension' + with self.assertRaises(ImportError) as err: + imp.load_dynamic(name, path) + self.assertIn(path, err.exception.path) + self.assertEqual(name, err.exception.name) + class ReloadTests(unittest.TestCase): @@ -209,72 +231,83 @@ class PEP3147Tests(unittest.TestCase): tag = imp.get_tag() + @unittest.skipUnless(sys.implementation.cache_tag is not None, + 'requires sys.implementation.cache_tag not be None') def test_cache_from_source(self): # Given the path to a .py file, return the path to its PEP 3147 # defined .pyc file (i.e. under __pycache__). - self.assertEqual( - imp.cache_from_source('/foo/bar/baz/qux.py', True), - '/foo/bar/baz/__pycache__/qux.{}.pyc'.format(self.tag)) + path = os.path.join('foo', 'bar', 'baz', 'qux.py') + expect = os.path.join('foo', 'bar', 'baz', '__pycache__', + '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__). - self.assertEqual( - imp.cache_from_source('/foo/bar/baz/qux.py', False), - '/foo/bar/baz/__pycache__/qux.{}.pyo'.format(self.tag)) + 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): - self.assertEqual(imp.cache_from_source('foo.py', True), - os.sep.join(('__pycache__', - 'foo.{}.pyc'.format(self.tag)))) + 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. - self.assertEqual( - imp.cache_from_source('/foo/bar/baz.py', []), - '/foo/bar/__pycache__/baz.{}.pyo'.format(self.tag)) - self.assertEqual( - imp.cache_from_source('/foo/bar/baz.py', [17]), - '/foo/bar/__pycache__/baz.{}.pyc'.format(self.tag)) + 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 - self.assertRaises( - RuntimeError, - imp.cache_from_source, '/foo/bar/baz.py', Bearish()) + with self.assertRaises(RuntimeError): + imp.cache_from_source('/foo/bar/baz.py', Bearish()) - @unittest.skipIf(os.altsep is None, - 'test meaningful only where os.altsep is defined') - def test_altsep_cache_from_source(self): - # Windows path and PEP 3147. - self.assertEqual( - imp.cache_from_source('\\foo\\bar\\baz\\qux.py', True), - '\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag)) - - @unittest.skipIf(os.altsep is None, - 'test meaningful only where os.altsep is defined') - def test_altsep_and_sep_cache_from_source(self): - # Windows path and PEP 3147 where altsep is right of sep. - self.assertEqual( - imp.cache_from_source('\\foo\\bar/baz\\qux.py', True), - '\\foo\\bar/baz\\__pycache__\\qux.{}.pyc'.format(self.tag)) - - @unittest.skipIf(os.altsep is None, + @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)) + '\\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') def test_source_from_cache(self): # Given the path to a PEP 3147 defined .pyc file, return the path to # its source. This tests the good path. - self.assertEqual(imp.source_from_cache( - '/foo/bar/baz/__pycache__/qux.{}.pyc'.format(self.tag)), - '/foo/bar/baz/qux.py') + path = os.path.join('foo', 'bar', 'baz', '__pycache__', + 'qux.{}.pyc'.format(self.tag)) + 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 @@ -305,6 +338,12 @@ class PEP3147Tests(unittest.TestCase): '/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) @@ -314,14 +353,16 @@ class PEP3147Tests(unittest.TestCase): shutil.rmtree('pep3147') self.addCleanup(cleanup) # Touch the __init__.py file. - with open('pep3147/__init__.py', 'w'): - pass + 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.forget('pep3147') + support.unload('pep3147') m = __import__('pep3147') - self.assertEqual(m.__file__, - os.sep.join(('.', 'pep3147', '__init__.py'))) + support.unload('pep3147') + self.assertEqual(m.__file__, expected___file__, (m.__file__, m.__path__, sys.path, sys.path_importer_cache)) class NullImporterTests(unittest.TestCase): |