From 471239a1458855de653c1986ab17453b509944c9 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Sun, 17 Jul 2011 19:17:55 -0700 Subject: Make warnings accept a callable for showwarnings instead of restricting itself to just functions and methods (which allows built-in functions to be used, etc.). Closes issue #10271. Thanks to lekma for the bug report. --- Python/_warnings.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index 615a2d3217..f8a7175f92 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -409,10 +409,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; } -- cgit v1.2.1 From d1d013c01c268d869597b35cbcd8b5d7c5baf2ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Wed, 28 Sep 2011 07:41:54 +0200 Subject: Implement PEP 393. --- Python/_warnings.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index f8a7175f92..2bcca91fa6 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -498,17 +498,19 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, *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); + int kind = PyUnicode_KIND(*filename); + void *data = PyUnicode_DATA(*filename); /* 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) == '.' && + Py_UNICODE_TOLOWER(PyUnicode_READ(kind, data, len-3)) == 'p' && + Py_UNICODE_TOLOWER(PyUnicode_READ(kind, data, len-2)) == 'y' && + (Py_UNICODE_TOLOWER(PyUnicode_READ(kind, data, len-1)) == 'c' || + Py_UNICODE_TOLOWER(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; } -- cgit v1.2.1 From 9146424d4d60bd9f2db88362ca1c3b069f966730 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 6 Oct 2011 02:34:51 +0200 Subject: Fix _warnings.c: make the filename string ready --- Python/_warnings.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index 2bcca91fa6..792e3ed6df 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -497,9 +497,16 @@ 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); - int kind = PyUnicode_KIND(*filename); - void *data = PyUnicode_DATA(*filename); + Py_ssize_t len; + int kind; + void *data; + + if (PyUnicode_READY(*filename)) + goto handle_error; + + len = PyUnicode_GetSize(*filename); + kind = PyUnicode_KIND(*filename); + data = PyUnicode_DATA(*filename); /* if filename.lower().endswith((".pyc", ".pyo")): */ if (len >= 4 && -- cgit v1.2.1 From dcacb24f386ae1b107462181af42b6826ec15fb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Sun, 9 Oct 2011 10:38:36 +0200 Subject: =?UTF-8?q?Add=20API=20for=20static=20strings,=20primarily=20good?= =?UTF-8?q?=20for=20identifiers.=20Thanks=20to=20Konrad=20Sch=C3=B6bel=20a?= =?UTF-8?q?nd=20Jasper=20Schulz=20for=20helping=20with=20the=20mass-editin?= =?UTF-8?q?g.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Python/_warnings.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index 792e3ed6df..e5631079a9 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; -- cgit v1.2.1 From c3eb93fe1ce5f5ff2666fb9f34a6fd7c46279554 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Mon, 10 Oct 2011 18:11:30 +0200 Subject: Use identifier API for PyObject_GetAttrString. --- Python/_warnings.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index e5631079a9..ebc9cb808c 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -247,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; -- cgit v1.2.1 From f5ad3b280b43227eb0e3fa63d89490a58ba9c28b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Fri, 14 Oct 2011 10:20:37 +0200 Subject: Rename _Py_identifier to _Py_IDENTIFIER. --- Python/_warnings.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index ebc9cb808c..6fcc128fb6 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -18,7 +18,7 @@ static int check_matched(PyObject *obj, PyObject *arg) { PyObject *result; - _Py_identifier(match); + _Py_IDENTIFIER(match); int rc; if (obj == Py_None) @@ -247,7 +247,7 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject PyObject *f_stderr; PyObject *name; char lineno_str[128]; - _Py_identifier(__name__); + _Py_IDENTIFIER(__name__); PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno); -- cgit v1.2.1 From 406e4d8f1d3d003e8754d9e65607a697e7f5c865 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Fri, 14 Oct 2011 15:16:45 +0200 Subject: Port SetAttrString/HasAttrString to SetAttrId/GetAttrId. --- Python/_warnings.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index 6fcc128fb6..5846426531 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -654,8 +654,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; @@ -663,16 +664,12 @@ 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; + Py_DECREF(tmp); + if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL) + return NULL; + Py_DECREF(tmp); /* Check/get the requisite pieces needed for the loader. */ loader = PyDict_GetItemString(module_globals, "__loader__"); @@ -682,11 +679,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) { @@ -695,8 +692,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; -- cgit v1.2.1 From 58e1626c76b718d8b7ecd5f847c7102e79a43302 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Mon, 7 Nov 2011 13:00:05 +0100 Subject: Make _PyUnicode_FromId return borrowed references. http://mail.python.org/pipermail/python-dev/2011-November/114347.html --- Python/_warnings.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index 5846426531..2e5b0dd4e2 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -666,10 +666,8 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) if ((tmp = _PyUnicode_FromId(&PyId_get_source)) == NULL) return NULL; - Py_DECREF(tmp); if ((tmp = _PyUnicode_FromId(&PyId_splitlines)) == NULL) return NULL; - Py_DECREF(tmp); /* Check/get the requisite pieces needed for the loader. */ loader = PyDict_GetItemString(module_globals, "__loader__"); -- cgit v1.2.1 From ca8cc12c914866d056515be4f028011fd623994c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 21 Nov 2011 02:49:52 +0100 Subject: Fix misuse of PyUnicode_GET_SIZE() => PyUnicode_GET_LENGTH() And PyUnicode_GetSize() => PyUnicode_GetLength() --- Python/_warnings.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'Python/_warnings.c') diff --git a/Python/_warnings.c b/Python/_warnings.c index 2e5b0dd4e2..a494dd9a3c 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -203,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; @@ -506,7 +506,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, if (PyUnicode_READY(*filename)) goto handle_error; - len = PyUnicode_GetSize(*filename); + len = PyUnicode_GetLength(*filename); kind = PyUnicode_KIND(*filename); data = PyUnicode_DATA(*filename); @@ -690,7 +690,7 @@ warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) } /* Split the source into lines. */ - source_list = PyObject_CallMethodObjArgs(source, + source_list = PyObject_CallMethodObjArgs(source, PyId_splitlines.object, NULL); Py_DECREF(source); -- cgit v1.2.1