| 1 |
import sys, os, py |
| 2 |
from rpython.rlib.rsre.test.test_match import get_code |
| 3 |
from rpython.rlib.rsre import rsre_re as re |
| 4 |
|
| 5 |
|
| 6 |
class TestRe: |
| 7 |
|
| 8 |
def test_search_star_plus(self): |
| 9 |
assert re.search('x*', 'axx').span(0) == (0, 0) |
| 10 |
assert re.search('x*', 'axx').span() == (0, 0) |
| 11 |
assert re.search('x+', 'axx').span(0) == (1, 3) |
| 12 |
assert re.search('x+', 'axx').span() == (1, 3) |
| 13 |
assert re.search('x', 'aaa') == None |
| 14 |
assert re.match('a*', 'xxx').span(0) == (0, 0) |
| 15 |
assert re.match('a*', 'xxx').span() == (0, 0) |
| 16 |
assert re.match('x*', 'xxxa').span(0) == (0, 3) |
| 17 |
assert re.match('x*', 'xxxa').span() == (0, 3) |
| 18 |
assert re.match('a+', 'xxx') == None |
| 19 |
|
| 20 |
def bump_num(self, matchobj): |
| 21 |
int_value = int(matchobj.group(0)) |
| 22 |
return str(int_value + 1) |
| 23 |
|
| 24 |
def test_basic_re_sub(self): |
| 25 |
assert re.sub("(?i)b+", "x", "bbbb BBBB") == 'x x' |
| 26 |
assert re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y') == ( |
| 27 |
'9.3 -3 24x100y') |
| 28 |
assert re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y', 3) == ( |
| 29 |
'9.3 -3 23x99y') |
| 30 |
|
| 31 |
assert re.sub('.', lambda m: r"\n", 'x') == '\\n' |
| 32 |
assert re.sub('.', r"\n", 'x') == '\n' |
| 33 |
|
| 34 |
s = r"\1\1" |
| 35 |
assert re.sub('(.)', s, 'x') == 'xx' |
| 36 |
assert re.sub('(.)', re.escape(s), 'x') == s |
| 37 |
assert re.sub('(.)', lambda m: s, 'x') == s |
| 38 |
|
| 39 |
assert re.sub('(?P<a>x)', '\g<a>\g<a>', 'xx') == 'xxxx' |
| 40 |
assert re.sub('(?P<a>x)', '\g<a>\g<1>', 'xx') == 'xxxx' |
| 41 |
assert re.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx') == 'xxxx' |
| 42 |
assert re.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx') == 'xxxx' |
| 43 |
|
| 44 |
assert re.sub('a',r'\t\n\v\r\f\a\b\B\Z\a\A\w\W\s\S\d\D','a') == ( |
| 45 |
'\t\n\v\r\f\a\b\\B\\Z\a\\A\\w\\W\\s\\S\\d\\D') |
| 46 |
assert re.sub('a', '\t\n\v\r\f\a', 'a') == '\t\n\v\r\f\a' |
| 47 |
assert re.sub('a', '\t\n\v\r\f\a', 'a') == ( |
| 48 |
(chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7))) |
| 49 |
|
| 50 |
assert re.sub('^\s*', 'X', 'test') == 'Xtest' |
| 51 |
|
| 52 |
def test_bug_449964(self): |
| 53 |
|
| 54 |
assert re.sub(r'(?P<unk>x)', '\g<1>\g<1>\\b', 'xx') == ( |
| 55 |
'xx\bxx\b') |
| 56 |
|
| 57 |
def test_bug_449000(self): |
| 58 |
|
| 59 |
assert re.sub(r'\r\n', r'\n', 'abc\r\ndef\r\n') == ( |
| 60 |
'abc\ndef\n') |
| 61 |
assert re.sub('\r\n', r'\n', 'abc\r\ndef\r\n') == ( |
| 62 |
'abc\ndef\n') |
| 63 |
assert re.sub(r'\r\n', '\n', 'abc\r\ndef\r\n') == ( |
| 64 |
'abc\ndef\n') |
| 65 |
assert re.sub('\r\n', '\n', 'abc\r\ndef\r\n') == ( |
| 66 |
'abc\ndef\n') |
| 67 |
|
| 68 |
def test_bug_1140(self): |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
for x in 'x', u'x': |
| 76 |
for y in 'y', u'y': |
| 77 |
z = re.sub(x, y, u'') |
| 78 |
assert z == u'' |
| 79 |
assert type(z) == unicode |
| 80 |
|
| 81 |
z = re.sub(x, y, '') |
| 82 |
assert z == '' |
| 83 |
assert type(z) == str |
| 84 |
|
| 85 |
z = re.sub(x, y, unicode(x)) |
| 86 |
assert z == y |
| 87 |
assert type(z) == unicode |
| 88 |
|
| 89 |
z = re.sub(x, y, str(x)) |
| 90 |
assert z == y |
| 91 |
assert type(z) == type(y) |
| 92 |
|
| 93 |
def test_sub_template_numeric_escape(self): |
| 94 |
|
| 95 |
assert re.sub('x', r'\0', 'x') == '\0' |
| 96 |
assert re.sub('x', r'\000', 'x') == '\000' |
| 97 |
assert re.sub('x', r'\001', 'x') == '\001' |
| 98 |
assert re.sub('x', r'\008', 'x') == '\0' + '8' |
| 99 |
assert re.sub('x', r'\009', 'x') == '\0' + '9' |
| 100 |
assert re.sub('x', r'\111', 'x') == '\111' |
| 101 |
assert re.sub('x', r'\117', 'x') == '\117' |
| 102 |
|
| 103 |
assert re.sub('x', r'\1111', 'x') == '\1111' |
| 104 |
assert re.sub('x', r'\1111', 'x') == '\111' + '1' |
| 105 |
|
| 106 |
assert re.sub('x', r'\00', 'x') == '\x00' |
| 107 |
assert re.sub('x', r'\07', 'x') == '\x07' |
| 108 |
assert re.sub('x', r'\08', 'x') == '\0' + '8' |
| 109 |
assert re.sub('x', r'\09', 'x') == '\0' + '9' |
| 110 |
assert re.sub('x', r'\0a', 'x') == '\0' + 'a' |
| 111 |
|
| 112 |
assert re.sub('x', r'\400', 'x') == '\0' |
| 113 |
assert re.sub('x', r'\777', 'x') == '\377' |
| 114 |
|
| 115 |
py.test.raises(re.error, re.sub, 'x', r'\1', 'x') |
| 116 |
py.test.raises(re.error, re.sub, 'x', r'\8', 'x') |
| 117 |
py.test.raises(re.error, re.sub, 'x', r'\9', 'x') |
| 118 |
py.test.raises(re.error, re.sub, 'x', r'\11', 'x') |
| 119 |
py.test.raises(re.error, re.sub, 'x', r'\18', 'x') |
| 120 |
py.test.raises(re.error, re.sub, 'x', r'\1a', 'x') |
| 121 |
py.test.raises(re.error, re.sub, 'x', r'\90', 'x') |
| 122 |
py.test.raises(re.error, re.sub, 'x', r'\99', 'x') |
| 123 |
py.test.raises(re.error, re.sub, 'x', r'\118', 'x') |
| 124 |
py.test.raises(re.error, re.sub, 'x', r'\11a', 'x') |
| 125 |
py.test.raises(re.error, re.sub, 'x', r'\181', 'x') |
| 126 |
py.test.raises(re.error, re.sub, 'x', r'\800', 'x') |
| 127 |
|
| 128 |
|
| 129 |
assert re.sub('(((((((((((x)))))))))))', r'\11', 'x') == 'x' |
| 130 |
assert re.sub('((((((((((y))))))))))(.)', r'\118', 'xyz') == ( |
| 131 |
'xz8') |
| 132 |
assert re.sub('((((((((((y))))))))))(.)', r'\11a', 'xyz') == ( |
| 133 |
'xza') |
| 134 |
|
| 135 |
def test_qualified_re_sub(self): |
| 136 |
assert re.sub('a', 'b', 'aaaaa') == 'bbbbb' |
| 137 |
assert re.sub('a', 'b', 'aaaaa', 1) == 'baaaa' |
| 138 |
|
| 139 |
def test_bug_114660(self): |
| 140 |
assert re.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello there') == ( |
| 141 |
'hello there') |
| 142 |
|
| 143 |
def test_bug_462270(self): |
| 144 |
|
| 145 |
assert re.sub('x*', '-', 'abxd') == '-a-b-d-' |
| 146 |
assert re.sub('x+', '-', 'abxd') == 'ab-d' |
| 147 |
|
| 148 |
def test_symbolic_refs(self): |
| 149 |
py.test.raises(re.error, re.sub, '(?P<a>x)', '\g<a', 'xx') |
| 150 |
py.test.raises(re.error, re.sub, '(?P<a>x)', '\g<', 'xx') |
| 151 |
py.test.raises(re.error, re.sub, '(?P<a>x)', '\g', 'xx') |
| 152 |
py.test.raises(re.error, re.sub, '(?P<a>x)', '\g<a a>', 'xx') |
| 153 |
py.test.raises(re.error, re.sub, '(?P<a>x)', '\g<1a1>', 'xx') |
| 154 |
py.test.raises(IndexError, re.sub, '(?P<a>x)', '\g<ab>', 'xx') |
| 155 |
py.test.raises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\g<b>', 'xx') |
| 156 |
py.test.raises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\\2', 'xx') |
| 157 |
py.test.raises(re.error, re.sub, '(?P<a>x)', '\g<-1>', 'xx') |
| 158 |
|
| 159 |
def test_re_subn(self): |
| 160 |
assert re.subn("(?i)b+", "x", "bbbb BBBB") == ('x x', 2) |
| 161 |
assert re.subn("b+", "x", "bbbb BBBB") == ('x BBBB', 1) |
| 162 |
assert re.subn("b+", "x", "xyz") == ('xyz', 0) |
| 163 |
assert re.subn("b*", "x", "xyz") == ('xxxyxzx', 4) |
| 164 |
assert re.subn("b*", "x", "xyz", 2) == ('xxxyz', 2) |
| 165 |
|
| 166 |
def test_re_split(self): |
| 167 |
assert re.split(":", ":a:b::c") == ['', 'a', 'b', '', 'c'] |
| 168 |
assert re.split(":*", ":a:b::c") == ['', 'a', 'b', 'c'] |
| 169 |
assert re.split("(:*)", ":a:b::c") == ( |
| 170 |
['', ':', 'a', ':', 'b', '::', 'c']) |
| 171 |
assert re.split("(?::*)", ":a:b::c") == ['', 'a', 'b', 'c'] |
| 172 |
assert re.split("(:)*", ":a:b::c") == ( |
| 173 |
['', ':', 'a', ':', 'b', ':', 'c']) |
| 174 |
assert re.split("([b:]+)", ":a:b::c") == ( |
| 175 |
['', ':', 'a', ':b::', 'c']) |
| 176 |
assert re.split("(b)|(:+)", ":a:b::c") == ( |
| 177 |
['', None, ':', 'a', None, ':', '', 'b', None, '', |
| 178 |
None, '::', 'c']) |
| 179 |
assert re.split("(?:b)|(?::+)", ":a:b::c") == ( |
| 180 |
['', 'a', '', '', 'c']) |
| 181 |
|
| 182 |
def test_qualified_re_split(self): |
| 183 |
assert re.split(":", ":a:b::c", 2) == ['', 'a', 'b::c'] |
| 184 |
assert re.split(':', 'a:b:c:d', 2) == ['a', 'b', 'c:d'] |
| 185 |
assert re.split("(:)", ":a:b::c", 2) == ( |
| 186 |
['', ':', 'a', ':', 'b::c']) |
| 187 |
assert re.split("(:*)", ":a:b::c", 2) == ( |
| 188 |
['', ':', 'a', ':', 'b::c']) |
| 189 |
|
| 190 |
def test_re_findall(self): |
| 191 |
assert re.findall(":+", "abc") == [] |
| 192 |
assert re.findall(":+", "a:b::c:::d") == [":", "::", ":::"] |
| 193 |
assert re.findall("(:+)", "a:b::c:::d") == [":", "::", ":::"] |
| 194 |
|
| 195 |
def test_re_findall_2(self): |
| 196 |
py.test.skip("findall() returning groups is not RPython") |
| 197 |
assert re.findall("(:)(:*)", "a:b::c:::d") == [(":", ""), |
| 198 |
(":", ":"), |
| 199 |
(":", "::")] |
| 200 |
|
| 201 |
def test_bug_117612(self): |
| 202 |
py.test.skip("findall() returning groups is not RPython") |
| 203 |
assert re.findall(r"(a|(b))", "aba") == ( |
| 204 |
[("a", ""),("b", "b"),("a", "")]) |
| 205 |
|
| 206 |
def test_re_match(self): |
| 207 |
assert re.match('a', 'a').groups() == () |
| 208 |
assert re.match('(a)', 'a').groups() == ('a',) |
| 209 |
assert re.match(r'(a)', 'a').group(0) == 'a' |
| 210 |
assert re.match(r'(a)', 'a').group(1) == 'a' |
| 211 |
|
| 212 |
|
| 213 |
pat = re.compile('((a)|(b))(c)?') |
| 214 |
assert pat.match('a').groups() == ('a', 'a', None, None) |
| 215 |
assert pat.match('b').groups() == ('b', None, 'b', None) |
| 216 |
assert pat.match('ac').groups() == ('a', 'a', None, 'c') |
| 217 |
assert pat.match('bc').groups() == ('b', None, 'b', 'c') |
| 218 |
assert pat.match('bc').groups("") == ('b', "", 'b', 'c') |
| 219 |
|
| 220 |
|
| 221 |
m = re.match('(a)', 'a') |
| 222 |
assert m.group(0) == 'a' |
| 223 |
assert m.group(0) == 'a' |
| 224 |
assert m.group(1) == 'a' |
| 225 |
|
| 226 |
|
| 227 |
pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?') |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
def test_bug_923(self): |
| 234 |
|
| 235 |
assert re.match(r'a(?=(b))?', "ab").groups() == ("b",) |
| 236 |
assert re.match(r'(a(?=(b))?)', "ab").groups() == ('a', 'b') |
| 237 |
assert re.match(r'(a)(?=(b))?', "ab").groups() == ('a', 'b') |
| 238 |
assert re.match(r'(?P<g1>a)(?=(?P<g2>b))?', "ab").groupdict() == {'g1': 'a', 'g2': 'b'} |
| 239 |
|
| 240 |
def test_re_groupref_exists(self): |
| 241 |
assert re.match('^(\()?([^()]+)(?(1)\))$', '(a)').groups() == ( |
| 242 |
('(', 'a')) |
| 243 |
assert re.match('^(\()?([^()]+)(?(1)\))$', 'a').groups() == ( |
| 244 |
(None, 'a')) |
| 245 |
assert re.match('^(\()?([^()]+)(?(1)\))$', 'a)') == None |
| 246 |
assert re.match('^(\()?([^()]+)(?(1)\))$', '(a') == None |
| 247 |
assert re.match('^(?:(a)|c)((?(1)b|d))$', 'ab').groups() == ( |
| 248 |
('a', 'b')) |
| 249 |
assert re.match('^(?:(a)|c)((?(1)b|d))$', 'cd').groups() == ( |
| 250 |
(None, 'd')) |
| 251 |
assert re.match('^(?:(a)|c)((?(1)|d))$', 'cd').groups() == ( |
| 252 |
(None, 'd')) |
| 253 |
assert re.match('^(?:(a)|c)((?(1)|d))$', 'a').groups() == ( |
| 254 |
('a', '')) |
| 255 |
|
| 256 |
|
| 257 |
p = re.compile('(?P<g1>a)(?P<g2>b)?((?(g2)c|d))') |
| 258 |
assert p.match('abc').groups() == ( |
| 259 |
('a', 'b', 'c')) |
| 260 |
assert p.match('ad').groups() == ( |
| 261 |
('a', None, 'd')) |
| 262 |
assert p.match('abd') == None |
| 263 |
assert p.match('ac') == None |
| 264 |
|
| 265 |
|
| 266 |
def test_re_groupref(self): |
| 267 |
assert re.match(r'^(\|)?([^()]+)\1$', '|a|').groups() == ( |
| 268 |
('|', 'a')) |
| 269 |
assert re.match(r'^(\|)?([^()]+)\1?$', 'a').groups() == ( |
| 270 |
(None, 'a')) |
| 271 |
assert re.match(r'^(\|)?([^()]+)\1$', 'a|') == None |
| 272 |
assert re.match(r'^(\|)?([^()]+)\1$', '|a') == None |
| 273 |
assert re.match(r'^(?:(a)|c)(\1)$', 'aa').groups() == ( |
| 274 |
('a', 'a')) |
| 275 |
assert re.match(r'^(?:(a)|c)(\1)?$', 'c').groups() == ( |
| 276 |
(None, None)) |
| 277 |
|
| 278 |
def test_groupdict(self): |
| 279 |
assert re.match('(?P<first>first) (?P<second>second)', |
| 280 |
'first second').groupdict() == ( |
| 281 |
{'first':'first', 'second':'second'}) |
| 282 |
|
| 283 |
def test_expand(self): |
| 284 |
assert (re.match("(?P<first>first) (?P<second>second)", |
| 285 |
"first second") |
| 286 |
.expand(r"\2 \1 \g<second> \g<first>")) == ( |
| 287 |
"second first second first") |
| 288 |
|
| 289 |
def test_repeat_minmax(self): |
| 290 |
assert re.match("^(\w){1}$", "abc") == None |
| 291 |
assert re.match("^(\w){1}?$", "abc") == None |
| 292 |
assert re.match("^(\w){1,2}$", "abc") == None |
| 293 |
assert re.match("^(\w){1,2}?$", "abc") == None |
| 294 |
|
| 295 |
assert re.match("^(\w){3}$", "abc").group(1) == "c" |
| 296 |
assert re.match("^(\w){1,3}$", "abc").group(1) == "c" |
| 297 |
assert re.match("^(\w){1,4}$", "abc").group(1) == "c" |
| 298 |
assert re.match("^(\w){3,4}?$", "abc").group(1) == "c" |
| 299 |
assert re.match("^(\w){3}?$", "abc").group(1) == "c" |
| 300 |
assert re.match("^(\w){1,3}?$", "abc").group(1) == "c" |
| 301 |
assert re.match("^(\w){1,4}?$", "abc").group(1) == "c" |
| 302 |
assert re.match("^(\w){3,4}?$", "abc").group(1) == "c" |
| 303 |
|
| 304 |
assert re.match("^x{1}$", "xxx") == None |
| 305 |
assert re.match("^x{1}?$", "xxx") == None |
| 306 |
assert re.match("^x{1,2}$", "xxx") == None |
| 307 |
assert re.match("^x{1,2}?$", "xxx") == None |
| 308 |
|
| 309 |
assert re.match("^x{3}$", "xxx") != None |
| 310 |
assert re.match("^x{1,3}$", "xxx") != None |
| 311 |
assert re.match("^x{1,4}$", "xxx") != None |
| 312 |
assert re.match("^x{3,4}?$", "xxx") != None |
| 313 |
assert re.match("^x{3}?$", "xxx") != None |
| 314 |
assert re.match("^x{1,3}?$", "xxx") != None |
| 315 |
assert re.match("^x{1,4}?$", "xxx") != None |
| 316 |
assert re.match("^x{3,4}?$", "xxx") != None |
| 317 |
|
| 318 |
assert re.match("^x{}$", "xxx") == None |
| 319 |
assert re.match("^x{}$", "x{}") != None |
| 320 |
|
| 321 |
def test_getattr(self): |
| 322 |
assert re.match("(a)", "a").pos == 0 |
| 323 |
assert re.match("(a)", "a").endpos == 1 |
| 324 |
assert re.match("(a)", "a").string == "a" |
| 325 |
assert re.match("(a)", "a").regs == ((0, 1), (0, 1)) |
| 326 |
assert re.match("(a)", "a").re != None |
| 327 |
|
| 328 |
def test_special_escapes(self): |
| 329 |
assert re.search(r"\b(b.)\b", |
| 330 |
"abcd abc bcd bx").group(1) == "bx" |
| 331 |
assert re.search(r"\B(b.)\B", |
| 332 |
"abc bcd bc abxd").group(1) == "bx" |
| 333 |
assert re.search(r"\b(b.)\b", |
| 334 |
"abcd abc bcd bx", re.LOCALE).group(1) == "bx" |
| 335 |
assert re.search(r"\B(b.)\B", |
| 336 |
"abc bcd bc abxd", re.LOCALE).group(1) == "bx" |
| 337 |
assert re.search(r"\b(b.)\b", |
| 338 |
"abcd abc bcd bx", re.UNICODE).group(1) == "bx" |
| 339 |
assert re.search(r"\B(b.)\B", |
| 340 |
"abc bcd bc abxd", re.UNICODE).group(1) == "bx" |
| 341 |
assert re.search(r"^abc$", "\nabc\n", re.M).group(0) == "abc" |
| 342 |
assert re.search(r"^\Aabc\Z$", "abc", re.M).group(0) == "abc" |
| 343 |
assert re.search(r"^\Aabc\Z$", "\nabc\n", re.M) == None |
| 344 |
assert re.search(r"\b(b.)\b", |
| 345 |
u"abcd abc bcd bx").group(1) == "bx" |
| 346 |
assert re.search(r"\B(b.)\B", |
| 347 |
u"abc bcd bc abxd").group(1) == "bx" |
| 348 |
assert re.search(r"^abc$", u"\nabc\n", re.M).group(0) == "abc" |
| 349 |
assert re.search(r"^\Aabc\Z$", u"abc", re.M).group(0) == "abc" |
| 350 |
assert re.search(r"^\Aabc\Z$", u"\nabc\n", re.M) == None |
| 351 |
assert re.search(r"\d\D\w\W\s\S", |
| 352 |
"1aa! a").group(0) == "1aa! a" |
| 353 |
assert re.search(r"\d\D\w\W\s\S", |
| 354 |
"1aa! a", re.LOCALE).group(0) == "1aa! a" |
| 355 |
assert re.search(r"\d\D\w\W\s\S", |
| 356 |
"1aa! a", re.UNICODE).group(0) == "1aa! a" |
| 357 |
|
| 358 |
def test_ignore_case(self): |
| 359 |
assert re.match("abc", "ABC", re.I).group(0) == "ABC" |
| 360 |
assert re.match("abc", u"ABC", re.I).group(0) == "ABC" |
| 361 |
|
| 362 |
def test_bigcharset(self): |
| 363 |
assert re.match(u"([\u2222\u2223])", |
| 364 |
u"\u2222").group(1) == u"\u2222" |
| 365 |
assert re.match(u"([\u2222\u2223])", |
| 366 |
u"\u2222", re.UNICODE).group(1) == u"\u2222" |
| 367 |
|
| 368 |
def test_anyall(self): |
| 369 |
assert re.match("a.b", "a\nb", re.DOTALL).group(0) == ( |
| 370 |
"a\nb") |
| 371 |
assert re.match("a.*b", "a\n\nb", re.DOTALL).group(0) == ( |
| 372 |
"a\n\nb") |
| 373 |
|
| 374 |
def test_non_consuming(self): |
| 375 |
assert re.match("(a(?=\s[^a]))", "a b").group(1) == "a" |
| 376 |
assert re.match("(a(?=\s[^a]*))", "a b").group(1) == "a" |
| 377 |
assert re.match("(a(?=\s[abc]))", "a b").group(1) == "a" |
| 378 |
assert re.match("(a(?=\s[abc]*))", "a bc").group(1) == "a" |
| 379 |
assert re.match(r"(a)(?=\s\1)", "a a").group(1) == "a" |
| 380 |
assert re.match(r"(a)(?=\s\1*)", "a aa").group(1) == "a" |
| 381 |
assert re.match(r"(a)(?=\s(abc|a))", "a a").group(1) == "a" |
| 382 |
|
| 383 |
assert re.match(r"(a(?!\s[^a]))", "a a").group(1) == "a" |
| 384 |
assert re.match(r"(a(?!\s[abc]))", "a d").group(1) == "a" |
| 385 |
assert re.match(r"(a)(?!\s\1)", "a b").group(1) == "a" |
| 386 |
assert re.match(r"(a)(?!\s(abc|a))", "a b").group(1) == "a" |
| 387 |
|
| 388 |
def test_ignore_case(self): |
| 389 |
assert re.match(r"(a\s[^a])", "a b", re.I).group(1) == "a b" |
| 390 |
assert re.match(r"(a\s[^a]*)", "a bb", re.I).group(1) == "a bb" |
| 391 |
assert re.match(r"(a\s[abc])", "a b", re.I).group(1) == "a b" |
| 392 |
assert re.match(r"(a\s[abc]*)", "a bb", re.I).group(1) == "a bb" |
| 393 |
assert re.match(r"((a)\s\2)", "a a", re.I).group(1) == "a a" |
| 394 |
assert re.match(r"((a)\s\2*)", "a aa", re.I).group(1) == "a aa" |
| 395 |
assert re.match(r"((a)\s(abc|a))", "a a", re.I).group(1) == "a a" |
| 396 |
assert re.match(r"((a)\s(abc|a)*)", "a aa", re.I).group(1) == "a aa" |
| 397 |
|
| 398 |
def test_category(self): |
| 399 |
assert re.match(r"(\s)", " ").group(1) == " " |
| 400 |
|
| 401 |
def test_getlower(self): |
| 402 |
import _sre |
| 403 |
assert _sre.getlower(ord('A'), 0) == ord('a') |
| 404 |
assert _sre.getlower(ord('A'), re.LOCALE) == ord('a') |
| 405 |
assert _sre.getlower(ord('A'), re.UNICODE) == ord('a') |
| 406 |
|
| 407 |
assert re.match("abc", "ABC", re.I).group(0) == "ABC" |
| 408 |
assert re.match("abc", u"ABC", re.I).group(0) == "ABC" |
| 409 |
|
| 410 |
def test_not_literal(self): |
| 411 |
assert re.search("\s([^a])", " b").group(1) == "b" |
| 412 |
assert re.search("\s([^a]*)", " bb").group(1) == "bb" |
| 413 |
|
| 414 |
def test_search_coverage(self): |
| 415 |
assert re.search("\s(b)", " b").group(1) == "b" |
| 416 |
assert re.search("a\s", "a ").group(0) == "a " |
| 417 |
|
| 418 |
def test_re_escape(self): |
| 419 |
p="" |
| 420 |
for i in range(0, 256): |
| 421 |
p = p + chr(i) |
| 422 |
assert re.match(re.escape(chr(i)), chr(i)) is not None |
| 423 |
assert re.match(re.escape(chr(i)), chr(i)).span() == (0,1) |
| 424 |
|
| 425 |
pat=re.compile(re.escape(p)) |
| 426 |
assert pat.match(p) is not None |
| 427 |
assert pat.match(p).span() == (0,256) |
| 428 |
|
| 429 |
def test_constants(self): |
| 430 |
assert re.I == re.IGNORECASE |
| 431 |
assert re.L == re.LOCALE |
| 432 |
assert re.M == re.MULTILINE |
| 433 |
assert re.S == re.DOTALL |
| 434 |
assert re.X == re.VERBOSE |
| 435 |
|
| 436 |
def test_flags(self): |
| 437 |
for flag in [re.I, re.M, re.X, re.S, re.L]: |
| 438 |
assert re.compile('^pattern$', flag) != None |
| 439 |
|
| 440 |
def test_sre_character_literals(self): |
| 441 |
for i in [0, 8, 16, 32, 64, 127, 128, 255]: |
| 442 |
assert re.match(r"\%03o" % i, chr(i)) != None |
| 443 |
assert re.match(r"\%03o0" % i, chr(i)+"0") != None |
| 444 |
assert re.match(r"\%03o8" % i, chr(i)+"8") != None |
| 445 |
assert re.match(r"\x%02x" % i, chr(i)) != None |
| 446 |
assert re.match(r"\x%02x0" % i, chr(i)+"0") != None |
| 447 |
assert re.match(r"\x%02xz" % i, chr(i)+"z") != None |
| 448 |
py.test.raises(re.error, re.match, "\911", "") |
| 449 |
|
| 450 |
def test_sre_character_class_literals(self): |
| 451 |
for i in [0, 8, 16, 32, 64, 127, 128, 255]: |
| 452 |
assert re.match(r"[\%03o]" % i, chr(i)) != None |
| 453 |
assert re.match(r"[\%03o0]" % i, chr(i)) != None |
| 454 |
assert re.match(r"[\%03o8]" % i, chr(i)) != None |
| 455 |
assert re.match(r"[\x%02x]" % i, chr(i)) != None |
| 456 |
assert re.match(r"[\x%02x0]" % i, chr(i)) != None |
| 457 |
assert re.match(r"[\x%02xz]" % i, chr(i)) != None |
| 458 |
py.test.raises(re.error, re.match, "[\911]", "") |
| 459 |
|
| 460 |
def test_bug_113254(self): |
| 461 |
assert re.match(r'(a)|(b)', 'b').start(1) == -1 |
| 462 |
assert re.match(r'(a)|(b)', 'b').end(1) == -1 |
| 463 |
assert re.match(r'(a)|(b)', 'b').span(1) == (-1, -1) |
| 464 |
|
| 465 |
def test_bug_527371(self): |
| 466 |
|
| 467 |
assert re.match(r'(a)?a','a').lastindex == None |
| 468 |
assert re.match(r'(a)(b)?b','ab').lastindex == 1 |
| 469 |
assert re.match(r'(?P<a>a)(?P<b>b)?b','ab').lastgroup == 'a' |
| 470 |
assert re.match("(?P<a>a(b))", "ab").lastgroup == 'a' |
| 471 |
assert re.match("((a))", "a").lastindex == 1 |
| 472 |
|
| 473 |
def test_bug_545855(self): |
| 474 |
|
| 475 |
|
| 476 |
py.test.raises(re.error, re.compile, 'foo[a-') |
| 477 |
|
| 478 |
def test_bug_418626(self): |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
assert re.match('.*?c', 10000*'ab'+'cd').end(0) == 20001 |
| 483 |
assert re.match('.*?cd', 5000*'ab'+'c'+5000*'ab'+'cde').end(0) == ( |
| 484 |
20003) |
| 485 |
assert re.match('.*?cd', 20000*'abc'+'de').end(0) == 60001 |
| 486 |
|
| 487 |
|
| 488 |
assert re.search('(a|b)*?c', 10000*'ab'+'cd').end(0) == 20001 |
| 489 |
|
| 490 |
def test_bug_612074(self): |
| 491 |
pat=u"["+re.escape(u"\u2039")+u"]" |
| 492 |
assert re.compile(pat) and 1 == 1 |
| 493 |
|
| 494 |
def test_stack_overflow(self): |
| 495 |
|
| 496 |
|
| 497 |
assert re.match('(x)*', 50000*'x').group(1) == 'x' |
| 498 |
assert re.match('(x)*y', 50000*'x'+'y').group(1) == 'x' |
| 499 |
assert re.match('(x)*?y', 50000*'x'+'y').group(1) == 'x' |
| 500 |
|
| 501 |
def test_scanner(self): |
| 502 |
def s_ident(scanner, token): return token |
| 503 |
def s_operator(scanner, token): return "op%s" % token |
| 504 |
def s_float(scanner, token): return float(token) |
| 505 |
def s_int(scanner, token): return int(token) |
| 506 |
|
| 507 |
scanner = re.Scanner([ |
| 508 |
(r"[a-zA-Z_]\w*", s_ident), |
| 509 |
(r"\d+\.\d*", s_float), |
| 510 |
(r"\d+", s_int), |
| 511 |
(r"=|\+|-|\*|/", s_operator), |
| 512 |
(r"\s+", None), |
| 513 |
]) |
| 514 |
|
| 515 |
assert scanner.scanner.scanner("").pattern != None |
| 516 |
|
| 517 |
assert scanner.scan("sum = 3*foo + 312.50 + bar") == ( |
| 518 |
(['sum', 'op=', 3, 'op*', 'foo', 'op+', 312.5, |
| 519 |
'op+', 'bar'], '')) |
| 520 |
|
| 521 |
def test_bug_448951(self): |
| 522 |
|
| 523 |
|
| 524 |
for op in '','?','*': |
| 525 |
assert re.match(r'((.%s):)?z'%op, 'z').groups() == ( |
| 526 |
(None, None)) |
| 527 |
assert re.match(r'((.%s):)?z'%op, 'a:z').groups() == ( |
| 528 |
('a:', 'a')) |
| 529 |
|
| 530 |
def test_bug_725106(self): |
| 531 |
|
| 532 |
assert re.match('^((a)|b)*', 'abc').groups() == ( |
| 533 |
('b', 'a')) |
| 534 |
assert re.match('^(([ab])|c)*', 'abc').groups() == ( |
| 535 |
('c', 'b')) |
| 536 |
assert re.match('^((d)|[ab])*', 'abc').groups() == ( |
| 537 |
('b', None)) |
| 538 |
assert re.match('^((a)c|[ab])*', 'abc').groups() == ( |
| 539 |
('b', None)) |
| 540 |
assert re.match('^((a)|b)*?c', 'abc').groups() == ( |
| 541 |
('b', 'a')) |
| 542 |
assert re.match('^(([ab])|c)*?d', 'abcd').groups() == ( |
| 543 |
('c', 'b')) |
| 544 |
assert re.match('^((d)|[ab])*?c', 'abc').groups() == ( |
| 545 |
('b', None)) |
| 546 |
assert re.match('^((a)c|[ab])*?c', 'abc').groups() == ( |
| 547 |
('b', None)) |
| 548 |
|
| 549 |
def test_bug_725149(self): |
| 550 |
|
| 551 |
assert re.match('(a)(?:(?=(b)*)c)*', 'abb').groups() == ( |
| 552 |
('a', None)) |
| 553 |
assert re.match('(a)((?!(b)*))*', 'abb').groups() == ( |
| 554 |
('a', None, None)) |
| 555 |
|
| 556 |
def test_bug_764548(self): |
| 557 |
|
| 558 |
try: |
| 559 |
unicode |
| 560 |
except NameError: |
| 561 |
return |
| 562 |
class my_unicode(unicode): pass |
| 563 |
pat = re.compile(my_unicode("abc")) |
| 564 |
assert pat.match("xyz") == None |
| 565 |
|
| 566 |
def test_finditer(self): |
| 567 |
iter = re.finditer(r":+", "a:b::c:::d") |
| 568 |
assert [item.group(0) for item in iter] == ( |
| 569 |
[":", "::", ":::"]) |
| 570 |
|
| 571 |
def test_bug_926075(self): |
| 572 |
try: |
| 573 |
unicode |
| 574 |
except NameError: |
| 575 |
return |
| 576 |
assert (re.compile('bug_926075') is not |
| 577 |
re.compile(eval("u'bug_926075'"))) |
| 578 |
|
| 579 |
def test_bug_931848(self): |
| 580 |
try: |
| 581 |
unicode |
| 582 |
except NameError: |
| 583 |
pass |
| 584 |
pattern = eval('u"[\u002E\u3002\uFF0E\uFF61]"') |
| 585 |
assert re.compile(pattern).split("a.b.c") == ( |
| 586 |
['a','b','c']) |
| 587 |
|
| 588 |
def test_bug_581080(self): |
| 589 |
iter = re.finditer(r"\s", "a b") |
| 590 |
assert iter.next().span() == (1,2) |
| 591 |
py.test.raises(StopIteration, iter.next) |
| 592 |
|
| 593 |
if 0: |
| 594 |
scanner = re.compile(r"\s").scanner("a b") |
| 595 |
assert scanner.search().span() == (1, 2) |
| 596 |
assert scanner.search() == None |
| 597 |
|
| 598 |
def test_bug_817234(self): |
| 599 |
iter = re.finditer(r".*", "asdf") |
| 600 |
assert iter.next().span() == (0, 4) |
| 601 |
assert iter.next().span() == (4, 4) |
| 602 |
py.test.raises(StopIteration, iter.next) |
| 603 |
|
| 604 |
def test_empty_array(self): |
| 605 |
|
| 606 |
import array |
| 607 |
for typecode in 'cbBuhHiIlLfd': |
| 608 |
a = array.array(typecode) |
| 609 |
assert re.compile("bla").match(a) == None |
| 610 |
assert re.compile("").match(a).groups() == () |
| 611 |
|
| 612 |
def test_inline_flags(self): |
| 613 |
|
| 614 |
upper_char = unichr(0x1ea0) |
| 615 |
lower_char = unichr(0x1ea1) |
| 616 |
|
| 617 |
p = re.compile(upper_char, re.I | re.U) |
| 618 |
q = p.match(lower_char) |
| 619 |
assert q != None |
| 620 |
|
| 621 |
p = re.compile(lower_char, re.I | re.U) |
| 622 |
q = p.match(upper_char) |
| 623 |
assert q != None |
| 624 |
|
| 625 |
p = re.compile('(?i)' + upper_char, re.U) |
| 626 |
q = p.match(lower_char) |
| 627 |
assert q != None |
| 628 |
|
| 629 |
p = re.compile('(?i)' + lower_char, re.U) |
| 630 |
q = p.match(upper_char) |
| 631 |
assert q != None |
| 632 |
|
| 633 |
p = re.compile('(?iu)' + upper_char) |
| 634 |
q = p.match(lower_char) |
| 635 |
assert q != None |
| 636 |
|
| 637 |
p = re.compile('(?iu)' + lower_char) |
| 638 |
q = p.match(upper_char) |
| 639 |
assert q != None |