简体   繁体   English

PyV8将错误抛出到JavaScript /获取当前行号

[英]PyV8 throw error into JavaScript/get current line number

I'm using PyV8 and I'd like to disallow assigning javascript objects to my python objects because of a current memory leak bug with PyV8 . 我正在使用PyV8,并且由于当前PyV8存在内存泄漏错误,因此我想禁止将javascript对象分配给python对象。 I came upwith this code: 我想到了以下代码:

def __setattr__(self, name, val):
    if isinstance(val, (PyV8.JSFunction, PyV8.JSObject)):
        raise ValueError("Can't put js objects on py values")
    object.__setattr__(self, name, val)

However, the traceback I get when I do this is not very helpful: 但是,执行此操作时得到的追溯不是很有帮助:

Traceback (most recent call last):
  File "pyv8-bug-186.py", line 142, in <module>
    """)
  File "pyv8-bug-186.py", line 75, in run_case
    obj = ctxt.eval(js)
ValueError: Can't put js objects on py values

Particularly, it gives no information at all about which javascript file was running or what the line number was, etc. I see two potential solutions: 特别是,它根本不提供有关正在运行哪个javascript文件或行号等的信息。我看到了两种可能的解决方案:

  1. Get the currently running javascript line number and include it in the python exception. 获取当前正在运行的javascript行号,并将其包括在python异常中。
  2. Somehow throw an exception into javascript so that V8 is the one that shows the error, which will then include the line numbers. 莫名其妙地在javascript中抛出了一个异常,因此V8是显示错误的代码,它将包含行号。

How would I go about doing either? 我该怎么做?

Not sure if this is by design, but for now, raise TypeError() from within Python causes a javascript exception to be raised and displayed (along with line and column numbers). 不知道这是否是设计raise TypeError() ,但就目前而言,从Python内部raise TypeError()会引发并显示javascript异常(以及行号和列号)。 Great success! 巨大的成功!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM