-
-
Notifications
You must be signed in to change notification settings - Fork 29.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Py_CompileString does not respect the coding cookie with the new parser if flags are empty #89980
Comments
When executing Py_CompileString with a source string that has a coding cookie, this is not respected as with the old parser. |
Py_CompileString() in Python 3.9 and later, using the PEG parser, appears to no longer honours source encoding cookies. A reduced test case: #include "Python.h"
#include <stdio.h>
const char *src = (
"# -*- coding: Latin-1 -*-\n"
"'''\xc3'''\n");
int main(int argc, char **argv)
{
Py_Initialize();
PyObject *res = Py_CompileString(src, "some_path", Py_file_input);
if (res) {
fprintf(stderr, "Compile succeeded.\n");
return 0;
} else {
fprintf(stderr, "Compile failed.\n");
PyErr_Print();
return 1;
}
} Compiling and running the resulting binary with Python 3.8 (or earlier):
With 3.9 and PYTHONOLDPARSER=1:
With 3.9 (without the env var) or 3.10: Writing the same bytes to a file and making python3.9 or python3.10 import them works fine, as does passing the bytes to compile(): Python 3.10.0+ (heads/3.10-dirty:7bac598819, Nov 16 2021, 20:35:12) [GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> b = open('encoding_bug.py', 'rb').read()
>>> b
b"# -*- coding: Latin-1 -*-\n'''\xc3'''\n"
>>> import encoding_bug
>>> encoding_bug.__doc__
'Ã'
>>> co = compile(b, 'some_path', 'exec')
>>> co
<code object <module> at 0x7f447e1b0c90, file "some_path", line 1>
>>> co.co_consts[0]
'Ã' It's just Py_CompileString() that fails. I don't understand why, and I do believe it's a regression. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: