From c236267c0efe1621fe98cca4490de29a0481acd3 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 6 Mar 2017 11:50:55 +0200 Subject: [PATCH 1/4] bpo-28974: `object.__format__(x, '')` is now equivalent to `str(x)` rather of `format(str(self), '')`. --- Doc/reference/datamodel.rst | 3 +++ Doc/whatsnew/3.7.rst | 4 ++++ Misc/NEWS | 3 +++ Objects/typeobject.c | 9 +-------- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 095a2380b379bc..5229dd731e21e6 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1282,6 +1282,9 @@ 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 of + ``format(str(self), '')``. .. _richcmpfuncs: .. method:: object.__lt__(self, other) diff --git a/Doc/whatsnew/3.7.rst b/Doc/whatsnew/3.7.rst index 5c5ca147e3adf5..cbce7a1af3da2f 100644 --- a/Doc/whatsnew/3.7.rst +++ b/Doc/whatsnew/3.7.rst @@ -87,6 +87,10 @@ Other Language Changes ``from ... import ...`` fails. (Contributed by Matthias Bussonnier in :issue:`29546`.) +* ``object.__format__(x, '')`` is now equivalent to ``str(x)`` rather of + ``format(str(self), '')``. + (Contributed by Serhiy Storchaka in :issue:`28974`.) + New Modules =========== diff --git a/Misc/NEWS b/Misc/NEWS index 81b02fdd4a48b3..5635f7506a34bb 100644 --- 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 of + `format(str(self), '')`. + - bpo-29714: Fix a regression that bytes format may fail when containing zero bytes inside. diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 18b67c83258dcb..49087f493e00e6 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -4440,8 +4440,6 @@ static PyObject * object_format(PyObject *self, PyObject *args) { PyObject *format_spec; - PyObject *self_as_str = NULL; - PyObject *result = NULL; if (!PyArg_ParseTuple(args, "U:__format__", &format_spec)) return NULL; @@ -4454,12 +4452,7 @@ object_format(PyObject *self, PyObject *args) 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); } static PyObject * From 5b75da2a16a65ee2690ec56532b0df3e04187c19 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 6 Mar 2017 23:55:02 +0200 Subject: [PATCH 2/4] Fixed wording. --- Doc/reference/datamodel.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 5229dd731e21e6..3694fcb002fa7f 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -1283,8 +1283,9 @@ Basic customization if passed any non-empty string. .. versionchanged:: 3.7 - ``object.__format__(x, '')`` is now equivalent to ``str(x)`` rather of - ``format(str(self), '')``. + ``object.__format__(x, '')`` is now equivalent to ``str(x)`` rather + than ``format(str(self), '')``. + .. _richcmpfuncs: .. method:: object.__lt__(self, other) From 787b81bb8b339e147a7022f367bcbef8d9420634 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 7 Mar 2017 09:57:22 +0200 Subject: [PATCH 3/4] Fix wording. --- Doc/whatsnew/3.7.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.7.rst b/Doc/whatsnew/3.7.rst index cbce7a1af3da2f..25573045ce6042 100644 --- a/Doc/whatsnew/3.7.rst +++ b/Doc/whatsnew/3.7.rst @@ -87,7 +87,7 @@ Other Language Changes ``from ... import ...`` fails. (Contributed by Matthias Bussonnier in :issue:`29546`.) -* ``object.__format__(x, '')`` is now equivalent to ``str(x)`` rather of +* ``object.__format__(x, '')`` is now equivalent to ``str(x)`` rather than ``format(str(self), '')``. (Contributed by Serhiy Storchaka in :issue:`28974`.) From affc6ffb581478e9fe235857db8451bd5582c176 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 7 Mar 2017 09:58:36 +0200 Subject: [PATCH 4/4] Fix wording. --- Misc/NEWS | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS b/Misc/NEWS index 5635f7506a34bb..dc59a973e74588 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,8 +10,8 @@ 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 of - `format(str(self), '')`. +- bpo-28974: `object.__format__(x, '')` is now equivalent to `str(x)` rather + than `format(str(self), '')`. - bpo-29714: Fix a regression that bytes format may fail when containing zero bytes inside.