diff --git a/Doc/c-api/exceptions.rst b/Doc/c-api/exceptions.rst index 619f0f6..a58dc63 100644 --- a/Doc/c-api/exceptions.rst +++ b/Doc/c-api/exceptions.rst @@ -302,12 +302,12 @@ in various ways. There is a separate error indicator for each thread. use. -.. cfunction:: int PyErr_WarnEx(PyObject *category, char *message, int stacklevel) +.. cfunction:: int PyErr_WarnEx(PyObject *category, char *message, int stack_level) Issue a warning message. The *category* argument is a warning category (see - below) or *NULL*; the *message* argument is a message string. *stacklevel* is a + below) or *NULL*; the *message* argument is a message string. *stack_level* is a positive number giving a number of stack frames; the warning will be issued from - the currently executing line of code in that stack frame. A *stacklevel* of 1 + the currently executing line of code in that stack frame. A *stack_level* of 1 is the function calling :cfunc:`PyErr_WarnEx`, 2 is the function above that, and so forth. @@ -348,6 +348,12 @@ in various ways. There is a separate error indicator for each thread. described there. +.. cfunction:: int PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level, const char *format, ...) + + Function similar to :cfunc:`PyErr_WarnEx`, but use + :cfunc:`PyUnicode_FromFormatV` to format the warning message. + + .. cfunction:: int PyErr_CheckSignals() .. index:: diff --git a/Include/warnings.h b/Include/warnings.h index f54eabd..c84cb9f 100644 --- a/Include/warnings.h +++ b/Include/warnings.h @@ -7,6 +7,7 @@ extern "C" { PyAPI_FUNC(PyObject*) _PyWarnings_Init(void); PyAPI_FUNC(int) PyErr_WarnEx(PyObject *, const char *, Py_ssize_t); +PyAPI_FUNC(int) PyErr_WarnFormat(PyObject *, Py_ssize_t, const char *, ...); PyAPI_FUNC(int) PyErr_WarnExplicit(PyObject *, const char *, const char *, int, const char *, PyObject *); diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 6c6eac1..7b8e1b6 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -56,10 +56,6 @@ PyModule_New(const char *name) return NULL; } -static char api_version_warning[] = -"Python C API version mismatch for module %.100s:\ - This Python has API version %d, module %.100s has version %d."; - PyObject * PyModule_Create2(struct PyModuleDef* module, int module_api_version) { @@ -79,12 +75,13 @@ PyModule_Create2(struct PyModuleDef* module, int module_api_version) } name = module->m_name; if (module_api_version != PYTHON_API_VERSION) { - char message[512]; - PyOS_snprintf(message, sizeof(message), - api_version_warning, name, - PYTHON_API_VERSION, name, - module_api_version); - if (PyErr_WarnEx(PyExc_RuntimeWarning, message, 1)) + int err; + err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, + "Python C API version mismatch for module %.100s: " + "This Python has API version %d, module %.100s has version %d.", + name, + PYTHON_API_VERSION, name, module_api_version); + if (err) return NULL; } /* Make sure name is fully qualified. diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 268a924..3ca070a 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -3892,13 +3892,11 @@ PyType_Ready(PyTypeObject *type) tp_reserved) but not tp_richcompare. */ if (type->tp_reserved && !type->tp_richcompare) { int error; - char msg[240]; - PyOS_snprintf(msg, sizeof(msg), - "Type %.100s defines tp_reserved (formerly " - "tp_compare) but not tp_richcompare. " - "Comparisons may not behave as intended.", - type->tp_name); - error = PyErr_WarnEx(PyExc_DeprecationWarning, msg, 1); + error = PyErr_WarnFormat(PyExc_DeprecationWarning, 1, + "Type %.100s defines tp_reserved (formerly " + "tp_compare) but not tp_richcompare. " + "Comparisons may not behave as intended.", + type->tp_name); if (error == -1) goto error; } diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index f2d666d..4577629 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -1556,12 +1556,13 @@ PyObject *PyUnicode_AsEncodedString(PyObject *unicode, /* If the codec returns a buffer, raise a warning and convert to bytes */ if (PyByteArray_Check(v)) { - char msg[100]; + int error; PyObject *b; - PyOS_snprintf(msg, sizeof(msg), - "encoder %s returned buffer instead of bytes", - encoding); - if (PyErr_WarnEx(PyExc_RuntimeWarning, msg, 1) < 0) { + + error = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, + "encoder %s returned buffer instead of bytes", + encoding); + if (error) { Py_DECREF(v); return NULL; } diff --git a/Python/_warnings.c b/Python/_warnings.c index dd7bb57..91a5007 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -498,23 +498,21 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, *filename = PyDict_GetItemString(globals, "__file__"); if (*filename != NULL) { Py_ssize_t len = PyUnicode_GetSize(*filename); - const char *file_str = _PyUnicode_AsString(*filename); - if (file_str == NULL || (len < 0 && PyErr_Occurred())) - goto handle_error; + Py_UNICODE *unicode = PyUnicode_AS_UNICODE(*filename); /* if filename.lower().endswith((".pyc", ".pyo")): */ if (len >= 4 && - file_str[len-4] == '.' && - tolower(file_str[len-3]) == 'p' && - tolower(file_str[len-2]) == 'y' && - (tolower(file_str[len-1]) == 'c' || - tolower(file_str[len-1]) == 'o')) + unicode[len-4] == '.' && + Py_UNICODE_TOLOWER(unicode[len-3]) == 'p' && + Py_UNICODE_TOLOWER(unicode[len-2]) == 'y' && + (Py_UNICODE_TOLOWER(unicode[len-1]) == 'c' || + Py_UNICODE_TOLOWER(unicode[len-1]) == 'o')) { - *filename = PyUnicode_FromStringAndSize(file_str, len-1); - if (*filename == NULL) - goto handle_error; - } - else + *filename = PyUnicode_FromUnicode(unicode, len-1); + if (*filename == NULL) + goto handle_error; + } + else Py_INCREF(*filename); } else { @@ -712,19 +710,17 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) /* Function to issue a warning message; may raise an exception. */ -int -PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level) + +static int +warn_unicode(PyObject *category, PyObject *message, + Py_ssize_t stack_level) { PyObject *res; - PyObject *message = PyUnicode_FromString(text); - if (message == NULL) - return -1; if (category == NULL) category = PyExc_RuntimeWarning; res = do_warn(message, category, stack_level); - Py_DECREF(message); if (res == NULL) return -1; Py_DECREF(res); @@ -732,6 +728,42 @@ PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level) return 0; } +int +PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level, + const char *format, ...) +{ + int ret; + PyObject *unicode; + va_list vargs; + +#ifdef HAVE_STDARG_PROTOTYPES + va_start(vargs, format); +#else + va_start(vargs); +#endif + unicode = PyUnicode_FromFormatV(format, vargs); + if (unicode != NULL) { + ret = warn_unicode(category, unicode, stack_level); + Py_DECREF(unicode); + } + else + ret = -1; + va_end(vargs); + return ret; +} + +int +PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level) +{ + int ret; + PyObject *message = PyUnicode_FromString(text); + if (message == NULL) + return -1; + ret = warn_unicode(category, message, stack_level); + Py_DECREF(message); + return ret; +} + /* PyErr_Warn is only for backwards compatability and will be removed. Use PyErr_WarnEx instead. */