@@ -107,9 +107,25 @@ sys_displayhook(PyObject *self, PyObject *args)
107107}
108108
109109static char displayhook_doc [] =
110- "displayhook(o ) -> None\n"
110+ "displayhook(object ) -> None\n"
111111"\n"
112- "Print o to the stdout, and save it in __builtin__._\n" ;
112+ "Print an object to sys.stdout and also save it in __builtin__._\n" ;
113+
114+ static PyObject *
115+ sys_excepthook (PyObject * self , PyObject * args )
116+ {
117+ PyObject * exc , * value , * tb ;
118+ if (!PyArg_ParseTuple (args , "OOO:excepthook" , & exc , & value , & tb ))
119+ return NULL ;
120+ PyErr_Display (exc , value , tb );
121+ Py_INCREF (Py_None );
122+ return Py_None ;
123+ }
124+
125+ static char excepthook_doc [] =
126+ "excepthook(exctype, value, traceback) -> None\n"
127+ "\n"
128+ "Handle an exception by displaying it with a traceback on sys.stderr.\n" ;
113129
114130static PyObject *
115131sys_exc_info (PyObject * self , PyObject * args )
@@ -378,6 +394,7 @@ static PyMethodDef sys_methods[] = {
378394 /* Might as well keep this in alphabetic order */
379395 {"displayhook" , sys_displayhook , 1 , displayhook_doc },
380396 {"exc_info" , sys_exc_info , 1 , exc_info_doc },
397+ {"excepthook" , sys_excepthook , 1 , excepthook_doc },
381398 {"exit" , sys_exit , 0 , exit_doc },
382399 {"getdefaultencoding" , sys_getdefaultencoding , 1 ,
383400 getdefaultencoding_doc },
@@ -477,13 +494,20 @@ Dynamic objects:\n\
477494argv -- command line arguments; argv[0] is the script pathname if known\n\
478495path -- module search path; path[0] is the script directory, else ''\n\
479496modules -- dictionary of loaded modules\n\
480- exitfunc -- you may set this to a function to be called when Python exits\n\
497+ \n\
498+ displayhook -- called to show results in an interactive session\n\
499+ excepthook -- called to handle any uncaught exception other than SystemExit\n\
500+ To customize printing in an interactive session or to install a custom\n\
501+ top-level exception handler, assign other functions to replace these.\n\
502+ \n\
503+ exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\
504+ Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\
481505\n\
482506stdin -- standard input file object; used by raw_input() and input()\n\
483507stdout -- standard output file object; used by the print statement\n\
484508stderr -- standard error object; used for error messages\n\
485- By assigning another file object (or an object that behaves like a file )\n\
486- to one of these, it is possible to redirect all of the interpreter's I/O.\n\
509+ By assigning other file objects (or objects that behave like files )\n\
510+ to these, it is possible to redirect all of the interpreter's I/O.\n\
487511\n\
488512last_type -- type of last uncaught exception\n\
489513last_value -- value of last uncaught exception\n\
@@ -498,7 +522,7 @@ exc_traceback -- traceback of exception currently being handled\n\
498522 because it is thread-safe.\n\
499523"
500524#ifndef MS_WIN16
501- /* Concatenating string here */
525+ /* concatenating string here */
502526"\n\
503527Static objects:\n\
504528\n\
@@ -512,15 +536,23 @@ platform -- platform identifier\n\
512536executable -- pathname of this Python interpreter\n\
513537prefix -- prefix used to find the Python library\n\
514538exec_prefix -- prefix used to find the machine-specific Python library\n\
515- dllhandle -- [Windows only] integer handle of the Python DLL\n\
539+ "
540+ #ifdef MS_WINDOWS
541+ /* concatenating string here */
542+ "dllhandle -- [Windows only] integer handle of the Python DLL\n\
516543winver -- [Windows only] version number of the Python DLL\n\
517- __stdin__ -- the original stdin; don't use!\n\
518- __stdout__ -- the original stdout; don't use!\n\
519- __stderr__ -- the original stderr; don't use!\n\
544+ "
545+ #endif /* MS_WINDOWS */
546+ "__stdin__ -- the original stdin; don't touch!\n\
547+ __stdout__ -- the original stdout; don't touch!\n\
548+ __stderr__ -- the original stderr; don't touch!\n\
549+ __displayhook__ -- the original displayhook; don't touch!\n\
550+ __excepthook__ -- the original excepthook; don't touch!\n\
520551\n\
521552Functions:\n\
522553\n\
523554displayhook() -- print an object to the screen, and save it in __builtin__._\n\
555+ excepthook() -- print an exception and its traceback to sys.stderr\n\
524556exc_info() -- return thread-safe information about the current exception\n\
525557exit() -- exit the interpreter by raising SystemExit\n\
526558getrefcount() -- return the reference count for an object (plus one :-)\n\
@@ -530,7 +562,7 @@ setprofile() -- set the global profiling function\n\
530562setrecursionlimit() -- set the max recursion depth for the interpreter\n\
531563settrace() -- set the global debug tracing function\n\
532564"
533- #endif
565+ #endif /* MS_WIN16 */
534566/* end of sys_doc */ ;
535567
536568PyObject *
@@ -555,6 +587,10 @@ _PySys_Init(void)
555587 PyDict_SetItemString (sysdict , "__stdin__" , sysin );
556588 PyDict_SetItemString (sysdict , "__stdout__" , sysout );
557589 PyDict_SetItemString (sysdict , "__stderr__" , syserr );
590+ PyDict_SetItemString (sysdict , "__displayhook__" ,
591+ PyDict_GetItemString (sysdict , "displayhook" ));
592+ PyDict_SetItemString (sysdict , "__excepthook__" ,
593+ PyDict_GetItemString (sysdict , "excepthook" ));
558594 Py_XDECREF (sysin );
559595 Py_XDECREF (sysout );
560596 Py_XDECREF (syserr );
0 commit comments