[Python-checkins] cpython: faulthandler: check PyThreadState_Get() result in dump_tracebacks_later()

victor.stinner python-checkins at python.org
Thu Apr 7 11:51:30 CEST 2011


http://hg.python.org/cpython/rev/7a77a0d9c5b7
changeset:   69182:7a77a0d9c5b7
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Thu Apr 07 11:37:19 2011 +0200
summary:
  faulthandler: check PyThreadState_Get() result in dump_tracebacks_later()

Cleanup also the code

files:
  Modules/faulthandler.c |  33 +++++++++++++++++++----------
  1 files changed, 21 insertions(+), 12 deletions(-)


diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c
--- a/Modules/faulthandler.c
+++ b/Modules/faulthandler.c
@@ -5,6 +5,9 @@
 #include <frameobject.h>
 #include <signal.h>
 
+/* Allocate at maximum 100 MB of the stack to raise the stack overflow */
+#define STACK_OVERFLOW_MAX_SIZE (100*1024*1024)
+
 #ifdef WITH_THREAD
 #  define FAULTHANDLER_LATER
 #endif
@@ -16,9 +19,6 @@
 #  define FAULTHANDLER_USER
 #endif
 
-/* Allocate at maximum 100 MB of the stack to raise the stack overflow */
-#define STACK_OVERFLOW_MAX_SIZE (100*1024*1024)
-
 #define PUTS(fd, str) write(fd, str, strlen(str))
 
 #ifdef HAVE_SIGACTION
@@ -451,8 +451,8 @@
 }
 
 static PyObject*
-faulthandler_dump_traceback_later(PyObject *self,
-                                  PyObject *args, PyObject *kwargs)
+faulthandler_dump_tracebacks_later(PyObject *self,
+                                   PyObject *args, PyObject *kwargs)
 {
     static char *kwlist[] = {"timeout", "repeat", "file", "exit", NULL};
     double timeout;
@@ -461,6 +461,7 @@
     PyObject *file = NULL;
     int fd;
     int exit = 0;
+    PyThreadState *tstate;
 
     if (!PyArg_ParseTupleAndKeywords(args, kwargs,
         "d|iOi:dump_tracebacks_later", kwlist,
@@ -477,6 +478,13 @@
         return NULL;
     }
 
+    tstate = PyThreadState_Get();
+    if (tstate == NULL) {
+        PyErr_SetString(PyExc_RuntimeError,
+                        "unable to get the current thread state");
+        return NULL;
+    }
+
     file = faulthandler_get_fileno(file, &fd);
     if (file == NULL)
         return NULL;
@@ -490,7 +498,7 @@
     thread.fd = fd;
     thread.timeout_ms = timeout_ms;
     thread.repeat = repeat;
-    thread.interp = PyThreadState_Get()->interp;
+    thread.interp = tstate->interp;
     thread.exit = exit;
 
     /* Arm these locks to serve as events when released */
@@ -826,7 +834,7 @@
 faulthandler_traverse(PyObject *module, visitproc visit, void *arg)
 {
 #ifdef FAULTHANDLER_USER
-    unsigned int index;
+    unsigned int signum;
 #endif
 
 #ifdef FAULTHANDLER_LATER
@@ -834,8 +842,8 @@
 #endif
 #ifdef FAULTHANDLER_USER
     if (user_signals != NULL) {
-        for (index=0; index < NSIG; index++)
-            Py_VISIT(user_signals[index].file);
+        for (signum=0; signum < NSIG; signum++)
+            Py_VISIT(user_signals[signum].file);
     }
 #endif
     Py_VISIT(fatal_error.file);
@@ -861,10 +869,11 @@
                "if all_threads is True, into file")},
 #ifdef FAULTHANDLER_LATER
     {"dump_tracebacks_later",
-     (PyCFunction)faulthandler_dump_traceback_later, METH_VARARGS|METH_KEYWORDS,
-     PyDoc_STR("dump_tracebacks_later(timeout, repeat=False, file=sys.stderr):\n"
+     (PyCFunction)faulthandler_dump_tracebacks_later, METH_VARARGS|METH_KEYWORDS,
+     PyDoc_STR("dump_tracebacks_later(timeout, repeat=False, file=sys.stderrn, exit=False):\n"
                "dump the traceback of all threads in timeout seconds,\n"
-               "or each timeout seconds if repeat is True.")},
+               "or each timeout seconds if repeat is True. If exit is True, "
+               "call _exit(1) which is not safe.")},
     {"cancel_dump_tracebacks_later",
      (PyCFunction)faulthandler_cancel_dump_tracebacks_later_py, METH_NOARGS,
      PyDoc_STR("cancel_dump_tracebacks_later():\ncancel the previous call "

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list