Avoid evaluating quoted f-strings

Fixes #1844.
This commit is contained in:
Ryan Gonzalez 2020-01-18 16:26:40 -06:00 committed by Kodi Arfer
parent 3c6077695b
commit a964acd51d
2 changed files with 17 additions and 2 deletions

View File

@ -606,7 +606,13 @@ class HyASTCompiler(object):
elif isinstance(form, HyString):
if form.is_format:
body.extend([HyKeyword("is_format"), form.is_format])
# Ensure that this f-string isn't evaluated right now.
body = [
copy.copy(form),
HyKeyword("is_format"),
form.is_format,
]
body[0].is_format = False
if form.brackets is not None:
body.extend([HyKeyword("brackets"), form.brackets])

View File

@ -1303,7 +1303,16 @@ cee\"} dee" "ey bee\ncee dee"))
; Format bracket strings
(assert (= #[f[a{p !r :9}]f] "a'xyzzy' "))
(assert (= #[f-string[result: {value :{width}.{precision}}]f-string]
"result: 12.34")))
"result: 12.34"))
; Quoting shouldn't evaluate the f-string immediately
; https://github.com/hylang/hy/issues/1844
(setv quoted 'f"hello {world}")
(assert quoted.is-format)
(with [(pytest.raises NameError)]
(eval quoted))
(setv world "goodbye")
(assert (= (eval quoted) "hello goodbye")))
(defn test-import-syntax []