Skip to content

Commit 3231591

Browse files
authored
Fixing the append file write mode (#1200)
## What's Changed? - Fixed the mode on the `pyodide` side - it was always using `w` rather than the mode provided in the code - Fixed the file write append mode on the frontend - it was incorrectly adding a newline character before anything was appended.
1 parent 59a693e commit 3231591

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## Unreleased
8+
9+
### Fixed
10+
11+
- Bugs in append mode for writing to files in python (#1200)
12+
713
## [0.29.1] - 2025-02-21
814

915
### Fixed

src/PyodideWorker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ const PyodideWorker = () => {
116116
self.content += content
117117
if len(self.content) > MAX_FILE_SIZE:
118118
raise OSError(f"File '{self.filename}' exceeds maximum file size of {MAX_FILE_SIZE} bytes")
119-
with _original_open(self.filename, "w") as f:
119+
with _original_open(self.filename, mode) as f:
120120
f.write(self.content)
121121
basthon.kernel.write_file({ "filename": self.filename, "content": self.content, "mode": mode })
122122

src/components/Editor/Runners/PythonRunner/PyodideRunner/PyodideRunner.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ const PyodideRunner = ({ active, outputPanels = ["text", "visual"] }) => {
223223
updatedContent = content;
224224
} else if (mode === "a") {
225225
updatedContent =
226-
(componentToUpdate ? componentToUpdate.content + "\n" : "") + content;
226+
(componentToUpdate ? componentToUpdate.content : "") + content;
227227
}
228228

229229
if (componentToUpdate) {

src/components/Editor/Runners/PythonRunner/PyodideRunner/PyodideRunner.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ describe("When file write event is received", () => {
307307
worker.postMessageFromWorker({
308308
method: "handleFileWrite",
309309
filename: "existing_file.txt",
310-
content: "new content",
310+
content: "\nnew content",
311311
mode: "a",
312312
});
313313
expect(dispatchSpy).toHaveBeenCalledWith({
@@ -364,7 +364,7 @@ describe("When file write event is received", () => {
364364
worker.postMessageFromWorker({
365365
method: "handleFileWrite",
366366
filename: "existing_file.txt",
367-
content: "new content",
367+
content: "\nnew content",
368368
mode: "a",
369369
});
370370
expect(dispatchSpy).toHaveBeenCalledWith({

0 commit comments

Comments
 (0)