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

Skip to content

Commit 8430c58

Browse files
committed
AMK's latest
1 parent 07bcd99 commit 8430c58

3 files changed

Lines changed: 106 additions & 31 deletions

File tree

Lib/re.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ def escape(pattern):
6666
alphanum=string.letters+'_'+string.digits
6767
for char in pattern:
6868
if char not in alphanum:
69-
if char == '\000': result.append(r'\000')
70-
else: result.append('\\' + char)
69+
if char=='\000': result.append('\\000')
70+
else: result.append('\\'+char)
7171
else: result.append(char)
7272
return string.join(result, '')
7373

@@ -132,9 +132,9 @@ def sub(self, repl, string, count=0):
132132
def subn(self, repl, source, count=0):
133133
"""Return a 2-tuple containing (new_string, number).
134134
new_string is the string obtained by replacing the leftmost
135-
non-overlapping occurrences of the pattern in string by the
136-
replacement repl. number is the number of substitutions that
137-
were made."""
135+
non-overlapping occurrences of the pattern in the source
136+
string by the replacement repl. number is the number of
137+
substitutions that were made."""
138138

139139
if count < 0:
140140
raise error, "negative substitution count"
@@ -174,7 +174,7 @@ def subn(self, repl, source, count=0):
174174
return (string.join(results, ''), n)
175175

176176
def split(self, source, maxsplit=0):
177-
"""Split \var{string} by the occurrences of the pattern,
177+
"""Split the \var{source} string by the occurrences of the pattern,
178178
returning a list containing the resulting substrings."""
179179

180180
if maxsplit < 0:

Lib/test/re_tests.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- mode: python -*-
33
# $Id$
44

5-
# Re test suite and benchmark suite v1.5b2
5+
# Re test suite and benchmark suite v1.5
66

77
# The 3 possible outcomes for each pattern
88
[SUCCEED, FAIL, SYNTAX_ERROR] = range(3)
@@ -62,23 +62,20 @@
6262

6363
('(?P<foo_123>a)', 'a', SUCCEED, 'g1', 'a'),
6464
('(?P<foo_123>a)(?P=foo_123)', 'aa', SUCCEED, 'g1', 'a'),
65-
65+
6666
# Test octal escapes
67-
('\\1', 'a', SYNTAX_ERROR),
67+
('\\1', 'a', SYNTAX_ERROR), # Backreference
68+
('[\\1]', '\1', SUCCEED, 'found', '\1'), # Character
6869
('\\09', chr(0) + '9', SUCCEED, 'found', chr(0) + '9'),
6970
('\\141', 'a', SUCCEED, 'found', 'a'),
7071
('(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)\\119', 'abcdefghijklk9', SUCCEED, 'found+"-"+g11', 'abcdefghijklk9-k'),
7172

72-
# Test that a literal \0 is handled everywhere
73-
('\0', '\0', SUCCEED, 'found', '\0'),
73+
# Test \0 is handled everywhere
7474
(r'\0', '\0', SUCCEED, 'found', '\0'),
75-
('[\0a]', '\0', SUCCEED, 'found', '\0'),
76-
('[a\0]', '\0', SUCCEED, 'found', '\0'),
77-
('[^a\0]', '\0', FAIL),
7875
(r'[\0a]', '\0', SUCCEED, 'found', '\0'),
7976
(r'[a\0]', '\0', SUCCEED, 'found', '\0'),
8077
(r'[^a\0]', '\0', FAIL),
81-
78+
8279
# Test various letter escapes
8380
(r'\a[\b]\f\n\r\t\v', '\a\b\f\n\r\t\v', SUCCEED, 'found', '\a\b\f\n\r\t\v'),
8481
(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 @@
103100
('(?s)a.{4,5}b', 'acc\nccb', SUCCEED, 'found', 'acc\nccb'),
104101
('(?s)a.b', 'a\nb', SUCCEED, 'found', 'a\nb'),
105102

103+
(')', '', SYNTAX_ERROR), # Unmatched right bracket
104+
('', '', SUCCEED, 'found', ''), # Empty pattern
106105
('abc', 'abc', SUCCEED, 'found', 'abc'),
107106
('abc', 'xbc', FAIL),
108107
('abc', 'axc', FAIL),
@@ -393,9 +392,6 @@
393392
('(.*)c(.*)', 'abcde', SUCCEED, 'found+"-"+g1+"-"+g2', 'abcde-ab-de'),
394393
('\\((.*), (.*)\\)', '(a, b)', SUCCEED, 'g2+"-"+g1', 'b-a'),
395394
('[k]', 'ab', FAIL),
396-
# XXX
397-
# ('abcd', 'abcd', SUCCEED, 'found+"-"+\\found+"-"+\\\\found', 'abcd-$&-\\abcd'),
398-
# ('a(bc)d', 'abcd', SUCCEED, 'g1+"-"+\\g1+"-"+\\\\g1', 'bc-$1-\\bc'),
399395
('a[-]?c', 'ac', SUCCEED, 'found', 'ac'),
400396
('(abc)\\1', 'abcabc', SUCCEED, 'g1', 'abc'),
401397
('([a-c]*)\\1', 'abcabc', SUCCEED, 'g1', 'abc'),

Lib/test/test_re.py

Lines changed: 92 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,36 @@
22
# -*- mode: python -*-
33
# $Id$
44

5+
import sys
6+
sys.path=['.']+sys.path
7+
58
from test_support import verbose, TestFailed
69
import re
710
import sys, os, string, traceback
811

912
# Misc tests from Tim Peters' re.doc
1013

14+
if verbose:
15+
print 'Running tests on re.search and re.match'
16+
17+
try:
18+
assert re.search('x*', 'axx').span(0) == (0, 0)
19+
assert re.search('x*', 'axx').span() == (0, 0)
20+
assert re.search('x+', 'axx').span(0) == (1, 3)
21+
assert re.search('x+', 'axx').span() == (1, 3)
22+
assert re.search('x', 'aaa') == None
23+
except:
24+
raise TestFailed, "re.search"
25+
26+
try:
27+
assert re.match('a*', 'xxx').span(0) == (0, 0)
28+
assert re.match('a*', 'xxx').span() == (0, 0)
29+
assert re.match('x*', 'xxxa').span(0) == (0, 3)
30+
assert re.match('x*', 'xxxa').span() == (0, 3)
31+
assert re.match('a+', 'xxx') == None
32+
except:
33+
raise TestFailed, "re.search"
34+
1135
if verbose:
1236
print 'Running tests on re.sub'
1337

@@ -19,25 +43,30 @@ def bump_num(matchobj):
1943
return str(int_value + 1)
2044

2145
assert re.sub(r'\d+', bump_num, '08.2 -2 23x99y') == '9.3 -3 24x100y'
46+
assert re.sub(r'\d+', bump_num, '08.2 -2 23x99y', 3) == '9.3 -3 23x99y'
2247

2348
assert re.sub('.', lambda m: r"\n", 'x') == '\\n'
2449
assert re.sub('.', r"\n", 'x') == '\n'
2550

2651
s = r"\1\1"
2752
assert re.sub('(.)', s, 'x') == 'xx'
28-
assert re.sub('(.)', re.escape(s), 'x') == s
53+
assert re.sub('(.)', re.escape(s), 'x') == s
2954
assert re.sub('(.)', lambda m: s, 'x') == s
3055

3156
assert re.sub('(?P<a>x)', '\g<a>\g<a>', 'xx') == 'xxxx'
57+
assert re.sub('(?P<a>x)', '\g<a>\g<1>', 'xx') == 'xxxx'
3258
assert re.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx') == 'xxxx'
59+
assert re.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx') == 'xxxx'
3360

34-
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'
61+
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'
3562
assert re.sub('a', '\t\n\v\r\f\a', 'a') == '\t\n\v\r\f\a'
3663
assert re.sub('a', '\t\n\v\r\f\a', 'a') == (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7))
3764

65+
assert re.sub('^\s*', 'X', 'test') == 'Xtest'
3866
except AssertionError:
3967
raise TestFailed, "re.sub"
4068

69+
4170
try:
4271
assert re.sub('a', 'b', 'aaaaa') == 'bbbbb'
4372
assert re.sub('a', 'b', 'aaaaa', 1) == 'baaaa'
@@ -75,6 +104,13 @@ def bump_num(matchobj):
75104
else:
76105
raise TestFailed, "symbolic reference"
77106

107+
try:
108+
re.sub('(?P<a>x)', '\g<1a1>', 'xx')
109+
except re.error, reason:
110+
pass
111+
else:
112+
raise TestFailed, "symbolic reference"
113+
78114
try:
79115
re.sub('(?P<a>x)', '\g<ab>', 'xx')
80116
except IndexError, reason:
@@ -104,9 +140,13 @@ def bump_num(matchobj):
104140
assert re.subn("b+", "x", "bbbb BBBB") == ('x BBBB', 1)
105141
assert re.subn("b+", "x", "xyz") == ('xyz', 0)
106142
assert re.subn("b*", "x", "xyz") == ('xxxyxzx', 4)
143+
assert re.subn("b*", "x", "xyz", 2) == ('xxxyz', 2)
107144
except AssertionError:
108145
raise TestFailed, "re.subn"
109146

147+
if verbose:
148+
print 'Running tests on re.split'
149+
110150
try:
111151
assert re.split(":", ":a:b::c") == ['', 'a', 'b', '', 'c']
112152
assert re.split(":*", ":a:b::c") == ['', 'a', 'b', 'c']
@@ -117,7 +157,6 @@ def bump_num(matchobj):
117157
assert re.split("(b)|(:+)", ":a:b::c") == \
118158
['', None, ':', 'a', None, ':', '', 'b', None, '', None, '::', 'c']
119159
assert re.split("(?:b)|(?::+)", ":a:b::c") == ['', 'a', '', '', 'c']
120-
121160
except AssertionError:
122161
raise TestFailed, "re.split"
123162

@@ -130,16 +169,55 @@ def bump_num(matchobj):
130169
except AssertionError:
131170
raise TestFailed, "qualified re.split"
132171

172+
try:
173+
# No groups at all
174+
m = re.match('a', 'a') ; assert m.groups() == ()
175+
# A single group
176+
m = re.match('(a)', 'a') ; assert m.groups() == ('a',)
177+
178+
pat = re.compile('((a)|(b))(c)?')
179+
assert pat.match('a').groups() == ('a', 'a', None, None)
180+
assert pat.match('b').groups() == ('b', None, 'b', None)
181+
assert pat.match('ac').groups() == ('a', 'a', None, 'c')
182+
assert pat.match('bc').groups() == ('b', None, 'b', 'c')
183+
except AssertionError:
184+
raise TestFailed, "match .groups() method"
185+
186+
try:
187+
# A single group
188+
m = re.match('(a)', 'a')
189+
assert m.group(0) == 'a' ; assert m.group(0) == 'a'
190+
assert m.group(1) == 'a' ; assert m.group(1, 1) == ('a', 'a')
191+
192+
pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
193+
assert pat.match('a').group(1, 2, 3) == ('a', None, None)
194+
assert pat.match('b').group('a1', 'b2', 'c3') == (None, 'b', None)
195+
assert pat.match('ac').group(1, 'b2', 3) == ('a', None, 'c')
196+
except AssertionError:
197+
raise TestFailed, "match .group() method"
198+
199+
try:
200+
p=""
201+
for i in range(0, 256):
202+
p = p + chr(i)
203+
assert re.match(re.escape(chr(i)), chr(i)) != None
204+
assert re.match(re.escape(chr(i)), chr(i)).span() == (0,1)
205+
206+
pat=re.compile( re.escape(p) )
207+
assert pat.match(p) != None
208+
assert pat.match(p).span() == (0,256)
209+
except AssertionError:
210+
raise TestFailed, "re.escape"
211+
212+
133213
if verbose:
134214
print 'Pickling a RegexObject instance'
135-
import pickle
136-
pat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
137-
s = pickle.dumps(pat)
138-
pat = pickle.loads(s)
139215

140-
if verbose:
141-
print 'Running tests on re.split'
142-
216+
import pickle
217+
pat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
218+
s = pickle.dumps(pat)
219+
pat = pickle.loads(s)
220+
143221
try:
144222
assert re.I == re.IGNORECASE
145223
assert re.L == re.LOCALE
@@ -156,11 +234,13 @@ def bump_num(matchobj):
156234
print 'Exception raised on flag', flags
157235

158236
from re_tests import *
237+
159238
if verbose:
160239
print 'Running re_tests test suite'
161240
else:
162241
# To save time, only run the first and last 10 tests
163-
pass #tests = tests[:10] + tests[-10:]
242+
#tests = tests[:10] + tests[-10:]
243+
pass
164244

165245
for t in tests:
166246
sys.stdout.flush()
@@ -180,7 +260,7 @@ def bump_num(matchobj):
180260
print '=== Syntax error:', t
181261
except KeyboardInterrupt: raise KeyboardInterrupt
182262
except:
183-
print '*** Unexpected error ***'
263+
print '*** Unexpected error ***', t
184264
if verbose:
185265
traceback.print_exc(file=sys.stdout)
186266
else:
@@ -250,4 +330,3 @@ def bump_num(matchobj):
250330
result=obj.search(s)
251331
if result==None:
252332
print '=== Fails on locale-sensitive match', t
253-

0 commit comments

Comments
 (0)