Open
Description
When trying to use booleans with Pyjnius, they get converted to integers in some situations, which causes errors.
>>> import jnius
>>> jnius.__version__
'1.3.0'
>>> Boolean = jnius.autoclass('java.lang.Boolean')
>>> Boolean("true") # this is consistent with java.lang.Integer behavior
<java.lang.Boolean at 0x11b53d5c8 jclass=java/lang/Boolean jself=<LocalRef obj=0x7fd240748ff0 at 0x10e942ef0>>
>>> Boolean.TRUE # should return True (to be consistent with java.lang.Integer.MAX_VALUE
1
>>> Boolean("true").compareTo(True) # should return 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "jnius/jnius_export_class.pxi", line 1145, in jnius.JavaMultipleMethod.__call__
File "jnius/jnius_export_class.pxi", line 857, in jnius.JavaMethod.__call__
File "jnius/jnius_export_class.pxi", line 954, in jnius.JavaMethod.call_method
File "jnius/jnius_jvm_dlopen.pxi", line 91, in jnius.create_jnienv
jnius.JavaException: JVM exception occurred: java.lang.Integer cannot be cast to java.lang.Boolean java.lang.ClassCastException
>>> Stack = jnius.autoclass('java.util.Stack')
>>> s = Stack()
>>> s.isEmpty()
True
>>> s.push(4)
4
>>> s.push(True) # should return True
1
>>> s.push(Boolean("true")) # should return True
1
The following test shows a change in behaviour between the current master branch and pyjnius 1.3.0:
-
Create a Java class with a function that accepts {{Boolean}} as argument:
class Test { public static void func(Boolean x) { System.out.println(x); } }
-
Call it from Python using pyjnius with a Python boolean:
python -c "from jnius import autoclass; test = autoclass('Test'); test.func(True)"
This will run on Pyjnius 1.3.0 (and will print out 1
instead of true
), but on the current master branch of pyjnius this will fail with TypeError: Invalid instance of 'java/lang/Integer' passed for a 'java/lang/Boolean'
If you use boolean
instead of Boolean
, it will work correctly on both versions and will print out true
.
Activity