Refactor pyexec (#1318)

This is some refactoring I did on the way towards resolving pyscript#1313.
I added a new _run_pyscript Python function which executes the code
inside a context manager that sets the display target. We can then
return a JS object wrapper directly from Python.

I moved the "installation" of the pyscript module to loadInterpreter,
and pyimport pyscript_py there and give it a type. This avoids a bunch
of creating and deleting of proxies for pyscript_py and allows us to
give it a type once and for all.

I also did some minor logic cleanup in a few places.
This commit is contained in:
Hood Chatham
2023-03-29 19:34:24 -07:00
committed by GitHub
parent 689878ce32
commit 854e9d1378
18 changed files with 197 additions and 166 deletions

View File

@@ -6,10 +6,6 @@ import pytest
pyscriptjs = Path(__file__).parents[2]
import pytest # noqa
pyscriptjs = Path(__file__).parents[2]
# add pyscript folder to path
python_source = pyscriptjs / "src" / "python"
sys.path.append(str(python_source))
@@ -18,6 +14,7 @@ sys.path.append(str(python_source))
python_plugins_source = pyscriptjs / "src" / "plugins" / "python"
sys.path.append(str(python_plugins_source))
# patch pyscript module where needed
import pyscript # noqa: E402
import pyscript_plugins_tester as ppt # noqa: E402

View File

@@ -1,4 +0,0 @@
"""Mock module that emulates some of the pyodide js module features for the sake of tests"""
from unittest.mock import Mock
install = Mock()

View File

@@ -1,4 +0,0 @@
"""Mock module that emulates some of the pyodide js module features for the sake of tests"""
from unittest.mock import Mock
create_proxy = Mock(side_effect=lambda x: x)

View File

@@ -1,4 +0,0 @@
"""Mock module that emulates some of the pyodide js module features for the sake of tests"""
from unittest.mock import Mock
create_proxy = Mock(side_effect=lambda x: x)

View File

@@ -1,18 +1,26 @@
import type { AppConfig } from '../../src/pyconfig';
import { InterpreterClient } from '../../src/interpreter_client';
import { RemoteInterpreter } from '../../src/remote_interpreter';
import { CaptureStdio } from '../../src/stdio';
import * as Synclink from 'synclink';
import { describe, beforeAll, afterAll, it, expect } from '@jest/globals';
// We can't import RemoteInterpreter at top level because we need to mock the
// Python package in setup.ts
// But we can import the types at top level.
// TODO: is there a better way to handle this?
import type { RemoteInterpreter } from '../../src/remote_interpreter';
describe('RemoteInterpreter', () => {
let interpreter: InterpreterClient;
let stdio: CaptureStdio = new CaptureStdio();
let RemoteInterpreter;
const { port1, port2 } = new MessageChannel();
beforeAll(async () => {
const SRC = '../pyscriptjs/node_modules/pyodide/pyodide.js';
const config: AppConfig = { interpreters: [{ src: SRC }] };
// Dynamic import of RemoteInterpreter sees our mocked Python package.
({ RemoteInterpreter } = await import('../../src/remote_interpreter'));
const remote_interpreter = new RemoteInterpreter(SRC);
port1.start();
port2.start();
Synclink.expose(remote_interpreter, port2);

View File

@@ -0,0 +1,6 @@
import { jest } from '@jest/globals';
import { directoryManifest } from '../../directoryManifest.mjs';
jest.unstable_mockModule('../../src/python_package', async () => ({
python_package: await directoryManifest('./src/python/'),
}));