[Python-checkins] cpython: Issue #13350: Replace most usages of PyUnicode_Format by PyUnicode_FromFormat.

amaury.forgeotdarc python-checkins at python.org
Sun Nov 6 15:15:25 CET 2011


http://hg.python.org/cpython/rev/386a319b1825
changeset:   73403:386a319b1825
parent:      73401:626c6c7f3af6
user:        Amaury Forgeot d'Arc <amauryfa at gmail.com>
date:        Sun Nov 06 15:10:48 2011 +0100
summary:
  Issue #13350: Replace most usages of PyUnicode_Format by PyUnicode_FromFormat.

files:
  Doc/includes/noddy2.c     |  18 +-----------------
  Doc/includes/noddy3.c     |  18 +-----------------
  Doc/includes/noddy4.c     |  18 +-----------------
  Misc/NEWS                 |   3 +++
  Modules/_ctypes/_ctypes.c |  24 +++---------------------
  Modules/_sqlite/cache.c   |  21 ++-------------------
  Objects/listobject.c      |  18 ++----------------
  7 files changed, 13 insertions(+), 107 deletions(-)


diff --git a/Doc/includes/noddy2.c b/Doc/includes/noddy2.c
--- a/Doc/includes/noddy2.c
+++ b/Doc/includes/noddy2.c
@@ -84,15 +84,6 @@
 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 @@
         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
--- a/Doc/includes/noddy3.c
+++ b/Doc/includes/noddy3.c
@@ -147,23 +147,7 @@
 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
--- a/Doc/includes/noddy4.c
+++ b/Doc/includes/noddy4.c
@@ -118,15 +118,6 @@
 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 @@
         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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 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
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -4599,38 +4599,20 @@
 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
--- a/Modules/_sqlite/cache.c
+++ b/Modules/_sqlite/cache.c
@@ -217,8 +217,6 @@
     pysqlite_Node* ptr;
     PyObject* prevkey;
     PyObject* nextkey;
-    PyObject* fmt_args;
-    PyObject* template;
     PyObject* display_str;
 
     ptr = self->first;
@@ -229,36 +227,21 @@
         } 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
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -2121,9 +2121,8 @@
 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 @@
         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;
 }
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list