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

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

Issue 14285: Traceback wrong on ImportError while executing a package
Left Patch Set: Created 4 years, 6 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 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 def test_run_module_in_namespace_package(self): 435 def test_run_module_in_namespace_package(self):
433 for depth in range(1, 4): 436 for depth in range(1, 4):
434 if verbose > 1: print("Testing package depth:", depth) 437 if verbose > 1: print("Testing package depth:", depth)
435 self._check_module(depth, namespace=True, parent_namespaces=True) 438 self._check_module(depth, namespace=True, parent_namespaces=True)
436 439
437 def test_run_package(self): 440 def test_run_package(self):
438 for depth in range(1, 4): 441 for depth in range(1, 4):
439 if verbose > 1: print("Testing package depth:", depth) 442 if verbose > 1: print("Testing package depth:", depth)
440 self._check_package(depth) 443 self._check_package(depth)
441 444
442 @unittest.expectedFailure
443 def test_run_package_init_exceptions(self): 445 def test_run_package_init_exceptions(self):
444 # These are currently wrapped in an ImportError; see Issue 14285 446 # These were previously wrapped in an ImportError; see Issue 14285
445 result = self._make_pkg("", 1, "__main__") 447 result = self._make_pkg("", 1, "__main__")
446 pkg_dir, _, mod_name, _ = result 448 pkg_dir, _, mod_name, _ = result
447 mod_name = mod_name.replace(".__main__", "") 449 mod_name = mod_name.replace(".__main__", "")
448 self.addCleanup(self._del_pkg, pkg_dir, 1, mod_name) 450 self.addCleanup(self._del_pkg, pkg_dir, 1, mod_name)
449 init = os.path.join(pkg_dir, "__runpy_pkg__", "__init__.py") 451 init = os.path.join(pkg_dir, "__runpy_pkg__", "__init__.py")
450 452
451 exceptions = (ImportError, AttributeError, TypeError, ValueError) 453 exceptions = (ImportError, AttributeError, TypeError, ValueError)
452 for exception in exceptions: 454 for exception in exceptions:
453 name = exception.__name__ 455 name = exception.__name__
454 with self.subTest(name): 456 with self.subTest(name):
455 source = "raise {0}('{0} in __init__.py.')".format(name) 457 source = "raise {0}('{0} in __init__.py.')".format(name)
456 with open(init, "wt", encoding="ascii") as mod_file: 458 with open(init, "wt", encoding="ascii") as mod_file:
457 mod_file.write(source) 459 mod_file.write(source)
458 try: 460 try:
459 run_module(mod_name) 461 run_module(mod_name)
460 except exception as err: 462 except exception as err:
461 self.assertNotIn("Error while finding spec", format(err)) 463 self.assertNotIn("finding spec", format(err))
464 else:
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))
462 else: 470 else:
463 self.fail("Nothing raised; expected {}".format(name)) 471 self.fail("Nothing raised; expected {}".format(name))
464 472
465 def test_run_package_in_namespace_package(self): 473 def test_run_package_in_namespace_package(self):
466 for depth in range(1, 4): 474 for depth in range(1, 4):
467 if verbose > 1: print("Testing package depth:", depth) 475 if verbose > 1: print("Testing package depth:", depth)
468 self._check_package(depth, parent_namespaces=True) 476 self._check_package(depth, parent_namespaces=True)
469 477
470 def test_run_namespace_package(self): 478 def test_run_namespace_package(self):
471 for depth in range(1, 4): 479 for depth in range(1, 4):
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 697
690 @no_tracing 698 @no_tracing
691 def test_main_recursion_error(self): 699 def test_main_recursion_error(self):
692 with temp_dir() as script_dir, temp_dir() as dummy_dir: 700 with temp_dir() as script_dir, temp_dir() as dummy_dir:
693 mod_name = '__main__' 701 mod_name = '__main__'
694 source = ("import runpy\n" 702 source = ("import runpy\n"
695 "runpy.run_path(%r)\n") % dummy_dir 703 "runpy.run_path(%r)\n") % dummy_dir
696 script_name = self._make_test_script(script_dir, mod_name, source) 704 script_name = self._make_test_script(script_dir, mod_name, source)
697 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)
698 msg = "recursion depth exceeded" 706 msg = "recursion depth exceeded"
699 self.assertRaisesRegex(RuntimeError, msg, run_path, zip_name) 707 self.assertRaisesRegex(RecursionError, msg, run_path, zip_name)
700 708
701 def test_encoding(self): 709 def test_encoding(self):
702 with temp_dir() as script_dir: 710 with temp_dir() as script_dir:
703 filename = os.path.join(script_dir, 'script.py') 711 filename = os.path.join(script_dir, 'script.py')
704 with open(filename, 'w', encoding='latin1') as f: 712 with open(filename, 'w', encoding='latin1') as f:
705 f.write(""" 713 f.write("""
706 #coding:latin1 714 #coding:latin1
707 s = "non-ASCII: h\xe9" 715 s = "non-ASCII: h\xe9"
708 """) 716 """)
709 result = run_path(filename) 717 result = run_path(filename)
710 self.assertEqual(result['s'], "non-ASCII: h\xe9") 718 self.assertEqual(result['s'], "non-ASCII: h\xe9")
711 719
712 720
713 if __name__ == "__main__": 721 if __name__ == "__main__":
714 unittest.main() 722 unittest.main()
LEFTRIGHT

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