Fix literal whitespace being deleted from f-strings

Fixes an issue with
 f"$HOME       = {$HOME}"
resulting in
 "$HOME= /home/foobar"
This commit is contained in:
David Strobach 2020-08-20 05:37:02 +02:00
parent 289fb15774
commit 5edd851feb
2 changed files with 5 additions and 4 deletions

View file

@ -129,6 +129,7 @@ fstring_adaptor_parameters = [
('f"{$HOME}"', "/foo/bar"), ('f"{$HOME}"', "/foo/bar"),
('f"{ $HOME }"', "/foo/bar"), ('f"{ $HOME }"', "/foo/bar"),
("f\"{'$HOME'}\"", "$HOME"), ("f\"{'$HOME'}\"", "$HOME"),
("f\"$HOME = {$HOME}\"", "$HOME = /foo/bar"),
("f\"{${'HOME'}}\"", "/foo/bar"), ("f\"{${'HOME'}}\"", "/foo/bar"),
("f'{${$FOO+$BAR}}'", "/foo/bar"), ("f'{${$FOO+$BAR}}'", "/foo/bar"),
("f\"${$FOO}{$BAR}={f'{$HOME}'}\"", "$HOME=/foo/bar"), ("f\"${$FOO}{$BAR}={f'{$HOME}'}\"", "$HOME=/foo/bar"),

View file

@ -11,9 +11,9 @@ from xonsh.platform import PYTHON_VERSION_INFO
@lazyobject @lazyobject
def RE_FSTR_FIELD_WRAPPER(): def RE_FSTR_FIELD_WRAPPER():
if PYTHON_VERSION_INFO > (3, 8): if PYTHON_VERSION_INFO > (3, 8):
return re.compile(r"__xonsh__\.eval_fstring_field\((\d+)\)\s*[^=]") return re.compile(r"(__xonsh__\.eval_fstring_field\((\d+)\))\s*[^=]")
else: else:
return re.compile(r"__xonsh__\.eval_fstring_field\((\d+)\)") return re.compile(r"(__xonsh__\.eval_fstring_field\((\d+)\))")
if PYTHON_VERSION_INFO > (3, 8): if PYTHON_VERSION_INFO > (3, 8):
@ -96,10 +96,10 @@ class FStringAdaptor:
match = RE_FSTR_FIELD_WRAPPER.search(value) match = RE_FSTR_FIELD_WRAPPER.search(value)
if match is None: if match is None:
continue continue
field = self.fields.pop(int(match.group(1)), None) field = self.fields.pop(int(match.group(2)), None)
if field is None: if field is None:
continue continue
self.repl = self.repl.replace(match.group(0), field[0], 1) self.repl = self.repl.replace(match.group(1), field[0], 1)
reparse = True reparse = True
if reparse: if reparse: