Commit 08b0e6f1 authored by Stefan Behnel's avatar Stefan Behnel

join UnicodeNode sequences in JoinedStrNode at compile time

parent 541ff4d0
...@@ -7,6 +7,7 @@ cython.declare(PyrexTypes=object, Naming=object, ExprNodes=object, Nodes=object, ...@@ -7,6 +7,7 @@ cython.declare(PyrexTypes=object, Naming=object, ExprNodes=object, Nodes=object,
error=object, warning=object, copy=object, _unicode=object) error=object, warning=object, copy=object, _unicode=object)
import copy import copy
import itertools
from . import PyrexTypes from . import PyrexTypes
from . import Naming from . import Naming
...@@ -350,15 +351,24 @@ class PostParse(ScopeTrackingTransform): ...@@ -350,15 +351,24 @@ class PostParse(ScopeTrackingTransform):
def visit_JoinedStrNode(self, node): def visit_JoinedStrNode(self, node):
""" """
Clean up after the parser by discarding empty Unicode strings. Clean up after the parser by discarding empty Unicode strings and merging
Empty or single-value join lists are not uncommon because f-string substring sequences. Empty or single-value join lists are not uncommon
format specs are always parsed into JoinedStrNodes. because f-string format specs are always parsed into JoinedStrNodes.
""" """
self.visitchildren(node)
unicode_node = ExprNodes.UnicodeNode
values = [] values = []
for value in node.values: for is_unode_group, substrings in itertools.groupby(node.values, lambda v: isinstance(v, unicode_node)):
if isinstance(value, ExprNodes.UnicodeNode) and not value.value: if is_unode_group:
continue # ignore empty Unicode strings substrings = list(substrings)
values.append(value) unode = substrings[0]
if len(substrings) > 1:
unode.value = EncodedString(u''.join(node.value for node in substrings))
# ignore empty Unicode strings
if unode.value:
values.append(unode)
else:
values.extend(substrings)
if not values: if not values:
node = ExprNodes.UnicodeNode(node.pos, value=EncodedString('')) node = ExprNodes.UnicodeNode(node.pos, value=EncodedString(''))
...@@ -366,7 +376,6 @@ class PostParse(ScopeTrackingTransform): ...@@ -366,7 +376,6 @@ class PostParse(ScopeTrackingTransform):
node = values[0] node = values[0]
else: else:
node.values = values node.values = values
self.visitchildren(node)
return node return node
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment