comparison 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
comparison
equal deleted inserted replaced
28117:9b4eed4bb3e2 28119:869695cba2cd
130 "\n" 130 "\n"
131 "Handle an exception by displaying it with a traceback on sys.stderr.\n" 131 "Handle an exception by displaying it with a traceback on sys.stderr.\n"
132 ); 132 );
133 133
134 static PyObject * 134 static PyObject *
135 sys_exc_info(PyObject *self) 135 sys_exc_info(PyObject *self, PyObject *noargs)
136 { 136 {
137 PyThreadState *tstate; 137 PyThreadState *tstate;
138 tstate = PyThreadState_Get(); 138 tstate = PyThreadState_Get();
139 return Py_BuildValue( 139 return Py_BuildValue(
140 "(OOO)", 140 "(OOO)",
145 } 145 }
146 146
147 PyDoc_STRVAR(exc_info_doc, 147 PyDoc_STRVAR(exc_info_doc,
148 "exc_info() -> (type, value, traceback)\n\ 148 "exc_info() -> (type, value, traceback)\n\
149 \n\ 149 \n\
150 Return information about the exception that is currently being handled.\n\ 150 Return information about the most recent exception caught by an except\n\
151 This should be called from inside an except clause only." 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."
152 ); 183 );
153 184
154 static PyObject * 185 static PyObject *
155 sys_exit(PyObject *self, PyObject *args) 186 sys_exit(PyObject *self, PyObject *args)
156 { 187 {
598 static PyMethodDef sys_methods[] = { 629 static PyMethodDef sys_methods[] = {
599 /* Might as well keep this in alphabetic order */ 630 /* Might as well keep this in alphabetic order */
600 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS, 631 {"callstats", (PyCFunction)PyEval_GetCallStats, METH_NOARGS,
601 callstats_doc}, 632 callstats_doc},
602 {"displayhook", sys_displayhook, METH_O, displayhook_doc}, 633 {"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},
604 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc}, 636 {"excepthook", sys_excepthook, METH_VARARGS, excepthook_doc},
605 {"exit", sys_exit, METH_VARARGS, exit_doc}, 637 {"exit", sys_exit, METH_VARARGS, exit_doc},
606 #ifdef Py_USING_UNICODE 638 #ifdef Py_USING_UNICODE
607 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding, 639 {"getdefaultencoding", (PyCFunction)sys_getdefaultencoding,
608 METH_NOARGS, getdefaultencoding_doc}, 640 METH_NOARGS, getdefaultencoding_doc},
784 Functions:\n\ 816 Functions:\n\
785 \n\ 817 \n\
786 displayhook() -- print an object to the screen, and save it in __builtin__._\n\ 818 displayhook() -- print an object to the screen, and save it in __builtin__._\n\
787 excepthook() -- print an exception and its traceback to sys.stderr\n\ 819 excepthook() -- print an exception and its traceback to sys.stderr\n\
788 exc_info() -- return thread-safe information about the current exception\n\ 820 exc_info() -- return thread-safe information about the current exception\n\
821 exc_clear() -- clear the exception state for the current thread\n\
789 exit() -- exit the interpreter by raising SystemExit\n\ 822 exit() -- exit the interpreter by raising SystemExit\n\
790 getdlopenflags() -- returns flags to be used for dlopen() calls\n\ 823 getdlopenflags() -- returns flags to be used for dlopen() calls\n\
791 getrefcount() -- return the reference count for an object (plus one :-)\n\ 824 getrefcount() -- return the reference count for an object (plus one :-)\n\
792 getrecursionlimit() -- return the max recursion depth for the interpreter\n\ 825 getrecursionlimit() -- return the max recursion depth for the interpreter\n\
793 setcheckinterval() -- control how often the interpreter checks for events\n\ 826 setcheckinterval() -- control how often the interpreter checks for events\n\