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 * static PyObject *
Noddy_name(Noddy* self) 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) { if (self->first == NULL) {
PyErr_SetString(PyExc_AttributeError, "first"); PyErr_SetString(PyExc_AttributeError, "first");
return NULL; return NULL;
@ -103,14 +94,7 @@ Noddy_name(Noddy* self)
return NULL; return NULL;
} }
args = Py_BuildValue("OO", self->first, self->last); return PyUnicode_FromFormat("%S %S", self->first, self->last);
if (args == NULL)
return NULL;
result = PyUnicode_Format(format, args);
Py_DECREF(args);
return result;
} }
static PyMethodDef Noddy_methods[] = { static PyMethodDef Noddy_methods[] = {

View File

@ -147,23 +147,7 @@ static PyGetSetDef Noddy_getseters[] = {
static PyObject * static PyObject *
Noddy_name(Noddy* self) Noddy_name(Noddy* self)
{ {
static PyObject *format = NULL; return PyUnicode_FromFormat("%S %S", self->first, self->last);
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;
} }
static PyMethodDef Noddy_methods[] = { static PyMethodDef Noddy_methods[] = {

View File

@ -118,15 +118,6 @@ static PyMemberDef Noddy_members[] = {
static PyObject * static PyObject *
Noddy_name(Noddy* self) 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) { if (self->first == NULL) {
PyErr_SetString(PyExc_AttributeError, "first"); PyErr_SetString(PyExc_AttributeError, "first");
return NULL; return NULL;
@ -137,14 +128,7 @@ Noddy_name(Noddy* self)
return NULL; return NULL;
} }
args = Py_BuildValue("OO", self->first, self->last); return PyUnicode_FromFormat("%S %S", self->first, self->last);
if (args == NULL)
return NULL;
result = PyUnicode_Format(format, args);
Py_DECREF(args);
return result;
} }
static PyMethodDef Noddy_methods[] = { static PyMethodDef Noddy_methods[] = {

View File

@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1?
Core and Builtins 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 - Issue #13342: input() used to ignore sys.stdin's and sys.stdout's unicode
error handler in interactive mode (when calling into PyOS_Readline()). error handler in interactive mode (when calling into PyOS_Readline()).

View File

@ -4599,38 +4599,20 @@ static PyNumberMethods Simple_as_number = {
static PyObject * static PyObject *
Simple_repr(CDataObject *self) Simple_repr(CDataObject *self)
{ {
PyObject *val, *name, *args, *result; PyObject *val, *result;
static PyObject *format;
if (Py_TYPE(self)->tp_base != &Simple_Type) { if (Py_TYPE(self)->tp_base != &Simple_Type) {
return PyUnicode_FromFormat("<%s object at %p>", return PyUnicode_FromFormat("<%s object at %p>",
Py_TYPE(self)->tp_name, self); Py_TYPE(self)->tp_name, self);
} }
if (format == NULL) {
format = PyUnicode_InternFromString("%s(%r)");
if (format == NULL)
return NULL;
}
val = Simple_get_value(self); val = Simple_get_value(self);
if (val == NULL) if (val == NULL)
return NULL; return NULL;
name = PyUnicode_FromString(Py_TYPE(self)->tp_name); result = PyUnicode_FromFormat("%s(%R)",
if (name == NULL) { Py_TYPE(self)->tp_name, val);
Py_DECREF(val);
return NULL;
}
args = PyTuple_Pack(2, name, val);
Py_DECREF(name);
Py_DECREF(val); Py_DECREF(val);
if (args == NULL)
return NULL;
result = PyUnicode_Format(format, args);
Py_DECREF(args);
return result; return result;
} }

View File

@ -217,8 +217,6 @@ PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)
pysqlite_Node* ptr; pysqlite_Node* ptr;
PyObject* prevkey; PyObject* prevkey;
PyObject* nextkey; PyObject* nextkey;
PyObject* fmt_args;
PyObject* template;
PyObject* display_str; PyObject* display_str;
ptr = self->first; ptr = self->first;
@ -229,36 +227,21 @@ PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)
} else { } else {
prevkey = Py_None; prevkey = Py_None;
} }
Py_INCREF(prevkey);
if (ptr->next) { if (ptr->next) {
nextkey = ptr->next->key; nextkey = ptr->next->key;
} else { } else {
nextkey = Py_None; nextkey = Py_None;
} }
Py_INCREF(nextkey);
fmt_args = Py_BuildValue("OOO", prevkey, ptr->key, nextkey); display_str = PyUnicode_FromFormat("%S <- %S -> %S\n",
if (!fmt_args) { prevkey, ptr->key, nextkey);
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);
if (!display_str) { if (!display_str) {
return NULL; return NULL;
} }
PyObject_Print(display_str, stdout, Py_PRINT_RAW); PyObject_Print(display_str, stdout, Py_PRINT_RAW);
Py_DECREF(display_str); Py_DECREF(display_str);
Py_DECREF(prevkey);
Py_DECREF(nextkey);
ptr = ptr->next; ptr = ptr->next;
} }

View File

@ -2121,9 +2121,8 @@ static PyObject *
listindex(PyListObject *self, PyObject *args) listindex(PyListObject *self, PyObject *args)
{ {
Py_ssize_t i, start=0, stop=Py_SIZE(self); 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; PyObject *start_obj = NULL, *stop_obj = NULL;
static PyObject *err_format = NULL;
if (!PyArg_ParseTuple(args, "O|OO:index", &v, &start_obj, &stop_obj)) if (!PyArg_ParseTuple(args, "O|OO:index", &v, &start_obj, &stop_obj))
return NULL; return NULL;
@ -2153,20 +2152,7 @@ listindex(PyListObject *self, PyObject *args)
else if (cmp < 0) else if (cmp < 0)
return NULL; return NULL;
} }
if (err_format == NULL) { PyErr_Format(PyExc_ValueError, "%R is not in list", v);
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);
return NULL; return NULL;
} }