@@ -132,7 +132,7 @@ PyDoc_STRVAR(excepthook_doc,
132132);
133133
134134static PyObject *
135- sys_exc_info (PyObject * self )
135+ sys_exc_info (PyObject * self , PyObject * noargs )
136136{
137137 PyThreadState * tstate ;
138138 tstate = PyThreadState_Get ();
@@ -147,8 +147,39 @@ sys_exc_info(PyObject *self)
147147PyDoc_STRVAR (exc_info_doc ,
148148"exc_info() -> (type, value, traceback)\n\
149149\n\
150- Return information about the exception that is currently being handled.\n\
151- This should be called from inside an except clause only."
150+ Return information about the most recent exception caught by an except\n\
151+ clause in the current stack frame or in an older stack frame."
152+ );
153+
154+ static PyObject *
155+ sys_exc_clear (PyObject * self , PyObject * noargs )
156+ {
157+ PyThreadState * tstate = PyThreadState_Get ();
158+ PyObject * tmp_type , * tmp_value , * tmp_tb ;
159+ tmp_type = tstate -> exc_type ;
160+ tmp_value = tstate -> exc_value ;
161+ tmp_tb = tstate -> exc_traceback ;
162+ tstate -> exc_type = NULL ;
163+ tstate -> exc_value = NULL ;
164+ tstate -> exc_traceback = NULL ;
165+ Py_XDECREF (tmp_type );
166+ Py_XDECREF (tmp_value );
167+ Py_XDECREF (tmp_tb );
168+ /* For b/w compatibility */
169+ PySys_SetObject ("exc_type" , Py_None );
170+ PySys_SetObject ("exc_value" , Py_None );
171+ PySys_SetObject ("exc_traceback" , Py_None );
172+ Py_INCREF (Py_None );
173+ return Py_None ;
174+ }
175+
176+ PyDoc_STRVAR (exc_clear_doc ,
177+ "exc_clear() -> None\n\
178+ \n\
179+ Clear global information on the current exception. Subsequent calls to\n\
180+ exc_info() will return (None,None,None) until another exception is raised\n\
181+ in the current thread or the execution stack returns to a frame where\n\
182+ another exception is being handled."
152183);
153184
154185static PyObject *
@@ -600,7 +631,8 @@ static PyMethodDef sys_methods[] = {
600631 {"callstats" , (PyCFunction )PyEval_GetCallStats , METH_NOARGS ,
601632 callstats_doc },
602633 {"displayhook" , sys_displayhook , METH_O , displayhook_doc },
603- {"exc_info" , (PyCFunction )sys_exc_info , METH_NOARGS , exc_info_doc },
634+ {"exc_info" , sys_exc_info , METH_NOARGS , exc_info_doc },
635+ {"exc_clear" , sys_exc_clear , METH_NOARGS , exc_clear_doc },
604636 {"excepthook" , sys_excepthook , METH_VARARGS , excepthook_doc },
605637 {"exit" , sys_exit , METH_VARARGS , exit_doc },
606638#ifdef Py_USING_UNICODE
@@ -786,6 +818,7 @@ Functions:\n\
786818displayhook() -- print an object to the screen, and save it in __builtin__._\n\
787819excepthook() -- print an exception and its traceback to sys.stderr\n\
788820exc_info() -- return thread-safe information about the current exception\n\
821+ exc_clear() -- clear the exception state for the current thread\n\
789822exit() -- exit the interpreter by raising SystemExit\n\
790823getdlopenflags() -- returns flags to be used for dlopen() calls\n\
791824getrefcount() -- return the reference count for an object (plus one :-)\n\
0 commit comments