From deba941d61c8f02f84a5d1452f568d7dbf4d85e3 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Tue, 30 Jun 2009 23:06:06 +0000 Subject: convert old fail* assertions to assert* --- Lib/test/test_runpy.py | 58 +++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) (limited to 'Lib/test/test_runpy.py') diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py index 43691cbbb6..43141e5817 100644 --- a/Lib/test/test_runpy.py +++ b/Lib/test/test_runpy.py @@ -37,14 +37,14 @@ class RunModuleCodeTest(unittest.TestCase): def test_run_code(self): saved_argv0 = sys.argv[0] d = _run_code(self.test_source, {}) - self.failUnless(d["result"] == self.expected_result) - self.failUnless(d["__name__"] is None) - self.failUnless(d["__file__"] is None) - self.failUnless(d["__loader__"] is None) - self.failUnless(d["__package__"] is None) - self.failUnless(d["run_argv0"] is saved_argv0) - self.failUnless("run_name" not in d) - self.failUnless(sys.argv[0] is saved_argv0) + self.assertTrue(d["result"] == self.expected_result) + self.assertTrue(d["__name__"] is None) + self.assertTrue(d["__file__"] is None) + self.assertTrue(d["__loader__"] is None) + self.assertTrue(d["__package__"] is None) + self.assertTrue(d["run_argv0"] is saved_argv0) + self.assertTrue("run_name" not in d) + self.assertTrue(sys.argv[0] is saved_argv0) def test_run_module_code(self): initial = object() @@ -60,19 +60,19 @@ class RunModuleCodeTest(unittest.TestCase): file, loader, package) - self.failUnless("result" not in d1) - self.failUnless(d2["initial"] is initial) + self.assertTrue("result" not in d1) + self.assertTrue(d2["initial"] is initial) self.assertEqual(d2["result"], self.expected_result) self.assertEqual(d2["nested"]["x"], 1) - self.failUnless(d2["__name__"] is name) - self.failUnless(d2["run_name_in_sys_modules"]) - self.failUnless(d2["module_in_sys_modules"]) - self.failUnless(d2["__file__"] is file) - self.failUnless(d2["run_argv0"] is file) - self.failUnless(d2["__loader__"] is loader) - self.failUnless(d2["__package__"] is package) - self.failUnless(sys.argv[0] is saved_argv0) - self.failUnless(name not in sys.modules) + self.assertTrue(d2["__name__"] is name) + self.assertTrue(d2["run_name_in_sys_modules"]) + self.assertTrue(d2["module_in_sys_modules"]) + self.assertTrue(d2["__file__"] is file) + self.assertTrue(d2["run_argv0"] is file) + self.assertTrue(d2["__loader__"] is loader) + self.assertTrue(d2["__package__"] is package) + self.assertTrue(sys.argv[0] is saved_argv0) + self.assertTrue(name not in sys.modules) class RunModuleTest(unittest.TestCase): @@ -159,14 +159,14 @@ class RunModuleTest(unittest.TestCase): try: if verbose: print("Running from source:", mod_name) d1 = run_module(mod_name) # Read from source - self.failUnless("x" in d1) + 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.failUnless("x" in d2) + self.assertTrue("x" in d2) self.assertEqual(d2["x"], 1) del d2 # Ensure __loader__ entry doesn't keep file open finally: @@ -211,19 +211,19 @@ from ..uncle.cousin import nephew pkg_name = mod_name.rpartition('.')[0] if verbose: print("Running from source:", mod_name) d1 = run_module(mod_name, run_name=run_name) # Read from source - self.failUnless("__package__" in d1) - self.failUnless(d1["__package__"] == pkg_name) - self.failUnless("sibling" in d1) - self.failUnless("nephew" in d1) + self.assertTrue("__package__" in d1) + self.assertTrue(d1["__package__"] == pkg_name) + self.assertTrue("sibling" in d1) + self.assertTrue("nephew" in d1) 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, run_name=run_name) # Read from bytecode - self.failUnless("__package__" in d2) - self.failUnless(d2["__package__"] == pkg_name) - self.failUnless("sibling" in d2) - self.failUnless("nephew" in d2) + self.assertTrue("__package__" in d2) + self.assertTrue(d2["__package__"] == pkg_name) + self.assertTrue("sibling" in d2) + self.assertTrue("nephew" in d2) del d2 # Ensure __loader__ entry doesn't keep file open finally: self._del_pkg(pkg_dir, depth, mod_name) -- cgit v1.2.1 From 6e8ffbafd5878e3983e14984637c27d0cd138532 Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Mon, 16 Nov 2009 06:49:25 +0000 Subject: Merged revisions 76286-76287,76289-76294,76296-76299,76301-76305,76307,76310-76311,76313-76322 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r76286 | nick.coghlan | 2009-11-15 17:30:34 +1000 (Sun, 15 Nov 2009) | 1 line Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation. ........ r76321 | nick.coghlan | 2009-11-16 13:55:51 +1000 (Mon, 16 Nov 2009) | 1 line Account for another cache when hunting ref leaks ........ r76322 | nick.coghlan | 2009-11-16 13:57:32 +1000 (Mon, 16 Nov 2009) | 1 line Allow for backslashes in file paths passed to the regex engine ........ --- Lib/test/test_runpy.py | 162 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 142 insertions(+), 20 deletions(-) (limited to 'Lib/test/test_runpy.py') diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py index 43141e5817..bd22814eec 100644 --- a/Lib/test/test_runpy.py +++ b/Lib/test/test_runpy.py @@ -5,8 +5,11 @@ import os.path import sys import tempfile from test.support import verbose, run_unittest, forget -from runpy import _run_code, _run_module_code, run_module +from test.script_helper import (temp_dir, make_script, compile_script, + make_pkg, make_zip_script, make_zip_pkg) + +from runpy import _run_code, _run_module_code, run_module, run_path # Note: This module can't safely test _run_module_as_main as it # runs its tests in the current process, which would mess with the # real __main__ module (usually test.regrtest) @@ -15,6 +18,7 @@ from runpy import _run_code, _run_module_code, run_module # Set up the test code and expected results class RunModuleCodeTest(unittest.TestCase): + """Unit tests for runpy._run_code and runpy._run_module_code""" expected_result = ["Top level assignment", "Lower level reference"] test_source = ( @@ -37,14 +41,14 @@ class RunModuleCodeTest(unittest.TestCase): def test_run_code(self): saved_argv0 = sys.argv[0] d = _run_code(self.test_source, {}) - self.assertTrue(d["result"] == self.expected_result) - self.assertTrue(d["__name__"] is None) - self.assertTrue(d["__file__"] is None) - self.assertTrue(d["__loader__"] is None) - self.assertTrue(d["__package__"] is None) - self.assertTrue(d["run_argv0"] is saved_argv0) - self.assertTrue("run_name" not in d) - self.assertTrue(sys.argv[0] is saved_argv0) + self.assertEqual(d["result"], self.expected_result) + self.assertIs(d["__name__"], None) + self.assertIs(d["__file__"], None) + self.assertIs(d["__loader__"], None) + self.assertIs(d["__package__"], None) + self.assertIs(d["run_argv0"], saved_argv0) + self.assertNotIn("run_name", d) + self.assertIs(sys.argv[0], saved_argv0) def test_run_module_code(self): initial = object() @@ -60,22 +64,23 @@ class RunModuleCodeTest(unittest.TestCase): file, loader, package) - self.assertTrue("result" not in d1) - self.assertTrue(d2["initial"] is initial) + self.assertNotIn("result", d1) + self.assertIs(d2["initial"], initial) self.assertEqual(d2["result"], self.expected_result) self.assertEqual(d2["nested"]["x"], 1) - self.assertTrue(d2["__name__"] is name) + self.assertIs(d2["__name__"], name) self.assertTrue(d2["run_name_in_sys_modules"]) self.assertTrue(d2["module_in_sys_modules"]) - self.assertTrue(d2["__file__"] is file) - self.assertTrue(d2["run_argv0"] is file) - self.assertTrue(d2["__loader__"] is loader) - self.assertTrue(d2["__package__"] is package) - self.assertTrue(sys.argv[0] is saved_argv0) - self.assertTrue(name not in sys.modules) + self.assertIs(d2["__file__"], file) + self.assertIs(d2["run_argv0"], file) + self.assertIs(d2["__loader__"], loader) + self.assertIs(d2["__package__"], package) + self.assertIs(sys.argv[0], saved_argv0) + self.assertNotIn(name, sys.modules) class RunModuleTest(unittest.TestCase): + """Unit tests for runpy.run_module""" def expect_import_error(self, mod_name): try: @@ -245,9 +250,126 @@ from ..uncle.cousin import nephew self._check_relative_imports(depth, "__main__") +class RunPathTest(unittest.TestCase): + """Unit tests for runpy.run_path""" + # Based on corresponding tests in test_cmd_line_script + + test_source = """\ +# Script may be run with optimisation enabled, so don't rely on assert +# statements being executed +def assertEqual(lhs, rhs): + if lhs != rhs: + raise AssertionError('%r != %r' % (lhs, rhs)) +def assertIs(lhs, rhs): + if lhs is not rhs: + raise AssertionError('%r is not %r' % (lhs, rhs)) +# Check basic code execution +result = ['Top level assignment'] +def f(): + result.append('Lower level reference') +f() +assertEqual(result, ['Top level assignment', 'Lower level reference']) +# Check the sys module +import sys +assertIs(globals(), sys.modules[__name__].__dict__) +argv0 = sys.argv[0] +""" + + def _make_test_script(self, script_dir, script_basename, source=None): + if source is None: + source = self.test_source + return make_script(script_dir, script_basename, source) + + def _check_script(self, script_name, expected_name, expected_file, + expected_argv0, expected_package): + result = run_path(script_name) + self.assertEqual(result["__name__"], expected_name) + self.assertEqual(result["__file__"], expected_file) + self.assertIn("argv0", result) + self.assertEqual(result["argv0"], expected_argv0) + self.assertEqual(result["__package__"], expected_package) + + def _check_import_error(self, script_name, msg): + # Double backslashes to handle path separators on Windows + msg = msg.replace("\\", "\\\\") + self.assertRaisesRegexp(ImportError, msg, run_path, script_name) + + def test_basic_script(self): + with temp_dir() as script_dir: + mod_name = 'script' + script_name = self._make_test_script(script_dir, mod_name) + self._check_script(script_name, "", script_name, + script_name, None) + + def test_script_compiled(self): + with temp_dir() as script_dir: + mod_name = 'script' + script_name = self._make_test_script(script_dir, mod_name) + compiled_name = compile_script(script_name) + os.remove(script_name) + self._check_script(compiled_name, "", compiled_name, + compiled_name, None) + + def test_directory(self): + with temp_dir() as script_dir: + mod_name = '__main__' + script_name = self._make_test_script(script_dir, mod_name) + self._check_script(script_dir, "", script_name, + script_dir, '') + + def test_directory_compiled(self): + with temp_dir() as script_dir: + mod_name = '__main__' + script_name = self._make_test_script(script_dir, mod_name) + compiled_name = compile_script(script_name) + os.remove(script_name) + self._check_script(script_dir, "", compiled_name, + script_dir, '') + + def test_directory_error(self): + with temp_dir() as script_dir: + mod_name = 'not_main' + script_name = self._make_test_script(script_dir, mod_name) + msg = "can't find '__main__' module in %r" % script_dir + self._check_import_error(script_dir, msg) + + def test_zipfile(self): + with temp_dir() as script_dir: + mod_name = '__main__' + script_name = self._make_test_script(script_dir, mod_name) + zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name) + self._check_script(zip_name, "", fname, zip_name, '') + + def test_zipfile_compiled(self): + with temp_dir() as script_dir: + mod_name = '__main__' + script_name = self._make_test_script(script_dir, mod_name) + compiled_name = compile_script(script_name) + zip_name, fname = make_zip_script(script_dir, 'test_zip', compiled_name) + self._check_script(zip_name, "", fname, zip_name, '') + + def test_zipfile_error(self): + with temp_dir() as script_dir: + mod_name = 'not_main' + script_name = self._make_test_script(script_dir, mod_name) + zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name) + msg = "can't find '__main__' module in %r" % zip_name + self._check_import_error(zip_name, msg) + + def test_main_recursion_error(self): + with temp_dir() as script_dir, temp_dir() as dummy_dir: + mod_name = '__main__' + source = ("import runpy\n" + "runpy.run_path(%r)\n") % dummy_dir + script_name = self._make_test_script(script_dir, mod_name, source) + zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name) + msg = "recursion depth exceeded" + self.assertRaisesRegexp(RuntimeError, msg, run_path, zip_name) + + + def test_main(): - run_unittest(RunModuleCodeTest) - run_unittest(RunModuleTest) + run_unittest(RunModuleCodeTest, RunModuleTest, RunPathTest) if __name__ == "__main__": test_main() -- cgit v1.2.1 From 44c8c955188831c1d2a59eda65ebe5b6eeacb483 Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Wed, 18 Nov 2009 11:35:25 +0000 Subject: Merged revisions 76362 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r76362 | nick.coghlan | 2009-11-18 21:27:53 +1000 (Wed, 18 Nov 2009) | 1 line Correctly escape arbitrary error message text in the runpy unit tests ........ --- Lib/test/test_runpy.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Lib/test/test_runpy.py') diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py index bd22814eec..88e05fedfb 100644 --- a/Lib/test/test_runpy.py +++ b/Lib/test/test_runpy.py @@ -3,6 +3,7 @@ import unittest import os import os.path import sys +import re import tempfile from test.support import verbose, run_unittest, forget from test.script_helper import (temp_dir, make_script, compile_script, @@ -290,8 +291,7 @@ argv0 = sys.argv[0] self.assertEqual(result["__package__"], expected_package) def _check_import_error(self, script_name, msg): - # Double backslashes to handle path separators on Windows - msg = msg.replace("\\", "\\\\") + msg = re.escape(msg) self.assertRaisesRegexp(ImportError, msg, run_path, script_name) def test_basic_script(self): -- cgit v1.2.1 From 7e20442f707b90d4d2eb0b47eabe9b29b923103e Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Wed, 25 Nov 2009 17:46:26 +0000 Subject: Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r75264 | andrew.kuchling | 2009-10-05 17:30:22 -0500 (Mon, 05 Oct 2009) | 1 line Add various items ........ r75268 | andrew.kuchling | 2009-10-05 17:45:39 -0500 (Mon, 05 Oct 2009) | 1 line Remove two notes ........ r75293 | kristjan.jonsson | 2009-10-09 09:32:19 -0500 (Fri, 09 Oct 2009) | 2 lines http://bugs.python.org/issue7029 a non-default timer wasn't actually used by the individual Tests. ........ r75318 | benjamin.peterson | 2009-10-10 16:15:58 -0500 (Sat, 10 Oct 2009) | 1 line remove script which uses long gone module ........ r75391 | andrew.kuchling | 2009-10-13 10:49:33 -0500 (Tue, 13 Oct 2009) | 1 line Link to PEP ........ r75392 | andrew.kuchling | 2009-10-13 11:11:49 -0500 (Tue, 13 Oct 2009) | 1 line Various link, textual, and markup fixes ........ r75436 | benjamin.peterson | 2009-10-15 10:39:15 -0500 (Thu, 15 Oct 2009) | 1 line don't need to mess up sys.path ........ r75478 | senthil.kumaran | 2009-10-17 20:58:45 -0500 (Sat, 17 Oct 2009) | 3 lines Fix a typo. ........ r75971 | benjamin.peterson | 2009-10-30 22:56:15 -0500 (Fri, 30 Oct 2009) | 1 line add some checks for evaluation order with parenthesis #7210 ........ r76003 | antoine.pitrou | 2009-10-31 19:30:13 -0500 (Sat, 31 Oct 2009) | 6 lines Hopefully fix the buildbot problems on test_mailbox, by computing the maildir toc cache refresh date before actually refreshing the cache. (see #6896) ........ r76058 | benjamin.peterson | 2009-11-02 10:14:19 -0600 (Mon, 02 Nov 2009) | 1 line grant list.index() a more informative error message #7252 ........ r76140 | nick.coghlan | 2009-11-07 02:13:55 -0600 (Sat, 07 Nov 2009) | 1 line Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package ........ r76141 | nick.coghlan | 2009-11-07 02:15:01 -0600 (Sat, 07 Nov 2009) | 1 line Some minor cleanups to private runpy code and docstrings ........ r76231 | benjamin.peterson | 2009-11-12 17:42:23 -0600 (Thu, 12 Nov 2009) | 1 line this main is much more useful ........ r76380 | antoine.pitrou | 2009-11-18 14:20:46 -0600 (Wed, 18 Nov 2009) | 3 lines Mention Giampolo R's new FTP TLS support in the what's new file ........ r76428 | benjamin.peterson | 2009-11-19 20:15:50 -0600 (Thu, 19 Nov 2009) | 1 line turn goto into do while loop ........ r76429 | benjamin.peterson | 2009-11-19 20:56:43 -0600 (Thu, 19 Nov 2009) | 2 lines avoid doing an uneeded import in a function ........ --- Lib/test/test_runpy.py | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) (limited to 'Lib/test/test_runpy.py') diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py index 88e05fedfb..0542194146 100644 --- a/Lib/test/test_runpy.py +++ b/Lib/test/test_runpy.py @@ -100,8 +100,8 @@ class RunModuleTest(unittest.TestCase): self.expect_import_error("a.bee") self.expect_import_error(".howard") self.expect_import_error("..eaten") - # Package - self.expect_import_error("logging") + # Package without __main__.py + self.expect_import_error("multiprocessing") def test_library_module(self): run_module("runpy") @@ -113,9 +113,9 @@ class RunModuleTest(unittest.TestCase): pkg_file.close() return pkg_fname - def _make_pkg(self, source, depth): + def _make_pkg(self, source, depth, mod_base="runpy_test"): pkg_name = "__runpy_pkg__" - test_fname = "runpy_test.py" + test_fname = mod_base+os.extsep+"py" pkg_dir = sub_dir = tempfile.mkdtemp() if verbose: print(" Package tree in:", sub_dir) sys.path.insert(0, pkg_dir) @@ -130,7 +130,7 @@ class RunModuleTest(unittest.TestCase): mod_file.write(source) mod_file.close() if verbose: print(" Created:", mod_fname) - mod_name = (pkg_name+".")*depth + "runpy_test" + mod_name = (pkg_name+".")*depth + mod_base return pkg_dir, mod_fname, mod_name def _del_pkg(self, top, depth, mod_name): @@ -179,6 +179,28 @@ class RunModuleTest(unittest.TestCase): self._del_pkg(pkg_dir, depth, mod_name) if verbose: print("Module 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.assertTrue("x" in d1) + self.assertTrue(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:", pkg_name) + d2 = run_module(pkg_name) # Read from bytecode + self.assertTrue("x" in 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 _add_relative_modules(self, base_dir, source, depth): if depth <= 1: raise ValueError("Relative module test needs depth > 1") @@ -240,6 +262,11 @@ from ..uncle.cousin import nephew if verbose: print("Testing package depth:", depth) self._check_module(depth) + def test_run_package(self): + for depth in range(1, 4): + if verbose: print("Testing package depth:", depth) + self._check_package(depth) + def test_explicit_relative_import(self): for depth in range(2, 5): if verbose: print("Testing relative imports at depth:", depth) -- cgit v1.2.1 From 3e1025652caea5abed08f93c312829d06cd82475 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Tue, 19 Jan 2010 00:09:57 +0000 Subject: use assert[Not]In where appropriate A patch from Dave Malcolm. --- Lib/test/test_runpy.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'Lib/test/test_runpy.py') diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py index 0542194146..995c8917d5 100644 --- a/Lib/test/test_runpy.py +++ b/Lib/test/test_runpy.py @@ -165,14 +165,14 @@ class RunModuleTest(unittest.TestCase): try: if verbose: print("Running from source:", mod_name) d1 = run_module(mod_name) # Read from source - self.assertTrue("x" in d1) + self.assertIn("x", 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.assertIn("x", d2) self.assertEqual(d2["x"], 1) del d2 # Ensure __loader__ entry doesn't keep file open finally: @@ -187,14 +187,14 @@ class RunModuleTest(unittest.TestCase): try: if verbose: print("Running from source:", pkg_name) d1 = run_module(pkg_name) # Read from source - self.assertTrue("x" in d1) + 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) if verbose: print("Running from compiled:", pkg_name) d2 = run_module(pkg_name) # Read from bytecode - self.assertTrue("x" in d2) + self.assertIn("x", d2) self.assertTrue(d2["x"] == 1) del d2 # Ensure __loader__ entry doesn't keep file open finally: @@ -239,19 +239,19 @@ from ..uncle.cousin import nephew pkg_name = mod_name.rpartition('.')[0] if verbose: print("Running from source:", mod_name) d1 = run_module(mod_name, run_name=run_name) # Read from source - self.assertTrue("__package__" in d1) + self.assertIn("__package__", d1) self.assertTrue(d1["__package__"] == pkg_name) - self.assertTrue("sibling" in d1) - self.assertTrue("nephew" in d1) + self.assertIn("sibling", d1) + self.assertIn("nephew", d1) 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, run_name=run_name) # Read from bytecode - self.assertTrue("__package__" in d2) + self.assertIn("__package__", d2) self.assertTrue(d2["__package__"] == pkg_name) - self.assertTrue("sibling" in d2) - self.assertTrue("nephew" in d2) + self.assertIn("sibling", d2) + self.assertIn("nephew", d2) del d2 # Ensure __loader__ entry doesn't keep file open finally: self._del_pkg(pkg_dir, depth, mod_name) -- cgit v1.2.1 From 4bd6574097d2cbd2e779196837124380e2dbe9bb Mon Sep 17 00:00:00 2001 From: Barry Warsaw Date: Sat, 17 Apr 2010 00:19:56 +0000 Subject: PEP 3147 --- Lib/test/test_runpy.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'Lib/test/test_runpy.py') diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py index 995c8917d5..068eca94a0 100644 --- a/Lib/test/test_runpy.py +++ b/Lib/test/test_runpy.py @@ -5,9 +5,10 @@ import os.path import sys import re import tempfile -from test.support import verbose, run_unittest, forget -from test.script_helper import (temp_dir, make_script, compile_script, - make_pkg, make_zip_script, make_zip_pkg) +import py_compile +from test.support import forget, make_legacy_pyc, run_unittest, verbose +from test.script_helper import ( + make_pkg, make_script, make_zip_pkg, make_zip_script, temp_dir) from runpy import _run_code, _run_module_code, run_module, run_path @@ -45,6 +46,7 @@ class RunModuleCodeTest(unittest.TestCase): self.assertEqual(d["result"], self.expected_result) self.assertIs(d["__name__"], None) self.assertIs(d["__file__"], None) + self.assertIs(d["__cached__"], None) self.assertIs(d["__loader__"], None) self.assertIs(d["__package__"], None) self.assertIs(d["run_argv0"], saved_argv0) @@ -73,6 +75,7 @@ class RunModuleCodeTest(unittest.TestCase): self.assertTrue(d2["run_name_in_sys_modules"]) self.assertTrue(d2["module_in_sys_modules"]) self.assertIs(d2["__file__"], file) + self.assertIs(d2["__cached__"], None) self.assertIs(d2["run_argv0"], file) self.assertIs(d2["__loader__"], loader) self.assertIs(d2["__package__"], package) @@ -170,6 +173,7 @@ class RunModuleTest(unittest.TestCase): del d1 # Ensure __loader__ entry doesn't keep file open __import__(mod_name) os.remove(mod_fname) + make_legacy_pyc(mod_fname) if verbose: print("Running from compiled:", mod_name) d2 = run_module(mod_name) # Read from bytecode self.assertIn("x", d2) @@ -192,6 +196,7 @@ class RunModuleTest(unittest.TestCase): del d1 # Ensure __loader__ entry doesn't keep file open __import__(mod_name) os.remove(mod_fname) + make_legacy_pyc(mod_fname) if verbose: print("Running from compiled:", pkg_name) d2 = run_module(pkg_name) # Read from bytecode self.assertIn("x", d2) @@ -246,6 +251,7 @@ from ..uncle.cousin import nephew del d1 # Ensure __loader__ entry doesn't keep file open __import__(mod_name) os.remove(mod_fname) + make_legacy_pyc(mod_fname) if verbose: print("Running from compiled:", mod_name) d2 = run_module(mod_name, run_name=run_name) # Read from bytecode self.assertIn("__package__", d2) @@ -313,6 +319,7 @@ argv0 = sys.argv[0] result = run_path(script_name) self.assertEqual(result["__name__"], expected_name) self.assertEqual(result["__file__"], expected_file) + self.assertEqual(result["__cached__"], None) self.assertIn("argv0", result) self.assertEqual(result["argv0"], expected_argv0) self.assertEqual(result["__package__"], expected_package) @@ -332,7 +339,7 @@ argv0 = sys.argv[0] with temp_dir() as script_dir: mod_name = 'script' script_name = self._make_test_script(script_dir, mod_name) - compiled_name = compile_script(script_name) + compiled_name = py_compile.compile(script_name, doraise=True) os.remove(script_name) self._check_script(compiled_name, "", compiled_name, compiled_name, None) @@ -348,9 +355,10 @@ argv0 = sys.argv[0] with temp_dir() as script_dir: mod_name = '__main__' script_name = self._make_test_script(script_dir, mod_name) - compiled_name = compile_script(script_name) + compiled_name = py_compile.compile(script_name, doraise=True) os.remove(script_name) - self._check_script(script_dir, "", compiled_name, + legacy_pyc = make_legacy_pyc(script_name) + self._check_script(script_dir, "", legacy_pyc, script_dir, '') def test_directory_error(self): @@ -371,8 +379,9 @@ argv0 = sys.argv[0] with temp_dir() as script_dir: mod_name = '__main__' script_name = self._make_test_script(script_dir, mod_name) - compiled_name = compile_script(script_name) - zip_name, fname = make_zip_script(script_dir, 'test_zip', compiled_name) + compiled_name = py_compile.compile(script_name, doraise=True) + zip_name, fname = make_zip_script(script_dir, 'test_zip', + compiled_name) self._check_script(zip_name, "", fname, zip_name, '') def test_zipfile_error(self): -- cgit v1.2.1 From 8de2231f98a9dfe7f4dd1ad4b7980c1c632ed844 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Sat, 3 Jul 2010 21:48:25 +0000 Subject: Make importlib.abc.SourceLoader the primary mechanism for importlib. This required moving the class from importlib/abc.py into importlib/_bootstrap.py and jiggering some code to work better with the class. This included changing how the file finder worked to better meet import semantics. This also led to fixing importlib to handle the empty string from sys.path as import currently does (and making me wish we didn't support that instead just required people to insert '.' instead to represent cwd). It also required making the new set_data abstractmethod create any needed subdirectories implicitly thanks to __pycache__ (it was either this or grow the SourceLoader ABC to gain an 'exists' method and either a mkdir method or have set_data with no data arg mean to create a directory). Lastly, as an optimization the file loaders cache the file path where the finder found something to use for loading (this is thanks to having a sourceless loader separate from the source loader to simplify the code and cut out stat calls). Unfortunately test_runpy assumed a loader would always work for a module, even if you changed from underneath it what it was expected to work with. By simply dropping the previous loader in test_runpy so the proper loader can be returned by the finder fixed the failure. At this point importlib deviates from import on two points: 1. The exception raised when trying to import a file is different (import does an explicit file check to print a special message, importlib just says the path cannot be imported as if it was just some module name). 2. the co_filename on a code object is not being set to where bytecode was actually loaded from instead of where the marshalled code object originally came from (a solution for this has already been agreed upon on python-dev but has not been implemented yet; issue8611). --- Lib/test/test_runpy.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'Lib/test/test_runpy.py') diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py index 068eca94a0..94e47d688e 100644 --- a/Lib/test/test_runpy.py +++ b/Lib/test/test_runpy.py @@ -6,7 +6,7 @@ import sys import re import tempfile import py_compile -from test.support import forget, make_legacy_pyc, run_unittest, verbose +from test.support import forget, make_legacy_pyc, run_unittest, unload, verbose from test.script_helper import ( make_pkg, make_script, make_zip_pkg, make_zip_script, temp_dir) @@ -174,6 +174,7 @@ class RunModuleTest(unittest.TestCase): __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) d2 = run_module(mod_name) # Read from bytecode self.assertIn("x", d2) @@ -197,6 +198,7 @@ class RunModuleTest(unittest.TestCase): __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) @@ -252,6 +254,7 @@ from ..uncle.cousin import nephew __import__(mod_name) os.remove(mod_fname) make_legacy_pyc(mod_fname) + unload(mod_name) # In case the loader caches paths if verbose: print("Running from compiled:", mod_name) d2 = run_module(mod_name, run_name=run_name) # Read from bytecode self.assertIn("__package__", d2) @@ -405,7 +408,11 @@ argv0 = sys.argv[0] def test_main(): - run_unittest(RunModuleCodeTest, RunModuleTest, RunPathTest) + run_unittest( + RunModuleCodeTest, + RunModuleTest, + RunPathTest + ) if __name__ == "__main__": test_main() -- cgit v1.2.1 From 2956b3ca751b9a9e7ab7cc09fae88b2568780cb9 Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Wed, 1 Dec 2010 02:32:32 +0000 Subject: #10273: Rename assertRegexpMatches and assertRaisesRegexp to assertRegex and assertRaisesRegex. --- Lib/test/test_runpy.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Lib/test/test_runpy.py') diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py index 94e47d688e..ad3ab397f4 100644 --- a/Lib/test/test_runpy.py +++ b/Lib/test/test_runpy.py @@ -329,7 +329,7 @@ argv0 = sys.argv[0] def _check_import_error(self, script_name, msg): msg = re.escape(msg) - self.assertRaisesRegexp(ImportError, msg, run_path, script_name) + self.assertRaisesRegex(ImportError, msg, run_path, script_name) def test_basic_script(self): with temp_dir() as script_dir: @@ -403,7 +403,7 @@ argv0 = sys.argv[0] script_name = self._make_test_script(script_dir, mod_name, source) zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name) msg = "recursion depth exceeded" - self.assertRaisesRegexp(RuntimeError, msg, run_path, zip_name) + self.assertRaisesRegex(RuntimeError, msg, run_path, zip_name) -- cgit v1.2.1