Skip to content
This repository was archived by the owner on May 8, 2018. It is now read-only.

Issue with python3 unicode vs string and pyjnius #1

Open
Kovak opened this issue Nov 30, 2013 · 5 comments
Open

Issue with python3 unicode vs string and pyjnius #1

Kovak opened this issue Nov 30, 2013 · 5 comments

Comments

@Kovak
Copy link

Kovak commented Nov 30, 2013

Hey Knappador,

I have been using kivy toaster in a project, however they are wanting to prepare for python3, so we are doing

from __future__ import unicode_literals, print_function

at the beginning of all our files.

This is resulting in a crash when trying to use a toast on Android with this error:

[WARNING           ] stderr:   File "/home/ehealth/Documents/SMART2/smart2_app/.buildozer/android/platform/python-for-android/build/python-install/lib/python2.7/site-packages/jnius/reflect.py", line 150, in autoclass
[WARNING           ] stderr: TypeError: Argument 'name' has incorrect type (expected str, got unicode)

I am looking into a fix for this, but I'm a little out of my depth and was hoping you might have an idea.

Thanks,
Kovak

@knappador
Copy link
Owner

If the variable being passed in as "text" is a unicode_literal, I bet JNIus
barfs at the coercion.

Going to take a wild guess that the line:
c = cast('java.lang.CharSequence', String(text))

Can be replaced with:
c = cast('java.lang.CharSequence', str(text))
Or:
c = cast('java.lang.CharSequence', String(str(text)))

I'm going to look at JNIus autocast code and try to make the real fix.

On Fri, Nov 29, 2013 at 8:06 PM, Kovak [email protected] wrote:

Hey Knappador,

I have been using kivy toaster in a project, however they are wanting to
prepare for python3, so we are doing

from future import unicode_literals, print_function

at the beginning of all our files.

This is resulting in a crash when trying to use a toast on Android with
this error:

[WARNING ] stderr: File "/home/ehealth/Documents/SMART2/smart2_app/.buildozer/android/platform/python-for-android/build/python-install/lib/python2.7/site-packages/jnius/reflect.py", line 150, in autoclass
[WARNING ] stderr: TypeError: Argument 'name' has incorrect type (expected str, got unicode)

I am looking into a fix for this, but I'm a little out of my depth and was
hoping you might have an idea.

Thanks,
Kovak


Reply to this email directly or view it on GitHubhttps://github.com//issues/1
.

@Kovak
Copy link
Author

Kovak commented Dec 1, 2013

The error is related to the find_javaclass method invoked in autocast
https://github.com/kivy/pyjnius/blob/481b3e5d30fbf2cd09409b024ead0a1ee27fb354/jnius/jnius_export_func.pxi

it expects bytes but receives a unicode string. I haven't been successful in converting the unicode_literal to bytes using the documented 'encode' method or the shorthand b'string' method.

@knappador
Copy link
Owner

Sounds like the change is needed in jnius. A lot of our jnius modules will
need some explicit string type if we don't handle it inside jnius instead.

On Sat, Nov 30, 2013 at 7:44 PM, Kovak [email protected] wrote:

The error is related to the find_javaclass method invoked in autocast

https://github.com/kivy/pyjnius/blob/481b3e5d30fbf2cd09409b024ead0a1ee27fb354/jnius/jnius_export_func.pxi

it expects bytes but receives a unicode string. I haven't been successful
in converting the unicode_literal to bytes using the documented 'encode'
method or the shorthand b'string' method/


Reply to this email directly or view it on GitHubhttps://github.com//issues/1#issuecomment-29565343
.

@Kovak Kovak closed this as completed Dec 1, 2013
@Kovak
Copy link
Author

Kovak commented Dec 2, 2013

Took a look at this issue again and I was too tired to notice last night that I missed some problematic instances and that is why I was having trouble. This works fine with pyjnius in python2 when done like this:

from __future__ import unicode_literals, print_function
from kivy.logger import Logger
from jnius import autoclass, PythonJavaClass, java_method, cast
from android import activity
from android.runnable import run_on_ui_thread

Toast = autoclass(b'android.widget.Toast')
context = autoclass(b'org.renpy.android.PythonActivity').mActivity

@run_on_ui_thread
def toast(text, length_long=False):
    duration = Toast.LENGTH_LONG if length_long else Toast.LENGTH_SHORT
    String = autoclass(b'java.lang.String')
    c = cast(b'java.lang.CharSequence', String(text))
    t = Toast.makeText(context, c, duration)
    t.show()

Should help anyone perhaps preparing for the jump to python3 using the __future__ imports

@Kovak Kovak reopened this Dec 2, 2013
@knappador
Copy link
Owner

I got into the jnius source but was on the wrong trail. I'll close this
once we fix jnius to always perform the appropriate coercion.

On Sun, Dec 1, 2013 at 9:12 PM, Kovak [email protected] wrote:

Reopened #1 #1.


Reply to this email directly or view it on GitHubhttps://github.com//issues/1
.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants