Commit 6524acb7 authored by Jelle Zijlstra's avatar Jelle Zijlstra Committed by Jelle Zijlstra

fstrings: fix more bugs

Not sure about the refcounting
parent 25417282
......@@ -2937,8 +2937,8 @@ class FormattedValueNode(ExprNode):
def generate_result_code(self, code):
value_result = self.value.py_result()
conversion_result = Naming.quick_temp_cname
format_spec_result = self.format_spec.py_result()
# TODO conversion chars
if self.conversion_char == 's':
fn = 'PyObject_Str'
elif self.conversion_char == 'r':
......@@ -2948,25 +2948,31 @@ class FormattedValueNode(ExprNode):
else:
fn = None
code.putln('{')
if fn is not None:
code.putln('%s = %s(%s); %s' % (
value_result,
code.putln('PyObject *%s = %s(%s); %s' % (
conversion_result,
fn,
value_result,
code.error_goto_if_null(value_result, self.pos)
code.error_goto_if_null(conversion_result, self.pos)
))
code.put_gotref(value_result)
decref_line = '__Pyx_DECREF(%s);' % value_result
else:
decref_line = ''
code.putln('PyObject *%s = %s;' % (conversion_result, value_result))
#code.put_incref(conversion_result, py_object_type)
# TODO this should need more refcounting, figure out whether this is correct
#code.put_gotref(conversion_result)
#code.put_decref(value_result, self.value.ctype())
decref_line = '' # '__Pyx_DECREF(%s);' % conversion_result
code.putln("%s = PyObject_Format(%s, %s); %s %s" % (
self.result(),
value_result,
conversion_result,
format_spec_result,
decref_line,
code.error_goto_if_null(self.result(), self.pos)))
code.put_gotref(self.py_result())
code.putln('}')
#-------------------------------------------------------------------
......
......@@ -998,15 +998,14 @@ def p_f_string(s, unicode_value, pos):
current_literal_start = 0
while i < size:
c = unicode_value[i]
if c == '}':
if i + 1 >= size or unicode_value[i + 1] != '}':
s.error("single '}' encountered in format string")
else:
i += 2
elif c == '{':
# double { escapes it
if i + 1 < size and unicode_value[i + 1] == '{':
if c in ('{', '}'):
if i + 1 < size and unicode_value[i + 1] == c:
encoded_str = EncodedString(unicode_value[current_literal_start:i + 1])
values.append(ExprNodes.UnicodeNode(pos, value = encoded_str))
i += 2
current_literal_start = i
elif c == '}':
s.error("single '}' encountered in format string")
else:
encoded_str = EncodedString(unicode_value[current_literal_start:i])
values.append(ExprNodes.UnicodeNode(pos, value = encoded_str))
......@@ -1018,7 +1017,6 @@ def p_f_string(s, unicode_value, pos):
encoded_str = EncodedString(unicode_value[current_literal_start:])
values.append(ExprNodes.UnicodeNode(pos, value = encoded_str))
print("F-STRING VALUES", values)
return values
......@@ -1109,8 +1107,6 @@ def p_f_string_expr(s, unicode_value, pos, starting_index):
if terminal_char != '}':
s.error("missing '}' in format string expression'")
print('expr=%r, conversion_char=%r, format_spec=%r' % (expr_str, conversion_char, format_spec_str))
# parse the expression
name = 'format string expression'
code_source = StringSourceDescriptor(name, expr_str)
......
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