Skip to content

Support dict_converter and iterating over python objects in js - #138

Merged
DerThorsten merged 1 commit into
emscripten-forge:mainfrom
ianthomas23:dict_converter
Oct 28, 2026
Merged

DerThorsten merged 1 commit into
emscripten-forge:mainfrom
ianthomas23:dict_converter

Conversation

@ianthomas23

Copy link
Copy Markdown
Contributor

This PR fixes two problems that occur when using pyodide_http in xeus-python kernels using SharedArrayBuffer. Essentially this will allow xeus-python kernels to work in JupyterLite (and xeus-runner) deployments that are hosted with cross-origin headers such that SharedArrayBuffer is enabled.

Fixes are based on similar code in pyodide with the help of Claude.

  1. Issue #316, support the dict_converter kwargs in to_js. The converter is passed down the call stack to where it is needed, and dictionary keys and values are recursively converted to JS. Addresses this pyodide_http line:

https://github.com/koenvo/pyodide-http/blob/b5fc794dfd0f6db18304aa9ec4d967e07123b6fc/pyodide_http/_streaming.py#L137

  1. Allow python objects to be iterated in JS. Without this there is an error as a JS symbol is used as a key in a dictionary, which can only accept strings, the error is BindingError: Cannot pass non-string to std::string. The fix is to proxy python iterators so that they behave like JS iterators. Addresses this pyodide_http line:

https://github.com/koenvo/pyodide-http/blob/b5fc794dfd0f6db18304aa9ec4d967e07123b6fc/pyodide_http/_streaming.py#L201

Before this PR, if you create a xeus-python JupyterLite deployment and serve it using cross-origin headers (e.g. via npx static-handler --coi dist/), then the python kernel fails to start with:

Screenshot 2026-10-27 at 15 33 09

The following screenshot shows works using this PR, after rebuilding the pyjs emscripten-forge package using this branch, and then the xeus-python package using the rebuild pyjs-rt:

Screenshot 2026-10-27 at 15 33 18

Note the mamba list output shows the use of the 2 locally-built packages.

Fixes #136.

@DerThorsten
DerThorsten merged commit 3528a1e into emscripten-forge:main Oct 28, 2026
1 check passed
@ianthomas23
ianthomas23 deleted the dict_converter branch October 28, 2026 07:57
DerThorsten added a commit to DerThorsten/pyjs that referenced this pull request Oct 30, 2026
Support dict_converter and iterating over python objects in js (emscripten-forge#138)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

to_js does not accept dict_converter like the pyodide version

2 participants