8000 fix access to interpreter globals without awaits by madhur-tandon · Pull Request #1529 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content

fix access to interpreter globals without awaits #1529

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
Jun 14, 2023
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
6 changes: 4 additions & 2 deletions pyscriptjs/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export let runtime;
export class PyScriptApp {
config: AppConfig;
interpreter: InterpreterClient;
unwrapped_remote: RemoteInterpreter;
readyPromise: Promise<void>;
PyScript: ReturnType<typeof make_PyScript>;
plugins: PluginManager;
Expand Down Expand Up @@ -139,10 +140,10 @@ export class PyScriptApp {
await this.plugins.configure(this.config);
this.plugins.beforeLaunch(this.config);
await this.loadInterpreter();
interpreter = this.interpreter;
interpreter = this.unwrapped_remote;
// TODO: This is for backwards compatibility, it should be removed
// when we finish the deprecation cycle of `runtime`
runtime = this.interpreter;
runtime = this.unwrapped_remote;
}

// lifecycle (2)
Expand Down Expand Up @@ -193,6 +194,7 @@ export class PyScriptApp {
logger.info('Starting the interpreter in the main thread');
// this is basically equivalent to worker_initialize()
const remote_interpreter = new RemoteInterpreter(interpreter_cfg.src);
this.unwrapped_remote = remote_interpreter;
const { port1, port2 } = new Synclink.FakeMessageChannel() as unknown as MessageChannel;
port1.start();
port2.start();
Expand Down
22 changes: 13 additions & 9 deletions pyscriptjs/tests/integration/test_interpreter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from .support import PyScriptTest
from .support import PyScriptTest, skip_worker


class TestInterpreterAccess(PyScriptTest):
"""Test accessing Python objects from JS via pyscript.interpreter"""

@skip_worker("WONTFIX: used without synclink to avoid awaits")
def test_interpreter_python_access(self):
self.pyscript_run(
"""
Expand All @@ -17,9 +18,9 @@ def py_func():

self.run_js(
"""
const x = await pyscript.interpreter.globals.get('x');
const py_func = await pyscript.interpreter.globals.get('py_func');
const py_func_res = await py_func();
const x = pyscript.interpreter.globals.get('x');
const py_func = pyscript.interpreter.globals.get('py_func');
const py_func_res = py_func();
console.log(`x is ${x}`);
console.log(`py_func() returns ${py_func_res}`);
"""
Expand All @@ -29,13 +30,14 @@ def py_func():
"py_func() returns 2",
]

@skip_worker("WONTFIX: used without synclink")
def test_interpreter_script_execution(self):
"""Test running Python code from js via pyscript.interpreter"""
self.pyscript_run("")

self.run_js(
"""
const interface = pyscript.interpreter._remote.interface;
const interface = pyscript.interpreter.interface;
await interface.runPython('print("Interpreter Ran This")');
"""
)
Expand All @@ -46,13 +48,14 @@ def test_interpreter_script_execution(self):
py_terminal = self.page.wait_for_selector("py-terminal")
assert py_terminal.text_content() == expected_message

@skip_worker("WONTFIX: used without synclink")
def test_backward_compatibility_runtime_script_execution(self):
"""Test running Python code from js via pyscript.runtime"""
self.pyscript_run("")

self.run_js(
"""
const interface = pyscript.runtime._remote.interpreter;
const interface = pyscript.runtime.interpreter;
await interface.runPython('print("Interpreter Ran This")');
"""
)
Expand All @@ -63,6 +66,7 @@ def test_backward_compatibility_runtime_script_execution(self):
py_terminal = self.page.wait_for_selector("py-terminal")
assert py_terminal.text_content() == expected_message

@skip_worker("WONTFIX: used without synclink to avoid awaits")
def test_backward_compatibility_runtime_python_access(self):
"""Test accessing Python objects from JS via pyscript.runtime"""
self.pyscript_run(
Expand All @@ -77,9 +81,9 @@ def py_func():

self.run_js(
"""
const x = await pyscript.interpreter.globals.get('x');
const py_func = await pyscript.interpreter.globals.get('py_func');
const py_func_res = await py_func();
const x = pyscript.runtime.globals.get('x');
const py_func = pyscript.runtime.globals.get('py_func');
const py_func_res = py_func();
console.log(`x is ${x}`);
console.log(`py_func() returns ${py_func_res}`);
"""
Expand Down
0