Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e5f117

Browse files
author
Victor Stinner
committed
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.
1 parent eb6f3ea commit 2e5f117

1 file changed

Lines changed: 11 additions & 13 deletions

File tree

Python/_warnings.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -498,23 +498,21 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
498498
*filename = PyDict_GetItemString(globals, "__file__");
499499
if (*filename != NULL) {
500500
Py_ssize_t len = PyUnicode_GetSize(*filename);
501-
const char *file_str = _PyUnicode_AsString(*filename);
502-
if (file_str == NULL || (len < 0 && PyErr_Occurred()))
503-
goto handle_error;
501+
Py_UNICODE *unicode = PyUnicode_AS_UNICODE(*filename);
504502

505503
/* if filename.lower().endswith((".pyc", ".pyo")): */
506504
if (len >= 4 &&
507-
file_str[len-4] == '.' &&
508-
tolower(file_str[len-3]) == 'p' &&
509-
tolower(file_str[len-2]) == 'y' &&
510-
(tolower(file_str[len-1]) == 'c' ||
511-
tolower(file_str[len-1]) == 'o'))
505+
unicode[len-4] == '.' &&
506+
Py_UNICODE_TOLOWER(unicode[len-3]) == 'p' &&
507+
Py_UNICODE_TOLOWER(unicode[len-2]) == 'y' &&
508+
(Py_UNICODE_TOLOWER(unicode[len-1]) == 'c' ||
509+
Py_UNICODE_TOLOWER(unicode[len-1]) == 'o'))
512510
{
513-
*filename = PyUnicode_FromStringAndSize(file_str, len-1);
514-
if (*filename == NULL)
515-
goto handle_error;
516-
}
517-
else
511+
*filename = PyUnicode_FromUnicode(unicode, len-1);
512+
if (*filename == NULL)
513+
goto handle_error;
514+
}
515+
else
518516
Py_INCREF(*filename);
519517
}
520518
else {

0 commit comments

Comments
 (0)