This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Py_CompileString does not respect the coding cookie with the new parser if flags are empty
Type: Stage: resolved
Components: Parser Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: gregory.p.smith, lukasz.langa, lys.nikolaou, miss-islington, pablogsal, twouters
Priority: normal Keywords: patch

Created on 2021-11-16 19:39 by pablogsal, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 29582 merged pablogsal, 2021-11-16 19:42
PR 29585 merged pablogsal, 2021-11-16 22:34
PR 29586 merged pablogsal, 2021-11-16 22:37
PR 29750 merged pablogsal, 2021-11-24 14:37
PR 29758 merged miss-islington, 2021-11-24 18:30
PR 29759 merged miss-islington, 2021-11-24 18:30
Messages (8)
msg406425 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-11-16 19:39
When executing Py_CompileString with a source string that has a coding cookie, this is not respected as with the old parser.
msg406427 - (view) Author: Thomas Wouters (twouters) * (Python committer) Date: 2021-11-16 19:45
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):

    % ./encoding_bug
    Compile succeeded.

With 3.9 and PYTHONOLDPARSER=1:

    % PYTHONOLDPARSER=1 ./encoding_bug
    Compile succeeded.

With 3.9 (without the env var) or 3.10:
    % ./encoding_bug
    Compile failed.
      File "some_path", line 2
        '''�'''
             ^
    SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xc3 in position 0: unexpected end of data

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.
msg406433 - (view) Author: miss-islington (miss-islington) Date: 2021-11-16 20:31
New changeset da20d7401de97b425897d3069f71f77b039eb16f by Pablo Galindo Salgado in branch 'main':
bpo-45822: Respect PEP 263's coding cookies in the parser even if flags are not provided (GH-29582)
https://github.com/python/cpython/commit/da20d7401de97b425897d3069f71f77b039eb16f
msg406507 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-11-17 23:17
New changeset e3aa9fd77bf474bb3e8a7a1d1bd1ebf45147945a by Pablo Galindo Salgado in branch '3.10':
[3.10] bpo-45822: Respect PEP 263's coding cookies in the parser even if flags are not provided (GH-29582) (GH-29586)
https://github.com/python/cpython/commit/e3aa9fd77bf474bb3e8a7a1d1bd1ebf45147945a
msg406508 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-11-17 23:18
New changeset 0ef308a2890571c850c624fb99ac00f8951363c6 by Pablo Galindo Salgado in branch '3.9':
bpo-45822: Respect PEP 263's coding cookies in the parser even if flags are not provided (GH-29582) (GH-29585)
https://github.com/python/cpython/commit/0ef308a2890571c850c624fb99ac00f8951363c6
msg406944 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-11-24 18:30
New changeset abfc794bbf2c6a0939ddd81b6e700c46944ba87a by Pablo Galindo Salgado in branch 'main':
bpo-45822: Minor cleanups to the test_Py_CompileString test (GH-29750)
https://github.com/python/cpython/commit/abfc794bbf2c6a0939ddd81b6e700c46944ba87a
msg408275 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-12-11 00:03
New changeset e1e3f648ad6ab467356d2d14e1d208583c1a76c6 by Miss Islington (bot) in branch '3.10':
bpo-45822: Minor cleanups to the test_Py_CompileString test (GH-29750) (GH-29758)
https://github.com/python/cpython/commit/e1e3f648ad6ab467356d2d14e1d208583c1a76c6
msg408276 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-12-11 00:03
New changeset 5f622f1d5c5425ed1e992da6611edfb486a9bf7c by Miss Islington (bot) in branch '3.9':
bpo-45822: Minor cleanups to the test_Py_CompileString test (GH-29750) (GH-29759)
https://github.com/python/cpython/commit/5f622f1d5c5425ed1e992da6611edfb486a9bf7c
History
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 89980
2021-12-11 00:03:19lukasz.langasetmessages: + msg408276
2021-12-11 00:03:00lukasz.langasetmessages: + msg408275
2021-11-24 18:30:20miss-islingtonsetpull_requests: + pull_request27997
2021-11-24 18:30:14pablogsalsetmessages: + msg406944
2021-11-24 18:30:14miss-islingtonsetpull_requests: + pull_request27996
2021-11-24 14:37:42pablogsalsetpull_requests: + pull_request27987
2021-11-17 23:18:20lukasz.langasetmessages: + msg406508
2021-11-17 23:17:22lukasz.langasetnosy: + lukasz.langa
messages: + msg406507
2021-11-16 22:37:44pablogsalsetpull_requests: + pull_request27829
2021-11-16 22:36:10pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-11-16 22:34:20pablogsalsetpull_requests: + pull_request27828
2021-11-16 20:31:00miss-islingtonsetnosy: + miss-islington
messages: + msg406433
2021-11-16 19:45:26twouterssetnosy: + gregory.p.smith
messages: + msg406427
2021-11-16 19:42:26pablogsalsetkeywords: + patch
stage: patch review
pull_requests: + pull_request27825
2021-11-16 19:39:26pablogsalcreate