diff Python/sysmodule.c @ 28119:869695cba2cd legacy-trunk

- New function sys.exc_clear() clears the current exception. This is rarely needed, but can sometimes be useful to release objects referenced by the traceback held in sys.exc_info()[2]. (SF patch #693195.) Thanks to Kevin Jacobs!
author Guido van Rossum <guido@python.org>
date Sat, 01 Mar 2003 03:20:41 +0000
parents 0fe321e78ad3
children 3f90d21debe9
line wrap: on
line diff
--- a/Python/sysmodule.c	Sat Mar 01 01:48:24 2003 +0000
+++ b/Python/sysmodule.c	Sat Mar 01 03:20:41 2003 +0000
@@ -132,7 +132,7 @@
 );
 
 static PyObject *
-sys_exc_info(PyObject *self)
+sys_exc_info(PyObject *self, PyObject *noargs)
 {
 	PyThreadState *tstate;
 	tstate = PyThreadState_Get();
@@ -147,8 +147,39 @@
 PyDoc_STRVAR(exc_info_doc,
 "exc_info() -> (type, value, traceback)\n\
 \n\
-Return information about the exception that is currently being handled.\n\
-This should be called from inside an except clause only."
+Return information about the most recent exception caught by an except\n\
+clause in the current stack frame or in an older stack frame."
+);
+
+static PyObject *
+sys_exc_clear(PyObject *self, PyObject *noargs)
+{
+	PyThreadState *tstate = PyThreadState_Get();
+	PyObject *tmp_type, *tmp_value, *tmp_tb;
+	tmp_type = tstate->exc_type;
+	tmp_value = tstate->exc_value;
+	tmp_tb = tstate->exc_traceback;
+	tstate->exc_type = NULL;
+	tstate->exc_value = NULL;
+	tstate->exc_traceback = NULL;
+	Py_XDECREF(tmp_type);
+	Py_XDECREF(tmp_value);
+	Py_XDECREF(tmp_tb);
+	/* For b/w compatibility */
+	PySys_SetObject("exc_type", Py_None);
+	PySys_SetObject("exc_value", Py_None);
+	PySys_SetObject("exc_traceback", Py_None);
+	Py_INCREF(Py_None);
+	return Py_None;
+}
+
+PyDoc_STRVAR(exc_clear_doc,
+"exc_clear() -> None\n\
+\n\
+Clear global information on the current exception.  Subsequent calls to\n\
+exc_info() will return (None,None,None) until another exception is raised\n\
+in the current thread or the execution stack returns to a frame where\n\
+another exception is being handled."
 );
 
 static PyObject *
@@ -600,7 +631,8 @@
 	{"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS, 
 	 callstats_doc},
 	{"displayhook",	sys_displayhook, METH_O, displayhook_doc},
-	{"exc_info",	(PyCFunction)sys_exc_info, METH_NOARGS, exc_info_doc},
+	{"exc_info",	sys_exc_info, METH_NOARGS, exc_info_doc},
+	{"exc_clear",	sys_exc_clear, METH_NOARGS, exc_clear_doc},
 	{"excepthook",	sys_excepthook, METH_VARARGS, excepthook_doc},
 	{"exit",	sys_exit, METH_VARARGS, exit_doc},
 #ifdef Py_USING_UNICODE
@@ -786,6 +818,7 @@
 displayhook() -- print an object to the screen, and save it in __builtin__._\n\
 excepthook() -- print an exception and its traceback to sys.stderr\n\
 exc_info() -- return thread-safe information about the current exception\n\
+exc_clear() -- clear the exception state for the current thread\n\
 exit() -- exit the interpreter by raising SystemExit\n\
 getdlopenflags() -- returns flags to be used for dlopen() calls\n\
 getrefcount() -- return the reference count for an object (plus one :-)\n\