chaquo / chaquopy

Chaquopy: the Python SDK for Android
https://chaquo.com/chaquopy/
MIT License
748 stars 127 forks source link

can I use eval() function ?? #1119

Closed lawiparadise closed 3 months ago

lawiparadise commented 3 months ago

I tried this code, but doesn't working

image

with this error com.chaquo.python.PyException: SystemError: frame does not exist image

please help me...TT

thank you!

mhsmith commented 3 months ago

Please don't post screenshots of text. Copy the text and mark it with triple backticks as shown here.

Also, when posting exceptions, please include the FULL stack trace.

In this case, I think the relevant part of the eval documentation is:

If the locals dictionary is omitted it defaults to the globals dictionary. If both dictionaries are omitted, the expression is executed with the globals and locals in the environment where eval() is called.

Since you're not calling the function from a Python environment, it's unable to do this. You can work around this by passing an empty dict as the second argument of eval.

lawiparadise commented 3 months ago

oh, sorry for the rude question form..

I tried

val py = Python.getInstance()
py.builtins.callAttr("eval", "1+4")

I saw the documentation of eval you gave me, but I think, under code

// kotlin
py.builtins.callAttr("eval", "1+4")   or    py.builtins.callAttr("eval", "'1+4'")

means

# python
eval(1+4)    or   eval('1+4')

so it's not a problem of secondary & third argument I think

and here's FULL stack trace

2024-03-21 00:28:24.668 17481-17481 AndroidRuntime           E  FATAL EXCEPTION: main
                                                                Process: com.example.cloiblockcoding, PID: 17481
                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cloiblockcoding/com.example.cloiblockcoding.activity.PlotActivity}: com.chaquo.python.PyException: SystemError: frame does not exist
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
                                                                    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
                                                                    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:2210)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                    at android.os.Looper.loopOnce(Looper.java:201)
                                                                    at android.os.Looper.loop(Looper.java:288)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:7839)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
                                                                Caused by: com.chaquo.python.PyException: SystemError: frame does not exist
                                                                    at <python>.chaquopy_java.call(chaquopy_java.pyx:354)
                                                                    at <python>.chaquopy_java.Java_com_chaquo_python_PyObject_callAttrThrowsNative(chaquopy_java.pyx:326)
                                                                    at com.chaquo.python.PyObject.callAttrThrowsNative(Native Method)
                                                                    at com.chaquo.python.PyObject.callAttrThrows(PyObject.java:232)
                                                                    at com.chaquo.python.PyObject.callAttr(PyObject.java:221)
                                                                    at com.example.cloiblockcoding.activity.PlotActivity.onCreate(PlotActivity.kt:40)
                                                                    at android.app.Activity.performCreate(Activity.java:8051)
                                                                    at android.app.Activity.performCreate(Activity.java:8031)
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792) 
                                                                    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
                                                                    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:2210) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                    at android.os.Looper.loopOnce(Looper.java:201) 
                                                                    at android.os.Looper.loop(Looper.java:288) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:7839) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 

thank you for your attention!!!

mhsmith commented 3 months ago

so it's not a problem of secondary & third argument I think

I think it is. Try this:

py.builtins.callAttr("eval", "1+4", py.builtins.callAttr("dict"))