gwchq-textjam 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,959 +1,959 @@
1
- /* eslint-disable @typescript-eslint/no-empty-function */
2
- /* global globalThis, importScripts, loadPyodide, SharedArrayBuffer, Atomics, pygal */
3
-
4
- function toAbsoluteFromOrigin(url) {
5
- if (url.startsWith("http") || url.startsWith("/")) return url;
6
- // eslint-disable-next-line no-restricted-globals
7
- return new URL(url, self.location.origin).href;
8
- }
9
-
10
- // removing submodules (matplotlib.pyplot = matplotlib)
11
- const normalizeImportName = (name) => name.split(".")[0];
12
-
13
- const WORKING_DIR = "/home/pyodide";
14
-
15
- const toStructuredCloneable = (value) => {
16
- if (value == null) return value;
17
-
18
- if (
19
- typeof value === "string" ||
20
- typeof value === "number" ||
21
- typeof value === "boolean"
22
- ) {
23
- return value;
24
- }
25
-
26
- if (value instanceof Uint8Array) {
27
- return value.slice();
28
- }
29
-
30
- if (ArrayBuffer.isView(value)) {
31
- return new Uint8Array(
32
- value.buffer.slice(value.byteOffset, value.byteOffset + value.byteLength),
33
- );
34
- }
35
-
36
- if (value instanceof ArrayBuffer) {
37
- return value.slice(0);
38
- }
39
-
40
- if (value instanceof Map) {
41
- return Object.fromEntries(
42
- [...value.entries()].map(([k, v]) => [k, toStructuredCloneable(v)]),
43
- );
44
- }
45
-
46
- if (Array.isArray(value)) {
47
- return value.map(toStructuredCloneable);
48
- }
49
-
50
- if (typeof value?.toJs === "function") {
51
- try {
52
- const converted = value.toJs({ dict_converter: Object.fromEntries });
53
- return toStructuredCloneable(converted);
54
- } catch (_) {
55
- try {
56
- const converted = value.toJs();
57
- return toStructuredCloneable(converted);
58
- } catch (_) {
59
- return String(value);
60
- }
61
- }
62
- }
63
-
64
- if (typeof value === "object") {
65
- const result = {};
66
- for (const [key, val] of Object.entries(value)) {
67
- result[key] = toStructuredCloneable(val);
68
- }
69
- return result;
70
- }
71
-
72
- return String(value);
73
- };
74
-
75
- // Nest the PyodideWorker function inside a globalThis object so we control when its initialised.
76
- const PyodideWorker = () => {
77
- let assets;
78
- let packageApiUrl;
79
-
80
- const handleInit = (data) => {
81
- assets = data.assets;
82
- packageApiUrl = data.packageApiUrl;
83
-
84
- // Import scripts dynamically based on the environment
85
- console.log("PyodideWorker", "importing scripts");
86
- importScripts(toAbsoluteFromOrigin(assets.pygalUrl));
87
-
88
- assets.pyodideBaseUrl = `${packageApiUrl}/pyodide.js`;
89
- importScripts(toAbsoluteFromOrigin(assets.pyodideBaseUrl));
90
-
91
- initialisePyodide(data);
92
- };
93
-
94
- const supportsAllFeatures = typeof SharedArrayBuffer !== "undefined";
95
-
96
- // eslint-disable-next-line no-restricted-globals
97
- if (!supportsAllFeatures && name !== "incremental-features") {
98
- console.warn(
99
- [
100
- "The code editor will not be able to capture standard input or stop execution because these HTTP headers are not set:",
101
- " - Cross-Origin-Opener-Policy: same-origin",
102
- " - Cross-Origin-Embedder-Policy: require-corp",
103
- "",
104
- "If your app can cope with or without these features, please initialize the web worker with { name: 'incremental-features' } to silence this warning.",
105
- "You can then check for the presence of { stdinBuffer, interruptBuffer } in the handleLoaded message to check whether these features are supported.",
106
- "",
107
- "If you definitely need these features, either configure your server to respond with the HTTP headers above, or register a service worker.",
108
- "Once the HTTP headers are set, the browser will block cross-domain resources so you will need to add 'crossorigin' to <script> and other tags.",
109
- "You may wish to scope the HTTP headers to only those pages that host the code editor to make the browser restriction easier to deal with.",
110
- "",
111
- "Please refer to these code snippets for registering a service worker:",
112
- " - https://github.com/RaspberryPiFoundation/python-execution-prototypes/blob/fd2c50e032cba3bb0e92e19a88eb62e5b120fe7a/pyodide/index.html#L92-L98",
113
- " - https://github.com/RaspberryPiFoundation/python-execution-prototypes/blob/fd2c50e032cba3bb0e92e19a88eb62e5b120fe7a/pyodide/serviceworker.js",
114
- ].join("\n"),
115
- );
116
- }
117
- let pyodide, pyodidePromise, stdinBuffer, interruptBuffer, stopped;
118
- let currentRunId = null;
119
- let stdinStrategy = "sab";
120
- let supportsJspi = false;
121
- let stdinFallbackConfig = null;
122
- const STDIN_CANCELLED_RESPONSE_CODE = 499;
123
- const STDIN_TIMEOUT_RESPONSE_CODE = 408;
124
- const STDIN_ABORTED_RESPONSE_CODE = 410;
125
- const STDIN_ROUTE_NOT_FOUND_RESPONSE_CODE = 404;
126
- // HTTP 204 No Content from the stdin Service Worker signals end-of-file —
127
- // distinct from a cancellation/interrupt so input() raises EOFError and the
128
- // user's `try/except EOFError` blocks keep working under the sync-XHR fallback.
129
- const STDIN_EOF_RESPONSE_CODE = 204;
130
- /** When true, input() uses postMessage + run_sync(getInputAsync) instead of setStdin (requires JSPI). */
131
- let useMessageStdin = false;
132
- /** When true, input() uses sync XHR via the Service Worker fallback. */
133
- let useSyncXhrStdin = false;
134
- /** True when current input request was cancelled from outside the worker. */
135
- let stdinCancelled = false;
136
- /** Used when SharedArrayBuffer is unavailable: resolve for the pending input() call. */
137
- let pendingStdinResolve = null;
138
- // Until Pyodide is fully initialised, keep stdout/stderr in the dev console only.
139
- // After initialisation, route them to the UI via postMessage.
140
- let userStdStreamsEnabled = false;
141
- // When true, suppress internal library / package loader logs from the UI console.
142
- let suppressInternalStdStreams = false;
143
-
144
- const onmessage = async ({ data }) => {
145
- if (data.method !== "init") {
146
- pyodide = await pyodidePromise;
147
- }
148
-
149
- switch (data.method) {
150
- case "stdinResponse": {
151
- if (pendingStdinResolve) {
152
- let content = data.ctrlD ? null : data.content ?? "";
153
- if (content && content.endsWith("\n")) {
154
- content = content.slice(0, -1);
155
- }
156
- pendingStdinResolve(content);
157
- pendingStdinResolve = null;
158
- }
159
- break;
160
- }
161
- case "init": {
162
- handleInit(data);
163
- break;
164
- }
165
- case "createDirectories": {
166
- if (Array.isArray(data.dirs)) {
167
- for (const dir of data.dirs) {
168
- try {
169
- pyodide.FS.mkdirTree(`${WORKING_DIR}/${dir}`);
170
- } catch (e) {
171
- console.error(`Failed to create directory ${dir}:`, e);
172
- }
173
- }
174
- }
175
- break;
176
- }
177
- case "writeFile": {
178
- const encoder = new TextEncoder();
179
- pyodide.FS.writeFile(
180
- `${WORKING_DIR}/${data.filename}`,
181
- encoder.encode(data.content),
182
- );
183
- break;
184
- }
185
- case "runPython":
186
- currentRunId = data.runId || null;
187
- stdinCancelled = false;
188
- runPython(data.python, data.userModuleNames);
189
- break;
190
- case "stopPython": {
191
- // Mark as stopped so future checks can raise KeyboardInterrupt.
192
- stopped = true;
193
- // If Python is currently blocked in input() via run_sync(getInputAsync),
194
- // resolve the pending stdin promise with EOF so execution can unwind
195
- // and clearPyodideData can run.
196
- if (pendingStdinResolve) {
197
- pendingStdinResolve(null);
198
- pendingStdinResolve = null;
199
- }
200
- stdinCancelled = true;
201
- break;
202
- }
203
- default: {
204
- throw new Error(`Unsupported method: ${data.method}`);
205
- }
206
- }
207
- };
208
-
209
- // eslint-disable-next-line no-restricted-globals
210
- addEventListener("message", async (event) => {
211
- onmessage(event);
212
- });
213
-
214
- const runPython = async (python, userModuleNames) => {
215
- stopped = false;
216
-
217
- if (stdinStrategy === "unavailable") {
218
- postMessage({
219
- method: "handleError",
220
- file: "main.py",
221
- line: "",
222
- mistake: "",
223
- type: "RuntimeError",
224
- info: "Python input fallback is unavailable. Please enable and activate the stdin Service Worker, then reload the page.",
225
- });
226
- await clearPyodideData(userModuleNames);
227
- return;
228
- }
229
-
230
- // When stdin uses postMessage (no SharedArrayBuffer), run_sync() in input() requires
231
- // runPythonAsync so that JSPI stack switching can suspend until the main thread sends stdinResponse.
232
- const runUserCode = useMessageStdin
233
- ? () => pyodide.runPythonAsync(python)
234
- : () => pyodide.runPython(python);
235
-
236
- try {
237
- await withSupportForPackages(python, async () => {
238
- await runUserCode();
239
- });
240
- } catch (error) {
241
- const isStdinControlError =
242
- error?.message === "PYODIDE_STDIN_CANCELLED" ||
243
- error?.message === "PYODIDE_STDIN_TIMEOUT" ||
244
- error?.message === "PYODIDE_STDIN_ABORTED";
245
-
246
- if (stdinCancelled || isStdinControlError) {
247
- postMessage({
248
- method: "handleError",
249
- file: "main.py",
250
- line: "",
251
- mistake: "",
252
- type: "KeyboardInterrupt",
253
- info: "Execution interrupted",
254
- });
255
- await clearPyodideData(userModuleNames);
256
- return;
257
- }
258
-
259
- if (!(error instanceof pyodide.ffi.PythonError)) {
260
- postMessage({
261
- method: "handleError",
262
- file: "main.py",
263
- line: "",
264
- mistake: "",
265
- type: "RuntimeError",
266
- info:
267
- error?.message ||
268
- "Python execution failed while reading stdin fallback input.",
269
- });
270
- await clearPyodideData(userModuleNames);
271
- return;
272
- }
273
- const parsed = parsePythonError(error);
274
- // Stop resolves stdin with EOF so input() raises EOFError; show as interrupt, not error.
275
- if (stopped && parsed.type === "EOFError") {
276
- postMessage({
277
- method: "handleError",
278
- ...parsed,
279
- type: "KeyboardInterrupt",
280
- info: "Execution interrupted",
281
- });
282
- } else {
283
- postMessage({ method: "handleError", ...parsed });
284
- }
285
- }
286
-
287
- await clearPyodideData(userModuleNames);
288
- };
289
-
290
- const checkIfStopped = () => {
291
- if (stopped) {
292
- throw new pyodide.ffi.PythonError("KeyboardInterrupt");
293
- }
294
- };
295
-
296
- const withSupportForPackages = async (
297
- python,
298
- runPythonFn = async () => {},
299
- ) => {
300
- // Suppress internal loader output (e.g. "Loading pyodide-http") from the
301
- // user console while resolving imports and loading packages.
302
- suppressInternalStdStreams = true;
303
-
304
- // `find_imports` returns a PyProxy whose .toJs() copies the contents but
305
- // leaves the underlying Python object alive. Destroy it explicitly so JS
306
- // GC can't try to finalise it later from a context where the GIL isn't
307
- // held (which would surface as a NoGilError).
308
- const importsProxy = pyodide._api.pyodide_code.find_imports(python);
309
- const imports = importsProxy.toJs();
310
- importsProxy.destroy();
311
-
312
- await pyodide.runPythonAsync(`
313
- import builtins
314
- if hasattr(builtins, "_original_open"):
315
- builtins.open = builtins._original_open
316
- `);
317
-
318
- // Load packages sequentially rather than in Promise.all. Each
319
- // `loadDependency` does multiple `await` hops (vendored .before(),
320
- // loadPackage, micropip.install, …). When several run concurrently their
321
- // await points interleave and Pyodide ends up servicing multiple in-flight
322
- // package loads — that interleaving is the trigger for the NoGilError seen
323
- // when complex projects load matplotlib's dependency tree. Sequential
324
- // loading costs a few ms on cold start but matches how `loadPackage`
325
- // already serialises its own work internally.
326
- // for (const name of imports) {
327
- // checkIfStopped();
328
- // await loadDependency(name);
329
- // }
330
- await Promise.all(imports.map((name) => loadDependency(name)));
331
-
332
- checkIfStopped();
333
- await pyodide.loadPackagesFromImports(python);
334
-
335
- checkIfStopped();
336
- await pyodide.runPythonAsync(
337
- `
338
- import basthon
339
- import builtins
340
- import os
341
-
342
- MAX_FILES = 100
343
- MAX_FILE_SIZE = 8500000
344
- PROJECT_ROOT = os.path.abspath("${WORKING_DIR}")
345
-
346
- if not hasattr(builtins, "_original_open"):
347
- builtins._original_open = builtins.open
348
-
349
- ORIGINAL_OPEN = builtins._original_open
350
-
351
- def _is_project_file(filename):
352
- abs_path = os.path.abspath(filename)
353
- return abs_path == PROJECT_ROOT or abs_path.startswith(PROJECT_ROOT + os.sep)
354
-
355
- def _to_project_relative(filename):
356
- abs_path = os.path.abspath(filename)
357
- return os.path.relpath(abs_path, PROJECT_ROOT)
358
-
359
- def _custom_open(filename, mode="r", *args, **kwargs):
360
- import os
361
- abs_path = os.path.abspath(filename)
362
-
363
- if "x" in mode and os.path.exists(abs_path):
364
- raise FileExistsError(f"File '{filename}' already exists")
365
-
366
- is_text_write = ("w" in mode or "a" in mode or "x" in mode) and "b" not in mode
367
- is_project_file = _is_project_file(abs_path)
368
-
369
- if is_text_write and is_project_file:
370
- if len(os.listdir(PROJECT_ROOT)) > MAX_FILES and not os.path.exists(abs_path):
371
- raise OSError(f"File system limit reached, no more than {MAX_FILES} files allowed")
372
- class CustomFile:
373
- def __init__(self, filename):
374
- self.filename = filename
375
- self.content = ""
376
-
377
- def write(self, content):
378
- self.content += content
379
- if len(self.content) > MAX_FILE_SIZE:
380
- raise OSError(f"File '{self.filename}' exceeds maximum file size of {MAX_FILE_SIZE} bytes")
381
- with ORIGINAL_OPEN(self.filename, mode, *args, **kwargs) as f:
382
- f.write(self.content)
383
- basthon.kernel.write_file({
384
- "filename": _to_project_relative(self.filename),
385
- "content": self.content,
386
- "mode": mode
387
- })
388
-
389
- def close(self):
390
- pass
391
-
392
- def __enter__(self):
393
- return self
394
-
395
- def __exit__(self, exc_type, exc_val, exc_tb):
396
- self.close()
397
-
398
- return CustomFile(abs_path)
399
- else:
400
- return ORIGINAL_OPEN(filename, mode, *args, **kwargs)
401
-
402
- # Override the built-in open function
403
- builtins.open = _custom_open
404
- `,
405
- { filename: "__custom_open__.py" },
406
- );
407
-
408
- // Re-enable user-visible stdout / stderr for the actual user code run.
409
- suppressInternalStdStreams = false;
410
-
411
- await runPythonFn();
412
-
413
- for (let name of imports) {
414
- checkIfStopped();
415
- const pkgName = normalizeImportName(name);
416
- await vendoredPackages[pkgName]?.after();
417
- }
418
- };
419
-
420
- const loadDependency = async (name) => {
421
- checkIfStopped();
422
-
423
- const pkgName = normalizeImportName(name);
424
- // If the import is for another user file then open it and load its dependencies.
425
- if (pyodide.FS.readdir(WORKING_DIR).includes(`${pkgName}.py`)) {
426
- const fileContent = pyodide.FS.readFile(`${WORKING_DIR}/${pkgName}.py`, {
427
- encoding: "utf8",
428
- });
429
- await withSupportForPackages(fileContent);
430
- return;
431
- }
432
-
433
- // If the import is for a vendored package then run its .before() hook.
434
- const vendoredPackage = vendoredPackages[pkgName];
435
- await vendoredPackage?.before();
436
- if (vendoredPackage) {
437
- return;
438
- }
439
-
440
- // If the import is for a module built into Python then do nothing.
441
- // The PyProxy is only used as a presence check, so destroy it immediately
442
- // — leaking it leaves a Python ref for JS GC to finalise later, which
443
- // raises NoGilError when GC runs outside a GIL-held context.
444
- try {
445
- const pythonModule = pyodide.pyimport(pkgName);
446
- if (pythonModule) {
447
- pythonModule.destroy();
448
- return;
449
- }
450
- } catch (_) {}
451
-
452
- // If the import is for a package built into Pyodide then load it.
453
- // Built-ins: https://pyodide.org/en/stable/usage/packages-in-pyodide.html
454
- try {
455
- await pyodide.loadPackage(pkgName);
456
-
457
- const pyodidePackage = pyodide.pyimport(pkgName);
458
- if (pyodidePackage) {
459
- pyodidePackage.destroy();
460
- return;
461
- }
462
- } catch (_) {}
463
-
464
- // Ensure micropip is loaded which can fetch packages from PyPi.
465
- // See: https://pyodide.org/en/stable/usage/loading-packages.html
466
- if (!pyodide.micropip) {
467
- await pyodide.loadPackage("micropip");
468
- pyodide.micropip = pyodide.pyimport("micropip");
469
- }
470
-
471
- // If the import is for a PyPi package then load it.
472
- // Otherwise, don't error now so that we get an error later from Python.
473
- await pyodide.micropip.install(pkgName).catch(() => {});
474
- };
475
-
476
- const vendoredPackages = {
477
- // Support for https://pypi.org/project/py-enigma/ due to package not having a whl file on PyPi.
478
- enigma: {
479
- before: async () => {
480
- await pyodide.loadPackage(toAbsoluteFromOrigin(assets.enigmaWhlUrl));
481
- },
482
- after: () => {},
483
- },
484
- turtle: {
485
- before: async () => {
486
- pyodide.registerJsModule("basthon", fakeBasthonPackage);
487
- await pyodide.loadPackage(toAbsoluteFromOrigin(assets.turtleWhlUrl));
488
- },
489
- after: () =>
490
- pyodide.runPython(`
491
- import turtle
492
- import basthon
493
-
494
- svg_dict = turtle.Screen().show_scene()
495
- basthon.kernel.display_event({ "display_type": "turtle", "content": svg_dict })
496
- turtle.restart()
497
- `),
498
- },
499
- p5: {
500
- before: async () => {
501
- pyodide.registerJsModule("basthon", fakeBasthonPackage);
502
- await pyodide.loadPackage([
503
- "setuptools",
504
- toAbsoluteFromOrigin(assets.p5WhlUrl),
505
- ]);
506
- },
507
- after: () => {},
508
- },
509
- pygal: {
510
- before: () => {
511
- pyodide.registerJsModule("pygal", { ...pygal });
512
- pygal.config.renderChart = (content) => {
513
- postMessage({ method: "handleVisual", origin: "pygal", content });
514
- };
515
- },
516
- after: () => {},
517
- },
518
- matplotlib: {
519
- before: async () => {
520
- pyodide.registerJsModule("basthon", fakeBasthonPackage);
521
- // Patch the document object to prevent matplotlib from trying to render. Since we are running in a web worker,
522
- // the document object is not available. We will instead capture the image and send it back to the main thread.
523
- pyodide.runPython(`
524
- import js
525
-
526
- class __DummyDocument__:
527
- def __init__(self, *args, **kwargs) -> None:
528
- return
529
- def __getattr__(self, __name: str):
530
- return __DummyDocument__
531
- js.document = __DummyDocument__()
532
- `);
533
- await pyodide.loadPackage("matplotlib")?.catch(() => {});
534
- let pyodidePackage;
535
- try {
536
- pyodidePackage = pyodide.pyimport("matplotlib");
537
- } catch (_) {}
538
- if (pyodidePackage) {
539
- // Destroy the throwaway proxy before running follow-up Python — the
540
- // proxy is only used as a "did matplotlib actually load?" gate, and
541
- // leaking it would hand a Python ref to JS GC.
542
- pyodidePackage.destroy();
543
- pyodide.runPython(`
544
- import matplotlib.pyplot as plt
545
- import io
546
- import basthon
547
-
548
- def show_chart():
549
- bytes_io = io.BytesIO()
550
- plt.savefig(bytes_io, format='jpg')
551
- bytes_io.seek(0)
552
- basthon.kernel.display_event({ "display_type": "matplotlib", "content": bytes_io.read() })
553
- plt.show = show_chart
554
- `);
555
- return;
556
- }
557
- },
558
- after: () => {
559
- pyodide.runPython(`
560
- import matplotlib.pyplot as plt
561
- plt.clf()
562
- `);
563
- },
564
- },
565
- seaborn: {
566
- before: async () => {
567
- pyodide.registerJsModule("basthon", fakeBasthonPackage);
568
- // Patch the document object to prevent matplotlib from trying to render. Since we are running in a web worker,
569
- // the document object is not available. We will instead capture the image and send it back to the main thread.
570
- pyodide.runPython(`
571
- import js
572
-
573
- class __DummyDocument__:
574
- def __init__(self, *args, **kwargs) -> None:
575
- return
576
- def __getattr__(self, __name: str):
577
- return __DummyDocument__
578
- js.document = __DummyDocument__()
579
- `);
580
-
581
- // Ensure micropip is loaded which can fetch packages from PyPi.
582
- // See: https://pyodide.org/en/stable/usage/loading-packages.html
583
- if (!pyodide.micropip) {
584
- await pyodide.loadPackage("micropip");
585
- pyodide.micropip = pyodide.pyimport("micropip");
586
- }
587
-
588
- // If the import is for a PyPi package then load it.
589
- // Otherwise, don't error now so that we get an error later from Python.
590
- await pyodide.micropip.install("seaborn").catch(() => {});
591
- },
592
- after: () => {
593
- pyodide.runPython(`
594
- import matplotlib.pyplot as plt
595
- import io
596
- import basthon
597
-
598
- def is_plot_empty():
599
- fig = plt.gcf()
600
- for ax in fig.get_axes():
601
- # Check if the axes contain any lines, patches, collections, etc.
602
- if ax.lines or ax.patches or ax.collections or ax.images or ax.texts:
603
- return False
604
- return True
605
-
606
- if not is_plot_empty():
607
- bytes_io = io.BytesIO()
608
- plt.savefig(bytes_io, format='jpg')
609
- bytes_io.seek(0)
610
- basthon.kernel.display_event({ "display_type": "matplotlib", "content": bytes_io.read() })
611
-
612
- plt.clf()
613
- `);
614
- },
615
- },
616
- plotly: {
617
- before: async () => {
618
- if (!pyodide.micropip) {
619
- await pyodide.loadPackage("micropip");
620
- pyodide.micropip = pyodide.pyimport("micropip");
621
- }
622
-
623
- // If the import is for a PyPi package then load it.
624
- // Otherwise, don't error now so that we get an error later from Python.
625
- await pyodide.micropip.install("plotly").catch(() => {});
626
- await pyodide.micropip.install("pandas").catch(() => {});
627
- pyodide.registerJsModule("basthon", fakeBasthonPackage);
628
- pyodide.runPython(`
629
- import plotly.graph_objs as go
630
-
631
- def _hacked_show(self, *args, **kwargs):
632
- basthon.kernel.display_event({
633
- "display_type": "plotly",
634
- "content": self.to_json()
635
- })
636
-
637
- go.Figure.show = _hacked_show
638
- `);
639
- },
640
- after: () => {},
641
- },
642
- };
643
-
644
- const fakeBasthonPackage = {
645
- kernel: {
646
- display_event: (event) => {
647
- let payload;
648
- try {
649
- payload = event.toJs({ dict_converter: Object.fromEntries });
650
- } catch (_) {
651
- payload = event.toJs();
652
- }
653
-
654
- const origin = String(payload.display_type);
655
- const content = toStructuredCloneable(payload.content);
656
-
657
- postMessage({ method: "handleVisual", origin, content });
658
- },
659
- write_file: (event) => {
660
- let payload;
661
- try {
662
- payload = event.toJs({ dict_converter: Object.fromEntries });
663
- } catch (_) {
664
- payload = event.toJs();
665
- }
666
-
667
- const filename = String(payload.filename);
668
- const content =
669
- typeof payload.content === "string"
670
- ? payload.content
671
- : toStructuredCloneable(payload.content);
672
- const mode = String(payload.mode);
673
-
674
- postMessage({ method: "handleFileWrite", filename, content, mode });
675
- },
676
- locals: () => pyodide.runPython("globals()"),
677
- /**
678
- * Returns a Promise that resolves with the next line of stdin when the main thread
679
- * sends stdinResponse. Resolves with null on EOF (e.g. Ctrl+D).
680
- * Used when SharedArrayBuffer is unavailable (no COOP/COEP) so we cannot block via Atomics.wait.
681
- * Requires JSPI and runPythonAsync() for run_sync() to work.
682
- */
683
- getInputAsync: () => {
684
- const promise = new Promise((resolve) => {
685
- pendingStdinResolve = resolve;
686
- });
687
- postMessage({ method: "handleInput" });
688
- return promise;
689
- },
690
- },
691
- };
692
-
693
- const readInputViaSyncXhr = (runId) => {
694
- const requestId = crypto.randomUUID();
695
- const requestUrl = new URL(
696
- stdinFallbackConfig.endpointPath,
697
- // eslint-disable-next-line no-restricted-globals
698
- self.location.origin,
699
- );
700
- requestUrl.searchParams.set("runId", runId || "");
701
- requestUrl.searchParams.set("requestId", requestId);
702
- requestUrl.searchParams.set("clientId", stdinFallbackConfig.clientId);
703
-
704
- const xhr = new XMLHttpRequest();
705
- xhr.open("GET", requestUrl.toString(), false);
706
- xhr.setRequestHeader("X-Pyodide-Stdin-Request", "true");
707
- xhr.setRequestHeader("Cache-Control", "no-store");
708
-
709
- try {
710
- xhr.send(null);
711
- } catch (_) {
712
- throw new Error("Failed to read Python input via stdin fallback");
713
- }
714
-
715
- if (xhr.status === STDIN_CANCELLED_RESPONSE_CODE) {
716
- stdinCancelled = true;
717
- throw new Error("PYODIDE_STDIN_CANCELLED");
718
- }
719
-
720
- if (xhr.status === STDIN_TIMEOUT_RESPONSE_CODE) {
721
- throw new Error("PYODIDE_STDIN_TIMEOUT");
722
- }
723
-
724
- if (xhr.status === STDIN_ABORTED_RESPONSE_CODE) {
725
- throw new Error("PYODIDE_STDIN_ABORTED");
726
- }
727
-
728
- // EOF: orchestrator signalled Ctrl+D / no more input. Returning null tells
729
- // Pyodide's line-based stdin handler to raise EOFError at the input() call
730
- // site, rather than escalating to a KeyboardInterrupt that tears down the run.
731
- if (xhr.status === STDIN_EOF_RESPONSE_CODE) {
732
- return null;
733
- }
734
-
735
- if (xhr.status !== 200) {
736
- throw new Error(
737
- `Python input request failed with status ${xhr.status}: ${xhr.responseText}`,
738
- );
739
- }
740
-
741
- let content = xhr.responseText ?? "";
742
- if (!content.endsWith("\n")) {
743
- content += "\n";
744
- }
745
-
746
- return content;
747
- };
748
-
749
- const clearPyodideData = async (userModuleNames) => {
750
- postMessage({ method: "handleLoading" });
751
- try {
752
- await pyodide.runPythonAsync(`
753
- import builtins
754
- import sys
755
-
756
- # Restore the real open before cleaning up
757
- if hasattr(builtins, "_original_open"):
758
- builtins.open = builtins._original_open
759
-
760
- # Remove user modules from sys.modules
761
- user_modules = ${JSON.stringify(userModuleNames)}
762
- for name in user_modules:
763
- if name in sys.modules:
764
- del sys.modules[name]
765
-
766
- # Clear all user-defined variables and modules
767
- for name in list(globals()):
768
- if not name.startswith('_') and name not in ('basthon', 'sys', 'builtins'):
769
- del globals()[name]
770
- `);
771
- } catch (error) {
772
- console.error("Error while clearing Pyodide data:", error);
773
- }
774
- console.log("clearPyodideData done");
775
- postMessage({
776
- method: "handleLoaded",
777
- stdinBuffer,
778
- interruptBuffer,
779
- stdinStrategy,
780
- supportsJspi,
781
- stdinFallbackEnabled: Boolean(stdinFallbackConfig?.enabled),
782
- });
783
- };
784
-
785
- const initialisePyodide = async (data) => {
786
- postMessage({ method: "handleLoading" });
787
-
788
- pyodidePromise = loadPyodide({
789
- stdout: (content) => {
790
- if (stopped) return;
791
- if (userStdStreamsEnabled && !suppressInternalStdStreams) {
792
- postMessage({ method: "handleOutput", stream: "stdout", content });
793
- } else {
794
- console.log(content);
795
- }
796
- },
797
- stderr: (content) => {
798
- if (stopped) return;
799
- if (userStdStreamsEnabled && !suppressInternalStdStreams) {
800
- postMessage({ method: "handleOutput", stream: "stderr", content });
801
- } else {
802
- console.error(content);
803
- }
804
- },
805
- });
806
-
807
- pyodide = await pyodidePromise;
808
-
809
- pyodide.registerJsModule("basthon", fakeBasthonPackage);
810
-
811
- supportsJspi =
812
- typeof WebAssembly?.Suspending === "function" &&
813
- typeof WebAssembly?.promising === "function";
814
-
815
- stdinFallbackConfig = {
816
- enabled: Boolean(data?.stdinFallback?.enabled),
817
- endpointPath: data?.stdinFallback?.endpointPath || "/pyodide-stdin",
818
- clientId: data?.stdinFallback?.clientId || "",
819
- };
820
-
821
- // When SharedArrayBuffer is unavailable, always use the postMessage-based
822
- // stdin path. JSPI / run_sync will raise at runtime if the environment
823
- // cannot stack-switch, but in JSPI-capable browsers this enables
824
- // interactive input() without COOP/COEP.
825
- useMessageStdin = !supportsAllFeatures && supportsJspi;
826
- useSyncXhrStdin =
827
- !supportsAllFeatures && !supportsJspi && stdinFallbackConfig.enabled;
828
- stdinStrategy = supportsAllFeatures
829
- ? "sab"
830
- : useMessageStdin
831
- ? "jspi"
832
- : useSyncXhrStdin
833
- ? "sync-xhr"
834
- : "unavailable";
835
- pyodide.globals.set("__stdin_via_message__", useMessageStdin);
836
-
837
- await pyodide.runPythonAsync(`
838
- import builtins
839
- from pyodide.ffi import run_sync as __run_sync__
840
-
841
- __old_input__ = builtins.input
842
- def __patched_input__(prompt=None):
843
- if prompt is not None:
844
- print(prompt)
845
- if __stdin_via_message__:
846
- result = __run_sync__(basthon.kernel.getInputAsync())
847
- if result is None:
848
- raise EOFError("EOF when reading a line")
849
- return result
850
- return __old_input__()
851
- builtins.input = __patched_input__
852
- `);
853
-
854
- await pyodide.runPythonAsync(`
855
- import builtins
856
- # Save the original open function only once
857
- if not hasattr(builtins, "_original_open"):
858
- builtins._original_open = builtins.open
859
- _original_open = builtins._original_open
860
- `);
861
-
862
- await pyodide.loadPackage("pyodide-http");
863
- await pyodide.runPythonAsync(`
864
- import pyodide_http
865
- pyodide_http.patch_all()
866
- `);
867
-
868
- if (supportsAllFeatures) {
869
- stdinBuffer =
870
- stdinBuffer || new Int32Array(new SharedArrayBuffer(1024 * 1024)); // 1 MiB
871
- stdinBuffer[0] = 1; // Store the length of content in the buffer at index 0.
872
- pyodide.setStdin({ isatty: true, read: readFromStdin });
873
-
874
- interruptBuffer =
875
- interruptBuffer || new Uint8Array(new SharedArrayBuffer(1));
876
- pyodide.setInterruptBuffer(interruptBuffer);
877
- } else if (useSyncXhrStdin) {
878
- pyodide.setStdin({
879
- stdin: () => readInputViaSyncXhr(currentRunId),
880
- isatty: true,
881
- });
882
- }
883
-
884
- // From this point on, anything written to stdout / stderr is considered
885
- // user-visible and will be forwarded to the UI.
886
- userStdStreamsEnabled = true;
887
-
888
- postMessage({
889
- method: "handleLoaded",
890
- stdinBuffer,
891
- interruptBuffer,
892
- stdinStrategy,
893
- supportsJspi,
894
- stdinFallbackEnabled: stdinFallbackConfig.enabled,
895
- });
896
- };
897
-
898
- const readFromStdin = (bufferToWrite) => {
899
- const previousLength = stdinBuffer[0];
900
- postMessage({ method: "handleInput" });
901
-
902
- while (true) {
903
- pyodide.checkInterrupt();
904
- const result = Atomics.wait(stdinBuffer, 0, previousLength, 100);
905
- if (result === "not-equal") {
906
- break;
907
- }
908
- }
909
-
910
- const currentLength = stdinBuffer[0];
911
- if (currentLength === -1) {
912
- return 0;
913
- } // Signals that stdin was closed.
914
-
915
- const addedBytes = stdinBuffer.slice(previousLength, currentLength);
916
- bufferToWrite.set(addedBytes);
917
-
918
- return addedBytes.length;
919
- };
920
-
921
- const parsePythonError = (error) => {
922
- const type = error.type;
923
- const [trace, info] = error.message.split(`${type}:`).map((s) => s?.trim());
924
-
925
- const lines = trace.split("\n");
926
-
927
- // if the third from last line matches /File "__custom_open__\.py", line (\d+)/g then strip off the last three lines
928
- if (
929
- lines.length > 3 &&
930
- /File "__custom_open__\.py", line (\d+)/g.test(lines[lines.length - 3])
931
- ) {
932
- lines.splice(-3, 3);
933
- }
934
-
935
- const snippetLine = lines[lines.length - 2]; // print("hi")invalid
936
- const caretLine = lines[lines.length - 1]; // ^^^^^^^
937
-
938
- const showsMistake = caretLine.includes("^");
939
- const mistake = showsMistake
940
- ? [snippetLine.slice(4), caretLine.slice(4)].join("\n")
941
- : "";
942
-
943
- const matches = [
944
- ...trace.matchAll(/File "(?!__custom_open__\.py)(.*)", line (\d+)/g),
945
- ];
946
- const match = matches[matches.length - 1];
947
-
948
- const path = match ? match[1] : "";
949
- const base = path.split("/").reverse()[0];
950
- const file = base === "<exec>" ? "main.py" : base;
951
-
952
- const line = match ? parseInt(match[2], 10) : "";
953
-
954
- return { file, line, mistake, type, info };
955
- };
956
- };
957
-
958
- globalThis.PyodideWorker = PyodideWorker;
959
- PyodideWorker();
1
+ /* eslint-disable @typescript-eslint/no-empty-function */
2
+ /* global globalThis, importScripts, loadPyodide, SharedArrayBuffer, Atomics, pygal */
3
+
4
+ function toAbsoluteFromOrigin(url) {
5
+ if (url.startsWith("http") || url.startsWith("/")) return url;
6
+ // eslint-disable-next-line no-restricted-globals
7
+ return new URL(url, self.location.origin).href;
8
+ }
9
+
10
+ // removing submodules (matplotlib.pyplot = matplotlib)
11
+ const normalizeImportName = (name) => name.split(".")[0];
12
+
13
+ const WORKING_DIR = "/home/pyodide";
14
+
15
+ const toStructuredCloneable = (value) => {
16
+ if (value == null) return value;
17
+
18
+ if (
19
+ typeof value === "string" ||
20
+ typeof value === "number" ||
21
+ typeof value === "boolean"
22
+ ) {
23
+ return value;
24
+ }
25
+
26
+ if (value instanceof Uint8Array) {
27
+ return value.slice();
28
+ }
29
+
30
+ if (ArrayBuffer.isView(value)) {
31
+ return new Uint8Array(
32
+ value.buffer.slice(value.byteOffset, value.byteOffset + value.byteLength),
33
+ );
34
+ }
35
+
36
+ if (value instanceof ArrayBuffer) {
37
+ return value.slice(0);
38
+ }
39
+
40
+ if (value instanceof Map) {
41
+ return Object.fromEntries(
42
+ [...value.entries()].map(([k, v]) => [k, toStructuredCloneable(v)]),
43
+ );
44
+ }
45
+
46
+ if (Array.isArray(value)) {
47
+ return value.map(toStructuredCloneable);
48
+ }
49
+
50
+ if (typeof value?.toJs === "function") {
51
+ try {
52
+ const converted = value.toJs({ dict_converter: Object.fromEntries });
53
+ return toStructuredCloneable(converted);
54
+ } catch (_) {
55
+ try {
56
+ const converted = value.toJs();
57
+ return toStructuredCloneable(converted);
58
+ } catch (_) {
59
+ return String(value);
60
+ }
61
+ }
62
+ }
63
+
64
+ if (typeof value === "object") {
65
+ const result = {};
66
+ for (const [key, val] of Object.entries(value)) {
67
+ result[key] = toStructuredCloneable(val);
68
+ }
69
+ return result;
70
+ }
71
+
72
+ return String(value);
73
+ };
74
+
75
+ // Nest the PyodideWorker function inside a globalThis object so we control when its initialised.
76
+ const PyodideWorker = () => {
77
+ let assets;
78
+ let packageApiUrl;
79
+
80
+ const handleInit = (data) => {
81
+ assets = data.assets;
82
+ packageApiUrl = data.packageApiUrl;
83
+
84
+ // Import scripts dynamically based on the environment
85
+ console.log("PyodideWorker", "importing scripts");
86
+ importScripts(toAbsoluteFromOrigin(assets.pygalUrl));
87
+
88
+ assets.pyodideBaseUrl = `${packageApiUrl}/pyodide.js`;
89
+ importScripts(toAbsoluteFromOrigin(assets.pyodideBaseUrl));
90
+
91
+ initialisePyodide(data);
92
+ };
93
+
94
+ const supportsAllFeatures = typeof SharedArrayBuffer !== "undefined";
95
+
96
+ // eslint-disable-next-line no-restricted-globals
97
+ if (!supportsAllFeatures && name !== "incremental-features") {
98
+ console.warn(
99
+ [
100
+ "The code editor will not be able to capture standard input or stop execution because these HTTP headers are not set:",
101
+ " - Cross-Origin-Opener-Policy: same-origin",
102
+ " - Cross-Origin-Embedder-Policy: require-corp",
103
+ "",
104
+ "If your app can cope with or without these features, please initialize the web worker with { name: 'incremental-features' } to silence this warning.",
105
+ "You can then check for the presence of { stdinBuffer, interruptBuffer } in the handleLoaded message to check whether these features are supported.",
106
+ "",
107
+ "If you definitely need these features, either configure your server to respond with the HTTP headers above, or register a service worker.",
108
+ "Once the HTTP headers are set, the browser will block cross-domain resources so you will need to add 'crossorigin' to <script> and other tags.",
109
+ "You may wish to scope the HTTP headers to only those pages that host the code editor to make the browser restriction easier to deal with.",
110
+ "",
111
+ "Please refer to these code snippets for registering a service worker:",
112
+ " - https://github.com/RaspberryPiFoundation/python-execution-prototypes/blob/fd2c50e032cba3bb0e92e19a88eb62e5b120fe7a/pyodide/index.html#L92-L98",
113
+ " - https://github.com/RaspberryPiFoundation/python-execution-prototypes/blob/fd2c50e032cba3bb0e92e19a88eb62e5b120fe7a/pyodide/serviceworker.js",
114
+ ].join("\n"),
115
+ );
116
+ }
117
+ let pyodide, pyodidePromise, stdinBuffer, interruptBuffer, stopped;
118
+ let currentRunId = null;
119
+ let stdinStrategy = "sab";
120
+ let supportsJspi = false;
121
+ let stdinFallbackConfig = null;
122
+ const STDIN_CANCELLED_RESPONSE_CODE = 499;
123
+ const STDIN_TIMEOUT_RESPONSE_CODE = 408;
124
+ const STDIN_ABORTED_RESPONSE_CODE = 410;
125
+ const STDIN_ROUTE_NOT_FOUND_RESPONSE_CODE = 404;
126
+ // HTTP 204 No Content from the stdin Service Worker signals end-of-file —
127
+ // distinct from a cancellation/interrupt so input() raises EOFError and the
128
+ // user's `try/except EOFError` blocks keep working under the sync-XHR fallback.
129
+ const STDIN_EOF_RESPONSE_CODE = 204;
130
+ /** When true, input() uses postMessage + run_sync(getInputAsync) instead of setStdin (requires JSPI). */
131
+ let useMessageStdin = false;
132
+ /** When true, input() uses sync XHR via the Service Worker fallback. */
133
+ let useSyncXhrStdin = false;
134
+ /** True when current input request was cancelled from outside the worker. */
135
+ let stdinCancelled = false;
136
+ /** Used when SharedArrayBuffer is unavailable: resolve for the pending input() call. */
137
+ let pendingStdinResolve = null;
138
+ // Until Pyodide is fully initialised, keep stdout/stderr in the dev console only.
139
+ // After initialisation, route them to the UI via postMessage.
140
+ let userStdStreamsEnabled = false;
141
+ // When true, suppress internal library / package loader logs from the UI console.
142
+ let suppressInternalStdStreams = false;
143
+
144
+ const onmessage = async ({ data }) => {
145
+ if (data.method !== "init") {
146
+ pyodide = await pyodidePromise;
147
+ }
148
+
149
+ switch (data.method) {
150
+ case "stdinResponse": {
151
+ if (pendingStdinResolve) {
152
+ let content = data.ctrlD ? null : data.content ?? "";
153
+ if (content && content.endsWith("\n")) {
154
+ content = content.slice(0, -1);
155
+ }
156
+ pendingStdinResolve(content);
157
+ pendingStdinResolve = null;
158
+ }
159
+ break;
160
+ }
161
+ case "init": {
162
+ handleInit(data);
163
+ break;
164
+ }
165
+ case "createDirectories": {
166
+ if (Array.isArray(data.dirs)) {
167
+ for (const dir of data.dirs) {
168
+ try {
169
+ pyodide.FS.mkdirTree(`${WORKING_DIR}/${dir}`);
170
+ } catch (e) {
171
+ console.error(`Failed to create directory ${dir}:`, e);
172
+ }
173
+ }
174
+ }
175
+ break;
176
+ }
177
+ case "writeFile": {
178
+ const encoder = new TextEncoder();
179
+ pyodide.FS.writeFile(
180
+ `${WORKING_DIR}/${data.filename}`,
181
+ encoder.encode(data.content),
182
+ );
183
+ break;
184
+ }
185
+ case "runPython":
186
+ currentRunId = data.runId || null;
187
+ stdinCancelled = false;
188
+ runPython(data.python, data.userModuleNames);
189
+ break;
190
+ case "stopPython": {
191
+ // Mark as stopped so future checks can raise KeyboardInterrupt.
192
+ stopped = true;
193
+ // If Python is currently blocked in input() via run_sync(getInputAsync),
194
+ // resolve the pending stdin promise with EOF so execution can unwind
195
+ // and clearPyodideData can run.
196
+ if (pendingStdinResolve) {
197
+ pendingStdinResolve(null);
198
+ pendingStdinResolve = null;
199
+ }
200
+ stdinCancelled = true;
201
+ break;
202
+ }
203
+ default: {
204
+ throw new Error(`Unsupported method: ${data.method}`);
205
+ }
206
+ }
207
+ };
208
+
209
+ // eslint-disable-next-line no-restricted-globals
210
+ addEventListener("message", async (event) => {
211
+ onmessage(event);
212
+ });
213
+
214
+ const runPython = async (python, userModuleNames) => {
215
+ stopped = false;
216
+
217
+ if (stdinStrategy === "unavailable") {
218
+ postMessage({
219
+ method: "handleError",
220
+ file: "main.py",
221
+ line: "",
222
+ mistake: "",
223
+ type: "RuntimeError",
224
+ info: "Python input fallback is unavailable. Please enable and activate the stdin Service Worker, then reload the page.",
225
+ });
226
+ await clearPyodideData(userModuleNames);
227
+ return;
228
+ }
229
+
230
+ // When stdin uses postMessage (no SharedArrayBuffer), run_sync() in input() requires
231
+ // runPythonAsync so that JSPI stack switching can suspend until the main thread sends stdinResponse.
232
+ const runUserCode = useMessageStdin
233
+ ? () => pyodide.runPythonAsync(python)
234
+ : () => pyodide.runPython(python);
235
+
236
+ try {
237
+ await withSupportForPackages(python, async () => {
238
+ await runUserCode();
239
+ });
240
+ } catch (error) {
241
+ const isStdinControlError =
242
+ error?.message === "PYODIDE_STDIN_CANCELLED" ||
243
+ error?.message === "PYODIDE_STDIN_TIMEOUT" ||
244
+ error?.message === "PYODIDE_STDIN_ABORTED";
245
+
246
+ if (stdinCancelled || isStdinControlError) {
247
+ postMessage({
248
+ method: "handleError",
249
+ file: "main.py",
250
+ line: "",
251
+ mistake: "",
252
+ type: "KeyboardInterrupt",
253
+ info: "Execution interrupted",
254
+ });
255
+ await clearPyodideData(userModuleNames);
256
+ return;
257
+ }
258
+
259
+ if (!(error instanceof pyodide.ffi.PythonError)) {
260
+ postMessage({
261
+ method: "handleError",
262
+ file: "main.py",
263
+ line: "",
264
+ mistake: "",
265
+ type: "RuntimeError",
266
+ info:
267
+ error?.message ||
268
+ "Python execution failed while reading stdin fallback input.",
269
+ });
270
+ await clearPyodideData(userModuleNames);
271
+ return;
272
+ }
273
+ const parsed = parsePythonError(error);
274
+ // Stop resolves stdin with EOF so input() raises EOFError; show as interrupt, not error.
275
+ if (stopped && parsed.type === "EOFError") {
276
+ postMessage({
277
+ method: "handleError",
278
+ ...parsed,
279
+ type: "KeyboardInterrupt",
280
+ info: "Execution interrupted",
281
+ });
282
+ } else {
283
+ postMessage({ method: "handleError", ...parsed });
284
+ }
285
+ }
286
+
287
+ await clearPyodideData(userModuleNames);
288
+ };
289
+
290
+ const checkIfStopped = () => {
291
+ if (stopped) {
292
+ throw new pyodide.ffi.PythonError("KeyboardInterrupt");
293
+ }
294
+ };
295
+
296
+ const withSupportForPackages = async (
297
+ python,
298
+ runPythonFn = async () => {},
299
+ ) => {
300
+ // Suppress internal loader output (e.g. "Loading pyodide-http") from the
301
+ // user console while resolving imports and loading packages.
302
+ suppressInternalStdStreams = true;
303
+
304
+ // `find_imports` returns a PyProxy whose .toJs() copies the contents but
305
+ // leaves the underlying Python object alive. Destroy it explicitly so JS
306
+ // GC can't try to finalise it later from a context where the GIL isn't
307
+ // held (which would surface as a NoGilError).
308
+ const importsProxy = pyodide._api.pyodide_code.find_imports(python);
309
+ const imports = importsProxy.toJs();
310
+ importsProxy.destroy();
311
+
312
+ await pyodide.runPythonAsync(`
313
+ import builtins
314
+ if hasattr(builtins, "_original_open"):
315
+ builtins.open = builtins._original_open
316
+ `);
317
+
318
+ // Load packages sequentially rather than in Promise.all. Each
319
+ // `loadDependency` does multiple `await` hops (vendored .before(),
320
+ // loadPackage, micropip.install, …). When several run concurrently their
321
+ // await points interleave and Pyodide ends up servicing multiple in-flight
322
+ // package loads — that interleaving is the trigger for the NoGilError seen
323
+ // when complex projects load matplotlib's dependency tree. Sequential
324
+ // loading costs a few ms on cold start but matches how `loadPackage`
325
+ // already serialises its own work internally.
326
+ // for (const name of imports) {
327
+ // checkIfStopped();
328
+ // await loadDependency(name);
329
+ // }
330
+ await Promise.all(imports.map((name) => loadDependency(name)));
331
+
332
+ checkIfStopped();
333
+ await pyodide.loadPackagesFromImports(python);
334
+
335
+ checkIfStopped();
336
+ await pyodide.runPythonAsync(
337
+ `
338
+ import basthon
339
+ import builtins
340
+ import os
341
+
342
+ MAX_FILES = 100
343
+ MAX_FILE_SIZE = 8500000
344
+ PROJECT_ROOT = os.path.abspath("${WORKING_DIR}")
345
+
346
+ if not hasattr(builtins, "_original_open"):
347
+ builtins._original_open = builtins.open
348
+
349
+ ORIGINAL_OPEN = builtins._original_open
350
+
351
+ def _is_project_file(filename):
352
+ abs_path = os.path.abspath(filename)
353
+ return abs_path == PROJECT_ROOT or abs_path.startswith(PROJECT_ROOT + os.sep)
354
+
355
+ def _to_project_relative(filename):
356
+ abs_path = os.path.abspath(filename)
357
+ return os.path.relpath(abs_path, PROJECT_ROOT)
358
+
359
+ def _custom_open(filename, mode="r", *args, **kwargs):
360
+ import os
361
+ abs_path = os.path.abspath(filename)
362
+
363
+ if "x" in mode and os.path.exists(abs_path):
364
+ raise FileExistsError(f"File '{filename}' already exists")
365
+
366
+ is_text_write = ("w" in mode or "a" in mode or "x" in mode) and "b" not in mode
367
+ is_project_file = _is_project_file(abs_path)
368
+
369
+ if is_text_write and is_project_file:
370
+ if len(os.listdir(PROJECT_ROOT)) > MAX_FILES and not os.path.exists(abs_path):
371
+ raise OSError(f"File system limit reached, no more than {MAX_FILES} files allowed")
372
+ class CustomFile:
373
+ def __init__(self, filename):
374
+ self.filename = filename
375
+ self.content = ""
376
+
377
+ def write(self, content):
378
+ self.content += content
379
+ if len(self.content) > MAX_FILE_SIZE:
380
+ raise OSError(f"File '{self.filename}' exceeds maximum file size of {MAX_FILE_SIZE} bytes")
381
+ with ORIGINAL_OPEN(self.filename, mode, *args, **kwargs) as f:
382
+ f.write(self.content)
383
+ basthon.kernel.write_file({
384
+ "filename": _to_project_relative(self.filename),
385
+ "content": self.content,
386
+ "mode": mode
387
+ })
388
+
389
+ def close(self):
390
+ pass
391
+
392
+ def __enter__(self):
393
+ return self
394
+
395
+ def __exit__(self, exc_type, exc_val, exc_tb):
396
+ self.close()
397
+
398
+ return CustomFile(abs_path)
399
+ else:
400
+ return ORIGINAL_OPEN(filename, mode, *args, **kwargs)
401
+
402
+ # Override the built-in open function
403
+ builtins.open = _custom_open
404
+ `,
405
+ { filename: "__custom_open__.py" },
406
+ );
407
+
408
+ // Re-enable user-visible stdout / stderr for the actual user code run.
409
+ suppressInternalStdStreams = false;
410
+
411
+ await runPythonFn();
412
+
413
+ for (let name of imports) {
414
+ checkIfStopped();
415
+ const pkgName = normalizeImportName(name);
416
+ await vendoredPackages[pkgName]?.after();
417
+ }
418
+ };
419
+
420
+ const loadDependency = async (name) => {
421
+ checkIfStopped();
422
+
423
+ const pkgName = normalizeImportName(name);
424
+ // If the import is for another user file then open it and load its dependencies.
425
+ if (pyodide.FS.readdir(WORKING_DIR).includes(`${pkgName}.py`)) {
426
+ const fileContent = pyodide.FS.readFile(`${WORKING_DIR}/${pkgName}.py`, {
427
+ encoding: "utf8",
428
+ });
429
+ await withSupportForPackages(fileContent);
430
+ return;
431
+ }
432
+
433
+ // If the import is for a vendored package then run its .before() hook.
434
+ const vendoredPackage = vendoredPackages[pkgName];
435
+ await vendoredPackage?.before();
436
+ if (vendoredPackage) {
437
+ return;
438
+ }
439
+
440
+ // If the import is for a module built into Python then do nothing.
441
+ // The PyProxy is only used as a presence check, so destroy it immediately
442
+ // — leaking it leaves a Python ref for JS GC to finalise later, which
443
+ // raises NoGilError when GC runs outside a GIL-held context.
444
+ try {
445
+ const pythonModule = pyodide.pyimport(pkgName);
446
+ if (pythonModule) {
447
+ pythonModule.destroy();
448
+ return;
449
+ }
450
+ } catch (_) {}
451
+
452
+ // If the import is for a package built into Pyodide then load it.
453
+ // Built-ins: https://pyodide.org/en/stable/usage/packages-in-pyodide.html
454
+ try {
455
+ await pyodide.loadPackage(pkgName);
456
+
457
+ const pyodidePackage = pyodide.pyimport(pkgName);
458
+ if (pyodidePackage) {
459
+ pyodidePackage.destroy();
460
+ return;
461
+ }
462
+ } catch (_) {}
463
+
464
+ // Ensure micropip is loaded which can fetch packages from PyPi.
465
+ // See: https://pyodide.org/en/stable/usage/loading-packages.html
466
+ if (!pyodide.micropip) {
467
+ await pyodide.loadPackage("micropip");
468
+ pyodide.micropip = pyodide.pyimport("micropip");
469
+ }
470
+
471
+ // If the import is for a PyPi package then load it.
472
+ // Otherwise, don't error now so that we get an error later from Python.
473
+ await pyodide.micropip.install(pkgName).catch(() => {});
474
+ };
475
+
476
+ const vendoredPackages = {
477
+ // Support for https://pypi.org/project/py-enigma/ due to package not having a whl file on PyPi.
478
+ enigma: {
479
+ before: async () => {
480
+ await pyodide.loadPackage(toAbsoluteFromOrigin(assets.enigmaWhlUrl));
481
+ },
482
+ after: () => {},
483
+ },
484
+ turtle: {
485
+ before: async () => {
486
+ pyodide.registerJsModule("basthon", fakeBasthonPackage);
487
+ await pyodide.loadPackage(toAbsoluteFromOrigin(assets.turtleWhlUrl));
488
+ },
489
+ after: () =>
490
+ pyodide.runPython(`
491
+ import turtle
492
+ import basthon
493
+
494
+ svg_dict = turtle.Screen().show_scene()
495
+ basthon.kernel.display_event({ "display_type": "turtle", "content": svg_dict })
496
+ turtle.restart()
497
+ `),
498
+ },
499
+ p5: {
500
+ before: async () => {
501
+ pyodide.registerJsModule("basthon", fakeBasthonPackage);
502
+ await pyodide.loadPackage([
503
+ "setuptools",
504
+ toAbsoluteFromOrigin(assets.p5WhlUrl),
505
+ ]);
506
+ },
507
+ after: () => {},
508
+ },
509
+ pygal: {
510
+ before: () => {
511
+ pyodide.registerJsModule("pygal", { ...pygal });
512
+ pygal.config.renderChart = (content) => {
513
+ postMessage({ method: "handleVisual", origin: "pygal", content });
514
+ };
515
+ },
516
+ after: () => {},
517
+ },
518
+ matplotlib: {
519
+ before: async () => {
520
+ pyodide.registerJsModule("basthon", fakeBasthonPackage);
521
+ // Patch the document object to prevent matplotlib from trying to render. Since we are running in a web worker,
522
+ // the document object is not available. We will instead capture the image and send it back to the main thread.
523
+ pyodide.runPython(`
524
+ import js
525
+
526
+ class __DummyDocument__:
527
+ def __init__(self, *args, **kwargs) -> None:
528
+ return
529
+ def __getattr__(self, __name: str):
530
+ return __DummyDocument__
531
+ js.document = __DummyDocument__()
532
+ `);
533
+ await pyodide.loadPackage("matplotlib")?.catch(() => {});
534
+ let pyodidePackage;
535
+ try {
536
+ pyodidePackage = pyodide.pyimport("matplotlib");
537
+ } catch (_) {}
538
+ if (pyodidePackage) {
539
+ // Destroy the throwaway proxy before running follow-up Python — the
540
+ // proxy is only used as a "did matplotlib actually load?" gate, and
541
+ // leaking it would hand a Python ref to JS GC.
542
+ pyodidePackage.destroy();
543
+ pyodide.runPython(`
544
+ import matplotlib.pyplot as plt
545
+ import io
546
+ import basthon
547
+
548
+ def show_chart():
549
+ bytes_io = io.BytesIO()
550
+ plt.savefig(bytes_io, format='jpg')
551
+ bytes_io.seek(0)
552
+ basthon.kernel.display_event({ "display_type": "matplotlib", "content": bytes_io.read() })
553
+ plt.show = show_chart
554
+ `);
555
+ return;
556
+ }
557
+ },
558
+ after: () => {
559
+ pyodide.runPython(`
560
+ import matplotlib.pyplot as plt
561
+ plt.clf()
562
+ `);
563
+ },
564
+ },
565
+ seaborn: {
566
+ before: async () => {
567
+ pyodide.registerJsModule("basthon", fakeBasthonPackage);
568
+ // Patch the document object to prevent matplotlib from trying to render. Since we are running in a web worker,
569
+ // the document object is not available. We will instead capture the image and send it back to the main thread.
570
+ pyodide.runPython(`
571
+ import js
572
+
573
+ class __DummyDocument__:
574
+ def __init__(self, *args, **kwargs) -> None:
575
+ return
576
+ def __getattr__(self, __name: str):
577
+ return __DummyDocument__
578
+ js.document = __DummyDocument__()
579
+ `);
580
+
581
+ // Ensure micropip is loaded which can fetch packages from PyPi.
582
+ // See: https://pyodide.org/en/stable/usage/loading-packages.html
583
+ if (!pyodide.micropip) {
584
+ await pyodide.loadPackage("micropip");
585
+ pyodide.micropip = pyodide.pyimport("micropip");
586
+ }
587
+
588
+ // If the import is for a PyPi package then load it.
589
+ // Otherwise, don't error now so that we get an error later from Python.
590
+ await pyodide.micropip.install("seaborn").catch(() => {});
591
+ },
592
+ after: () => {
593
+ pyodide.runPython(`
594
+ import matplotlib.pyplot as plt
595
+ import io
596
+ import basthon
597
+
598
+ def is_plot_empty():
599
+ fig = plt.gcf()
600
+ for ax in fig.get_axes():
601
+ # Check if the axes contain any lines, patches, collections, etc.
602
+ if ax.lines or ax.patches or ax.collections or ax.images or ax.texts:
603
+ return False
604
+ return True
605
+
606
+ if not is_plot_empty():
607
+ bytes_io = io.BytesIO()
608
+ plt.savefig(bytes_io, format='jpg')
609
+ bytes_io.seek(0)
610
+ basthon.kernel.display_event({ "display_type": "matplotlib", "content": bytes_io.read() })
611
+
612
+ plt.clf()
613
+ `);
614
+ },
615
+ },
616
+ plotly: {
617
+ before: async () => {
618
+ if (!pyodide.micropip) {
619
+ await pyodide.loadPackage("micropip");
620
+ pyodide.micropip = pyodide.pyimport("micropip");
621
+ }
622
+
623
+ // If the import is for a PyPi package then load it.
624
+ // Otherwise, don't error now so that we get an error later from Python.
625
+ await pyodide.micropip.install("plotly").catch(() => {});
626
+ await pyodide.micropip.install("pandas").catch(() => {});
627
+ pyodide.registerJsModule("basthon", fakeBasthonPackage);
628
+ pyodide.runPython(`
629
+ import plotly.graph_objs as go
630
+
631
+ def _hacked_show(self, *args, **kwargs):
632
+ basthon.kernel.display_event({
633
+ "display_type": "plotly",
634
+ "content": self.to_json()
635
+ })
636
+
637
+ go.Figure.show = _hacked_show
638
+ `);
639
+ },
640
+ after: () => {},
641
+ },
642
+ };
643
+
644
+ const fakeBasthonPackage = {
645
+ kernel: {
646
+ display_event: (event) => {
647
+ let payload;
648
+ try {
649
+ payload = event.toJs({ dict_converter: Object.fromEntries });
650
+ } catch (_) {
651
+ payload = event.toJs();
652
+ }
653
+
654
+ const origin = String(payload.display_type);
655
+ const content = toStructuredCloneable(payload.content);
656
+
657
+ postMessage({ method: "handleVisual", origin, content });
658
+ },
659
+ write_file: (event) => {
660
+ let payload;
661
+ try {
662
+ payload = event.toJs({ dict_converter: Object.fromEntries });
663
+ } catch (_) {
664
+ payload = event.toJs();
665
+ }
666
+
667
+ const filename = String(payload.filename);
668
+ const content =
669
+ typeof payload.content === "string"
670
+ ? payload.content
671
+ : toStructuredCloneable(payload.content);
672
+ const mode = String(payload.mode);
673
+
674
+ postMessage({ method: "handleFileWrite", filename, content, mode });
675
+ },
676
+ locals: () => pyodide.runPython("globals()"),
677
+ /**
678
+ * Returns a Promise that resolves with the next line of stdin when the main thread
679
+ * sends stdinResponse. Resolves with null on EOF (e.g. Ctrl+D).
680
+ * Used when SharedArrayBuffer is unavailable (no COOP/COEP) so we cannot block via Atomics.wait.
681
+ * Requires JSPI and runPythonAsync() for run_sync() to work.
682
+ */
683
+ getInputAsync: () => {
684
+ const promise = new Promise((resolve) => {
685
+ pendingStdinResolve = resolve;
686
+ });
687
+ postMessage({ method: "handleInput" });
688
+ return promise;
689
+ },
690
+ },
691
+ };
692
+
693
+ const readInputViaSyncXhr = (runId) => {
694
+ const requestId = crypto.randomUUID();
695
+ const requestUrl = new URL(
696
+ stdinFallbackConfig.endpointPath,
697
+ // eslint-disable-next-line no-restricted-globals
698
+ self.location.origin,
699
+ );
700
+ requestUrl.searchParams.set("runId", runId || "");
701
+ requestUrl.searchParams.set("requestId", requestId);
702
+ requestUrl.searchParams.set("clientId", stdinFallbackConfig.clientId);
703
+
704
+ const xhr = new XMLHttpRequest();
705
+ xhr.open("GET", requestUrl.toString(), false);
706
+ xhr.setRequestHeader("X-Pyodide-Stdin-Request", "true");
707
+ xhr.setRequestHeader("Cache-Control", "no-store");
708
+
709
+ try {
710
+ xhr.send(null);
711
+ } catch (_) {
712
+ throw new Error("Failed to read Python input via stdin fallback");
713
+ }
714
+
715
+ if (xhr.status === STDIN_CANCELLED_RESPONSE_CODE) {
716
+ stdinCancelled = true;
717
+ throw new Error("PYODIDE_STDIN_CANCELLED");
718
+ }
719
+
720
+ if (xhr.status === STDIN_TIMEOUT_RESPONSE_CODE) {
721
+ throw new Error("PYODIDE_STDIN_TIMEOUT");
722
+ }
723
+
724
+ if (xhr.status === STDIN_ABORTED_RESPONSE_CODE) {
725
+ throw new Error("PYODIDE_STDIN_ABORTED");
726
+ }
727
+
728
+ // EOF: orchestrator signalled Ctrl+D / no more input. Returning null tells
729
+ // Pyodide's line-based stdin handler to raise EOFError at the input() call
730
+ // site, rather than escalating to a KeyboardInterrupt that tears down the run.
731
+ if (xhr.status === STDIN_EOF_RESPONSE_CODE) {
732
+ return null;
733
+ }
734
+
735
+ if (xhr.status !== 200) {
736
+ throw new Error(
737
+ `Python input request failed with status ${xhr.status}: ${xhr.responseText}`,
738
+ );
739
+ }
740
+
741
+ let content = xhr.responseText ?? "";
742
+ if (!content.endsWith("\n")) {
743
+ content += "\n";
744
+ }
745
+
746
+ return content;
747
+ };
748
+
749
+ const clearPyodideData = async (userModuleNames) => {
750
+ postMessage({ method: "handleLoading" });
751
+ try {
752
+ await pyodide.runPythonAsync(`
753
+ import builtins
754
+ import sys
755
+
756
+ # Restore the real open before cleaning up
757
+ if hasattr(builtins, "_original_open"):
758
+ builtins.open = builtins._original_open
759
+
760
+ # Remove user modules from sys.modules
761
+ user_modules = ${JSON.stringify(userModuleNames)}
762
+ for name in user_modules:
763
+ if name in sys.modules:
764
+ del sys.modules[name]
765
+
766
+ # Clear all user-defined variables and modules
767
+ for name in list(globals()):
768
+ if not name.startswith('_') and name not in ('basthon', 'sys', 'builtins'):
769
+ del globals()[name]
770
+ `);
771
+ } catch (error) {
772
+ console.error("Error while clearing Pyodide data:", error);
773
+ }
774
+ console.log("clearPyodideData done");
775
+ postMessage({
776
+ method: "handleLoaded",
777
+ stdinBuffer,
778
+ interruptBuffer,
779
+ stdinStrategy,
780
+ supportsJspi,
781
+ stdinFallbackEnabled: Boolean(stdinFallbackConfig?.enabled),
782
+ });
783
+ };
784
+
785
+ const initialisePyodide = async (data) => {
786
+ postMessage({ method: "handleLoading" });
787
+
788
+ pyodidePromise = loadPyodide({
789
+ stdout: (content) => {
790
+ if (stopped) return;
791
+ if (userStdStreamsEnabled && !suppressInternalStdStreams) {
792
+ postMessage({ method: "handleOutput", stream: "stdout", content });
793
+ } else {
794
+ console.log(content);
795
+ }
796
+ },
797
+ stderr: (content) => {
798
+ if (stopped) return;
799
+ if (userStdStreamsEnabled && !suppressInternalStdStreams) {
800
+ postMessage({ method: "handleOutput", stream: "stderr", content });
801
+ } else {
802
+ console.error(content);
803
+ }
804
+ },
805
+ });
806
+
807
+ pyodide = await pyodidePromise;
808
+
809
+ pyodide.registerJsModule("basthon", fakeBasthonPackage);
810
+
811
+ supportsJspi =
812
+ typeof WebAssembly?.Suspending === "function" &&
813
+ typeof WebAssembly?.promising === "function";
814
+
815
+ stdinFallbackConfig = {
816
+ enabled: Boolean(data?.stdinFallback?.enabled),
817
+ endpointPath: data?.stdinFallback?.endpointPath || "/pyodide-stdin",
818
+ clientId: data?.stdinFallback?.clientId || "",
819
+ };
820
+
821
+ // When SharedArrayBuffer is unavailable, always use the postMessage-based
822
+ // stdin path. JSPI / run_sync will raise at runtime if the environment
823
+ // cannot stack-switch, but in JSPI-capable browsers this enables
824
+ // interactive input() without COOP/COEP.
825
+ useMessageStdin = !supportsAllFeatures && supportsJspi;
826
+ useSyncXhrStdin =
827
+ !supportsAllFeatures && !supportsJspi && stdinFallbackConfig.enabled;
828
+ stdinStrategy = supportsAllFeatures
829
+ ? "sab"
830
+ : useMessageStdin
831
+ ? "jspi"
832
+ : useSyncXhrStdin
833
+ ? "sync-xhr"
834
+ : "unavailable";
835
+ pyodide.globals.set("__stdin_via_message__", useMessageStdin);
836
+
837
+ await pyodide.runPythonAsync(`
838
+ import builtins
839
+ from pyodide.ffi import run_sync as __run_sync__
840
+
841
+ __old_input__ = builtins.input
842
+ def __patched_input__(prompt=None):
843
+ if prompt is not None:
844
+ print(prompt)
845
+ if __stdin_via_message__:
846
+ result = __run_sync__(basthon.kernel.getInputAsync())
847
+ if result is None:
848
+ raise EOFError("EOF when reading a line")
849
+ return result
850
+ return __old_input__()
851
+ builtins.input = __patched_input__
852
+ `);
853
+
854
+ await pyodide.runPythonAsync(`
855
+ import builtins
856
+ # Save the original open function only once
857
+ if not hasattr(builtins, "_original_open"):
858
+ builtins._original_open = builtins.open
859
+ _original_open = builtins._original_open
860
+ `);
861
+
862
+ await pyodide.loadPackage("pyodide-http");
863
+ await pyodide.runPythonAsync(`
864
+ import pyodide_http
865
+ pyodide_http.patch_all()
866
+ `);
867
+
868
+ if (supportsAllFeatures) {
869
+ stdinBuffer =
870
+ stdinBuffer || new Int32Array(new SharedArrayBuffer(1024 * 1024)); // 1 MiB
871
+ stdinBuffer[0] = 1; // Store the length of content in the buffer at index 0.
872
+ pyodide.setStdin({ isatty: true, read: readFromStdin });
873
+
874
+ interruptBuffer =
875
+ interruptBuffer || new Uint8Array(new SharedArrayBuffer(1));
876
+ pyodide.setInterruptBuffer(interruptBuffer);
877
+ } else if (useSyncXhrStdin) {
878
+ pyodide.setStdin({
879
+ stdin: () => readInputViaSyncXhr(currentRunId),
880
+ isatty: true,
881
+ });
882
+ }
883
+
884
+ // From this point on, anything written to stdout / stderr is considered
885
+ // user-visible and will be forwarded to the UI.
886
+ userStdStreamsEnabled = true;
887
+
888
+ postMessage({
889
+ method: "handleLoaded",
890
+ stdinBuffer,
891
+ interruptBuffer,
892
+ stdinStrategy,
893
+ supportsJspi,
894
+ stdinFallbackEnabled: stdinFallbackConfig.enabled,
895
+ });
896
+ };
897
+
898
+ const readFromStdin = (bufferToWrite) => {
899
+ const previousLength = stdinBuffer[0];
900
+ postMessage({ method: "handleInput" });
901
+
902
+ while (true) {
903
+ pyodide.checkInterrupt();
904
+ const result = Atomics.wait(stdinBuffer, 0, previousLength, 100);
905
+ if (result === "not-equal") {
906
+ break;
907
+ }
908
+ }
909
+
910
+ const currentLength = stdinBuffer[0];
911
+ if (currentLength === -1) {
912
+ return 0;
913
+ } // Signals that stdin was closed.
914
+
915
+ const addedBytes = stdinBuffer.slice(previousLength, currentLength);
916
+ bufferToWrite.set(addedBytes);
917
+
918
+ return addedBytes.length;
919
+ };
920
+
921
+ const parsePythonError = (error) => {
922
+ const type = error.type;
923
+ const [trace, info] = error.message.split(`${type}:`).map((s) => s?.trim());
924
+
925
+ const lines = trace.split("\n");
926
+
927
+ // if the third from last line matches /File "__custom_open__\.py", line (\d+)/g then strip off the last three lines
928
+ if (
929
+ lines.length > 3 &&
930
+ /File "__custom_open__\.py", line (\d+)/g.test(lines[lines.length - 3])
931
+ ) {
932
+ lines.splice(-3, 3);
933
+ }
934
+
935
+ const snippetLine = lines[lines.length - 2]; // print("hi")invalid
936
+ const caretLine = lines[lines.length - 1]; // ^^^^^^^
937
+
938
+ const showsMistake = caretLine.includes("^");
939
+ const mistake = showsMistake
940
+ ? [snippetLine.slice(4), caretLine.slice(4)].join("\n")
941
+ : "";
942
+
943
+ const matches = [
944
+ ...trace.matchAll(/File "(?!__custom_open__\.py)(.*)", line (\d+)/g),
945
+ ];
946
+ const match = matches[matches.length - 1];
947
+
948
+ const path = match ? match[1] : "";
949
+ const base = path.split("/").reverse()[0];
950
+ const file = base === "<exec>" ? "main.py" : base;
951
+
952
+ const line = match ? parseInt(match[2], 10) : "";
953
+
954
+ return { file, line, mistake, type, info };
955
+ };
956
+ };
957
+
958
+ globalThis.PyodideWorker = PyodideWorker;
959
+ PyodideWorker();