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_logger_as_file_lines(caplog): with printing_exc(reraise=False, file_=LoggerAsFile(logging.getLogger('test-logger'), separate_lines=True)): f(10) assert_smart_equals_ref('test_print.logger_as_file_lines', caplog.text)
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_printing_exc_skip_cur_frame(): out = StringIO() with printing_exc(reraise=False, file_=out, skip_cur_frame=True): f(10) assert_smart_equals_ref('test_print.printing_exc_skip_cur_frame', out.getvalue())
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_explicit_e(): try: f(10000) except Exception as e_: # noqa e = e_ assert_smart_equals_ref(f'test_core.explicit_e', core.format_exc(e=e)) # noqa
def _test_cmd(name: str, tmp_path, argv: List[str], raises: bool): assert_smart_equals_ref( f'test_main.{name}', run_py(tmp_path=tmp_path, argv=['-m', 'traceback_with_variables.main'] + argv, raises=raises).replace('[script-arg [script-arg ...]]', '[script-arg ...]') # python 3.9+ )
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_printing_exc_to_tty(): if sys.platform == 'win32': return out = StringIO() out.isatty = lambda: True with printing_exc(reraise=False, file_=out): f(10) assert_smart_equals_ref('test_print.printing_exc_to_tty', out.getvalue())
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_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_cmd(name: str, tmp_path, 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_py( # for python3.9 tmp_path=tmp_path, argv=['-m', 'traceback_with_variables.main'] + argv, raises=raises))))
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_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_logger_as_file(caplog): with printing_tb(reraise=False, file_=LoggerAsFile(logging.getLogger('test-logger'))): f(10) assert_smart_equals_ref('test_print.logger_as_file', caplog.text)
def test_printing_tb(): out = StringIO() with printing_tb(reraise=False, file_=out, flush=True): f(10) assert_smart_equals_ref('test_print.printing_tb', out.getvalue())
def check(name, arg, **kwargs): try: f(arg) except Exception: # noqa assert_smart_equals_ref('test_core.{}'.format(name), core.format_exc(**kwargs))
def test_print_cur_tb(): out = StringIO() get_cur_tb(out) assert_smart_equals_ref('test_print.print_cur_tb', out.getvalue())
def test_cur_tb(num_skipped_frames): assert_smart_equals_ref(f'test_core.cur_tb_{num_skipped_frames}', get_cur_tb(num_skipped_frames))
def test_printing_exc(): out = StringIO() with printing_exc(reraise=False, file_=out): f(10) assert_smart_equals_ref('test_print.printing_exc', out.getvalue())
def check(name, arg, **kwargs): try: f(arg) except Exception as e: # noqa assert_smart_equals_ref('test_core.{}'.format(name), '\n'.join(core.iter_tb_lines(e, **kwargs)))
def test_prints_exc(): out = StringIO() with pytest.raises(ZeroDivisionError): prints_exc(file_=out)(f)(10) assert_smart_equals_ref('test_print.prints_exc', out.getvalue())