summaryrefslogtreecommitdiff
path: root/Python
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2010-08-08 22:12:45 +0000
committerVictor Stinner <victor.stinner@haypocalc.com>2010-08-08 22:12:45 +0000
commit9cf5be7cb4a473c73ca651db5ebee9dda4cad25a (patch)
tree75cad0cb44b34ccf873427044fb39ae51b06fe0a /Python
parentf445cdca00f3dec33ebcf41f25a59afa113470f3 (diff)
downloadcpython-9cf5be7cb4a473c73ca651db5ebee9dda4cad25a.tar.gz
Issue #9425: fix setup_context() for non-ascii filenames
setup_context() replaces .pyc or .pyo filename suffix by .py, but it didn't work if the filename contains a non-ascii character because the function used the wrong unit for the length (number of characters instead of the number of bytes). With this patch, it uses unicode filenames instead of bytes filenames, to fix the bug and to be fully unicode compliant.
Diffstat (limited to 'Python')
-rw-r--r--Python/_warnings.c24
1 files changed, 11 insertions, 13 deletions
diff --git a/Python/_warnings.c b/Python/_warnings.c
index dd7bb57965..6067ce3a47 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 {