gwchq-textjam 0.1.115 → 0.1.118

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.
@@ -240,6 +240,13 @@ const PyodideWorker = () => {
240
240
  // user console while resolving imports and loading packages.
241
241
  suppressInternalStdStreams = true;
242
242
  const imports = await pyodide._api.pyodide_code.find_imports(python).toJs();
243
+
244
+ await pyodide.runPythonAsync(`
245
+ import builtins
246
+ if hasattr(builtins, "_original_open"):
247
+ builtins.open = builtins._original_open
248
+ `);
249
+
243
250
  await Promise.all(imports.map((name) => loadDependency(name)));
244
251
 
245
252
  checkIfStopped();
@@ -256,6 +263,11 @@ const PyodideWorker = () => {
256
263
  MAX_FILE_SIZE = 8500000
257
264
  PROJECT_ROOT = os.path.abspath("${WORKING_DIR}")
258
265
 
266
+ if not hasattr(builtins, "_original_open"):
267
+ builtins._original_open = builtins.open
268
+
269
+ ORIGINAL_OPEN = builtins._original_open
270
+
259
271
  def _is_project_file(filename):
260
272
  abs_path = os.path.abspath(filename)
261
273
  return abs_path == PROJECT_ROOT or abs_path.startswith(PROJECT_ROOT + os.sep)
@@ -265,6 +277,7 @@ const PyodideWorker = () => {
265
277
  return os.path.relpath(abs_path, PROJECT_ROOT)
266
278
 
267
279
  def _custom_open(filename, mode="r", *args, **kwargs):
280
+ import os
268
281
  abs_path = os.path.abspath(filename)
269
282
 
270
283
  if "x" in mode and os.path.exists(abs_path):
@@ -285,7 +298,7 @@ const PyodideWorker = () => {
285
298
  self.content += content
286
299
  if len(self.content) > MAX_FILE_SIZE:
287
300
  raise OSError(f"File '{self.filename}' exceeds maximum file size of {MAX_FILE_SIZE} bytes")
288
- with _original_open(self.filename, mode, *args, **kwargs) as f:
301
+ with ORIGINAL_OPEN(self.filename, mode, *args, **kwargs) as f:
289
302
  f.write(self.content)
290
303
  basthon.kernel.write_file({
291
304
  "filename": _to_project_relative(self.filename),
@@ -304,7 +317,7 @@ const PyodideWorker = () => {
304
317
 
305
318
  return CustomFile(abs_path)
306
319
  else:
307
- return _original_open(filename, mode, *args, **kwargs)
320
+ return ORIGINAL_OPEN(filename, mode, *args, **kwargs)
308
321
 
309
322
  # Override the built-in open function
310
323
  builtins.open = _custom_open
@@ -592,18 +605,24 @@ const PyodideWorker = () => {
592
605
  postMessage({ method: "handleLoading" });
593
606
  try {
594
607
  await pyodide.runPythonAsync(`
595
- # Clear all user-defined variables and modules
596
- for name in list(globals()):
597
- if not name.startswith('_') and not name=='basthon':
598
- del globals()[name]
599
-
600
- import sys
601
- # Remove user modules from sys.modules
602
- user_modules = ${JSON.stringify(userModuleNames)}
603
- for name in user_modules:
604
- if name in sys.modules:
605
- del sys.modules[name]
606
- `);
608
+ import builtins
609
+ import sys
610
+
611
+ # Restore the real open before cleaning up
612
+ if hasattr(builtins, "_original_open"):
613
+ builtins.open = builtins._original_open
614
+
615
+ # Remove user modules from sys.modules
616
+ user_modules = ${JSON.stringify(userModuleNames)}
617
+ for name in user_modules:
618
+ if name in sys.modules:
619
+ del sys.modules[name]
620
+
621
+ # Clear all user-defined variables and modules
622
+ for name in list(globals()):
623
+ if not name.startswith('_') and name not in ('basthon', 'sys', 'builtins'):
624
+ del globals()[name]
625
+ `);
607
626
  } catch (error) {
608
627
  console.error("Error while clearing Pyodide data:", error);
609
628
  }
@@ -662,9 +681,11 @@ const PyodideWorker = () => {
662
681
  `);
663
682
 
664
683
  await pyodide.runPythonAsync(`
665
- import builtins
666
- # Save the original open function
667
- _original_open = builtins.open
684
+ import builtins
685
+ # Save the original open function only once
686
+ if not hasattr(builtins, "_original_open"):
687
+ builtins._original_open = builtins.open
688
+ _original_open = builtins._original_open
668
689
  `);
669
690
 
670
691
  await pyodide.loadPackage("pyodide-http");