]> granicus.if.org Git - python/commitdiff
bpo-37960: Silence only necessary errors in repr() of buffered and text streams....
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 29 Aug 2019 08:13:29 +0000 (01:13 -0700)
committerGitHub <noreply@github.com>
Thu, 29 Aug 2019 08:13:29 +0000 (01:13 -0700)
(cherry picked from commit b235a1b47394eedc5f8ea4cf214f56c4c6932e59)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/_pyio.py
Misc/NEWS.d/next/Library/2019-08-27-10-52-13.bpo-37960.CTY7Lw.rst [new file with mode: 0644]
Modules/_io/bufferedio.c
Modules/_io/textio.c

index 0b6493bc8dc926281803f955c4eb99c358512ce7..650109295fe8e3c9bac53e7ecc16f8e477f8b942 100644 (file)
@@ -407,7 +407,7 @@ class IOBase(metaclass=abc.ABCMeta):
         """Destructor.  Calls close()."""
         try:
             closed = self.closed
-        except Exception:
+        except AttributeError:
             # If getting closed fails, then the object is probably
             # in an unusable state, so ignore.
             return
@@ -865,7 +865,7 @@ class _BufferedIOMixin(BufferedIOBase):
         clsname = self.__class__.__qualname__
         try:
             name = self.name
-        except Exception:
+        except AttributeError:
             return "<{}.{}>".format(modname, clsname)
         else:
             return "<{}.{} name={!r}>".format(modname, clsname, name)
@@ -2079,13 +2079,13 @@ class TextIOWrapper(TextIOBase):
                                  self.__class__.__qualname__)
         try:
             name = self.name
-        except Exception:
+        except AttributeError:
             pass
         else:
             result += " name={0!r}".format(name)
         try:
             mode = self.mode
-        except Exception:
+        except AttributeError:
             pass
         else:
             result += " mode={0!r}".format(mode)
diff --git a/Misc/NEWS.d/next/Library/2019-08-27-10-52-13.bpo-37960.CTY7Lw.rst b/Misc/NEWS.d/next/Library/2019-08-27-10-52-13.bpo-37960.CTY7Lw.rst
new file mode 100644 (file)
index 0000000..421cfb9
--- /dev/null
@@ -0,0 +1,2 @@
+``repr()`` of buffered and text streams now silences only expected
+exceptions when get the value of "name" and "mode" attributes.
index 44e12db6a30ed051791e64399c8daa677c1116d2..8e8ff97ff8c676f951f8cc0e301c7ec36414ff16 100644 (file)
@@ -1378,12 +1378,14 @@ buffered_repr(buffered *self)
 {
     PyObject *nameobj, *res;
 
-    nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
-    if (nameobj == NULL) {
-        if (PyErr_ExceptionMatches(PyExc_Exception))
-            PyErr_Clear();
-        else
+    if (_PyObject_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) {
+        if (!PyErr_ExceptionMatches(PyExc_ValueError)) {
             return NULL;
+        }
+        /* Ignore ValueError raised if the underlying stream was detached */
+        PyErr_Clear();
+    }
+    if (nameobj == NULL) {
         res = PyUnicode_FromFormat("<%s>", Py_TYPE(self)->tp_name);
     }
     else {
index 73b2756afce5e641ae028ce93f3d051d1dce1d6f..4fe1b295f8856a35603f8fb72f1f98dd183f3fc8 100644 (file)
@@ -2860,14 +2860,14 @@ textiowrapper_repr(textio *self)
         }
         goto error;
     }
-    nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
-    if (nameobj == NULL) {
-        if (PyErr_ExceptionMatches(PyExc_Exception))
-            PyErr_Clear();
-        else
+    if (_PyObject_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) {
+        if (!PyErr_ExceptionMatches(PyExc_ValueError)) {
             goto error;
+        }
+        /* Ignore ValueError raised if the underlying stream was detached */
+        PyErr_Clear();
     }
-    else {
+    if (nameobj != NULL) {
         s = PyUnicode_FromFormat(" name=%R", nameobj);
         Py_DECREF(nameobj);
         if (s == NULL)
@@ -2876,14 +2876,10 @@ textiowrapper_repr(textio *self)
         if (res == NULL)
             goto error;
     }
-    modeobj = _PyObject_GetAttrId((PyObject *) self, &PyId_mode);
-    if (modeobj == NULL) {
-        if (PyErr_ExceptionMatches(PyExc_Exception))
-            PyErr_Clear();
-        else
-            goto error;
+    if (_PyObject_LookupAttrId((PyObject *) self, &PyId_mode, &modeobj) < 0) {
+        goto error;
     }
-    else {
+    if (modeobj != NULL) {
         s = PyUnicode_FromFormat(" mode=%R", modeobj);
         Py_DECREF(modeobj);
         if (s == NULL)