Skip to content
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

codeop: inconsistent handling of invalid escapes #96052

Closed
kevinushey opened this issue Aug 17, 2022 · 6 comments
Closed

codeop: inconsistent handling of invalid escapes #96052

kevinushey opened this issue Aug 17, 2022 · 6 comments
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@kevinushey
Copy link

Bug report

It seems like the codeop module is inconsistent with regards to handling of invalid escapes. For example, '\(' is accepted at the top level, but apparently not within indented blocks.

Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import codeop
>>> codeop.compile_command("'\('")
<code object <module> at 0x00000244A5BE6290, file "<input>", line 1>
>>> codeop.compile_command("while False:\n\t'\('")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\kevin\AppData\Local\Programs\Python\Python310\lib\codeop.py", line 107, in compile_command
    return _maybe_compile(_compile, source, filename, symbol)
  File "C:\Users\kevin\AppData\Local\Programs\Python\Python310\lib\codeop.py", line 70, in _maybe_compile
    compiler(source + "\n", filename, symbol)
  File "C:\Users\kevin\AppData\Local\Programs\Python\Python310\lib\codeop.py", line 86, in _compile
    return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT)
  File "<input>", line 2
    '\('
    ^^^^
SyntaxError: invalid escape sequence '\('

This can also be seen when submitting code via IDLE; e.g.

Screenshot 2022-08-17 100851

However, such code is accepted by a "plain" Python terminal:

Screenshot 2022-08-17 102531

Given the discrepancy, it seems like invalid escapes should perhaps be accepted by codeop here?

Your environment

Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32

@kevinushey kevinushey added the type-bug An unexpected behavior, bug, or error label Aug 17, 2022
@serhiy-storchaka
Copy link
Member

It is because the codeop turns SyntaxWarning into SyntaxError when call compile() second time.

When run Python with the -Wa option you see warnings:

>>> import codeop
>>> codeop.compile_command("while False:\n\t'\('")
<stdin>:1: DeprecationWarning: invalid escape sequence '\('
<input>:2: DeprecationWarning: invalid escape sequence '\('
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.10/codeop.py", line 107, in compile_command
    return _maybe_compile(_compile, source, filename, symbol)
  File "/usr/lib/python3.10/codeop.py", line 70, in _maybe_compile
    compiler(source + "\n", filename, symbol)
  File "/usr/lib/python3.10/codeop.py", line 86, in _compile
    return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT)
  File "<input>", line 2
    '\('
    ^^^^
SyntaxError: invalid escape sequence '\('

The first warning (starting with ":1:") is emitted by the REPL. The second one (starting with ":2:"). And finally you get an error when compile() is called after warnings.simplefilter("error"). Without that filter you would get a doubled warning.

Duplicated warnings were silenced in bpo-40807, and turned into errors in bpo-41520. @vstinner

@serhiy-storchaka serhiy-storchaka self-assigned this Aug 18, 2022
@vstinner
Copy link
Member

I'm no longer sure if codeop should display SyntaxError or raise an exception by default (see issue #85692).

But I dislike the fact that warnings depends if the first or the second compile() function succeeded. We can to treat warnings the same way for all compile() calls.

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 20, 2022
…input

Previously codeop.compile_command() emitted compiler warnings (SyntaxWarning or
DeprecationWarning) and raised a SyntaxError for incomplete input containing
a potentially incorrect code. Now it always returns None for incomplete input
without emitting any warnings.
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Aug 20, 2022
…input

Previously codeop.compile_command() emitted compiler warnings (SyntaxWarning or
DeprecationWarning) and raised a SyntaxError for incomplete input containing
a potentially incorrect code. Now it always returns None for incomplete input
without emitting any warnings.
serhiy-storchaka added a commit that referenced this issue Sep 16, 2022
…H-96132)

Previously codeop.compile_command() emitted compiler warnings (SyntaxWarning or
DeprecationWarning) and raised a SyntaxError for incomplete input containing
a potentially incorrect code. Now it always returns None for incomplete input
without emitting any warnings.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 16, 2022
…input (pythonGH-96132)

Previously codeop.compile_command() emitted compiler warnings (SyntaxWarning or
DeprecationWarning) and raised a SyntaxError for incomplete input containing
a potentially incorrect code. Now it always returns None for incomplete input
without emitting any warnings.
(cherry picked from commit 426d72e)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 16, 2022
…input (pythonGH-96132)

Previously codeop.compile_command() emitted compiler warnings (SyntaxWarning or
DeprecationWarning) and raised a SyntaxError for incomplete input containing
a potentially incorrect code. Now it always returns None for incomplete input
without emitting any warnings.
(cherry picked from commit 426d72e)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
miss-islington added a commit that referenced this issue Sep 25, 2022
…H-96132)

Previously codeop.compile_command() emitted compiler warnings (SyntaxWarning or
DeprecationWarning) and raised a SyntaxError for incomplete input containing
a potentially incorrect code. Now it always returns None for incomplete input
without emitting any warnings.
(cherry picked from commit 426d72e)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
miss-islington added a commit that referenced this issue Sep 25, 2022
…H-96132)

Previously codeop.compile_command() emitted compiler warnings (SyntaxWarning or
DeprecationWarning) and raised a SyntaxError for incomplete input containing
a potentially incorrect code. Now it always returns None for incomplete input
without emitting any warnings.
(cherry picked from commit 426d72e)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@vstinner
Copy link
Member

@serhiy-storchaka: Is this issue fixed or not? It's still open.

Issue #97562 can be closed as a duplicate if I understood correctly.

@serhiy-storchaka
Copy link
Member

Yes, this issue has been fixed.

@kevinushey
Copy link
Author

Should I see the fix in 3.11.0rc2 / 3.10.7?

Screen Shot 2022-09-26 at 9 41 43 AM

@serhiy-storchaka
Copy link
Member

No, this change was not included in 3.11rc2 and 3.10.7. You have to wait the next bugfix releases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants