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 fails to identify invalid numeric group references in replacement strings #70141
Comments
This code example:
will correctly raise a KeyError due to the invalid group reference. However, this very similar code example:
fails to raise an error. It seems that the only way to check whether a numeric group reference is compatible with a given pattern is to test it against a string which happens to match. But this is obviously infeasible when checking unknown expressions (e.g. those taken from user input). And in any case: errors should be raised at the point where they occur (i.e. during compilation), not at some indeterminate point in the future. Regular expression objects have a "groups" attribute which holds the number of capturing groups in the pattern. So there seems no good reason why the replacement string parser can't identify invalid numeric group references in exactly the same way that it does for symbolic ones. |
Well, at least on the surface of it, the fix seems pretty straightforward: |
Here is the updated patch with fixes to the test suite. |
Needed new tests for changed behavior. Test re.sub() with incorrect groups and the string that doesn't match the pattern (e.g. empty string). Added other comments on Rietveld. |
Updated patch taking Serhiy's comments into account. There was another case on line 725 for when zero is used as a group number, I'm not sure though if it falls within the scope of this issue, so it's not included in the current patch. |
I've modified addgroup to take a pos argument, this seem to introduce minimal disturbance. |
Updated patch fixing the position issue. |
LGTM. Thank you for your contribution SilentGhost. |
New changeset cea983246919 by Serhiy Storchaka in branch '3.6': New changeset 15e3695affa2 by Serhiy Storchaka in branch 'default': |
Committed with additional changes. Fixed yet one occurrence of "invalid group reference" without group index, and made small style changes. |
Misc/NEWS
so that it is managed by towncrier #552Note: 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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: