diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2016-09-06 22:07:53 +0300 |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2016-09-06 22:07:53 +0300 |
commit | ea525a2d1a798f529b015f86b174d478806ac7ec (patch) | |
tree | 4ed79494c2a6a8e3460027a246020d25c1eaca16 /Objects/abstract.c | |
parent | 620bb277f89ba373b8c50e1a9fb9c8ba77a245c4 (diff) | |
download | cpython-git-ea525a2d1a798f529b015f86b174d478806ac7ec.tar.gz |
Issue #27078: Added BUILD_STRING opcode. Optimized f-strings evaluation.
Diffstat (limited to 'Objects/abstract.c')
-rw-r--r-- | Objects/abstract.c | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c index 654fc0295b..d876dc5fe4 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -675,13 +675,31 @@ PyObject_Format(PyObject *obj, PyObject *format_spec) PyObject *result = NULL; _Py_IDENTIFIER(__format__); + if (format_spec != NULL && !PyUnicode_Check(format_spec)) { + PyErr_Format(PyExc_SystemError, + "Format specifier must be a string, not %.200s", + Py_TYPE(format_spec)->tp_name); + return NULL; + } + + /* Fast path for common types. */ + if (format_spec == NULL || PyUnicode_GET_LENGTH(format_spec) == 0) { + if (PyUnicode_CheckExact(obj)) { + Py_INCREF(obj); + return obj; + } + if (PyLong_CheckExact(obj)) { + return PyObject_Str(obj); + } + } + /* If no format_spec is provided, use an empty string */ if (format_spec == NULL) { empty = PyUnicode_New(0, 0); format_spec = empty; } - /* Find the (unbound!) __format__ method (a borrowed reference) */ + /* Find the (unbound!) __format__ method */ meth = _PyObject_LookupSpecial(obj, &PyId___format__); if (meth == NULL) { if (!PyErr_Occurred()) |