oracle / graalpython

A Python 3 implementation built on GraalVM
Other
1.2k stars 103 forks source link

Add support for the 'pack' builtin #349

Closed andrewstein closed 1 year ago

andrewstein commented 1 year ago

The following java code:

    Context context = Context.create("python");
    context.eval("python", "from struct import pack");

Throws an error:

ImportError: cannot import name 'pack' from 'struct' (/Library/Java/JavaVirtualMachines/graalvm-ce-java11-22.3.1/Contents/Home/languages/python/lib-python/3/struct.py)

Please add support for the pack builtin....

FYI

$ uname -m -r -s -v
Darwin 22.6.0 Darwin Kernel Version 22.6.0: Wed Jul  5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000 arm64
$ java -version
openjdk version "11.0.18" 2023-01-17
OpenJDK Runtime Environment GraalVM CE 22.3.1 (build 11.0.18+10-jvmci-22.3-b13)
OpenJDK 64-Bit Server VM GraalVM CE 22.3.1 (build 11.0.18+10-jvmci-22.3-b13, mixed mode)
 $ graalpy --version
GraalVM Python 3.8.5 (GraalVM CE Native 22.3.1)
andrewstein commented 1 year ago

NOTE: I have the same issue with java 17

msimacek commented 1 year ago

Hi, struct module is supported, but it is implemented as a C module, so you need to allow native access and IO to be able to import it. Create your context with:

Context.newBuilder("python").allowNativeAccess(true).allowIO(IOAccess.ALL).build()
andrewstein commented 1 year ago

Thank you @msimacek ,

What you wrote put me on the right path. I did manage to ge it to work eventually.

allowIo() receives a boolean argument. When I tried

Context context = Context.newBuilder("python").allowNativeAccess(true).allowIO(true).build();

I got a SystemError: Language 'python' not found.

What did work for me is

Context context = Context.newBuilder("python").allowAllAccess(true).build()

So there is something else that I need beyond allowNativeAccess and allowIO

msimacek commented 1 year ago

Ah, the code I wrote was for GraalVM 23.0. Can you update?

andrewstein commented 1 year ago

Not immediately.

On my personal machine, I get graal via brew on macOs, where the latest is 22.3.1. I can circumvent this on my machine to try it out, but eventually I will have to update the company wide toolchain (which is actually on 21.3.0 on Linux) as well. So, updating is not trivial, but can be done. It will take a week or two. So, the path of least resistance at the moment is a 21.3.0 solution over a "better" 23.0 solution. I will try out your suggestion, but it will take some time. Thank you very much for the help.

msimacek commented 1 year ago

I think in 21.3.0 it also required polyglot access and host array access, but I'm not sure anymore. But using allowAllAccess is fine as well if you don't need to sandbox the code. I'll close this issue, since we found a solution.