gpt4 book ai didi

javascript - Python-spidermonkey : How to get a line num. 错误

转载 作者:行者123 更新时间:2023-12-02 18:52:04 24 4
gpt4 key购买 nike

如何检索发生错误的行号?

在下一个脚本中,该行输出(作为最后一次加注的结果),但没有行号。在 JSError 对象中。

from spidermonkey import Runtime, JSError

runtime = Runtime()
context = runtime.new_context()

try:
context.execute( "function test(){ return 'OK' }; \n\ntest2()" )
except JSError as e:
print "ERROR!"
print "args = {0}".format( e.args )
print "message = {0}".format( e.message )
print "__dict__ = {0}".format( e.__dict__ )
raise

结果:

ERROR!
args = ('ReferenceError: test2 is not defined',)
message = ReferenceError: test2 is not defined
__dict__ = {}
Traceback (most recent call last):
File "test.py", line 7, in <module>
context.execute( "function test(){ return 'OK' }; \n\ntest2()" )
File "<anonymous JavaScript>", line 3, in JavaScript code
spidermonkey.JSError: ReferenceError: test2 is not defined

最佳答案

如果您只需要行号,可以从 traceback.extract_tb 检索它功能。

import sys, traceback
from spidermonkey import Runtime, JSError

runtime = Runtime()
context = runtime.new_context()

try:
context.execute( "function test(){ return 'OK' }; \n\ntest2()" )
except JSError as e:
info = sys.exc_info()

print "ERROR!"
for file, lineno, function, text in traceback.extract_tb(info[2]):
print "line #", lineno, ": ", text

关于javascript - Python-spidermonkey : How to get a line num. 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15718142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com