niner / inline-python-pm

Inline::Python - Write Perl subs and classes in Python.
https://metacpan.org/release/Inline-Python
20 stars 13 forks source link

croak() with full Python exception tracebacks; don't leak memory when croak()ing #20

Closed pypt closed 6 years ago

pypt commented 7 years ago

Hi Stefan,

Would you consider pulling in the following:

Tested and works on OS X, Ubuntu 12.04 with Python 2.7 and 3.5 (see Travis build).

Please let me know if you have any questions or need some additions.

niner commented 7 years ago

An interesting idea!

As an alternative implementation, what do you think about throwing an exception object instead of a string? This object could override stringification to return just the exception message itself, but also provide a method for getting the full backtrace. The stringification method could also check whether Carp::Always is active and automatically print a full backtrace in that case.

Throwing an object could also solve the memory leak, as we'd create a fully refcounted SV for the message itself.

pypt commented 7 years ago

Even though it's not stated explicitly in perlvar, I think everyone expects $@ to always be a string (and not some sort of an object that might be cast into a string), so returning an object instead of a string is very likely to break someone's code.

For example, Inline::Python returns objects for booleans to Perl which doesn't have a native boolean type. While it's a rather safe behavior, in our own particular case (we're using Inline::Python to gradually migrate our Perl codebase to Python) it still required some additional code changes and testing because, for example, one can't pass the returned boolean to the JSON module for serialization without enabling allow_blessed first.

So, I'd still opt for returning a vanilla string with a traceback. If someone really wants to get the exception object back, one can try ... except ... on the Python's side of the game and return the exception to Perl themselves.

pypt commented 7 years ago

Hi Stefan, have you maybe made a decision on this one?

niner commented 7 years ago

Sorry, I'm quite drained currently. Major systems restructuring, FOSDEM, quarterly management meeting. No time to think this through. Will get back to you once I've found some quiet hour.

pypt commented 7 years ago

Thanks Stefan, take your time!

pypt commented 7 years ago

Hi Stefan, still overloaded?