Commit 96aeaec6 authored by Serhiy Storchaka's avatar Serhiy Storchaka Committed by GitHub

bpo-36793: Remove unneeded __str__ definitions. (GH-13081)

Classes that define __str__ the same as __repr__ can
just inherit it from object.
parent 96466308
...@@ -811,6 +811,13 @@ Changes in Python behavior ...@@ -811,6 +811,13 @@ Changes in Python behavior
raised when getting the attribute from the type dictionary are no longer raised when getting the attribute from the type dictionary are no longer
ignored. (Contributed by Serhiy Storchaka in :issue:`35459`.) ignored. (Contributed by Serhiy Storchaka in :issue:`35459`.)
* Removed ``__str__`` implementations from builtin types :class:`bool`,
:class:`int`, :class:`float`, :class:`complex` and few classes from
the standard library. They now inherit ``__str__()`` from :class:`object`.
As result, defining the ``__repr__()`` method in the subclass of these
classes will affect they string representation.
(Contributed by Serhiy Storchaka in :issue:`36793`.)
* On AIX, :attr:`sys.platform` doesn't contain the major version anymore. * On AIX, :attr:`sys.platform` doesn't contain the major version anymore.
It is always ``'aix'``, instead of ``'aix3'`` .. ``'aix7'``. Since It is always ``'aix'``, instead of ``'aix3'`` .. ``'aix7'``. Since
older Python versions include the version number, it is recommended to older Python versions include the version number, it is recommended to
......
...@@ -5631,8 +5631,6 @@ class _WorkRep(object): ...@@ -5631,8 +5631,6 @@ class _WorkRep(object):
def __repr__(self): def __repr__(self):
return "(%r, %r, %r)" % (self.sign, self.int, self.exp) return "(%r, %r, %r)" % (self.sign, self.int, self.exp)
__str__ = __repr__
def _normalize(op1, op2, prec = 0): def _normalize(op1, op2, prec = 0):
......
...@@ -262,8 +262,6 @@ class dispatcher: ...@@ -262,8 +262,6 @@ class dispatcher:
status.append(repr(self.addr)) status.append(repr(self.addr))
return '<%s at %#x>' % (' '.join(status), id(self)) return '<%s at %#x>' % (' '.join(status), id(self))
__str__ = __repr__
def add_channel(self, map=None): def add_channel(self, map=None):
#self.log_info('adding channel %s' % self) #self.log_info('adding channel %s' % self)
if map is None: if map is None:
......
...@@ -2300,7 +2300,7 @@ class DocTestCase(unittest.TestCase): ...@@ -2300,7 +2300,7 @@ class DocTestCase(unittest.TestCase):
name = self._dt_test.name.split('.') name = self._dt_test.name.split('.')
return "%s (%s)" % (name[-1], '.'.join(name[:-1])) return "%s (%s)" % (name[-1], '.'.join(name[:-1]))
__str__ = __repr__ __str__ = object.__str__
def shortDescription(self): def shortDescription(self):
return "Doctest: " + self._dt_test.name return "Doctest: " + self._dt_test.name
...@@ -2399,7 +2399,6 @@ class DocFileCase(DocTestCase): ...@@ -2399,7 +2399,6 @@ class DocFileCase(DocTestCase):
def __repr__(self): def __repr__(self):
return self._dt_test.filename return self._dt_test.filename
__str__ = __repr__
def format_failure(self, err): def format_failure(self, err):
return ('Failed doctest test for %s\n File "%s", line 0\n\n%s' return ('Failed doctest test for %s\n File "%s", line 0\n\n%s'
......
...@@ -241,11 +241,9 @@ class Charset: ...@@ -241,11 +241,9 @@ class Charset:
self.output_codec = CODEC_MAP.get(self.output_charset, self.output_codec = CODEC_MAP.get(self.output_charset,
self.output_charset) self.output_charset)
def __str__(self): def __repr__(self):
return self.input_charset.lower() return self.input_charset.lower()
__repr__ = __str__
def __eq__(self, other): def __eq__(self, other):
return str(self) == str(other).lower() return str(self) == str(other).lower()
......
...@@ -1419,8 +1419,7 @@ class IncompleteRead(HTTPException): ...@@ -1419,8 +1419,7 @@ class IncompleteRead(HTTPException):
e = '' e = ''
return '%s(%i bytes read%s)' % (self.__class__.__name__, return '%s(%i bytes read%s)' % (self.__class__.__name__,
len(self.partial), e) len(self.partial), e)
def __str__(self): __str__ = object.__str__
return repr(self)
class ImproperConnectionState(HTTPException): class ImproperConnectionState(HTTPException):
pass pass
......
...@@ -268,7 +268,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, ...@@ -268,7 +268,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
list=list, list=list,
str=str, str=str,
tuple=tuple, tuple=tuple,
_intstr=int.__str__, _intstr=int.__repr__,
): ):
if _indent is not None and not isinstance(_indent, str): if _indent is not None and not isinstance(_indent, str):
...@@ -307,7 +307,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, ...@@ -307,7 +307,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
elif value is False: elif value is False:
yield buf + 'false' yield buf + 'false'
elif isinstance(value, int): elif isinstance(value, int):
# Subclasses of int/float may override __str__, but we still # Subclasses of int/float may override __repr__, but we still
# want to encode them as integers/floats in JSON. One example # want to encode them as integers/floats in JSON. One example
# within the standard library is IntEnum. # within the standard library is IntEnum.
yield buf + _intstr(value) yield buf + _intstr(value)
......
...@@ -364,12 +364,10 @@ class LogRecord(object): ...@@ -364,12 +364,10 @@ class LogRecord(object):
else: else:
self.process = None self.process = None
def __str__(self): def __repr__(self):
return '<LogRecord: %s, %s, %s, %s, "%s">'%(self.name, self.levelno, return '<LogRecord: %s, %s, %s, %s, "%s">'%(self.name, self.levelno,
self.pathname, self.lineno, self.msg) self.pathname, self.lineno, self.msg)
__repr__ = __str__
def getMessage(self): def getMessage(self):
""" """
Return the message for this LogRecord. Return the message for this LogRecord.
......
...@@ -59,11 +59,9 @@ class _NamedIntConstant(int): ...@@ -59,11 +59,9 @@ class _NamedIntConstant(int):
self.name = name self.name = name
return self return self
def __str__(self): def __repr__(self):
return self.name return self.name
__repr__ = __str__
MAXREPEAT = _NamedIntConstant(MAXREPEAT, 'MAXREPEAT') MAXREPEAT = _NamedIntConstant(MAXREPEAT, 'MAXREPEAT')
def _makecodes(names): def _makecodes(names):
......
...@@ -203,7 +203,6 @@ if _mswindows: ...@@ -203,7 +203,6 @@ if _mswindows:
return "%s(%d)" % (self.__class__.__name__, int(self)) return "%s(%d)" % (self.__class__.__name__, int(self))
__del__ = Close __del__ = Close
__str__ = __repr__
else: else:
# When select or poll has indicated that the file is writable, # When select or poll has indicated that the file is writable,
# we can write up to _PIPE_BUF bytes without risk of blocking. # we can write up to _PIPE_BUF bytes without risk of blocking.
......
...@@ -186,8 +186,7 @@ INTERNAL_ERROR = -32603 ...@@ -186,8 +186,7 @@ INTERNAL_ERROR = -32603
class Error(Exception): class Error(Exception):
"""Base class for client errors.""" """Base class for client errors."""
def __str__(self): __str__ = object.__str__
return repr(self)
## ##
# Indicates an HTTP-level protocol error. This is raised by the HTTP # Indicates an HTTP-level protocol error. This is raised by the HTTP
...@@ -869,8 +868,6 @@ class MultiCall: ...@@ -869,8 +868,6 @@ class MultiCall:
def __repr__(self): def __repr__(self):
return "<%s at %#x>" % (self.__class__.__name__, id(self)) return "<%s at %#x>" % (self.__class__.__name__, id(self))
__str__ = __repr__
def __getattr__(self, name): def __getattr__(self, name):
return _MultiCallMethod(self.__call_list, name) return _MultiCallMethod(self.__call_list, name)
...@@ -1468,8 +1465,6 @@ class ServerProxy: ...@@ -1468,8 +1465,6 @@ class ServerProxy:
(self.__class__.__name__, self.__host, self.__handler) (self.__class__.__name__, self.__host, self.__handler)
) )
__str__ = __repr__
def __getattr__(self, name): def __getattr__(self, name):
# magic method dispatcher # magic method dispatcher
return _Method(self.__request, name) return _Method(self.__request, name)
......
Removed ``__str__`` implementations from builtin types :class:`bool`,
:class:`int`, :class:`float`, :class:`complex` and few classes from the
standard library. They now inherit ``__str__()`` from :class:`object`.
...@@ -5390,7 +5390,7 @@ static PyTypeObject PyDecContext_Type = ...@@ -5390,7 +5390,7 @@ static PyTypeObject PyDecContext_Type =
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
(hashfunc) 0, /* tp_hash */ (hashfunc) 0, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
(reprfunc) context_repr, /* tp_str */ 0, /* tp_str */
(getattrofunc) context_getattr, /* tp_getattro */ (getattrofunc) context_getattr, /* tp_getattro */
(setattrofunc) context_setattr, /* tp_setattro */ (setattrofunc) context_setattr, /* tp_setattro */
(PyBufferProcs *) 0, /* tp_as_buffer */ (PyBufferProcs *) 0, /* tp_as_buffer */
......
...@@ -1482,7 +1482,7 @@ encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc, ...@@ -1482,7 +1482,7 @@ encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
return _steal_accumulate(acc, encoded); return _steal_accumulate(acc, encoded);
} }
else if (PyLong_Check(obj)) { else if (PyLong_Check(obj)) {
PyObject *encoded = PyLong_Type.tp_str(obj); PyObject *encoded = PyLong_Type.tp_repr(obj);
if (encoded == NULL) if (encoded == NULL)
return -1; return -1;
return _steal_accumulate(acc, encoded); return _steal_accumulate(acc, encoded);
...@@ -1646,7 +1646,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc, ...@@ -1646,7 +1646,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
goto bail; goto bail;
} }
else if (PyLong_Check(key)) { else if (PyLong_Check(key)) {
kstr = PyLong_Type.tp_str(key); kstr = PyLong_Type.tp_repr(key);
if (kstr == NULL) { if (kstr == NULL) {
goto bail; goto bail;
} }
......
...@@ -147,7 +147,7 @@ PyTypeObject PyBool_Type = { ...@@ -147,7 +147,7 @@ PyTypeObject PyBool_Type = {
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
0, /* tp_hash */ 0, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
bool_repr, /* tp_str */ 0, /* tp_str */
0, /* tp_getattro */ 0, /* tp_getattro */
0, /* tp_setattro */ 0, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
......
...@@ -1129,7 +1129,7 @@ PyTypeObject PyComplex_Type = { ...@@ -1129,7 +1129,7 @@ PyTypeObject PyComplex_Type = {
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
(hashfunc)complex_hash, /* tp_hash */ (hashfunc)complex_hash, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
(reprfunc)complex_repr, /* tp_str */ 0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */ PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */ 0, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
......
...@@ -1923,7 +1923,7 @@ PyTypeObject PyFloat_Type = { ...@@ -1923,7 +1923,7 @@ PyTypeObject PyFloat_Type = {
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
(hashfunc)float_hash, /* tp_hash */ (hashfunc)float_hash, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
(reprfunc)float_repr, /* tp_str */ 0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */ PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */ 0, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
......
...@@ -5592,7 +5592,7 @@ PyTypeObject PyLong_Type = { ...@@ -5592,7 +5592,7 @@ PyTypeObject PyLong_Type = {
0, /* tp_as_mapping */ 0, /* tp_as_mapping */
(hashfunc)long_hash, /* tp_hash */ (hashfunc)long_hash, /* tp_hash */
0, /* tp_call */ 0, /* tp_call */
long_to_decimal_string, /* tp_str */ 0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */ PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */ 0, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment