diff --git a/Doc/includes/noddy2.c b/Doc/includes/noddy2.c index c02d49b92e9..964155845fe 100644 --- a/Doc/includes/noddy2.c +++ b/Doc/includes/noddy2.c @@ -84,15 +84,6 @@ static PyMemberDef Noddy_members[] = { static PyObject * Noddy_name(Noddy* self) { - static PyObject *format = NULL; - PyObject *args, *result; - - if (format == NULL) { - format = PyUnicode_FromString("%s %s"); - if (format == NULL) - return NULL; - } - if (self->first == NULL) { PyErr_SetString(PyExc_AttributeError, "first"); return NULL; @@ -103,14 +94,7 @@ Noddy_name(Noddy* self) return NULL; } - args = Py_BuildValue("OO", self->first, self->last); - if (args == NULL) - return NULL; - - result = PyUnicode_Format(format, args); - Py_DECREF(args); - - return result; + return PyUnicode_FromFormat("%S %S", self->first, self->last); } static PyMethodDef Noddy_methods[] = { diff --git a/Doc/includes/noddy3.c b/Doc/includes/noddy3.c index 002a0e10ad0..8a5a753ca43 100644 --- a/Doc/includes/noddy3.c +++ b/Doc/includes/noddy3.c @@ -147,23 +147,7 @@ static PyGetSetDef Noddy_getseters[] = { static PyObject * Noddy_name(Noddy* self) { - static PyObject *format = NULL; - PyObject *args, *result; - - if (format == NULL) { - format = PyUnicode_FromString("%s %s"); - if (format == NULL) - return NULL; - } - - args = Py_BuildValue("OO", self->first, self->last); - if (args == NULL) - return NULL; - - result = PyUnicode_Format(format, args); - Py_DECREF(args); - - return result; + return PyUnicode_FromFormat("%S %S", self->first, self->last); } static PyMethodDef Noddy_methods[] = { diff --git a/Doc/includes/noddy4.c b/Doc/includes/noddy4.c index c747682bb6a..eb9622a87d9 100644 --- a/Doc/includes/noddy4.c +++ b/Doc/includes/noddy4.c @@ -118,15 +118,6 @@ static PyMemberDef Noddy_members[] = { static PyObject * Noddy_name(Noddy* self) { - static PyObject *format = NULL; - PyObject *args, *result; - - if (format == NULL) { - format = PyUnicode_FromString("%s %s"); - if (format == NULL) - return NULL; - } - if (self->first == NULL) { PyErr_SetString(PyExc_AttributeError, "first"); return NULL; @@ -137,14 +128,7 @@ Noddy_name(Noddy* self) return NULL; } - args = Py_BuildValue("OO", self->first, self->last); - if (args == NULL) - return NULL; - - result = PyUnicode_Format(format, args); - Py_DECREF(args); - - return result; + return PyUnicode_FromFormat("%S %S", self->first, self->last); } static PyMethodDef Noddy_methods[] = { diff --git a/Misc/NEWS b/Misc/NEWS index 27e4117885c..82edaa4f783 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1? Core and Builtins ----------------- +- Issue #13350: Simplify some C code by replacing most usages of + PyUnicode_Format by PyUnicode_FromFormat. + - Issue #13342: input() used to ignore sys.stdin's and sys.stdout's unicode error handler in interactive mode (when calling into PyOS_Readline()). diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index e08dea1a7cd..338c4f7ce87 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -4599,38 +4599,20 @@ static PyNumberMethods Simple_as_number = { static PyObject * Simple_repr(CDataObject *self) { - PyObject *val, *name, *args, *result; - static PyObject *format; + PyObject *val, *result; if (Py_TYPE(self)->tp_base != &Simple_Type) { return PyUnicode_FromFormat("<%s object at %p>", Py_TYPE(self)->tp_name, self); } - if (format == NULL) { - format = PyUnicode_InternFromString("%s(%r)"); - if (format == NULL) - return NULL; - } - val = Simple_get_value(self); if (val == NULL) return NULL; - name = PyUnicode_FromString(Py_TYPE(self)->tp_name); - if (name == NULL) { - Py_DECREF(val); - return NULL; - } - - args = PyTuple_Pack(2, name, val); - Py_DECREF(name); + result = PyUnicode_FromFormat("%s(%R)", + Py_TYPE(self)->tp_name, val); Py_DECREF(val); - if (args == NULL) - return NULL; - - result = PyUnicode_Format(format, args); - Py_DECREF(args); return result; } diff --git a/Modules/_sqlite/cache.c b/Modules/_sqlite/cache.c index 735a2426ccf..3693363bb2a 100644 --- a/Modules/_sqlite/cache.c +++ b/Modules/_sqlite/cache.c @@ -217,8 +217,6 @@ PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args) pysqlite_Node* ptr; PyObject* prevkey; PyObject* nextkey; - PyObject* fmt_args; - PyObject* template; PyObject* display_str; ptr = self->first; @@ -229,36 +227,21 @@ PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args) } else { prevkey = Py_None; } - Py_INCREF(prevkey); if (ptr->next) { nextkey = ptr->next->key; } else { nextkey = Py_None; } - Py_INCREF(nextkey); - fmt_args = Py_BuildValue("OOO", prevkey, ptr->key, nextkey); - if (!fmt_args) { - return NULL; - } - template = PyUnicode_FromString("%s <- %s ->%s\n"); - if (!template) { - Py_DECREF(fmt_args); - return NULL; - } - display_str = PyUnicode_Format(template, fmt_args); - Py_DECREF(template); - Py_DECREF(fmt_args); + display_str = PyUnicode_FromFormat("%S <- %S -> %S\n", + prevkey, ptr->key, nextkey); if (!display_str) { return NULL; } PyObject_Print(display_str, stdout, Py_PRINT_RAW); Py_DECREF(display_str); - Py_DECREF(prevkey); - Py_DECREF(nextkey); - ptr = ptr->next; } diff --git a/Objects/listobject.c b/Objects/listobject.c index bbc529a6bdb..cf4f2766d57 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -2121,9 +2121,8 @@ static PyObject * listindex(PyListObject *self, PyObject *args) { Py_ssize_t i, start=0, stop=Py_SIZE(self); - PyObject *v, *format_tuple, *err_string; + PyObject *v; PyObject *start_obj = NULL, *stop_obj = NULL; - static PyObject *err_format = NULL; if (!PyArg_ParseTuple(args, "O|OO:index", &v, &start_obj, &stop_obj)) return NULL; @@ -2153,20 +2152,7 @@ listindex(PyListObject *self, PyObject *args) else if (cmp < 0) return NULL; } - if (err_format == NULL) { - err_format = PyUnicode_FromString("%r is not in list"); - if (err_format == NULL) - return NULL; - } - format_tuple = PyTuple_Pack(1, v); - if (format_tuple == NULL) - return NULL; - err_string = PyUnicode_Format(err_format, format_tuple); - Py_DECREF(format_tuple); - if (err_string == NULL) - return NULL; - PyErr_SetObject(PyExc_ValueError, err_string); - Py_DECREF(err_string); + PyErr_Format(PyExc_ValueError, "%R is not in list", v); return NULL; }