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

Delta Between Two Patch Sets: Lib/test/test_cmd_line_script.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/runpy.py ('k') | Lib/test/test_runpy.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # tests command line execution of scripts 1 # tests command line execution of scripts
2 2
3 import contextlib 3 import contextlib
4 import importlib 4 import importlib
5 import importlib.machinery 5 import importlib.machinery
6 import zipimport 6 import zipimport
7 import unittest 7 import unittest
8 import sys 8 import sys
9 import os 9 import os
10 import os.path 10 import os.path
11 import py_compile 11 import py_compile
12 import subprocess 12 import subprocess
13 13
14 import textwrap 14 import textwrap
15 from test import support 15 from test import support
16 from test.script_helper import ( 16 from test.support.script_helper import (
17 make_pkg, make_script, make_zip_pkg, make_zip_script, 17 make_pkg, make_script, make_zip_pkg, make_zip_script,
18 assert_python_ok, assert_python_failure, temp_dir, 18 assert_python_ok, assert_python_failure, spawn_python, kill_python)
19 spawn_python, kill_python)
20 19
21 verbose = support.verbose 20 verbose = support.verbose
22 21
23 example_args = ['test1', 'test2', 'test3'] 22 example_args = ['test1', 'test2', 'test3']
24 23
25 test_source = """\ 24 test_source = """\
26 # Script may be run with optimisation enabled, so don't rely on assert 25 # Script may be run with optimisation enabled, so don't rely on assert
27 # statements being executed 26 # statements being executed
28 def assertEqual(lhs, rhs): 27 def assertEqual(lhs, rhs):
29 if lhs != rhs: 28 if lhs != rhs:
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 importlib.invalidate_caches() 106 importlib.invalidate_caches()
108 return to_return 107 return to_return
109 108
110 class CmdLineTest(unittest.TestCase): 109 class CmdLineTest(unittest.TestCase):
111 def _check_output(self, script_name, exit_code, data, 110 def _check_output(self, script_name, exit_code, data,
112 expected_file, expected_argv0, 111 expected_file, expected_argv0,
113 expected_path0, expected_package, 112 expected_path0, expected_package,
114 expected_loader): 113 expected_loader):
115 if verbose > 1: 114 if verbose > 1:
116 print("Output from test script %r:" % script_name) 115 print("Output from test script %r:" % script_name)
117 print(data) 116 print(repr(data))
118 self.assertEqual(exit_code, 0) 117 self.assertEqual(exit_code, 0)
119 printed_loader = '__loader__==%a' % expected_loader 118 printed_loader = '__loader__==%a' % expected_loader
120 printed_file = '__file__==%a' % expected_file 119 printed_file = '__file__==%a' % expected_file
121 printed_package = '__package__==%r' % expected_package 120 printed_package = '__package__==%r' % expected_package
122 printed_argv0 = 'sys.argv[0]==%a' % expected_argv0 121 printed_argv0 = 'sys.argv[0]==%a' % expected_argv0
123 printed_path0 = 'sys.path[0]==%a' % expected_path0 122 printed_path0 = 'sys.path[0]==%a' % expected_path0
124 printed_cwd = 'cwd==%a' % os.getcwd() 123 printed_cwd = 'cwd==%a' % os.getcwd()
125 if verbose > 1: 124 if verbose > 1:
126 print('Expected output:') 125 print('Expected output:')
127 print(printed_file) 126 print(printed_file)
(...skipping 18 matching lines...) Expand all
146 self._check_output(script_name, rc, out + err, expected_file, 145 self._check_output(script_name, rc, out + err, expected_file,
147 expected_argv0, expected_path0, 146 expected_argv0, expected_path0,
148 expected_package, expected_loader) 147 expected_package, expected_loader)
149 148
150 def _check_import_error(self, script_name, expected_msg, 149 def _check_import_error(self, script_name, expected_msg,
151 *cmd_line_switches): 150 *cmd_line_switches):
152 run_args = cmd_line_switches + (script_name,) 151 run_args = cmd_line_switches + (script_name,)
153 rc, out, err = assert_python_failure(*run_args) 152 rc, out, err = assert_python_failure(*run_args)
154 if verbose > 1: 153 if verbose > 1:
155 print('Output from test script %r:' % script_name) 154 print('Output from test script %r:' % script_name)
156 print(err) 155 print(repr(err))
157 print('Expected output: %r' % expected_msg) 156 print('Expected output: %r' % expected_msg)
158 self.assertIn(expected_msg.encode('utf-8'), err) 157 self.assertIn(expected_msg.encode('utf-8'), err)
159 158
160 def test_dash_c_loader(self): 159 def test_dash_c_loader(self):
161 rc, out, err = assert_python_ok("-c", "print(__loader__)") 160 rc, out, err = assert_python_ok("-c", "print(__loader__)")
162 expected = repr(importlib.machinery.BuiltinImporter).encode("utf-8") 161 expected = repr(importlib.machinery.BuiltinImporter).encode("utf-8")
163 self.assertIn(expected, out) 162 self.assertIn(expected, out)
164 163
165 def test_stdin_loader(self): 164 def test_stdin_loader(self):
166 # Unfortunately, there's no way to automatically test the fully 165 # Unfortunately, there's no way to automatically test the fully
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 def test_repl_stdout_flush_separate_stderr(self): 215 def test_repl_stdout_flush_separate_stderr(self):
217 self.check_repl_stdout_flush(True) 216 self.check_repl_stdout_flush(True)
218 217
219 def test_repl_stderr_flush(self): 218 def test_repl_stderr_flush(self):
220 self.check_repl_stderr_flush() 219 self.check_repl_stderr_flush()
221 220
222 def test_repl_stderr_flush_separate_stderr(self): 221 def test_repl_stderr_flush_separate_stderr(self):
223 self.check_repl_stderr_flush(True) 222 self.check_repl_stderr_flush(True)
224 223
225 def test_basic_script(self): 224 def test_basic_script(self):
226 with temp_dir() as script_dir: 225 with support.temp_dir() as script_dir:
227 script_name = _make_test_script(script_dir, 'script') 226 script_name = _make_test_script(script_dir, 'script')
228 self._check_script(script_name, script_name, script_name, 227 self._check_script(script_name, script_name, script_name,
229 script_dir, None, 228 script_dir, None,
230 importlib.machinery.SourceFileLoader) 229 importlib.machinery.SourceFileLoader)
231 230
232 def test_script_compiled(self): 231 def test_script_compiled(self):
233 with temp_dir() as script_dir: 232 with support.temp_dir() as script_dir:
234 script_name = _make_test_script(script_dir, 'script') 233 script_name = _make_test_script(script_dir, 'script')
235 py_compile.compile(script_name, doraise=True) 234 py_compile.compile(script_name, doraise=True)
236 os.remove(script_name) 235 os.remove(script_name)
237 pyc_file = support.make_legacy_pyc(script_name) 236 pyc_file = support.make_legacy_pyc(script_name)
238 self._check_script(pyc_file, pyc_file, 237 self._check_script(pyc_file, pyc_file,
239 pyc_file, script_dir, None, 238 pyc_file, script_dir, None,
240 importlib.machinery.SourcelessFileLoader) 239 importlib.machinery.SourcelessFileLoader)
241 240
242 def test_directory(self): 241 def test_directory(self):
243 with temp_dir() as script_dir: 242 with support.temp_dir() as script_dir:
244 script_name = _make_test_script(script_dir, '__main__') 243 script_name = _make_test_script(script_dir, '__main__')
245 self._check_script(script_dir, script_name, script_dir, 244 self._check_script(script_dir, script_name, script_dir,
246 script_dir, '', 245 script_dir, '',
247 importlib.machinery.SourceFileLoader) 246 importlib.machinery.SourceFileLoader)
248 247
249 def test_directory_compiled(self): 248 def test_directory_compiled(self):
250 with temp_dir() as script_dir: 249 with support.temp_dir() as script_dir:
251 script_name = _make_test_script(script_dir, '__main__') 250 script_name = _make_test_script(script_dir, '__main__')
252 py_compile.compile(script_name, doraise=True) 251 py_compile.compile(script_name, doraise=True)
253 os.remove(script_name) 252 os.remove(script_name)
254 pyc_file = support.make_legacy_pyc(script_name) 253 pyc_file = support.make_legacy_pyc(script_name)
255 self._check_script(script_dir, pyc_file, script_dir, 254 self._check_script(script_dir, pyc_file, script_dir,
256 script_dir, '', 255 script_dir, '',
257 importlib.machinery.SourcelessFileLoader) 256 importlib.machinery.SourcelessFileLoader)
258 257
259 def test_directory_error(self): 258 def test_directory_error(self):
260 with temp_dir() as script_dir: 259 with support.temp_dir() as script_dir:
261 msg = "can't find '__main__' module in %r" % script_dir 260 msg = "can't find '__main__' module in %r" % script_dir
262 self._check_import_error(script_dir, msg) 261 self._check_import_error(script_dir, msg)
263 262
264 def test_zipfile(self): 263 def test_zipfile(self):
265 with temp_dir() as script_dir: 264 with support.temp_dir() as script_dir:
266 script_name = _make_test_script(script_dir, '__main__') 265 script_name = _make_test_script(script_dir, '__main__')
267 zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_ name) 266 zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_ name)
268 self._check_script(zip_name, run_name, zip_name, zip_name, '', 267 self._check_script(zip_name, run_name, zip_name, zip_name, '',
269 zipimport.zipimporter) 268 zipimport.zipimporter)
270 269
271 def test_zipfile_compiled(self): 270 def test_zipfile_compiled(self):
272 with temp_dir() as script_dir: 271 with support.temp_dir() as script_dir:
273 script_name = _make_test_script(script_dir, '__main__') 272 script_name = _make_test_script(script_dir, '__main__')
274 compiled_name = py_compile.compile(script_name, doraise=True) 273 compiled_name = py_compile.compile(script_name, doraise=True)
275 zip_name, run_name = make_zip_script(script_dir, 'test_zip', compile d_name) 274 zip_name, run_name = make_zip_script(script_dir, 'test_zip', compile d_name)
276 self._check_script(zip_name, run_name, zip_name, zip_name, '', 275 self._check_script(zip_name, run_name, zip_name, zip_name, '',
277 zipimport.zipimporter) 276 zipimport.zipimporter)
278 277
279 def test_zipfile_error(self): 278 def test_zipfile_error(self):
280 with temp_dir() as script_dir: 279 with support.temp_dir() as script_dir:
281 script_name = _make_test_script(script_dir, 'not_main') 280 script_name = _make_test_script(script_dir, 'not_main')
282 zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_ name) 281 zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_ name)
283 msg = "can't find '__main__' module in %r" % zip_name 282 msg = "can't find '__main__' module in %r" % zip_name
284 self._check_import_error(zip_name, msg) 283 self._check_import_error(zip_name, msg)
285 284
286 def test_module_in_package(self): 285 def test_module_in_package(self):
287 with temp_dir() as script_dir: 286 with support.temp_dir() as script_dir:
288 pkg_dir = os.path.join(script_dir, 'test_pkg') 287 pkg_dir = os.path.join(script_dir, 'test_pkg')
289 make_pkg(pkg_dir) 288 make_pkg(pkg_dir)
290 script_name = _make_test_script(pkg_dir, 'script') 289 script_name = _make_test_script(pkg_dir, 'script')
291 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.sc ript') 290 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.sc ript')
292 self._check_script(launch_name, script_name, script_name, 291 self._check_script(launch_name, script_name, script_name,
293 script_dir, 'test_pkg', 292 script_dir, 'test_pkg',
294 importlib.machinery.SourceFileLoader) 293 importlib.machinery.SourceFileLoader)
295 294
296 def test_module_in_package_in_zipfile(self): 295 def test_module_in_package_in_zipfile(self):
297 with temp_dir() as script_dir: 296 with support.temp_dir() as script_dir:
298 zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'tes t_pkg', 'script') 297 zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'tes t_pkg', 'script')
299 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.sc ript', zip_name) 298 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.sc ript', zip_name)
300 self._check_script(launch_name, run_name, run_name, 299 self._check_script(launch_name, run_name, run_name,
301 zip_name, 'test_pkg', zipimport.zipimporter) 300 zip_name, 'test_pkg', zipimport.zipimporter)
302 301
303 def test_module_in_subpackage_in_zipfile(self): 302 def test_module_in_subpackage_in_zipfile(self):
304 with temp_dir() as script_dir: 303 with support.temp_dir() as script_dir:
305 zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'tes t_pkg', 'script', depth=2) 304 zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'tes t_pkg', 'script', depth=2)
306 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.te st_pkg.script', zip_name) 305 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.te st_pkg.script', zip_name)
307 self._check_script(launch_name, run_name, run_name, 306 self._check_script(launch_name, run_name, run_name,
308 zip_name, 'test_pkg.test_pkg', 307 zip_name, 'test_pkg.test_pkg',
309 zipimport.zipimporter) 308 zipimport.zipimporter)
310 309
311 def test_package(self): 310 def test_package(self):
312 with temp_dir() as script_dir: 311 with support.temp_dir() as script_dir:
313 pkg_dir = os.path.join(script_dir, 'test_pkg') 312 pkg_dir = os.path.join(script_dir, 'test_pkg')
314 make_pkg(pkg_dir) 313 make_pkg(pkg_dir)
315 script_name = _make_test_script(pkg_dir, '__main__') 314 script_name = _make_test_script(pkg_dir, '__main__')
316 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') 315 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
317 self._check_script(launch_name, script_name, 316 self._check_script(launch_name, script_name,
318 script_name, script_dir, 'test_pkg', 317 script_name, script_dir, 'test_pkg',
319 importlib.machinery.SourceFileLoader) 318 importlib.machinery.SourceFileLoader)
320 319
321 def test_package_compiled(self): 320 def test_package_compiled(self):
322 with temp_dir() as script_dir: 321 with support.temp_dir() as script_dir:
323 pkg_dir = os.path.join(script_dir, 'test_pkg') 322 pkg_dir = os.path.join(script_dir, 'test_pkg')
324 make_pkg(pkg_dir) 323 make_pkg(pkg_dir)
325 script_name = _make_test_script(pkg_dir, '__main__') 324 script_name = _make_test_script(pkg_dir, '__main__')
326 compiled_name = py_compile.compile(script_name, doraise=True) 325 compiled_name = py_compile.compile(script_name, doraise=True)
327 os.remove(script_name) 326 os.remove(script_name)
328 pyc_file = support.make_legacy_pyc(script_name) 327 pyc_file = support.make_legacy_pyc(script_name)
329 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') 328 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
330 self._check_script(launch_name, pyc_file, 329 self._check_script(launch_name, pyc_file,
331 pyc_file, script_dir, 'test_pkg', 330 pyc_file, script_dir, 'test_pkg',
332 importlib.machinery.SourcelessFileLoader) 331 importlib.machinery.SourcelessFileLoader)
333 332
334 def test_package_error(self): 333 def test_package_error(self):
335 with temp_dir() as script_dir: 334 with support.temp_dir() as script_dir:
336 pkg_dir = os.path.join(script_dir, 'test_pkg') 335 pkg_dir = os.path.join(script_dir, 'test_pkg')
337 make_pkg(pkg_dir) 336 make_pkg(pkg_dir)
338 msg = ("'test_pkg' is a package and cannot " 337 msg = ("'test_pkg' is a package and cannot "
339 "be directly executed") 338 "be directly executed")
340 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') 339 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
341 self._check_import_error(launch_name, msg) 340 self._check_import_error(launch_name, msg)
342 341
343 def test_package_recursion(self): 342 def test_package_recursion(self):
344 with temp_dir() as script_dir: 343 with support.temp_dir() as script_dir:
345 pkg_dir = os.path.join(script_dir, 'test_pkg') 344 pkg_dir = os.path.join(script_dir, 'test_pkg')
346 make_pkg(pkg_dir) 345 make_pkg(pkg_dir)
347 main_dir = os.path.join(pkg_dir, '__main__') 346 main_dir = os.path.join(pkg_dir, '__main__')
348 make_pkg(main_dir) 347 make_pkg(main_dir)
349 msg = ("Cannot use package as __main__ module; " 348 msg = ("Cannot use package as __main__ module; "
350 "'test_pkg' is a package and cannot " 349 "'test_pkg' is a package and cannot "
351 "be directly executed") 350 "be directly executed")
352 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') 351 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
353 self._check_import_error(launch_name, msg) 352 self._check_import_error(launch_name, msg)
354 353
355 def test_issue8202(self): 354 def test_issue8202(self):
356 # Make sure package __init__ modules see "-m" in sys.argv0 while 355 # Make sure package __init__ modules see "-m" in sys.argv0 while
357 # searching for the module to execute 356 # searching for the module to execute
358 with temp_dir() as script_dir: 357 with support.temp_dir() as script_dir:
359 with support.change_cwd(path=script_dir): 358 with support.change_cwd(path=script_dir):
360 pkg_dir = os.path.join(script_dir, 'test_pkg') 359 pkg_dir = os.path.join(script_dir, 'test_pkg')
361 make_pkg(pkg_dir, "import sys; print('init_argv0==%r' % sys.argv [0])") 360 make_pkg(pkg_dir, "import sys; print('init_argv0==%r' % sys.argv [0])")
362 script_name = _make_test_script(pkg_dir, 'script') 361 script_name = _make_test_script(pkg_dir, 'script')
363 rc, out, err = assert_python_ok('-m', 'test_pkg.script', *exampl e_args, __isolated=False) 362 rc, out, err = assert_python_ok('-m', 'test_pkg.script', *exampl e_args, __isolated=False)
364 if verbose > 1: 363 if verbose > 1:
365 print(out) 364 print(repr(out))
366 expected = "init_argv0==%r" % '-m' 365 expected = "init_argv0==%r" % '-m'
367 self.assertIn(expected.encode('utf-8'), out) 366 self.assertIn(expected.encode('utf-8'), out)
368 self._check_output(script_name, rc, out, 367 self._check_output(script_name, rc, out,
369 script_name, script_name, '', 'test_pkg', 368 script_name, script_name, '', 'test_pkg',
370 importlib.machinery.SourceFileLoader) 369 importlib.machinery.SourceFileLoader)
371 370
372 def test_issue8202_dash_c_file_ignored(self): 371 def test_issue8202_dash_c_file_ignored(self):
373 # Make sure a "-c" file in the current directory 372 # Make sure a "-c" file in the current directory
374 # does not alter the value of sys.path[0] 373 # does not alter the value of sys.path[0]
375 with temp_dir() as script_dir: 374 with support.temp_dir() as script_dir:
376 with support.change_cwd(path=script_dir): 375 with support.change_cwd(path=script_dir):
377 with open("-c", "w") as f: 376 with open("-c", "w") as f:
378 f.write("data") 377 f.write("data")
379 rc, out, err = assert_python_ok('-c', 378 rc, out, err = assert_python_ok('-c',
380 'import sys; print("sys.path[0]==%r" % sys.path[0])', 379 'import sys; print("sys.path[0]==%r" % sys.path[0])',
381 __isolated=False) 380 __isolated=False)
382 if verbose > 1: 381 if verbose > 1:
383 print(out) 382 print(repr(out))
384 expected = "sys.path[0]==%r" % '' 383 expected = "sys.path[0]==%r" % ''
385 self.assertIn(expected.encode('utf-8'), out) 384 self.assertIn(expected.encode('utf-8'), out)
386 385
387 def test_issue8202_dash_m_file_ignored(self): 386 def test_issue8202_dash_m_file_ignored(self):
388 # Make sure a "-m" file in the current directory 387 # Make sure a "-m" file in the current directory
389 # does not alter the value of sys.path[0] 388 # does not alter the value of sys.path[0]
390 with temp_dir() as script_dir: 389 with support.temp_dir() as script_dir:
391 script_name = _make_test_script(script_dir, 'other') 390 script_name = _make_test_script(script_dir, 'other')
392 with support.change_cwd(path=script_dir): 391 with support.change_cwd(path=script_dir):
393 with open("-m", "w") as f: 392 with open("-m", "w") as f:
394 f.write("data") 393 f.write("data")
395 rc, out, err = assert_python_ok('-m', 'other', *example_args , 394 rc, out, err = assert_python_ok('-m', 'other', *example_args ,
396 __isolated=False) 395 __isolated=False)
397 self._check_output(script_name, rc, out, 396 self._check_output(script_name, rc, out,
398 script_name, script_name, '', '', 397 script_name, script_name, '', '',
399 importlib.machinery.SourceFileLoader) 398 importlib.machinery.SourceFileLoader)
400 399
401 @contextlib.contextmanager 400 @contextlib.contextmanager
402 def setup_test_pkg(self, *args): 401 def setup_test_pkg(self, *args):
403 with temp_dir() as script_dir, \ 402 with support.temp_dir() as script_dir, \
404 support.change_cwd(path=script_dir): 403 support.change_cwd(path=script_dir):
405 pkg_dir = os.path.join(script_dir, 'test_pkg') 404 pkg_dir = os.path.join(script_dir, 'test_pkg')
406 make_pkg(pkg_dir, *args) 405 make_pkg(pkg_dir, *args)
407 yield pkg_dir 406 yield pkg_dir
408 407
409 def check_dash_m_failure(self, *args): 408 def check_dash_m_failure(self, *args):
410 rc, out, err = assert_python_failure('-m', *args, __isolated=False) 409 rc, out, err = assert_python_failure('-m', *args, __isolated=False)
411 if verbose > 1: 410 if verbose > 1:
412 print(out) 411 print(repr(out))
413 self.assertEqual(rc, 1) 412 self.assertEqual(rc, 1)
414 return err 413 return err
415 414
416 def test_dash_m_error_code_is_one(self): 415 def test_dash_m_error_code_is_one(self):
417 # If a module is invoked with the -m command line flag 416 # If a module is invoked with the -m command line flag
418 # and results in an error that the return code to the 417 # and results in an error that the return code to the
419 # shell is '1' 418 # shell is '1'
420 with self.setup_test_pkg() as pkg_dir: 419 with self.setup_test_pkg() as pkg_dir:
421 script_name = _make_test_script(pkg_dir, 'other', 420 script_name = _make_test_script(pkg_dir, 'other',
422 "if __name__ == '__main__': raise Va lueError") 421 "if __name__ == '__main__': raise Va lueError")
423 err = self.check_dash_m_failure('test_pkg.other', *example_args) 422 err = self.check_dash_m_failure('test_pkg.other', *example_args)
424 self.assertIn(b'ValueError', err) 423 self.assertIn(b'ValueError', err)
425 424
426 def test_dash_m_errors(self): 425 def test_dash_m_errors(self):
427 # Exercise error reporting for various invalid package executions 426 # Exercise error reporting for various invalid package executions
428 tests = ( 427 tests = (
429 ('builtins', br'No code object available'), 428 ('builtins', br'No code object available'),
430 ('builtins.x', br'Error while finding spec.*AttributeError'), 429 ('builtins.x', br'Error while finding spec.*AttributeError'),
431 ('builtins.x.y', br'Error while finding spec.*' 430 ('builtins.x.y', br'No module named.*not a package'),
432 br'ImportError.*No module named.*not a package'), 431 ('os.path', br'loader.*cannot handle'),
433 ('os.path', br'loader cannot handle'),
434 ('importlib', br'No module named.*' 432 ('importlib', br'No module named.*'
435 br'is a package and cannot be directly executed'), 433 br'is a package and cannot be directly executed'),
436 ('importlib.nonexistant', br'No module named'), 434 ('importlib.nonexistant', br'No module named'),
435 ('.unittest', br'Relative module names not supported'),
437 ) 436 )
438 for name, regex in tests: 437 for name, regex in tests:
439 with self.subTest(name): 438 with self.subTest(name):
440 rc, _, err = assert_python_failure('-m', name) 439 rc, _, err = assert_python_failure('-m', name)
441 self.assertEqual(rc, 1) 440 self.assertEqual(rc, 1)
442 self.assertRegex(err, regex) 441 self.assertRegex(err, regex)
443 self.assertNotIn(b'Traceback', err) 442 self.assertNotIn(b'Traceback', err)
444 443
445 def test_dash_m_init_traceback(self): 444 def test_dash_m_init_traceback(self):
446 # These were wrapped in an ImportError and tracebacks were 445 # These were wrapped in an ImportError and tracebacks were
(...skipping 21 matching lines...) Expand all
468 467
469 def test_pep_409_verbiage(self): 468 def test_pep_409_verbiage(self):
470 # Make sure PEP 409 syntax properly suppresses 469 # Make sure PEP 409 syntax properly suppresses
471 # the context of an exception 470 # the context of an exception
472 script = textwrap.dedent("""\ 471 script = textwrap.dedent("""\
473 try: 472 try:
474 raise ValueError 473 raise ValueError
475 except: 474 except:
476 raise NameError from None 475 raise NameError from None
477 """) 476 """)
478 with temp_dir() as script_dir: 477 with support.temp_dir() as script_dir:
479 script_name = _make_test_script(script_dir, 'script', script) 478 script_name = _make_test_script(script_dir, 'script', script)
480 exitcode, stdout, stderr = assert_python_failure(script_name) 479 exitcode, stdout, stderr = assert_python_failure(script_name)
481 text = stderr.decode('ascii').split('\n') 480 text = stderr.decode('ascii').split('\n')
482 self.assertEqual(len(text), 4) 481 self.assertEqual(len(text), 4)
483 self.assertTrue(text[0].startswith('Traceback')) 482 self.assertTrue(text[0].startswith('Traceback'))
484 self.assertTrue(text[1].startswith(' File ')) 483 self.assertTrue(text[1].startswith(' File '))
485 self.assertTrue(text[3].startswith('NameError')) 484 self.assertTrue(text[3].startswith('NameError'))
486 485
487 def test_non_ascii(self): 486 def test_non_ascii(self):
488 # Mac OS X denies the creation of a file with an invalid UTF-8 name. 487 # Mac OS X denies the creation of a file with an invalid UTF-8 name.
(...skipping 23 matching lines...) Expand all
512 import sys 511 import sys
513 error = None 512 error = None
514 try: 513 try:
515 raise ValueError('some text') 514 raise ValueError('some text')
516 except ValueError as err: 515 except ValueError as err:
517 error = err 516 error = err
518 517
519 if error: 518 if error:
520 sys.exit(error) 519 sys.exit(error)
521 """) 520 """)
522 with temp_dir() as script_dir: 521 with support.temp_dir() as script_dir:
523 script_name = _make_test_script(script_dir, 'script', script) 522 script_name = _make_test_script(script_dir, 'script', script)
524 exitcode, stdout, stderr = assert_python_failure(script_name) 523 exitcode, stdout, stderr = assert_python_failure(script_name)
525 text = stderr.decode('ascii') 524 text = stderr.decode('ascii')
526 self.assertEqual(text, "some text") 525 self.assertEqual(text, "some text")
527 526
528 527
529 def test_main(): 528 def test_main():
530 support.run_unittest(CmdLineTest) 529 support.run_unittest(CmdLineTest)
531 support.reap_children() 530 support.reap_children()
532 531
533 if __name__ == '__main__': 532 if __name__ == '__main__':
534 test_main() 533 test_main()
LEFTRIGHT

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