From 3c6077695b05055d22e77e842503d1fd5b78d41d Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Sat, 18 Jan 2020 16:10:58 -0600 Subject: [PATCH 1/3] Fix some incorrect indentation --- hy/compiler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index f219dac..48d8af4 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -606,9 +606,9 @@ class HyASTCompiler(object): elif isinstance(form, HyString): if form.is_format: - body.extend([HyKeyword("is_format"), form.is_format]) + body.extend([HyKeyword("is_format"), form.is_format]) if form.brackets is not None: - body.extend([HyKeyword("brackets"), form.brackets]) + body.extend([HyKeyword("brackets"), form.brackets]) ret = HyExpression([HySymbol(name)] + body).replace(form) return imports, ret, False From a964acd51d49a11be773dd6406fd5b889fe3e923 Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Sat, 18 Jan 2020 16:26:40 -0600 Subject: [PATCH 2/3] Avoid evaluating quoted f-strings Fixes #1844. --- hy/compiler.py | 8 +++++++- tests/native_tests/language.hy | 11 ++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index 48d8af4..3d527f7 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -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]) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 7732207..903663e 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -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 [] From 15be7693fd90b4b9a8789034ef57b74d215b61d6 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Thu, 2 Apr 2020 08:31:16 -0400 Subject: [PATCH 3/3] Update NEWS --- NEWS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.rst b/NEWS.rst index b84345e..7a523da 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -12,6 +12,7 @@ Bug Fixes ------------------------------ * Improved support for nesting anaphoric macros by only applying symbol replacement where absolutely necessary. +* Quoted f-strings are no longer evaluated prematurely. 0.18.0 ==============================