gwchq-textjam 0.1.13 → 0.1.15

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,529 +1,529 @@
1
- /* global globalThis, importScripts, loadPyodide, SharedArrayBuffer, Atomics, pygal */
2
-
3
- function toAbsoluteFromOrigin(url) {
4
- if (url.startsWith("http") || url.startsWith("/")) return url;
5
- return new URL(url, self.location.origin).href;
6
- }
7
-
8
- // Nest the PyodideWorker function inside a globalThis object so we control when its initialised.
9
- const PyodideWorker = () => {
10
- let assets;
11
- let packageApiUrl;
12
-
13
- const handleInit = (data) => {
14
- assets = data.assets;
15
- packageApiUrl = data.packageApiUrl;
16
-
17
- // Import scripts dynamically based on the environment
18
- console.log("PyodideWorker", "importing scripts");
19
- importScripts(toAbsoluteFromOrigin(assets.pygalUrl));
20
-
21
- assets.pyodideBaseUrl = `${packageApiUrl}/pyodide.js`;
22
- importScripts(toAbsoluteFromOrigin(assets.pyodideBaseUrl));
23
-
24
- initialisePyodide();
25
- };
26
-
27
- const supportsAllFeatures = typeof SharedArrayBuffer !== "undefined";
28
-
29
- // eslint-disable-next-line no-restricted-globals
30
- if (!supportsAllFeatures && name !== "incremental-features") {
31
- console.warn(
32
- [
33
- "The code editor will not be able to capture standard input or stop execution because these HTTP headers are not set:",
34
- " - Cross-Origin-Opener-Policy: same-origin",
35
- " - Cross-Origin-Embedder-Policy: require-corp",
36
- "",
37
- "If your app can cope with or without these features, please initialize the web worker with { name: 'incremental-features' } to silence this warning.",
38
- "You can then check for the presence of { stdinBuffer, interruptBuffer } in the handleLoaded message to check whether these features are supported.",
39
- "",
40
- "If you definitely need these features, either configure your server to respond with the HTTP headers above, or register a service worker.",
41
- "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.",
42
- "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.",
43
- "",
44
- "Please refer to these code snippets for registering a service worker:",
45
- " - https://github.com/RaspberryPiFoundation/python-execution-prototypes/blob/fd2c50e032cba3bb0e92e19a88eb62e5b120fe7a/pyodide/index.html#L92-L98",
46
- " - https://github.com/RaspberryPiFoundation/python-execution-prototypes/blob/fd2c50e032cba3bb0e92e19a88eb62e5b120fe7a/pyodide/serviceworker.js",
47
- ].join("\n"),
48
- );
49
- }
50
- let pyodide, pyodidePromise, stdinBuffer, interruptBuffer, stopped;
51
-
52
- const onmessage = async ({ data }) => {
53
- pyodide = await pyodidePromise;
54
- let encoder = new TextEncoder();
55
-
56
- switch (data.method) {
57
- case "init":
58
- handleInit(data);
59
- break;
60
- case "writeFile":
61
- pyodide.FS.writeFile(data.filename, encoder.encode(data.content));
62
- break;
63
- case "runPython":
64
- runPython(data.python);
65
- break;
66
- case "stopPython":
67
- stopped = true;
68
- break;
69
- default:
70
- throw new Error(`Unsupported method: ${data.method}`);
71
- }
72
- };
73
-
74
- // eslint-disable-next-line no-restricted-globals
75
- addEventListener("message", async (event) => {
76
- onmessage(event);
77
- });
78
-
79
- const runPython = async (python) => {
80
- stopped = false;
81
-
82
- try {
83
- await withSupportForPackages(python, async () => {
84
- await pyodide.runPython(python);
85
- });
86
- } catch (error) {
87
- if (!(error instanceof pyodide.ffi.PythonError)) {
88
- throw error;
89
- }
90
- postMessage({ method: "handleError", ...parsePythonError(error) });
91
- }
92
-
93
- await clearPyodideData();
94
- };
95
-
96
- const checkIfStopped = () => {
97
- if (stopped) {
98
- throw new pyodide.ffi.PythonError("KeyboardInterrupt");
99
- }
100
- };
101
-
102
- const withSupportForPackages = async (
103
- python,
104
- runPythonFn = async () => {},
105
- ) => {
106
- const imports = await pyodide._api.pyodide_code.find_imports(python).toJs();
107
- await Promise.all(imports.map((name) => loadDependency(name)));
108
-
109
- checkIfStopped();
110
- await pyodide.loadPackagesFromImports(python);
111
-
112
- checkIfStopped();
113
- await pyodide.runPythonAsync(
114
- `
115
- import basthon
116
- import builtins
117
- import os
118
-
119
- MAX_FILES = 100
120
- MAX_FILE_SIZE = 8500000
121
-
122
- def _custom_open(filename, mode="r", *args, **kwargs):
123
- if "x" in mode and os.path.exists(filename):
124
- raise FileExistsError(f"File '{filename}' already exists")
125
- if ("w" in mode or "a" in mode or "x" in mode) and "b" not in mode:
126
- if len(os.listdir()) > MAX_FILES and not os.path.exists(filename):
127
- raise OSError(f"File system limit reached, no more than {MAX_FILES} files allowed")
128
- class CustomFile:
129
- def __init__(self, filename):
130
- self.filename = filename
131
- self.content = ""
132
-
133
- def write(self, content):
134
- self.content += content
135
- if len(self.content) > MAX_FILE_SIZE:
136
- raise OSError(f"File '{self.filename}' exceeds maximum file size of {MAX_FILE_SIZE} bytes")
137
- with _original_open(self.filename, mode) as f:
138
- f.write(self.content)
139
- basthon.kernel.write_file({ "filename": self.filename, "content": self.content, "mode": mode })
140
-
141
- def close(self):
142
- pass
143
-
144
- def __enter__(self):
145
- return self
146
-
147
- def __exit__(self, exc_type, exc_val, exc_tb):
148
- self.close()
149
-
150
- return CustomFile(filename)
151
- else:
152
- return _original_open(filename, mode, *args, **kwargs)
153
-
154
- # Override the built-in open function
155
- builtins.open = _custom_open
156
- `,
157
- { filename: "__custom_open__.py" },
158
- );
159
- await runPythonFn();
160
-
161
- for (let name of imports) {
162
- checkIfStopped();
163
- await vendoredPackages[name]?.after();
164
- }
165
- };
166
-
167
- const loadDependency = async (name) => {
168
- checkIfStopped();
169
-
170
- // If the import is for another user file then open it and load its dependencies.
171
- if (pyodide.FS.readdir("/home/pyodide").includes(`${name}.py`)) {
172
- const fileContent = pyodide.FS.readFile(`/home/pyodide/${name}.py`, {
173
- encoding: "utf8",
174
- });
175
- await withSupportForPackages(fileContent);
176
- return;
177
- }
178
-
179
- // If the import is for a vendored package then run its .before() hook.
180
- const vendoredPackage = vendoredPackages[name];
181
- await vendoredPackage?.before();
182
- if (vendoredPackage) {
183
- return;
184
- }
185
-
186
- // If the import is for a module built into Python then do nothing.
187
- let pythonModule;
188
- try {
189
- pythonModule = pyodide.pyimport(name);
190
- } catch (_) {}
191
- if (pythonModule) {
192
- return;
193
- }
194
-
195
- // If the import is for a package built into Pyodide then load it.
196
- // Built-ins: https://pyodide.org/en/stable/usage/packages-in-pyodide.html
197
- await pyodide.loadPackage(name)?.catch(() => {});
198
- let pyodidePackage;
199
- try {
200
- pyodidePackage = pyodide.pyimport(name);
201
- } catch (_) {}
202
- if (pyodidePackage) {
203
- return;
204
- }
205
-
206
- // Ensure micropip is loaded which can fetch packages from PyPi.
207
- // See: https://pyodide.org/en/stable/usage/loading-packages.html
208
- if (!pyodide.micropip) {
209
- await pyodide.loadPackage("micropip");
210
- pyodide.micropip = pyodide.pyimport("micropip");
211
- }
212
-
213
- // If the import is for a PyPi package then load it.
214
- // Otherwise, don't error now so that we get an error later from Python.
215
- await pyodide.micropip.install(name).catch(() => {});
216
- };
217
-
218
- const vendoredPackages = {
219
- // Support for https://pypi.org/project/py-enigma/ due to package not having a whl file on PyPi.
220
- enigma: {
221
- before: async () => {
222
- await pyodide.loadPackage(toAbsoluteFromOrigin(assets.enigmaWhlUrl));
223
- },
224
- after: () => {},
225
- },
226
- turtle: {
227
- before: async () => {
228
- pyodide.registerJsModule("basthon", fakeBasthonPackage);
229
- await pyodide.loadPackage(toAbsoluteFromOrigin(assets.turtleWhlUrl));
230
- },
231
- after: () =>
232
- pyodide.runPython(`
233
- import turtle
234
- import basthon
235
-
236
- svg_dict = turtle.Screen().show_scene()
237
- basthon.kernel.display_event({ "display_type": "turtle", "content": svg_dict })
238
- turtle.restart()
239
- `),
240
- },
241
- p5: {
242
- before: async () => {
243
- pyodide.registerJsModule("basthon", fakeBasthonPackage);
244
- await pyodide.loadPackage([
245
- "setuptools",
246
- toAbsoluteFromOrigin(assets.p5WhlUrl),
247
- ]);
248
- },
249
- after: () => {},
250
- },
251
- pygal: {
252
- before: () => {
253
- pyodide.registerJsModule("pygal", { ...pygal });
254
- pygal.config.renderChart = (content) => {
255
- postMessage({ method: "handleVisual", origin: "pygal", content });
256
- };
257
- },
258
- after: () => {},
259
- },
260
- matplotlib: {
261
- before: async () => {
262
- pyodide.registerJsModule("basthon", fakeBasthonPackage);
263
- // Patch the document object to prevent matplotlib from trying to render. Since we are running in a web worker,
264
- // the document object is not available. We will instead capture the image and send it back to the main thread.
265
- pyodide.runPython(`
266
- import js
267
-
268
- class __DummyDocument__:
269
- def __init__(self, *args, **kwargs) -> None:
270
- return
271
- def __getattr__(self, __name: str):
272
- return __DummyDocument__
273
- js.document = __DummyDocument__()
274
- `);
275
- await pyodide.loadPackage("matplotlib")?.catch(() => {});
276
- let pyodidePackage;
277
- try {
278
- pyodidePackage = pyodide.pyimport("matplotlib");
279
- } catch (_) {}
280
- if (pyodidePackage) {
281
- pyodide.runPython(`
282
- import matplotlib.pyplot as plt
283
- import io
284
- import basthon
285
-
286
- def show_chart():
287
- bytes_io = io.BytesIO()
288
- plt.savefig(bytes_io, format='jpg')
289
- bytes_io.seek(0)
290
- basthon.kernel.display_event({ "display_type": "matplotlib", "content": bytes_io.read() })
291
- plt.show = show_chart
292
- `);
293
- return;
294
- }
295
- },
296
- after: () => {
297
- pyodide.runPython(`
298
- import matplotlib.pyplot as plt
299
- plt.clf()
300
- `);
301
- },
302
- },
303
- seaborn: {
304
- before: async () => {
305
- pyodide.registerJsModule("basthon", fakeBasthonPackage);
306
- // Patch the document object to prevent matplotlib from trying to render. Since we are running in a web worker,
307
- // the document object is not available. We will instead capture the image and send it back to the main thread.
308
- pyodide.runPython(`
309
- import js
310
-
311
- class __DummyDocument__:
312
- def __init__(self, *args, **kwargs) -> None:
313
- return
314
- def __getattr__(self, __name: str):
315
- return __DummyDocument__
316
- js.document = __DummyDocument__()
317
- `);
318
-
319
- // Ensure micropip is loaded which can fetch packages from PyPi.
320
- // See: https://pyodide.org/en/stable/usage/loading-packages.html
321
- if (!pyodide.micropip) {
322
- await pyodide.loadPackage("micropip");
323
- pyodide.micropip = pyodide.pyimport("micropip");
324
- }
325
-
326
- // If the import is for a PyPi package then load it.
327
- // Otherwise, don't error now so that we get an error later from Python.
328
- await pyodide.micropip.install("seaborn").catch(() => {});
329
- },
330
- after: () => {
331
- pyodide.runPython(`
332
- import matplotlib.pyplot as plt
333
- import io
334
- import basthon
335
-
336
- def is_plot_empty():
337
- fig = plt.gcf()
338
- for ax in fig.get_axes():
339
- # Check if the axes contain any lines, patches, collections, etc.
340
- if ax.lines or ax.patches or ax.collections or ax.images or ax.texts:
341
- return False
342
- return True
343
-
344
- if not is_plot_empty():
345
- bytes_io = io.BytesIO()
346
- plt.savefig(bytes_io, format='jpg')
347
- bytes_io.seek(0)
348
- basthon.kernel.display_event({ "display_type": "matplotlib", "content": bytes_io.read() })
349
-
350
- plt.clf()
351
- `);
352
- },
353
- },
354
- plotly: {
355
- before: async () => {
356
- if (!pyodide.micropip) {
357
- await pyodide.loadPackage("micropip");
358
- pyodide.micropip = pyodide.pyimport("micropip");
359
- }
360
-
361
- // If the import is for a PyPi package then load it.
362
- // Otherwise, don't error now so that we get an error later from Python.
363
- await pyodide.micropip.install("plotly").catch(() => {});
364
- await pyodide.micropip.install("pandas").catch(() => {});
365
- pyodide.registerJsModule("basthon", fakeBasthonPackage);
366
- pyodide.runPython(`
367
- import plotly.graph_objs as go
368
-
369
- def _hacked_show(self, *args, **kwargs):
370
- basthon.kernel.display_event({
371
- "display_type": "plotly",
372
- "content": self.to_json()
373
- })
374
-
375
- go.Figure.show = _hacked_show
376
- `);
377
- },
378
- after: () => {},
379
- },
380
- };
381
-
382
- const fakeBasthonPackage = {
383
- kernel: {
384
- display_event: (event) => {
385
- const origin = event.toJs().get("display_type");
386
- const content = event.toJs().get("content");
387
-
388
- postMessage({ method: "handleVisual", origin, content });
389
- },
390
- write_file: (event) => {
391
- const filename = event.toJs().get("filename");
392
- const content = event.toJs().get("content");
393
- const mode = event.toJs().get("mode");
394
- postMessage({ method: "handleFileWrite", filename, content, mode });
395
- },
396
- locals: () => pyodide.runPython("globals()"),
397
- },
398
- };
399
-
400
- const clearPyodideData = async () => {
401
- postMessage({ method: "handleLoading" });
402
- console.log("clearPyodideData");
403
- await pyodide.runPythonAsync(`
404
- # Clear all user-defined variables and modules
405
- for name in dir():
406
- if not name.startswith('_') and not name=='basthon':
407
- del globals()[name]
408
- `);
409
- console.log("clearPyodideData done");
410
- postMessage({ method: "handleLoaded", stdinBuffer, interruptBuffer });
411
- };
412
-
413
- const initialisePyodide = async () => {
414
- postMessage({ method: "handleLoading" });
415
-
416
- pyodidePromise = loadPyodide({
417
- stdout: (content) =>
418
- postMessage({ method: "handleOutput", stream: "stdout", content }),
419
- stderr: (content) =>
420
- postMessage({ method: "handleOutput", stream: "stderr", content }),
421
- });
422
-
423
- pyodide = await pyodidePromise;
424
-
425
- pyodide.registerJsModule("basthon", fakeBasthonPackage);
426
-
427
- await pyodide.runPythonAsync(`
428
- __old_input__ = input
429
- def __patched_input__(prompt=False):
430
- if (prompt):
431
- print(prompt)
432
- return __old_input__()
433
- __builtins__.input = __patched_input__
434
- `);
435
-
436
- await pyodide.runPythonAsync(`
437
- import builtins
438
- # Save the original open function
439
- _original_open = builtins.open
440
- `);
441
-
442
- await pyodide.loadPackage("pyodide-http");
443
- await pyodide.runPythonAsync(`
444
- import pyodide_http
445
- pyodide_http.patch_all()
446
- `);
447
-
448
- if (supportsAllFeatures) {
449
- stdinBuffer =
450
- stdinBuffer || new Int32Array(new SharedArrayBuffer(1024 * 1024)); // 1 MiB
451
- stdinBuffer[0] = 1; // Store the length of content in the buffer at index 0.
452
- pyodide.setStdin({ isatty: true, read: readFromStdin });
453
-
454
- interruptBuffer =
455
- interruptBuffer || new Uint8Array(new SharedArrayBuffer(1));
456
- pyodide.setInterruptBuffer(interruptBuffer);
457
- }
458
-
459
- postMessage({ method: "handleLoaded", stdinBuffer, interruptBuffer });
460
- };
461
-
462
- const readFromStdin = (bufferToWrite) => {
463
- const previousLength = stdinBuffer[0];
464
- postMessage({ method: "handleInput" });
465
-
466
- while (true) {
467
- pyodide.checkInterrupt();
468
- const result = Atomics.wait(stdinBuffer, 0, previousLength, 100);
469
- if (result === "not-equal") {
470
- break;
471
- }
472
- }
473
-
474
- const currentLength = stdinBuffer[0];
475
- if (currentLength === -1) {
476
- return 0;
477
- } // Signals that stdin was closed.
478
-
479
- const addedBytes = stdinBuffer.slice(previousLength, currentLength);
480
- bufferToWrite.set(addedBytes);
481
-
482
- return addedBytes.length;
483
- };
484
-
485
- const parsePythonError = (error) => {
486
- const type = error.type;
487
- const [trace, info] = error.message.split(`${type}:`).map((s) => s?.trim());
488
-
489
- const lines = trace.split("\n");
490
-
491
- // if the third from last line matches /File "__custom_open__\.py", line (\d+)/g then strip off the last three lines
492
- if (
493
- lines.length > 3 &&
494
- /File "__custom_open__\.py", line (\d+)/g.test(lines[lines.length - 3])
495
- ) {
496
- lines.splice(-3, 3);
497
- }
498
-
499
- const snippetLine = lines[lines.length - 2]; // print("hi")invalid
500
- const caretLine = lines[lines.length - 1]; // ^^^^^^^
501
-
502
- const showsMistake = caretLine.includes("^");
503
- const mistake = showsMistake
504
- ? [snippetLine.slice(4), caretLine.slice(4)].join("\n")
505
- : "";
506
-
507
- const matches = [
508
- ...trace.matchAll(/File "(?!__custom_open__\.py)(.*)", line (\d+)/g),
509
- ];
510
- const match = matches[matches.length - 1];
511
-
512
- const path = match ? match[1] : "";
513
- const base = path.split("/").reverse()[0];
514
- const file = base === "<exec>" ? "main.py" : base;
515
-
516
- const line = match ? parseInt(match[2], 10) : "";
517
-
518
- return { file, line, mistake, type, info };
519
- };
520
-
521
- // return {
522
- // postMessage,
523
- // onmessage,
524
- // };
525
- };
526
-
527
- globalThis.PyodideWorker = PyodideWorker;
528
- PyodideWorker();
529
- // export default PyodideWorker;
1
+ /* global globalThis, importScripts, loadPyodide, SharedArrayBuffer, Atomics, pygal */
2
+
3
+ function toAbsoluteFromOrigin(url) {
4
+ if (url.startsWith("http") || url.startsWith("/")) return url;
5
+ return new URL(url, self.location.origin).href;
6
+ }
7
+
8
+ // Nest the PyodideWorker function inside a globalThis object so we control when its initialised.
9
+ const PyodideWorker = () => {
10
+ let assets;
11
+ let packageApiUrl;
12
+
13
+ const handleInit = (data) => {
14
+ assets = data.assets;
15
+ packageApiUrl = data.packageApiUrl;
16
+
17
+ // Import scripts dynamically based on the environment
18
+ console.log("PyodideWorker", "importing scripts");
19
+ importScripts(toAbsoluteFromOrigin(assets.pygalUrl));
20
+
21
+ assets.pyodideBaseUrl = `${packageApiUrl}/pyodide.js`;
22
+ importScripts(toAbsoluteFromOrigin(assets.pyodideBaseUrl));
23
+
24
+ initialisePyodide();
25
+ };
26
+
27
+ const supportsAllFeatures = typeof SharedArrayBuffer !== "undefined";
28
+
29
+ // eslint-disable-next-line no-restricted-globals
30
+ if (!supportsAllFeatures && name !== "incremental-features") {
31
+ console.warn(
32
+ [
33
+ "The code editor will not be able to capture standard input or stop execution because these HTTP headers are not set:",
34
+ " - Cross-Origin-Opener-Policy: same-origin",
35
+ " - Cross-Origin-Embedder-Policy: require-corp",
36
+ "",
37
+ "If your app can cope with or without these features, please initialize the web worker with { name: 'incremental-features' } to silence this warning.",
38
+ "You can then check for the presence of { stdinBuffer, interruptBuffer } in the handleLoaded message to check whether these features are supported.",
39
+ "",
40
+ "If you definitely need these features, either configure your server to respond with the HTTP headers above, or register a service worker.",
41
+ "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.",
42
+ "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.",
43
+ "",
44
+ "Please refer to these code snippets for registering a service worker:",
45
+ " - https://github.com/RaspberryPiFoundation/python-execution-prototypes/blob/fd2c50e032cba3bb0e92e19a88eb62e5b120fe7a/pyodide/index.html#L92-L98",
46
+ " - https://github.com/RaspberryPiFoundation/python-execution-prototypes/blob/fd2c50e032cba3bb0e92e19a88eb62e5b120fe7a/pyodide/serviceworker.js",
47
+ ].join("\n"),
48
+ );
49
+ }
50
+ let pyodide, pyodidePromise, stdinBuffer, interruptBuffer, stopped;
51
+
52
+ const onmessage = async ({ data }) => {
53
+ pyodide = await pyodidePromise;
54
+ let encoder = new TextEncoder();
55
+
56
+ switch (data.method) {
57
+ case "init":
58
+ handleInit(data);
59
+ break;
60
+ case "writeFile":
61
+ pyodide.FS.writeFile(data.filename, encoder.encode(data.content));
62
+ break;
63
+ case "runPython":
64
+ runPython(data.python);
65
+ break;
66
+ case "stopPython":
67
+ stopped = true;
68
+ break;
69
+ default:
70
+ throw new Error(`Unsupported method: ${data.method}`);
71
+ }
72
+ };
73
+
74
+ // eslint-disable-next-line no-restricted-globals
75
+ addEventListener("message", async (event) => {
76
+ onmessage(event);
77
+ });
78
+
79
+ const runPython = async (python) => {
80
+ stopped = false;
81
+
82
+ try {
83
+ await withSupportForPackages(python, async () => {
84
+ await pyodide.runPython(python);
85
+ });
86
+ } catch (error) {
87
+ if (!(error instanceof pyodide.ffi.PythonError)) {
88
+ throw error;
89
+ }
90
+ postMessage({ method: "handleError", ...parsePythonError(error) });
91
+ }
92
+
93
+ await clearPyodideData();
94
+ };
95
+
96
+ const checkIfStopped = () => {
97
+ if (stopped) {
98
+ throw new pyodide.ffi.PythonError("KeyboardInterrupt");
99
+ }
100
+ };
101
+
102
+ const withSupportForPackages = async (
103
+ python,
104
+ runPythonFn = async () => {},
105
+ ) => {
106
+ const imports = await pyodide._api.pyodide_code.find_imports(python).toJs();
107
+ await Promise.all(imports.map((name) => loadDependency(name)));
108
+
109
+ checkIfStopped();
110
+ await pyodide.loadPackagesFromImports(python);
111
+
112
+ checkIfStopped();
113
+ await pyodide.runPythonAsync(
114
+ `
115
+ import basthon
116
+ import builtins
117
+ import os
118
+
119
+ MAX_FILES = 100
120
+ MAX_FILE_SIZE = 8500000
121
+
122
+ def _custom_open(filename, mode="r", *args, **kwargs):
123
+ if "x" in mode and os.path.exists(filename):
124
+ raise FileExistsError(f"File '{filename}' already exists")
125
+ if ("w" in mode or "a" in mode or "x" in mode) and "b" not in mode:
126
+ if len(os.listdir()) > MAX_FILES and not os.path.exists(filename):
127
+ raise OSError(f"File system limit reached, no more than {MAX_FILES} files allowed")
128
+ class CustomFile:
129
+ def __init__(self, filename):
130
+ self.filename = filename
131
+ self.content = ""
132
+
133
+ def write(self, content):
134
+ self.content += content
135
+ if len(self.content) > MAX_FILE_SIZE:
136
+ raise OSError(f"File '{self.filename}' exceeds maximum file size of {MAX_FILE_SIZE} bytes")
137
+ with _original_open(self.filename, mode) as f:
138
+ f.write(self.content)
139
+ basthon.kernel.write_file({ "filename": self.filename, "content": self.content, "mode": mode })
140
+
141
+ def close(self):
142
+ pass
143
+
144
+ def __enter__(self):
145
+ return self
146
+
147
+ def __exit__(self, exc_type, exc_val, exc_tb):
148
+ self.close()
149
+
150
+ return CustomFile(filename)
151
+ else:
152
+ return _original_open(filename, mode, *args, **kwargs)
153
+
154
+ # Override the built-in open function
155
+ builtins.open = _custom_open
156
+ `,
157
+ { filename: "__custom_open__.py" },
158
+ );
159
+ await runPythonFn();
160
+
161
+ for (let name of imports) {
162
+ checkIfStopped();
163
+ await vendoredPackages[name]?.after();
164
+ }
165
+ };
166
+
167
+ const loadDependency = async (name) => {
168
+ checkIfStopped();
169
+
170
+ // If the import is for another user file then open it and load its dependencies.
171
+ if (pyodide.FS.readdir("/home/pyodide").includes(`${name}.py`)) {
172
+ const fileContent = pyodide.FS.readFile(`/home/pyodide/${name}.py`, {
173
+ encoding: "utf8",
174
+ });
175
+ await withSupportForPackages(fileContent);
176
+ return;
177
+ }
178
+
179
+ // If the import is for a vendored package then run its .before() hook.
180
+ const vendoredPackage = vendoredPackages[name];
181
+ await vendoredPackage?.before();
182
+ if (vendoredPackage) {
183
+ return;
184
+ }
185
+
186
+ // If the import is for a module built into Python then do nothing.
187
+ let pythonModule;
188
+ try {
189
+ pythonModule = pyodide.pyimport(name);
190
+ } catch (_) {}
191
+ if (pythonModule) {
192
+ return;
193
+ }
194
+
195
+ // If the import is for a package built into Pyodide then load it.
196
+ // Built-ins: https://pyodide.org/en/stable/usage/packages-in-pyodide.html
197
+ await pyodide.loadPackage(name)?.catch(() => {});
198
+ let pyodidePackage;
199
+ try {
200
+ pyodidePackage = pyodide.pyimport(name);
201
+ } catch (_) {}
202
+ if (pyodidePackage) {
203
+ return;
204
+ }
205
+
206
+ // Ensure micropip is loaded which can fetch packages from PyPi.
207
+ // See: https://pyodide.org/en/stable/usage/loading-packages.html
208
+ if (!pyodide.micropip) {
209
+ await pyodide.loadPackage("micropip");
210
+ pyodide.micropip = pyodide.pyimport("micropip");
211
+ }
212
+
213
+ // If the import is for a PyPi package then load it.
214
+ // Otherwise, don't error now so that we get an error later from Python.
215
+ await pyodide.micropip.install(name).catch(() => {});
216
+ };
217
+
218
+ const vendoredPackages = {
219
+ // Support for https://pypi.org/project/py-enigma/ due to package not having a whl file on PyPi.
220
+ enigma: {
221
+ before: async () => {
222
+ await pyodide.loadPackage(toAbsoluteFromOrigin(assets.enigmaWhlUrl));
223
+ },
224
+ after: () => {},
225
+ },
226
+ turtle: {
227
+ before: async () => {
228
+ pyodide.registerJsModule("basthon", fakeBasthonPackage);
229
+ await pyodide.loadPackage(toAbsoluteFromOrigin(assets.turtleWhlUrl));
230
+ },
231
+ after: () =>
232
+ pyodide.runPython(`
233
+ import turtle
234
+ import basthon
235
+
236
+ svg_dict = turtle.Screen().show_scene()
237
+ basthon.kernel.display_event({ "display_type": "turtle", "content": svg_dict })
238
+ turtle.restart()
239
+ `),
240
+ },
241
+ p5: {
242
+ before: async () => {
243
+ pyodide.registerJsModule("basthon", fakeBasthonPackage);
244
+ await pyodide.loadPackage([
245
+ "setuptools",
246
+ toAbsoluteFromOrigin(assets.p5WhlUrl),
247
+ ]);
248
+ },
249
+ after: () => {},
250
+ },
251
+ pygal: {
252
+ before: () => {
253
+ pyodide.registerJsModule("pygal", { ...pygal });
254
+ pygal.config.renderChart = (content) => {
255
+ postMessage({ method: "handleVisual", origin: "pygal", content });
256
+ };
257
+ },
258
+ after: () => {},
259
+ },
260
+ matplotlib: {
261
+ before: async () => {
262
+ pyodide.registerJsModule("basthon", fakeBasthonPackage);
263
+ // Patch the document object to prevent matplotlib from trying to render. Since we are running in a web worker,
264
+ // the document object is not available. We will instead capture the image and send it back to the main thread.
265
+ pyodide.runPython(`
266
+ import js
267
+
268
+ class __DummyDocument__:
269
+ def __init__(self, *args, **kwargs) -> None:
270
+ return
271
+ def __getattr__(self, __name: str):
272
+ return __DummyDocument__
273
+ js.document = __DummyDocument__()
274
+ `);
275
+ await pyodide.loadPackage("matplotlib")?.catch(() => {});
276
+ let pyodidePackage;
277
+ try {
278
+ pyodidePackage = pyodide.pyimport("matplotlib");
279
+ } catch (_) {}
280
+ if (pyodidePackage) {
281
+ pyodide.runPython(`
282
+ import matplotlib.pyplot as plt
283
+ import io
284
+ import basthon
285
+
286
+ def show_chart():
287
+ bytes_io = io.BytesIO()
288
+ plt.savefig(bytes_io, format='jpg')
289
+ bytes_io.seek(0)
290
+ basthon.kernel.display_event({ "display_type": "matplotlib", "content": bytes_io.read() })
291
+ plt.show = show_chart
292
+ `);
293
+ return;
294
+ }
295
+ },
296
+ after: () => {
297
+ pyodide.runPython(`
298
+ import matplotlib.pyplot as plt
299
+ plt.clf()
300
+ `);
301
+ },
302
+ },
303
+ seaborn: {
304
+ before: async () => {
305
+ pyodide.registerJsModule("basthon", fakeBasthonPackage);
306
+ // Patch the document object to prevent matplotlib from trying to render. Since we are running in a web worker,
307
+ // the document object is not available. We will instead capture the image and send it back to the main thread.
308
+ pyodide.runPython(`
309
+ import js
310
+
311
+ class __DummyDocument__:
312
+ def __init__(self, *args, **kwargs) -> None:
313
+ return
314
+ def __getattr__(self, __name: str):
315
+ return __DummyDocument__
316
+ js.document = __DummyDocument__()
317
+ `);
318
+
319
+ // Ensure micropip is loaded which can fetch packages from PyPi.
320
+ // See: https://pyodide.org/en/stable/usage/loading-packages.html
321
+ if (!pyodide.micropip) {
322
+ await pyodide.loadPackage("micropip");
323
+ pyodide.micropip = pyodide.pyimport("micropip");
324
+ }
325
+
326
+ // If the import is for a PyPi package then load it.
327
+ // Otherwise, don't error now so that we get an error later from Python.
328
+ await pyodide.micropip.install("seaborn").catch(() => {});
329
+ },
330
+ after: () => {
331
+ pyodide.runPython(`
332
+ import matplotlib.pyplot as plt
333
+ import io
334
+ import basthon
335
+
336
+ def is_plot_empty():
337
+ fig = plt.gcf()
338
+ for ax in fig.get_axes():
339
+ # Check if the axes contain any lines, patches, collections, etc.
340
+ if ax.lines or ax.patches or ax.collections or ax.images or ax.texts:
341
+ return False
342
+ return True
343
+
344
+ if not is_plot_empty():
345
+ bytes_io = io.BytesIO()
346
+ plt.savefig(bytes_io, format='jpg')
347
+ bytes_io.seek(0)
348
+ basthon.kernel.display_event({ "display_type": "matplotlib", "content": bytes_io.read() })
349
+
350
+ plt.clf()
351
+ `);
352
+ },
353
+ },
354
+ plotly: {
355
+ before: async () => {
356
+ if (!pyodide.micropip) {
357
+ await pyodide.loadPackage("micropip");
358
+ pyodide.micropip = pyodide.pyimport("micropip");
359
+ }
360
+
361
+ // If the import is for a PyPi package then load it.
362
+ // Otherwise, don't error now so that we get an error later from Python.
363
+ await pyodide.micropip.install("plotly").catch(() => {});
364
+ await pyodide.micropip.install("pandas").catch(() => {});
365
+ pyodide.registerJsModule("basthon", fakeBasthonPackage);
366
+ pyodide.runPython(`
367
+ import plotly.graph_objs as go
368
+
369
+ def _hacked_show(self, *args, **kwargs):
370
+ basthon.kernel.display_event({
371
+ "display_type": "plotly",
372
+ "content": self.to_json()
373
+ })
374
+
375
+ go.Figure.show = _hacked_show
376
+ `);
377
+ },
378
+ after: () => {},
379
+ },
380
+ };
381
+
382
+ const fakeBasthonPackage = {
383
+ kernel: {
384
+ display_event: (event) => {
385
+ const origin = event.toJs().get("display_type");
386
+ const content = event.toJs().get("content");
387
+
388
+ postMessage({ method: "handleVisual", origin, content });
389
+ },
390
+ write_file: (event) => {
391
+ const filename = event.toJs().get("filename");
392
+ const content = event.toJs().get("content");
393
+ const mode = event.toJs().get("mode");
394
+ postMessage({ method: "handleFileWrite", filename, content, mode });
395
+ },
396
+ locals: () => pyodide.runPython("globals()"),
397
+ },
398
+ };
399
+
400
+ const clearPyodideData = async () => {
401
+ postMessage({ method: "handleLoading" });
402
+ console.log("clearPyodideData");
403
+ await pyodide.runPythonAsync(`
404
+ # Clear all user-defined variables and modules
405
+ for name in dir():
406
+ if not name.startswith('_') and not name=='basthon':
407
+ del globals()[name]
408
+ `);
409
+ console.log("clearPyodideData done");
410
+ postMessage({ method: "handleLoaded", stdinBuffer, interruptBuffer });
411
+ };
412
+
413
+ const initialisePyodide = async () => {
414
+ postMessage({ method: "handleLoading" });
415
+
416
+ pyodidePromise = loadPyodide({
417
+ stdout: (content) =>
418
+ postMessage({ method: "handleOutput", stream: "stdout", content }),
419
+ stderr: (content) =>
420
+ postMessage({ method: "handleOutput", stream: "stderr", content }),
421
+ });
422
+
423
+ pyodide = await pyodidePromise;
424
+
425
+ pyodide.registerJsModule("basthon", fakeBasthonPackage);
426
+
427
+ await pyodide.runPythonAsync(`
428
+ __old_input__ = input
429
+ def __patched_input__(prompt=False):
430
+ if (prompt):
431
+ print(prompt)
432
+ return __old_input__()
433
+ __builtins__.input = __patched_input__
434
+ `);
435
+
436
+ await pyodide.runPythonAsync(`
437
+ import builtins
438
+ # Save the original open function
439
+ _original_open = builtins.open
440
+ `);
441
+
442
+ await pyodide.loadPackage("pyodide-http");
443
+ await pyodide.runPythonAsync(`
444
+ import pyodide_http
445
+ pyodide_http.patch_all()
446
+ `);
447
+
448
+ if (supportsAllFeatures) {
449
+ stdinBuffer =
450
+ stdinBuffer || new Int32Array(new SharedArrayBuffer(1024 * 1024)); // 1 MiB
451
+ stdinBuffer[0] = 1; // Store the length of content in the buffer at index 0.
452
+ pyodide.setStdin({ isatty: true, read: readFromStdin });
453
+
454
+ interruptBuffer =
455
+ interruptBuffer || new Uint8Array(new SharedArrayBuffer(1));
456
+ pyodide.setInterruptBuffer(interruptBuffer);
457
+ }
458
+
459
+ postMessage({ method: "handleLoaded", stdinBuffer, interruptBuffer });
460
+ };
461
+
462
+ const readFromStdin = (bufferToWrite) => {
463
+ const previousLength = stdinBuffer[0];
464
+ postMessage({ method: "handleInput" });
465
+
466
+ while (true) {
467
+ pyodide.checkInterrupt();
468
+ const result = Atomics.wait(stdinBuffer, 0, previousLength, 100);
469
+ if (result === "not-equal") {
470
+ break;
471
+ }
472
+ }
473
+
474
+ const currentLength = stdinBuffer[0];
475
+ if (currentLength === -1) {
476
+ return 0;
477
+ } // Signals that stdin was closed.
478
+
479
+ const addedBytes = stdinBuffer.slice(previousLength, currentLength);
480
+ bufferToWrite.set(addedBytes);
481
+
482
+ return addedBytes.length;
483
+ };
484
+
485
+ const parsePythonError = (error) => {
486
+ const type = error.type;
487
+ const [trace, info] = error.message.split(`${type}:`).map((s) => s?.trim());
488
+
489
+ const lines = trace.split("\n");
490
+
491
+ // if the third from last line matches /File "__custom_open__\.py", line (\d+)/g then strip off the last three lines
492
+ if (
493
+ lines.length > 3 &&
494
+ /File "__custom_open__\.py", line (\d+)/g.test(lines[lines.length - 3])
495
+ ) {
496
+ lines.splice(-3, 3);
497
+ }
498
+
499
+ const snippetLine = lines[lines.length - 2]; // print("hi")invalid
500
+ const caretLine = lines[lines.length - 1]; // ^^^^^^^
501
+
502
+ const showsMistake = caretLine.includes("^");
503
+ const mistake = showsMistake
504
+ ? [snippetLine.slice(4), caretLine.slice(4)].join("\n")
505
+ : "";
506
+
507
+ const matches = [
508
+ ...trace.matchAll(/File "(?!__custom_open__\.py)(.*)", line (\d+)/g),
509
+ ];
510
+ const match = matches[matches.length - 1];
511
+
512
+ const path = match ? match[1] : "";
513
+ const base = path.split("/").reverse()[0];
514
+ const file = base === "<exec>" ? "main.py" : base;
515
+
516
+ const line = match ? parseInt(match[2], 10) : "";
517
+
518
+ return { file, line, mistake, type, info };
519
+ };
520
+
521
+ // return {
522
+ // postMessage,
523
+ // onmessage,
524
+ // };
525
+ };
526
+
527
+ globalThis.PyodideWorker = PyodideWorker;
528
+ PyodideWorker();
529
+ // export default PyodideWorker;