-
Notifications
You must be signed in to change notification settings - Fork 256
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
Boolean conversions between Java and Python #602
Comments
Bools are now auto-converted to Integers instead of Booleans, so we have to perform the conversion explicitly now. Tracked upstream in kivy/pyjnius#602 .
Bools are now auto-converted to Integers instead of Booleans, so we have to perform the conversion explicitly now. Tracked upstream in kivy/pyjnius#602 .
Confirmed: There is no way to specify instances of Booleans in the current release. Booleans get converted to |
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: The contents of that list seems to be an I took this quite uncommon approach of using |
Ah, that's interesting. If my Java code returns |
In Python 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/pyjnius<https://github.com/kivy/pyjnius/blob/master/jnius/jnius_conversion.pxi>
github.com<https://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 GitHub<#602 (comment)>, or unsubscribe<https://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:
|
I moved essential logic from Python to Java so I can work around that bug for now. 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
.The text was updated successfully, but these errors were encountered: