Skip to content

Fixed Pyodide transform w/ LiteralMap #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.js.map

Large diffs are not rendered by default.

19 changes: 14 additions & 5 deletions esm/interpreter/pyodide.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { create } from 'gc-hook';
import { RUNNING_IN_WORKER, fetchFiles, fetchJSModules, fetchPaths, writeFile } from './_utils.js';
import { getFormat, loader, registerJSModule, run, runAsync, runEvent } from './_python.js';
import { stdio } from './_io.js';
import { isArray } from '../utils.js';

const type = 'pyodide';
const toJsOptions = { dict_converter: Object.fromEntries };
Expand Down Expand Up @@ -101,11 +102,7 @@ export default {
run: overrideMethod(run),
runAsync: overrideMethod(runAsync),
runEvent: overrideMethod(runEvent),
transform: ({ ffi: { PyProxy } }, value) => (
value instanceof PyProxy ?
value.toJs(toJsOptions) :
value
),
transform: (interpreter, value) => transform.call(interpreter, value),
writeFile: (interpreter, path, buffer, url) => {
const format = getFormat(path, url);
if (format) {
Expand All @@ -118,6 +115,18 @@ export default {
},
};

function transform(value) {
const { ffi: { PyProxy } } = this;
if (value && typeof value === 'object') {
if (value instanceof PyProxy) return value.toJs(toJsOptions);
// I believe this case is for LiteralMap which is not a PyProxy
// and yet it needs to be re-converted to something useful.
if (value instanceof Map) return new Map([...value.entries()]);
if (isArray(value)) return value.map(transform, this);
}
return value;
}

// exposed utility to import packages via polyscript.lazy_py_modules
async function importPackages(packages) {
await this.loadPackage('micropip');
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@
"@webreflection/idb-map": "^0.3.1",
"basic-devtools": "^0.1.6",
"codedent": "^0.1.2",
"coincident": "^2.0.0",
"coincident": "^2.0.1",
"gc-hook": "^0.3.1",
"html-escaper": "^3.0.3",
"proxy-target": "^3.0.2",
"sticky-module": "^0.1.1",
"to-json-callback": "^0.1.1"
},
"worker": {
"blob": "sha256-1s+d/z97T1TW7Z1FKqUH9XkdwJnOr2YgB5K73a94okA="
"blob": "sha256-8aAxB/a1uoulnOjj6nNa2WkuNijTqucD8FzBxAW4XP4="
}
}