Skip to content

Commit be3bb4d

Browse files
Emit OS specific line endings for prn and println (#811)
Hi, can you please consider patch to emit OS specific line endings for print fns. It fixes #810. thanks Co-authored-by: ikappaki <[email protected]> Co-authored-by: Chris Rink <[email protected]>
1 parent 0183ca2 commit be3bb4d

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2525
* Loosen the dependency specification for Immutables and Pyrsistent to allow for a wider version range (#805)
2626
* Allow `case` forms with only a default expression (#807)
2727
* Make `pr` a dynamic variable (#820)
28+
* Emit OS specific line endings for the `println` and `prn` fns (#810)
2829

2930
### Fixed
3031
* Fix issue with `(count nil)` throwing an exception (#759)

src/basilisp/core.lpy

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4149,12 +4149,12 @@
41494149
(defn prn
41504150
"Same as :lpy:fn:`pr`, but appending a newline afterwards."
41514151
([]
4152-
(.write *out* \newline)
4152+
(.write *out* os/linesep)
41534153
nil)
41544154
([x]
41554155
(let [stdout *out*]
41564156
(.write stdout (repr x))
4157-
(.write stdout \newline)
4157+
(.write stdout os/linesep)
41584158
nil))
41594159
([x & args]
41604160
(let [stdout *out*
@@ -4163,7 +4163,7 @@
41634163
(.write stdout (repr x))
41644164
(.write stdout sep)
41654165
(.write stdout (apply str repr-args))
4166-
(.write stdout \newline)
4166+
(.write stdout os/linesep)
41674167
nil)))
41684168

41694169
(defn pr-str
@@ -4204,7 +4204,7 @@
42044204
([x]
42054205
(let [stdout *out*]
42064206
(.write stdout (basilisp.lang.runtime/lstr x))
4207-
(.write stdout \newline)
4207+
(.write stdout os/linesep)
42084208
nil))
42094209
([x & args]
42104210
(let [stdout *out*
@@ -4213,7 +4213,7 @@
42134213
(.write stdout (basilisp.lang.runtime/lstr x))
42144214
(.write stdout sep)
42154215
(.write stdout (apply str repr-args))
4216-
(.write stdout \newline)
4216+
(.write stdout os/linesep)
42174217
nil)))
42184218

42194219
(defn print-str

tests/basilisp/cli_test.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_nothing_to_uninstall(self, tmp_path: pathlib.Path, run_cli, capsys):
116116

117117
def test_debug_flag(run_cli):
118118
result = run_cli(["run", "--disable-ns-cache", "true", "-c", "(println (+ 1 2))"])
119-
assert "3\n" == result.lisp_out
119+
assert f"3{os.linesep}" == result.lisp_out
120120
assert os.environ["BASILISP_DO_NOT_CACHE_NAMESPACES"].lower() == "true"
121121

122122

@@ -125,14 +125,14 @@ def test_no_flag(self, run_cli):
125125
result = run_cli(
126126
["run", "--warn-on-var-indirection", "-c", "(println (+ 1 2))"]
127127
)
128-
assert "3\n" == result.lisp_out
128+
assert f"3{os.linesep}" == result.lisp_out
129129

130130
@pytest.mark.parametrize("val", BOOL_TRUE | BOOL_FALSE)
131131
def test_valid_flag(self, run_cli, val):
132132
result = run_cli(
133133
["run", "--warn-on-var-indirection", val, "-c", "(println (+ 1 2))"]
134134
)
135-
assert "3\n" == result.lisp_out
135+
assert f"3{os.linesep}" == result.lisp_out
136136

137137
@pytest.mark.parametrize("val", ["maybe", "not-no", "4"])
138138
def test_invalid_flag(self, run_cli, val):
@@ -211,10 +211,10 @@ def test_other_exception(self, run_cli):
211211

212212
class TestRun:
213213
cli_args_params = [
214-
([], "0\n"),
215-
(["--"], "0\n"),
216-
(["1", "2", "3"], "6\n"),
217-
(["--", "1", "2", "3"], "6\n"),
214+
([], f"0{os.linesep}"),
215+
(["--"], f"0{os.linesep}"),
216+
(["1", "2", "3"], f"6{os.linesep}"),
217+
(["--", "1", "2", "3"], f"6{os.linesep}"),
218218
]
219219
cli_args_code = "(println (apply + (map int *command-line-args*)))"
220220

@@ -224,11 +224,11 @@ def test_run_ns_and_code_mutually_exclusive(self, run_cli):
224224

225225
def test_run_code(self, run_cli):
226226
result = run_cli(["run", "-c", "(println (+ 1 2))"])
227-
assert "3\n" == result.lisp_out
227+
assert f"3{os.linesep}" == result.lisp_out
228228

229229
def test_run_code_main_ns(self, run_cli):
230230
result = run_cli(["run", "-c", "(println *main-ns*)"])
231-
assert "nil\n" == result.lisp_out
231+
assert f"nil{os.linesep}" == result.lisp_out
232232

233233
@pytest.mark.parametrize("args,ret", cli_args_params)
234234
def test_run_code_with_args(self, run_cli, args: List[str], ret: str):
@@ -239,13 +239,13 @@ def test_run_file(self, isolated_filesystem, run_cli):
239239
with open("test.lpy", mode="w") as f:
240240
f.write("(println (+ 1 2))")
241241
result = run_cli(["run", "test.lpy"])
242-
assert "3\n" == result.lisp_out
242+
assert f"3{os.linesep}" == result.lisp_out
243243

244244
def test_run_file_main_ns(self, isolated_filesystem, run_cli):
245245
with open("test.lpy", mode="w") as f:
246246
f.write("(println *main-ns*)")
247247
result = run_cli(["run", "test.lpy"])
248-
assert "nil\n" == result.lisp_out
248+
assert f"nil{os.linesep}" == result.lisp_out
249249

250250
@pytest.mark.parametrize("args,ret", cli_args_params)
251251
def test_run_file_with_args(
@@ -275,14 +275,14 @@ def test_cannot_run_namespace_with_in_ns_arg(
275275
def test_run_namespace(self, run_cli, namespace_file: pathlib.Path):
276276
namespace_file.write_text("(println (+ 1 2))")
277277
result = run_cli(["run", "-n", "package.core"])
278-
assert "3\n" == result.lisp_out
278+
assert f"3{os.linesep}" == result.lisp_out
279279

280280
def test_run_namespace_main_ns(self, run_cli, namespace_file: pathlib.Path):
281281
namespace_file.write_text(
282282
"(ns package.core) (println (name *ns*)) (println *main-ns*)"
283283
)
284284
result = run_cli(["run", "-n", "package.core"])
285-
assert "package.core\npackage.core\n" == result.lisp_out
285+
assert f"package.core{os.linesep}package.core{os.linesep}" == result.lisp_out
286286

287287
@pytest.mark.parametrize("args,ret", cli_args_params)
288288
def test_run_namespace_with_args(
@@ -294,11 +294,11 @@ def test_run_namespace_with_args(
294294

295295
def test_run_stdin(self, run_cli):
296296
result = run_cli(["run", "-"], input="(println (+ 1 2))")
297-
assert "3\n" == result.lisp_out
297+
assert f"3{os.linesep}" == result.lisp_out
298298

299299
def test_run_stdin_main_ns(self, run_cli):
300300
result = run_cli(["run", "-"], input="(println *main-ns*)")
301-
assert "nil\n" == result.lisp_out
301+
assert f"nil{os.linesep}" == result.lisp_out
302302

303303
@pytest.mark.parametrize("args,ret", cli_args_params)
304304
def test_run_stdin_with_args(self, run_cli, args: List[str], ret: str):

tests/basilisp/contrib/nrepl_server_test.lpy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,14 @@
312312
{:id @id* :out ":hi"}
313313
{:id @id* :out " "}
314314
{:id @id* :out "there"}
315-
{:id @id* :out "\n"}
315+
{:id @id* :out os/linesep}
316316
{:id @id* :ns "user" :value "nil"}
317317
{:id @id* :ns "user" :status ["done"]})
318318

319319
(client-send! client {:id (id-inc!) :op "eval" :ns "xyz" :code "(ns xyz (:import [sys :as s])) (println s/__name__) (* 2 3)"})
320320
(are [response] (= response (client-recv! client))
321321
{:id @id* :out "sys"}
322-
{:id @id* :out "\n"}
322+
{:id @id* :out os/linesep}
323323
{:id @id* :ns "xyz" :value "6"}
324324
{:id @id* :ns "xyz" :status ["done"]})
325325

@@ -336,7 +336,7 @@
336336
(client-send! client {:id (id-inc!) :op "eval" :code "(println :hey)\n(/ 4 0)"})
337337
(are [response] (= response (client-recv! client))
338338
{:id @id* :out ":hey"}
339-
{:id @id* :out "\n"}
339+
{:id @id* :out os/linesep}
340340
{:id @id* :err "ZeroDivisionError('Fraction(4, 0)')"})
341341
(let [{:keys [id ex status ns]} (client-recv! client)]
342342
(is (= @id* id))

tests/basilisp/core_test.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import itertools
2+
import os
23
import re
34
from decimal import Decimal
45
from fractions import Fraction
@@ -1599,10 +1600,12 @@ def test_pr_str(self):
15991600
assert ':hi "there" 3' == core.pr_str(kw.keyword("hi"), "there", 3)
16001601

16011602
def test_prn_str(self):
1602-
assert "\n" == core.prn_str()
1603-
assert '""\n' == core.prn_str("")
1604-
assert ":kw\n" == core.prn_str(kw.keyword("kw"))
1605-
assert ':hi "there" 3\n' == core.prn_str(kw.keyword("hi"), "there", 3)
1603+
assert os.linesep == core.prn_str()
1604+
assert '""' + os.linesep == core.prn_str("")
1605+
assert ":kw" + os.linesep == core.prn_str(kw.keyword("kw"))
1606+
assert ':hi "there" 3' + os.linesep == core.prn_str(
1607+
kw.keyword("hi"), "there", 3
1608+
)
16061609

16071610
def test_print_str(self):
16081611
assert "" == core.print_str()
@@ -1611,10 +1614,12 @@ def test_print_str(self):
16111614
assert ":hi there 3" == core.print_str(kw.keyword("hi"), "there", 3)
16121615

16131616
def test_println_str(self):
1614-
assert "\n" == core.println_str()
1615-
assert "\n" == core.println_str("")
1616-
assert ":kw\n" == core.println_str(kw.keyword("kw"))
1617-
assert ":hi there 3\n" == core.println_str(kw.keyword("hi"), "there", 3)
1617+
assert os.linesep == core.println_str()
1618+
assert os.linesep == core.println_str("")
1619+
assert ":kw" + os.linesep == core.println_str(kw.keyword("kw"))
1620+
assert ":hi there 3" + os.linesep == core.println_str(
1621+
kw.keyword("hi"), "there", 3
1622+
)
16181623

16191624

16201625
class TestRegexFunctions:

0 commit comments

Comments
 (0)