-- tightened up parsing of octal numbers
-- improved the SRE test harness: don't use asserts, test a few more things (including more boundary conditions)
This commit is contained in:
parent
412f246024
commit
143328ba63
|
@ -15,7 +15,7 @@ from sre_constants import *
|
||||||
MAXREPEAT = 65535
|
MAXREPEAT = 65535
|
||||||
|
|
||||||
SPECIAL_CHARS = ".\\[{()*+?^$|"
|
SPECIAL_CHARS = ".\\[{()*+?^$|"
|
||||||
REPEAT_CHARS = "*+?{"
|
REPEAT_CHARS = "*+?{"
|
||||||
|
|
||||||
DIGITS = tuple("0123456789")
|
DIGITS = tuple("0123456789")
|
||||||
|
|
||||||
|
@ -259,13 +259,12 @@ def _escape(source, escape, state):
|
||||||
# hexadecimal escape
|
# hexadecimal escape
|
||||||
while source.next in HEXDIGITS and len(escape) < 4:
|
while source.next in HEXDIGITS and len(escape) < 4:
|
||||||
escape = escape + source.get()
|
escape = escape + source.get()
|
||||||
escape = escape[2:]
|
if len(escape) != 4:
|
||||||
if len(escape) != 2:
|
raise ValueError
|
||||||
raise error, "bogus escape: %s" % repr("\\" + escape)
|
return LITERAL, int(escape[2:], 16) & 0xff
|
||||||
return LITERAL, int(escape, 16) & 0xff
|
|
||||||
elif escape[1:2] == "0":
|
elif escape[1:2] == "0":
|
||||||
# octal escape
|
# octal escape
|
||||||
while source.next in OCTDIGITS and len(escape) < 5:
|
while source.next in OCTDIGITS and len(escape) < 4:
|
||||||
escape = escape + source.get()
|
escape = escape + source.get()
|
||||||
return LITERAL, int(escape[1:], 8) & 0xff
|
return LITERAL, int(escape[1:], 8) & 0xff
|
||||||
elif escape[1:2] in DIGITS:
|
elif escape[1:2] in DIGITS:
|
||||||
|
@ -273,7 +272,8 @@ def _escape(source, escape, state):
|
||||||
here = source.tell()
|
here = source.tell()
|
||||||
if source.next in DIGITS:
|
if source.next in DIGITS:
|
||||||
escape = escape + source.get()
|
escape = escape + source.get()
|
||||||
if escape[2] in OCTDIGITS and source.next in OCTDIGITS:
|
if (escape[1] in OCTDIGITS and escape[2] in OCTDIGITS and
|
||||||
|
source.next in OCTDIGITS):
|
||||||
# got three octal digits; this is an octal escape
|
# got three octal digits; this is an octal escape
|
||||||
escape = escape + source.get()
|
escape = escape + source.get()
|
||||||
return LITERAL, int(escape[1:], 8) & 0xff
|
return LITERAL, int(escape[1:], 8) & 0xff
|
||||||
|
@ -281,7 +281,7 @@ def _escape(source, escape, state):
|
||||||
group = _group(escape, state.groups)
|
group = _group(escape, state.groups)
|
||||||
if group:
|
if group:
|
||||||
return GROUPREF, group
|
return GROUPREF, group
|
||||||
raise error, "bogus escape: %s" % repr(escape)
|
raise ValueError
|
||||||
if len(escape) == 2:
|
if len(escape) == 2:
|
||||||
return LITERAL, ord(escape[1])
|
return LITERAL, ord(escape[1])
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
|
|
@ -1,2 +1 @@
|
||||||
test_sre
|
test_sre
|
||||||
maximum recursion limit exceeded
|
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
# FIXME: this is basically test_re.py, with a few minor changes
|
# SRE test harness for the Python regression suite
|
||||||
|
|
||||||
|
# this is based on test_re.py, but uses a test function instead
|
||||||
|
# of all those asserts
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
sys.path=['.']+sys.path
|
sys.path=['.']+sys.path
|
||||||
|
@ -7,227 +10,188 @@ from test_support import verbose, TestFailed
|
||||||
import sre
|
import sre
|
||||||
import sys, os, string, traceback
|
import sys, os, string, traceback
|
||||||
|
|
||||||
|
#
|
||||||
|
# test support
|
||||||
|
|
||||||
|
def test(expression, result, exception=None):
|
||||||
|
try:
|
||||||
|
r = eval(expression)
|
||||||
|
except:
|
||||||
|
if exception:
|
||||||
|
if not isinstance(sys.exc_value, exception):
|
||||||
|
print expression, "FAILED"
|
||||||
|
# display name, not actual value
|
||||||
|
if exception is sre.error:
|
||||||
|
print "expected", "sre.error"
|
||||||
|
else:
|
||||||
|
print "expected", exception.__name__
|
||||||
|
print "got", sys.exc_type.__name__, str(sys.exc_value)
|
||||||
|
else:
|
||||||
|
print expression, "FAILED"
|
||||||
|
traceback.print_exc(file=sys.stdout)
|
||||||
|
else:
|
||||||
|
if exception:
|
||||||
|
print expression, "FAILED"
|
||||||
|
if exception is sre.error:
|
||||||
|
print "expected", "sre.error"
|
||||||
|
else:
|
||||||
|
print "expected", exception.__name__
|
||||||
|
print "got result", repr(r)
|
||||||
|
else:
|
||||||
|
if r != result:
|
||||||
|
print expression, "FAILED"
|
||||||
|
print "expected", repr(result)
|
||||||
|
print "got result", repr(r)
|
||||||
|
|
||||||
|
if verbose:
|
||||||
|
print 'Running tests on character literals'
|
||||||
|
|
||||||
|
for i in range(0, 256):
|
||||||
|
test(r"""sre.match("\%03o" % i, chr(i)) != None""", 1)
|
||||||
|
test(r"""sre.match("\%03o0" % i, chr(i)+"0") != None""", 1)
|
||||||
|
test(r"""sre.match("\%03o8" % i, chr(i)+"8") != None""", 1)
|
||||||
|
test(r"""sre.match("\x%02x" % i, chr(i)) != None""", 1)
|
||||||
|
test(r"""sre.match("\x%02x0" % i, chr(i)+"0") != None""", 1)
|
||||||
|
test(r"""sre.match("\x%02xz" % i, chr(i)+"z") != None""", 1)
|
||||||
|
test(r"""sre.match("\911", "")""", None, sre.error)
|
||||||
|
|
||||||
|
#
|
||||||
# Misc tests from Tim Peters' re.doc
|
# Misc tests from Tim Peters' re.doc
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
print 'Running tests on sre.search and sre.match'
|
print 'Running tests on sre.search and sre.match'
|
||||||
|
|
||||||
try:
|
test(r"""sre.search('x*', 'axx').span(0)""", (0, 0))
|
||||||
assert sre.search('x*', 'axx').span(0) == (0, 0)
|
test(r"""sre.search('x*', 'axx').span()""", (0, 0))
|
||||||
assert sre.search('x*', 'axx').span() == (0, 0)
|
test(r"""sre.search('x+', 'axx').span(0)""", (1, 3))
|
||||||
assert sre.search('x+', 'axx').span(0) == (1, 3)
|
test(r"""sre.search('x+', 'axx').span()""", (1, 3))
|
||||||
assert sre.search('x+', 'axx').span() == (1, 3)
|
test(r"""sre.search('x', 'aaa')""", None)
|
||||||
assert sre.search('x', 'aaa') == None
|
|
||||||
except:
|
|
||||||
raise TestFailed, "sre.search"
|
|
||||||
|
|
||||||
try:
|
test(r"""sre.match('a*', 'xxx').span(0)""", (0, 0))
|
||||||
assert sre.match('a*', 'xxx').span(0) == (0, 0)
|
test(r"""sre.match('a*', 'xxx').span()""", (0, 0))
|
||||||
assert sre.match('a*', 'xxx').span() == (0, 0)
|
test(r"""sre.match('x*', 'xxxa').span(0)""", (0, 3))
|
||||||
assert sre.match('x*', 'xxxa').span(0) == (0, 3)
|
test(r"""sre.match('x*', 'xxxa').span()""", (0, 3))
|
||||||
assert sre.match('x*', 'xxxa').span() == (0, 3)
|
test(r"""sre.match('a+', 'xxx')""", None)
|
||||||
assert sre.match('a+', 'xxx') == None
|
|
||||||
except:
|
|
||||||
raise TestFailed, "sre.search"
|
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
print 'Running tests on sre.sub'
|
print 'Running tests on sre.sub'
|
||||||
|
|
||||||
try:
|
test(r"""sre.sub("(?i)b+", "x", "bbbb BBBB")""", 'x x')
|
||||||
assert sre.sub("(?i)b+", "x", "bbbb BBBB") == 'x x'
|
|
||||||
|
|
||||||
def bump_num(matchobj):
|
def bump_num(matchobj):
|
||||||
int_value = int(matchobj.group(0))
|
int_value = int(matchobj.group(0))
|
||||||
return str(int_value + 1)
|
return str(int_value + 1)
|
||||||
|
|
||||||
assert sre.sub(r'\d+', bump_num, '08.2 -2 23x99y') == '9.3 -3 24x100y'
|
test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y')""", '9.3 -3 24x100y')
|
||||||
assert sre.sub(r'\d+', bump_num, '08.2 -2 23x99y', 3) == '9.3 -3 23x99y'
|
test(r"""sre.sub(r'\d+', bump_num, '08.2 -2 23x99y', 3)""", '9.3 -3 23x99y')
|
||||||
|
|
||||||
assert sre.sub('.', lambda m: r"\n", 'x') == '\\n'
|
test(r"""sre.sub('.', lambda m: r"\n", 'x')""", '\\n')
|
||||||
assert sre.sub('.', r"\n", 'x') == '\n'
|
test(r"""sre.sub('.', r"\n", 'x')""", '\n')
|
||||||
|
|
||||||
s = r"\1\1"
|
s = r"\1\1"
|
||||||
assert sre.sub('(.)', s, 'x') == 'xx'
|
|
||||||
assert sre.sub('(.)', sre.escape(s), 'x') == s
|
|
||||||
assert sre.sub('(.)', lambda m: s, 'x') == s
|
|
||||||
|
|
||||||
assert sre.sub('(?P<a>x)', '\g<a>\g<a>', 'xx') == 'xxxx'
|
test(r"""sre.sub('(.)', s, 'x')""", 'xx')
|
||||||
assert sre.sub('(?P<a>x)', '\g<a>\g<1>', 'xx') == 'xxxx'
|
test(r"""sre.sub('(.)', sre.escape(s), 'x')""", s)
|
||||||
assert sre.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx') == 'xxxx'
|
test(r"""sre.sub('(.)', lambda m: s, 'x')""", s)
|
||||||
assert sre.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx') == 'xxxx'
|
|
||||||
|
|
||||||
assert sre.sub('a', r'\t\n\v\r\f\a\b\B\Z\a\A\w\W\s\S\d\D', 'a') == '\t\n\v\r\f\a\b\\B\\Z\a\\A\\w\\W\\s\\S\\d\\D'
|
test(r"""sre.sub('(?P<a>x)', '\g<a>\g<a>', 'xx')""", 'xxxx')
|
||||||
assert sre.sub('a', '\t\n\v\r\f\a', 'a') == '\t\n\v\r\f\a'
|
test(r"""sre.sub('(?P<a>x)', '\g<a>\g<1>', 'xx')""", 'xxxx')
|
||||||
assert sre.sub('a', '\t\n\v\r\f\a', 'a') == (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7))
|
test(r"""sre.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx')""", 'xxxx')
|
||||||
|
test(r"""sre.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx')""", 'xxxx')
|
||||||
|
|
||||||
assert sre.sub('^\s*', 'X', 'test') == 'Xtest'
|
test(r"""sre.sub('a', r'\t\n\v\r\f\a\b\B\Z\a\A\w\W\s\S\d\D', 'a')""", '\t\n\v\r\f\a\b\\B\\Z\a\\A\\w\\W\\s\\S\\d\\D')
|
||||||
except AssertionError:
|
test(r"""sre.sub('a', '\t\n\v\r\f\a', 'a')""", '\t\n\v\r\f\a')
|
||||||
raise TestFailed, "sre.sub"
|
test(r"""sre.sub('a', '\t\n\v\r\f\a', 'a')""", (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)))
|
||||||
|
|
||||||
|
test(r"""sre.sub('^\s*', 'X', 'test')""", 'Xtest')
|
||||||
|
|
||||||
try:
|
# qualified sub
|
||||||
assert sre.sub('a', 'b', 'aaaaa') == 'bbbbb'
|
test(r"""sre.sub('a', 'b', 'aaaaa')""", 'bbbbb')
|
||||||
assert sre.sub('a', 'b', 'aaaaa', 1) == 'baaaa'
|
test(r"""sre.sub('a', 'b', 'aaaaa', 1)""", 'baaaa')
|
||||||
except AssertionError:
|
|
||||||
raise TestFailed, "qualified sre.sub"
|
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
print 'Running tests on symbolic references'
|
print 'Running tests on symbolic references'
|
||||||
|
|
||||||
try:
|
test(r"""sre.sub('(?P<a>x)', '\g<a', 'xx')""", None, sre.error)
|
||||||
sre.sub('(?P<a>x)', '\g<a', 'xx')
|
test(r"""sre.sub('(?P<a>x)', '\g<', 'xx')""", None, sre.error)
|
||||||
except sre.error, reason:
|
test(r"""sre.sub('(?P<a>x)', '\g', 'xx')""", None, sre.error)
|
||||||
pass
|
test(r"""sre.sub('(?P<a>x)', '\g<a a>', 'xx')""", None, sre.error)
|
||||||
else:
|
test(r"""sre.sub('(?P<a>x)', '\g<1a1>', 'xx')""", None, sre.error)
|
||||||
raise TestFailed, "symbolic reference"
|
test(r"""sre.sub('(?P<a>x)', '\g<ab>', 'xx')""", None, IndexError)
|
||||||
|
test(r"""sre.sub('(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')""", None, sre.error)
|
||||||
try:
|
test(r"""sre.sub('(?P<a>x)|(?P<b>y)', '\\2', 'xx')""", None, sre.error)
|
||||||
sre.sub('(?P<a>x)', '\g<', 'xx')
|
|
||||||
except sre.error, reason:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "symbolic reference"
|
|
||||||
|
|
||||||
try:
|
|
||||||
sre.sub('(?P<a>x)', '\g', 'xx')
|
|
||||||
except sre.error, reason:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "symbolic reference"
|
|
||||||
|
|
||||||
try:
|
|
||||||
sre.sub('(?P<a>x)', '\g<a a>', 'xx')
|
|
||||||
except sre.error, reason:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "symbolic reference"
|
|
||||||
|
|
||||||
try:
|
|
||||||
sre.sub('(?P<a>x)', '\g<1a1>', 'xx')
|
|
||||||
except sre.error, reason:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "symbolic reference"
|
|
||||||
|
|
||||||
try:
|
|
||||||
sre.sub('(?P<a>x)', '\g<ab>', 'xx')
|
|
||||||
except IndexError, reason:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "symbolic reference"
|
|
||||||
|
|
||||||
try:
|
|
||||||
sre.sub('(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')
|
|
||||||
except sre.error, reason:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "symbolic reference"
|
|
||||||
|
|
||||||
try:
|
|
||||||
sre.sub('(?P<a>x)|(?P<b>y)', '\\2', 'xx')
|
|
||||||
except sre.error, reason:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise TestFailed, "symbolic reference"
|
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
print 'Running tests on sre.subn'
|
print 'Running tests on sre.subn'
|
||||||
|
|
||||||
try:
|
test(r"""sre.subn("(?i)b+", "x", "bbbb BBBB")""", ('x x', 2))
|
||||||
assert sre.subn("(?i)b+", "x", "bbbb BBBB") == ('x x', 2)
|
test(r"""sre.subn("b+", "x", "bbbb BBBB")""", ('x BBBB', 1))
|
||||||
assert sre.subn("b+", "x", "bbbb BBBB") == ('x BBBB', 1)
|
test(r"""sre.subn("b+", "x", "xyz")""", ('xyz', 0))
|
||||||
assert sre.subn("b+", "x", "xyz") == ('xyz', 0)
|
test(r"""sre.subn("b*", "x", "xyz")""", ('xxxyxzx', 4))
|
||||||
assert sre.subn("b*", "x", "xyz") == ('xxxyxzx', 4)
|
test(r"""sre.subn("b*", "x", "xyz", 2)""", ('xxxyz', 2))
|
||||||
assert sre.subn("b*", "x", "xyz", 2) == ('xxxyz', 2)
|
|
||||||
except AssertionError:
|
|
||||||
raise TestFailed, "sre.subn"
|
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
print 'Running tests on sre.split'
|
print 'Running tests on sre.split'
|
||||||
|
|
||||||
try:
|
test(r"""sre.split(":", ":a:b::c")""", ['', 'a', 'b', '', 'c'])
|
||||||
assert sre.split(":", ":a:b::c") == ['', 'a', 'b', '', 'c']
|
test(r"""sre.split(":*", ":a:b::c")""", ['', 'a', 'b', 'c'])
|
||||||
assert sre.split(":*", ":a:b::c") == ['', 'a', 'b', 'c']
|
test(r"""sre.split("(:*)", ":a:b::c")""", ['', ':', 'a', ':', 'b', '::', 'c'])
|
||||||
assert sre.split("(:*)", ":a:b::c") == ['', ':', 'a', ':', 'b', '::', 'c']
|
test(r"""sre.split("(?::*)", ":a:b::c")""", ['', 'a', 'b', 'c'])
|
||||||
assert sre.split("(?::*)", ":a:b::c") == ['', 'a', 'b', 'c']
|
test(r"""sre.split("(:)*", ":a:b::c")""", ['', ':', 'a', ':', 'b', ':', 'c'])
|
||||||
assert sre.split("(:)*", ":a:b::c") == ['', ':', 'a', ':', 'b', ':', 'c']
|
test(r"""sre.split("([b:]+)", ":a:b::c")""", ['', ':', 'a', ':b::', 'c'])
|
||||||
assert sre.split("([b:]+)", ":a:b::c") == ['', ':', 'a', ':b::', 'c']
|
test(r"""sre.split("(b)|(:+)", ":a:b::c")""",
|
||||||
assert sre.split("(b)|(:+)", ":a:b::c") == \
|
['', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c'])
|
||||||
['', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c']
|
test(r"""sre.split("(?:b)|(?::+)", ":a:b::c")""", ['', 'a', '', '', 'c'])
|
||||||
assert sre.split("(?:b)|(?::+)", ":a:b::c") == ['', 'a', '', '', 'c']
|
|
||||||
except AssertionError:
|
|
||||||
raise TestFailed, "sre.split"
|
|
||||||
|
|
||||||
try:
|
test(r"""sre.split(":", ":a:b::c", 2)""", ['', 'a', 'b::c'])
|
||||||
assert sre.split(":", ":a:b::c", 2) == ['', 'a', 'b::c']
|
test(r"""sre.split(':', 'a:b:c:d', 2)""", ['a', 'b', 'c:d'])
|
||||||
assert sre.split(':', 'a:b:c:d', 2) == ['a', 'b', 'c:d']
|
|
||||||
|
|
||||||
assert sre.split("(:)", ":a:b::c", 2) == ['', ':', 'a', ':', 'b::c']
|
test(r"""sre.split("(:)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c'])
|
||||||
assert sre.split("(:*)", ":a:b::c", 2) == ['', ':', 'a', ':', 'b::c']
|
test(r"""sre.split("(:*)", ":a:b::c", 2)""", ['', ':', 'a', ':', 'b::c'])
|
||||||
except AssertionError:
|
|
||||||
raise TestFailed, "qualified sre.split"
|
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
print "Running tests on sre.findall"
|
print "Running tests on sre.findall"
|
||||||
|
|
||||||
try:
|
test(r"""sre.findall(":+", "abc")""", [])
|
||||||
assert sre.findall(":+", "abc") == []
|
test(r"""sre.findall(":+", "a:b::c:::d")""", [":", "::", ":::"])
|
||||||
assert sre.findall(":+", "a:b::c:::d") == [":", "::", ":::"]
|
test(r"""sre.findall("(:+)", "a:b::c:::d")""", [":", "::", ":::"])
|
||||||
assert sre.findall("(:+)", "a:b::c:::d") == [":", "::", ":::"]
|
test(r"""sre.findall("(:)(:*)", "a:b::c:::d")""",
|
||||||
assert sre.findall("(:)(:*)", "a:b::c:::d") == [(":", ""),
|
[(":", ""), (":", ":"), (":", "::")])
|
||||||
(":", ":"),
|
test(r"""sre.findall("(a)|(b)", "abc")""", [("a", ""), ("", "b")])
|
||||||
(":", "::")]
|
|
||||||
assert sre.findall("(a)|(b)", "abc") == [("a", ""), ("", "b")]
|
|
||||||
except AssertionError:
|
|
||||||
raise TestFailed, "sre.findall"
|
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
print "Running tests on sre.match"
|
print "Running tests on sre.match"
|
||||||
|
|
||||||
try:
|
test(r"""sre.match('a', 'a').groups()""", ())
|
||||||
# No groups at all
|
test(r"""sre.match('(a)', 'a').groups()""", ('a',))
|
||||||
m = sre.match('a', 'a') ; assert m.groups() == ()
|
test(r"""sre.match('(a)', 'a').group(0)""", 'a')
|
||||||
# A single group
|
test(r"""sre.match('(a)', 'a').group(1)""", 'a')
|
||||||
m = sre.match('(a)', 'a') ; assert m.groups() == ('a',)
|
test(r"""sre.match('(a)', 'a').group(1, 1)""", ('a', 'a'))
|
||||||
|
|
||||||
pat = sre.compile('((a)|(b))(c)?')
|
pat = sre.compile('((a)|(b))(c)?')
|
||||||
assert pat.match('a').groups() == ('a', 'a', None, None)
|
test(r"""pat.match('a').groups()""", ('a', 'a', None, None))
|
||||||
assert pat.match('b').groups() == ('b', None, 'b', None)
|
test(r"""pat.match('b').groups()""", ('b', None, 'b', None))
|
||||||
assert pat.match('ac').groups() == ('a', 'a', None, 'c')
|
test(r"""pat.match('ac').groups()""", ('a', 'a', None, 'c'))
|
||||||
assert pat.match('bc').groups() == ('b', None, 'b', 'c')
|
test(r"""pat.match('bc').groups()""", ('b', None, 'b', 'c'))
|
||||||
assert pat.match('bc').groups("") == ('b', "", 'b', 'c')
|
test(r"""pat.match('bc').groups("")""", ('b', "", 'b', 'c'))
|
||||||
except AssertionError:
|
|
||||||
raise TestFailed, "match .groups() method"
|
|
||||||
|
|
||||||
try:
|
pat = sre.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
|
||||||
# A single group
|
test(r"""pat.match('a').group(1, 2, 3)""", ('a', None, None))
|
||||||
m = sre.match('(a)', 'a')
|
test(r"""pat.match('b').group('a1', 'b2', 'c3')""", (None, 'b', None))
|
||||||
assert m.group(0) == 'a' ; assert m.group(0) == 'a'
|
test(r"""pat.match('ac').group(1, 'b2', 3)""", ('a', None, 'c'))
|
||||||
assert m.group(1) == 'a' ; assert m.group(1, 1) == ('a', 'a')
|
|
||||||
|
|
||||||
pat = sre.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
|
|
||||||
assert pat.match('a').group(1, 2, 3) == ('a', None, None)
|
|
||||||
assert pat.match('b').group('a1', 'b2', 'c3') == (None, 'b', None)
|
|
||||||
assert pat.match('ac').group(1, 'b2', 3) == ('a', None, 'c')
|
|
||||||
except AssertionError:
|
|
||||||
raise TestFailed, "match .group() method"
|
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
print "Running tests on sre.escape"
|
print "Running tests on sre.escape"
|
||||||
|
|
||||||
try:
|
p = ""
|
||||||
p=""
|
for i in range(0, 256):
|
||||||
for i in range(0, 256):
|
p = p + chr(i)
|
||||||
p = p + chr(i)
|
test(r"""sre.match(sre.escape(chr(i)), chr(i)) != None""", 1)
|
||||||
assert sre.match(sre.escape(chr(i)), chr(i)) != None
|
test(r"""sre.match(sre.escape(chr(i)), chr(i)).span()""", (0,1))
|
||||||
assert sre.match(sre.escape(chr(i)), chr(i)).span() == (0,1)
|
|
||||||
|
|
||||||
pat=sre.compile( sre.escape(p) )
|
|
||||||
assert pat.match(p) != None
|
|
||||||
assert pat.match(p).span() == (0,256)
|
|
||||||
except AssertionError:
|
|
||||||
raise TestFailed, "sre.escape"
|
|
||||||
|
|
||||||
|
pat = sre.compile(sre.escape(p))
|
||||||
|
test(r"""pat.match(p) != None""", 1)
|
||||||
|
test(r"""pat.match(p).span()""", (0,256))
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
print 'Pickling a SRE_Pattern instance'
|
print 'Pickling a SRE_Pattern instance'
|
||||||
|
@ -248,16 +212,14 @@ try:
|
||||||
except:
|
except:
|
||||||
print TestFailed, 're module cPickle' # expected
|
print TestFailed, 're module cPickle' # expected
|
||||||
|
|
||||||
try:
|
# constants
|
||||||
assert sre.I == sre.IGNORECASE
|
test(r"""sre.I""", sre.IGNORECASE)
|
||||||
assert sre.L == sre.LOCALE
|
test(r"""sre.L""", sre.LOCALE)
|
||||||
assert sre.M == sre.MULTILINE
|
test(r"""sre.M""", sre.MULTILINE)
|
||||||
assert sre.S == sre.DOTALL
|
test(r"""sre.S""", sre.DOTALL)
|
||||||
assert sre.X == sre.VERBOSE
|
test(r"""sre.X""", sre.VERBOSE)
|
||||||
assert sre.T == sre.TEMPLATE
|
test(r"""sre.T""", sre.TEMPLATE)
|
||||||
assert sre.U == sre.UNICODE
|
test(r"""sre.U""", sre.UNICODE)
|
||||||
except AssertionError:
|
|
||||||
raise TestFailed, 're module constants'
|
|
||||||
|
|
||||||
for flags in [sre.I, sre.M, sre.X, sre.S, sre.L, sre.T, sre.U]:
|
for flags in [sre.I, sre.M, sre.X, sre.S, sre.L, sre.T, sre.U]:
|
||||||
try:
|
try:
|
||||||
|
@ -270,10 +232,9 @@ if verbose:
|
||||||
|
|
||||||
# Try nasty case that overflows the straightforward recursive
|
# Try nasty case that overflows the straightforward recursive
|
||||||
# implementation of repeated groups.
|
# implementation of repeated groups.
|
||||||
try:
|
test(r"""sre.match('(x)*', 50000*'x').span()""", (0, 50000), RuntimeError)
|
||||||
assert sre.match('(x)*', 50000*'x').span() == (0, 50000)
|
test(r"""sre.match('(x)*y', 50000*'x'+'y').span()""", (0, 50001), RuntimeError)
|
||||||
except RuntimeError, v:
|
test(r"""sre.match('(x)*?y', 50000*'x'+'y').span()""", (0, 50001), RuntimeError)
|
||||||
print v
|
|
||||||
|
|
||||||
from re_tests import *
|
from re_tests import *
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue