Kaydet (Commit) 2e5f1178 authored tarafından Victor Stinner's avatar Victor Stinner

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.
üst eb6f3ead
...@@ -498,23 +498,21 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, ...@@ -498,23 +498,21 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
*filename = PyDict_GetItemString(globals, "__file__"); *filename = PyDict_GetItemString(globals, "__file__");
if (*filename != NULL) { if (*filename != NULL) {
Py_ssize_t len = PyUnicode_GetSize(*filename); Py_ssize_t len = PyUnicode_GetSize(*filename);
const char *file_str = _PyUnicode_AsString(*filename); Py_UNICODE *unicode = PyUnicode_AS_UNICODE(*filename);
if (file_str == NULL || (len < 0 && PyErr_Occurred()))
goto handle_error;
/* if filename.lower().endswith((".pyc", ".pyo")): */ /* if filename.lower().endswith((".pyc", ".pyo")): */
if (len >= 4 && if (len >= 4 &&
file_str[len-4] == '.' && unicode[len-4] == '.' &&
tolower(file_str[len-3]) == 'p' && Py_UNICODE_TOLOWER(unicode[len-3]) == 'p' &&
tolower(file_str[len-2]) == 'y' && Py_UNICODE_TOLOWER(unicode[len-2]) == 'y' &&
(tolower(file_str[len-1]) == 'c' || (Py_UNICODE_TOLOWER(unicode[len-1]) == 'c' ||
tolower(file_str[len-1]) == 'o')) Py_UNICODE_TOLOWER(unicode[len-1]) == 'o'))
{ {
*filename = PyUnicode_FromStringAndSize(file_str, len-1); *filename = PyUnicode_FromUnicode(unicode, len-1);
if (*filename == NULL) if (*filename == NULL)
goto handle_error; goto handle_error;
} }
else else
Py_INCREF(*filename); Py_INCREF(*filename);
} }
else { else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment