Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 510c97b

Browse files
author
Fredrik Lundh
committed
return -1 for undefined groups (as implemented in 1.5.2) instead of
None (as documented) from start/end/span. closes bug #113254
1 parent ff07f8c commit 510c97b

2 files changed

Lines changed: 10 additions & 17 deletions

File tree

Lib/test/test_sre.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test(expression, result, exception=None):
4646
if verbose:
4747
print 'Running tests on character literals'
4848

49-
for i in range(0, 256):
49+
for i in [0, 8, 16, 32, 64, 127, 128, 255]:
5050
test(r"""sre.match("\%03o" % i, chr(i)) != None""", 1)
5151
test(r"""sre.match("\%03o0" % i, chr(i)+"0") != None""", 1)
5252
test(r"""sre.match("\%03o8" % i, chr(i)+"8") != None""", 1)
@@ -73,6 +73,11 @@ def test(expression, result, exception=None):
7373
test(r"""sre.match('x*', 'xxxa').span()""", (0, 3))
7474
test(r"""sre.match('a+', 'xxx')""", None)
7575

76+
# bug 113254
77+
test(r"""sre.match('(a)|(b)', 'b').start(1)""", -1)
78+
test(r"""sre.match('(a)|(b)', 'b').end(1)""", -1)
79+
test(r"""sre.match('(a)|(b)', 'b').span(1)""", (-1, -1))
80+
7681
if verbose:
7782
print 'Running tests on sre.sub'
7883

Modules/_sre.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* 00-08-07 fl use PyOS_CheckStack() if available
1818
* 00-08-08 fl changed findall to return empty strings instead of None
1919
* 00-08-27 fl properly propagate memory errors
20+
* 00-09-02 fl return -1 instead of None for start/end/span
2021
*
2122
* Copyright (c) 1997-2000 by Secret Labs AB. All rights reserved.
2223
*
@@ -1983,11 +1984,7 @@ match_start(MatchObject* self, PyObject* args)
19831984
return NULL;
19841985
}
19851986

1986-
if (self->mark[index*2] < 0) {
1987-
Py_INCREF(Py_None);
1988-
return Py_None;
1989-
}
1990-
1987+
/* mark is -1 if group is undefined */
19911988
return Py_BuildValue("i", self->mark[index*2]);
19921989
}
19931990

@@ -2010,11 +2007,7 @@ match_end(MatchObject* self, PyObject* args)
20102007
return NULL;
20112008
}
20122009

2013-
if (self->mark[index*2] < 0) {
2014-
Py_INCREF(Py_None);
2015-
return Py_None;
2016-
}
2017-
2010+
/* mark is -1 if group is undefined */
20182011
return Py_BuildValue("i", self->mark[index*2+1]);
20192012
}
20202013

@@ -2064,12 +2057,7 @@ match_span(MatchObject* self, PyObject* args)
20642057
return NULL;
20652058
}
20662059

2067-
if (self->mark[index*2] < 0) {
2068-
Py_INCREF(Py_None);
2069-
Py_INCREF(Py_None);
2070-
return Py_BuildValue("OO", Py_None, Py_None);
2071-
}
2072-
2060+
/* marks are -1 if group is undefined */
20732061
return _pair(self->mark[index*2], self->mark[index*2+1]);
20742062
}
20752063

0 commit comments

Comments
 (0)