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

re module: wrong capturing groups #78475

Closed
beardypig mannequin opened this issue Jul 31, 2018 · 10 comments
Closed

re module: wrong capturing groups #78475

beardypig mannequin opened this issue Jul 31, 2018 · 10 comments
Assignees
Labels
3.7 (EOL) end of life 3.8 only security fixes topic-regex type-bug An unexpected behavior, bug, or error

Comments

@beardypig
Copy link
Mannequin

beardypig mannequin commented Jul 31, 2018

BPO 34294
Nosy @ezio-melotti, @serhiy-storchaka, @animalize, @beardypig, @miss-islington, @tirkarthi
PRs
  • bpo-34294: re module, fix wrong capturing groups in rare cases #11546
  • bpo-34294: re module, fix wrong capturing groups in rare cases #11546
  • bpo-34294: re module, fix wrong capturing groups in rare cases #11546
  • [3.7] bpo-34294: re module, fix wrong capturing groups in rare cases. (GH-11546) #11919
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2019-02-18.14:10:13.122>
    created_at = <Date 2018-07-31.13:11:05.106>
    labels = ['expert-regex', '3.8', 'type-bug', '3.7']
    title = 're module: wrong capturing groups'
    updated_at = <Date 2019-02-18.14:10:13.118>
    user = 'https://github.com/beardypig'

    bugs.python.org fields:

    activity = <Date 2019-02-18.14:10:13.118>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2019-02-18.14:10:13.122>
    closer = 'serhiy.storchaka'
    components = ['Regular Expressions']
    creation = <Date 2018-07-31.13:11:05.106>
    creator = 'beardypig'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 34294
    keywords = ['patch', 'patch', 'patch', '3.7regression']
    message_count = 10.0
    messages = ['322771', '322799', '324990', '333548', '333580', '334078', '334139', '335832', '335833', '335836']
    nosy_count = 7.0
    nosy_names = ['ezio.melotti', 'mrabarnett', 'serhiy.storchaka', 'malin', 'beardypig', 'miss-islington', 'xtreak']
    pr_nums = ['11546', '11546', '11546', '11919']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue34294'
    versions = ['Python 3.7', 'Python 3.8']

    @beardypig
    Copy link
    Mannequin Author

    beardypig mannequin commented Jul 31, 2018

    I am experiencing and issue with the following regex when using finditer.

    (?=<(?P<tag>\w+)/?>(?:(?P<text>.+?)</(?P=tag)>)?)", "<test><foo2/></test>
    

    (I know it's not the best method of dealing with HTML, and this is a simplified version)

    For example:

    [m.groupdict() for m in re.finditer(r"(?=<(?P<tag>\w+)/?>(?:(?P<text>.+?)</(?P=tag)>)?)", "<test><foo2/></test>")]
    

    In Python 2.7, 3.5, and 3.6 it returns

    [{'tag': 'test', 'text': '<foo2/>'}, {'tag': 'foo2', 'text': None}]
    

    But starting with 3.7 it returns

    [{'tag': 'test', 'text': '<foo2/>'}, {'tag': 'foo2', 'text': '<foo2/>'}]
    

    The "text" group appears to be a copy of the previous "text" group.

    Some other examples:

    "<test>Hello</test><foo/>" => [{'tag': 'test', 'text': 'Hello'}, {'tag': 'foo', 'text': 'Hello'}] (expected: [{'tag': 'test', 'text': 'Hello'}, {'tag': 'foo', 'text': None}])
    "<test>Hello</test><foo/><foo/>" => [{'tag': 'test', 'text': 'Hello'}, {'tag': 'foo', 'text': 'Hello'}, {'tag': 'foo', 'text': None}] (expected: [{'tag': 'test', 'text': 'Hello'}, {'tag': 'foo', 'text': None}, {'tag': 'foo', 'text': None}])
    

    @beardypig beardypig mannequin added 3.7 (EOL) end of life 3.8 only security fixes topic-regex type-bug An unexpected behavior, bug, or error labels Jul 31, 2018
    @serhiy-storchaka serhiy-storchaka self-assigned this Jul 31, 2018
    @tirkarthi
    Copy link
    Member

    ➜ cpython git:(70d56fb) ✗ ./python.exe
    Python 3.7.0a2+ (tags/v3.7.0a2-341-g70d56fb525:70d56fb525, Jul 31 2018, 21:58:10)
    [Clang 7.0.2 (clang-700.1.81)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.

    >>
    ➜ cpython git:(70d56fb) ✗ ./python.exe -c 'import re; print([m.groupdict() for m in re.finditer(r"(?=<(?P<tag>\w+)/?>(?:(?P<text>.+?)</(?P=tag)>)?)", "<test><foo2/></test>")])'
    [{'tag': 'test', 'text': '<foo2/>'}, {'tag': 'foo2', 'text': '<foo2/>'}]

    ➜ cpython git:(e69fbb6) ✗ ./python.exe
    Python 3.7.0a2+ (tags/v3.7.0a2-340-ge69fbb6a56:e69fbb6a56, Jul 31 2018, 22:12:06)
    [Clang 7.0.2 (clang-700.1.81)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.

    >>
    ➜ cpython git:(e69fbb6) ✗ ./python.exe -c 'import re; print([m.groupdict() for m in re.finditer(r"(?=<(?P<tag>\w+)/?>(?:(?P<text>.+?)</(?P=tag)>)?)", "<test><foo2/></test>")])'
    [{'tag': 'test', 'text': '<foo2/>'}, {'tag': 'foo2', 'text': None}]

    Does this have something to do with 70d56fb(bpo-25054, bpo-1647489) ?

    Thanks

    @animalize
    Copy link
    Mannequin

    animalize mannequin commented Sep 11, 2018

    This bug generates wrong results silently, so I suggest mark it as release blocker for 3.7.1

    @animalize
    Copy link
    Mannequin

    animalize mannequin commented Jan 13, 2019

    Simplify the test-case, it seem the state is not reset properly.

    Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47)
    >>> import re
    >>> re.findall(r"(?=(<\w+>)(<\w+>)?)", "<aaa><bbb>")
    [('<aaa>', '<bbb>'), ('<bbb>', '')]
    
    Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28)
    >>> import re
    >>> re.findall(r"(?=(<\w+>)(<\w+>)?)", "<aaa><bbb>")
    [('<aaa>', '<bbb>'), ('<bbb>', '<bbb>')]

    @animalize
    Copy link
    Mannequin

    animalize mannequin commented Jan 14, 2019

    I tried to fix it, feel free to create a new PR if you don't want this one.

    PR11546 has a small question, should state->data_stack be dealloced as well?

    FYI, function state_reset(SRE_STATE* state) in file _sre.c:

    cpython/Modules/_sre.c

    Lines 340 to 352 in d4f9cf5

    LOCAL(void)
    state_reset(SRE_STATE* state)
    {
    /* FIXME: dynamic! */
    /*memset(state->mark, 0, sizeof(*state->mark) * SRE_MARK_SIZE);*/
    state->lastmark = -1;
    state->lastindex = -1;
    state->repeat = NULL;
    data_stack_dealloc(state);
    }

    @animalize
    Copy link
    Mannequin

    animalize mannequin commented Jan 20, 2019

    Serhiy Storchaka lost his sight.
    Please stop any work and rest, because your left eye will have more burden, and your mental burden will make it worse.
    Go to hospital ASAP.

    If any other core developer want to review this patch, I would like to give a detailed explanation, the logic is not very compilcated.

    @animalize
    Copy link
    Mannequin

    animalize mannequin commented Jan 21, 2019

    Original post's bug was introduced in Python 3.7.0

    When investigate the code, I found another bug about capturing groups. This bug exists since very early version.
    regex module doesn't have this bug.

    Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 bit (Intel)] on win32
    >>> import re
    >>> re.search(r"\b(?=(\t)|(x))x", "a\tx").groups()
    ('', 'x')

    Expected result: (None, 'x')

    Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
    >>> import regex
    >>> regex.search(r"\b(?=(\t)|(x))x", "a\tx").groups()
    (None, 'x')

    @animalize animalize mannequin changed the title re.finditer and lookahead bug re module: wrong capturing groups Jan 21, 2019
    @serhiy-storchaka
    Copy link
    Member

    New changeset 4a7f44a by Serhiy Storchaka (animalize) in branch 'master':
    bpo-34294: re module, fix wrong capturing groups in rare cases. (GH-11546)
    4a7f44a

    @miss-islington
    Copy link
    Contributor

    New changeset 0e379d4 by Miss Islington (bot) in branch '3.7':
    bpo-34294: re module, fix wrong capturing groups in rare cases. (GH-11546)
    0e379d4

    @serhiy-storchaka
    Copy link
    Member

    Thank you for your PR Ma Lin!

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes topic-regex type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants