def test_activate_by_import(tmp_path): code = '''import traceback_with_variables.activate_by_import def f(n): return n / 0 f(10)''' assert_smart_equals_ref('test_override.activate_by_import', run_code(tmp_path=tmp_path, code=code, raises=True))
def test_activate_in_ipython_by_import_error(tmp_path): code = '''import traceback_with_variables.activate_in_ipython_by_import''' (tmp_path / 'IPython.py').write_text('import nonexistent_module') assert_smart_equals_ref( 'test_global_hooks.activate_in_ipython_by_import_error' '', run_code(tmp_path, [], code, [], True))
def test_activate_by_import(tmp_path): code = '''import traceback_with_variables.activate_by_import def f(n): return n / 0 f(10)''' assert_smart_equals_ref('test_global_hooks.activate_by_import', run_code(tmp_path, [], code, [], True))
def test_deactivate_by_env_var(tmp_path): code = '''from traceback_with_variables import override_print_tb def f(n): return n / 0 override_print_tb(activate_by_env_var='NONEXISTENT') f(10)''' assert_smart_equals_ref('test_override.deactivate_by_env_var', run_code(tmp_path=tmp_path, code=code, raises=True))
def test_prints_tb_noncall(tmp_path): code = '''from traceback_with_variables import prints_tb @prints_tb def f(n): return n / 0 try: f(10) except: pass''' assert_smart_equals_ref('test_print.prints_tb_noncall', run_code(tmp_path=tmp_path, code=code, raises=False))
def _test_code(name: str, tmp_path, main_argv: List[str], code: str, code_argv: List[str], raises: bool): assert_smart_equals_ref( f'test_main.{name}', run_code(tmp_path=tmp_path, python_argv=['-m', 'traceback_with_variables.main'] + main_argv, code=code, code_argv=code_argv, raises=raises).replace('[script-arg [script-arg ...]]', '[script-arg ...]') # python 3.9+ )
def test_printing_exc_stderr(tmp_path): code = '''from traceback_with_variables import printing_exc def f(n): return n / 0 def main(): with printing_exc(reraise=False): return f(10) main()''' assert_smart_equals_ref('test_print.printing_exc_stderr', run_code(tmp_path, [], code, [], False))
def test_printing_tb_stderr(tmp_path): code = '''from traceback_with_variables import printing_tb def f(n): return n / 0 def main(): try: with printing_tb(): return f(10) except: pass main()''' assert_smart_equals_ref('test_print.printing_tb_stderr', run_code(tmp_path=tmp_path, code=code, raises=False))
def _test_activate_in_ipython_by_import(tmp_path, name, win32: bool): if (not win32) and sys.platform == 'win32': return code = '''from traceback_with_variables import activate_in_ipython_by_import def f(n): return n / 0 f(10)''' out = run_code(tmp_path, ['-m', 'IPython'], code, [], True) out = '\n'.join(out.split('\n')[:-1] + ['']) if win32: out = rm_ansi(out) assert_smart_equals_ref(name, re.sub(r'^.*(variables\.activate)', r'\1', out, flags=re.S))
def _test_code(name: str, tmp_path, main_argv: List[str], code: str, code_argv: List[str], raises: bool): assert_smart_equals_ref( f'test_main.{name}', re.sub( r']\s+', ']\n', re.sub( r'\[([^-][^\s]+) \[[^\s]+ ...]]', r'[\1 ...]', run_code( # for python3.9 tmp_path=tmp_path, python_argv=['-m', 'traceback_with_variables.main'] + main_argv, code=code, code_argv=code_argv, raises=raises))))