Search code examples
javascriptpythonexceptionv8pyv8

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. 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:

  1. Get the currently running javascript line number and include it in the python exception.
  2. Somehow throw an exception into javascript so that V8 is the one that shows the error, which will then include the line numbers.

How would I go about doing either?


Solution

  • 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). Great success!