diff options
Diffstat (limited to 'Python/_warnings.c')
-rw-r--r-- | Python/_warnings.c | 97 |
1 files changed, 56 insertions, 41 deletions
diff --git a/Python/_warnings.c b/Python/_warnings.c index c12db44..f33e477 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -18,11 +18,12 @@ static int check_matched(PyObject *obj, PyObject *arg) { PyObject *result; + _Py_IDENTIFIER(match); int rc; if (obj == Py_None) return 1; - result = PyObject_CallMethod(obj, "match", "O", arg); + result = _PyObject_CallMethodId(obj, &PyId_match, "O", arg); if (result == NULL) return -1; @@ -202,13 +203,13 @@ normalize_module(PyObject *filename) mod_str = _PyUnicode_AsString(filename); if (mod_str == NULL) - return NULL; - len = PyUnicode_GetSize(filename); + return NULL; + len = PyUnicode_GetLength(filename); if (len < 0) return NULL; if (len >= 3 && strncmp(mod_str + (len - 3), ".py", 3) == 0) { - module = PyUnicode_FromStringAndSize(mod_str, len-3); + module = PyUnicode_Substring(filename, 0, len-3); } else { module = filename; @@ -246,10 +247,11 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject PyObject *f_stderr; PyObject *name; char lineno_str[128]; + _Py_IDENTIFIER(__name__); PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno); - name = PyObject_GetAttrString(category, "__name__"); + name = _PyObject_GetAttrId(category, &PyId___name__); if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */ return; @@ -409,10 +411,10 @@ warn_explicit(PyObject *category, PyObject *message, else { PyObject *res; - if (!PyMethod_Check(show_fxn) && !PyFunction_Check(show_fxn)) { + if (!PyCallable_Check(show_fxn)) { PyErr_SetString(PyExc_TypeError, "warnings.showwarning() must be set to a " - "function or method"); + "callable"); Py_DECREF(show_fxn); goto cleanup; } @@ -497,18 +499,28 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, /* Setup filename. */ *filename = PyDict_GetItemString(globals, "__file__"); if (*filename != NULL && PyUnicode_Check(*filename)) { - Py_ssize_t len = PyUnicode_GetSize(*filename); - Py_UNICODE *unicode = PyUnicode_AS_UNICODE(*filename); + Py_ssize_t len; + int kind; + void *data; + + if (PyUnicode_READY(*filename)) + goto handle_error; + + len = PyUnicode_GetLength(*filename); + kind = PyUnicode_KIND(*filename); + data = PyUnicode_DATA(*filename); +#define ascii_lower(c) ((c <= 127) ? Py_TOLOWER(c) : 0) /* if filename.lower().endswith((".pyc", ".pyo")): */ if (len >= 4 && - 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')) + PyUnicode_READ(kind, data, len-4) == '.' && + ascii_lower(PyUnicode_READ(kind, data, len-3)) == 'p' && + ascii_lower(PyUnicode_READ(kind, data, len-2)) == 'y' && + (ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'c' || + ascii_lower(PyUnicode_READ(kind, data, len-1)) == 'o')) { - *filename = PyUnicode_FromUnicode(unicode, len-1); + *filename = PyUnicode_Substring(*filename, 0, + PyUnicode_GET_LENGTH(*filename)-1); if (*filename == NULL) goto handle_error; } @@ -643,8 +655,9 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) return NULL; if (module_globals) { - static PyObject *get_source_name = NULL; - static PyObject *splitlines_name = NULL; + _Py_IDENTIFIER(get_source); + _Py_IDENTIFIER(splitlines); + PyObject *tmp; PyObject *loader; PyObject *module_name; PyObject *source; @@ -652,16 +665,10 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) PyObject *source_line; PyObject *returned; - if (get_source_name == NULL) { - get_source_name = PyUnicode_InternFromString("get_source"); - if (!get_source_name) - return NULL; - } - if (splitlines_name == NULL) { - splitlines_name = PyUnicode_InternFromString("splitlines"); - if (!splitlines_name) - return NULL; - } + if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL) + return NULL; + if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL) + return NULL; /* Check/get the requisite pieces needed for the loader. */ loader = PyDict_GetItemString(module_globals, "__loader__"); @@ -671,11 +678,11 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) goto standard_call; /* Make sure the loader implements the optional get_source() method. */ - if (!PyObject_HasAttrString(loader, "get_source")) + if (!_PyObject_HasAttrId(loader, &PyId_get_source)) goto standard_call; /* Call get_source() to get the source code. */ - source = PyObject_CallMethodObjArgs(loader, get_source_name, - module_name, NULL); + source = PyObject_CallMethodObjArgs(loader, PyId_get_source.object, + module_name, NULL); if (!source) return NULL; else if (source == Py_None) { @@ -684,8 +691,9 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) } /* Split the source into lines. */ - source_list = PyObject_CallMethodObjArgs(source, splitlines_name, - NULL); + source_list = PyObject_CallMethodObjArgs(source, + PyId_splitlines.object, + NULL); Py_DECREF(source); if (!source_list) return NULL; @@ -950,23 +958,30 @@ _PyWarnings_Init(void) if (m == NULL) return NULL; - _filters = init_filters(); - if (_filters == NULL) - return NULL; + if (_filters == NULL) { + _filters = init_filters(); + if (_filters == NULL) + return NULL; + } Py_INCREF(_filters); if (PyModule_AddObject(m, "filters", _filters) < 0) return NULL; - _once_registry = PyDict_New(); - if (_once_registry == NULL) - return NULL; + if (_once_registry == NULL) { + _once_registry = PyDict_New(); + if (_once_registry == NULL) + return NULL; + } Py_INCREF(_once_registry); if (PyModule_AddObject(m, "_onceregistry", _once_registry) < 0) return NULL; - _default_action = PyUnicode_FromString("default"); - if (_default_action == NULL) - return NULL; + if (_default_action == NULL) { + _default_action = PyUnicode_FromString("default"); + if (_default_action == NULL) + return NULL; + } + Py_INCREF(_default_action); if (PyModule_AddObject(m, "_defaultaction", _default_action) < 0) return NULL; return m; |