[Python-checkins] python/dist/src/Modules _sre.c,2.93,2.94

niemeyer@users.sourceforge.net niemeyer@users.sourceforge.net
Sun, 27 Apr 2003 05:34:16 -0700


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1:/tmp/cvs-serv31416/Modules

Modified Files:
	_sre.c 
Log Message:
Applied patch #725106, by Greg Chapman, fixing capturing groups
within repeats of alternatives. The only change to the original
patch was to convert the tests to the new test_re.py file.

This patch fixes cases like:

>>> re.match('((a)|b)*', 'abc').groups()
('b', '') 

Which is wrong (it's impossible to match the empty string),
and incompatible with other regex systems, like the following
examples show:

% perl -e '"abc" =~ /^((a)|b)*/; print "$1 $2\n";'
b a

% echo "abc" | sed -r -e "s/^((a)|b)*/\1 \2|/"
b a|c


Index: _sre.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/_sre.c,v
retrieving revision 2.93
retrieving revision 2.94
diff -C2 -d -r2.93 -r2.94
*** _sre.c	27 Apr 2003 06:58:54 -0000	2.93
--- _sre.c	27 Apr 2003 12:34:14 -0000	2.94
***************
*** 948,955 ****
--- 948,965 ----
                      (ptr >= end || !SRE_CHARSET(pattern + 3, (SRE_CODE) *ptr)))
                      continue;
+                 if (state->repeat) {
+                     i = mark_save(state, 0, lastmark);
+                     if (i < 0)
+                         return i;
+                 }
                  state->ptr = ptr;
                  i = SRE_MATCH(state, pattern + 1, level + 1);
                  if (i)
                      return i;
+                 if (state->repeat) {
+                     i = mark_restore(state, 0, lastmark);
+                     if (i < 0)
+                         return i;
+                 }
                  LASTMARK_RESTORE();
              }