Merge pull request #1868 from refi64/f-string-quote
Avoid evaluating quoted f-strings
This commit is contained in:
commit
5c9f04021e
1
NEWS.rst
1
NEWS.rst
@ -12,6 +12,7 @@ Bug Fixes
|
|||||||
------------------------------
|
------------------------------
|
||||||
* Improved support for nesting anaphoric macros by only applying
|
* Improved support for nesting anaphoric macros by only applying
|
||||||
symbol replacement where absolutely necessary.
|
symbol replacement where absolutely necessary.
|
||||||
|
* Quoted f-strings are no longer evaluated prematurely.
|
||||||
|
|
||||||
0.18.0
|
0.18.0
|
||||||
==============================
|
==============================
|
||||||
|
@ -606,7 +606,13 @@ class HyASTCompiler(object):
|
|||||||
|
|
||||||
elif isinstance(form, HyString):
|
elif isinstance(form, HyString):
|
||||||
if form.is_format:
|
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:
|
if form.brackets is not None:
|
||||||
body.extend([HyKeyword("brackets"), form.brackets])
|
body.extend([HyKeyword("brackets"), form.brackets])
|
||||||
|
|
||||||
|
@ -1303,7 +1303,16 @@ cee\"} dee" "ey bee\ncee dee"))
|
|||||||
; Format bracket strings
|
; Format bracket strings
|
||||||
(assert (= #[f[a{p !r :9}]f] "a'xyzzy' "))
|
(assert (= #[f[a{p !r :9}]f] "a'xyzzy' "))
|
||||||
(assert (= #[f-string[result: {value :{width}.{precision}}]f-string]
|
(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 []
|
(defn test-import-syntax []
|
||||||
|
Loading…
Reference in New Issue
Block a user