Python 错误发生在哪里?exec(编译(脚本,字符串,exec)
我有两个文件。其中一个脚本(从另一个文件调用函数)被编译和执行。我需要的是确定错误发生的位置:是在user_script.py中创建脚本(脚本的哪一行)时出错,还是在foo(参数)函数中出错 我希望捕获任何错误(SyntaxError、TypeError等),并根据它是发生在脚本本身还是发生在函数foo(parameter)中,以不同的方式处理它们 我展示了两个带有NameError的示例,但原则上我想对任何类型的错误都做同样的处理。我应该参考哪些属性来区分它们 例1 user\u script.pyPython 错误发生在哪里?exec(编译(脚本,字符串,exec),python,exception,python-3.x,exception-handling,traceback,Python,Exception,Python 3.x,Exception Handling,Traceback,我有两个文件。其中一个脚本(从另一个文件调用函数)被编译和执行。我需要的是确定错误发生的位置:是在user_script.py中创建脚本(脚本的哪一行)时出错,还是在foo(参数)函数中出错 我希望捕获任何错误(SyntaxError、TypeError等),并根据它是发生在脚本本身还是发生在函数foo(parameter)中,以不同的方式处理它们 我展示了两个带有NameError的示例,但原则上我想对任何类型的错误都做同样的处理。我应该参考哪些属性来区分它们 例1 user\u script
import sys
import traceback
from Catch_errors.my_function import function
script="a=1\nb=3\nfunction.foo(c)"
exec(compile(script,"<string>",'exec'))
class function:
def foo(parameter):
a = parameter
print(a) # or e.g. causing the error print(a+'sss')
import sys
import traceback
from Catch_errors.my_function import function
script="a=1\nb=3\nfunction.foo(2)"
exec(compile(script,"<string>",'exec'))
class function:
def foo(parameter):
a = parameter
print(b)
输出:
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0\helpers\pydev\pydevd.py", line 2199, in <module>
globals = debugger.run(setup['file'], None, None)
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0\helpers\pydev\pydevd.py", line 1638, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/Support/PycharmProjects/HelloWorldProject/Catch_errors/user_Script.py", line 7, in <module>
exec(compile(script,"<string>",'exec'))
File "<string>", line 3, in <module>
NameError: name 'c' is not defined
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0\helpers\pydev\pydevd.py", line 2199, in <module>
globals = debugger.run(setup['file'], None, None)
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0\helpers\pydev\pydevd.py", line 1638, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/Support/PycharmProjects/HelloWorldProject/Catch_errors/user_Script.py", line 5, in <module>
script="a=1\nb=3\nfunction.foo("+b+")"
NameError: name 'b' is not defined
输出:
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0\helpers\pydev\pydevd.py", line 2199, in <module>
globals = debugger.run(setup['file'], None, None)
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0\helpers\pydev\pydevd.py", line 1638, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/Support/PycharmProjects/HelloWorldProject/Catch_errors/user_Script.py", line 7, in <module>
exec(compile(script,"<string>",'exec'))
File "<string>", line 3, in <module>
NameError: name 'c' is not defined
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0\helpers\pydev\pydevd.py", line 2199, in <module>
globals = debugger.run(setup['file'], None, None)
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0\helpers\pydev\pydevd.py", line 1638, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/Support/PycharmProjects/HelloWorldProject/Catch_errors/user_Script.py", line 5, in <module>
script="a=1\nb=3\nfunction.foo("+b+")"
NameError: name 'b' is not defined
回溯(最近一次呼叫最后一次):
文件“C:\Program Files(x86)\JetBrains\PyCharm Community Edition 4.0\helpers\pydev\pydevd.py”,第2199行,在
globals=debugger.run(安装程序['file'],无,无)
文件“C:\Program Files(x86)\JetBrains\PyCharm Community Edition 4.0\helpers\pydev\pydevd.py”,第1638行,正在运行
pydev_imports.execfile(文件、全局、局部)#执行脚本
文件“C:\Program Files(x86)\JetBrains\PyCharm Community Edition 4.0\helpers\pydev\\u pydev\u imps\\u pydev\u execfile.py”,execfile中第18行
exec(编译(内容+“\n”,文件,'exec'),全局,loc)
文件“C:/Users/Support/PycharmProjects/HelloWorldProject/Catch_errors/user_Script.py”,第5行,在
script=“a=1\nb=3\n函数.foo(“+b+”)”
名称错误:未定义名称“b”
您需要使function.foo成为一个静态方法
class function:
@staticmethod
def foo(parameter):
a = parameter
print(a) # or e.g. causing the error print(a+'sss')
\n
在script=“a=1\nb=3\n function.foo(2)”语句中缺失
foo1()
,并按类实例调用,例如ClassName().FunctionName()import sys
import traceback
from test1 import function
script="a=1\nb=3\nfunction.foo(2)" # of e.g. script="a=1\nb=3\function.foo(2, 3)"
try:exec(compile(script,"<string>",'exec'))
except:print traceback.format_exc()
script="a=1\nb=3\nfunction().foo1(33)" # of e.g. script="a=1\nb=3\function.foo(2, 3)"
try:exec(compile(script,"<string>",'exec'))
except:print traceback.format_exc()
script="a=1\nb=3\nfunction().foo1(c)"
try:exec(compile(script,"<string>",'exec'))
except NameError, e:
print "NameError exception, ", traceback.format_exc()
except:
print traceback.format_exc()
输出:
$ python test.py
in foo: 2
in foo1: 3
NameError exception, Traceback (most recent call last):
File "test.py", line 1527, in <module>
try:exec(compile(script,"<string>",'exec'))
File "<string>", line 3, in <module>
NameError: name 'c' is not defined
$python test.py
在富:2
在foo1:3中
NameError异常,回溯(最后一次调用):
文件“test.py”,第1527行,在
try:exec(编译(脚本,“,'exec'))
文件“”,第3行,在
NameError:未定义名称“c”
导入系统,回溯
从Catch_errors.my_函数导入函数
script=“a=1\nb=3\n函数().foo(a)”
尝试:
compiled_script=compile(脚本,“,'exec')
exec(编译的_脚本)
除:
exc_类型,exc_对象,exc_tb=sys.exc_info()
tr=回溯.提取\u tb(sys.exc\u info()[-1])
印刷品(tr)
如果tr[-1][0]中的“my_function.py”:
打印(“函数“+str(tr[-1][-2])+”()中的异常,行“+tr[-1][-1][-1][-1]”)
打印(exc_对象参数[0])
其他:
打印('用户脚本中的异常:{}'。格式(exc_obj))
此外,如果您准确地发布您的错误,也会有所帮助。不,您的建议是错误的。它会导致TypeError,而我的版本工作正常。但是,我并不是在寻找创建正确脚本的方法,而是希望捕获用户可能会出现的任何错误。我想捕获任何错误(SyntaxError、TypeError等),并根据它是发生在脚本本身还是发生在函数foo(参数)
中来处理它们。如果您的脚本没有编译,您将得到SyntaxError
。但是,向我们展示实际的回溯将让我们更好地帮助您。您必须反思回溯,以确定异常在堆栈中发生的位置。我认为我的建议没有错。我只是没有注意到代码中的第二个错误(第一个错误是调用类名后没有括号的类变量,第二个错误是缺少\n
。我还建议您包括“但希望捕获用户可以执行的任何可能的错误。我希望捕获任何错误(SyntaxError、TypeError等)并根据它是在脚本本身中还是在您帖子中的函数foo(parameter)“中而不是在注释中发生的,对它们进行不同的处理。那么我应该引用哪些属性?当我将其设置为静态时,我的案例的具体变化是什么?@Spu:two change需要1。\n在script=“a=1\nb=3\n function.foo(2)”中缺少语句.2.使用静态方法。有关详细信息,请参阅我的解决方案。@vivek sable最后一个“\n”真的没有必要,你测试过吗?@huazhihao我在访问方法方面没有问题。我的问题是关于错误处理的正确方法。我已经更新了帖子。谢谢,这很有用,但对我的主要问题没有任何帮助。你知道如何区分脚本中的错误和函数中的错误吗?你正在尝试ing访问类中定义的foo
方法。在我的@Spu:解决方案中,您可以看到foo
和foo1
方法之间的区别。如果要调用foo
方法,则必须将其设为静态方法并从类名调用。如果要调用foo1
,则需要创建instance类,然后调用。我在访问该方法方面没有问题。我的问题是关于错误处理的正确方法。我已更新了帖子。两个示例请参见我的注释。@Spu:nowc
未定义。您在尝试什么?