exception handling

36 views
Skip to first unread message

mcot

unread,
Dec 13, 2010, 12:15:10 PM12/13/10
to PyV8
Hi, is there any way to make this raise a python exception instead of
a generic PyV8.JSError:

import PyV8


class TestException(Exception):
def __init__(self):
pass

def __str__(self):
return "test exception"

class Global(PyV8.JSClass):

def __init__(self):

pass

def testExceptions(self):
raise TestException()

ctx = PyV8.JSContext(Global())
ctx.enter()
ctx.eval("""testExceptions()""")


My Output is:

Traceback (most recent call last):
ctx.eval("""testExceptions()""")
PyV8.JSError: JSError: Error ( @ 1 : 0 ) -> testExceptions()


Trying to make it the python exception so I can catch it based on the
python exception.

Flier Lu

unread,
Dec 13, 2010, 9:21:40 PM12/13/10
to PyV8
Sure, I have submitted an issue to trace it, it will be ready soon

http://code.google.com/p/pyv8/issues/detail?id=63

Flier Lu

unread,
Dec 15, 2010, 12:17:25 PM12/15/10
to PyV8
Please verify the issue with SVN trunk after r310

def testExceptionMapping(self):
class TestException(Exception):
pass

class Global(JSClass):
def raiseExceptions(self):
raise TestException()
with JSContext(Global()) as ctxt:
self.assertRaises(TestException, ctxt.eval,
"this.raiseExceptions();")

The current implementation may introduce a slight memory leak, because
pyv8 must trace the Python exception in the Javascript exception, if
the JS exception was eaten by JS code, those Python exception may not
be collected. Because I can't find any way to trace the lifecycle of
Javascript object.

Flier Lu

unread,
Dec 17, 2010, 12:18:48 PM12/17/10
to PyV8
The memory leak has been fixed by tracing object lifecycle, which also
support the destructor

mcot

unread,
Dec 17, 2010, 2:12:38 PM12/17/10
to PyV8
This looks great. This is an awesome improvement to be able to see
errors when js code calls back into python.

Thanks,
Matt
Reply all
Reply to author
Forward
0 new messages