def test_with_extension(ext): source = TESTFN + ext if is_jython: pyc = TESTFN + '$py.class' else: pyc = TESTFN + '.pyc' with open(source, 'w') as f: print("# This tests Python's ability to import a", ext, 'file.', file=f) a = random.randrange(1000) b = random.randrange(1000) print('a =', a, file=f) print('b =', b, file=f) if TESTFN in sys.modules: del sys.modules[TESTFN] importlib.invalidate_caches() try: try: mod = __import__(TESTFN) except ImportError as err: self.fail('import from %s failed: %s' % (ext, err)) self.assertEqual(mod.a, a, 'module loaded (%s) but contents invalid' % mod) self.assertEqual(mod.b, b, 'module loaded (%s) but contents invalid' % mod) finally: forget(TESTFN) unlink(source) unlink(pyc)
def test_badimport(self): # This tests the fix for issue 5230, where if pydoc found the module # but the module had an internal import error pydoc would report no doc # found. modname = 'testmod_xyzzy' testpairs = ( ('i_am_not_here', 'i_am_not_here'), ('test.i_am_not_here_either', 'i_am_not_here_either'), ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'), ('i_am_not_here.{}'.format(modname), 'i_am_not_here.{}'.format(modname)), ('test.{}'.format(modname), modname), ) @contextmanager def newdirinpath(dir): os.mkdir(dir) sys.path.insert(0, dir) yield sys.path.pop(0) rmtree(dir) with newdirinpath(TESTFN), EnvironmentVarGuard() as env: env['PYTHONPATH'] = TESTFN fullmodname = os.path.join(TESTFN, modname) sourcefn = fullmodname + os.extsep + "py" for importstring, expectedinmsg in testpairs: f = open(sourcefn, 'w') f.write("import {}\n".format(importstring)) f.close() try: result = run_pydoc(modname).decode("ascii") finally: forget(modname) expected = badimport_pattern % (modname, expectedinmsg) self.assertEqual(expected, result)
def test_badimport(self): # This tests the fix for issue 5230, where if pydoc found the module # but the module had an internal import error pydoc would report no doc # found. modname = 'testmod_xyzzy' testpairs = ( ('i_am_not_here', 'i_am_not_here'), ('test.i_am_not_here_either', 'i_am_not_here_either'), ('test.i_am_not_here.neither_am_i', 'i_am_not_here.neither_am_i'), ('i_am_not_here.{}'.format(modname), 'i_am_not_here.{}'.format(modname)), ('test.{}'.format(modname), modname), ) @contextmanager def newdirinpath(dir): os.mkdir(dir) sys.path.insert(0, dir) yield sys.path.pop(0) rmtree(dir) with newdirinpath(TESTFN), EnvironmentVarGuard() as env: env['PYTHONPATH'] = TESTFN fullmodname = os.path.join(TESTFN, modname) sourcefn = fullmodname + os.extsep + "py" for importstring, expectedinmsg in testpairs: with open(sourcefn, 'w') as f: f.write("import {}\n".format(importstring)) try: result = run_pydoc(modname).decode("ascii") finally: forget(modname) expected = badimport_pattern % (modname, expectedinmsg) self.assertEqual(expected, result)
def test_run_name(self): depth = 1 run_name = "And now for something completely different" pkg_dir, mod_fname, mod_name, mod_spec = (self._make_pkg( example_source, depth)) forget(mod_name) expected_ns = example_namespace.copy() expected_ns.update({ "__name__": run_name, "__file__": mod_fname, "__cached__": importlib.util.cache_from_source(mod_fname), "__package__": mod_name.rpartition(".")[0], "__spec__": mod_spec, }) def create_ns(init_globals): return run_module(mod_name, init_globals, run_name) try: self.check_code_execution(create_ns, expected_ns) finally: self._del_pkg(pkg_dir)
def _check_package(self, depth, alter_sys=False): pkg_dir, mod_fname, mod_name = ( self._make_pkg(example_source, depth, "__main__")) pkg_name = mod_name.rpartition(".")[0] forget(mod_name) expected_ns = example_namespace.copy() expected_ns.update({ "__name__": mod_name, "__file__": mod_fname, "__package__": pkg_name, }) if alter_sys: expected_ns.update({ "run_argv0": mod_fname, "run_name_in_sys_modules": True, "module_in_sys_modules": True, }) def create_ns(init_globals): return run_module(pkg_name, init_globals, alter_sys=alter_sys) try: if verbose > 1: print("Running from source:", pkg_name) self.check_code_execution(create_ns, expected_ns) importlib.invalidate_caches() __import__(mod_name) os.remove(mod_fname) make_legacy_pyc(mod_fname) unload(mod_name) # In case loader caches paths if verbose > 1: print("Running from compiled:", pkg_name) importlib.invalidate_caches() self._fix_ns_for_legacy_pyc(expected_ns, alter_sys) self.check_code_execution(create_ns, expected_ns) finally: self._del_pkg(pkg_dir, depth, pkg_name) if verbose > 1: print("Package executed successfully")
def test_with_extension(ext): # The extension is normally ".py", perhaps ".pyw". source = TESTFN + ext if is_jython: pyc = TESTFN + "$py.class" else: pyc = TESTFN + ".pyc" with open(source, "w") as f: print("# This tests Python's ability to import a", ext, "file.", file=f) a = random.randrange(1000) b = random.randrange(1000) print("a =", a, file=f) print("b =", b, file=f) if TESTFN in sys.modules: del sys.modules[TESTFN] importlib.invalidate_caches() try: try: mod = __import__(TESTFN) except ImportError as err: self.fail("import from %s failed: %s" % (ext, err)) self.assertEqual(mod.a, a, "module loaded (%s) but contents invalid" % mod) self.assertEqual(mod.b, b, "module loaded (%s) but contents invalid" % mod) finally: forget(TESTFN) unlink(source) unlink(pyc)
def test_with_extension(ext): # The extension is normally ".py", perhaps ".pyw". source = TESTFN + ext if is_jython: pyc = TESTFN + "$py.class" else: pyc = TESTFN + ".pyc" with open(source, "w") as f: print("# This tests Python's ability to import a", ext, "file.", file=f) a = random.randrange(1000) b = random.randrange(1000) print("a =", a, file=f) print("b =", b, file=f) if TESTFN in sys.modules: del sys.modules[TESTFN] importlib.invalidate_caches() try: try: mod = __import__(TESTFN) except ImportError as err: self.fail("import from %s failed: %s" % (ext, err)) self.assertEqual( mod.a, a, "module loaded (%s) but contents invalid" % mod) self.assertEqual( mod.b, b, "module loaded (%s) but contents invalid" % mod) finally: forget(TESTFN) unlink(source) unlink(pyc)
def _check_package(self, depth): pkg_dir, mod_fname, mod_name = self._make_pkg("x=1\n", depth, "__main__") pkg_name, _, _ = mod_name.rpartition(".") forget(mod_name) try: if verbose: print("Running from source:", pkg_name) d1 = run_module(pkg_name) # Read from source self.assertIn("x", d1) self.assertTrue(d1["x"] == 1) del d1 # Ensure __loader__ entry doesn't keep file open __import__(mod_name) os.remove(mod_fname) make_legacy_pyc(mod_fname) unload(mod_name) # In case loader caches paths if verbose: print("Running from compiled:", pkg_name) d2 = run_module(pkg_name) # Read from bytecode self.assertIn("x", d2) self.assertTrue(d2["x"] == 1) del d2 # Ensure __loader__ entry doesn't keep file open finally: self._del_pkg(pkg_dir, depth, pkg_name) if verbose: print("Package executed successfully")
def _check_package(self, depth): pkg_dir, mod_fname, mod_name = (self._make_pkg("x=1\n", depth, "__main__")) pkg_name, _, _ = mod_name.rpartition(".") forget(mod_name) try: if verbose: print("Running from source:", pkg_name) d1 = run_module(pkg_name) # Read from source self.assertIn("x", d1) self.assertTrue(d1["x"] == 1) del d1 # Ensure __loader__ entry doesn't keep file open importlib.invalidate_caches() __import__(mod_name) os.remove(mod_fname) make_legacy_pyc(mod_fname) unload(mod_name) # In case loader caches paths if verbose: print("Running from compiled:", pkg_name) importlib.invalidate_caches() d2 = run_module(pkg_name) # Read from bytecode self.assertIn("x", d2) self.assertTrue(d2["x"] == 1) del d2 # Ensure __loader__ entry doesn't keep file open finally: self._del_pkg(pkg_dir, depth, pkg_name) if verbose: print("Package executed successfully")
def test_missing_source(self): __import__(TESTFN) pyc_file = importlib.util.cache_from_source(self.source) self.assertTrue(os.path.exists(pyc_file)) os.remove(self.source) forget(TESTFN) importlib.invalidate_caches() self.assertRaises(ImportError, __import__, TESTFN)
def test_missing_source(self): # With PEP 3147 cache layout, removing the source but leaving the pyc # file does not satisfy the import. __import__(TESTFN) pyc_file = imp.cache_from_source(self.source) self.assertTrue(os.path.exists(pyc_file)) os.remove(self.source) forget(TESTFN) self.assertRaises(ImportError, __import__, TESTFN)
def _check_package(self, depth, alter_sys=False, *, namespace=False, parent_namespaces=False): pkg_dir, mod_fname, mod_name, mod_spec = self._make_pkg( example_source, depth, '__main__', namespace=namespace, parent_namespaces=parent_namespaces) pkg_name = mod_name.rpartition('.')[0] forget(mod_name) expected_ns = example_namespace.copy() expected_ns.update({ '__name__': mod_name, '__file__': mod_fname, '__cached__': importlib.util.cache_from_source(mod_fname), '__package__': pkg_name, '__spec__': mod_spec }) if alter_sys: expected_ns.update({ 'run_argv0': mod_fname, 'run_name_in_sys_modules': True, 'module_in_sys_modules': True }) def create_ns(init_globals): return run_module(pkg_name, init_globals, alter_sys=alter_sys) try: if verbose > 1: print('Running from source:', pkg_name) self.check_code_execution(create_ns, expected_ns) importlib.invalidate_caches() __import__(mod_name) os.remove(mod_fname) if not sys.dont_write_bytecode: make_legacy_pyc(mod_fname) unload(mod_name) if verbose > 1: print('Running from compiled:', pkg_name) importlib.invalidate_caches() self._fix_ns_for_legacy_pyc(expected_ns, alter_sys) self.check_code_execution(create_ns, expected_ns) finally: self._del_pkg(pkg_dir) if verbose > 1: print('Package executed successfully')
def test_forget(self): mod_filename = TESTFN + '.py' with open(mod_filename, 'w') as f: print('foo = 1', file=f) sys.path.insert(0, os.curdir) importlib.invalidate_caches() try: mod = __import__(TESTFN) self.assertIn(TESTFN, sys.modules) support.forget(TESTFN) self.assertNotIn(TESTFN, sys.modules) finally: del sys.path[0] support.unlink(mod_filename)
def test_forget(self): mod_filename = TESTFN + '.py' with open(mod_filename, 'wt') as f: f.write('foo = 1\n') sys.path.insert(0, os.curdir) try: mod = __import__(TESTFN) self.assertIn(TESTFN, sys.modules) support.forget(TESTFN) self.assertNotIn(TESTFN, sys.modules) finally: del sys.path[0] support.unlink(mod_filename) support.rmtree('__pycache__')
def test_forget(self): mod_filename = TESTFN + ".py" with open(mod_filename, "w") as f: print("foo = 1", file=f) sys.path.insert(0, os.curdir) importlib.invalidate_caches() try: mod = __import__(TESTFN) self.assertIn(TESTFN, sys.modules) support.forget(TESTFN) self.assertNotIn(TESTFN, sys.modules) finally: del sys.path[0] support.unlink(mod_filename) support.rmtree("__pycache__")
def _check_module(self, depth, alter_sys=False, *, namespace=False, parent_namespaces=False): pkg_dir, mod_fname, mod_name, mod_spec = (self._make_pkg( example_source, depth, namespace=namespace, parent_namespaces=parent_namespaces)) forget(mod_name) expected_ns = example_namespace.copy() expected_ns.update({ "__name__": mod_name, "__file__": mod_fname, "__cached__": mod_spec.cached, "__package__": mod_name.rpartition(".")[0], "__spec__": mod_spec, }) if alter_sys: expected_ns.update({ "run_argv0": mod_fname, "run_name_in_sys_modules": True, "module_in_sys_modules": True, }) def create_ns(init_globals): return run_module(mod_name, init_globals, alter_sys=alter_sys) try: if verbose > 1: print(("Running from source:", mod_name)) self.check_code_execution(create_ns, expected_ns) importlib.invalidate_caches() __import__(mod_name) os.remove(mod_fname) if not sys.dont_write_bytecode: make_legacy_pyc(mod_fname) unload(mod_name) # In case loader caches paths importlib.invalidate_caches() if verbose > 1: print(("Running from compiled:", mod_name)) self._fix_ns_for_legacy_pyc(expected_ns, alter_sys) self.check_code_execution(create_ns, expected_ns) finally: self._del_pkg(pkg_dir, depth, mod_name) if verbose > 1: print("Module executed successfully")
def test_run_name(self): depth = 1 run_name = "And now for something completely different" pkg_dir, mod_fname, mod_name = ( self._make_pkg(example_source, depth)) forget(mod_name) expected_ns = example_namespace.copy() expected_ns.update({ "__name__": run_name, "__file__": mod_fname, "__package__": mod_name.rpartition(".")[0], }) def create_ns(init_globals): return run_module(mod_name, init_globals, run_name) try: self.check_code_execution(create_ns, expected_ns) finally: self._del_pkg(pkg_dir, depth, mod_name)
def test_package___file__(self): # 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. with open('pep3147/__init__.py', 'w'): pass m = __import__('pep3147') # Ensure we load the pyc file. support.forget('pep3147') m = __import__('pep3147') self.assertEqual(m.__file__, os.sep.join(('.', 'pep3147', '__init__.py')))
def _check_module(self, depth): pkg_dir, mod_fname, mod_name = (self._make_pkg("x=1\n", depth)) forget(mod_name) try: if verbose: print("Running from source:", mod_name) d1 = run_module(mod_name) # Read from source self.assertTrue("x" in d1) self.assertEqual(d1["x"], 1) del d1 # Ensure __loader__ entry doesn't keep file open __import__(mod_name) os.remove(mod_fname) if verbose: print("Running from compiled:", mod_name) d2 = run_module(mod_name) # Read from bytecode self.assertTrue("x" in d2) self.assertEqual(d2["x"], 1) del d2 # Ensure __loader__ entry doesn't keep file open finally: self._del_pkg(pkg_dir, depth, mod_name) if verbose: print("Module executed successfully")
def _check_module(self, depth): pkg_dir, mod_fname, mod_name = ( self._make_pkg("x=1\n", depth)) forget(mod_name) try: if verbose: print("Running from source:", mod_name) d1 = run_module(mod_name) # Read from source self.assertTrue("x" in d1) self.assertEqual(d1["x"], 1) del d1 # Ensure __loader__ entry doesn't keep file open __import__(mod_name) os.remove(mod_fname) if verbose: print("Running from compiled:", mod_name) d2 = run_module(mod_name) # Read from bytecode self.assertTrue("x" in d2) self.assertEqual(d2["x"], 1) del d2 # Ensure __loader__ entry doesn't keep file open finally: self._del_pkg(pkg_dir, depth, mod_name) if verbose: print("Module executed successfully")
def _check_module(self, depth, alter_sys=False, *, namespace=False, parent_namespaces=False): pkg_dir, mod_fname, mod_name, mod_spec = self._make_pkg( example_source, depth, namespace=namespace, parent_namespaces=parent_namespaces ) forget(mod_name) expected_ns = example_namespace.copy() expected_ns.update( { "__name__": mod_name, "__file__": mod_fname, "__cached__": mod_spec.cached, "__package__": mod_name.rpartition(".")[0], "__spec__": mod_spec, } ) if alter_sys: expected_ns.update({"run_argv0": mod_fname, "run_name_in_sys_modules": True, "module_in_sys_modules": True}) def create_ns(init_globals): return run_module(mod_name, init_globals, alter_sys=alter_sys) try: if verbose > 1: print("Running from source:", mod_name) self.check_code_execution(create_ns, expected_ns) importlib.invalidate_caches() __import__(mod_name) os.remove(mod_fname) if not sys.dont_write_bytecode: make_legacy_pyc(mod_fname) unload(mod_name) # In case loader caches paths importlib.invalidate_caches() if verbose > 1: print("Running from compiled:", mod_name) self._fix_ns_for_legacy_pyc(expected_ns, alter_sys) self.check_code_execution(create_ns, expected_ns) finally: self._del_pkg(pkg_dir) if verbose > 1: print("Module executed successfully")
def _check_module(self, depth): pkg_dir, mod_fname, mod_name = ( self._make_pkg("x=1\n", depth)) forget(mod_name) try: if verbose: print("Running from source:", mod_name) d1 = run_module(mod_name) # Read from source self.assertIn("x", d1) self.assertEqual(d1["x"], 1) del d1 # Ensure __loader__ entry doesn't keep file open importlib.invalidate_caches() __import__(mod_name) os.remove(mod_fname) make_legacy_pyc(mod_fname) unload(mod_name) # In case loader caches paths if verbose: print("Running from compiled:", mod_name) importlib.invalidate_caches() d2 = run_module(mod_name) # Read from bytecode self.assertIn("x", d2) self.assertEqual(d2["x"], 1) del d2 # Ensure __loader__ entry doesn't keep file open finally: self._del_pkg(pkg_dir, depth, mod_name) if verbose: print("Module executed successfully")
def _clean(self): forget(TESTFN) rmtree('__pycache__') unlink(self.source)