def __init__(self, locals=None, use_rich=False): # noqa """This class builds upon Python's code.InteractiveConsole so as to provide friendly tracebacks. It keeps track of code fragment executed by treating each of them as an individual source file. """ _ = current_lang.translate friendly_traceback.exclude_file_from_traceback(codeop.__file__) self.fake_filename = "<friendly-console:%d>" self.counter = 1 self.old_locals = {} self.saved_builtins = {} for name in dir(builtins): self.saved_builtins[name] = getattr(builtins, name) self.rich_console = False if theme.rich_available and use_rich: try: self.rich_console = theme.init_rich_console() friendly_traceback.set_formatter("rich") except Exception: print(_("\n Installed version of Rich is too old.\n\n")) elif use_rich: print(_("\n Rich is not installed.\n\n")) super().__init__(locals=locals) self.check_for_builtins_changes() self.check_for_annotations()
def run(self, event=None): # Since we run this from a Tkinter function, any sys.excepthook # will be ignored - hence, we need to catch the tracebacks locally. try: # If you comment out the following line, the traceback generated # might include information from runpy - something like this # could confuse beginners when they try with their own code as # they would have no idea where runpy was used. friendly_traceback.exclude_file_from_traceback(runpy.__file__) runpy.run_path(self.filename, run_name="__main__") except Exception: friendly_traceback.explain() finally: friendly_traceback.include_file_in_traceback(runpy.__file__)
def test_cleaned_traceback(): """Assert this test filename does not appear in tracebacks if we exclude it. """ friendly.install(redirect="capture") friendly.exclude_file_from_traceback(__file__) try: raise ValueError except ValueError: friendly.explain() output = friendly.get_output() assert "test_clean_traceback" not in output # cleanup for other tests friendly.include_file_in_traceback(__file__) friendly.uninstall()
def collect_dialects(self): """Find known dialects and create corresponding dictionaries.""" dialects = glob.glob(os.path.dirname(__file__) + "/dialects/*.py") for f in dialects: if os.path.isfile(f) and not f.endswith("__init__.py"): dialect = os.path.basename(f).split(".")[0] friendly_traceback.exclude_file_from_traceback(runpy.__file__) _module = runpy.run_path(f) from_py = _module[dialect] self.dict_from_py[dialect] = from_py to_py = {} for k, v in from_py.items(): if isinstance(v, str): to_py[v] = k else: for val in v: to_py[val] = k self.dict_to_py[dialect] = to_py
def install_in_idle_shell(): """Installs Friendly-traceback in IDLE's shell so that it can retrieve code entered in IDLE's repl. Note that this requires Python version 3.10+ since IDLE did not support custom excepthook in previous versions of Python. Furthermore, Friendly-traceback is bypassed when code entered in IDLE's repl raises SyntaxErrors. """ import friendly_traceback friendly_traceback.exclude_file_from_traceback(idlelib_run.__file__) rpchandler = rpc.objecttable["exec"].rpchandler # noqa def get_lines(filename, linenumber): lines = rpchandler.remotecall("linecache", "getlines", (filename, None), {}) new_lines = [] for line in lines: if not line.endswith("\n"): line += "\n" if filename.startswith("<pyshell#") and line.startswith("\t"): # Remove extra indentation added in the shell (\t == 8 spaces) line = " " + line[1:] new_lines.append(line) if linenumber is None: return new_lines return new_lines[linenumber - 1:linenumber] source_cache.idle_get_lines = get_lines friendly_traceback.install(include="friendly_tb", redirect=idle_writer) # Current limitation idle_writer(" WARNING\n", "ERROR") # noqa idle_writer( "Friendly-traceback cannot handle SyntaxErrors for code entered in the shell.\n" )
try: from IPython.core import interactiveshell as shell from IPython.core import compilerop except ImportError: raise ValueError("IPython cannot be imported.") from friendly_traceback import ( install, exclude_file_from_traceback, explain_traceback, set_formatter, ) from friendly_traceback.console_helpers import * # noqa from friendly_traceback.console_helpers import helpers # noqa __all__ = list(helpers.keys()) __all__.append("set_formatter") shell.InteractiveShell.showtraceback = lambda self, *args, **kwargs: explain_traceback( ) shell.InteractiveShell.showsyntaxerror = ( lambda self, *args, **kwargs: explain_traceback()) exclude_file_from_traceback(shell.__file__) exclude_file_from_traceback(compilerop.__file__) install(include="friendly_tb") set_formatter("repl") print("Friendly-traceback installed.")
import _gui from demo_gettext import demo_lang # this demo includes its own translations import friendly_traceback # set up the translations for this demo demo_lang.install() # Note: there is no point to do friendly_traceback.install() in this program # since exceptions would be intercepted by Tkinter; see the run() method below # However, we may wish to ensure that this file does not show in # the traceback results, so that it looks like tracebacks running from # Python instead of from this program. friendly_traceback.exclude_file_from_traceback(__file__) class App(_gui.App): def __init__(self, master=None): super().__init__(master=master) friendly_traceback.set_formatter(formatter=self.formatter) def change_lang(self, event=None): """Keeping all the translations in sync""" lang = self.lang.get() friendly_traceback.set_lang(lang) demo_lang.install(lang) self.set_ui_lang() def set_ui_lang(self):