]> granicus.if.org Git - python/commitdiff
bpo-36793: Remove unneeded __str__ definitions. (GH-13081)
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 6 May 2019 19:29:40 +0000 (22:29 +0300)
committerGitHub <noreply@github.com>
Mon, 6 May 2019 19:29:40 +0000 (22:29 +0300)
Classes that define __str__ the same as __repr__ can
just inherit it from object.

18 files changed:
Doc/whatsnew/3.8.rst
Lib/_pydecimal.py
Lib/asyncore.py
Lib/doctest.py
Lib/email/charset.py
Lib/http/client.py
Lib/json/encoder.py
Lib/logging/__init__.py
Lib/sre_constants.py
Lib/subprocess.py
Lib/xmlrpc/client.py
Misc/NEWS.d/next/Core and Builtins/2019-05-04-16-15-33.bpo-36793.Izog4Z.rst [new file with mode: 0644]
Modules/_decimal/_decimal.c
Modules/_json.c
Objects/boolobject.c
Objects/complexobject.c
Objects/floatobject.c
Objects/longobject.c

index 64ef6e1840605b44e8cbdd639e59d1bb3f04e010..d6388f8faaba4f550e3e662d74ab4dc787a10c0c 100644 (file)
@@ -811,6 +811,13 @@ Changes in Python behavior
   raised when getting the attribute from the type dictionary are no longer
   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.
   It is always ``'aix'``, instead of ``'aix3'`` .. ``'aix7'``.  Since
   older Python versions include the version number, it is recommended to
index 44ea5b41b2a1f6e10f904c9a211e14c09b3457ea..c14d8ca86a1181d951048b2fb939560793b1bb64 100644 (file)
@@ -5631,8 +5631,6 @@ class _WorkRep(object):
     def __repr__(self):
         return "(%r, %r, %r)" % (self.sign, self.int, self.exp)
 
-    __str__ = __repr__
-
 
 
 def _normalize(op1, op2, prec = 0):
index 828f4d4fe7897b734f56fcf02355d1a58da4a199..0e92be3ad1912c14b59ccd240a896f745f08ccc9 100644 (file)
@@ -262,8 +262,6 @@ class dispatcher:
                 status.append(repr(self.addr))
         return '<%s at %#x>' % (' '.join(status), id(self))
 
-    __str__ = __repr__
-
     def add_channel(self, map=None):
         #self.log_info('adding channel %s' % self)
         if map is None:
index 79d91a040c2eeeff533f100f8342ac2ed7b71d9a..bf4889f59e0da4cb6103fc2e87e3f5067a6f7f98 100644 (file)
@@ -2300,7 +2300,7 @@ class DocTestCase(unittest.TestCase):
         name = self._dt_test.name.split('.')
         return "%s (%s)" % (name[-1], '.'.join(name[:-1]))
 
-    __str__ = __repr__
+    __str__ = object.__str__
 
     def shortDescription(self):
         return "Doctest: " + self._dt_test.name
@@ -2399,7 +2399,6 @@ class DocFileCase(DocTestCase):
 
     def __repr__(self):
         return self._dt_test.filename
-    __str__ = __repr__
 
     def format_failure(self, err):
         return ('Failed doctest test for %s\n  File "%s", line 0\n\n%s'
index ee564040c68f8b3572757693bc3d5dc375fe80ce..d3d759ad9115f04fd809d74f3feaeb3b0eb4333c 100644 (file)
@@ -241,11 +241,9 @@ class Charset:
         self.output_codec = CODEC_MAP.get(self.output_charset,
                                           self.output_charset)
 
-    def __str__(self):
+    def __repr__(self):
         return self.input_charset.lower()
 
-    __repr__ = __str__
-
     def __eq__(self, other):
         return str(self) == str(other).lower()
 
index f71a062d2b5783e4fd92b44153a453460f29e699..82908ebe3afd65d5109d3e285960f347154483a2 100644 (file)
@@ -1419,8 +1419,7 @@ class IncompleteRead(HTTPException):
             e = ''
         return '%s(%i bytes read%s)' % (self.__class__.__name__,
                                         len(self.partial), e)
-    def __str__(self):
-        return repr(self)
+    __str__ = object.__str__
 
 class ImproperConnectionState(HTTPException):
     pass
index 2d7b8989c711daf8168412277b7dbed252783618..c8c78b9c237652d13a22c6d79bf171280e7eb420 100644 (file)
@@ -268,7 +268,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
         list=list,
         str=str,
         tuple=tuple,
-        _intstr=int.__str__,
+        _intstr=int.__repr__,
     ):
 
     if _indent is not None and not isinstance(_indent, str):
@@ -307,7 +307,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
             elif value is False:
                 yield buf + 'false'
             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
                 # within the standard library is IntEnum.
                 yield buf + _intstr(value)
index 7355396541a3e9c54dbd6ea93e93fb854e5bc828..e093982a0cdfbc9f4573ac67fde038c223dec8cc 100644 (file)
@@ -364,12 +364,10 @@ class LogRecord(object):
         else:
             self.process = None
 
-    def __str__(self):
+    def __repr__(self):
         return '<LogRecord: %s, %s, %s, %s, "%s">'%(self.name, self.levelno,
             self.pathname, self.lineno, self.msg)
 
-    __repr__ = __str__
-
     def getMessage(self):
         """
         Return the message for this LogRecord.
index 13deb00bc81c25c1d7af8fbf41850dcb7c74fb20..8e613cb3fa5dcbae0c58b1b04f7e397863b911d9 100644 (file)
@@ -59,11 +59,9 @@ class _NamedIntConstant(int):
         self.name = name
         return self
 
-    def __str__(self):
+    def __repr__(self):
         return self.name
 
-    __repr__ = __str__
-
 MAXREPEAT = _NamedIntConstant(MAXREPEAT, 'MAXREPEAT')
 
 def _makecodes(names):
index 0496b447e8ea035759674509ffdfc393d1ca15fc..6cc9eb322e280b2686098164cbb4cd610fdc2657 100644 (file)
@@ -203,7 +203,6 @@ if _mswindows:
             return "%s(%d)" % (self.__class__.__name__, int(self))
 
         __del__ = Close
-        __str__ = __repr__
 else:
     # When select or poll has indicated that the file is writable,
     # we can write up to _PIPE_BUF bytes without risk of blocking.
index a0e923a20322291adf0d2f9224ebc47d8097a288..b9875745000febf35d53e0bb7ad198fa8effef0f 100644 (file)
@@ -186,8 +186,7 @@ INTERNAL_ERROR        = -32603
 
 class Error(Exception):
     """Base class for client errors."""
-    def __str__(self):
-        return repr(self)
+    __str__ = object.__str__
 
 ##
 # Indicates an HTTP-level protocol error.  This is raised by the HTTP
@@ -869,8 +868,6 @@ class MultiCall:
     def __repr__(self):
         return "<%s at %#x>" % (self.__class__.__name__, id(self))
 
-    __str__ = __repr__
-
     def __getattr__(self, name):
         return _MultiCallMethod(self.__call_list, name)
 
@@ -1468,8 +1465,6 @@ class ServerProxy:
             (self.__class__.__name__, self.__host, self.__handler)
             )
 
-    __str__ = __repr__
-
     def __getattr__(self, name):
         # magic method dispatcher
         return _Method(self.__request, name)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-05-04-16-15-33.bpo-36793.Izog4Z.rst b/Misc/NEWS.d/next/Core and Builtins/2019-05-04-16-15-33.bpo-36793.Izog4Z.rst
new file mode 100644 (file)
index 0000000..6c79f97
--- /dev/null
@@ -0,0 +1,3 @@
+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`.
index 51aed2c67dc65ee309f92f3cd9e34248d5ce1766..d977b14f5b0cb8b0dea3f9e010a7eda3de967ba8 100644 (file)
@@ -5390,7 +5390,7 @@ static PyTypeObject PyDecContext_Type =
     0,                                         /* tp_as_mapping */
     (hashfunc) 0,                              /* tp_hash */
     0,                                         /* tp_call */
-    (reprfunc) context_repr,                   /* tp_str */
+    0,                                         /* tp_str */
     (getattrofunc) context_getattr,            /* tp_getattro */
     (setattrofunc) context_setattr,            /* tp_setattro */
     (PyBufferProcs *) 0,                       /* tp_as_buffer */
index 2d7c1bf1e1c77d805265c5d67b1a67ccabe2920a..4faa9cc22edf01d8f880ef7a123f328cb8ba7bc8 100644 (file)
@@ -1482,7 +1482,7 @@ encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
         return _steal_accumulate(acc, encoded);
     }
     else if (PyLong_Check(obj)) {
-        PyObject *encoded = PyLong_Type.tp_str(obj);
+        PyObject *encoded = PyLong_Type.tp_repr(obj);
         if (encoded == NULL)
             return -1;
         return _steal_accumulate(acc, encoded);
@@ -1646,7 +1646,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
                 goto bail;
         }
         else if (PyLong_Check(key)) {
-            kstr = PyLong_Type.tp_str(key);
+            kstr = PyLong_Type.tp_repr(key);
             if (kstr == NULL) {
                 goto bail;
             }
index b92fafe620c102d8bd167df1cfcfd188b012da46..508ea61f18001784a5e498f723a09a04d73c9544 100644 (file)
@@ -147,7 +147,7 @@ PyTypeObject PyBool_Type = {
     0,                                          /* tp_as_mapping */
     0,                                          /* tp_hash */
     0,                                          /* tp_call */
-    bool_repr,                                  /* tp_str */
+    0,                                          /* tp_str */
     0,                                          /* tp_getattro */
     0,                                          /* tp_setattro */
     0,                                          /* tp_as_buffer */
index 6e3d47b62d193740fa0fe089048a137aa203ce19..cae2bf11dc9b39fdc74a22a356bb5600d2f1c238 100644 (file)
@@ -1129,7 +1129,7 @@ PyTypeObject PyComplex_Type = {
     0,                                          /* tp_as_mapping */
     (hashfunc)complex_hash,                     /* tp_hash */
     0,                                          /* tp_call */
-    (reprfunc)complex_repr,                     /* tp_str */
+    0,                                          /* tp_str */
     PyObject_GenericGetAttr,                    /* tp_getattro */
     0,                                          /* tp_setattro */
     0,                                          /* tp_as_buffer */
index b952df8807224a1891e3c5b4b6138746235e6c6c..adb9b80c271349fba3450b8e60c1ef0c10113ed7 100644 (file)
@@ -1923,7 +1923,7 @@ PyTypeObject PyFloat_Type = {
     0,                                          /* tp_as_mapping */
     (hashfunc)float_hash,                       /* tp_hash */
     0,                                          /* tp_call */
-    (reprfunc)float_repr,                       /* tp_str */
+    0,                                          /* tp_str */
     PyObject_GenericGetAttr,                    /* tp_getattro */
     0,                                          /* tp_setattro */
     0,                                          /* tp_as_buffer */
index da697a784faad0be06296c97753029e38074f5d6..9fb1fb02c276bd1446597ba1ce622d7bbe66ba12 100644 (file)
@@ -5592,7 +5592,7 @@ PyTypeObject PyLong_Type = {
     0,                                          /* tp_as_mapping */
     (hashfunc)long_hash,                        /* tp_hash */
     0,                                          /* tp_call */
-    long_to_decimal_string,                     /* tp_str */
+    0,                                          /* tp_str */
     PyObject_GenericGetAttr,                    /* tp_getattro */
     0,                                          /* tp_setattro */
     0,                                          /* tp_as_buffer */