]> granicus.if.org Git - python/commitdiff
bpo-28974: `object.__format__(x, '')` is now equivalent to `str(x)` (#506)
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 13 May 2017 09:40:52 +0000 (12:40 +0300)
committerGitHub <noreply@github.com>
Sat, 13 May 2017 09:40:52 +0000 (12:40 +0300)
rather than `format(str(self), '')`.

Doc/reference/datamodel.rst
Doc/whatsnew/3.7.rst
Misc/NEWS
Objects/typeobject.c

index 25afc351e8b5774e86c91777517b58295ab492ac..36bd71a7703fb7a845d54ad271a65dfd45a1c12d 100644 (file)
@@ -1282,6 +1282,10 @@ Basic customization
       The __format__ method of ``object`` itself raises a :exc:`TypeError`
       if passed any non-empty string.
 
+   .. versionchanged:: 3.7
+      ``object.__format__(x, '')`` is now equivalent to ``str(x)`` rather
+      than ``format(str(self), '')``.
+
 
 .. _richcmpfuncs:
 .. method:: object.__lt__(self, other)
index 57fd4e42a5455e536e9ce2132202097d2e6daa0f..3a001d7b4482ad9c0fbb220b6f41ec457f0cfa74 100644 (file)
@@ -89,6 +89,10 @@ Other Language Changes
   a name are now supported.
   (Contributed by Serhiy Storchaka in :issue:`30024`.)
 
+* ``object.__format__(x, '')`` is now equivalent to ``str(x)`` rather than
+  ``format(str(self), '')``.
+  (Contributed by Serhiy Storchaka in :issue:`28974`.)
+
 
 New Modules
 ===========
index 4e17a66bd9624e4d4c402ca04edc1a4af07f2152..73cd82c8b6b7571990c82f2884c61f9ffb3b9ba9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1?
 Core and Builtins
 -----------------
 
+- bpo-28974: ``object.__format__(x, '')`` is now equivalent to ``str(x)``
+  rather than ``format(str(self), '')``.
+
 - bpo-30024: Circular imports involving absolute imports with binding
   a submodule to a name are now supported.
 
index 9eb725f062ba56259f94e4b7318c1854d91fc28f..121d66d8bcabccaa552a15d2a8f9cdea6f681cfc 100644 (file)
@@ -4493,9 +4493,6 @@ static PyObject *
 object___format___impl(PyObject *self, PyObject *format_spec)
 /*[clinic end generated code: output=34897efb543a974b input=7c3b3bc53a6fb7fa]*/
 {
-    PyObject *self_as_str = NULL;
-    PyObject *result = NULL;
-
     /* Issue 7994: If we're converting to a string, we
        should reject format specifications */
     if (PyUnicode_GET_LENGTH(format_spec) > 0) {
@@ -4504,12 +4501,7 @@ object___format___impl(PyObject *self, PyObject *format_spec)
                      self->ob_type->tp_name);
         return NULL;
     }
-    self_as_str = PyObject_Str(self);
-    if (self_as_str != NULL) {
-        result = PyObject_Format(self_as_str, format_spec);
-        Py_DECREF(self_as_str);
-    }
-    return result;
+    return PyObject_Str(self);
 }
 
 /*[clinic input]