You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There's actually no guarantee at all that Python source files exist anywhere on the filesystem, and even if they do they might as well be inside a zip archive or even a Rust executable. Because of this using __file__ for anything but debugging or logging, and in particular loading resources, is a fragile practice and packages like wasmtime-py should avoid it.
In practical terms the header of the generated code that looks like this:
from .intrinsicsimport_decode_utf8, _encode_utf8, _loadimportctypesimportpathlibimportwasmtimetry:
importimportlib.resourcesasimportlib_resourcesimportlib_resources.files() # check if implicit anchor is supportedexceptTypeError:
importimportlib_resourcesclassRoot:
def__init__(self, store: wasmtime.Store) ->None:
file=importlib_resources.files() / ('root.core0.wasm')
module=wasmtime.Module.from_file(store.engine, file.read_bytes())
Note that the importlib.resources.files() (the zero argument form) is very new, freshly added in 3.12, and unfortunately consumers of the bindings will have to add the importlib-resources>=5.10 polyfill as a dependency to support all versions of Python.
(Potentially you could have another fallback that uses __file__ if neither the zero argument form is supported nor the polyfill is installed, but I find a three-deep chain of compatibility shims pretty hard to maintain.)
GIven that the component support is relatively "new" in a sense this seems reasonable to me to add! It'd probably be ok to document this dependency for older Python versions to play better with the ecosystem.
It'd probably be ok to document this dependency for older Python versions to play better with the ecosystem.
I think one really easy way to handle this is to add this dependency to wasmtime itself, since by definition if you have wasmtime.bindgen you also have that. This is the approach I took in #224.
Activity
alexcrichton commentedon Apr 4, 2024
GIven that the component support is relatively "new" in a sense this seems reasonable to me to add! It'd probably be ok to document this dependency for older Python versions to play better with the ecosystem.
whitequark commentedon Apr 4, 2024
I think one really easy way to handle this is to add this dependency to wasmtime itself, since by definition if you have
wasmtime.bindgenyou also have that. This is the approach I took in #224.alexcrichton commentedon Apr 4, 2024
Excellent point!