test_runpy.py 34 KB

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