(original) (raw)

changeset: 86702:5261351e7a20 tag: tip parent: 86700:8962d1c442a6 user: Victor Stinner victor.stinner@gmail.com date: Mon Oct 28 18:40:09 2013 +0100 files: Python/_warnings.c description: warnings: avoid useless conversions from/to UTF-8 diff -r 8962d1c442a6 -r 5261351e7a20 Python/_warnings.c --- a/Python/_warnings.c Mon Oct 28 08:08:09 2013 +0100 +++ b/Python/_warnings.c Mon Oct 28 18:40:09 2013 +0100 @@ -99,7 +99,7 @@ get_default_action(void) /* The item is a borrowed reference. */ -static const char * +static PyObject* get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno, PyObject *module, PyObject **item) { @@ -152,13 +152,12 @@ get_filter(PyObject *category, PyObject return NULL; if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) - return _PyUnicode_AsString(action); + return action; } action = get_default_action(); - if (action != NULL) { - return _PyUnicode_AsString(action); - } + if (action != NULL) + return action; PyErr_SetString(PyExc_ValueError, MODULE_NAME ".defaultaction not found"); @@ -192,23 +191,26 @@ static PyObject * normalize_module(PyObject *filename) { PyObject *module; - const char *mod_str; + int kind; + void *data; Py_ssize_t len; - int rc = PyObject_IsTrue(filename); - if (rc == -1) - return NULL; - else if (rc == 0) - return PyUnicode_FromString(""); - - mod_str = _PyUnicode_AsString(filename); - if (mod_str == NULL) - return NULL; len = PyUnicode_GetLength(filename); if (len < 0) return NULL; + + if (len == 0) + return PyUnicode_FromString(""); + + kind = PyUnicode_KIND(filename); + data = PyUnicode_DATA(filename); + + /* if filename.endswith(".py"): */ if (len >= 3 && - strncmp(mod_str + (len - 3), ".py", 3) == 0) { + PyUnicode_READ(kind, data, len-3) == '.' && + PyUnicode_READ(kind, data, len-2) == 'p' && + PyUnicode_READ(kind, data, len-1) == 'y') + { module = PyUnicode_Substring(filename, 0, len-3); } else { @@ -273,19 +275,42 @@ show_warning(PyObject *filename, int lin /* Print " source_line\n" */ if (sourceline) { - char *source_line_str = _PyUnicode_AsString(sourceline); - if (source_line_str == NULL) - return; - while (*source_line_str == ' ' || *source_line_str == '\t' || - *source_line_str == '\014') - source_line_str++; + int kind; + void *data; + Py_ssize_t i, len; + Py_UCS4 ch; - PyFile_WriteString(source_line_str, f_stderr); + if (PyUnicode_READY(sourceline) < 1) + goto error; + + kind = PyUnicode_KIND(sourceline); + data = PyUnicode_DATA(sourceline); + len = PyUnicode_GET_LENGTH(sourceline); + for (i=0; i<len; i++)="" {="" +="" ch="PyUnicode_READ(kind," data,="" i);="" if="" (ch="" !=" " &&="" )="" break;="" }="" (i="" pyobject="" *truncated;="" truncated="PyUnicode_Substring(sourceline," i,="" len);="" (!truncated)="" goto="" error;="" pyfile_writeobject(sourceline,="" f_stderr,="" py_print_raw);="" py_decref(truncated);="" else="" pyfile_writestring("\n",="" f_stderr);="" _py_displaysourceline(f_stderr,="" filename,="" lineno,="" 2);="" +error:="" pyerr_clear();="" @@="" -296,7="" +321,7="" warn_explicit(pyobject="" *category,="" pyobje="" *key="NULL," *text="NULL," *result="NULL," *lineno_obj="NULL;" *item="Py_None;" -="" const="" char="" *action;="" int="" rc;="" (registry="" !pydict_check(registry)="" -354,7="" +379,7="" (action="=" null)="" cleanup;="" (strcmp(action,="" "error")="=" 0)="" (pyunicode_comparewithasciistring(action,="" pyerr_setobject(category,="" message);="" -362,13="" +387,13="" *="" store="" in="" the="" registry="" that="" we've="" been="" here,="" *except*="" when="" action="" is="" "always".="" rc="0;" "always")="" pydict_setitem(registry,="" key,="" py_true)="" <="" "ignore")="=" return_none;="" "once")="=" null="" ||="" py_none)="" -377,24="" +402,15="" _once_registry[(text,="" category)]="1" text,="" category,="" 0);="" "module")="=" registry[(text,="" 0)]="1" "default")="" *to_str="PyObject_Str(item);" *err_str="???" ;="" (to_str="" err_str="_PyUnicode_AsString(to_str);" (err_str="=" pyerr_format(pyexc_runtimeerror,="" "unrecognized="" (%s)="" warnings.filters:\n="" %s",="" action,="" err_str);="" py_xdecref(to_str);="" (%r)="" %r",="" item);="" -528,11="" +544,8="" setup_context(py_ssize_t="" stack_level,="" py="" py_incref(*filename);="" *module_str="_PyUnicode_AsString(*module);" *filename="NULL;" (module_str="=" handle_error;="" (strcmp(module_str,="" "__main__")="=" (pyunicode_comparewithasciistring(*module,="" *argv="PySys_GetObject("argv");" (argv="" pylist_size(argv)=""> 0) { int is_true; </len;>/victor.stinner@gmail.com