[Python-checkins] cpython (3.4): Issue #21497: faulthandler functions now raise a better error if sys.stderr is
victor.stinner
python-checkins at python.org
Wed May 14 17:17:16 CEST 2014
http://hg.python.org/cpython/rev/ddd7db7cf036
changeset: 90704:ddd7db7cf036
branch: 3.4
parent: 90701:36bc19c78270
user: Victor Stinner <victor.stinner at gmail.com>
date: Wed May 14 17:15:50 2014 +0200
summary:
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'").
files:
Lib/test/test_faulthandler.py | 25 +++++++++++++++++++++++
Modules/faulthandler.c | 4 +++
2 files changed, 29 insertions(+), 0 deletions(-)
diff --git a/Lib/test/test_faulthandler.py b/Lib/test/test_faulthandler.py
--- a/Lib/test/test_faulthandler.py
+++ b/Lib/test/test_faulthandler.py
@@ -591,6 +591,31 @@
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()
diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c
--- a/Modules/faulthandler.c
+++ b/Modules/faulthandler.c
@@ -144,6 +144,10 @@
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, "");
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list