[Python-checkins] cpython: whatsnew: object.__format__ raises TypeError on non-empty string.

r.david.murray python-checkins at python.org
Wed Feb 12 00:34:45 CET 2014


http://hg.python.org/cpython/rev/f56b98143792
changeset: 89158:f56b98143792
user: R David Murray <rdmurray at bitdance.com>
date: Tue Feb 11 18:34:22 2014 -0500
summary:
 whatsnew: object.__format__ raises TypeError on non-empty string.
See issues #7994 and #9856. I also modified with wording of the format doc
entry to better match what really happens, and added a versionchanged to the
datamodel object.__format__ section.
files:
 Doc/library/functions.rst | 9 +++++----
 Doc/reference/datamodel.rst | 4 ++++
 Doc/whatsnew/3.4.rst | 9 +++++++++
 3 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst
--- a/Doc/library/functions.rst
+++ b/Doc/library/functions.rst
@@ -540,12 +540,13 @@
 A call to ``format(value, format_spec)`` is translated to
 ``type(value).__format__(format_spec)`` which bypasses the instance
 dictionary when searching for the value's :meth:`__format__` method. A
- :exc:`TypeError` exception is raised if the method is not found or if either
- the *format_spec* or the return value are not strings.
+ :exc:`TypeError` exception is raised if the method search reaches
+ :mod:`object` and the *format_spec* is non-empty, or if either the
+ *format_spec* or the return value are not strings.
 
- .. versionadded:: 3.4
+ .. versionchanged:: 3.4
 ``object().__format__(format_spec)`` raises :exc:`TypeError`
- if *format_spec* is not empty string.
+ if *format_spec* is not an empty string.
 
 
 .. _func-frozenset:
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -1226,6 +1226,10 @@
 
 The return value must be a string object.
 
+ .. versionchanged:: 3.4
+ The __format__ method of ``object`` itself raises a :exc:`TypeError`
+ if passed any non-empty string.
+
 
 .. _richcmpfuncs:
 .. method:: object.__lt__(self, other)
diff --git a/Doc/whatsnew/3.4.rst b/Doc/whatsnew/3.4.rst
--- a/Doc/whatsnew/3.4.rst
+++ b/Doc/whatsnew/3.4.rst
@@ -1600,6 +1600,15 @@
 * :class:`inspect.Signature`: positional-only parameters are now required
 to have a valid name.
 
+* :meth:`object.__format__` no longer accepts non-empty format strings, it now
+ raises a :exc:`TypeError` instead. Using a non-empty string has been
+ deprecated since Python 3.2. This change has been made to prevent a
+ situation where previously working (but incorrect) code would start failing
+ if an object gained a __format__ method, which means that your code may now
+ raise a :exc:`TypeError` if you are using an ``'s'`` format code with objects
+ that do not have a __format__ method that handles it. See :issue:`7994` for
+ background.
+
 
 Code Cleanups
 -------------
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /