[英]Customize traceback from exceptions in Python-based DSL
我正在創建一個基於python的DSL,只是為了好玩。 現在,它只是將文本編譯成一個直接執行python代碼的AST。 我的意思是如果我寫:
a = int(read("input something: "))
b = a**2
它將其轉換為如下樹:
Program:
VariableSet a:
Call:
VariableGet int
Call:
VariableGet read
Literal("input something: ")
VariableSet b:
BinaryExpession **:
VariableGet a
Literal(2)
這些節點中的每一個都將使用如下代碼執行:
class VariableSet(object):
def __init__(self, name, expr):
self.name = name
self.expr = expr
def __call__(self, scope):
value = self.expr(scope)
scope.put(self.name, value)
return value
它確實很好用。 我想,當某些指令拋出異常時,異常堆棧回溯指向DSL源代碼,而不是python AST代碼。 現在,我看到的只有:
Traceback (most recent call last):
File "parser.py", line 164, in <module>
parse(data)(scope)
File "/home/juanplopes/Projects/my-parser/ast.py", line 158, in __call__
result = expr(scope)
File "/home/juanplopes/Projects/my-parser/ast.py", line 63, in __call__
value = self.expr(scope)
File "/home/juanplopes/Projects/my-parser/ast.py", line 81, in __call__
return self.method(scope)(*[arg(scope) for arg in self.args])
File "/home/juanplopes/Projects/my-parser/ast.py", line 81, in __call__
return self.method(scope)(*[arg(scope) for arg in self.args])
File "parser.py", line 153, in read
raise Exception('some exception')
Exception: some exception
有沒有辦法自定義如何在python中生成回溯?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.