oracle / graalpython

GraalPy – A high-performance embeddable Python 3 runtime for Java
https://www.graalvm.org/python/
Other
1.24k stars 108 forks source link

TypeError: '>=' not supported between instances of 'TypeError' and 'int' in `traceback.py` module #247

Closed Galunid closed 2 years ago

Galunid commented 2 years ago

When I run the following python code:

import java
import traceback

try:
    BigInteger = java.type("java.math.BigInteger")
    myBigInt = BigInteger(42)
    myBigInt.shiftLeft(128)
    # public Java methods can just be called
    myBigInt["not"]()
    # Java method names that are keywords in Python can be accessed using "[]"
    byteArray = myBigInt.toByteArray()
    # Java arrays can act like Python lists
    print(list(byteArray))
except TypeError as terr:
    traceback.print_exc(terr)

I get the following log:

Traceback (most recent call last):
  File "/app/pyexec.py", line 3, in <module 'pyexec.py'>
    myBigInt = BigInteger(42)
TypeError: invalid instantiation of foreign object
bash-4.2# graalpython --polyglot --jvm pyexec.py 
Traceback (most recent call last):
  File "/app/pyexec.py", line 7, in <module 'pyexec.py'>
    myBigInt = BigInteger(42)
TypeError: invalid instantiation of foreign object

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/app/pyexec.py", line 16, in <module 'pyexec.py'>
    traceback.print_exc(terr)
  File "/opt/graalvm-ce-java17-21.3.0/languages/python/lib-python/3/traceback.py", line 163, in print_exc
    print_exception(*sys.exc_info(), limit=limit, file=file, chain=chain)
  File "/opt/graalvm-ce-java17-21.3.0/languages/python/lib-python/3/traceback.py", line 103, in print_exception
    for line in TracebackException(
  File "/opt/graalvm-ce-java17-21.3.0/languages/python/lib-python/3/traceback.py", line 509, in __init__
    self.stack = StackSummary.extract(
  File "/opt/graalvm-ce-java17-21.3.0/languages/python/lib-python/3/traceback.py", line 340, in extract
    if limit >= 0:
TypeError: '>=' not supported between instances of 'TypeError' and 'int'

Happens in both GraalVM Python 3.8.5 (GraalVM CE Native 22.1.0-dev) and Python 3.8.5 (GraalVM CE Native 21.3.0). The first is in Manjaro Linux Virtual Machine, the latter in docker container.

msimacek commented 2 years ago

You're not calling traceback.print_exc right. You would get the same error on CPython. Try it:

import traceback
try:
    1/0
except Exception as e:
    traceback.print_exc(e)

Outputs:

Traceback (most recent call last):
  File "/home/msimacek/t.py", line 3, in <module>
    1/0
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/msimacek/t.py", line 5, in <module>
    traceback.print_exc(e)
  File "/usr/lib64/python3.9/traceback.py", line 163, in print_exc
    print_exception(*sys.exc_info(), limit=limit, file=file, chain=chain)
  File "/usr/lib64/python3.9/traceback.py", line 103, in print_exception
    for line in TracebackException(
  File "/usr/lib64/python3.9/traceback.py", line 517, in __init__
    self.stack = StackSummary.extract(
  File "/usr/lib64/python3.9/traceback.py", line 340, in extract
    if limit >= 0:
TypeError: '>=' not supported between instances of 'ZeroDivisionError' and 'int'

The correct way is either:

traceback.print_exc()  # Implicitly takes the current caught exception

or

traceback.print_exception(type(e), e, e.__traceback__)