Skip to content
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

Strings Encoding Issues in Python 3 due to latest Cython updates #300

Closed
madhawav opened this issue Oct 8, 2017 · 6 comments
Closed

Strings Encoding Issues in Python 3 due to latest Cython updates #300

madhawav opened this issue Oct 8, 2017 · 6 comments
Labels
Milestone

Comments

@madhawav
Copy link
Contributor

madhawav commented Oct 8, 2017

The latest release version of Cython (0.27.1) is causing pyjnius issues related to string encoding. I have experienced this issue in Python 3.5 for Ubuntu 16.04 (fresh installation).

The issue is as follows.

A strings passed from Python to Java as parameter of a method gets encoded incorrectly. For example, when I pass the string "query" as a method parameter and system.out.println from Java, it gets printed as "å".

When I install pyjnius using Cython 0.25.2, this issue is resolved.
Also, this issue does not occur with Python 2.7.


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@iMcG33k
Copy link

iMcG33k commented Oct 10, 2017

The same issue on Cython (0.27.1) Python 3.6 macos 10.13 JDK 1.8.
But java String Works well

Stack = autoclass('java.util.Stack')
stack = Stack()
JString = autoclass('java.lang.String')
stack.push(JString("hello"))
stack.push(JString("world"))
print(stack.pop())

output:
Hello world

world
hello

@psader
Copy link
Contributor

psader commented Oct 21, 2017

The issue is resolved by modifying populate_args in jnius_conversion.pxi, such that the input py_str is always converted with .encode('utf-8') before calling NewStringUTF. This means only running the code under except (UnicodeEncodeError, TypeError): so that there's only one possible call to NewStringUTF.
The test drivers pass with this change with python 2 and 3. There may be some case not covered by them, but calling a string conversion routine twice to see which one works is a bad code smell, because otherwise you end up with the "Bush hid the facts" bug (https://en.wikipedia.org/wiki/Bush_hid_the_facts).

@psader
Copy link
Contributor

psader commented Oct 21, 2017

There's also #285 to resolve this.

jmuhlich added a commit to jmuhlich/ashlar that referenced this issue Mar 2, 2018
This is a problem with pyjnius and the most recent versions of cython
which corrupt Python strings passed to Java methods. Explicitly constructing
a Java string to pass to Java does work, so this is the solution used here.
jmuhlich added a commit to labsyspharm/ashlar that referenced this issue Mar 2, 2018
This is a problem with pyjnius and the most recent versions of cython
which corrupt Python strings passed to Java methods. Explicitly constructing
a Java string to pass to Java does work, so this is the solution used here.
@jakirkham
Copy link

Should this be closed now that PR ( #285 ) is merged or is this still outstanding?

@people-can-fly
Copy link

one workaround from @Ariel-Lieberman using java.long.string

Stack = autoclass('java.util.Stack')
jString = autoclass('java.lang.String')
stack = Stack()
stack.push(jString('OK'))
print(stack.pop())

@KeyWeeUsr
Copy link
Contributor

Resolved by #285. If the issue still persists, open a new issue and reference old ones if they are related. This one is rather messy together with the references.

@KeyWeeUsr KeyWeeUsr added the bug label Nov 29, 2018
@KeyWeeUsr KeyWeeUsr added this to the 1.1.4 milestone Nov 29, 2018
jmuhlich added a commit to jmuhlich/ashlar that referenced this issue Jan 8, 2020
Also upgrade pyjnius to the latest release which fixes the long-standing Python
3 string conversion bug (kivy/pyjnius#300). This seems to have changed how
interfaces/subclasses behave, so some new casts and alternative BioFormats API
use was needed. Nothing was needed specifically for the BioFormats 6 change
itself, though.
jmuhlich added a commit to labsyspharm/ashlar that referenced this issue Jan 8, 2020
Also upgrade pyjnius to the latest release which fixes the long-standing Python
3 string conversion bug (kivy/pyjnius#300). This seems to have changed how
interfaces/subclasses behave, so some new casts and alternative BioFormats API
use was needed. Nothing was needed specifically for the BioFormats 6 change
itself, though.

The pyjnius update supports newer Java versions than 8, so we might as well
upgrade to Java 11.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants