Open lg8080 opened 3 years ago
Confirmed: There is no way to specify instances of Booleans in the current release. Booleans get converted to Integer(1)
.
Unfortunately my Java code expects instances of Boolean, not of Integer.
This bug ate up 3 hours of my time just to identify it :-( Didn't expect anything like it.
And I don't know how I can work around it. The only way seems to be to reimplement my python logic in Java :-(
Cant you pass python True and Java autoboxing will deal with it?
Unfortunately: No.
I did something like this:
myList = jnius.autoclass("java.util.ArrayList")
myList.add(jnius.autoclass("java.lang.Boolean").TRUE)
(BTW: jnius.autoclass("java.lang.Boolean").TRUE
should return an instance of Boolean
.)
The contents of that list seems to be an Integer(1)
. Somehow for some reason (= details are still unclear to me) instances of Boolean
get converted to Integer
. My Java code (correctly) complains that it receives instances of Integer
instead of Boolean
.
I took this quite uncommon approach of using Boolean.TRUE
because of passing Python True
and relying on autoboxing did lead to the unexpected result of receiving an Integer, not a Boolean.
Ah, that's interesting. If my Java code returns Boolean.TRUE
as a return value of a method I receive an integer 1
in Python.
In Python 1
as equivalent to true
. So it's quite natural that most code works quite well though we've a bug here.
Please note:
>>> type(True)
<class 'bool'>
>>> type(1)
<class 'int'>
And:
>>> isinstance(1, bool)
False
>>> isinstance(1, int)
True
But:
>>> isinstance(True, bool)
True
>>> isinstance(True, int)
True
Don’t think about Python - There is custom conversions code - see populate_args() in
https://github.com/kivy/pyjnius/blob/master/jnius/jnius_conversion.pxi [pyjnius.png] pyjnius/jnius/jnius_conversion.pxi at master · kivy/pyjniushttps://github.com/kivy/pyjnius/blob/master/jnius/jnius_conversion.pxi github.comhttps://github.com/kivy/pyjnius/blob/master/jnius/jnius_conversion.pxi
Perhaps we need a bit more handling here for booleans.
Craig
Sent from my iPhone
On 25 Jul 2023, at 21:43, mmj579 @.***> wrote:
In Python 1 as equivalent to true. So it's quite natural that most code works quite well though we've a bug here.
Please note:
type(True) <class 'bool'> type(1) <class 'int'>
And:
isinstance(1, bool) False isinstance(1, int) True
But:
isinstance(True, bool) True isinstance(True, int) True
— Reply to this email directly, view it on GitHubhttps://github.com/kivy/pyjnius/issues/602#issuecomment-1649871353, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAEXTCRQ5KN4C7ISJT3SLSTXR7EKPANCNFSM5B357OTQ. You are receiving this because you commented.Message ID: @.***>
@mmj579 As a workaround while this is not fixed yet, you can try this:
myList = jnius.autoclass("java.util.ArrayList")()
myList.add(jnius.autoclass("java.lang.Boolean")("true"))
I moved essential logic from Python to Java so I can work around that bug for now. The only remaining problems are values returned as integers instead of booleans. That's not ideal but it's okay. I can live with that.
Thank you for your comprehensive support! In my experience, it's pretty unusual to get feedback this quickly, especially on open source projects. I really appreciate that!
And: Pyjnius is a fantastic Python module! Very good work!
When trying to use booleans with Pyjnius, they get converted to integers in some situations, which causes errors.
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:
Call it from Python using pyjnius with a Python boolean:
This will run on Pyjnius 1.3.0 (and will print out
1
instead oftrue
), but on the current master branch of pyjnius this will fail withTypeError: Invalid instance of 'java/lang/Integer' passed for a 'java/lang/Boolean'
If you use
boolean
instead ofBoolean
, it will work correctly on both versions and will print outtrue
.