comparison Python/errors.c @ 46414:840064802952 legacy-trunk

Re-implement the 'warnings' module in C. This allows for usage of the 'warnings' code in places where it was previously not possible (e.g., the parser). It could also potentially lead to a speed-up in interpreter start-up if the C version of the code (_warnings) is imported over the use of the Python version in key places. Closes issue #1631171.
author Brett Cannon <bcannon@gmail.com>
date Sat, 12 Apr 2008 23:44:07 +0000
parents 0cbe94bea2c7
children 4fe192d48960
comparison
equal deleted inserted replaced
46413:2a9858255de1 46414:840064802952
639 Py_XDECREF(tb); 639 Py_XDECREF(tb);
640 } 640 }
641 641
642 extern PyObject *PyModule_GetWarningsModule(void); 642 extern PyObject *PyModule_GetWarningsModule(void);
643 643
644 /* Function to issue a warning message; may raise an exception. */
645 int
646 PyErr_WarnEx(PyObject *category, const char *message, Py_ssize_t stack_level)
647 {
648 PyObject *dict, *func = NULL;
649 PyObject *warnings_module = PyModule_GetWarningsModule();
650
651 if (warnings_module != NULL) {
652 dict = PyModule_GetDict(warnings_module);
653 if (dict != NULL)
654 func = PyDict_GetItemString(dict, "warn");
655 }
656 if (func == NULL) {
657 PySys_WriteStderr("warning: %s\n", message);
658 return 0;
659 }
660 else {
661 PyObject *res;
662
663 if (category == NULL)
664 category = PyExc_RuntimeWarning;
665 res = PyObject_CallFunction(func, "sOn",
666 message, category, stack_level);
667 if (res == NULL)
668 return -1;
669 Py_DECREF(res);
670 return 0;
671 }
672 }
673
674 /* PyErr_Warn is only for backwards compatability and will be removed.
675 Use PyErr_WarnEx instead. */
676
677 #undef PyErr_Warn
678
679 PyAPI_FUNC(int)
680 PyErr_Warn(PyObject *category, char *message)
681 {
682 return PyErr_WarnEx(category, message, 1);
683 }
684
685 /* Warning with explicit origin */
686 int
687 PyErr_WarnExplicit(PyObject *category, const char *message,
688 const char *filename, int lineno,
689 const char *module, PyObject *registry)
690 {
691 PyObject *mod, *dict, *func = NULL;
692
693 mod = PyImport_ImportModuleNoBlock("warnings");
694 if (mod != NULL) {
695 dict = PyModule_GetDict(mod);
696 func = PyDict_GetItemString(dict, "warn_explicit");
697 Py_DECREF(mod);
698 }
699 if (func == NULL) {
700 PySys_WriteStderr("warning: %s\n", message);
701 return 0;
702 }
703 else {
704 PyObject *res;
705
706 if (category == NULL)
707 category = PyExc_RuntimeWarning;
708 if (registry == NULL)
709 registry = Py_None;
710 res = PyObject_CallFunction(func, "sOsizO", message, category,
711 filename, lineno, module, registry);
712 if (res == NULL)
713 return -1;
714 Py_DECREF(res);
715 return 0;
716 }
717 }
718
719 644
720 /* Set file and line information for the current exception. 645 /* Set file and line information for the current exception.
721 If the exception is not a SyntaxError, also sets additional attributes 646 If the exception is not a SyntaxError, also sets additional attributes
722 to make printing of exceptions believe it is a syntax error. */ 647 to make printing of exceptions believe it is a syntax error. */
723 648