Debian Codegraph @

Lib/test/test_runpy.py

raw
1 # Test the runpy module
2 import contextlib
3 import importlib.machinery, importlib.util
4 import os.path
5 import pathlib
6 import py_compile
7 import re
8 import signal
9 import subprocess
10 import sys
11 import tempfile
12 import textwrap
13 import unittest
14 import warnings
15 from test.support import (
16 force_not_colorized_test_class,
17 infinite_recursion,
18 no_tracing,
19 requires_resource,
20 requires_subprocess,
21 verbose,
22 )
23 from test.support.import_helper import forget, make_legacy_pyc, unload
24 from test.support.os_helper import create_empty_file, temp_dir, FakePath
25 from test.support.script_helper import make_script, make_zip_script
26
27
28 import runpy
29 from runpy import _run_code, _run_module_code, run_module, run_path
30 # Note: This module can't safely test _run_module_as_main as it
31 # runs its tests in the current process, which would mess with the
32 # real __main__ module (usually test.regrtest)
33 # See test_cmd_line_script for a test that executes that code path
34
35
36 # Set up the test code and expected results
37 example_source = """\
38 # Check basic code execution
39 result = ['Top level assignment']
40 def f():
41 result.append('Lower level reference')
42 f()
43 del f
44 # Check the sys module
45 import sys
46 run_argv0 = sys.argv[0]
47 run_name_in_sys_modules = __name__ in sys.modules
48 module_in_sys_modules = (run_name_in_sys_modules and
49 globals() is sys.modules[__name__].__dict__)
50 # Check nested operation
51 import runpy
52 nested = runpy._run_module_code('x=1\\n', mod_name='<run>')
53 """
54
55 implicit_namespace = {
56 "__name__": None,
57 "__file__": None,
58 "__cached__": None,
59 "__package__": None,
60 "__doc__": None,
61 "__spec__": None
62 }
63 example_namespace = {
64 "sys": sys,
65 "runpy": runpy,
66 "result": ["Top level assignment", "Lower level reference"],
67 "run_argv0": sys.argv[0],
68 "run_name_in_sys_modules": False,
69 "module_in_sys_modules": False,
70 "nested": dict(implicit_namespace,
71 x=1, __name__="<run>", __loader__=None),
72 }
73 example_namespace.update(implicit_namespace)
74
75 class CodeExecutionMixin:
76 # Issue #15230 (run_path not handling run_name correctly) highlighted a
77 # problem with the way arguments were being passed from higher level APIs
78 # down to lower level code. This mixin makes it easier to ensure full
79 # testing occurs at those upper layers as well, not just at the utility
80 # layer
81
82 # Figuring out the loader details in advance is hard to do, so we skip
83 # checking the full details of loader and loader_state
84 CHECKED_SPEC_ATTRIBUTES = ["name", "parent", "origin", "cached",
85 "has_location", "submodule_search_locations"]
86
87 def assertNamespaceMatches(self, result_ns, expected_ns):
88 """Check two namespaces match.
89
90 Ignores any unspecified interpreter created names
91 """
92 # Avoid side effects
93 result_ns = result_ns.copy()
94 expected_ns = expected_ns.copy()
95 # Impls are permitted to add extra names, so filter them out
96 for k in list(result_ns):
97 if k.startswith("__") and k.endswith("__"):
98 if k not in expected_ns:
99 result_ns.pop(k)
100 if k not in expected_ns["nested"]:
101 result_ns["nested"].pop(k)
102 # Spec equality includes the loader, so we take the spec out of the
103 # result namespace and check that separately
104 result_spec = result_ns.pop("__spec__")
105 expected_spec = expected_ns.pop("__spec__")
106 if expected_spec is None:
107 self.assertIsNone(result_spec)
108 else:
109 # If an expected loader is set, we just check we got the right
110 # type, rather than checking for full equality
111 if expected_spec.loader is not None:
112 self.assertEqual(type(result_spec.loader),
113 type(expected_spec.loader))
114 for attr in self.CHECKED_SPEC_ATTRIBUTES:
115 k = "__spec__." + attr
116 actual = (k, getattr(result_spec, attr))
117 expected = (k, getattr(expected_spec, attr))
118 self.assertEqual(actual, expected)
119 # For the rest, we still don't use direct dict comparison on the
120 # namespace, as the diffs are too hard to debug if anything breaks
121 self.assertEqual(set(result_ns), set(expected_ns))
122 for k in result_ns:
123 actual = (k, result_ns[k])
124 expected = (k, expected_ns[k])
125 self.assertEqual(actual, expected)
126
127 def check_code_execution(self, create_namespace, expected_namespace):
128 """Check that an interface runs the example code correctly
129
130 First argument is a callable accepting the initial globals and
131 using them to create the actual namespace
132 Second argument is the expected result
133 """
134 sentinel = object()
135 expected_ns = expected_namespace.copy()
136 run_name = expected_ns["__name__"]
137 saved_argv0 = sys.argv[0]
138 saved_mod = sys.modules.get(run_name, sentinel)
139 # Check without initial globals
140 result_ns = create_namespace(None)
141 self.assertNamespaceMatches(result_ns, expected_ns)
142 self.assertIs(sys.argv[0], saved_argv0)
143 self.assertIs(sys.modules.get(run_name, sentinel), saved_mod)
144 # And then with initial globals
145 initial_ns = {"sentinel": sentinel}
146 expected_ns["sentinel"] = sentinel
147 result_ns = create_namespace(initial_ns)
148 self.assertIsNot(result_ns, initial_ns)
149 self.assertNamespaceMatches(result_ns, expected_ns)
150 self.assertIs(sys.argv[0], saved_argv0)
151 self.assertIs(sys.modules.get(run_name, sentinel), saved_mod)
152
153
154 class ExecutionLayerTestCase(unittest.TestCase, CodeExecutionMixin):
155 """Unit tests for runpy._run_code and runpy._run_module_code"""
156
157 def test_run_code(self):
158 expected_ns = example_namespace.copy()
159 expected_ns.update({
160 "__loader__": None,
161 })
162 def create_ns(init_globals):
163 return _run_code(example_source, {}, init_globals)
164 self.check_code_execution(create_ns, expected_ns)
165
166 def test_run_module_code(self):
167 mod_name = "<Nonsense>"
168 mod_fname = "Some other nonsense"
169 mod_loader = "Now you're just being silly"
170 mod_package = '' # Treat as a top level module
171 mod_spec = importlib.machinery.ModuleSpec(mod_name,
172 origin=mod_fname,
173 loader=mod_loader)
174 expected_ns = example_namespace.copy()
175 expected_ns.update({
176 "__name__": mod_name,
177 "__file__": mod_fname,
178 "__loader__": mod_loader,
179 "__package__": mod_package,
180 "__spec__": mod_spec,
181 "run_argv0": mod_fname,
182 "run_name_in_sys_modules": True,
183 "module_in_sys_modules": True,
184 })
185 def create_ns(init_globals):
186 return _run_module_code(example_source,
187 init_globals,
188 mod_name,
189 mod_spec)
190 self.check_code_execution(create_ns, expected_ns)
191
192 # TODO: Use self.addCleanup to get rid of a lot of try-finally blocks
193 class RunModuleTestCase(unittest.TestCase, CodeExecutionMixin):
194 """Unit tests for runpy.run_module"""
195
196 def expect_import_error(self, mod_name):
197 try:
198 run_module(mod_name)
199 except ImportError:
200 pass
201 else:
202 self.fail("Expected import error for " + mod_name)
203
204 def test_invalid_names(self):
205 # Builtin module
206 self.expect_import_error("sys")
207 # Non-existent modules
208 self.expect_import_error("sys.imp.eric")
209 self.expect_import_error("os.path.half")
210 self.expect_import_error("a.bee")
211 # Relative names not allowed
212 self.expect_import_error(".howard")
213 self.expect_import_error("..eaten")
214 self.expect_import_error(".test_runpy")
215 self.expect_import_error(".unittest")
216 # Package without __main__.py
217 self.expect_import_error("multiprocessing")
218
219 def test_invalid_names_set_name_attribute(self):
220 cases = [
221 # (mod_name, expected_name) -- comment indicates raise site
222 ("nonexistent_runpy_test_module",
223 "nonexistent_runpy_test_module"), # spec is None
224 ("sys.imp.eric", "sys.imp.eric"), # find_spec error
225 (".relative_name", ".relative_name"), # relative name rejected
226 ("sys", "sys"), # builtin: no code object
227 ("multiprocessing", "multiprocessing"), # package without __main__
228 ]
229 for mod_name, expected_name in cases:
230 with self.subTest(mod_name=mod_name):
231 try:
232 run_module(mod_name)
233 except ImportError as exc:
234 self.assertEqual(exc.name, expected_name)
235 else:
236 self.fail("Expected ImportError for %r" % mod_name)
237
238 def test_library_module(self):
239 self.assertEqual(run_module("runpy")["__name__"], "runpy")
240
241 def _add_pkg_dir(self, pkg_dir, namespace=False):
242 os.mkdir(pkg_dir)
243 if namespace:
244 return None
245 pkg_fname = os.path.join(pkg_dir, "__init__.py")
246 create_empty_file(pkg_fname)
247 return pkg_fname
248
249 def _make_pkg(self, source, depth, mod_base="runpy_test",
250 *, namespace=False, parent_namespaces=False):
251 # Enforce a couple of internal sanity checks on test cases
252 if (namespace or parent_namespaces) and not depth:
253 raise RuntimeError("Can't mark top level module as a "
254 "namespace package")
255 pkg_name = "__runpy_pkg__"
256 test_fname = mod_base+os.extsep+"py"
257 pkg_dir = sub_dir = os.path.realpath(tempfile.mkdtemp())
258 if verbose > 1: print(" Package tree in:", sub_dir)
259 sys.path.insert(0, pkg_dir)
260 if verbose > 1: print(" Updated sys.path:", sys.path[0])
261 if depth:
262 namespace_flags = [parent_namespaces] * depth
263 namespace_flags[-1] = namespace
264 for namespace_flag in namespace_flags:
265 sub_dir = os.path.join(sub_dir, pkg_name)
266 pkg_fname = self._add_pkg_dir(sub_dir, namespace_flag)
267 if verbose > 1: print(" Next level in:", sub_dir)
268 if verbose > 1: print(" Created:", pkg_fname)
269 mod_fname = os.path.join(sub_dir, test_fname)
270 with open(mod_fname, "w") as mod_file:
271 mod_file.write(source)
272 if verbose > 1: print(" Created:", mod_fname)
273 mod_name = (pkg_name+".")*depth + mod_base
274 mod_spec = importlib.util.spec_from_file_location(mod_name,
275 mod_fname)
276 return pkg_dir, mod_fname, mod_name, mod_spec
277
278 def _del_pkg(self, top):
279 for entry in list(sys.modules):
280 if entry.startswith("__runpy_pkg__"):
281 del sys.modules[entry]
282 if verbose > 1: print(" Removed sys.modules entries")
283 del sys.path[0]
284 if verbose > 1: print(" Removed sys.path entry")
285 for root, dirs, files in os.walk(top, topdown=False):
286 for name in files:
287 try:
288 os.remove(os.path.join(root, name))
289 except OSError as ex:
290 if verbose > 1: print(ex) # Persist with cleaning up
291 for name in dirs:
292 fullname = os.path.join(root, name)
293 try:
294 os.rmdir(fullname)
295 except OSError as ex:
296 if verbose > 1: print(ex) # Persist with cleaning up
297 try:
298 os.rmdir(top)
299 if verbose > 1: print(" Removed package tree")
300 except OSError as ex:
301 if verbose > 1: print(ex) # Persist with cleaning up
302
303 def _fix_ns_for_legacy_pyc(self, ns, alter_sys):
304 char_to_add = "c"
305 ns["__file__"] += char_to_add
306 ns["__cached__"] = ns["__file__"]
307 spec = ns["__spec__"]
308 new_spec = importlib.util.spec_from_file_location(spec.name,
309 ns["__file__"])
310 ns["__spec__"] = new_spec
311 if alter_sys:
312 ns["run_argv0"] += char_to_add
313
314
315 def _check_module(self, depth, alter_sys=False,
316 *, namespace=False, parent_namespaces=False):
317 pkg_dir, mod_fname, mod_name, mod_spec = (
318 self._make_pkg(example_source, depth,
319 namespace=namespace,
320 parent_namespaces=parent_namespaces))
321 forget(mod_name)
322 expected_ns = example_namespace.copy()
323 expected_ns.update({
324 "__name__": mod_name,
325 "__file__": mod_fname,
326 "__cached__": mod_spec.cached,
327 "__package__": mod_name.rpartition(".")[0],
328 "__spec__": mod_spec,
329 })
330 if alter_sys:
331 expected_ns.update({
332 "run_argv0": mod_fname,
333 "run_name_in_sys_modules": True,
334 "module_in_sys_modules": True,
335 })
336 def create_ns(init_globals):
337 return run_module(mod_name, init_globals, alter_sys=alter_sys)
338 try:
339 if verbose > 1: print("Running from source:", mod_name)
340 self.check_code_execution(create_ns, expected_ns)
341 importlib.invalidate_caches()
342 __import__(mod_name)
343 os.remove(mod_fname)
344 if not sys.dont_write_bytecode:
345 make_legacy_pyc(mod_fname)
346 unload(mod_name) # In case loader caches paths
347 importlib.invalidate_caches()
348 if verbose > 1: print("Running from compiled:", mod_name)
349 self._fix_ns_for_legacy_pyc(expected_ns, alter_sys)
350 self.check_code_execution(create_ns, expected_ns)
351 finally:
352 self._del_pkg(pkg_dir)
353 if verbose > 1: print("Module executed successfully")
354
355 def _check_package(self, depth, alter_sys=False,
356 *, namespace=False, parent_namespaces=False):
357 pkg_dir, mod_fname, mod_name, mod_spec = (
358 self._make_pkg(example_source, depth, "__main__",
359 namespace=namespace,
360 parent_namespaces=parent_namespaces))
361 pkg_name = mod_name.rpartition(".")[0]
362 forget(mod_name)
363 expected_ns = example_namespace.copy()
364 expected_ns.update({
365 "__name__": mod_name,
366 "__file__": mod_fname,
367 "__cached__": importlib.util.cache_from_source(mod_fname),
368 "__package__": pkg_name,
369 "__spec__": mod_spec,
370 })
371 if alter_sys:
372 expected_ns.update({
373 "run_argv0": mod_fname,
374 "run_name_in_sys_modules": True,
375 "module_in_sys_modules": True,
376 })
377 def create_ns(init_globals):
378 return run_module(pkg_name, init_globals, alter_sys=alter_sys)
379 try:
380 if verbose > 1: print("Running from source:", pkg_name)
381 self.check_code_execution(create_ns, expected_ns)
382 importlib.invalidate_caches()
383 __import__(mod_name)
384 os.remove(mod_fname)
385 if not sys.dont_write_bytecode:
386 make_legacy_pyc(mod_fname)
387 unload(mod_name) # In case loader caches paths
388 if verbose > 1: print("Running from compiled:", pkg_name)
389 importlib.invalidate_caches()
390 self._fix_ns_for_legacy_pyc(expected_ns, alter_sys)
391 self.check_code_execution(create_ns, expected_ns)
392 finally:
393 self._del_pkg(pkg_dir)
394 if verbose > 1: print("Package executed successfully")
395
396 def _add_relative_modules(self, base_dir, source, depth):
397 if depth <= 1:
398 raise ValueError("Relative module test needs depth > 1")
399 pkg_name = "__runpy_pkg__"
400 module_dir = base_dir
401 for i in range(depth):
402 parent_dir = module_dir
403 module_dir = os.path.join(module_dir, pkg_name)
404 # Add sibling module
405 sibling_fname = os.path.join(module_dir, "sibling.py")
406 create_empty_file(sibling_fname)
407 if verbose > 1: print(" Added sibling module:", sibling_fname)
408 # Add nephew module
409 uncle_dir = os.path.join(parent_dir, "uncle")
410 self._add_pkg_dir(uncle_dir)
411 if verbose > 1: print(" Added uncle package:", uncle_dir)
412 cousin_dir = os.path.join(uncle_dir, "cousin")
413 self._add_pkg_dir(cousin_dir)
414 if verbose > 1: print(" Added cousin package:", cousin_dir)
415 nephew_fname = os.path.join(cousin_dir, "nephew.py")
416 create_empty_file(nephew_fname)
417 if verbose > 1: print(" Added nephew module:", nephew_fname)
418
419 def _check_relative_imports(self, depth, run_name=None):
420 contents = r"""\
421 from __future__ import absolute_import
422 from . import sibling
423 from ..uncle.cousin import nephew
424 """
425 pkg_dir, mod_fname, mod_name, mod_spec = (
426 self._make_pkg(contents, depth))
427 if run_name is None:
428 expected_name = mod_name
429 else:
430 expected_name = run_name
431 try:
432 self._add_relative_modules(pkg_dir, contents, depth)
433 pkg_name = mod_name.rpartition('.')[0]
434 if verbose > 1: print("Running from source:", mod_name)
435 d1 = run_module(mod_name, run_name=run_name) # Read from source
436 self.assertEqual(d1["__name__"], expected_name)
437 self.assertEqual(d1["__package__"], pkg_name)
438 self.assertIn("sibling", d1)
439 self.assertIn("nephew", d1)
440 del d1 # Ensure __loader__ entry doesn't keep file open
441 importlib.invalidate_caches()
442 __import__(mod_name)
443 os.remove(mod_fname)
444 if not sys.dont_write_bytecode:
445 make_legacy_pyc(mod_fname)
446 unload(mod_name) # In case the loader caches paths
447 if verbose > 1: print("Running from compiled:", mod_name)
448 importlib.invalidate_caches()
449 d2 = run_module(mod_name, run_name=run_name) # Read from bytecode
450 self.assertEqual(d2["__name__"], expected_name)
451 self.assertEqual(d2["__package__"], pkg_name)
452 self.assertIn("sibling", d2)
453 self.assertIn("nephew", d2)
454 del d2 # Ensure __loader__ entry doesn't keep file open
455 finally:
456 self._del_pkg(pkg_dir)
457 if verbose > 1: print("Module executed successfully")
458
459 def test_run_module(self):
460 for depth in range(4):
461 if verbose > 1: print("Testing package depth:", depth)
462 self._check_module(depth)
463
464 def test_run_module_in_namespace_package(self):
465 for depth in range(1, 4):
466 if verbose > 1: print("Testing package depth:", depth)
467 self._check_module(depth, namespace=True, parent_namespaces=True)
468
469 def test_run_package(self):
470 for depth in range(1, 4):
471 if verbose > 1: print("Testing package depth:", depth)
472 self._check_package(depth)
473
474 def test_run_package_init_exceptions(self):
475 # These were previously wrapped in an ImportError; see Issue 14285
476 result = self._make_pkg("", 1, "__main__")
477 pkg_dir, _, mod_name, _ = result
478 mod_name = mod_name.replace(".__main__", "")
479 self.addCleanup(self._del_pkg, pkg_dir)
480 init = os.path.join(pkg_dir, "__runpy_pkg__", "__init__.py")
481
482 exceptions = (ImportError, AttributeError, TypeError, ValueError)
483 for exception in exceptions:
484 name = exception.__name__
485 with self.subTest(name):
486 source = "raise {0}('{0} in __init__.py.')".format(name)
487 with open(init, "wt", encoding="ascii") as mod_file:
488 mod_file.write(source)
489 try:
490 run_module(mod_name)
491 except exception as err:
492 self.assertNotIn("finding spec", format(err))
493 else:
494 self.fail("Nothing raised; expected {}".format(name))
495 try:
496 run_module(mod_name + ".submodule")
497 except exception as err:
498 self.assertNotIn("finding spec", format(err))
499 else:
500 self.fail("Nothing raised; expected {}".format(name))
501
502 def test_submodule_imported_warning(self):
503 pkg_dir, _, mod_name, _ = self._make_pkg("", 1)
504 try:
505 __import__(mod_name)
506 with self.assertWarnsRegex(RuntimeWarning,
507 r"found in sys\.modules"):
508 run_module(mod_name)
509 finally:
510 self._del_pkg(pkg_dir)
511
512 def test_package_imported_no_warning(self):
513 pkg_dir, _, mod_name, _ = self._make_pkg("", 1, "__main__")
514 self.addCleanup(self._del_pkg, pkg_dir)
515 package = mod_name.replace(".__main__", "")
516 # No warning should occur if we only imported the parent package
517 __import__(package)
518 self.assertIn(package, sys.modules)
519 with warnings.catch_warnings():
520 warnings.simplefilter("error", RuntimeWarning)
521 run_module(package)
522 # But the warning should occur if we imported the __main__ submodule
523 __import__(mod_name)
524 with self.assertWarnsRegex(RuntimeWarning, r"found in sys\.modules"):
525 run_module(package)
526
527 def test_run_package_in_namespace_package(self):
528 for depth in range(1, 4):
529 if verbose > 1: print("Testing package depth:", depth)
530 self._check_package(depth, parent_namespaces=True)
531
532 def test_run_namespace_package(self):
533 for depth in range(1, 4):
534 if verbose > 1: print("Testing package depth:", depth)
535 self._check_package(depth, namespace=True)
536
537 def test_run_namespace_package_in_namespace_package(self):
538 for depth in range(1, 4):
539 if verbose > 1: print("Testing package depth:", depth)
540 self._check_package(depth, namespace=True, parent_namespaces=True)
541
542 def test_run_module_alter_sys(self):
543 for depth in range(4):
544 if verbose > 1: print("Testing package depth:", depth)
545 self._check_module(depth, alter_sys=True)
546
547 def test_run_package_alter_sys(self):
548 for depth in range(1, 4):
549 if verbose > 1: print("Testing package depth:", depth)
550 self._check_package(depth, alter_sys=True)
551
552 def test_explicit_relative_import(self):
553 for depth in range(2, 5):
554 if verbose > 1: print("Testing relative imports at depth:", depth)
555 self._check_relative_imports(depth)
556
557 def test_main_relative_import(self):
558 for depth in range(2, 5):
559 if verbose > 1: print("Testing main relative imports at depth:", depth)
560 self._check_relative_imports(depth, "__main__")
561
562 def test_run_name(self):
563 depth = 1
564 run_name = "And now for something completely different"
565 pkg_dir, mod_fname, mod_name, mod_spec = (
566 self._make_pkg(example_source, depth))
567 forget(mod_name)
568 expected_ns = example_namespace.copy()
569 expected_ns.update({
570 "__name__": run_name,
571 "__file__": mod_fname,
572 "__cached__": importlib.util.cache_from_source(mod_fname),
573 "__package__": mod_name.rpartition(".")[0],
574 "__spec__": mod_spec,
575 })
576 def create_ns(init_globals):
577 return run_module(mod_name, init_globals, run_name)
578 try:
579 self.check_code_execution(create_ns, expected_ns)
580 finally:
581 self._del_pkg(pkg_dir)
582
583 def test_pkgutil_walk_packages(self):
584 # This is a dodgy hack to use the test_runpy infrastructure to test
585 # issue #15343. Issue #15348 declares this is indeed a dodgy hack ;)
586 import pkgutil
587 max_depth = 4
588 base_name = "__runpy_pkg__"
589 package_suffixes = ["uncle", "uncle.cousin"]
590 module_suffixes = ["uncle.cousin.nephew", base_name + ".sibling"]
591 expected_packages = set()
592 expected_modules = set()
593 for depth in range(1, max_depth):
594 pkg_name = ".".join([base_name] * depth)
595 expected_packages.add(pkg_name)
596 for name in package_suffixes:
597 expected_packages.add(pkg_name + "." + name)
598 for name in module_suffixes:
599 expected_modules.add(pkg_name + "." + name)
600 pkg_name = ".".join([base_name] * max_depth)
601 expected_packages.add(pkg_name)
602 expected_modules.add(pkg_name + ".runpy_test")
603 pkg_dir, mod_fname, mod_name, mod_spec = (
604 self._make_pkg("", max_depth))
605 self.addCleanup(self._del_pkg, pkg_dir)
606 for depth in range(2, max_depth+1):
607 self._add_relative_modules(pkg_dir, "", depth)
608 for moduleinfo in pkgutil.walk_packages([pkg_dir]):
609 self.assertIsInstance(moduleinfo, pkgutil.ModuleInfo)
610 self.assertIsInstance(moduleinfo.module_finder,
611 importlib.machinery.FileFinder)
612 if moduleinfo.ispkg:
613 expected_packages.remove(moduleinfo.name)
614 else:
615 expected_modules.remove(moduleinfo.name)
616 self.assertEqual(len(expected_packages), 0, expected_packages)
617 self.assertEqual(len(expected_modules), 0, expected_modules)
618
619 class RunPathTestCase(unittest.TestCase, CodeExecutionMixin):
620 """Unit tests for runpy.run_path"""
621
622 def _make_test_script(self, script_dir, script_basename,
623 source=None, omit_suffix=False):
624 if source is None:
625 source = example_source
626 return make_script(script_dir, script_basename,
627 source, omit_suffix)
628
629 def _check_script(self, script_name, expected_name, expected_file,
630 expected_argv0, mod_name=None,
631 expect_spec=True, check_loader=True):
632 # First check is without run_name
633 def create_ns(init_globals):
634 return run_path(script_name, init_globals)
635 expected_ns = example_namespace.copy()
636 if mod_name is None:
637 spec_name = expected_name
638 else:
639 spec_name = mod_name
640 if expect_spec:
641 mod_spec = importlib.util.spec_from_file_location(spec_name,
642 expected_file)
643 mod_cached = mod_spec.cached
644 if not check_loader:
645 mod_spec.loader = None
646 else:
647 mod_spec = mod_cached = None
648
649 expected_ns.update({
650 "__name__": expected_name,
651 "__file__": expected_file,
652 "__cached__": mod_cached,
653 "__package__": "",
654 "__spec__": mod_spec,
655 "run_argv0": expected_argv0,
656 "run_name_in_sys_modules": True,
657 "module_in_sys_modules": True,
658 })
659 self.check_code_execution(create_ns, expected_ns)
660 # Second check makes sure run_name works in all cases
661 run_name = "prove.issue15230.is.fixed"
662 def create_ns(init_globals):
663 return run_path(script_name, init_globals, run_name)
664 if expect_spec and mod_name is None:
665 mod_spec = importlib.util.spec_from_file_location(run_name,
666 expected_file)
667 if not check_loader:
668 mod_spec.loader = None
669 expected_ns["__spec__"] = mod_spec
670 expected_ns["__name__"] = run_name
671 expected_ns["__package__"] = run_name.rpartition(".")[0]
672 self.check_code_execution(create_ns, expected_ns)
673
674 def _check_import_error(self, script_name, msg):
675 msg = re.escape(msg)
676 self.assertRaisesRegex(ImportError, msg, run_path, script_name)
677
678 def test_basic_script(self):
679 with temp_dir() as script_dir:
680 mod_name = 'script'
681 script_name = self._make_test_script(script_dir, mod_name)
682 self._check_script(script_name, "<run_path>", script_name,
683 script_name, expect_spec=False)
684
685 def test_basic_script_with_pathlike_object(self):
686 with temp_dir() as script_dir:
687 mod_name = 'script'
688 script_name = self._make_test_script(script_dir, mod_name)
689 self._check_script(FakePath(script_name), "<run_path>",
690 script_name,
691 script_name,
692 expect_spec=False)
693
694 def test_basic_script_no_suffix(self):
695 with temp_dir() as script_dir:
696 mod_name = 'script'
697 script_name = self._make_test_script(script_dir, mod_name,
698 omit_suffix=True)
699 self._check_script(script_name, "<run_path>", script_name,
700 script_name, expect_spec=False)
701
702 def test_script_compiled(self):
703 with temp_dir() as script_dir:
704 mod_name = 'script'
705 script_name = self._make_test_script(script_dir, mod_name)
706 compiled_name = py_compile.compile(script_name, doraise=True)
707 os.remove(script_name)
708 self._check_script(compiled_name, "<run_path>", compiled_name,
709 compiled_name, expect_spec=False)
710
711 def test_directory(self):
712 with temp_dir() as script_dir:
713 mod_name = '__main__'
714 script_name = self._make_test_script(script_dir, mod_name)
715 self._check_script(script_dir, "<run_path>", script_name,
716 script_dir, mod_name=mod_name)
717
718 def test_directory_compiled(self):
719 with temp_dir() as script_dir:
720 mod_name = '__main__'
721 script_name = self._make_test_script(script_dir, mod_name)
722 compiled_name = py_compile.compile(script_name, doraise=True)
723 os.remove(script_name)
724 if not sys.dont_write_bytecode:
725 legacy_pyc = make_legacy_pyc(script_name)
726 self._check_script(script_dir, "<run_path>", legacy_pyc,
727 script_dir, mod_name=mod_name)
728
729 def test_directory_error(self):
730 with temp_dir() as script_dir:
731 mod_name = 'not_main'
732 script_name = self._make_test_script(script_dir, mod_name)
733 msg = "can't find '__main__' module in %r" % script_dir
734 self._check_import_error(script_dir, msg)
735
736 def test_directory_error_sets_name_attribute(self):
737 with temp_dir() as script_dir:
738 self._make_test_script(script_dir, 'not_main')
739 try:
740 run_path(script_dir)
741 except ImportError as exc:
742 self.assertEqual(exc.name, '__main__')
743 else:
744 self.fail("Expected ImportError for directory without "
745 "__main__.py")
746
747 def test_zipfile(self):
748 with temp_dir() as script_dir:
749 mod_name = '__main__'
750 script_name = self._make_test_script(script_dir, mod_name)
751 zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name)
752 self._check_script(zip_name, "<run_path>", fname, zip_name,
753 mod_name=mod_name, check_loader=False)
754
755 def test_zipfile_compiled(self):
756 with temp_dir() as script_dir:
757 mod_name = '__main__'
758 script_name = self._make_test_script(script_dir, mod_name)
759 compiled_name = py_compile.compile(script_name, doraise=True)
760 zip_name, fname = make_zip_script(script_dir, 'test_zip',
761 compiled_name)
762 self._check_script(zip_name, "<run_path>", fname, zip_name,
763 mod_name=mod_name, check_loader=False)
764
765 def test_zipfile_error(self):
766 with temp_dir() as script_dir:
767 mod_name = 'not_main'
768 script_name = self._make_test_script(script_dir, mod_name)
769 zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name)
770 msg = "can't find '__main__' module in %r" % zip_name
771 self._check_import_error(zip_name, msg)
772
773 @no_tracing
774 @requires_resource('cpu')
775 def test_main_recursion_error(self):
776 with temp_dir() as script_dir, temp_dir() as dummy_dir:
777 mod_name = '__main__'
778 source = ("import runpy\n"
779 "runpy.run_path(%r)\n") % dummy_dir
780 script_name = self._make_test_script(script_dir, mod_name, source)
781 zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name)
782 with infinite_recursion(25):
783 self.assertRaises(RecursionError, run_path, zip_name)
784
785 def test_encoding(self):
786 with temp_dir() as script_dir:
787 filename = os.path.join(script_dir, 'script.py')
788 with open(filename, 'w', encoding='latin1') as f:
789 f.write("""
790 #coding:latin1
791 s = "non-ASCII: h\xe9"
792 """)
793 result = run_path(filename)
794 self.assertEqual(result['s'], "non-ASCII: h\xe9")
795
796
797 @force_not_colorized_test_class
798 class TestExit(unittest.TestCase):
799 STATUS_CONTROL_C_EXIT = 0xC000013A
800 EXPECTED_CODE = (
801 STATUS_CONTROL_C_EXIT
802 if sys.platform == "win32"
803 else -signal.SIGINT
804 )
805 @staticmethod
806 @contextlib.contextmanager
807 def tmp_path(*args, **kwargs):
808 with temp_dir() as tmp_fn:
809 yield pathlib.Path(tmp_fn)
810
811
812 def run(self, *args, **kwargs):
813 with self.tmp_path() as tmp:
814 self.ham = ham = tmp / "ham.py"
815 ham.write_text(
816 textwrap.dedent(
817 """\
818 raise KeyboardInterrupt
819 """
820 )
821 )
822 super().run(*args, **kwargs)
823
824 @requires_subprocess()
825 def assertSigInt(self, cmd, *args, **kwargs):
826 # Use -E to ignore PYTHONSAFEPATH
827 cmd = [sys.executable, '-E', *cmd]
828 proc = subprocess.run(cmd, *args, **kwargs, text=True, stderr=subprocess.PIPE)
829 self.assertEndsWith(proc.stderr, "\nKeyboardInterrupt\n")
830 self.assertEqual(proc.returncode, self.EXPECTED_CODE)
831
832 def test_pymain_run_file(self):
833 self.assertSigInt([self.ham])
834
835 def test_pymain_run_file_runpy_run_module(self):
836 tmp = self.ham.parent
837 run_module = tmp / "run_module.py"
838 run_module.write_text(
839 textwrap.dedent(
840 """\
841 import runpy
842 runpy.run_module("ham")
843 """
844 )
845 )
846 self.assertSigInt([run_module], cwd=tmp)
847
848 def test_pymain_run_file_runpy_run_module_as_main(self):
849 tmp = self.ham.parent
850 run_module_as_main = tmp / "run_module_as_main.py"
851 run_module_as_main.write_text(
852 textwrap.dedent(
853 """\
854 import runpy
855 runpy._run_module_as_main("ham")
856 """
857 )
858 )
859 self.assertSigInt([run_module_as_main], cwd=tmp)
860
861 def test_pymain_run_command_run_module(self):
862 self.assertSigInt(
863 ["-c", "import runpy; runpy.run_module('ham')"],
864 cwd=self.ham.parent,
865 )
866
867 def test_pymain_run_command(self):
868 self.assertSigInt(["-c", "import ham"], cwd=self.ham.parent)
869
870 def test_pymain_run_stdin(self):
871 self.assertSigInt([], input="import ham", cwd=self.ham.parent)
872
873 def test_pymain_run_module(self):
874 ham = self.ham
875 self.assertSigInt(["-m", ham.stem], cwd=ham.parent)
876
877
878 if __name__ == "__main__":
879 unittest.main()