Skip to content

Internal error when compile RE with conditional expression referring to not defined group #91700

Closed
@serhiy-storchaka

Description

@serhiy-storchaka

When compile a regular expression with conditional expression which refers to not defined group (i.e. refers by group number which larger than the number of captured groups) you get an internal error:

>>> re.compile('()(?(2)a)')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/serhiy/py/cpython/Lib/re/__init__.py", line 225, in compile
    return _compile(pattern, flags)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/re/__init__.py", line 273, in _compile
    p = _compiler.compile(pattern, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/re/_compiler.py", line 836, in compile
    return _sre.compile(
           ^^^^^^^^^^^^^
RuntimeError: invalid SRE code

Referring to groups which are defined after the conditional expression is allowed. There are special tests for this. It means that we cannot just compare the reference number with the number of groups defined before it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10only security fixes3.11only security fixes3.9only security fixesstdlibPython modules in the Lib dirtopic-regextype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions