Skip to content

Commit 0d06028

Browse files
authored
Properly escape string output at the REPL (#902)
Fixes #894
1 parent eab3633 commit 0d06028

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
### Fixed
99
* Fix a bug where `.` characters were not allowed in keyword names (#899)
10+
* Fix a bug where nested quotation marks were not escaped properly by various print functions and at the REPL (#894)
1011

1112
## [v0.1.0b2]
1213
### Added

src/basilisp/lang/obj.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,9 @@ def _lrepr_str(
204204
if human_readable:
205205
return o
206206
if print_readably is None or print_readably is False:
207-
return f'"{o}"'
208-
return f'"{o.encode("unicode_escape").decode("utf-8")}"'
207+
return o
208+
escaped = o.encode("unicode_escape").replace(b'"', rb"\"").decode("utf-8")
209+
return f'"{escaped}"'
209210

210211

211212
@lrepr.register(list)

tests/basilisp/lrepr_test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def test_print_meta(lcompile: CompileFn, s: str, code: str):
147147

148148

149149
def test_print_readably(lcompile: CompileFn):
150-
assert '"Hello\nworld!"' == lcompile(
150+
assert "Hello\nworld!" == lcompile(
151151
'(binding [*print-readably* false] (pr-str "Hello\\nworld!"))'
152152
)
153153

@@ -169,6 +169,7 @@ def test_print_readably(lcompile: CompileFn):
169169
("##-Inf", "(pr-str ##-Inf)"),
170170
('"hi"', '(pr-str "hi")'),
171171
('"Hello\\nworld!"', '(pr-str "Hello\nworld!")'),
172+
(r'"\"Hello world!\""', r'(pr-str "\"Hello world!\"")'),
172173
(
173174
'#uuid "81f35603-0408-4b3d-bbc0-462e3702747f"',
174175
'(pr-str #uuid "81f35603-0408-4b3d-bbc0-462e3702747f")',
@@ -214,6 +215,7 @@ def test_lrepr(lcompile: CompileFn, repr: str, code: str):
214215
(-float("inf"), "(read-string (pr-str ##-Inf))"),
215216
("hi", '(read-string (pr-str "hi"))'),
216217
("Hello\nworld!", '(read-string (pr-str "Hello\nworld!"))'),
218+
('"Hello world!"', r'(read-string (pr-str "\"Hello world!\""))'),
217219
(b"", '(read-string (pr-str #b ""))'),
218220
(
219221
b"\x7fELF\x01\x01\x01\x00",
@@ -267,6 +269,7 @@ def test_lrepr_round_trip_special_cases(lcompile: CompileFn):
267269
("##-Inf", "(print-str ##-Inf)"),
268270
("hi", '(print-str "hi")'),
269271
("Hello\nworld!", '(print-str "Hello\nworld!")'),
272+
('"Hello world!"', r'(print-str "\"Hello world!\"")'),
270273
('#b ""', '(print-str #b "")'),
271274
(
272275
r'#b "\x7fELF\x01\x01\x01\x00"',
@@ -319,6 +322,7 @@ def test_lstr(lcompile: CompileFn, s: str, code: str):
319322
("##-Inf", "(str ##-Inf)"),
320323
("hi", '(str "hi")'),
321324
("Hello\nworld!", '(str "Hello\nworld!")'),
325+
('"Hello world!"', r'(str "\"Hello world!\"")'),
322326
('#b ""', '(str #b "")'),
323327
(
324328
r'#b "\x7fELF\x01\x01\x01\x00"',

0 commit comments

Comments
 (0)