Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-28974: object.__format__(x, '') is now equivalent to str(x) #506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/reference/datamodel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
===========
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
10 changes: 1 addition & 9 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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]
Expand Down