Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Java stack traces into Python format #1203

Open
mhsmith opened this issue Jul 6, 2024 · 0 comments
Open

Convert Java stack traces into Python format #1203

mhsmith opened this issue Jul 6, 2024 · 0 comments

Comments

@mhsmith
Copy link
Member

mhsmith commented Jul 6, 2024

The reverse (converting Python exceptions to Java format) is already implemented, and marks Python frames with a package name of <python>. But if you use the Python console to trigger a Java exception, you get a confusing mixture of Java and Python stack traces:

>>> Integer.parseInt("abc")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "class.pxi", line 962, in java.chaquopy.JavaMultipleMethod.__get__.lambda3
  File "class.pxi", line 1000, in java.chaquopy.JavaMultipleMethod.__call__
  File "class.pxi", line 781, in java.chaquopy.JavaMethod.__call__
  File "class.pxi", line 936, in java.chaquopy.JavaMethod.call_static_method
  File "env.pxi", line 292, in java.chaquopy.CQPEnv.CallStaticIntMethodA
  File "env.pxi", line 546, in java.chaquopy.CQPEnv.check_exception
java.lang.NumberFormatException: Invalid int: "abc"
	at java.lang.Integer.invalidInt(Integer.java:138)
	at java.lang.Integer.parse(Integer.java:410)
	at java.lang.Integer.parseInt(Integer.java:367)
	at java.lang.Integer.parseInt(Integer.java:334)
	at com.chaquo.python.PyObject.callAttrThrows(Native Method)
	at com.chaquo.python.PyObject.callAttr(PyObject.java:209)
	at com.chaquo.python.utils.ReplActivity$Task.run(ReplActivity.java:36)
	at com.chaquo.python.utils.ConsoleActivity$Task.lambda$start$0$ConsoleActivity$Task(ConsoleActivity.java:350)
	at com.chaquo.python.utils.-$$Lambda$ConsoleActivity$Task$AONo-zGHGFofNeDXqOaxrxZtbco.run(lambda)
	at java.lang.Thread.run(Thread.java:818)

There was previously no public API for manipulating Python stack traces, but one was added in Python 3.7 (types.TracebackType).

Any Java frames not located between the throw and the catch (e.g. callAttrThrows and below in the trace above) should be omitted from the trace when caught by Python.

Update the documentation to match.

@mhsmith mhsmith changed the title Better Java -> Python exception integration Convert Java stack traces into Python format Jul 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant