From 4bb5766ecb4c20049c5025db0eea52973a39a1b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Lozier?= Date: Mon, 17 Feb 2020 07:54:47 -0500 Subject: [PATCH] Enable test_traceback --- .../Cases/IronPythonCasesManifest.ini | 3 +- Tests/test_traceback.py | 861 +++++++++--------- 2 files changed, 432 insertions(+), 432 deletions(-) diff --git a/Src/IronPythonTest/Cases/IronPythonCasesManifest.ini b/Src/IronPythonTest/Cases/IronPythonCasesManifest.ini index 16623c012..b1622e512 100644 --- a/Src/IronPythonTest/Cases/IronPythonCasesManifest.ini +++ b/Src/IronPythonTest/Cases/IronPythonCasesManifest.ini @@ -198,8 +198,7 @@ Reason=disabled due to https://github.com/IronLanguages/ironpython2/issues/182 Ignore=true [IronPython.test_traceback] -Ignore=true -Reason=TODO: Needs lots of work +IsolationLevel=PROCESS [IronPython.hosting.editor_svcs.test_errorlistener] Ignore=true diff --git a/Tests/test_traceback.py b/Tests/test_traceback.py index e8abe5949..9e1d2a51b 100644 --- a/Tests/test_traceback.py +++ b/Tests/test_traceback.py @@ -2,509 +2,510 @@ # The .NET Foundation licenses this file to you under the Apache 2.0 License. # See the LICENSE file in the project root for more information. -# !!! DO NOT MOVE OR CHANGE THE FOLLOWING LINES +import os +import shutil +import sys +import tempfile +import unittest + +from iptest import is_cli, is_posix, run_test + +FILE = __file__ + +def getlineno(fn): + return fn.__code__.co_firstlineno + def _raise_exception(): raise Exception() -_retb = (18, 0, 'test_traceback.py', '_raise_exception') +_retb = (getlineno(_raise_exception) + 1, 0, FILE, '_raise_exception') -from iptest.assert_util import * -from iptest.file_util import * -if not is_cli: import os def _raise_exception_with_finally(): try: raise Exception() finally: pass -_rewftb= (27, 0, 'test_traceback.py', '_raise_exception_with_finally') - -def assert_traceback(expected): - import sys - tb = sys.exc_info()[2] - - if expected is None: - AreEqual(None, expected) - else: - tb_list = [] - while tb is not None : - f = tb.tb_frame - co = f.f_code - filename = co.co_filename.lower() - name = co.co_name - tb_list.append((tb.tb_lineno, tb.tb_lasti, filename, name)) - tb = tb.tb_next - - #print tb_list - - AreEqual(len(tb_list), len(expected)) - - for x in range(len(expected)): - AreEqual(tb_list[x][0], expected[x][0]) - AreEqual(tb_list[x][2:], expected[x][2:]) - - -def test_no_traceback(): - #assert_traceback(None) - try: - _raise_exception() - except: - pass - assert_traceback(None) - -FILE="test_traceback.py" - -LINE100 = 70 - -def test_catch_others_exception(): - try: - _raise_exception() - except: - assert_traceback([(LINE100 + 2, 0, FILE, 'test_catch_others_exception'), _retb]) - -LINE110 = 78 +_rewftb = (getlineno(_raise_exception_with_finally) + 2, 0, FILE, '_raise_exception_with_finally') -def test_catch_its_own_exception(): - try: - raise Exception() - except: - assert_traceback([(LINE110 + 2, 0, FILE, 'test_catch_its_own_exception')]) +class TracebackTest(unittest.TestCase): + def assert_traceback(self, expected): + tb = sys.exc_info()[2] -LINE120 = 86 + if expected is None: + self.assertEqual(None, expected) + else: + tb_list = [] + while tb is not None : + f = tb.tb_frame + co = f.f_code + filename = co.co_filename + name = co.co_name + tb_list.append((tb.tb_lineno, tb.tb_lasti, filename, name)) + tb = tb.tb_next -def test_catch_others_exception_with_finally(): - try: - _raise_exception_with_finally() - except: - assert_traceback([(LINE120 + 2, 0, FILE, 'test_catch_others_exception_with_finally'), _rewftb]) + self.assertEqual(len(tb_list), len(expected)) -LINE130 = 94 + for x in range(len(expected)): + self.assertEqual(tb_list[x][0], expected[x][0]) + self.assertEqual(tb_list[x][2:], expected[x][2:]) -def test_nested_caught_outside(): - try: - x = 2 + def test_no_traceback(self): try: _raise_exception() - except NameError: - Assert(False, "unhittable") - y = 2 - except: - assert_traceback([(LINE130 + 4, 0, FILE, 'test_nested_caught_outside'), _retb]) - -LINE140 = 108 + except: + pass + self.assert_traceback(None) -def test_nested_caught_inside(): - try: - x = 2 + def test_catch_others_exception(self): + lineno = getlineno(lambda _: None) try: _raise_exception() except: - assert_traceback([(LINE140 + 4, 0, FILE, 'test_nested_caught_inside'), _retb]) - y = 2 - except: - assert_traceback(None) + self.assert_traceback([(lineno + 2, 0, FILE, 'test_catch_others_exception'), _retb]) -LINE150 = 120 + def test_catch_its_own_exception(self): + lineno = getlineno(lambda _: None) + try: + raise Exception() + except: + self.assert_traceback([(lineno + 2, 0, FILE, 'test_catch_its_own_exception')]) -def test_throw_in_except(): - try: - _raise_exception() - except: - assert_traceback([(LINE150+2, 0, FILE, 'test_throw_in_except'), _retb]) + def test_catch_others_exception_with_finally(self): + lineno = getlineno(lambda _: None) try: - assert_traceback([(LINE150+2, 0, FILE, 'test_throw_in_except'), _retb]) - _raise_exception() + _raise_exception_with_finally() + except: + self.assert_traceback([(lineno + 2, 0, FILE, 'test_catch_others_exception_with_finally'), _rewftb]) + + def test_nested_caught_outside(self): + lineno = getlineno(lambda _: None) + try: + x = 2 + try: + _raise_exception() + except NameError: + self.fail("unhittable") + y = 2 except: - assert_traceback([(LINE150+7, 0, FILE, 'test_throw_in_except'), _retb]) - assert_traceback([(LINE150+7, 0, FILE, 'test_throw_in_except'), _retb]) + self.assert_traceback([(lineno + 4, 0, FILE, 'test_nested_caught_outside'), _retb]) -LINE160 = 134 + def test_nested_caught_inside(self): + lineno = getlineno(lambda _: None) + try: + x = 2 + try: + _raise_exception() + except: + self.assert_traceback([(lineno + 3, 0, FILE, 'test_nested_caught_inside'), _retb]) + y = 2 + except: + self.assert_traceback(None) -class C1: - def M(self): + def test_throw_in_except(self): + lineno = getlineno(lambda _: None) try: _raise_exception() except: - assert_traceback([(LINE160 + 3, 0, FILE, 'M'), _retb]) + self.assert_traceback([(lineno + 2, 0, FILE, 'test_throw_in_except'), _retb]) + try: + self.assert_traceback([(lineno + 2, 0, FILE, 'test_throw_in_except'), _retb]) + _raise_exception() + except: + self.assert_traceback([(lineno + 7, 0, FILE, 'test_throw_in_except'), _retb]) + self.assert_traceback(None) + + def test_throw_in_method(self): + lineno = getlineno(lambda _: None) + + class C1: + def M(self2): + try: + _raise_exception() + except: + self.assert_traceback([(lineno + 5, 0, FILE, 'M'), _retb]) + + c = C1() + c.M() -def test_throw_in_method(): - c = C1() - c.M() + def test_throw_when_defining_class(self): + lineno = getlineno(lambda _: None) -LINE170 = 147 + class C2(object): + try: + _raise_exception() + except: + self.assert_traceback([(lineno + 4, 0, FILE, 'C2'), _retb]) + + def test_throw_when_defining_class_directly(self): + lineno = getlineno(lambda _: None) + + def throw_when_defining_class_directly(): + class C1: + def M(self2): + try: + _raise_exception() + except: + self.assert_traceback([(lineno + 5, 0, FILE, 'M'), _retb]) + + class C3(C1): + _raise_exception() -def test_throw_when_defining_class(): - class C2(object): try: - _raise_exception() + throw_when_defining_class_directly() except: - assert_traceback([(LINE170 + 3, 0, FILE, 'C2'), _retb]) + self.assert_traceback([(lineno + 14, 0, FILE, 'test_throw_when_defining_class_directly'), + (lineno + 10, 0, FILE, 'throw_when_defining_class_directly'), + (lineno + 11, 0, FILE, 'C3'), _retb]) -def throw_when_defining_class_directly(): - class C3(C1): - _raise_exception() + def test_compiled_code(self): + lineno = getlineno(lambda _: None) + try: + codeobj = compile('\nraise Exception()', '', 'exec') + exec(codeobj, {}) + except: + self.assert_traceback([(lineno + 3, 0, FILE, 'test_compiled_code'), (2, 0, '', '')]) -LINE180 = 160 + def test_throw_before_yield(self): + lineno = getlineno(lambda _: None) -def test_throw_when_defining_class_directly(): - try: - throw_when_defining_class_directly() - except: - assert_traceback([(LINE180 + 2, 0, FILE, 'test_throw_when_defining_class_directly'), - (LINE180 - 5, 0, FILE, 'throw_when_defining_class_directly'), - (LINE180 - 4, 0, FILE, 'C3'), _retb]) -LINE200 = 169 - -def test_compiled_code(): - try: - codeobj = compile('\nraise Exception()', '', 'exec') - exec(codeobj, {}) - except: - assert_traceback([(LINE200+3, 0, FILE, 'test_compiled_code'), (2, 0, '', '')]) - -def generator_throw_before_yield(): - _raise_exception() - yield 1 - -LINE210 = 181 - -def test_throw_before_yield(): - try: - for x in generator_throw_before_yield(): - pass - except: - assert_traceback([(LINE210+3, 0, FILE, 'test_throw_before_yield'), (LINE210-4, 2, 'test_traceback.py', 'generator_throw_before_yield'), _retb]) + def generator_throw_before_yield(): + _raise_exception() + yield 1 -def generator_throw_after_yield(): - yield 1 - _raise_exception() + try: + for x in generator_throw_before_yield(): + pass + except: + self.assert_traceback([(lineno + 7, 0, FILE, 'test_throw_before_yield'), (lineno + 3, 2, FILE, 'generator_throw_before_yield'), _retb]) -LINE220 = 194 + def test_throw_while_yield(self): + lineno = getlineno(lambda _: None) -def test_throw_while_yield(): - try: - for x in generator_throw_while_yield(): - pass - except: - assert_traceback([(LINE220+3, 0, FILE, 'test_throw_while_yield')]) + def generator_throw_after_yield(): + yield 1 + _raise_exception() -def generator_yield_inside_try(): - try: - yield 1 - yield 2 - _raise_exception() - except NameError: - pass + try: + for x in generator_throw_while_yield(): + pass + except: + self.assert_traceback([(lineno + 7, 0, FILE, 'test_throw_while_yield')]) -LINE230 = 211 + def test_yield_inside_try(self): + lineno = getlineno(lambda _: None) -def test_yield_inside_try(): - try: - for x in generator_yield_inside_try(): - pass - except: - assert_traceback([(LINE230+3, 0, FILE, 'test_yield_inside_try'), (LINE230-5, 2, 'test_traceback.py', 'generator_yield_inside_try'), _retb]) + def generator_yield_inside_try(): + try: + yield 1 + yield 2 + _raise_exception() + except NameError: + pass -LINE240 = 221 + try: + for x in generator_yield_inside_try(): + pass + except: + self.assert_traceback([(lineno + 11, 0, FILE, 'test_yield_inside_try'), (lineno + 6, 2, FILE, 'generator_yield_inside_try'), _retb]) -def test_throw_and_throw(): - try: - _raise_exception() - except: - assert_traceback([(LINE240 + 2, 0, FILE, 'test_throw_and_throw'), _retb]) - try: - _raise_exception() - except: - assert_traceback([(LINE240 + 6, 0, FILE, 'test_throw_and_throw'), _retb]) -LINE250 = 233 - -def test_throw_in_another_file(): - if is_cli: _f_file = path_combine(get_full_dir_name(testpath.public_testdir), 'foo.py') - else: _f_file = os.getcwd() + '\\foo.py' - write_to_file(_f_file, ''' + def test_throw_and_throw(self): + lineno = getlineno(lambda _: None) + try: + _raise_exception() + except: + self.assert_traceback([(lineno + 2, 0, FILE, 'test_throw_and_throw'), _retb]) + try: + _raise_exception() + except: + self.assert_traceback([(lineno + 6, 0, FILE, 'test_throw_and_throw'), _retb]) + + def test_throw_in_another_file(self): + lineno = getlineno(lambda _: None) + _f_file = os.path.join(os.getcwd(), 'foo.py') + with open(_f_file, "w") as f: + f.write(''' def another_raise(): raise Exception() '''); - try: - import foo - foo.another_raise() - except: - assert_traceback([(LINE250 + 8, 0, FILE, 'test_throw_in_another_file'), (3, 0, _f_file.lower(), 'another_raise')]) - finally: - os.remove(_f_file) - -class MyException(Exception): pass + try: + import foo + foo.another_raise() + except: + self.assert_traceback([(lineno + 9, 0, FILE, 'test_throw_in_another_file'), (3, 0, _f_file, 'another_raise')]) + finally: + os.remove(_f_file) -Line260 = 250 -def catch_MyException(): - try: - _raise_exception() - except MyException: - assert_traceback([]) # UNREACABLE. THIS TRICK SIMPLIFIES THE CHECK + def test_catch_MyException(self): + lineno = getlineno(lambda _: None) + class MyException(Exception): pass -def test_catch_MyException(): - try: - catch_MyException() - except: - assert_traceback([(Line260+8, 0, FILE, 'test_catch_MyException'), (Line260+2, 0, FILE, 'catch_MyException'), _retb]) + def catch_MyException(): + try: + _raise_exception() + except MyException: + self.assert_traceback([]) # UNREACHABLE. THIS TRICK SIMPLIFIES THE CHECK -Line263 = 263 + try: + catch_MyException() + except: + self.assert_traceback([(lineno + 10, 0, FILE, 'test_catch_MyException'), (lineno + 5, 0, FILE, 'catch_MyException'), _retb]) -def test_cp11923_first(): - try: - _t_test = path_combine(testpath.public_testdir, "cp11923.py") - write_to_file(_t_test, """def f(): + def test_cp11923_first(self): + line_num = getlineno(lambda _: None) + try: + _t_test = os.path.join(os.getcwd(), "cp11923.py") + with open(_t_test, "w") as f: + f.write(""" +def f(): x = 'something bad' raise Exception(x)""") - - import cp11923 - for i in xrange(3): + + import cp11923 + for i in range(3): + try: + cp11923.f() + except: + self.assert_traceback([(line_num + 12, 69, FILE, 'test_cp11923_first'), (4, 22, _t_test, 'f')]) + import importlib + importlib.reload(cp11923) + + finally: + os.remove(_t_test) + + def test_reraise(self): + line_num = getlineno(lambda _: None) + def g(): + f() + + def f(): try: - cp11923.f() + raise Exception except: - assert_traceback([(Line263 + 11, 69, 'test_traceback.py', 'test_cp11923_first'), (3, 22, get_full_dir_name(_t_test).lower(), 'f')]) - reload(cp11923) - - finally: - import os - os.unlink(_t_test) - -############################################################################### -##TESTS BEYOND THIS POINT SHOULD NOT DEPEND ON LINE NUMBERS IN THIS FILE####### -############################################################################### - -def test_cp11923_second(): - import os - import sys - old_path = [x for x in sys.path] - sys.path.append(os.getcwd()) - - try: - #Test setup - _t_test = os.path.join(testpath.public_testdir, "cp11116_main.py") - write_to_file(_t_test, """import cp11116_a -try: - cp11116_a.a() -except: - pass - -cp11116_a.a() -""") - - _t_test_a = os.path.join(testpath.public_testdir, "cp11116_a.py") - write_to_file(_t_test_a, """def a(): - raise None -""") - - #Actual test - t_out, t_in, t_err = os.popen3(sys.executable + " " + os.path.join(os.getcwd(), "cp11116_main.py")) - lines = t_err.readlines() - t_err.close() - t_out.close() - t_in.close() - - #Verification - Assert("cp11116_main.py\", line 7, in" in lines[1], lines[1]) - line_num = 3 - if is_cli: - line_num -= 1 - Assert(lines[line_num].rstrip().endswith("cp11116_a.py\", line 2, in a"), lines[line_num]) - - finally: - sys.path = old_path - os.unlink(_t_test) - os.unlink(_t_test_a) + raise -Line331 = 332 -def test_reraise(): - def g(): - f() - - def f(): try: - raise Exception + g() except: - raise + self.assert_traceback([(line_num + 11, 0, FILE, 'test_reraise'), (line_num + 2, 0, FILE, 'g'), (line_num + 6, 0, FILE, 'f')]) - try: - g() - except: - assert_traceback([(Line331+9, 0, 'test_traceback.py', 'test_reraise'), (Line331, 0, 'test_traceback.py', 'g'), (Line331+4, 0, 'test_traceback.py', 'f')]) + def test_reraise_finally(self): + line_num = getlineno(lambda _: None) + def g(): + f() + def f(): + try: + raise Exception + finally: + raise -def test_reraise_finally(): - def g(): - f() - - def f(): try: + g() + except: + self.assert_traceback([(line_num + 11, 30, FILE, 'test_reraise_finally'), (line_num + 2, 3, FILE, 'g'), (line_num + 6,13, FILE, 'f')]) + + def test_xafter_finally_raise(self): + line_num = getlineno(lambda _: None) + def g(): raise Exception - finally: - raise - try: - g() - except: - assert_traceback([(Line331+25, 30, 'test_traceback.py', 'test_reraise_finally'), (Line331+16, 3, 'test_traceback.py', 'g'), (Line331+22,13, 'test_traceback.py', 'f')]) - -Line361 = 361 -def test_xafter_finally_raise(): - def g(): - raise Exception - - def nop(): pass - - def f(): - try: - nop() - finally: - nop() - - try: - g() - except Exception, e: - assert_traceback([(Line361+14, 30, 'test_traceback.py', 'f'), (Line361+3, 3, 'test_traceback.py', 'g')]) + def nop(): pass - f() + def f(): + try: + nop() + finally: + nop() + + try: + g() + except Exception as e: + self.assert_traceback([(line_num + 13, 30, FILE, 'f'), (line_num + 2, 3, FILE, 'g')]) -Line381 = 381 -def test_uncaught_exception_thru_try(): - def baz(): - raise StopIteration - - def f(): - try: - baz() - except TypeError: - pass - try: f() - except: - assert_traceback([(Line381+11, 30, 'test_traceback.py', 'test_uncaught_exception_thru_try'), (Line381+7, 3, 'test_traceback.py', 'f'), (Line381+3, 3, 'test_traceback.py', 'baz')]) - - -Line397=397 -def test_with_traceback(): - from thread import allocate_lock - def f(): - g() - - def g(): - h() - - def h(): - raise Exception('hello!!') - - try: - with allocate_lock(): - f() - except: - assert_traceback([(Line397+14, 30, 'test_traceback.py', 'test_with_traceback'), - (Line397+4, 3, 'test_traceback.py', 'f'), - (Line397+7, 3, 'test_traceback.py', 'g'), - (Line397+10, 3, 'test_traceback.py', 'h')]) - - -Line419=419 -def test_xraise_again(): - def f(): - g() - - def g(): - h() - - def h(): - raise Exception('hello!!') - - try: + + def test_uncaught_exception_thru_try(self): + line_num = getlineno(lambda _: None) + def baz(): + raise StopIteration + + def f(): + try: + baz() + except TypeError: + pass try: f() - except Exception, e: - raise e - except: - assert_traceback([(Line419+15, 30, 'test_traceback.py', 'test_xraise_again'), ]) - -Line438=438 -def test_with_traceback_enter_throws(): - class ctx_mgr(object): - def __enter__(*args): - raise Exception('hello') - def __exit__(*args): - pass - - def h(): - raise Exception('hello!!') - - - try: - with ctx_mgr(): - h() - except: - assert_traceback([(Line438+13, 30, 'test_traceback.py', 'test_with_traceback_enter_throws'), - (Line438+4, 3, 'test_traceback.py', '__enter__')]) - -Line457=457 -def test_with_traceback_exit_throws(): - class ctx_mgr(object): - def __enter__(*args): - pass - def __exit__(*args): - raise Exception('hello') - - def h(): - raise Exception('hello!!') - - try: - with ctx_mgr(): + except: + self.assert_traceback([(line_num + 10, 30, FILE, 'test_uncaught_exception_thru_try'), (line_num + 6, 3, FILE, 'f'), (line_num + 2, 3, FILE, 'baz')]) + + def test_with_traceback(self): + line_num=getlineno(lambda _: None) + from _thread import allocate_lock + def f(): + g() + + def g(): h() - except: - assert_traceback([(Line457+13, 30, 'test_traceback.py', 'test_with_traceback_exit_throws'), - (Line457+6, 3, 'test_traceback.py', '__exit__')]) - -Line475=475 -def test_with_traceback_ctor_throws(): - class ctx_mgr(object): - def __init__(self): - raise Exception('hello') - def __enter__(*args): - pass - def __exit__(*args): - pass - - def h(): - raise Exception('hello!!') - - try: - with ctx_mgr(): + + def h(): + raise Exception('hello!!') + + try: + with allocate_lock(): + f() + except: + self.assert_traceback([(line_num + 13, 30, FILE, 'test_with_traceback'), + (line_num + 3, 3, FILE, 'f'), + (line_num + 6, 3, FILE, 'g'), + (line_num + 9, 3, FILE, 'h')]) + + @unittest.expectedFailure # https://github.com/IronLanguages/ironpython3/issues/738 + def test_xraise_again(self): + line_num=getlineno(lambda _: None) + def f(): + g() + + def g(): h() - except: - assert_traceback([(Line475+14, 30, 'test_traceback.py', 'test_with_traceback_ctor_throws'), - (Line475+4, 3, 'test_traceback.py', '__init__')]) + def h(): + raise Exception('hello!!') + + try: + try: + f() + except Exception as e: + raise e + except: + self.assert_traceback([(line_num + 14, 30, FILE, 'test_xraise_again'), + (line_num + 12, 30, FILE, 'test_xraise_again'), + (line_num + 2, 3, FILE, 'f'), + (line_num + 5, 3, FILE, 'g'), + (line_num + 8, 3, FILE, 'h')]) + + def test_with_traceback_enter_throws(self): + line_num=getlineno(lambda _: None) + class ctx_mgr(object): + def __enter__(*args): + raise Exception('hello') + def __exit__(*args): + pass + + def h(): + raise Exception('hello!!') + + + try: + with ctx_mgr(): + h() + except: + self.assert_traceback([(line_num + 12, 30, FILE, 'test_with_traceback_enter_throws'), + (line_num + 3, 3, FILE, '__enter__')]) + + def test_with_traceback_exit_throws(self): + line_num=getlineno(lambda _: None) + class ctx_mgr(object): + def __enter__(*args): + pass + def __exit__(*args): + raise Exception('hello') + + def h(): + raise Exception('hello!!') + + try: + with ctx_mgr(): + h() + except: + self.assert_traceback([(line_num + 12, 30, FILE, 'test_with_traceback_exit_throws'), + (line_num + 5, 3, FILE, '__exit__')]) + + def test_with_traceback_ctor_throws(self): + line_num=getlineno(lambda _: None) + class ctx_mgr(object): + def __init__(self): + raise Exception('hello') + def __enter__(*args): + pass + def __exit__(*args): + pass + + def h(): + raise Exception('hello!!') + + try: + with ctx_mgr(): + h() + except: + self.assert_traceback([(line_num + 13, 30, FILE, 'test_with_traceback_ctor_throws'), + (line_num + 3, 3, FILE, '__init__')]) + + def test_with_mixed_stack(self): + """tests a stack which is mixed w/ interpreted and non-interpreted frames + because f() has a loop in it""" + line_num=getlineno(lambda _: None) + def a(): + with xxx() as abc: + f() + + def f(): + for z in (): + pass + + 1/0 + + class xxx(object): + def __enter__(*args): pass + def __exit__(*args): pass + + try: + a() + except: + self.assert_traceback([(line_num + 16, 30, FILE, 'test_with_mixed_stack'), + (line_num + 3, 3, FILE, 'a'), + (line_num + 9, 3, FILE, 'f')]) + + @unittest.skipIf(is_posix, "https://github.com/IronLanguages/ironpython3/issues/541") + def test_cp11923_second(self): + try: + #Test setup + _t_test = os.path.join(os.getcwd(), "cp11116_main.py") + with open(_t_test, "w") as f: + f.write("""import cp11116_a +try: + cp11116_a.a() +except: + pass + +cp11116_a.a() +""") + + _t_test_a = os.path.join(os.getcwd(), "cp11116_a.py") + with open(_t_test_a, "w") as f: + f.write("""def a(): + raise None +""") + + #Actual test + import subprocess + p = subprocess.Popen([sys.executable, os.path.join(os.getcwd(), "cp11116_main.py")], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + t_out, t_in, t_err = (p.stdin, p.stdout, p.stderr) + lines = t_err.readlines() + t_err.close() + t_out.close() + t_in.close() + + #Verification + self.assertIn(b"cp11116_main.py\", line 7, in", lines[1]) + line_num = 3 + if is_cli: + line_num -= 1 + self.assertTrue(lines[line_num].rstrip().endswith(b"cp11116_a.py\", line 2, in a")) + + finally: + os.remove(_t_test) + os.remove(_t_test_a) -Line496=496 -def test_with_mixed_stack(): - """tests a stack which is mixed w/ interpreted and non-interpreted frames -because f() has a loop in it""" - def a(): - with xxx() as abc: - f() - - def f(): - for z in (): - pass - - 1/0 - - class xxx(object): - def __enter__(*args): pass - def __exit__(*args): pass - - try: - a() - except: - assert_traceback([(Line496+19, 30, 'test_traceback.py', 'test_with_mixed_stack'), - (Line496+6, 3, 'test_traceback.py', 'a'), - (Line496+12, 3, 'test_traceback.py', 'f')]) - run_test(__name__)