comparison Python/sysmodule.c @ 17164:b44e77a12978 legacy-trunk

Add sys.excepthook. Update docstring and library reference section on 'sys' module. New API PyErr_Display, just for displaying errors, called by excepthook. Uncaught exceptions now call sys.excepthook; if that fails, we fall back to calling PyErr_Display directly. Also comes with sys.__excepthook__ and sys.__displayhook__.
author Ka-Ping Yee <ping@zesty.ca>
date Fri, 23 Mar 2001 02:46:52 +0000
parents d41f9b0a40ca
children beb9d2b02846
comparison
equal deleted inserted replaced
17163:ad683f20bb14 17164:b44e77a12978
105 Py_INCREF(Py_None); 105 Py_INCREF(Py_None);
106 return Py_None; 106 return Py_None;
107 } 107 }
108 108
109 static char displayhook_doc[] = 109 static char displayhook_doc[] =
110 "displayhook(o) -> None\n" 110 "displayhook(object) -> None\n"
111 "\n" 111 "\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";
113 129
114 static PyObject * 130 static PyObject *
115 sys_exc_info(PyObject *self, PyObject *args) 131 sys_exc_info(PyObject *self, PyObject *args)
116 { 132 {
117 PyThreadState *tstate; 133 PyThreadState *tstate;
376 392
377 static PyMethodDef sys_methods[] = { 393 static PyMethodDef sys_methods[] = {
378 /* Might as well keep this in alphabetic order */ 394 /* Might as well keep this in alphabetic order */
379 {"displayhook", sys_displayhook, 1, displayhook_doc}, 395 {"displayhook", sys_displayhook, 1, displayhook_doc},
380 {"exc_info", sys_exc_info, 1, exc_info_doc}, 396 {"exc_info", sys_exc_info, 1, exc_info_doc},
397 {"excepthook", sys_excepthook, 1, excepthook_doc},
381 {"exit", sys_exit, 0, exit_doc}, 398 {"exit", sys_exit, 0, exit_doc},
382 {"getdefaultencoding", sys_getdefaultencoding, 1, 399 {"getdefaultencoding", sys_getdefaultencoding, 1,
383 getdefaultencoding_doc}, 400 getdefaultencoding_doc},
384 #ifdef COUNT_ALLOCS 401 #ifdef COUNT_ALLOCS
385 {"getcounts", sys_getcounts, 1}, 402 {"getcounts", sys_getcounts, 1},
475 Dynamic objects:\n\ 492 Dynamic objects:\n\
476 \n\ 493 \n\
477 argv -- command line arguments; argv[0] is the script pathname if known\n\ 494 argv -- command line arguments; argv[0] is the script pathname if known\n\
478 path -- module search path; path[0] is the script directory, else ''\n\ 495 path -- module search path; path[0] is the script directory, else ''\n\
479 modules -- dictionary of loaded modules\n\ 496 modules -- 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\
481 \n\ 505 \n\
482 stdin -- standard input file object; used by raw_input() and input()\n\ 506 stdin -- standard input file object; used by raw_input() and input()\n\
483 stdout -- standard output file object; used by the print statement\n\ 507 stdout -- standard output file object; used by the print statement\n\
484 stderr -- standard error object; used for error messages\n\ 508 stderr -- standard error object; used for error messages\n\
485 By assigning another file object (or an object that behaves like a file)\n\ 509 By assigning other file objects (or objects that behave like files)\n\
486 to one of these, it is possible to redirect all of the interpreter's I/O.\n\ 510 to these, it is possible to redirect all of the interpreter's I/O.\n\
487 \n\ 511 \n\
488 last_type -- type of last uncaught exception\n\ 512 last_type -- type of last uncaught exception\n\
489 last_value -- value of last uncaught exception\n\ 513 last_value -- value of last uncaught exception\n\
490 last_traceback -- traceback of last uncaught exception\n\ 514 last_traceback -- traceback of last uncaught exception\n\
491 These three are only available in an interactive session after a\n\ 515 These three are only available in an interactive session after a\n\
496 exc_traceback -- traceback of exception currently being handled\n\ 520 exc_traceback -- traceback of exception currently being handled\n\
497 The function exc_info() should be used instead of these three,\n\ 521 The function exc_info() should be used instead of these three,\n\
498 because it is thread-safe.\n\ 522 because it is thread-safe.\n\
499 " 523 "
500 #ifndef MS_WIN16 524 #ifndef MS_WIN16
501 /* Concatenating string here */ 525 /* concatenating string here */
502 "\n\ 526 "\n\
503 Static objects:\n\ 527 Static objects:\n\
504 \n\ 528 \n\
505 maxint -- the largest supported integer (the smallest is -maxint-1)\n\ 529 maxint -- the largest supported integer (the smallest is -maxint-1)\n\
506 builtin_module_names -- tuple of module names built into this intepreter\n\ 530 builtin_module_names -- tuple of module names built into this intepreter\n\
510 copyright -- copyright notice pertaining to this interpreter\n\ 534 copyright -- copyright notice pertaining to this interpreter\n\
511 platform -- platform identifier\n\ 535 platform -- platform identifier\n\
512 executable -- pathname of this Python interpreter\n\ 536 executable -- pathname of this Python interpreter\n\
513 prefix -- prefix used to find the Python library\n\ 537 prefix -- prefix used to find the Python library\n\
514 exec_prefix -- prefix used to find the machine-specific Python library\n\ 538 exec_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\
516 winver -- [Windows only] version number of the Python DLL\n\ 543 winver -- [Windows only] version number of the Python DLL\n\
517 __stdin__ -- the original stdin; don't use!\n\ 544 "
518 __stdout__ -- the original stdout; don't use!\n\ 545 #endif /* MS_WINDOWS */
519 __stderr__ -- the original stderr; don't use!\n\ 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\
520 \n\ 551 \n\
521 Functions:\n\ 552 Functions:\n\
522 \n\ 553 \n\
523 displayhook() -- print an object to the screen, and save it in __builtin__._\n\ 554 displayhook() -- print an object to the screen, and save it in __builtin__._\n\
555 excepthook() -- print an exception and its traceback to sys.stderr\n\
524 exc_info() -- return thread-safe information about the current exception\n\ 556 exc_info() -- return thread-safe information about the current exception\n\
525 exit() -- exit the interpreter by raising SystemExit\n\ 557 exit() -- exit the interpreter by raising SystemExit\n\
526 getrefcount() -- return the reference count for an object (plus one :-)\n\ 558 getrefcount() -- return the reference count for an object (plus one :-)\n\
527 getrecursionlimit() -- return the max recursion depth for the interpreter\n\ 559 getrecursionlimit() -- return the max recursion depth for the interpreter\n\
528 setcheckinterval() -- control how often the interpreter checks for events\n\ 560 setcheckinterval() -- control how often the interpreter checks for events\n\
529 setprofile() -- set the global profiling function\n\ 561 setprofile() -- set the global profiling function\n\
530 setrecursionlimit() -- set the max recursion depth for the interpreter\n\ 562 setrecursionlimit() -- set the max recursion depth for the interpreter\n\
531 settrace() -- set the global debug tracing function\n\ 563 settrace() -- set the global debug tracing function\n\
532 " 564 "
533 #endif 565 #endif /* MS_WIN16 */
534 /* end of sys_doc */ ; 566 /* end of sys_doc */ ;
535 567
536 PyObject * 568 PyObject *
537 _PySys_Init(void) 569 _PySys_Init(void)
538 { 570 {
553 PyDict_SetItemString(sysdict, "stderr", syserr); 585 PyDict_SetItemString(sysdict, "stderr", syserr);
554 /* Make backup copies for cleanup */ 586 /* Make backup copies for cleanup */
555 PyDict_SetItemString(sysdict, "__stdin__", sysin); 587 PyDict_SetItemString(sysdict, "__stdin__", sysin);
556 PyDict_SetItemString(sysdict, "__stdout__", sysout); 588 PyDict_SetItemString(sysdict, "__stdout__", sysout);
557 PyDict_SetItemString(sysdict, "__stderr__", syserr); 589 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"));
558 Py_XDECREF(sysin); 594 Py_XDECREF(sysin);
559 Py_XDECREF(sysout); 595 Py_XDECREF(sysout);
560 Py_XDECREF(syserr); 596 Py_XDECREF(syserr);
561 PyDict_SetItemString(sysdict, "version", 597 PyDict_SetItemString(sysdict, "version",
562 v = PyString_FromString(Py_GetVersion())); 598 v = PyString_FromString(Py_GetVersion()));