Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(33680)

Delta Between Two Patch Sets: Lib/test/test_runpy.py

Issue 14285: Traceback wrong on ImportError while executing a package
Left Patch Set: Created 3 years, 9 months ago
Right Patch Set: Created 3 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_cmd_line_script.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # Test the runpy module 1 # Test the runpy module
2 import unittest 2 import unittest
3 import os 3 import os
4 import os.path 4 import os.path
5 import sys 5 import sys
6 import re 6 import re
7 import tempfile 7 import tempfile
8 import importlib, importlib.machinery, importlib.util 8 import importlib, importlib.machinery, importlib.util
9 import py_compile 9 import py_compile
10 from test.support import ( 10 from test.support import (
11 forget, make_legacy_pyc, run_unittest, unload, verbose, no_tracing, 11 forget, make_legacy_pyc, unload, verbose, no_tracing,
12 create_empty_file) 12 create_empty_file, temp_dir)
13 from test.script_helper import ( 13 from test.support.script_helper import (
14 make_pkg, make_script, make_zip_pkg, make_zip_script, temp_dir) 14 make_pkg, make_script, make_zip_pkg, make_zip_script)
15 15
16 16
17 import runpy 17 import runpy
18 from runpy import _run_code, _run_module_code, run_module, run_path 18 from runpy import _run_code, _run_module_code, run_module, run_path
19 # Note: This module can't safely test _run_module_as_main as it 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 20 # runs its tests in the current process, which would mess with the
21 # real __main__ module (usually test.regrtest) 21 # real __main__ module (usually test.regrtest)
22 # See test_cmd_line_script for a test that executes that code path 22 # See test_cmd_line_script for a test that executes that code path
23 23
24 24
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 else: 190 else:
191 self.fail("Expected import error for " + mod_name) 191 self.fail("Expected import error for " + mod_name)
192 192
193 def test_invalid_names(self): 193 def test_invalid_names(self):
194 # Builtin module 194 # Builtin module
195 self.expect_import_error("sys") 195 self.expect_import_error("sys")
196 # Non-existent modules 196 # Non-existent modules
197 self.expect_import_error("sys.imp.eric") 197 self.expect_import_error("sys.imp.eric")
198 self.expect_import_error("os.path.half") 198 self.expect_import_error("os.path.half")
199 self.expect_import_error("a.bee") 199 self.expect_import_error("a.bee")
200 # Relative names not allowed
200 self.expect_import_error(".howard") 201 self.expect_import_error(".howard")
201 self.expect_import_error("..eaten") 202 self.expect_import_error("..eaten")
203 self.expect_import_error(".test_runpy")
204 self.expect_import_error(".unittest")
202 # Package without __main__.py 205 # Package without __main__.py
203 self.expect_import_error("multiprocessing") 206 self.expect_import_error("multiprocessing")
204 207
205 def test_library_module(self): 208 def test_library_module(self):
206 self.assertEqual(run_module("runpy")["__name__"], "runpy") 209 self.assertEqual(run_module("runpy")["__name__"], "runpy")
207 210
208 def _add_pkg_dir(self, pkg_dir, namespace=False): 211 def _add_pkg_dir(self, pkg_dir, namespace=False):
209 os.mkdir(pkg_dir) 212 os.mkdir(pkg_dir)
210 if namespace: 213 if namespace:
211 return None 214 return None
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 os.rmdir(fullname) 265 os.rmdir(fullname)
263 except OSError as ex: 266 except OSError as ex:
264 if verbose > 1: print(ex) # Persist with cleaning up 267 if verbose > 1: print(ex) # Persist with cleaning up
265 try: 268 try:
266 os.rmdir(top) 269 os.rmdir(top)
267 if verbose > 1: print(" Removed package tree") 270 if verbose > 1: print(" Removed package tree")
268 except OSError as ex: 271 except OSError as ex:
269 if verbose > 1: print(ex) # Persist with cleaning up 272 if verbose > 1: print(ex) # Persist with cleaning up
270 273
271 def _fix_ns_for_legacy_pyc(self, ns, alter_sys): 274 def _fix_ns_for_legacy_pyc(self, ns, alter_sys):
272 char_to_add = "c" if __debug__ else "o" 275 char_to_add = "c"
273 ns["__file__"] += char_to_add 276 ns["__file__"] += char_to_add
274 ns["__cached__"] = ns["__file__"] 277 ns["__cached__"] = ns["__file__"]
275 spec = ns["__spec__"] 278 spec = ns["__spec__"]
276 new_spec = importlib.util.spec_from_file_location(spec.name, 279 new_spec = importlib.util.spec_from_file_location(spec.name,
277 ns["__file__"]) 280 ns["__file__"])
278 ns["__spec__"] = new_spec 281 ns["__spec__"] = new_spec
279 if alter_sys: 282 if alter_sys:
280 ns["run_argv0"] += char_to_add 283 ns["run_argv0"] += char_to_add
281 284
282 285
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
453 with self.subTest(name): 456 with self.subTest(name):
454 source = "raise {0}('{0} in __init__.py.')".format(name) 457 source = "raise {0}('{0} in __init__.py.')".format(name)
455 with open(init, "wt", encoding="ascii") as mod_file: 458 with open(init, "wt", encoding="ascii") as mod_file:
456 mod_file.write(source) 459 mod_file.write(source)
457 try: 460 try:
458 run_module(mod_name) 461 run_module(mod_name)
459 except exception as err: 462 except exception as err:
460 self.assertNotIn("finding spec", format(err)) 463 self.assertNotIn("finding spec", format(err))
461 else: 464 else:
462 self.fail("Nothing raised; expected {}".format(name)) 465 self.fail("Nothing raised; expected {}".format(name))
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))
463 472
464 def test_run_package_in_namespace_package(self): 473 def test_run_package_in_namespace_package(self):
465 for depth in range(1, 4): 474 for depth in range(1, 4):
466 if verbose > 1: print("Testing package depth:", depth) 475 if verbose > 1: print("Testing package depth:", depth)
467 self._check_package(depth, parent_namespaces=True) 476 self._check_package(depth, parent_namespaces=True)
468 477
469 def test_run_namespace_package(self): 478 def test_run_namespace_package(self):
470 for depth in range(1, 4): 479 for depth in range(1, 4):
471 if verbose > 1: print("Testing package depth:", depth) 480 if verbose > 1: print("Testing package depth:", depth)
472 self._check_package(depth, namespace=True) 481 self._check_package(depth, namespace=True)
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
688 697
689 @no_tracing 698 @no_tracing
690 def test_main_recursion_error(self): 699 def test_main_recursion_error(self):
691 with temp_dir() as script_dir, temp_dir() as dummy_dir: 700 with temp_dir() as script_dir, temp_dir() as dummy_dir:
692 mod_name = '__main__' 701 mod_name = '__main__'
693 source = ("import runpy\n" 702 source = ("import runpy\n"
694 "runpy.run_path(%r)\n") % dummy_dir 703 "runpy.run_path(%r)\n") % dummy_dir
695 script_name = self._make_test_script(script_dir, mod_name, source) 704 script_name = self._make_test_script(script_dir, mod_name, source)
696 zip_name, fname = make_zip_script(script_dir, 'test_zip', script_nam e) 705 zip_name, fname = make_zip_script(script_dir, 'test_zip', script_nam e)
697 msg = "recursion depth exceeded" 706 msg = "recursion depth exceeded"
698 self.assertRaisesRegex(RuntimeError, msg, run_path, zip_name) 707 self.assertRaisesRegex(RecursionError, msg, run_path, zip_name)
699 708
700 def test_encoding(self): 709 def test_encoding(self):
701 with temp_dir() as script_dir: 710 with temp_dir() as script_dir:
702 filename = os.path.join(script_dir, 'script.py') 711 filename = os.path.join(script_dir, 'script.py')
703 with open(filename, 'w', encoding='latin1') as f: 712 with open(filename, 'w', encoding='latin1') as f:
704 f.write(""" 713 f.write("""
705 #coding:latin1 714 #coding:latin1
706 s = "non-ASCII: h\xe9" 715 s = "non-ASCII: h\xe9"
707 """) 716 """)
708 result = run_path(filename) 717 result = run_path(filename)
709 self.assertEqual(result['s'], "non-ASCII: h\xe9") 718 self.assertEqual(result['s'], "non-ASCII: h\xe9")
710 719
711 720
712 if __name__ == "__main__": 721 if __name__ == "__main__":
713 unittest.main() 722 unittest.main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+