Skip to content

Python tooling docs fail with NotImplmentedError #84

Closed

Description

@sachitv

I was following the instructions here: https://component-model.bytecodealliance.org/language-support/python.html#running-components-from-python-applications

I have tried using wasmtime from the fork mentioned there (dicej/wasmtime-py) as well as wasmtime 14.0.0 but I encounter failures when running wasmtime.bindgen.
Environment
Ubuntu 22.04 (amd64) also reproduced on Mac OS Sonoma (14.1) (arm64)
Poetry 1.7.0
Python 3.12.0 via pyenv

Command:
python3 -m wasmtime.bindgen add.wasm --out-dir add

Failure:

Traceback (most recent call last):
  File "/home/sachit/Development/learn-componentize/.venv/lib/python3.12/site-packages/wasmtime/_func.py", line 260, in enter_wasm
    yield byref(trap)
  File "/home/sachit/Development/learn-componentize/.venv/lib/python3.12/site-packages/wasmtime/_func.py", line 101, in __call__
    raise WasmtimeError._from_ptr(error)
wasmtime._error.WasmtimeError: error while executing at wasm backtrace:
    0:  0x100 - wit-component:shim!indirect-wasi:cli/terminal-stdin-get-terminal-stdin
    1: 0x254f - wit-component:adapter:wasi_snapshot_preview1!wasi_snapshot_preview1::descriptors::Descriptors::new::ha2fdde51d30a71bd
    2: 0x1640 - wit-component:adapter:wasi_snapshot_preview1!wasi_snapshot_preview1::State::descriptors::h780c546b61bcfc7f
    3: 0x180c - wit-component:adapter:wasi_snapshot_preview1!fd_write
    4:  0x13a - wit-component:shim!adapt-wasi_snapshot_preview1-fd_write
    5: 0x1b080d - <unknown>!wasi::lib_generated::fd_write::hd4964fea612b930f
    6: 0x1acdfa - <unknown>!<std::io::Write::write_fmt::Adapter<T> as core::fmt::Write>::write_str::h375f1d6863bea9df
    7: 0x1ba82b - <unknown>!core::fmt::write::h0eddb54b80b97b9d
    8: 0x1add1c - <unknown>!std::io::Write::write_fmt::h6d46415105134b08
    9: 0x1afaa2 - <unknown>!std::panicking::default_hook::{{closure}}::he04c18047097e21e
   10: 0x1ad3d8 - <unknown>!std::panicking::default_hook::hb03d7fae0dedb715
   11: 0x1b007b - <unknown>!std::panicking::rust_panic_with_hook::hc93abff18edee779
   12: 0x1af73e - <unknown>!std::panicking::begin_panic_handler::{{closure}}::h922bcdd9c6fdedfb
   13: 0x1af6a3 - <unknown>!std::sys_common::backtrace::__rust_end_short_backtrace::h2597d6ecb1d3419e
   14: 0x1afd27 - <unknown>!rust_begin_unwind
   15: 0x1b5689 - <unknown>!core::panicking::panic_fmt::h35d9e7e9c02f9eb5
   16: 0x1b5c5f - <unknown>!core::panicking::panic::h2d50353119445d1c
   17: 0x9662 - <unknown>!bindgen::bindgen::InterfaceGenerator::types::he68d4fe0b27868c4
   18: 0x3ebe - <unknown>!bindgen::bindgen::WasmtimePy::generate::h662240fac89ac3d8
   19: 0x2f9dd - <unknown>!<bindgen::bindings::PythonBindings as bindgen::bindings::Guest>::generate::h8ccba8d7064427e7
   20: 0x2fbe6 - <unknown>!generate

Caused by:
    python exception

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/sachit/Development/learn-componentize/.venv/lib/python3.12/site-packages/wasmtime/bindgen/__main__.py", line 40, in <module>
    main()
  File "/home/sachit/Development/learn-componentize/.venv/lib/python3.12/site-packages/wasmtime/bindgen/__main__.py", line 30, in main
    files = generate(name, contents)
            ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sachit/Development/learn-componentize/.venv/lib/python3.12/site-packages/wasmtime/bindgen/__init__.py", line 144, in generate
    result = root.generate(store, name, component)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sachit/Development/learn-componentize/.venv/lib/python3.12/site-packages/wasmtime/bindgen/generated/__init__.py", line 288, in generate
    ret = self.lift_callee0(caller, ptr, len0, ptr1, len2)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sachit/Development/learn-componentize/.venv/lib/python3.12/site-packages/wasmtime/_func.py", line 91, in __call__
    with enter_wasm(store) as trap:
  File "/home/sachit/.pyenv/versions/3.12.0/lib/python3.12/contextlib.py", line 155, in __exit__
    self.gen.throw(value)
  File "/home/sachit/Development/learn-componentize/.venv/lib/python3.12/site-packages/wasmtime/_func.py", line 266, in enter_wasm
    maybe_raise_last_exn()
  File "/home/sachit/Development/learn-componentize/.venv/lib/python3.12/site-packages/wasmtime/_func.py", line 276, in maybe_raise_last_exn
    raise exn
  File "/home/sachit/Development/learn-componentize/.venv/lib/python3.12/site-packages/wasmtime/_func.py", line 184, in trampoline
    pyresults = func(*pyparams)
                ^^^^^^^^^^^^^^^
  File "/home/sachit/Development/learn-componentize/.venv/lib/python3.12/site-packages/wasmtime/bindgen/generated/__init__.py", line 200, in lowering16_callee
    ret = import_object.terminal_stdin.get_terminal_stdin()
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sachit/Development/learn-componentize/.venv/lib/python3.12/site-packages/wasmtime/bindgen/__init__.py", line 95, in get_terminal_stdin
    raise NotImplementedError
NotImplementedError

Activity

  1. vados-cosmonic commented on Oct 31, 2024

    @vados-cosmonic
    Collaborator

    tl;dr - use --stub-wasi when running componentize-py componentize to exclude WASI bindings, see the "EDIT" below

    Hey @sachitv are you still seeing this problem with the latest documentation & tooling ?

    The examples do work for me (likely thanks to the previous work done fixing the docs) -- though I used uv while I was doing my setup, and the commands have changed a little bit with componentize-py.

    Generally, with the files set up like so:

    component-py
    ├── component.py
    ├── pyproject.toml
    └── wit
        └── world.wit
    
    3 directories, 7 files
    

    (Files in the above files match exactly what was written in the language guide)

    I was able to generate bindings (optional!):

    uv tool componentize-py bindings .

    This step is optional, but it will serve as a peek behind the curtain, as you can see the generated code.

    To build a component, you can run

    uv tool componentize-py componentize example -o component.wasm

    This will build the example and output it to component.wasm, after which you can run it with the example host:

    cargo run --release -- 1 2 /path/to/your/component.wasm

    [EDIT] I just noticed that your problem was with the running we do need to update the docs to include the --stub-wasi option when calling componentize-py -- this is what does the part noted in the docs:

    # Get an add component that does not import the WASI CLI world
    

    The documentation doesn't go far enough in making it clear HOW to do this. The correct incantation is:

    uv tool run componentize-py componentize component --stub-wasi --output py.wasm
    

    After that, you can generate bindings with:

    uv run -m wasmtime.bindgen py.wasm --out-dir add
    

    With code like the following, the example will work:

    from add import Root
    from wasmtime import Config, Engine, Store
    
    def main():
        config = Config()
        config.cache = True
        engine = Engine(config)
        store = Store(engine)
        component = Root(store)
        print("1 + 2 = ", component.add(store, 1, 2))
    
    if __name__ == '__main__':
        main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions