Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1 | # Test the runpy module |
| 2 | import unittest |
| 3 | import os |
| 4 | import os.path |
| 5 | import sys |
Nick Coghlan | 16eb0fb | 2009-11-18 11:35:25 +0000 | [diff] [blame] | 6 | import re |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 7 | import tempfile |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 8 | import importlib, importlib.machinery, importlib.util |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 9 | import py_compile |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 10 | from test.support import ( |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 11 | forget, make_legacy_pyc, unload, verbose, no_tracing, |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 12 | create_empty_file, temp_dir) |
| 13 | from test.support.script_helper import ( |
| 14 | make_pkg, make_script, make_zip_pkg, make_zip_script) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 15 | |
Nick Coghlan | 8ecf504 | 2012-07-15 21:19:18 +1000 | [diff] [blame] | 16 | |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 17 | import runpy |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 18 | from runpy import _run_code, _run_module_code, run_module, run_path |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 19 | # Note: This module can't safely test _run_module_as_main as it |
| 20 | # runs its tests in the current process, which would mess with the |
| 21 | # real __main__ module (usually test.regrtest) |
| 22 | # See test_cmd_line_script for a test that executes that code path |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 23 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 24 | |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 25 | # Set up the test code and expected results |
| 26 | example_source = """\ |
| 27 | # Check basic code execution |
| 28 | result = ['Top level assignment'] |
| 29 | def f(): |
| 30 | result.append('Lower level reference') |
| 31 | f() |
| 32 | del f |
| 33 | # Check the sys module |
| 34 | import sys |
| 35 | run_argv0 = sys.argv[0] |
| 36 | run_name_in_sys_modules = __name__ in sys.modules |
| 37 | module_in_sys_modules = (run_name_in_sys_modules and |
| 38 | globals() is sys.modules[__name__].__dict__) |
| 39 | # Check nested operation |
| 40 | import runpy |
| 41 | nested = runpy._run_module_code('x=1\\n', mod_name='<run>') |
| 42 | """ |
| 43 | |
| 44 | implicit_namespace = { |
| 45 | "__name__": None, |
| 46 | "__file__": None, |
| 47 | "__cached__": None, |
| 48 | "__package__": None, |
| 49 | "__doc__": None, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 50 | "__spec__": None |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 51 | } |
| 52 | example_namespace = { |
| 53 | "sys": sys, |
| 54 | "runpy": runpy, |
| 55 | "result": ["Top level assignment", "Lower level reference"], |
| 56 | "run_argv0": sys.argv[0], |
| 57 | "run_name_in_sys_modules": False, |
| 58 | "module_in_sys_modules": False, |
| 59 | "nested": dict(implicit_namespace, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 60 | x=1, __name__="<run>", __loader__=None), |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 61 | } |
| 62 | example_namespace.update(implicit_namespace) |
| 63 | |
| 64 | class CodeExecutionMixin: |
| 65 | # Issue #15230 (run_path not handling run_name correctly) highlighted a |
| 66 | # problem with the way arguments were being passed from higher level APIs |
| 67 | # down to lower level code. This mixin makes it easier to ensure full |
| 68 | # testing occurs at those upper layers as well, not just at the utility |
| 69 | # layer |
| 70 | |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 71 | # Figuring out the loader details in advance is hard to do, so we skip |
| 72 | # checking the full details of loader and loader_state |
| 73 | CHECKED_SPEC_ATTRIBUTES = ["name", "parent", "origin", "cached", |
| 74 | "has_location", "submodule_search_locations"] |
| 75 | |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 76 | def assertNamespaceMatches(self, result_ns, expected_ns): |
| 77 | """Check two namespaces match. |
| 78 | |
| 79 | Ignores any unspecified interpreter created names |
| 80 | """ |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 81 | # Avoid side effects |
| 82 | result_ns = result_ns.copy() |
| 83 | expected_ns = expected_ns.copy() |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 84 | # Impls are permitted to add extra names, so filter them out |
| 85 | for k in list(result_ns): |
| 86 | if k.startswith("__") and k.endswith("__"): |
| 87 | if k not in expected_ns: |
| 88 | result_ns.pop(k) |
| 89 | if k not in expected_ns["nested"]: |
| 90 | result_ns["nested"].pop(k) |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 91 | # Spec equality includes the loader, so we take the spec out of the |
| 92 | # result namespace and check that separately |
| 93 | result_spec = result_ns.pop("__spec__") |
| 94 | expected_spec = expected_ns.pop("__spec__") |
| 95 | if expected_spec is None: |
| 96 | self.assertIsNone(result_spec) |
| 97 | else: |
| 98 | # If an expected loader is set, we just check we got the right |
| 99 | # type, rather than checking for full equality |
| 100 | if expected_spec.loader is not None: |
| 101 | self.assertEqual(type(result_spec.loader), |
| 102 | type(expected_spec.loader)) |
| 103 | for attr in self.CHECKED_SPEC_ATTRIBUTES: |
| 104 | k = "__spec__." + attr |
| 105 | actual = (k, getattr(result_spec, attr)) |
| 106 | expected = (k, getattr(expected_spec, attr)) |
| 107 | self.assertEqual(actual, expected) |
| 108 | # For the rest, we still don't use direct dict comparison on the |
| 109 | # namespace, as the diffs are too hard to debug if anything breaks |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 110 | self.assertEqual(set(result_ns), set(expected_ns)) |
| 111 | for k in result_ns: |
| 112 | actual = (k, result_ns[k]) |
| 113 | expected = (k, expected_ns[k]) |
| 114 | self.assertEqual(actual, expected) |
| 115 | |
| 116 | def check_code_execution(self, create_namespace, expected_namespace): |
| 117 | """Check that an interface runs the example code correctly |
| 118 | |
| 119 | First argument is a callable accepting the initial globals and |
| 120 | using them to create the actual namespace |
| 121 | Second argument is the expected result |
| 122 | """ |
| 123 | sentinel = object() |
| 124 | expected_ns = expected_namespace.copy() |
| 125 | run_name = expected_ns["__name__"] |
| 126 | saved_argv0 = sys.argv[0] |
| 127 | saved_mod = sys.modules.get(run_name, sentinel) |
| 128 | # Check without initial globals |
| 129 | result_ns = create_namespace(None) |
| 130 | self.assertNamespaceMatches(result_ns, expected_ns) |
| 131 | self.assertIs(sys.argv[0], saved_argv0) |
| 132 | self.assertIs(sys.modules.get(run_name, sentinel), saved_mod) |
| 133 | # And then with initial globals |
| 134 | initial_ns = {"sentinel": sentinel} |
| 135 | expected_ns["sentinel"] = sentinel |
| 136 | result_ns = create_namespace(initial_ns) |
| 137 | self.assertIsNot(result_ns, initial_ns) |
| 138 | self.assertNamespaceMatches(result_ns, expected_ns) |
| 139 | self.assertIs(sys.argv[0], saved_argv0) |
| 140 | self.assertIs(sys.modules.get(run_name, sentinel), saved_mod) |
| 141 | |
| 142 | |
| 143 | class ExecutionLayerTestCase(unittest.TestCase, CodeExecutionMixin): |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 144 | """Unit tests for runpy._run_code and runpy._run_module_code""" |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 145 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 146 | def test_run_code(self): |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 147 | expected_ns = example_namespace.copy() |
| 148 | expected_ns.update({ |
| 149 | "__loader__": None, |
| 150 | }) |
| 151 | def create_ns(init_globals): |
| 152 | return _run_code(example_source, {}, init_globals) |
| 153 | self.check_code_execution(create_ns, expected_ns) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 154 | |
| 155 | def test_run_module_code(self): |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 156 | mod_name = "<Nonsense>" |
| 157 | mod_fname = "Some other nonsense" |
| 158 | mod_loader = "Now you're just being silly" |
| 159 | mod_package = '' # Treat as a top level module |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 160 | mod_spec = importlib.machinery.ModuleSpec(mod_name, |
| 161 | origin=mod_fname, |
| 162 | loader=mod_loader) |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 163 | expected_ns = example_namespace.copy() |
| 164 | expected_ns.update({ |
| 165 | "__name__": mod_name, |
| 166 | "__file__": mod_fname, |
| 167 | "__loader__": mod_loader, |
| 168 | "__package__": mod_package, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 169 | "__spec__": mod_spec, |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 170 | "run_argv0": mod_fname, |
| 171 | "run_name_in_sys_modules": True, |
| 172 | "module_in_sys_modules": True, |
| 173 | }) |
| 174 | def create_ns(init_globals): |
| 175 | return _run_module_code(example_source, |
| 176 | init_globals, |
| 177 | mod_name, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 178 | mod_spec) |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 179 | self.check_code_execution(create_ns, expected_ns) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 180 | |
Nick Coghlan | 8ecf504 | 2012-07-15 21:19:18 +1000 | [diff] [blame] | 181 | # TODO: Use self.addCleanup to get rid of a lot of try-finally blocks |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 182 | class RunModuleTestCase(unittest.TestCase, CodeExecutionMixin): |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 183 | """Unit tests for runpy.run_module""" |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 184 | |
| 185 | def expect_import_error(self, mod_name): |
| 186 | try: |
| 187 | run_module(mod_name) |
| 188 | except ImportError: |
| 189 | pass |
| 190 | else: |
| 191 | self.fail("Expected import error for " + mod_name) |
| 192 | |
| 193 | def test_invalid_names(self): |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 194 | # Builtin module |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 195 | self.expect_import_error("sys") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 196 | # Non-existent modules |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 197 | self.expect_import_error("sys.imp.eric") |
| 198 | self.expect_import_error("os.path.half") |
| 199 | self.expect_import_error("a.bee") |
Martin Panter | 7dda421 | 2015-12-10 06:47:06 +0000 | [diff] [blame] | 200 | # Relative names not allowed |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 201 | self.expect_import_error(".howard") |
| 202 | self.expect_import_error("..eaten") |
Martin Panter | 7dda421 | 2015-12-10 06:47:06 +0000 | [diff] [blame] | 203 | self.expect_import_error(".test_runpy") |
| 204 | self.expect_import_error(".unittest") |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 205 | # Package without __main__.py |
| 206 | self.expect_import_error("multiprocessing") |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 207 | |
| 208 | def test_library_module(self): |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 209 | self.assertEqual(run_module("runpy")["__name__"], "runpy") |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 210 | |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 211 | def _add_pkg_dir(self, pkg_dir, namespace=False): |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 212 | os.mkdir(pkg_dir) |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 213 | if namespace: |
| 214 | return None |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 215 | pkg_fname = os.path.join(pkg_dir, "__init__.py") |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 216 | create_empty_file(pkg_fname) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 217 | return pkg_fname |
| 218 | |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 219 | def _make_pkg(self, source, depth, mod_base="runpy_test", |
| 220 | *, namespace=False, parent_namespaces=False): |
| 221 | # Enforce a couple of internal sanity checks on test cases |
| 222 | if (namespace or parent_namespaces) and not depth: |
| 223 | raise RuntimeError("Can't mark top level module as a " |
| 224 | "namespace package") |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 225 | pkg_name = "__runpy_pkg__" |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 226 | test_fname = mod_base+os.extsep+"py" |
Nick Coghlan | eb3e62f | 2012-07-17 20:42:39 +1000 | [diff] [blame] | 227 | pkg_dir = sub_dir = os.path.realpath(tempfile.mkdtemp()) |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 228 | if verbose > 1: print(" Package tree in:", sub_dir) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 229 | sys.path.insert(0, pkg_dir) |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 230 | if verbose > 1: print(" Updated sys.path:", sys.path[0]) |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 231 | if depth: |
| 232 | namespace_flags = [parent_namespaces] * depth |
| 233 | namespace_flags[-1] = namespace |
| 234 | for namespace_flag in namespace_flags: |
| 235 | sub_dir = os.path.join(sub_dir, pkg_name) |
| 236 | pkg_fname = self._add_pkg_dir(sub_dir, namespace_flag) |
| 237 | if verbose > 1: print(" Next level in:", sub_dir) |
| 238 | if verbose > 1: print(" Created:", pkg_fname) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 239 | mod_fname = os.path.join(sub_dir, test_fname) |
| 240 | mod_file = open(mod_fname, "w") |
| 241 | mod_file.write(source) |
| 242 | mod_file.close() |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 243 | if verbose > 1: print(" Created:", mod_fname) |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 244 | mod_name = (pkg_name+".")*depth + mod_base |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 245 | mod_spec = importlib.util.spec_from_file_location(mod_name, |
| 246 | mod_fname) |
| 247 | return pkg_dir, mod_fname, mod_name, mod_spec |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 248 | |
| 249 | def _del_pkg(self, top, depth, mod_name): |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 250 | for entry in list(sys.modules): |
| 251 | if entry.startswith("__runpy_pkg__"): |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 252 | del sys.modules[entry] |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 253 | if verbose > 1: print(" Removed sys.modules entries") |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 254 | del sys.path[0] |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 255 | if verbose > 1: print(" Removed sys.path entry") |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 256 | for root, dirs, files in os.walk(top, topdown=False): |
| 257 | for name in files: |
| 258 | try: |
| 259 | os.remove(os.path.join(root, name)) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 260 | except OSError as ex: |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 261 | if verbose > 1: print(ex) # Persist with cleaning up |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 262 | for name in dirs: |
| 263 | fullname = os.path.join(root, name) |
| 264 | try: |
| 265 | os.rmdir(fullname) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 266 | except OSError as ex: |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 267 | if verbose > 1: print(ex) # Persist with cleaning up |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 268 | try: |
| 269 | os.rmdir(top) |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 270 | if verbose > 1: print(" Removed package tree") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 271 | except OSError as ex: |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 272 | if verbose > 1: print(ex) # Persist with cleaning up |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 273 | |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 274 | def _fix_ns_for_legacy_pyc(self, ns, alter_sys): |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 275 | char_to_add = "c" |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 276 | ns["__file__"] += char_to_add |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 277 | ns["__cached__"] = ns["__file__"] |
| 278 | spec = ns["__spec__"] |
| 279 | new_spec = importlib.util.spec_from_file_location(spec.name, |
| 280 | ns["__file__"]) |
| 281 | ns["__spec__"] = new_spec |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 282 | if alter_sys: |
| 283 | ns["run_argv0"] += char_to_add |
| 284 | |
| 285 | |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 286 | def _check_module(self, depth, alter_sys=False, |
| 287 | *, namespace=False, parent_namespaces=False): |
| 288 | pkg_dir, mod_fname, mod_name, mod_spec = ( |
| 289 | self._make_pkg(example_source, depth, |
| 290 | namespace=namespace, |
| 291 | parent_namespaces=parent_namespaces)) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 292 | forget(mod_name) |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 293 | expected_ns = example_namespace.copy() |
| 294 | expected_ns.update({ |
| 295 | "__name__": mod_name, |
| 296 | "__file__": mod_fname, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 297 | "__cached__": mod_spec.cached, |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 298 | "__package__": mod_name.rpartition(".")[0], |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 299 | "__spec__": mod_spec, |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 300 | }) |
| 301 | if alter_sys: |
| 302 | expected_ns.update({ |
| 303 | "run_argv0": mod_fname, |
| 304 | "run_name_in_sys_modules": True, |
| 305 | "module_in_sys_modules": True, |
| 306 | }) |
| 307 | def create_ns(init_globals): |
| 308 | return run_module(mod_name, init_globals, alter_sys=alter_sys) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 309 | try: |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 310 | if verbose > 1: print("Running from source:", mod_name) |
| 311 | self.check_code_execution(create_ns, expected_ns) |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 312 | importlib.invalidate_caches() |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 313 | __import__(mod_name) |
| 314 | os.remove(mod_fname) |
Ezio Melotti | c28f6fa | 2013-03-16 19:48:51 +0200 | [diff] [blame] | 315 | if not sys.dont_write_bytecode: |
| 316 | make_legacy_pyc(mod_fname) |
| 317 | unload(mod_name) # In case loader caches paths |
Ezio Melotti | e5e7a7c | 2013-03-16 21:49:20 +0200 | [diff] [blame] | 318 | importlib.invalidate_caches() |
Ezio Melotti | c28f6fa | 2013-03-16 19:48:51 +0200 | [diff] [blame] | 319 | if verbose > 1: print("Running from compiled:", mod_name) |
| 320 | self._fix_ns_for_legacy_pyc(expected_ns, alter_sys) |
| 321 | self.check_code_execution(create_ns, expected_ns) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 322 | finally: |
| 323 | self._del_pkg(pkg_dir, depth, mod_name) |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 324 | if verbose > 1: print("Module executed successfully") |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 325 | |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 326 | def _check_package(self, depth, alter_sys=False, |
| 327 | *, namespace=False, parent_namespaces=False): |
| 328 | pkg_dir, mod_fname, mod_name, mod_spec = ( |
| 329 | self._make_pkg(example_source, depth, "__main__", |
| 330 | namespace=namespace, |
| 331 | parent_namespaces=parent_namespaces)) |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 332 | pkg_name = mod_name.rpartition(".")[0] |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 333 | forget(mod_name) |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 334 | expected_ns = example_namespace.copy() |
| 335 | expected_ns.update({ |
| 336 | "__name__": mod_name, |
| 337 | "__file__": mod_fname, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 338 | "__cached__": importlib.util.cache_from_source(mod_fname), |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 339 | "__package__": pkg_name, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 340 | "__spec__": mod_spec, |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 341 | }) |
| 342 | if alter_sys: |
| 343 | expected_ns.update({ |
| 344 | "run_argv0": mod_fname, |
| 345 | "run_name_in_sys_modules": True, |
| 346 | "module_in_sys_modules": True, |
| 347 | }) |
| 348 | def create_ns(init_globals): |
| 349 | return run_module(pkg_name, init_globals, alter_sys=alter_sys) |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 350 | try: |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 351 | if verbose > 1: print("Running from source:", pkg_name) |
| 352 | self.check_code_execution(create_ns, expected_ns) |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 353 | importlib.invalidate_caches() |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 354 | __import__(mod_name) |
| 355 | os.remove(mod_fname) |
Ezio Melotti | c28f6fa | 2013-03-16 19:48:51 +0200 | [diff] [blame] | 356 | if not sys.dont_write_bytecode: |
| 357 | make_legacy_pyc(mod_fname) |
| 358 | unload(mod_name) # In case loader caches paths |
| 359 | if verbose > 1: print("Running from compiled:", pkg_name) |
Ezio Melotti | e5e7a7c | 2013-03-16 21:49:20 +0200 | [diff] [blame] | 360 | importlib.invalidate_caches() |
Ezio Melotti | c28f6fa | 2013-03-16 19:48:51 +0200 | [diff] [blame] | 361 | self._fix_ns_for_legacy_pyc(expected_ns, alter_sys) |
| 362 | self.check_code_execution(create_ns, expected_ns) |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 363 | finally: |
| 364 | self._del_pkg(pkg_dir, depth, pkg_name) |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 365 | if verbose > 1: print("Package executed successfully") |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 366 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 367 | def _add_relative_modules(self, base_dir, source, depth): |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 368 | if depth <= 1: |
| 369 | raise ValueError("Relative module test needs depth > 1") |
| 370 | pkg_name = "__runpy_pkg__" |
| 371 | module_dir = base_dir |
| 372 | for i in range(depth): |
| 373 | parent_dir = module_dir |
| 374 | module_dir = os.path.join(module_dir, pkg_name) |
| 375 | # Add sibling module |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 376 | sibling_fname = os.path.join(module_dir, "sibling.py") |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 377 | create_empty_file(sibling_fname) |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 378 | if verbose > 1: print(" Added sibling module:", sibling_fname) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 379 | # Add nephew module |
| 380 | uncle_dir = os.path.join(parent_dir, "uncle") |
| 381 | self._add_pkg_dir(uncle_dir) |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 382 | if verbose > 1: print(" Added uncle package:", uncle_dir) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 383 | cousin_dir = os.path.join(uncle_dir, "cousin") |
| 384 | self._add_pkg_dir(cousin_dir) |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 385 | if verbose > 1: print(" Added cousin package:", cousin_dir) |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 386 | nephew_fname = os.path.join(cousin_dir, "nephew.py") |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 387 | create_empty_file(nephew_fname) |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 388 | if verbose > 1: print(" Added nephew module:", nephew_fname) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 389 | |
| 390 | def _check_relative_imports(self, depth, run_name=None): |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 391 | contents = r"""\ |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 392 | from __future__ import absolute_import |
| 393 | from . import sibling |
| 394 | from ..uncle.cousin import nephew |
| 395 | """ |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 396 | pkg_dir, mod_fname, mod_name, mod_spec = ( |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 397 | self._make_pkg(contents, depth)) |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 398 | if run_name is None: |
| 399 | expected_name = mod_name |
| 400 | else: |
| 401 | expected_name = run_name |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 402 | try: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 403 | self._add_relative_modules(pkg_dir, contents, depth) |
| 404 | pkg_name = mod_name.rpartition('.')[0] |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 405 | if verbose > 1: print("Running from source:", mod_name) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 406 | d1 = run_module(mod_name, run_name=run_name) # Read from source |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 407 | self.assertEqual(d1["__name__"], expected_name) |
| 408 | self.assertEqual(d1["__package__"], pkg_name) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 409 | self.assertIn("sibling", d1) |
| 410 | self.assertIn("nephew", d1) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 411 | del d1 # Ensure __loader__ entry doesn't keep file open |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 412 | importlib.invalidate_caches() |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 413 | __import__(mod_name) |
| 414 | os.remove(mod_fname) |
Ezio Melotti | c28f6fa | 2013-03-16 19:48:51 +0200 | [diff] [blame] | 415 | if not sys.dont_write_bytecode: |
| 416 | make_legacy_pyc(mod_fname) |
| 417 | unload(mod_name) # In case the loader caches paths |
| 418 | if verbose > 1: print("Running from compiled:", mod_name) |
Ezio Melotti | e5e7a7c | 2013-03-16 21:49:20 +0200 | [diff] [blame] | 419 | importlib.invalidate_caches() |
Ezio Melotti | c28f6fa | 2013-03-16 19:48:51 +0200 | [diff] [blame] | 420 | d2 = run_module(mod_name, run_name=run_name) # Read from bytecode |
| 421 | self.assertEqual(d2["__name__"], expected_name) |
| 422 | self.assertEqual(d2["__package__"], pkg_name) |
| 423 | self.assertIn("sibling", d2) |
| 424 | self.assertIn("nephew", d2) |
| 425 | del d2 # Ensure __loader__ entry doesn't keep file open |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 426 | finally: |
| 427 | self._del_pkg(pkg_dir, depth, mod_name) |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 428 | if verbose > 1: print("Module executed successfully") |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 429 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 430 | def test_run_module(self): |
| 431 | for depth in range(4): |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 432 | if verbose > 1: print("Testing package depth:", depth) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 433 | self._check_module(depth) |
| 434 | |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 435 | def test_run_module_in_namespace_package(self): |
| 436 | for depth in range(1, 4): |
| 437 | if verbose > 1: print("Testing package depth:", depth) |
| 438 | self._check_module(depth, namespace=True, parent_namespaces=True) |
| 439 | |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 440 | def test_run_package(self): |
| 441 | for depth in range(1, 4): |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 442 | if verbose > 1: print("Testing package depth:", depth) |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 443 | self._check_package(depth) |
| 444 | |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 445 | def test_run_package_init_exceptions(self): |
| 446 | # These were previously wrapped in an ImportError; see Issue 14285 |
| 447 | result = self._make_pkg("", 1, "__main__") |
| 448 | pkg_dir, _, mod_name, _ = result |
| 449 | mod_name = mod_name.replace(".__main__", "") |
| 450 | self.addCleanup(self._del_pkg, pkg_dir, 1, mod_name) |
| 451 | init = os.path.join(pkg_dir, "__runpy_pkg__", "__init__.py") |
| 452 | |
| 453 | exceptions = (ImportError, AttributeError, TypeError, ValueError) |
| 454 | for exception in exceptions: |
| 455 | name = exception.__name__ |
| 456 | with self.subTest(name): |
| 457 | source = "raise {0}('{0} in __init__.py.')".format(name) |
| 458 | with open(init, "wt", encoding="ascii") as mod_file: |
| 459 | mod_file.write(source) |
| 460 | try: |
| 461 | run_module(mod_name) |
| 462 | except exception as err: |
| 463 | self.assertNotIn("finding spec", format(err)) |
| 464 | else: |
| 465 | self.fail("Nothing raised; expected {}".format(name)) |
Martin Panter | 7dda421 | 2015-12-10 06:47:06 +0000 | [diff] [blame] | 466 | try: |
| 467 | run_module(mod_name + ".submodule") |
| 468 | except exception as err: |
| 469 | self.assertNotIn("finding spec", format(err)) |
| 470 | else: |
| 471 | self.fail("Nothing raised; expected {}".format(name)) |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 472 | |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 473 | def test_run_package_in_namespace_package(self): |
| 474 | for depth in range(1, 4): |
| 475 | if verbose > 1: print("Testing package depth:", depth) |
| 476 | self._check_package(depth, parent_namespaces=True) |
| 477 | |
| 478 | def test_run_namespace_package(self): |
| 479 | for depth in range(1, 4): |
| 480 | if verbose > 1: print("Testing package depth:", depth) |
| 481 | self._check_package(depth, namespace=True) |
| 482 | |
| 483 | def test_run_namespace_package_in_namespace_package(self): |
| 484 | for depth in range(1, 4): |
| 485 | if verbose > 1: print("Testing package depth:", depth) |
| 486 | self._check_package(depth, namespace=True, parent_namespaces=True) |
| 487 | |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 488 | def test_run_module_alter_sys(self): |
| 489 | for depth in range(4): |
| 490 | if verbose > 1: print("Testing package depth:", depth) |
| 491 | self._check_module(depth, alter_sys=True) |
| 492 | |
| 493 | def test_run_package_alter_sys(self): |
| 494 | for depth in range(1, 4): |
| 495 | if verbose > 1: print("Testing package depth:", depth) |
| 496 | self._check_package(depth, alter_sys=True) |
| 497 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 498 | def test_explicit_relative_import(self): |
| 499 | for depth in range(2, 5): |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 500 | if verbose > 1: print("Testing relative imports at depth:", depth) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 501 | self._check_relative_imports(depth) |
| 502 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 503 | def test_main_relative_import(self): |
| 504 | for depth in range(2, 5): |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 505 | if verbose > 1: print("Testing main relative imports at depth:", depth) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 506 | self._check_relative_imports(depth, "__main__") |
| 507 | |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 508 | def test_run_name(self): |
| 509 | depth = 1 |
| 510 | run_name = "And now for something completely different" |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 511 | pkg_dir, mod_fname, mod_name, mod_spec = ( |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 512 | self._make_pkg(example_source, depth)) |
| 513 | forget(mod_name) |
| 514 | expected_ns = example_namespace.copy() |
| 515 | expected_ns.update({ |
| 516 | "__name__": run_name, |
| 517 | "__file__": mod_fname, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 518 | "__cached__": importlib.util.cache_from_source(mod_fname), |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 519 | "__package__": mod_name.rpartition(".")[0], |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 520 | "__spec__": mod_spec, |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 521 | }) |
| 522 | def create_ns(init_globals): |
| 523 | return run_module(mod_name, init_globals, run_name) |
| 524 | try: |
| 525 | self.check_code_execution(create_ns, expected_ns) |
| 526 | finally: |
| 527 | self._del_pkg(pkg_dir, depth, mod_name) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 528 | |
Nick Coghlan | 8ecf504 | 2012-07-15 21:19:18 +1000 | [diff] [blame] | 529 | def test_pkgutil_walk_packages(self): |
| 530 | # This is a dodgy hack to use the test_runpy infrastructure to test |
| 531 | # issue #15343. Issue #15348 declares this is indeed a dodgy hack ;) |
| 532 | import pkgutil |
| 533 | max_depth = 4 |
| 534 | base_name = "__runpy_pkg__" |
| 535 | package_suffixes = ["uncle", "uncle.cousin"] |
| 536 | module_suffixes = ["uncle.cousin.nephew", base_name + ".sibling"] |
| 537 | expected_packages = set() |
| 538 | expected_modules = set() |
| 539 | for depth in range(1, max_depth): |
| 540 | pkg_name = ".".join([base_name] * depth) |
| 541 | expected_packages.add(pkg_name) |
| 542 | for name in package_suffixes: |
| 543 | expected_packages.add(pkg_name + "." + name) |
| 544 | for name in module_suffixes: |
| 545 | expected_modules.add(pkg_name + "." + name) |
| 546 | pkg_name = ".".join([base_name] * max_depth) |
| 547 | expected_packages.add(pkg_name) |
| 548 | expected_modules.add(pkg_name + ".runpy_test") |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 549 | pkg_dir, mod_fname, mod_name, mod_spec = ( |
Nick Coghlan | 8ecf504 | 2012-07-15 21:19:18 +1000 | [diff] [blame] | 550 | self._make_pkg("", max_depth)) |
| 551 | self.addCleanup(self._del_pkg, pkg_dir, max_depth, mod_name) |
| 552 | for depth in range(2, max_depth+1): |
| 553 | self._add_relative_modules(pkg_dir, "", depth) |
| 554 | for finder, mod_name, ispkg in pkgutil.walk_packages([pkg_dir]): |
| 555 | self.assertIsInstance(finder, |
| 556 | importlib.machinery.FileFinder) |
| 557 | if ispkg: |
| 558 | expected_packages.remove(mod_name) |
| 559 | else: |
| 560 | expected_modules.remove(mod_name) |
| 561 | self.assertEqual(len(expected_packages), 0, expected_packages) |
| 562 | self.assertEqual(len(expected_modules), 0, expected_modules) |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 563 | |
| 564 | class RunPathTestCase(unittest.TestCase, CodeExecutionMixin): |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 565 | """Unit tests for runpy.run_path""" |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 566 | |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 567 | def _make_test_script(self, script_dir, script_basename, |
| 568 | source=None, omit_suffix=False): |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 569 | if source is None: |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 570 | source = example_source |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 571 | return make_script(script_dir, script_basename, |
| 572 | source, omit_suffix) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 573 | |
| 574 | def _check_script(self, script_name, expected_name, expected_file, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 575 | expected_argv0, mod_name=None, |
| 576 | expect_spec=True, check_loader=True): |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 577 | # First check is without run_name |
| 578 | def create_ns(init_globals): |
| 579 | return run_path(script_name, init_globals) |
| 580 | expected_ns = example_namespace.copy() |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 581 | if mod_name is None: |
| 582 | spec_name = expected_name |
| 583 | else: |
| 584 | spec_name = mod_name |
| 585 | if expect_spec: |
| 586 | mod_spec = importlib.util.spec_from_file_location(spec_name, |
| 587 | expected_file) |
| 588 | mod_cached = mod_spec.cached |
| 589 | if not check_loader: |
| 590 | mod_spec.loader = None |
| 591 | else: |
| 592 | mod_spec = mod_cached = None |
| 593 | |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 594 | expected_ns.update({ |
| 595 | "__name__": expected_name, |
| 596 | "__file__": expected_file, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 597 | "__cached__": mod_cached, |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 598 | "__package__": "", |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 599 | "__spec__": mod_spec, |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 600 | "run_argv0": expected_argv0, |
| 601 | "run_name_in_sys_modules": True, |
| 602 | "module_in_sys_modules": True, |
| 603 | }) |
| 604 | self.check_code_execution(create_ns, expected_ns) |
| 605 | # Second check makes sure run_name works in all cases |
| 606 | run_name = "prove.issue15230.is.fixed" |
| 607 | def create_ns(init_globals): |
| 608 | return run_path(script_name, init_globals, run_name) |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 609 | if expect_spec and mod_name is None: |
| 610 | mod_spec = importlib.util.spec_from_file_location(run_name, |
| 611 | expected_file) |
| 612 | if not check_loader: |
| 613 | mod_spec.loader = None |
| 614 | expected_ns["__spec__"] = mod_spec |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 615 | expected_ns["__name__"] = run_name |
| 616 | expected_ns["__package__"] = run_name.rpartition(".")[0] |
| 617 | self.check_code_execution(create_ns, expected_ns) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 618 | |
| 619 | def _check_import_error(self, script_name, msg): |
Nick Coghlan | 16eb0fb | 2009-11-18 11:35:25 +0000 | [diff] [blame] | 620 | msg = re.escape(msg) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 621 | self.assertRaisesRegex(ImportError, msg, run_path, script_name) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 622 | |
| 623 | def test_basic_script(self): |
| 624 | with temp_dir() as script_dir: |
| 625 | mod_name = 'script' |
| 626 | script_name = self._make_test_script(script_dir, mod_name) |
| 627 | self._check_script(script_name, "<run_path>", script_name, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 628 | script_name, expect_spec=False) |
| 629 | |
| 630 | def test_basic_script_no_suffix(self): |
| 631 | with temp_dir() as script_dir: |
| 632 | mod_name = 'script' |
| 633 | script_name = self._make_test_script(script_dir, mod_name, |
| 634 | omit_suffix=True) |
| 635 | self._check_script(script_name, "<run_path>", script_name, |
| 636 | script_name, expect_spec=False) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 637 | |
| 638 | def test_script_compiled(self): |
| 639 | with temp_dir() as script_dir: |
| 640 | mod_name = 'script' |
| 641 | script_name = self._make_test_script(script_dir, mod_name) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 642 | compiled_name = py_compile.compile(script_name, doraise=True) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 643 | os.remove(script_name) |
| 644 | self._check_script(compiled_name, "<run_path>", compiled_name, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 645 | compiled_name, expect_spec=False) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 646 | |
| 647 | def test_directory(self): |
| 648 | with temp_dir() as script_dir: |
| 649 | mod_name = '__main__' |
| 650 | script_name = self._make_test_script(script_dir, mod_name) |
| 651 | self._check_script(script_dir, "<run_path>", script_name, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 652 | script_dir, mod_name=mod_name) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 653 | |
| 654 | def test_directory_compiled(self): |
| 655 | with temp_dir() as script_dir: |
| 656 | mod_name = '__main__' |
| 657 | script_name = self._make_test_script(script_dir, mod_name) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 658 | compiled_name = py_compile.compile(script_name, doraise=True) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 659 | os.remove(script_name) |
Ezio Melotti | c28f6fa | 2013-03-16 19:48:51 +0200 | [diff] [blame] | 660 | if not sys.dont_write_bytecode: |
| 661 | legacy_pyc = make_legacy_pyc(script_name) |
| 662 | self._check_script(script_dir, "<run_path>", legacy_pyc, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 663 | script_dir, mod_name=mod_name) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 664 | |
| 665 | def test_directory_error(self): |
| 666 | with temp_dir() as script_dir: |
| 667 | mod_name = 'not_main' |
| 668 | script_name = self._make_test_script(script_dir, mod_name) |
| 669 | msg = "can't find '__main__' module in %r" % script_dir |
| 670 | self._check_import_error(script_dir, msg) |
| 671 | |
| 672 | def test_zipfile(self): |
| 673 | with temp_dir() as script_dir: |
| 674 | mod_name = '__main__' |
| 675 | script_name = self._make_test_script(script_dir, mod_name) |
| 676 | zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name) |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 677 | self._check_script(zip_name, "<run_path>", fname, zip_name, |
| 678 | mod_name=mod_name, check_loader=False) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 679 | |
| 680 | def test_zipfile_compiled(self): |
| 681 | with temp_dir() as script_dir: |
| 682 | mod_name = '__main__' |
| 683 | script_name = self._make_test_script(script_dir, mod_name) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 684 | compiled_name = py_compile.compile(script_name, doraise=True) |
| 685 | zip_name, fname = make_zip_script(script_dir, 'test_zip', |
| 686 | compiled_name) |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 687 | self._check_script(zip_name, "<run_path>", fname, zip_name, |
| 688 | mod_name=mod_name, check_loader=False) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 689 | |
| 690 | def test_zipfile_error(self): |
| 691 | with temp_dir() as script_dir: |
| 692 | mod_name = 'not_main' |
| 693 | script_name = self._make_test_script(script_dir, mod_name) |
| 694 | zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name) |
| 695 | msg = "can't find '__main__' module in %r" % zip_name |
| 696 | self._check_import_error(zip_name, msg) |
| 697 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 698 | @no_tracing |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 699 | def test_main_recursion_error(self): |
| 700 | with temp_dir() as script_dir, temp_dir() as dummy_dir: |
| 701 | mod_name = '__main__' |
| 702 | source = ("import runpy\n" |
| 703 | "runpy.run_path(%r)\n") % dummy_dir |
| 704 | script_name = self._make_test_script(script_dir, mod_name, source) |
| 705 | zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name) |
| 706 | msg = "recursion depth exceeded" |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 707 | self.assertRaisesRegex(RecursionError, msg, run_path, zip_name) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 708 | |
Victor Stinner | 6c47102 | 2011-07-04 01:45:39 +0200 | [diff] [blame] | 709 | def test_encoding(self): |
| 710 | with temp_dir() as script_dir: |
| 711 | filename = os.path.join(script_dir, 'script.py') |
| 712 | with open(filename, 'w', encoding='latin1') as f: |
| 713 | f.write(""" |
| 714 | #coding:latin1 |
Benjamin Peterson | 951a9e3 | 2012-10-12 11:44:10 -0400 | [diff] [blame] | 715 | s = "non-ASCII: h\xe9" |
Victor Stinner | 6c47102 | 2011-07-04 01:45:39 +0200 | [diff] [blame] | 716 | """) |
| 717 | result = run_path(filename) |
Benjamin Peterson | 951a9e3 | 2012-10-12 11:44:10 -0400 | [diff] [blame] | 718 | self.assertEqual(result['s'], "non-ASCII: h\xe9") |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 719 | |
| 720 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 721 | if __name__ == "__main__": |
Brett Cannon | 3e9a9ae | 2013-06-12 21:25:59 -0400 | [diff] [blame] | 722 | unittest.main() |