[Zope-Perl] Exception traceback
Gisle Aas
gisle@ActiveState.com
26 May 2000 12:22:22 +0200
Today I improved the handling of python exceptions that pass through
perl. What happens on the language barriers now is:
python called from perl
Any exceptions from python are extracted with PyErr_Fetch()
and packed up in a Python::Err object which is assigned to $@
and then re-thrown as a perl exception.
perl called from python
If perl has an outstanding exception when we get back to python
then we check if $@ is a Python::Err object and if so unpack it and
restore python exception state with PyErr_Restore().
If $@ is not a Python::Err object, then raise an python exception
"perl.error" with the stringified $@ as the value.
This arrangement should make it possible for python exceptions to pass
through a layer of perl code unaltered. The only problem is that the
perl code make no traces of itself in the traceback object, and from
studying the code Python/traceback.c it does not appear that python
could easily deal with foreign callframes/traceback objects without
some core python patching. The question is if this will be worth it?
Example of current behaviour:
------------------------------------------------------->8------------
import perl
perl.eval("""
sub callback {
my $obj = shift;
my $meth = shift;
return $obj->$meth(@_);
}
""")
class Foo:
def foo(self, a):
print "calling foo", a
return 3/a
pass
obj = Foo()
print perl.call("callback", obj, "foo", 3)
print perl.call("callback", obj, "foo", 4.0)
print perl.call("callback", obj, "foo", 0)
------------------------------------------------------->8------------
$ python callback.py
calling foo 3
1
calling foo 4.0
0.75
calling foo 0
Traceback (most recent call last):
File "callback.py", line 24, in ?
print perl.call("callback", obj, "foo", 0)
File "callback.py", line 17, in foo
return 3/a
ZeroDivisionError: integer division or modulo
Regards,
Gisle