]> granicus.if.org Git - python/commitdiff
bpo-37960: Silence only necessary errors in repr() of buffered and text streams....
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 29 Aug 2019 06:25:22 +0000 (09:25 +0300)
committerGitHub <noreply@github.com>
Thu, 29 Aug 2019 06:25:22 +0000 (09:25 +0300)
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 40e0c9f9e2c66b62576239264a279f1e1055cb1d..1b24ef9f0e10a83f3577db5fe78dfe0cdb1df082 100644 (file)
@@ -409,7 +409,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
@@ -867,7 +867,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)
@@ -2083,13 +2083,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 86dd277f94c1bf0493439425140eadd077a056b0..586e93f25e28f45a1d94a9c2360b2d7a4cb42e02 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 05911d9222ff6e60955d9b92d66a0a856956c979..7ddac8062a96e9dfbb2630c93f3f20d95afa64b4 100644 (file)
@@ -2899,14 +2899,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)
@@ -2915,14 +2915,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)