Issue #13350: Replace most usages of PyUnicode_Format by PyUnicode_FromFormat.

This commit is contained in:
Amaury Forgeot d'Arc 2011-11-06 15:10:48 +01:00
parent 08ad2fbc7f
commit 864741b2c7
7 changed files with 13 additions and 107 deletions

View File

@ -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[] = {

View File

@ -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[] = {

View File

@ -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[] = {

View File

@ -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()).

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}