Mercurial > cpython-lru_cache-weakref
comparison Python/errors.c @ 39301:7dc5f3ede4cd legacy-trunk
Add PyErr_WarnEx() so C code can pass the stacklevel to warnings.warn().
This provides the proper warning for struct.pack().
PyErr_Warn() is now deprecated in favor of PyErr_WarnEx().
As mentioned by Tim Peters on python-dev.
| author | Neal Norwitz <nnorwitz@gmail.com> |
|---|---|
| date | Sun, 30 Jul 2006 06:55:48 +0000 |
| parents | 5531955993ed |
| children | 40585fb387bb |
comparison
equal
deleted
inserted
replaced
| 39300:b745b3fd66a6 | 39301:7dc5f3ede4cd |
|---|---|
| 630 | 630 |
| 631 extern PyObject *PyModule_GetWarningsModule(void); | 631 extern PyObject *PyModule_GetWarningsModule(void); |
| 632 | 632 |
| 633 /* Function to issue a warning message; may raise an exception. */ | 633 /* Function to issue a warning message; may raise an exception. */ |
| 634 int | 634 int |
| 635 PyErr_Warn(PyObject *category, char *message) | 635 PyErr_WarnEx(PyObject *category, const char *message, Py_ssize_t stack_level) |
| 636 { | 636 { |
| 637 PyObject *dict, *func = NULL; | 637 PyObject *dict, *func = NULL; |
| 638 PyObject *warnings_module = PyModule_GetWarningsModule(); | 638 PyObject *warnings_module = PyModule_GetWarningsModule(); |
| 639 | 639 |
| 640 if (warnings_module != NULL) { | 640 if (warnings_module != NULL) { |
| 648 else { | 648 else { |
| 649 PyObject *res; | 649 PyObject *res; |
| 650 | 650 |
| 651 if (category == NULL) | 651 if (category == NULL) |
| 652 category = PyExc_RuntimeWarning; | 652 category = PyExc_RuntimeWarning; |
| 653 res = PyObject_CallFunction(func, "sO", message, category); | 653 res = PyObject_CallFunction(func, "sOn", |
| 654 message, category, stack_level); | |
| 654 if (res == NULL) | 655 if (res == NULL) |
| 655 return -1; | 656 return -1; |
| 656 Py_DECREF(res); | 657 Py_DECREF(res); |
| 657 return 0; | 658 return 0; |
| 658 } | 659 } |
| 659 } | 660 } |
| 660 | 661 |
| 662 /* PyErr_Warn is only for backwards compatability and will be removed. | |
| 663 Use PyErr_WarnEx instead. */ | |
| 664 | |
| 665 #undef PyErr_Warn | |
| 666 | |
| 667 int | |
| 668 PyErr_Warn(PyObject *category, char *message) | |
| 669 { | |
| 670 return PyErr_WarnEx(category, message, 1); | |
| 671 } | |
| 661 | 672 |
| 662 /* Warning with explicit origin */ | 673 /* Warning with explicit origin */ |
| 663 int | 674 int |
| 664 PyErr_WarnExplicit(PyObject *category, const char *message, | 675 PyErr_WarnExplicit(PyObject *category, const char *message, |
| 665 const char *filename, int lineno, | 676 const char *filename, int lineno, |
