Skip to content

Flask: Cannot convert bytes object to java.lang.String #464

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

Closed
matsbohlinsson opened this issue Mar 15, 2021 · 5 comments
Closed

Flask: Cannot convert bytes object to java.lang.String #464

matsbohlinsson opened this issue Mar 15, 2021 · 5 comments
Labels

Comments

@matsbohlinsson
Copy link

matsbohlinsson commented Mar 15, 2021

Im trying to run flask. During startup if flask, I get the following runtime error:
TypeError: Cannot convert bytes object to java.lang.String

Seems related to call:click.echo()

 main()
  File "/data/user/0/com.plainembedded.mavicmax/files/chaquopy/AssetFinder/requirements/flaskcode/cli.py", line 59, in main
  File "/data/user/0/com.plainembedded.mavicmax/files/chaquopy/AssetFinder/requirements/click/core.py", line 829, in __call__
  File "/data/user/0/com.plainembedded.mavicmax/files/chaquopy/AssetFinder/requirements/click/core.py", line 782, in main
  File "/data/user/0/com.plainembedded.mavicmax/files/chaquopy/AssetFinder/requirements/click/core.py", line 1066, in invoke
  File "/data/user/0/com.plainembedded.mavicmax/files/chaquopy/AssetFinder/requirements/click/core.py", line 610, in invoke
  File "/data/user/0/com.plainembedded.mavicmax/files/chaquopy/AssetFinder/requirements/flaskcode/cli.py", line 50, in run
  File "/data/user/0/com.plainembedded.mavicmax/files/chaquopy/AssetFinder/requirements/click/utils.py", line 272, in echo
  **File "bootstrap/java/android/stream.py", line 59, in write
  File "class.pxi", line 781, in java.chaquopy.JavaMethod.__call__
  File "conversion.pxi", line 256, in java.chaquopy.p2j**
TypeError: Cannot convert bytes object to java.lang.String

My code:

from flask import Flask
import flaskcode

app = Flask(__name__)
app.config.from_object(flaskcode.default_config)
app.config['FLASKCODE_RESOURCE_BASEPATH'] = '/sdcard/mavicmax/'
app.register_blueprint(flaskcode.blueprint, url_prefix='/flaskcode')

@app.route('/')
def hello():
    return "Hello World!"

if __name__ == '__main__':
    app.run()
@mhsmith mhsmith added the bug label Mar 16, 2021
@mhsmith
Copy link
Member

mhsmith commented Mar 16, 2021

I've run your code and it doesn't reproduce the issue. In fact, it doesn't look like your code even uses flaskcode.cli, so please explain how flaskcode.cli.main is being called.

Also, what version of Chaquopy are you using?

@matsbohlinsson
Copy link
Author

matsbohlinsson commented Mar 16, 2021

Version: classpath 'com.chaquo.python:gradle:9.1.0'

I uploaded the code to cloned version of your examples repo:
https://github.com/matsbohlinsson/chaquopy-hello-test/commits/master

The latest commit in the repo is exactly what I'm running. You should be able to reproduce it, I hope :-)

The last commit causes the stacktrace below:

D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.chaquo.python.hello, PID: 28891
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chaquo.python.hello/hello.MainActivity}: com.chaquo.python.PyException: TypeError: Cannot convert bytes object to java.lang.String
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3308)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3457)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7560)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     Caused by: com.chaquo.python.PyException: TypeError: Cannot convert bytes object to java.lang.String
        at <python>.java.chaquopy.p2j(conversion.pxi:256)
        at <python>.java.chaquopy.JavaMethod.__call__(class.pxi:781)
        at <python>.java.android.stream.write(stream.py:59)
        at <python>.click.utils.echo(utils.py:272)
        at <python>.flask.cli.show_server_banner(cli.py:670)
        at <python>.flask.app.run(app.py:985)
        at <python>.hello.onCreate(hello.py:19)
        at <python>.chaquopy_java.call(chaquopy_java.pyx:380)
        at <python>.chaquopy_java.Java_com_chaquo_python_PyObject_callAttrThrowsNative(chaquopy_java.pyx:352)
        at com.chaquo.python.PyObject.callAttrThrowsNative(Native Method)
        at com.chaquo.python.PyObject.callAttrThrows(PyObject.java:232)
        at com.chaquo.python.PyObject._chaquopyCall(PyObject.java:239)
        at hello.MainActivity.onCreate(MainActivity.java:24)
        at android.app.Activity.performCreate(Activity.java:7894)
        at android.app.Activity.performCreate(Activity.java:7881)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3283)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3457) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:224) 
        at android.app.ActivityThread.main(ActivityThread.java:7560) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
I/Process: Sending signal. PID: 28891 SIG: 9

@mhsmith
Copy link
Member

mhsmith commented Mar 16, 2021

OK, it looks like there's an incompatibility between the way click accesses stdout and the way Chaquopy redirects it to Logcat. We'll fix this in the next version of Chaquopy. Meanwhile, you can work around the problem by adding the following code to the start of your Python file:

import sys
for stream in [sys.stdout, sys.stderr]:
    def write_override(s, write_original=stream.write):
        if not isinstance(s, str):
            raise TypeError()
        return write_original(s)
    stream.write = write_override

@matsbohlinsson
Copy link
Author

Wow it works!!

Thank you, this was a major thing for me!

@mhsmith
Copy link
Member

mhsmith commented Sep 22, 2021

This issue is fixed in Chaquopy 10.0.1. To upgrade, edit your app's top-level build.gradle file and change the version number of com.chaquo.python:gradle.

@mhsmith mhsmith closed this as completed Sep 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants