Skip to content

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

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10 (EOL)end of life3.11only security fixes3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytopic-regextype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions