Kaydet (Commit) 8430c583 authored tarafından Guido van Rossum's avatar Guido van Rossum

AMK's latest

üst 07bcd998
...@@ -66,8 +66,8 @@ def escape(pattern): ...@@ -66,8 +66,8 @@ def escape(pattern):
alphanum=string.letters+'_'+string.digits alphanum=string.letters+'_'+string.digits
for char in pattern: for char in pattern:
if char not in alphanum: if char not in alphanum:
if char == '\000': result.append(r'\000') if char=='\000': result.append('\\000')
else: result.append('\\' + char) else: result.append('\\'+char)
else: result.append(char) else: result.append(char)
return string.join(result, '') return string.join(result, '')
...@@ -132,9 +132,9 @@ class RegexObject: ...@@ -132,9 +132,9 @@ class RegexObject:
def subn(self, repl, source, count=0): def subn(self, repl, source, count=0):
"""Return a 2-tuple containing (new_string, number). """Return a 2-tuple containing (new_string, number).
new_string is the string obtained by replacing the leftmost new_string is the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in string by the non-overlapping occurrences of the pattern in the source
replacement repl. number is the number of substitutions that string by the replacement repl. number is the number of
were made.""" substitutions that were made."""
if count < 0: if count < 0:
raise error, "negative substitution count" raise error, "negative substitution count"
...@@ -174,7 +174,7 @@ class RegexObject: ...@@ -174,7 +174,7 @@ class RegexObject:
return (string.join(results, ''), n) return (string.join(results, ''), n)
def split(self, source, maxsplit=0): def split(self, source, maxsplit=0):
"""Split \var{string} by the occurrences of the pattern, """Split the \var{source} string by the occurrences of the pattern,
returning a list containing the resulting substrings.""" returning a list containing the resulting substrings."""
if maxsplit < 0: if maxsplit < 0:
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
# -*- mode: python -*- # -*- mode: python -*-
# $Id$ # $Id$
# Re test suite and benchmark suite v1.5b2 # Re test suite and benchmark suite v1.5
# The 3 possible outcomes for each pattern # The 3 possible outcomes for each pattern
[SUCCEED, FAIL, SYNTAX_ERROR] = range(3) [SUCCEED, FAIL, SYNTAX_ERROR] = range(3)
...@@ -62,23 +62,20 @@ tests = [ ...@@ -62,23 +62,20 @@ tests = [
('(?P<foo_123>a)', 'a', SUCCEED, 'g1', 'a'), ('(?P<foo_123>a)', 'a', SUCCEED, 'g1', 'a'),
('(?P<foo_123>a)(?P=foo_123)', 'aa', SUCCEED, 'g1', 'a'), ('(?P<foo_123>a)(?P=foo_123)', 'aa', SUCCEED, 'g1', 'a'),
# Test octal escapes # Test octal escapes
('\\1', 'a', SYNTAX_ERROR), ('\\1', 'a', SYNTAX_ERROR), # Backreference
('[\\1]', '\1', SUCCEED, 'found', '\1'), # Character
('\\09', chr(0) + '9', SUCCEED, 'found', chr(0) + '9'), ('\\09', chr(0) + '9', SUCCEED, 'found', chr(0) + '9'),
('\\141', 'a', SUCCEED, 'found', 'a'), ('\\141', 'a', SUCCEED, 'found', 'a'),
('(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)\\119', 'abcdefghijklk9', SUCCEED, 'found+"-"+g11', 'abcdefghijklk9-k'), ('(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)\\119', 'abcdefghijklk9', SUCCEED, 'found+"-"+g11', 'abcdefghijklk9-k'),
# Test that a literal \0 is handled everywhere # Test \0 is handled everywhere
('\0', '\0', SUCCEED, 'found', '\0'),
(r'\0', '\0', SUCCEED, 'found', '\0'), (r'\0', '\0', SUCCEED, 'found', '\0'),
('[\0a]', '\0', SUCCEED, 'found', '\0'),
('[a\0]', '\0', SUCCEED, 'found', '\0'),
('[^a\0]', '\0', FAIL),
(r'[\0a]', '\0', SUCCEED, 'found', '\0'), (r'[\0a]', '\0', SUCCEED, 'found', '\0'),
(r'[a\0]', '\0', SUCCEED, 'found', '\0'), (r'[a\0]', '\0', SUCCEED, 'found', '\0'),
(r'[^a\0]', '\0', FAIL), (r'[^a\0]', '\0', FAIL),
# Test various letter escapes # Test various letter escapes
(r'\a[\b]\f\n\r\t\v', '\a\b\f\n\r\t\v', SUCCEED, 'found', '\a\b\f\n\r\t\v'), (r'\a[\b]\f\n\r\t\v', '\a\b\f\n\r\t\v', SUCCEED, 'found', '\a\b\f\n\r\t\v'),
(r'[\a][\b][\f][\n][\r][\t][\v]', '\a\b\f\n\r\t\v', SUCCEED, 'found', '\a\b\f\n\r\t\v'), (r'[\a][\b][\f][\n][\r][\t][\v]', '\a\b\f\n\r\t\v', SUCCEED, 'found', '\a\b\f\n\r\t\v'),
...@@ -103,6 +100,8 @@ tests = [ ...@@ -103,6 +100,8 @@ tests = [
('(?s)a.{4,5}b', 'acc\nccb', SUCCEED, 'found', 'acc\nccb'), ('(?s)a.{4,5}b', 'acc\nccb', SUCCEED, 'found', 'acc\nccb'),
('(?s)a.b', 'a\nb', SUCCEED, 'found', 'a\nb'), ('(?s)a.b', 'a\nb', SUCCEED, 'found', 'a\nb'),
(')', '', SYNTAX_ERROR), # Unmatched right bracket
('', '', SUCCEED, 'found', ''), # Empty pattern
('abc', 'abc', SUCCEED, 'found', 'abc'), ('abc', 'abc', SUCCEED, 'found', 'abc'),
('abc', 'xbc', FAIL), ('abc', 'xbc', FAIL),
('abc', 'axc', FAIL), ('abc', 'axc', FAIL),
...@@ -393,9 +392,6 @@ tests = [ ...@@ -393,9 +392,6 @@ tests = [
('(.*)c(.*)', 'abcde', SUCCEED, 'found+"-"+g1+"-"+g2', 'abcde-ab-de'), ('(.*)c(.*)', 'abcde', SUCCEED, 'found+"-"+g1+"-"+g2', 'abcde-ab-de'),
('\\((.*), (.*)\\)', '(a, b)', SUCCEED, 'g2+"-"+g1', 'b-a'), ('\\((.*), (.*)\\)', '(a, b)', SUCCEED, 'g2+"-"+g1', 'b-a'),
('[k]', 'ab', FAIL), ('[k]', 'ab', FAIL),
# XXX
# ('abcd', 'abcd', SUCCEED, 'found+"-"+\\found+"-"+\\\\found', 'abcd-$&-\\abcd'),
# ('a(bc)d', 'abcd', SUCCEED, 'g1+"-"+\\g1+"-"+\\\\g1', 'bc-$1-\\bc'),
('a[-]?c', 'ac', SUCCEED, 'found', 'ac'), ('a[-]?c', 'ac', SUCCEED, 'found', 'ac'),
('(abc)\\1', 'abcabc', SUCCEED, 'g1', 'abc'), ('(abc)\\1', 'abcabc', SUCCEED, 'g1', 'abc'),
('([a-c]*)\\1', 'abcabc', SUCCEED, 'g1', 'abc'), ('([a-c]*)\\1', 'abcabc', SUCCEED, 'g1', 'abc'),
......
...@@ -2,12 +2,36 @@ ...@@ -2,12 +2,36 @@
# -*- mode: python -*- # -*- mode: python -*-
# $Id$ # $Id$
import sys
sys.path=['.']+sys.path
from test_support import verbose, TestFailed from test_support import verbose, TestFailed
import re import re
import sys, os, string, traceback import sys, os, string, traceback
# Misc tests from Tim Peters' re.doc # Misc tests from Tim Peters' re.doc
if verbose:
print 'Running tests on re.search and re.match'
try:
assert re.search('x*', 'axx').span(0) == (0, 0)
assert re.search('x*', 'axx').span() == (0, 0)
assert re.search('x+', 'axx').span(0) == (1, 3)
assert re.search('x+', 'axx').span() == (1, 3)
assert re.search('x', 'aaa') == None
except:
raise TestFailed, "re.search"
try:
assert re.match('a*', 'xxx').span(0) == (0, 0)
assert re.match('a*', 'xxx').span() == (0, 0)
assert re.match('x*', 'xxxa').span(0) == (0, 3)
assert re.match('x*', 'xxxa').span() == (0, 3)
assert re.match('a+', 'xxx') == None
except:
raise TestFailed, "re.search"
if verbose: if verbose:
print 'Running tests on re.sub' print 'Running tests on re.sub'
...@@ -19,25 +43,30 @@ try: ...@@ -19,25 +43,30 @@ try:
return str(int_value + 1) return str(int_value + 1)
assert re.sub(r'\d+', bump_num, '08.2 -2 23x99y') == '9.3 -3 24x100y' assert re.sub(r'\d+', bump_num, '08.2 -2 23x99y') == '9.3 -3 24x100y'
assert re.sub(r'\d+', bump_num, '08.2 -2 23x99y', 3) == '9.3 -3 23x99y'
assert re.sub('.', lambda m: r"\n", 'x') == '\\n' assert re.sub('.', lambda m: r"\n", 'x') == '\\n'
assert re.sub('.', r"\n", 'x') == '\n' assert re.sub('.', r"\n", 'x') == '\n'
s = r"\1\1" s = r"\1\1"
assert re.sub('(.)', s, 'x') == 'xx' assert re.sub('(.)', s, 'x') == 'xx'
assert re.sub('(.)', re.escape(s), 'x') == s assert re.sub('(.)', re.escape(s), 'x') == s
assert re.sub('(.)', lambda m: s, 'x') == s assert re.sub('(.)', lambda m: s, 'x') == s
assert re.sub('(?P<a>x)', '\g<a>\g<a>', 'xx') == 'xxxx' assert re.sub('(?P<a>x)', '\g<a>\g<a>', 'xx') == 'xxxx'
assert re.sub('(?P<a>x)', '\g<a>\g<1>', 'xx') == 'xxxx'
assert re.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx') == 'xxxx' assert re.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx') == 'xxxx'
assert re.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx') == 'xxxx'
assert re.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\bBZ\aAwWsSdD' assert re.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'
assert re.sub('a', '\t\n\v\r\f\a', 'a') == '\t\n\v\r\f\a' assert re.sub('a', '\t\n\v\r\f\a', 'a') == '\t\n\v\r\f\a'
assert re.sub('a', '\t\n\v\r\f\a', 'a') == (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)) assert re.sub('a', '\t\n\v\r\f\a', 'a') == (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7))
assert re.sub('^\s*', 'X', 'test') == 'Xtest'
except AssertionError: except AssertionError:
raise TestFailed, "re.sub" raise TestFailed, "re.sub"
try: try:
assert re.sub('a', 'b', 'aaaaa') == 'bbbbb' assert re.sub('a', 'b', 'aaaaa') == 'bbbbb'
assert re.sub('a', 'b', 'aaaaa', 1) == 'baaaa' assert re.sub('a', 'b', 'aaaaa', 1) == 'baaaa'
...@@ -75,6 +104,13 @@ except re.error, reason: ...@@ -75,6 +104,13 @@ except re.error, reason:
else: else:
raise TestFailed, "symbolic reference" raise TestFailed, "symbolic reference"
try:
re.sub('(?P<a>x)', '\g<1a1>', 'xx')
except re.error, reason:
pass
else:
raise TestFailed, "symbolic reference"
try: try:
re.sub('(?P<a>x)', '\g<ab>', 'xx') re.sub('(?P<a>x)', '\g<ab>', 'xx')
except IndexError, reason: except IndexError, reason:
...@@ -104,9 +140,13 @@ try: ...@@ -104,9 +140,13 @@ try:
assert re.subn("b+", "x", "bbbb BBBB") == ('x BBBB', 1) assert re.subn("b+", "x", "bbbb BBBB") == ('x BBBB', 1)
assert re.subn("b+", "x", "xyz") == ('xyz', 0) assert re.subn("b+", "x", "xyz") == ('xyz', 0)
assert re.subn("b*", "x", "xyz") == ('xxxyxzx', 4) assert re.subn("b*", "x", "xyz") == ('xxxyxzx', 4)
assert re.subn("b*", "x", "xyz", 2) == ('xxxyz', 2)
except AssertionError: except AssertionError:
raise TestFailed, "re.subn" raise TestFailed, "re.subn"
if verbose:
print 'Running tests on re.split'
try: try:
assert re.split(":", ":a:b::c") == ['', 'a', 'b', '', 'c'] assert re.split(":", ":a:b::c") == ['', 'a', 'b', '', 'c']
assert re.split(":*", ":a:b::c") == ['', 'a', 'b', 'c'] assert re.split(":*", ":a:b::c") == ['', 'a', 'b', 'c']
...@@ -117,7 +157,6 @@ try: ...@@ -117,7 +157,6 @@ try:
assert re.split("(b)|(:+)", ":a:b::c") == \ assert re.split("(b)|(:+)", ":a:b::c") == \
['', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c'] ['', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c']
assert re.split("(?:b)|(?::+)", ":a:b::c") == ['', 'a', '', '', 'c'] assert re.split("(?:b)|(?::+)", ":a:b::c") == ['', 'a', '', '', 'c']
except AssertionError: except AssertionError:
raise TestFailed, "re.split" raise TestFailed, "re.split"
...@@ -130,16 +169,55 @@ try: ...@@ -130,16 +169,55 @@ try:
except AssertionError: except AssertionError:
raise TestFailed, "qualified re.split" raise TestFailed, "qualified re.split"
try:
# No groups at all
m = re.match('a', 'a') ; assert m.groups() == ()
# A single group
m = re.match('(a)', 'a') ; assert m.groups() == ('a',)
pat = re.compile('((a)|(b))(c)?')
assert pat.match('a').groups() == ('a', 'a', None, None)
assert pat.match('b').groups() == ('b', None, 'b', None)
assert pat.match('ac').groups() == ('a', 'a', None, 'c')
assert pat.match('bc').groups() == ('b', None, 'b', 'c')
except AssertionError:
raise TestFailed, "match .groups() method"
try:
# A single group
m = re.match('(a)', 'a')
assert m.group(0) == 'a' ; assert m.group(0) == 'a'
assert m.group(1) == 'a' ; assert m.group(1, 1) == ('a', 'a')
pat = re.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"
try:
p=""
for i in range(0, 256):
p = p + chr(i)
assert re.match(re.escape(chr(i)), chr(i)) != None
assert re.match(re.escape(chr(i)), chr(i)).span() == (0,1)
pat=re.compile( re.escape(p) )
assert pat.match(p) != None
assert pat.match(p).span() == (0,256)
except AssertionError:
raise TestFailed, "re.escape"
if verbose: if verbose:
print 'Pickling a RegexObject instance' print 'Pickling a RegexObject instance'
import pickle
pat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
s = pickle.dumps(pat)
pat = pickle.loads(s)
if verbose: import pickle
print 'Running tests on re.split' pat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
s = pickle.dumps(pat)
pat = pickle.loads(s)
try: try:
assert re.I == re.IGNORECASE assert re.I == re.IGNORECASE
assert re.L == re.LOCALE assert re.L == re.LOCALE
...@@ -156,11 +234,13 @@ for flags in [re.I, re.M, re.X, re.S, re.L]: ...@@ -156,11 +234,13 @@ for flags in [re.I, re.M, re.X, re.S, re.L]:
print 'Exception raised on flag', flags print 'Exception raised on flag', flags
from re_tests import * from re_tests import *
if verbose: if verbose:
print 'Running re_tests test suite' print 'Running re_tests test suite'
else: else:
# To save time, only run the first and last 10 tests # To save time, only run the first and last 10 tests
pass #tests = tests[:10] + tests[-10:] #tests = tests[:10] + tests[-10:]
pass
for t in tests: for t in tests:
sys.stdout.flush() sys.stdout.flush()
...@@ -180,7 +260,7 @@ for t in tests: ...@@ -180,7 +260,7 @@ for t in tests:
print '=== Syntax error:', t print '=== Syntax error:', t
except KeyboardInterrupt: raise KeyboardInterrupt except KeyboardInterrupt: raise KeyboardInterrupt
except: except:
print '*** Unexpected error ***' print '*** Unexpected error ***', t
if verbose: if verbose:
traceback.print_exc(file=sys.stdout) traceback.print_exc(file=sys.stdout)
else: else:
...@@ -250,4 +330,3 @@ for t in tests: ...@@ -250,4 +330,3 @@ for t in tests:
result=obj.search(s) result=obj.search(s)
if result==None: if result==None:
print '=== Fails on locale-sensitive match', t print '=== Fails on locale-sensitive match', t
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment