Commit 6c7c6dfa authored by Jelle Zijlstra's avatar Jelle Zijlstra Committed by Jelle Zijlstra

fstrings: implement concatenation using PyUnicode_Join

Using _PyUnicode_Writer might be faster but seems more complicated to implement.
parent 437a90b4
......@@ -2886,16 +2886,34 @@ class JoinedStrNode(ExprNode):
self.values = [v.analyse_types(env) for v in self.values]
self.values = [v.coerce_to_pyobject(env) for v in self.values]
self.is_temp = 1
# TODO make this actually work
return self.values[1] if len(self.values) > 1 else self.values[0]
return self
if len(self.values) == 1:
# this is not uncommon because f-string format specs are parsed into JoinedStrNodes
return self.values[0]
else:
return self
def generate_result_code(self, code):
# TODO this just returns the first value and the refnanny doesn't like it
val = self.values[1] if len(self.values) > 1 else self.values[0]
code.putln('%s = %s;' % (self.result(), val.py_result()))
code.putln('__Pyx_INCREF(%s);' % self.result())
list_var = Naming.quick_temp_cname
num_items = len(self.values)
code.putln('{')
code.putln('PyObject *%s = PyList_New(%s); %s' % (
list_var,
num_items,
code.error_goto_if_null(list_var, self.pos)))
code.put_gotref(list_var)
for i, value in enumerate(self.values):
code.put_incref(value.result(), value.ctype())
code.put_giveref(value.py_result())
code.putln('PyList_SET_ITEM(%s, %s, %s);' % (list_var, i, value.py_result()))
code.putln('%s = PyUnicode_Join(%s, %s); __Pyx_DECREF(%s); %s' % (
self.result(),
Naming.empty_unicode,
list_var,
list_var,
code.error_goto_if_null(list_var, self.pos)))
code.put_gotref(self.py_result())
code.putln('}')
class FormattedValueNode(ExprNode):
......
......@@ -675,6 +675,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code.putln('static PyObject *%s;' % Naming.builtins_cname)
code.putln('static PyObject *%s;' % Naming.empty_tuple)
code.putln('static PyObject *%s;' % Naming.empty_bytes)
code.putln('static PyObject *%s;' % Naming.empty_unicode)
if Options.pre_import is not None:
code.putln('static PyObject *%s;' % Naming.preimport_cname)
code.putln('static int %s;' % Naming.lineno_cname)
......@@ -2103,6 +2104,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
Naming.empty_tuple, code.error_goto_if_null(Naming.empty_tuple, self.pos)))
code.putln("%s = PyBytes_FromStringAndSize(\"\", 0); %s" % (
Naming.empty_bytes, code.error_goto_if_null(Naming.empty_bytes, self.pos)))
code.putln("%s = PyUnicode_FromStringAndSize(\"\", 0); %s" % (
Naming.empty_unicode, code.error_goto_if_null(Naming.empty_unicode, self.pos)))
for ext_type in ('CyFunction', 'FusedFunction', 'Coroutine', 'Generator', 'StopAsyncIteration'):
code.putln("#ifdef __Pyx_%s_USED" % ext_type)
......
......@@ -95,6 +95,7 @@ gilstate_cname = pyrex_prefix + "state"
skip_dispatch_cname = pyrex_prefix + "skip_dispatch"
empty_tuple = pyrex_prefix + "empty_tuple"
empty_bytes = pyrex_prefix + "empty_bytes"
empty_unicode = pyrex_prefix + "empty_unicode"
print_function = pyrex_prefix + "print"
print_function_kwargs = pyrex_prefix + "print_kwargs"
cleanup_cname = pyrex_prefix + "module_cleanup"
......
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