8000 bpo-28974: `object.__format__(x, '')` is now equivalent to `str(x)` (… · python/cpython@7e19dbc · GitHub
[go: up one dir, main page]

Skip to content

Commit 7e19dbc

Browse files
bpo-28974: object.__format__(x, '') is now equivalent to str(x) (#506)
rather than `format(str(self), '')`.
1 parent a66f9c6 commit 7e19dbc

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

Doc/reference/datamodel.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,10 @@ Basic customization
12821282
The __format__ method of ``object`` itself raises a :exc:`TypeError`
12831283
if passed any non-empty string.
12841284

1285+
.. versionchanged:: 3.7
1286+
``object.__format__(x, '')`` is now equivalent to ``str(x)`` rather
1287+
than ``format(str(self), '')``.
1288+
12851289

12861290
.. _richcmpfuncs:
12871291
.. method:: object.__lt__(self, other)

Doc/whatsnew/3.7.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ Other Language Changes
8989
a name are now supported.
9090
(Contributed by Serhiy Storchaka in :issue:`30024`.)
9191

92+
* ``object.__format__(x, '')`` is now equivalent to ``str(x)`` rather than
93+
``format(str(self), '')``.
94+
(Contributed by Serhiy Storchaka in :issue:`28974`.)
95+
9296

9397
New Modules
9498
===========

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- bpo-28974: ``object.__format__(x, '')`` is now equivalent to ``str(x)``
14+
rather than ``format(str(self), '')``.
15+
1316
- bpo-30024: Circular imports involving absolute imports with binding
1417
a submodule to a name are now supported.
1518

Objects/typeobject.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4493,9 +4493,6 @@ static PyObject *
44934493
object___format___impl(PyObject *self, PyObject *format_spec)
44944494
/*[clinic end generated code: output=34897efb543a974b input=7c3b3bc53a6fb7fa]*/
44954495
{
4496-
PyObject *self_as_str = NULL;
4497-
PyObject *result = NULL;
4498-
44994496
/* Issue 7994: If we're converting to a string, we
45004497
should reject format specifications */
45014498
if (PyUnicode_GET_LENGTH(format_spec) > 0) {
@@ -4504,12 +4501,7 @@ object___format___impl(PyObject *self, PyObject *format_spec)
45044501
self->ob_type->tp_name);
45054502
return NULL;
45064503
}
4507-
self_as_str = PyObject_Str(self);
4508-
if (self_as_str != NULL) {
4509-
result = PyObject_Format(self_as_str, format_spec);
4510-
Py_DECREF(self_as_str);
4511-
}
4512-
return result;
4504+
return PyObject_Str(self);
45134505
}
45144506

45154507
/*[clinic input]

0 commit comments

Comments
 (0)
0