summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2003-03-01 03:20:41 (GMT)
committerGuido van Rossum <guido@python.org>2003-03-01 03:20:41 (GMT)
commit46d3dc37e4a01e8d72b00ab9ad4e4a07022b7b64 (patch)
treed2c8e67f458cd38115f6865be8034150d9e7242c /Python
parentd1a283be269c66eb813948febc3ce6c9405fb64f (diff)
downloadcpython-46d3dc37e4a01e8d72b00ab9ad4e4a07022b7b64.zip
cpython-46d3dc37e4a01e8d72b00ab9ad4e4a07022b7b64.tar.gz
cpython-46d3dc37e4a01e8d72b00ab9ad4e4a07022b7b64.tar.bz2
- 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!
Diffstat (limited to 'Python')
-rw-r--r--Python/sysmodule.c41
1 files changed, 37 insertions, 4 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 1f51f98..13b86f2 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -132,7 +132,7 @@ PyDoc_STRVAR(excepthook_doc,
);
static PyObject *
-sys_exc_info(PyObject *self)
+sys_exc_info(PyObject *self, PyObject *noargs)
{
PyThreadState *tstate;
tstate = PyThreadState_Get();
@@ -147,8 +147,39 @@ sys_exc_info(PyObject *self)
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 @@ static PyMethodDef sys_methods[] = {
{"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 @@ Functions:\n\
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\