Skip to content

Commit

Permalink
getting for open as f and mode=x working
Browse files Browse the repository at this point in the history
  • Loading branch information
loiswells97 committed Dec 11, 2024
1 parent eee4c1d commit 2ea87a0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/PyodideWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,15 @@ const PyodideWorker = () => {
await pyodide.runPythonAsync(`
import basthon
import builtins
import os
# Save the original open function
_original_open = builtins.open
def _custom_open(filename, mode="r", *args, **kwargs):
if "w" in mode or "a" in mode:
if "x" in mode and os.path.exists(filename):
raise FileExistsError(f"File '{filename}' already exists")
if "w" in mode or "a" in mode or "x" in mode:
class CustomFile:
def __init__(self, filename):
self.filename = filename
Expand All @@ -400,6 +403,12 @@ const PyodideWorker = () => {
def close(self):
pass
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
return CustomFile(filename)
else:
return _original_open(filename, mode, *args, **kwargs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const VisualOutputPane = ({ visuals, setVisuals }) => {
(item) => item.extension === extension && item.name === name

Check failure on line 74 in src/components/Editor/Runners/PythonRunner/PyodideRunner/VisualOutputPane.jsx

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
);
let updatedContent;
if (content.mode === "w") {
if (content.mode === "w" || content.mode === "x") {
updatedContent = content.content;
} else if (content.mode === "a") {
updatedContent = componentToUpdate.content + "\n" + content.content;
Expand Down

0 comments on commit 2ea87a0

Please sign in to comment.