Commit 9bd19dba authored by Tom Niget's avatar Tom Niget

Use __ for traceback info

parent 775f5b0b
...@@ -50,10 +50,10 @@ def exception_hook(exc_type, exc_value, tb): ...@@ -50,10 +50,10 @@ def exception_hook(exc_type, exc_value, tb):
if name == "visit" and (node := local_vars["node"]) and isinstance(node, ast.AST): if name == "visit" and (node := local_vars["node"]) and isinstance(node, ast.AST):
last_node = node last_node = node
if node := local_vars.get("TB_NODE", None): if node := local_vars.get("__TB_NODE__", None):
last_node = node last_node = node
if local_vars.get("TB_SKIP", None) and tb.tb_next: if local_vars.get("__TB_SKIP__", None) and tb.tb_next:
tb = tb.tb_next tb = tb.tb_next
continue continue
...@@ -61,7 +61,7 @@ def exception_hook(exc_type, exc_value, tb): ...@@ -61,7 +61,7 @@ def exception_hook(exc_type, exc_value, tb):
line_no = tb.tb_lineno line_no = tb.tb_lineno
print(cf.red(f"File {filename}:{line_no}, in {cf.green(name)}"), end="") print(cf.red(f"File {filename}:{line_no}, in {cf.green(name)}"), end="")
if info := local_vars.get("TB", None): if info := local_vars.get("__TB__", None):
print(f": {cf.magenta(info)}\x1b[24m") print(f": {cf.magenta(info)}\x1b[24m")
else: else:
print() print()
...@@ -177,7 +177,7 @@ else: ...@@ -177,7 +177,7 @@ else:
def transpile(source, name: str, path=None): def transpile(source, name: str, path=None):
TB = f"transpiling module {cf.white(name)}" __TB__ = f"transpiling module {cf.white(name)}"
res = ast.parse(source, type_comments=True) res = ast.parse(source, type_comments=True)
IfMainVisitor().visit(res) IfMainVisitor().visit(res)
......
...@@ -16,8 +16,8 @@ from transpiler.utils import UnsupportedNodeError, highlight ...@@ -16,8 +16,8 @@ from transpiler.utils import UnsupportedNodeError, highlight
class UniversalVisitor: class UniversalVisitor:
def visit(self, node): def visit(self, node):
"""Visit a node.""" """Visit a node."""
TB = f"emitting C++ code for {highlight(node)}" __TB__ = f"emitting C++ code for {highlight(node)}"
# TB_SKIP = True # __TB_SKIP__ = True
if type(node) == list: if type(node) == list:
for n in node: for n in node:
......
...@@ -13,7 +13,7 @@ class FileVisitor(BlockVisitor): ...@@ -13,7 +13,7 @@ class FileVisitor(BlockVisitor):
module_name: str module_name: str
def visit_Module(self, node: ast.Module) -> Iterable[str]: def visit_Module(self, node: ast.Module) -> Iterable[str]:
TB = "emitting C++ code for Python module" __TB__ = "emitting C++ code for Python module"
stmt: ast.AST stmt: ast.AST
yield "#include <python/builtins.hpp>" yield "#include <python/builtins.hpp>"
......
...@@ -19,7 +19,7 @@ IGNORED_IMPORTS = {"typon", "typing", "__future__", "dataclasses", "enum"} ...@@ -19,7 +19,7 @@ IGNORED_IMPORTS = {"typon", "typing", "__future__", "dataclasses", "enum"}
class ModuleVisitor(BlockVisitor): class ModuleVisitor(BlockVisitor):
includes: list[str] = field(default_factory=list) includes: list[str] = field(default_factory=list)
def visit_Import(self, node: ast.Import) -> Iterable[str]: def visit_Import(self, node: ast.Import) -> Iterable[str]:
TB = f"emitting C++ code for {highlight(node)}" __TB__ = f"emitting C++ code for {highlight(node)}"
for alias in node.names: for alias in node.names:
concrete = self.fix_name(alias.asname or alias.name) concrete = self.fix_name(alias.asname or alias.name)
if alias.module_obj.is_python: if alias.module_obj.is_python:
...@@ -46,7 +46,7 @@ class ModuleVisitor(BlockVisitor): ...@@ -46,7 +46,7 @@ class ModuleVisitor(BlockVisitor):
yield "" yield ""
def emit_python_func(self, mod: str, name: str, alias: str, fty: FunctionType) -> Iterable[str]: def emit_python_func(self, mod: str, name: str, alias: str, fty: FunctionType) -> Iterable[str]:
TB = f"emitting C++ code for Python function {highlight(f'{mod}.{name}')}" __TB__ = f"emitting C++ code for Python function {highlight(f'{mod}.{name}')}"
yield "struct {" yield "struct {"
yield f"auto operator()(" yield f"auto operator()("
......
...@@ -98,17 +98,17 @@ class ScoperBlockVisitor(ScoperVisitor): ...@@ -98,17 +98,17 @@ class ScoperBlockVisitor(ScoperVisitor):
node.is_declare = decl node.is_declare = decl
if node.value is not None: if node.value is not None:
ty_val = self.get_type(node.value) ty_val = self.get_type(node.value)
TB = f"unifying annotation {highlight(node.annotation)} with value {highlight(node.value)} of type {highlight(ty_val)}" __TB__ = f"unifying annotation {highlight(node.annotation)} with value {highlight(node.value)} of type {highlight(ty_val)}"
ty.unify(ty_val) ty.unify(ty_val)
def visit_assign_target(self, target, decl_val: BaseType) -> bool: def visit_assign_target(self, target, decl_val: BaseType) -> bool:
TB = f"analyzing assignment target {highlight(target)} with value {highlight(decl_val)}" __TB__ = f"analyzing assignment target {highlight(target)} with value {highlight(decl_val)}"
if isinstance(target, ast.Name): if isinstance(target, ast.Name):
if target.id == "_": if target.id == "_":
return False return False
target.type = decl_val target.type = decl_val
if vdecl := self.scope.get(target.id, {VarKind.LOCAL, VarKind.GLOBAL, VarKind.NONLOCAL}, restrict_function=True): if vdecl := self.scope.get(target.id, {VarKind.LOCAL, VarKind.GLOBAL, VarKind.NONLOCAL}, restrict_function=True):
TB = f"unifying existing variable {highlight(target.id)} of type {highlight(vdecl.type)} with assigned value {highlight(decl_val)}" __TB__ = f"unifying existing variable {highlight(target.id)} of type {highlight(vdecl.type)} with assigned value {highlight(decl_val)}"
vdecl.type.unify(decl_val) vdecl.type.unify(decl_val)
return False return False
else: else:
...@@ -333,7 +333,7 @@ class ScoperBlockVisitor(ScoperVisitor): ...@@ -333,7 +333,7 @@ class ScoperBlockVisitor(ScoperVisitor):
def visit(self, node: ast.AST): def visit(self, node: ast.AST):
if isinstance(node, ast.AST): if isinstance(node, ast.AST):
TB_SKIP = True __TB_SKIP__ = True
super().visit(node) super().visit(node)
node.scope = self.scope node.scope = self.scope
else: else:
......
...@@ -57,7 +57,7 @@ class ScoperVisitor(NodeVisitorSeq): ...@@ -57,7 +57,7 @@ class ScoperVisitor(NodeVisitorSeq):
def visit_block(self, block: list[ast.AST]): def visit_block(self, block: list[ast.AST]):
if not block: if not block:
return return
TB = f"running type analysis on block starting with {highlight(block[0])}" __TB__ = f"running type analysis on block starting with {highlight(block[0])}"
self.fdecls = [] self.fdecls = []
for b in block: for b in block:
self.visit(b) self.visit(b)
...@@ -82,8 +82,8 @@ class ScoperVisitor(NodeVisitorSeq): ...@@ -82,8 +82,8 @@ class ScoperVisitor(NodeVisitorSeq):
exc = None exc = None
def visit_function_definition(self, node, rtype): def visit_function_definition(self, node, rtype):
TB = f"running type analysis on the body of {highlight(node)}" __TB__ = f"running type analysis on the body of {highlight(node)}"
TB_NODE = node __TB_NODE__ = node
from transpiler.phases.typing.block import ScoperBlockVisitor from transpiler.phases.typing.block import ScoperBlockVisitor
for b in node.body: for b in node.body:
decls = {} decls = {}
......
...@@ -38,10 +38,10 @@ class ScoperExprVisitor(ScoperVisitor): ...@@ -38,10 +38,10 @@ class ScoperExprVisitor(ScoperVisitor):
def visit(self, node) -> BaseType: def visit(self, node) -> BaseType:
if existing := getattr(node, "type", None): if existing := getattr(node, "type", None):
return existing.resolve() return existing.resolve()
TB_SKIP = True __TB_SKIP__ = True
res = super().visit(node) res = super().visit(node)
if not res: if not res:
TB_SKIP = False __TB_SKIP__ = False
raise NotImplementedError(f"`{ast.unparse(node)}` {type(node)}") raise NotImplementedError(f"`{ast.unparse(node)}` {type(node)}")
res = res.resolve() res = res.resolve()
if True or not hasattr(res, "from_node"): if True or not hasattr(res, "from_node"):
......
...@@ -84,7 +84,7 @@ class BaseType(ABC): ...@@ -84,7 +84,7 @@ class BaseType(ABC):
def unify(self, other: "BaseType", mode = UnifyMode.NORMAL): def unify(self, other: "BaseType", mode = UnifyMode.NORMAL):
a, b = self.resolve(), other.resolve() a, b = self.resolve(), other.resolve()
TB = f"unifying {highlight(a)} and {highlight(b)}" __TB__ = f"unifying {highlight(a)} and {highlight(b)}"
if isinstance(b, TypeVariable): if isinstance(b, TypeVariable):
a, b = b, a a, b = b, a
a.unify_internal(b, mode) a.unify_internal(b, mode)
...@@ -261,7 +261,7 @@ class TypeOperator(BaseType, ABC): ...@@ -261,7 +261,7 @@ class TypeOperator(BaseType, ABC):
from transpiler.phases.typing.exceptions import TypeMismatchError, TypeMismatchKind from transpiler.phases.typing.exceptions import TypeMismatchError, TypeMismatchKind
# TODO(zdimension): this is really broken... but it would be nice # TODO(zdimension): this is really broken... but it would be nice
# if from_node := next(filter(None, (getattr(x, "from_node", None) for x in (other, self))), None): # if from_node := next(filter(None, (getattr(x, "from_node", None) for x in (other, self))), None):
# TB_NODE = from_node # __TB_NODE__ = from_node
if not isinstance(other, TypeOperator): if not isinstance(other, TypeOperator):
raise TypeMismatchError(self, other, TypeMismatchKind.DIFFERENT_TYPE) raise TypeMismatchError(self, other, TypeMismatchKind.DIFFERENT_TYPE)
if mode.match_protocol: if mode.match_protocol:
......
...@@ -7,7 +7,7 @@ from transpiler.utils import UnsupportedNodeError, highlight ...@@ -7,7 +7,7 @@ from transpiler.utils import UnsupportedNodeError, highlight
class NodeVisitorSeq: class NodeVisitorSeq:
def visit(self, node): def visit(self, node):
TB = f"running type analysis on {highlight(node)}" __TB__ = f"running type analysis on {highlight(node)}"
"""Visit a node.""" """Visit a node."""
if type(node) == list: if type(node) == list:
for n in node: for n in node:
......
...@@ -61,7 +61,7 @@ def highlight(code, full=False): ...@@ -61,7 +61,7 @@ def highlight(code, full=False):
""" """
Syntax highlights code as Python using colorama Syntax highlights code as Python using colorama
""" """
TB = f"syntax highlighting {code}" __TB__ = f"syntax highlighting {code}"
if code is None: if code is None:
return cf.yellow("<None>") return cf.yellow("<None>")
if type(code) == list: if type(code) == list:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment