Skip to content

Commit 67987ab

Browse files
authored
Fix issue with basilisp.io/writer :append failing (#742)
Hi, could you please review fix to make `basilisp.io/writer`'s :append true option working. It fixes #741. I've also added some basic tests for it. Thanks Co-authored-by: ikappaki <[email protected]>
1 parent 2669d72 commit 67987ab

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
* Fixed issue with import modules aliasing using ns eval (#719)
2828
* Fix issue with `ns-resolve` throwing an error on macros (#720)
2929
* Fix issue with py module `readerwritelock` locks handling (#722)
30+
* Fix issue with basilisp.io/writer :append mode not working (#741).
3031

3132
### Removed
3233
* Removed the dependency `astor` for versions of Python 3.9+ (#736)

src/basilisp/io.lpy

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@
8080

8181
(defn- clean-writer-mode
8282
[opts]
83-
(let [mode (:mode opts "w")
83+
(let [append? (:append opts)
84+
mode (:mode opts "")
8485
clean-mode (cond
8586
(:append opts) (str "a" mode)
8687
(not (str/includes? mode "w")) (str "w" mode)
@@ -96,7 +97,8 @@
9697
(ex-info "Writers may only be open in write or append mode"
9798
{:mode mode})))
9899

99-
(assoc opts :mode clean-mode)))
100+
(-> (assoc opts :mode clean-mode)
101+
(dissoc :append))))
100102

101103
(defn- clean-binary-mode
102104
[opts]
@@ -325,6 +327,11 @@
325327
The writer instances returned are always text-based, not binary. In general, the
326328
writers should be compatible with Python's ``io.TextIOBase`` interface.
327329

330+
``opts`` is an optional collection of keyword/value pairs
331+
transmitted as a map to the writer. The acceptable keywords align
332+
with those recognized by the fn:``open`` function. Moreover, setting the
333+
:append option to true will configure the writer for append mode.
334+
328335
Callers should take care to open a writer instance using
329336
:lpy:fn:`basilisp.core/with-open` to ensure that any resources are properly closed
330337
afterwards. Note that for in-memory IO buffers such as ``io.BytesIO`` and

tests/basilisp/test_io.lpy

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,22 @@
175175
(os/close fd)
176176
(os/unlink filename)))))))
177177

178+
(testing "writer local files"
179+
(let [path (bio/path *tempdir* "writer-test-writer-local-files.txt")]
180+
(testing "writer file object write"
181+
(with-open [f (bio/writer path)]
182+
(.write f "some file contents"))
183+
(is (= "some file contents" (slurp path))))
184+
185+
(testing "writer file object append"
186+
(with-open [f (bio/writer path :append true)]
187+
(.write f " and then some more"))
188+
(is (= "some file contents and then some more" (slurp path)))
189+
190+
(with-open [f (bio/writer path :append false)]
191+
(.write f "some other content"))
192+
(is (= "some other content" (slurp path))))))
193+
178194
(testing "http requests"
179195
(let [url (str "http://localhost:" *http-port* "/writer-http-req.txt")]
180196
(is (thrown? basilisp.lang.exception/ExceptionInfo

0 commit comments

Comments
 (0)