Commit 29c1030e authored by Stefan Behnel's avatar Stefan Behnel

simplify code generated for f-string formatting using simple utility functions

parent 0d7af347
......@@ -3029,40 +3029,28 @@ class FormattedValueNode(ExprNode):
return self
def generate_result_code(self, code):
conversion_result = value_result = self.value.py_result()
value_result = self.value.py_result()
# common case: expect Unicode pass-through if no format spec
no_format_spec = isinstance(self.format_spec, UnicodeNode) and not self.format_spec.value.strip()
if self.conversion_char:
fn = self.find_conversion_func(self.conversion_char)
if fn is None:
assert False, "invalid conversion character found: '%s'" % self.conversion_char
code.putln('{')
conversion_result = Naming.quick_temp_cname
code.putln('PyObject *%s = %s(%s); %s' % (
conversion_result,
fn,
value_result,
code.error_goto_if_null(conversion_result, self.pos)
))
code.put_gotref(conversion_result)
if isinstance(self.format_spec, UnicodeNode) and not self.format_spec.value:
# common case: no format spec => expect Unicode pass-through
assert fn is not None, "invalid conversion character found: '%s'" % self.conversion_char
value_result = '%s(%s)' % (fn, value_result)
code.globalstate.use_utility_code(UtilityCode.load_cached("PyObjectFormatAndDecref", "StringTools.c"))
format_func = '__Pyx_PyObject_FormatSimpleAndDecref' if no_format_spec else '__Pyx_PyObject_FormatAndDecref'
elif no_format_spec:
code.globalstate.use_utility_code(UtilityCode.load_cached("PyObjectFormatSimple", "StringTools.c"))
format_func = '__Pyx_PyObject_FormatSimple'
else:
format_func = 'PyObject_Format'
code.put("%s = %s(%s, %s); " % (
code.putln("%s = %s(%s, %s); %s" % (
self.result(),
format_func,
conversion_result,
self.format_spec.py_result()))
if self.conversion_char:
code.put_decref(conversion_result, py_object_type)
code.putln('}')
code.putln(code.error_goto_if_null(self.result(), self.pos))
value_result,
self.format_spec.py_result(),
code.error_goto_if_null(self.result(), self.pos)))
code.put_gotref(self.py_result())
......
......@@ -123,7 +123,6 @@
#define PyObject_Realloc(p) PyMem_Realloc(p)
#endif
#define __Pyx_PyObject_FormatSimple(s, f) (likely(PyUnicode_CheckExact(s)) ? (Py_INCREF(s), s) : PyObject_Format(s, f))
#define __Pyx_PyString_FormatSafe(a, b) ((unlikely((a) == Py_None)) ? PyNumber_Remainder(a, b) : __Pyx_PyString_Format(a, b))
#define __Pyx_PyUnicode_FormatSafe(a, b) ((unlikely((a) == Py_None)) ? PyNumber_Remainder(a, b) : PyUnicode_Format(a, b))
......
......@@ -811,3 +811,23 @@ static CYTHON_INLINE int __Pyx_PyByteArray_Append(PyObject* bytearray, int value
Py_DECREF(retval);
return 0;
}
//////////////////// PyObjectFormatSimple.proto ////////////////////
#define __Pyx_PyObject_FormatSimple(s, f) (likely(PyUnicode_CheckExact(s)) ? (Py_INCREF(s), s) : PyObject_Format(s, f))
//////////////////// PyObjectFormatAndDecref.proto ////////////////////
#define __Pyx_PyObject_FormatSimpleAndDecref(s, f) \
((unlikely(!s) || likely(PyUnicode_CheckExact(s))) ? s : __Pyx_PyObject_FormatAndDecref(s, f))
static CYTHON_INLINE PyObject* __Pyx_PyObject_FormatAndDecref(PyObject* s, PyObject* f);
//////////////////// PyObjectFormatAndDecref ////////////////////
static CYTHON_INLINE PyObject* __Pyx_PyObject_FormatAndDecref(PyObject* s, PyObject* f) {
PyObject *result = PyObject_Format(s, f);
Py_DECREF(s);
return result;
}
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