Commits

Victor Stinner  committed cb26887 Merge

(Merge 3.4) Issue #21497: faulthandler functions now raise a better error if
sys.stderr is None: RuntimeError("sys.stderr is None") instead of
AttributeError("'NoneType' object has no attribute 'fileno'").

  • Participants
  • Parent commits 0d38044, ddd7db7

Comments (0)

Files changed (2)

File Lib/test/test_faulthandler.py

     def test_register_chain(self):
         self.check_register(chain=True)
 
+    @contextmanager
+    def check_stderr_none(self):
+        stderr = sys.stderr
+        try:
+            sys.stderr = None
+            with self.assertRaises(RuntimeError) as cm:
+                yield
+            self.assertEqual(str(cm.exception), "sys.stderr is None")
+        finally:
+            sys.stderr = stderr
+
+    def test_stderr_None(self):
+        # Issue #21497: provide an helpful error if sys.stderr is None,
+        # instead of just an attribute error: "None has no attribute fileno".
+        with self.check_stderr_none():
+            faulthandler.enable()
+        with self.check_stderr_none():
+            faulthandler.dump_traceback()
+        if hasattr(faulthandler, 'dump_traceback_later'):
+            with self.check_stderr_none():
+                faulthandler.dump_traceback_later(1e-3)
+        if hasattr(faulthandler, "register"):
+            with self.check_stderr_none():
+                faulthandler.register(signal.SIGUSR1)
+
 
 if __name__ == "__main__":
     unittest.main()

File Modules/faulthandler.c

             PyErr_SetString(PyExc_RuntimeError, "unable to get sys.stderr");
             return NULL;
         }
+        if (file == Py_None) {
+            PyErr_SetString(PyExc_RuntimeError, "sys.stderr is None");
+            return NULL;
+        }
     }
 
     result = _PyObject_CallMethodId(file, &PyId_fileno, "");