[Python-bugs-list] [ python-Bugs-672491 ] 2.3a1 computes lastindex incorrectly
SourceForge.net
noreply@sourceforge.net
Tue, 04 Feb 2003 17:56:03 -0800
Bugs item #672491, was opened at 2003-01-22 06:28
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=672491&group_id=5470
Category: Regular Expressions
Group: Python 2.3
Status: Open
Resolution: None
Priority: 6
Submitted By: Martin v. Löwis (loewis)
Assigned to: Fredrik Lundh (effbot)
Summary: 2.3a1 computes lastindex incorrectly
Initial Comment:
In Python 2.[012], the code
import re
exp = re.compile("(?P<NCName>[a-zA-Z_](\w|[_.-])*)")
print exp.match("namespace").lastgroup
prints "NCName". In Python 2.3a1, it prints "None". The
problem is that last index is 2, instead of 1, as it
should be.
----------------------------------------------------------------------
Comment By: Greg Chapman (glchapman)
Date: 2003-02-04 16:56
Message:
Logged In: YES
user_id=86307
I believe the discrepancy was deliberately introduced in revision 2.84 of _sre.c. I agree with you that lastindex should return the the index of the matching group with the rightmost closing parenthesis (perhaps some elaboration in the docs is also in order). If this is the correct interpretation, two places need to be patched: 1) the handling of SRE_OP_MARK needs to be reverted to the 2.22 code and 2) the code in the lastmark_restore function needs to be tweaked so that lastindex is not accidentally set to the last matched group entered.
Thinking further though, given a (contrived) pattern like this:
re.match('((x))y(?:(a)b|ac)', 'xyac')
what should lastindex be? I assume 1, given the definition above (lastindex = matching group with rightmost close parens). In 2.22 it is 3, since group 3 matched before the branch failed at the 'b'. In 2.3a1 it is 2, since lastindex is restored (after the branch fails) using the saved lastmark. Anyway, if it should be 1, then I think _sre.c will have to save lastindex as well as lastmark when processing the three opcodes which may end up calling lastmark_restore.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=672491&group_id=5470