Skip to content

codeop: inconsistent handling of invalid escapes #96052

Closed
@kevinushey

Description

@kevinushey

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

Metadata

Metadata

Labels

type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions