-
Notifications
You must be signed in to change notification settings - Fork 27
Improve user expeirence #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,38 @@ | ||
| from traceback import print_exception | ||
| from pathlib import Path | ||
| from contextvars import ContextVar | ||
|
|
||
| cur_file_var = ContextVar("cur_file_var",default=None) | ||
|
|
||
| class Pos: | ||
| file: Path | ||
| line: int | ||
|
|
||
| def __init__(self, line: int, file: Path | None = None): | ||
| self.line = line | ||
| self.file = file or cur_file_var.get() | ||
|
|
||
| class CompileError(Exception): | ||
| def __init__(self, text): | ||
| super(Exception, self).__init__(text) | ||
| where: None | Pos | ||
| def __init__(self, text, where = None): | ||
| super(Exception, self).__init__(text) | ||
| self.where = where | ||
|
|
||
| line_cache = {} | ||
|
|
||
| def register_lines(filename: str, lines: list[str]): | ||
| line_cache[filename] = lines | ||
|
|
||
| def pretty_print_error(ex: Exception, file = None): | ||
| stack = [] | ||
| while (ex.__cause__ is not None): | ||
| stack.append(ex) | ||
| ex = ex.__cause__ | ||
| stack.append(ex) | ||
| for e in stack[::-1]: | ||
| if isinstance(e, CompileError): | ||
| print(e,file=file) | ||
| if e.where is not None and e.where.file in line_cache: | ||
| print(f"{e.where.line:>4} |",line_cache[e.where.file][e.where.line-1],file=file) | ||
| continue | ||
| print_exception(e,chain=False, file = file) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| from CompileError import CompileError | ||
| from CompileError import CompileError, Pos | ||
| import traceback | ||
|
|
||
| class block_base(object): | ||
| def __init__(self, line): | ||
| self.line = line | ||
| def compile(self, func): | ||
| raise NotImplementedError('Section does not implement compile()') | ||
|
|
||
|
|
@@ -12,11 +14,9 @@ def compile_lines(self, func, lines): | |
| try: | ||
| func.compile_blocks(lines) | ||
| except CompileError as e: | ||
| print(e) | ||
| raise CompileError(f'Unable to compile {self.block_name} at line {self.line}') | ||
| except e: | ||
| print(traceback.format_exc()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we getting rid of printing tracebacks? Having tracebacks are invaluable for debugging.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct, sorry , I was thinking about the end user experience however , which suffers from them. |
||
| raise CompileError(f'Error compiling {self.block_name} at line {self.line}') | ||
| raise CompileError(f'Unable to compile {self.block_name} at line {self.line}', Pos(self.line)) from e | ||
| except Exception as e: | ||
| raise CompileError(f'Error compiling {self.block_name} at line {self.line}', Pos(self.line)) from e | ||
|
|
||
| @property | ||
| def block_name(self): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use @dataclasses.dataclass from the standard library here (https://docs.python.org/3/library/dataclasses.html)