| 1 |
from test.support import (gc_collect, bigmemtest, _2G, |
| 2 |
cpython_only, captured_stdout, |
| 3 |
check_disallow_instantiation, linked_to_musl, |
| 4 |
warnings_helper, SHORT_TIMEOUT, Stopwatch, requires_resource) |
| 5 |
import locale |
| 6 |
import re |
| 7 |
import string |
| 8 |
import sys |
| 9 |
import unittest |
| 10 |
import warnings |
| 11 |
from re import Scanner |
| 12 |
from weakref import proxy |
| 13 |
|
| 14 |
# some platforms lack working multiprocessing |
| 15 |
try: |
| 16 |
import _multiprocessing # noqa: F401 |
| 17 |
except ImportError: |
| 18 |
multiprocessing = None |
| 19 |
else: |
| 20 |
import multiprocessing |
| 21 |
|
| 22 |
# Misc tests from Tim Peters' re.doc |
| 23 |
|
| 24 |
# WARNING: Don't change details in these tests if you don't know |
| 25 |
# what you're doing. Some of these tests were carefully modeled to |
| 26 |
# cover most of the code. |
| 27 |
|
| 28 |
class S(str): |
| 29 |
def __getitem__(self, index): |
| 30 |
return S(super().__getitem__(index)) |
| 31 |
|
| 32 |
class B(bytes): |
| 33 |
def __getitem__(self, index): |
| 34 |
return B(super().__getitem__(index)) |
| 35 |
|
| 36 |
class ReTests(unittest.TestCase): |
| 37 |
|
| 38 |
def assertTypedEqual(self, actual, expect, msg=None): |
| 39 |
self.assertEqual(actual, expect, msg) |
| 40 |
def recurse(actual, expect): |
| 41 |
if isinstance(expect, (tuple, list)): |
| 42 |
for x, y in zip(actual, expect): |
| 43 |
recurse(x, y) |
| 44 |
else: |
| 45 |
self.assertIs(type(actual), type(expect), msg) |
| 46 |
recurse(actual, expect) |
| 47 |
|
| 48 |
def checkPatternError(self, pattern, errmsg, pos=None): |
| 49 |
with self.assertRaises(re.PatternError) as cm: |
| 50 |
re.compile(pattern) |
| 51 |
with self.subTest(pattern=pattern): |
| 52 |
err = cm.exception |
| 53 |
self.assertEqual(err.msg, errmsg) |
| 54 |
if pos is not None: |
| 55 |
self.assertEqual(err.pos, pos) |
| 56 |
|
| 57 |
def checkTemplateError(self, pattern, repl, string, errmsg, pos=None): |
| 58 |
with self.assertRaises(re.PatternError) as cm: |
| 59 |
re.sub(pattern, repl, string) |
| 60 |
with self.subTest(pattern=pattern, repl=repl): |
| 61 |
err = cm.exception |
| 62 |
self.assertEqual(err.msg, errmsg) |
| 63 |
if pos is not None: |
| 64 |
self.assertEqual(err.pos, pos) |
| 65 |
|
| 66 |
def test_error_is_PatternError_alias(self): |
| 67 |
assert re.error is re.PatternError |
| 68 |
|
| 69 |
def test_keep_buffer(self): |
| 70 |
# See bug 14212 |
| 71 |
b = bytearray(b'x') |
| 72 |
it = re.finditer(b'a', b) |
| 73 |
with self.assertRaises(BufferError): |
| 74 |
b.extend(b'x'*400) |
| 75 |
list(it) |
| 76 |
del it |
| 77 |
gc_collect() |
| 78 |
b.extend(b'x'*400) |
| 79 |
|
| 80 |
def test_weakref(self): |
| 81 |
s = 'QabbbcR' |
| 82 |
x = re.compile('ab+c') |
| 83 |
y = proxy(x) |
| 84 |
self.assertEqual(x.findall('QabbbcR'), y.findall('QabbbcR')) |
| 85 |
|
| 86 |
def test_search_star_plus(self): |
| 87 |
self.assertEqual(re.search('x*', 'axx').span(0), (0, 0)) |
| 88 |
self.assertEqual(re.search('x*', 'axx').span(), (0, 0)) |
| 89 |
self.assertEqual(re.search('x+', 'axx').span(0), (1, 3)) |
| 90 |
self.assertEqual(re.search('x+', 'axx').span(), (1, 3)) |
| 91 |
self.assertIsNone(re.search('x', 'aaa')) |
| 92 |
self.assertEqual(re.match('a*', 'xxx').span(0), (0, 0)) |
| 93 |
self.assertEqual(re.match('a*', 'xxx').span(), (0, 0)) |
| 94 |
self.assertEqual(re.match('x*', 'xxxa').span(0), (0, 3)) |
| 95 |
self.assertEqual(re.match('x*', 'xxxa').span(), (0, 3)) |
| 96 |
self.assertIsNone(re.match('a+', 'xxx')) |
| 97 |
|
| 98 |
def test_branching(self): |
| 99 |
"""Test Branching |
| 100 |
Test expressions using the OR ('|') operator.""" |
| 101 |
self.assertEqual(re.match('(ab|ba)', 'ab').span(), (0, 2)) |
| 102 |
self.assertEqual(re.match('(ab|ba)', 'ba').span(), (0, 2)) |
| 103 |
self.assertEqual(re.match('(abc|bac|ca|cb)', 'abc').span(), |
| 104 |
(0, 3)) |
| 105 |
self.assertEqual(re.match('(abc|bac|ca|cb)', 'bac').span(), |
| 106 |
(0, 3)) |
| 107 |
self.assertEqual(re.match('(abc|bac|ca|cb)', 'ca').span(), |
| 108 |
(0, 2)) |
| 109 |
self.assertEqual(re.match('(abc|bac|ca|cb)', 'cb').span(), |
| 110 |
(0, 2)) |
| 111 |
self.assertEqual(re.match('((a)|(b)|(c))', 'a').span(), (0, 1)) |
| 112 |
self.assertEqual(re.match('((a)|(b)|(c))', 'b').span(), (0, 1)) |
| 113 |
self.assertEqual(re.match('((a)|(b)|(c))', 'c').span(), (0, 1)) |
| 114 |
|
| 115 |
def bump_num(self, matchobj): |
| 116 |
int_value = int(matchobj.group(0)) |
| 117 |
return str(int_value + 1) |
| 118 |
|
| 119 |
def test_basic_re_sub(self): |
| 120 |
self.assertTypedEqual(re.sub('y', 'a', 'xyz'), 'xaz') |
| 121 |
self.assertTypedEqual(re.sub('y', S('a'), S('xyz')), 'xaz') |
| 122 |
self.assertTypedEqual(re.sub(b'y', b'a', b'xyz'), b'xaz') |
| 123 |
self.assertTypedEqual(re.sub(b'y', B(b'a'), B(b'xyz')), b'xaz') |
| 124 |
self.assertTypedEqual(re.sub(b'y', bytearray(b'a'), bytearray(b'xyz')), b'xaz') |
| 125 |
self.assertTypedEqual(re.sub(b'y', memoryview(b'a'), memoryview(b'xyz')), b'xaz') |
| 126 |
for y in ("\xe0", "\u0430", "\U0001d49c"): |
| 127 |
self.assertEqual(re.sub(y, 'a', 'x%sz' % y), 'xaz') |
| 128 |
|
| 129 |
self.assertEqual(re.sub("(?i)b+", "x", "bbbb BBBB"), 'x x') |
| 130 |
self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y'), |
| 131 |
'9.3 -3 24x100y') |
| 132 |
with self.assertWarns(DeprecationWarning) as w: |
| 133 |
self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y', 3), |
| 134 |
'9.3 -3 23x99y') |
| 135 |
self.assertEqual(w.filename, __file__) |
| 136 |
self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y', count=3), |
| 137 |
'9.3 -3 23x99y') |
| 138 |
|
| 139 |
self.assertEqual(re.sub('.', lambda m: r"\n", 'x'), '\\n') |
| 140 |
self.assertEqual(re.sub('.', r"\n", 'x'), '\n') |
| 141 |
|
| 142 |
s = r"\1\1" |
| 143 |
self.assertEqual(re.sub('(.)', s, 'x'), 'xx') |
| 144 |
self.assertEqual(re.sub('(.)', s.replace('\\', r'\\'), 'x'), s) |
| 145 |
self.assertEqual(re.sub('(.)', lambda m: s, 'x'), s) |
| 146 |
|
| 147 |
self.assertEqual(re.sub('(?P<a>x)', r'\g<a>\g<a>', 'xx'), 'xxxx') |
| 148 |
self.assertEqual(re.sub('(?P<a>x)', r'\g<a>\g<1>', 'xx'), 'xxxx') |
| 149 |
self.assertEqual(re.sub('(?P<unk>x)', r'\g<unk>\g<unk>', 'xx'), 'xxxx') |
| 150 |
self.assertEqual(re.sub('(?P<unk>x)', r'\g<1>\g<1>', 'xx'), 'xxxx') |
| 151 |
self.assertEqual(re.sub('()x', r'\g<0>\g<0>', 'xx'), 'xxxx') |
| 152 |
|
| 153 |
self.assertEqual(re.sub('a', r'\t\n\v\r\f\a\b', 'a'), '\t\n\v\r\f\a\b') |
| 154 |
self.assertEqual(re.sub('a', '\t\n\v\r\f\a\b', 'a'), '\t\n\v\r\f\a\b') |
| 155 |
self.assertEqual(re.sub('a', '\t\n\v\r\f\a\b', 'a'), |
| 156 |
(chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)+chr(8))) |
| 157 |
for c in 'cdehijklmopqsuwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ': |
| 158 |
with self.subTest(c): |
| 159 |
with self.assertRaises(re.PatternError): |
| 160 |
self.assertEqual(re.sub('a', '\\' + c, 'a'), '\\' + c) |
| 161 |
|
| 162 |
self.assertEqual(re.sub(r'^\s*', 'X', 'test'), 'Xtest') |
| 163 |
|
| 164 |
def test_bug_449964(self): |
| 165 |
# fails for group followed by other escape |
| 166 |
self.assertEqual(re.sub(r'(?P<unk>x)', r'\g<1>\g<1>\b', 'xx'), |
| 167 |
'xx\bxx\b') |
| 168 |
|
| 169 |
def test_bug_449000(self): |
| 170 |
# Test for sub() on escaped characters |
| 171 |
self.assertEqual(re.sub(r'\r\n', r'\n', 'abc\r\ndef\r\n'), |
| 172 |
'abc\ndef\n') |
| 173 |
self.assertEqual(re.sub('\r\n', r'\n', 'abc\r\ndef\r\n'), |
| 174 |
'abc\ndef\n') |
| 175 |
self.assertEqual(re.sub(r'\r\n', '\n', 'abc\r\ndef\r\n'), |
| 176 |
'abc\ndef\n') |
| 177 |
self.assertEqual(re.sub('\r\n', '\n', 'abc\r\ndef\r\n'), |
| 178 |
'abc\ndef\n') |
| 179 |
|
| 180 |
def test_bug_1661(self): |
| 181 |
# Verify that flags do not get silently ignored with compiled patterns |
| 182 |
pattern = re.compile('.') |
| 183 |
self.assertRaises(ValueError, re.match, pattern, 'A', re.I) |
| 184 |
self.assertRaises(ValueError, re.search, pattern, 'A', re.I) |
| 185 |
self.assertRaises(ValueError, re.findall, pattern, 'A', re.I) |
| 186 |
self.assertRaises(ValueError, re.compile, pattern, re.I) |
| 187 |
|
| 188 |
def test_bug_3629(self): |
| 189 |
# A regex that triggered a bug in the sre-code validator |
| 190 |
re.compile("(?P<quote>)(?(quote))") |
| 191 |
|
| 192 |
def test_sub_template_numeric_escape(self): |
| 193 |
# bug 776311 and friends |
| 194 |
self.assertEqual(re.sub('x', r'\0', 'x'), '\0') |
| 195 |
self.assertEqual(re.sub('x', r'\000', 'x'), '\000') |
| 196 |
self.assertEqual(re.sub('x', r'\001', 'x'), '\001') |
| 197 |
self.assertEqual(re.sub('x', r'\008', 'x'), '\0' + '8') |
| 198 |
self.assertEqual(re.sub('x', r'\009', 'x'), '\0' + '9') |
| 199 |
self.assertEqual(re.sub('x', r'\111', 'x'), '\111') |
| 200 |
self.assertEqual(re.sub('x', r'\117', 'x'), '\117') |
| 201 |
self.assertEqual(re.sub('x', r'\377', 'x'), '\377') |
| 202 |
|
| 203 |
self.assertEqual(re.sub('x', r'\1111', 'x'), '\1111') |
| 204 |
self.assertEqual(re.sub('x', r'\1111', 'x'), '\111' + '1') |
| 205 |
|
| 206 |
self.assertEqual(re.sub('x', r'\00', 'x'), '\x00') |
| 207 |
self.assertEqual(re.sub('x', r'\07', 'x'), '\x07') |
| 208 |
self.assertEqual(re.sub('x', r'\08', 'x'), '\0' + '8') |
| 209 |
self.assertEqual(re.sub('x', r'\09', 'x'), '\0' + '9') |
| 210 |
self.assertEqual(re.sub('x', r'\0a', 'x'), '\0' + 'a') |
| 211 |
|
| 212 |
self.checkTemplateError('x', r'\400', 'x', |
| 213 |
r'octal escape value \400 outside of ' |
| 214 |
r'range 0-0o377', 0) |
| 215 |
self.checkTemplateError('x', r'\777', 'x', |
| 216 |
r'octal escape value \777 outside of ' |
| 217 |
r'range 0-0o377', 0) |
| 218 |
|
| 219 |
self.checkTemplateError('x', r'\1', 'x', 'invalid group reference 1', 1) |
| 220 |
self.checkTemplateError('x', r'\8', 'x', 'invalid group reference 8', 1) |
| 221 |
self.checkTemplateError('x', r'\9', 'x', 'invalid group reference 9', 1) |
| 222 |
self.checkTemplateError('x', r'\11', 'x', 'invalid group reference 11', 1) |
| 223 |
self.checkTemplateError('x', r'\18', 'x', 'invalid group reference 18', 1) |
| 224 |
self.checkTemplateError('x', r'\1a', 'x', 'invalid group reference 1', 1) |
| 225 |
self.checkTemplateError('x', r'\90', 'x', 'invalid group reference 90', 1) |
| 226 |
self.checkTemplateError('x', r'\99', 'x', 'invalid group reference 99', 1) |
| 227 |
self.checkTemplateError('x', r'\118', 'x', 'invalid group reference 11', 1) |
| 228 |
self.checkTemplateError('x', r'\11a', 'x', 'invalid group reference 11', 1) |
| 229 |
self.checkTemplateError('x', r'\181', 'x', 'invalid group reference 18', 1) |
| 230 |
self.checkTemplateError('x', r'\800', 'x', 'invalid group reference 80', 1) |
| 231 |
self.checkTemplateError('x', r'\8', '', 'invalid group reference 8', 1) |
| 232 |
|
| 233 |
# in python2.3 (etc), these loop endlessly in sre_parser.py |
| 234 |
self.assertEqual(re.sub('(((((((((((x)))))))))))', r'\11', 'x'), 'x') |
| 235 |
self.assertEqual(re.sub('((((((((((y))))))))))(.)', r'\118', 'xyz'), |
| 236 |
'xz8') |
| 237 |
self.assertEqual(re.sub('((((((((((y))))))))))(.)', r'\11a', 'xyz'), |
| 238 |
'xza') |
| 239 |
|
| 240 |
def test_qualified_re_sub(self): |
| 241 |
self.assertEqual(re.sub('a', 'b', 'aaaaa'), 'bbbbb') |
| 242 |
with self.assertWarns(DeprecationWarning) as w: |
| 243 |
self.assertEqual(re.sub('a', 'b', 'aaaaa', 1), 'baaaa') |
| 244 |
self.assertEqual(w.filename, __file__) |
| 245 |
self.assertEqual(re.sub('a', 'b', 'aaaaa', count=1), 'baaaa') |
| 246 |
|
| 247 |
with self.assertRaisesRegex(TypeError, |
| 248 |
r"sub\(\) got multiple values for argument 'count'"): |
| 249 |
re.sub('a', 'b', 'aaaaa', 1, count=1) |
| 250 |
with self.assertRaisesRegex(TypeError, |
| 251 |
r"sub\(\) got multiple values for argument 'flags'"): |
| 252 |
re.sub('a', 'b', 'aaaaa', 1, 0, flags=0) |
| 253 |
with self.assertRaisesRegex(TypeError, |
| 254 |
r"sub\(\) takes from 3 to 5 positional arguments but 6 " |
| 255 |
r"were given"): |
| 256 |
re.sub('a', 'b', 'aaaaa', 1, 0, 0) |
| 257 |
|
| 258 |
def test_misuse_flags(self): |
| 259 |
with self.assertWarns(DeprecationWarning) as w: |
| 260 |
result = re.sub('a', 'b', 'aaaaa', re.I) |
| 261 |
self.assertEqual(result, re.sub('a', 'b', 'aaaaa', count=int(re.I))) |
| 262 |
self.assertEqual(str(w.warning), |
| 263 |
"'count' is passed as positional argument") |
| 264 |
self.assertEqual(w.filename, __file__) |
| 265 |
with self.assertWarns(DeprecationWarning) as w: |
| 266 |
result = re.subn("b*", "x", "xyz", re.I) |
| 267 |
self.assertEqual(result, re.subn("b*", "x", "xyz", count=int(re.I))) |
| 268 |
self.assertEqual(str(w.warning), |
| 269 |
"'count' is passed as positional argument") |
| 270 |
self.assertEqual(w.filename, __file__) |
| 271 |
with self.assertWarns(DeprecationWarning) as w: |
| 272 |
result = re.split(":", ":a:b::c", re.I) |
| 273 |
self.assertEqual(result, re.split(":", ":a:b::c", maxsplit=int(re.I))) |
| 274 |
self.assertEqual(str(w.warning), |
| 275 |
"'maxsplit' is passed as positional argument") |
| 276 |
self.assertEqual(w.filename, __file__) |
| 277 |
|
| 278 |
def test_bug_114660(self): |
| 279 |
self.assertEqual(re.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello there'), |
| 280 |
'hello there') |
| 281 |
|
| 282 |
def test_symbolic_groups(self): |
| 283 |
re.compile(r'(?P<a>x)(?P=a)(?(a)y)') |
| 284 |
re.compile(r'(?P<a1>x)(?P=a1)(?(a1)y)') |
| 285 |
re.compile(r'(?P<a1>x)\1(?(1)y)') |
| 286 |
re.compile(b'(?P<a1>x)(?P=a1)(?(a1)y)') |
| 287 |
# New valid identifiers in Python 3 |
| 288 |
re.compile('(?P<µ>x)(?P=µ)(?(µ)y)') |
| 289 |
re.compile('(?P<𝔘𝔫𝔦𝔠𝔬𝔡𝔢>x)(?P=𝔘𝔫𝔦𝔠𝔬𝔡𝔢)(?(𝔘𝔫𝔦𝔠𝔬𝔡𝔢)y)') |
| 290 |
# Support > 100 groups. |
| 291 |
pat = '|'.join('x(?P<a%d>%x)y' % (i, i) for i in range(1, 200 + 1)) |
| 292 |
pat = '(?:%s)(?(200)z|t)' % pat |
| 293 |
self.assertEqual(re.match(pat, 'xc8yz').span(), (0, 5)) |
| 294 |
|
| 295 |
def test_symbolic_groups_errors(self): |
| 296 |
self.checkPatternError(r'(?P<a>)(?P<a>)', |
| 297 |
"redefinition of group name 'a' as group 2; " |
| 298 |
"was group 1") |
| 299 |
self.checkPatternError(r'(?P<a>(?P=a))', |
| 300 |
"cannot refer to an open group", 10) |
| 301 |
self.checkPatternError(r'(?Pxy)', 'unknown extension ?Px') |
| 302 |
self.checkPatternError(r'(?P<a>)(?P=a', 'missing ), unterminated name', 11) |
| 303 |
self.checkPatternError(r'(?P=', 'missing group name', 4) |
| 304 |
self.checkPatternError(r'(?P=)', 'missing group name', 4) |
| 305 |
self.checkPatternError(r'(?P=1)', "bad character in group name '1'", 4) |
| 306 |
self.checkPatternError(r'(?P=a)', "unknown group name 'a'") |
| 307 |
self.checkPatternError(r'(?P=a1)', "unknown group name 'a1'") |
| 308 |
self.checkPatternError(r'(?P=a.)', "bad character in group name 'a.'", 4) |
| 309 |
self.checkPatternError(r'(?P<)', 'missing >, unterminated name', 4) |
| 310 |
self.checkPatternError(r'(?P<a', 'missing >, unterminated name', 4) |
| 311 |
self.checkPatternError(r'(?P<', 'missing group name', 4) |
| 312 |
self.checkPatternError(r'(?P<>)', 'missing group name', 4) |
| 313 |
self.checkPatternError(r'(?P<1>)', "bad character in group name '1'", 4) |
| 314 |
self.checkPatternError(r'(?P<a.>)', "bad character in group name 'a.'", 4) |
| 315 |
self.checkPatternError(r'(?(', 'missing group name', 3) |
| 316 |
self.checkPatternError(r'(?())', 'missing group name', 3) |
| 317 |
self.checkPatternError(r'(?(a))', "unknown group name 'a'", 3) |
| 318 |
self.checkPatternError(r'(?(-1))', "bad character in group name '-1'", 3) |
| 319 |
self.checkPatternError(r'(?(1a))', "bad character in group name '1a'", 3) |
| 320 |
self.checkPatternError(r'(?(a.))', "bad character in group name 'a.'", 3) |
| 321 |
self.checkPatternError('(?P<©>x)', "bad character in group name '©'", 4) |
| 322 |
self.checkPatternError('(?P=©)', "bad character in group name '©'", 4) |
| 323 |
self.checkPatternError('(?(©)y)', "bad character in group name '©'", 3) |
| 324 |
self.checkPatternError(b'(?P<\xc2\xb5>x)', |
| 325 |
r"bad character in group name '\xc2\xb5'", 4) |
| 326 |
self.checkPatternError(b'(?P=\xc2\xb5)', |
| 327 |
r"bad character in group name '\xc2\xb5'", 4) |
| 328 |
self.checkPatternError(b'(?(\xc2\xb5)y)', |
| 329 |
r"bad character in group name '\xc2\xb5'", 3) |
| 330 |
|
| 331 |
def test_symbolic_refs(self): |
| 332 |
self.assertEqual(re.sub('(?P<a>x)|(?P<b>y)', r'\g<b>', 'xx'), '') |
| 333 |
self.assertEqual(re.sub('(?P<a>x)|(?P<b>y)', r'\2', 'xx'), '') |
| 334 |
self.assertEqual(re.sub(b'(?P<a1>x)', br'\g<a1>', b'xx'), b'xx') |
| 335 |
# New valid identifiers in Python 3 |
| 336 |
self.assertEqual(re.sub('(?P<µ>x)', r'\g<µ>', 'xx'), 'xx') |
| 337 |
self.assertEqual(re.sub('(?P<𝔘𝔫𝔦𝔠𝔬𝔡𝔢>x)', r'\g<𝔘𝔫𝔦𝔠𝔬𝔡𝔢>', 'xx'), 'xx') |
| 338 |
# Support > 100 groups. |
| 339 |
pat = '|'.join('x(?P<a%d>%x)y' % (i, i) for i in range(1, 200 + 1)) |
| 340 |
self.assertEqual(re.sub(pat, r'\g<200>', 'xc8yzxc8y'), 'c8zc8') |
| 341 |
|
| 342 |
def test_symbolic_refs_errors(self): |
| 343 |
self.checkTemplateError('(?P<a>x)', r'\g<a', 'xx', |
| 344 |
'missing >, unterminated name', 3) |
| 345 |
self.checkTemplateError('(?P<a>x)', r'\g<', 'xx', |
| 346 |
'missing group name', 3) |
| 347 |
self.checkTemplateError('(?P<a>x)', r'\g', 'xx', 'missing <', 2) |
| 348 |
self.checkTemplateError('(?P<a>x)', r'\g<a a>', 'xx', |
| 349 |
"bad character in group name 'a a'", 3) |
| 350 |
self.checkTemplateError('(?P<a>x)', r'\g<>', 'xx', |
| 351 |
'missing group name', 3) |
| 352 |
self.checkTemplateError('(?P<a>x)', r'\g<1a1>', 'xx', |
| 353 |
"bad character in group name '1a1'", 3) |
| 354 |
self.checkTemplateError('(?P<a>x)', r'\g<2>', 'xx', |
| 355 |
'invalid group reference 2', 3) |
| 356 |
self.checkTemplateError('(?P<a>x)', r'\2', 'xx', |
| 357 |
'invalid group reference 2', 1) |
| 358 |
with self.assertRaisesRegex(IndexError, "unknown group name 'ab'"): |
| 359 |
re.sub('(?P<a>x)', r'\g<ab>', 'xx') |
| 360 |
self.checkTemplateError('(?P<a>x)', r'\g<-1>', 'xx', |
| 361 |
"bad character in group name '-1'", 3) |
| 362 |
self.checkTemplateError('(?P<a>x)', r'\g<+1>', 'xx', |
| 363 |
"bad character in group name '+1'", 3) |
| 364 |
self.checkTemplateError('()'*10, r'\g<1_0>', 'xx', |
| 365 |
"bad character in group name '1_0'", 3) |
| 366 |
self.checkTemplateError('(?P<a>x)', r'\g< 1 >', 'xx', |
| 367 |
"bad character in group name ' 1 '", 3) |
| 368 |
self.checkTemplateError('(?P<a>x)', r'\g<©>', 'xx', |
| 369 |
"bad character in group name '©'", 3) |
| 370 |
self.checkTemplateError(b'(?P<a>x)', b'\\g<\xc2\xb5>', b'xx', |
| 371 |
r"bad character in group name '\xc2\xb5'", 3) |
| 372 |
self.checkTemplateError('(?P<a>x)', r'\g<㊀>', 'xx', |
| 373 |
"bad character in group name '㊀'", 3) |
| 374 |
self.checkTemplateError('(?P<a>x)', r'\g<¹>', 'xx', |
| 375 |
"bad character in group name '¹'", 3) |
| 376 |
self.checkTemplateError('(?P<a>x)', r'\g<१>', 'xx', |
| 377 |
"bad character in group name '१'", 3) |
| 378 |
|
| 379 |
def test_re_subn(self): |
| 380 |
self.assertEqual(re.subn("(?i)b+", "x", "bbbb BBBB"), ('x x', 2)) |
| 381 |
self.assertEqual(re.subn("b+", "x", "bbbb BBBB"), ('x BBBB', 1)) |
| 382 |
self.assertEqual(re.subn("b+", "x", "xyz"), ('xyz', 0)) |
| 383 |
self.assertEqual(re.subn("b*", "x", "xyz"), ('xxxyxzx', 4)) |
| 384 |
with self.assertWarns(DeprecationWarning) as w: |
| 385 |
self.assertEqual(re.subn("b*", "x", "xyz", 2), ('xxxyz', 2)) |
| 386 |
self.assertEqual(w.filename, __file__) |
| 387 |
self.assertEqual(re.subn("b*", "x", "xyz", count=2), ('xxxyz', 2)) |
| 388 |
|
| 389 |
with self.assertRaisesRegex(TypeError, |
| 390 |
r"subn\(\) got multiple values for argument 'count'"): |
| 391 |
re.subn('a', 'b', 'aaaaa', 1, count=1) |
| 392 |
with self.assertRaisesRegex(TypeError, |
| 393 |
r"subn\(\) got multiple values for argument 'flags'"): |
| 394 |
re.subn('a', 'b', 'aaaaa', 1, 0, flags=0) |
| 395 |
with self.assertRaisesRegex(TypeError, |
| 396 |
r"subn\(\) takes from 3 to 5 positional arguments but 6 " |
| 397 |
r"were given"): |
| 398 |
re.subn('a', 'b', 'aaaaa', 1, 0, 0) |
| 399 |
|
| 400 |
def test_re_split(self): |
| 401 |
for string in ":a:b::c", S(":a:b::c"): |
| 402 |
self.assertTypedEqual(re.split(":", string), |
| 403 |
['', 'a', 'b', '', 'c']) |
| 404 |
self.assertTypedEqual(re.split(":+", string), |
| 405 |
['', 'a', 'b', 'c']) |
| 406 |
self.assertTypedEqual(re.split("(:+)", string), |
| 407 |
['', ':', 'a', ':', 'b', '::', 'c']) |
| 408 |
for string in (b":a:b::c", B(b":a:b::c"), bytearray(b":a:b::c"), |
| 409 |
memoryview(b":a:b::c")): |
| 410 |
self.assertTypedEqual(re.split(b":", string), |
| 411 |
[b'', b'a', b'b', b'', b'c']) |
| 412 |
self.assertTypedEqual(re.split(b":+", string), |
| 413 |
[b'', b'a', b'b', b'c']) |
| 414 |
self.assertTypedEqual(re.split(b"(:+)", string), |
| 415 |
[b'', b':', b'a', b':', b'b', b'::', b'c']) |
| 416 |
for a, b, c in ("\xe0\xdf\xe7", "\u0430\u0431\u0432", |
| 417 |
"\U0001d49c\U0001d49e\U0001d4b5"): |
| 418 |
string = ":%s:%s::%s" % (a, b, c) |
| 419 |
self.assertEqual(re.split(":", string), ['', a, b, '', c]) |
| 420 |
self.assertEqual(re.split(":+", string), ['', a, b, c]) |
| 421 |
self.assertEqual(re.split("(:+)", string), |
| 422 |
['', ':', a, ':', b, '::', c]) |
| 423 |
|
| 424 |
self.assertEqual(re.split("(?::+)", ":a:b::c"), ['', 'a', 'b', 'c']) |
| 425 |
self.assertEqual(re.split("(:)+", ":a:b::c"), |
| 426 |
['', ':', 'a', ':', 'b', ':', 'c']) |
| 427 |
self.assertEqual(re.split("([b:]+)", ":a:b::c"), |
| 428 |
['', ':', 'a', ':b::', 'c']) |
| 429 |
self.assertEqual(re.split("(b)|(:+)", ":a:b::c"), |
| 430 |
['', None, ':', 'a', None, ':', '', 'b', None, '', |
| 431 |
None, '::', 'c']) |
| 432 |
self.assertEqual(re.split("(?:b)|(?::+)", ":a:b::c"), |
| 433 |
['', 'a', '', '', 'c']) |
| 434 |
|
| 435 |
for sep, expected in [ |
| 436 |
(':*', ['', '', 'a', '', 'b', '', 'c', '']), |
| 437 |
('(?::*)', ['', '', 'a', '', 'b', '', 'c', '']), |
| 438 |
('(:*)', ['', ':', '', '', 'a', ':', '', '', 'b', '::', '', '', 'c', '', '']), |
| 439 |
('(:)*', ['', ':', '', None, 'a', ':', '', None, 'b', ':', '', None, 'c', None, '']), |
| 440 |
]: |
| 441 |
with self.subTest(sep=sep): |
| 442 |
self.assertTypedEqual(re.split(sep, ':a:b::c'), expected) |
| 443 |
|
| 444 |
for sep, expected in [ |
| 445 |
('', ['', ':', 'a', ':', 'b', ':', ':', 'c', '']), |
| 446 |
(r'\b', [':', 'a', ':', 'b', '::', 'c', '']), |
| 447 |
(r'(?=:)', ['', ':a', ':b', ':', ':c']), |
| 448 |
(r'(?<=:)', [':', 'a:', 'b:', ':', 'c']), |
| 449 |
]: |
| 450 |
with self.subTest(sep=sep): |
| 451 |
self.assertTypedEqual(re.split(sep, ':a:b::c'), expected) |
| 452 |
|
| 453 |
def test_qualified_re_split(self): |
| 454 |
with self.assertWarns(DeprecationWarning) as w: |
| 455 |
self.assertEqual(re.split(":", ":a:b::c", 2), ['', 'a', 'b::c']) |
| 456 |
self.assertEqual(w.filename, __file__) |
| 457 |
self.assertEqual(re.split(":", ":a:b::c", maxsplit=2), ['', 'a', 'b::c']) |
| 458 |
self.assertEqual(re.split(':', 'a:b:c:d', maxsplit=2), ['a', 'b', 'c:d']) |
| 459 |
self.assertEqual(re.split("(:)", ":a:b::c", maxsplit=2), |
| 460 |
['', ':', 'a', ':', 'b::c']) |
| 461 |
self.assertEqual(re.split("(:+)", ":a:b::c", maxsplit=2), |
| 462 |
['', ':', 'a', ':', 'b::c']) |
| 463 |
self.assertEqual(re.split("(:*)", ":a:b::c", maxsplit=2), |
| 464 |
['', ':', '', '', 'a:b::c']) |
| 465 |
|
| 466 |
with self.assertRaisesRegex(TypeError, |
| 467 |
r"split\(\) got multiple values for argument 'maxsplit'"): |
| 468 |
re.split(":", ":a:b::c", 2, maxsplit=2) |
| 469 |
with self.assertRaisesRegex(TypeError, |
| 470 |
r"split\(\) got multiple values for argument 'flags'"): |
| 471 |
re.split(":", ":a:b::c", 2, 0, flags=0) |
| 472 |
with self.assertRaisesRegex(TypeError, |
| 473 |
r"split\(\) takes from 2 to 4 positional arguments but 5 " |
| 474 |
r"were given"): |
| 475 |
re.split(":", ":a:b::c", 2, 0, 0) |
| 476 |
|
| 477 |
def test_re_findall(self): |
| 478 |
self.assertEqual(re.findall(":+", "abc"), []) |
| 479 |
for string in "a:b::c:::d", S("a:b::c:::d"): |
| 480 |
self.assertTypedEqual(re.findall(":+", string), |
| 481 |
[":", "::", ":::"]) |
| 482 |
self.assertTypedEqual(re.findall("(:+)", string), |
| 483 |
[":", "::", ":::"]) |
| 484 |
self.assertTypedEqual(re.findall("(:)(:*)", string), |
| 485 |
[(":", ""), (":", ":"), (":", "::")]) |
| 486 |
for string in (b"a:b::c:::d", B(b"a:b::c:::d"), bytearray(b"a:b::c:::d"), |
| 487 |
memoryview(b"a:b::c:::d")): |
| 488 |
self.assertTypedEqual(re.findall(b":+", string), |
| 489 |
[b":", b"::", b":::"]) |
| 490 |
self.assertTypedEqual(re.findall(b"(:+)", string), |
| 491 |
[b":", b"::", b":::"]) |
| 492 |
self.assertTypedEqual(re.findall(b"(:)(:*)", string), |
| 493 |
[(b":", b""), (b":", b":"), (b":", b"::")]) |
| 494 |
for x in ("\xe0", "\u0430", "\U0001d49c"): |
| 495 |
xx = x * 2 |
| 496 |
xxx = x * 3 |
| 497 |
string = "a%sb%sc%sd" % (x, xx, xxx) |
| 498 |
self.assertEqual(re.findall("%s+" % x, string), [x, xx, xxx]) |
| 499 |
self.assertEqual(re.findall("(%s+)" % x, string), [x, xx, xxx]) |
| 500 |
self.assertEqual(re.findall("(%s)(%s*)" % (x, x), string), |
| 501 |
[(x, ""), (x, x), (x, xx)]) |
| 502 |
|
| 503 |
def test_bug_117612(self): |
| 504 |
self.assertEqual(re.findall(r"(a|(b))", "aba"), |
| 505 |
[("a", ""),("b", "b"),("a", "")]) |
| 506 |
|
| 507 |
def test_re_match(self): |
| 508 |
for string in 'a', S('a'): |
| 509 |
self.assertEqual(re.match('a', string).groups(), ()) |
| 510 |
self.assertEqual(re.match('(a)', string).groups(), ('a',)) |
| 511 |
self.assertEqual(re.match('(a)', string).group(0), 'a') |
| 512 |
self.assertEqual(re.match('(a)', string).group(1), 'a') |
| 513 |
self.assertEqual(re.match('(a)', string).group(1, 1), ('a', 'a')) |
| 514 |
for string in b'a', B(b'a'), bytearray(b'a'), memoryview(b'a'): |
| 515 |
self.assertEqual(re.match(b'a', string).groups(), ()) |
| 516 |
self.assertEqual(re.match(b'(a)', string).groups(), (b'a',)) |
| 517 |
self.assertEqual(re.match(b'(a)', string).group(0), b'a') |
| 518 |
self.assertEqual(re.match(b'(a)', string).group(1), b'a') |
| 519 |
self.assertEqual(re.match(b'(a)', string).group(1, 1), (b'a', b'a')) |
| 520 |
for a in ("\xe0", "\u0430", "\U0001d49c"): |
| 521 |
self.assertEqual(re.match(a, a).groups(), ()) |
| 522 |
self.assertEqual(re.match('(%s)' % a, a).groups(), (a,)) |
| 523 |
self.assertEqual(re.match('(%s)' % a, a).group(0), a) |
| 524 |
self.assertEqual(re.match('(%s)' % a, a).group(1), a) |
| 525 |
self.assertEqual(re.match('(%s)' % a, a).group(1, 1), (a, a)) |
| 526 |
|
| 527 |
pat = re.compile('((a)|(b))(c)?') |
| 528 |
self.assertEqual(pat.match('a').groups(), ('a', 'a', None, None)) |
| 529 |
self.assertEqual(pat.match('b').groups(), ('b', None, 'b', None)) |
| 530 |
self.assertEqual(pat.match('ac').groups(), ('a', 'a', None, 'c')) |
| 531 |
self.assertEqual(pat.match('bc').groups(), ('b', None, 'b', 'c')) |
| 532 |
self.assertEqual(pat.match('bc').groups(""), ('b', "", 'b', 'c')) |
| 533 |
|
| 534 |
pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?') |
| 535 |
self.assertEqual(pat.match('a').group(1, 2, 3), ('a', None, None)) |
| 536 |
self.assertEqual(pat.match('b').group('a1', 'b2', 'c3'), |
| 537 |
(None, 'b', None)) |
| 538 |
self.assertEqual(pat.match('ac').group(1, 'b2', 3), ('a', None, 'c')) |
| 539 |
|
| 540 |
def test_group(self): |
| 541 |
class Index: |
| 542 |
def __init__(self, value): |
| 543 |
self.value = value |
| 544 |
def __index__(self): |
| 545 |
return self.value |
| 546 |
# A single group |
| 547 |
m = re.match('(a)(b)', 'ab') |
| 548 |
self.assertEqual(m.group(), 'ab') |
| 549 |
self.assertEqual(m.group(0), 'ab') |
| 550 |
self.assertEqual(m.group(1), 'a') |
| 551 |
self.assertEqual(m.group(Index(1)), 'a') |
| 552 |
self.assertRaises(IndexError, m.group, -1) |
| 553 |
self.assertRaises(IndexError, m.group, 3) |
| 554 |
self.assertRaises(IndexError, m.group, 1<<1000) |
| 555 |
self.assertRaises(IndexError, m.group, Index(1<<1000)) |
| 556 |
self.assertRaises(IndexError, m.group, 'x') |
| 557 |
# Multiple groups |
| 558 |
self.assertEqual(m.group(2, 1), ('b', 'a')) |
| 559 |
self.assertEqual(m.group(Index(2), Index(1)), ('b', 'a')) |
| 560 |
|
| 561 |
def test_match_getitem(self): |
| 562 |
pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?') |
| 563 |
|
| 564 |
m = pat.match('a') |
| 565 |
self.assertEqual(m['a1'], 'a') |
| 566 |
self.assertEqual(m['b2'], None) |
| 567 |
self.assertEqual(m['c3'], None) |
| 568 |
self.assertEqual('a1={a1} b2={b2} c3={c3}'.format_map(m), 'a1=a b2=None c3=None') |
| 569 |
self.assertEqual(m[0], 'a') |
| 570 |
self.assertEqual(m[1], 'a') |
| 571 |
self.assertEqual(m[2], None) |
| 572 |
self.assertEqual(m[3], None) |
| 573 |
with self.assertRaisesRegex(IndexError, 'no such group'): |
| 574 |
m['X'] |
| 575 |
with self.assertRaisesRegex(IndexError, 'no such group'): |
| 576 |
m[-1] |
| 577 |
with self.assertRaisesRegex(IndexError, 'no such group'): |
| 578 |
m[4] |
| 579 |
with self.assertRaisesRegex(IndexError, 'no such group'): |
| 580 |
m[0, 1] |
| 581 |
with self.assertRaisesRegex(IndexError, 'no such group'): |
| 582 |
m[(0,)] |
| 583 |
with self.assertRaisesRegex(IndexError, 'no such group'): |
| 584 |
m[(0, 1)] |
| 585 |
with self.assertRaisesRegex(IndexError, 'no such group'): |
| 586 |
'a1={a2}'.format_map(m) |
| 587 |
|
| 588 |
m = pat.match('ac') |
| 589 |
self.assertEqual(m['a1'], 'a') |
| 590 |
self.assertEqual(m['b2'], None) |
| 591 |
self.assertEqual(m['c3'], 'c') |
| 592 |
self.assertEqual('a1={a1} b2={b2} c3={c3}'.format_map(m), 'a1=a b2=None c3=c') |
| 593 |
self.assertEqual(m[0], 'ac') |
| 594 |
self.assertEqual(m[1], 'a') |
| 595 |
self.assertEqual(m[2], None) |
| 596 |
self.assertEqual(m[3], 'c') |
| 597 |
|
| 598 |
# Cannot assign. |
| 599 |
with self.assertRaises(TypeError): |
| 600 |
m[0] = 1 |
| 601 |
|
| 602 |
# No len(). |
| 603 |
self.assertRaises(TypeError, len, m) |
| 604 |
|
| 605 |
def test_re_fullmatch(self): |
| 606 |
# Issue 16203: Proposal: add re.fullmatch() method. |
| 607 |
self.assertEqual(re.fullmatch(r"a", "a").span(), (0, 1)) |
| 608 |
for string in "ab", S("ab"): |
| 609 |
self.assertEqual(re.fullmatch(r"a|ab", string).span(), (0, 2)) |
| 610 |
for string in b"ab", B(b"ab"), bytearray(b"ab"), memoryview(b"ab"): |
| 611 |
self.assertEqual(re.fullmatch(br"a|ab", string).span(), (0, 2)) |
| 612 |
for a, b in "\xe0\xdf", "\u0430\u0431", "\U0001d49c\U0001d49e": |
| 613 |
r = r"%s|%s" % (a, a + b) |
| 614 |
self.assertEqual(re.fullmatch(r, a + b).span(), (0, 2)) |
| 615 |
self.assertEqual(re.fullmatch(r".*?$", "abc").span(), (0, 3)) |
| 616 |
self.assertEqual(re.fullmatch(r".*?", "abc").span(), (0, 3)) |
| 617 |
self.assertEqual(re.fullmatch(r"a.*?b", "ab").span(), (0, 2)) |
| 618 |
self.assertEqual(re.fullmatch(r"a.*?b", "abb").span(), (0, 3)) |
| 619 |
self.assertEqual(re.fullmatch(r"a.*?b", "axxb").span(), (0, 4)) |
| 620 |
self.assertIsNone(re.fullmatch(r"a+", "ab")) |
| 621 |
self.assertIsNone(re.fullmatch(r"abc$", "abc\n")) |
| 622 |
self.assertIsNone(re.fullmatch(r"abc\z", "abc\n")) |
| 623 |
self.assertIsNone(re.fullmatch(r"abc\Z", "abc\n")) |
| 624 |
self.assertIsNone(re.fullmatch(r"(?m)abc$", "abc\n")) |
| 625 |
self.assertEqual(re.fullmatch(r"ab(?=c)cd", "abcd").span(), (0, 4)) |
| 626 |
self.assertEqual(re.fullmatch(r"ab(?<=b)cd", "abcd").span(), (0, 4)) |
| 627 |
self.assertEqual(re.fullmatch(r"(?=a|ab)ab", "ab").span(), (0, 2)) |
| 628 |
|
| 629 |
self.assertEqual( |
| 630 |
re.compile(r"bc").fullmatch("abcd", pos=1, endpos=3).span(), (1, 3)) |
| 631 |
self.assertEqual( |
| 632 |
re.compile(r".*?$").fullmatch("abcd", pos=1, endpos=3).span(), (1, 3)) |
| 633 |
self.assertEqual( |
| 634 |
re.compile(r".*?").fullmatch("abcd", pos=1, endpos=3).span(), (1, 3)) |
| 635 |
|
| 636 |
def test_re_groupref_exists(self): |
| 637 |
self.assertEqual(re.match(r'^(\()?([^()]+)(?(1)\))$', '(a)').groups(), |
| 638 |
('(', 'a')) |
| 639 |
self.assertEqual(re.match(r'^(\()?([^()]+)(?(1)\))$', 'a').groups(), |
| 640 |
(None, 'a')) |
| 641 |
self.assertIsNone(re.match(r'^(\()?([^()]+)(?(1)\))$', 'a)')) |
| 642 |
self.assertIsNone(re.match(r'^(\()?([^()]+)(?(1)\))$', '(a')) |
| 643 |
self.assertEqual(re.match('^(?:(a)|c)((?(1)b|d))$', 'ab').groups(), |
| 644 |
('a', 'b')) |
| 645 |
self.assertEqual(re.match(r'^(?:(a)|c)((?(1)b|d))$', 'cd').groups(), |
| 646 |
(None, 'd')) |
| 647 |
self.assertEqual(re.match(r'^(?:(a)|c)((?(1)|d))$', 'cd').groups(), |
| 648 |
(None, 'd')) |
| 649 |
self.assertEqual(re.match(r'^(?:(a)|c)((?(1)|d))$', 'a').groups(), |
| 650 |
('a', '')) |
| 651 |
|
| 652 |
# Tests for bug #1177831: exercise groups other than the first group |
| 653 |
p = re.compile('(?P<g1>a)(?P<g2>b)?((?(g2)c|d))') |
| 654 |
self.assertEqual(p.match('abc').groups(), |
| 655 |
('a', 'b', 'c')) |
| 656 |
self.assertEqual(p.match('ad').groups(), |
| 657 |
('a', None, 'd')) |
| 658 |
self.assertIsNone(p.match('abd')) |
| 659 |
self.assertIsNone(p.match('ac')) |
| 660 |
|
| 661 |
# Support > 100 groups. |
| 662 |
pat = '|'.join('x(?P<a%d>%x)y' % (i, i) for i in range(1, 200 + 1)) |
| 663 |
pat = '(?:%s)(?(200)z)' % pat |
| 664 |
self.assertEqual(re.match(pat, 'xc8yz').span(), (0, 5)) |
| 665 |
|
| 666 |
def test_re_groupref_exists_errors(self): |
| 667 |
self.checkPatternError(r'(?P<a>)(?(0)a|b)', 'bad group number', 10) |
| 668 |
self.checkPatternError(r'()(?(-1)a|b)', |
| 669 |
"bad character in group name '-1'", 5) |
| 670 |
self.checkPatternError(r'()(?(+1)a|b)', |
| 671 |
"bad character in group name '+1'", 5) |
| 672 |
self.checkPatternError(r'()'*10 + r'(?(1_0)a|b)', |
| 673 |
"bad character in group name '1_0'", 23) |
| 674 |
self.checkPatternError(r'()(?( 1 )a|b)', |
| 675 |
"bad character in group name ' 1 '", 5) |
| 676 |
self.checkPatternError(r'()(?(㊀)a|b)', |
| 677 |
"bad character in group name '㊀'", 5) |
| 678 |
self.checkPatternError(r'()(?(¹)a|b)', |
| 679 |
"bad character in group name '¹'", 5) |
| 680 |
self.checkPatternError(r'()(?(१)a|b)', |
| 681 |
"bad character in group name '१'", 5) |
| 682 |
self.checkPatternError(r'()(?(1', |
| 683 |
"missing ), unterminated name", 5) |
| 684 |
self.checkPatternError(r'()(?(1)a', |
| 685 |
"missing ), unterminated subpattern", 2) |
| 686 |
self.checkPatternError(r'()(?(1)a|b', |
| 687 |
'missing ), unterminated subpattern', 2) |
| 688 |
self.checkPatternError(r'()(?(1)a|b|c', |
| 689 |
'conditional backref with more than ' |
| 690 |
'two branches', 10) |
| 691 |
self.checkPatternError(r'()(?(1)a|b|c)', |
| 692 |
'conditional backref with more than ' |
| 693 |
'two branches', 10) |
| 694 |
self.checkPatternError(r'()(?(2)a)', |
| 695 |
"invalid group reference 2", 5) |
| 696 |
|
| 697 |
def test_re_groupref_exists_validation_bug(self): |
| 698 |
for i in range(256): |
| 699 |
with self.subTest(code=i): |
| 700 |
re.compile(r'()(?(1)\x%02x?)' % i) |
| 701 |
|
| 702 |
def test_re_groupref_overflow(self): |
| 703 |
from re._constants import MAXGROUPS |
| 704 |
self.checkTemplateError('()', r'\g<%s>' % MAXGROUPS, 'xx', |
| 705 |
'invalid group reference %d' % MAXGROUPS, 3) |
| 706 |
self.checkPatternError(r'(?P<a>)(?(%d))' % MAXGROUPS, |
| 707 |
'invalid group reference %d' % MAXGROUPS, 10) |
| 708 |
|
| 709 |
def test_re_groupref(self): |
| 710 |
self.assertEqual(re.match(r'^(\|)?([^()]+)\1$', '|a|').groups(), |
| 711 |
('|', 'a')) |
| 712 |
self.assertEqual(re.match(r'^(\|)?([^()]+)\1?$', 'a').groups(), |
| 713 |
(None, 'a')) |
| 714 |
self.assertIsNone(re.match(r'^(\|)?([^()]+)\1$', 'a|')) |
| 715 |
self.assertIsNone(re.match(r'^(\|)?([^()]+)\1$', '|a')) |
| 716 |
self.assertEqual(re.match(r'^(?:(a)|c)(\1)$', 'aa').groups(), |
| 717 |
('a', 'a')) |
| 718 |
self.assertEqual(re.match(r'^(?:(a)|c)(\1)?$', 'c').groups(), |
| 719 |
(None, None)) |
| 720 |
|
| 721 |
self.checkPatternError(r'(abc\1)', 'cannot refer to an open group', 4) |
| 722 |
|
| 723 |
def test_groupdict(self): |
| 724 |
self.assertEqual(re.match('(?P<first>first) (?P<second>second)', |
| 725 |
'first second').groupdict(), |
| 726 |
{'first':'first', 'second':'second'}) |
| 727 |
|
| 728 |
def test_expand(self): |
| 729 |
self.assertEqual(re.match("(?P<first>first) (?P<second>second)", |
| 730 |
"first second") |
| 731 |
.expand(r"\2 \1 \g<second> \g<first>"), |
| 732 |
"second first second first") |
| 733 |
self.assertEqual(re.match("(?P<first>first)|(?P<second>second)", |
| 734 |
"first") |
| 735 |
.expand(r"\2 \g<second>"), |
| 736 |
" ") |
| 737 |
|
| 738 |
def test_repeat_minmax(self): |
| 739 |
self.assertIsNone(re.match(r"^(\w){1}$", "abc")) |
| 740 |
self.assertIsNone(re.match(r"^(\w){1}?$", "abc")) |
| 741 |
self.assertIsNone(re.match(r"^(\w){1,2}$", "abc")) |
| 742 |
self.assertIsNone(re.match(r"^(\w){1,2}?$", "abc")) |
| 743 |
|
| 744 |
self.assertEqual(re.match(r"^(\w){3}$", "abc").group(1), "c") |
| 745 |
self.assertEqual(re.match(r"^(\w){1,3}$", "abc").group(1), "c") |
| 746 |
self.assertEqual(re.match(r"^(\w){1,4}$", "abc").group(1), "c") |
| 747 |
self.assertEqual(re.match(r"^(\w){3,4}?$", "abc").group(1), "c") |
| 748 |
self.assertEqual(re.match(r"^(\w){3}?$", "abc").group(1), "c") |
| 749 |
self.assertEqual(re.match(r"^(\w){1,3}?$", "abc").group(1), "c") |
| 750 |
self.assertEqual(re.match(r"^(\w){1,4}?$", "abc").group(1), "c") |
| 751 |
self.assertEqual(re.match(r"^(\w){3,4}?$", "abc").group(1), "c") |
| 752 |
|
| 753 |
self.assertIsNone(re.match(r"^x{1}$", "xxx")) |
| 754 |
self.assertIsNone(re.match(r"^x{1}?$", "xxx")) |
| 755 |
self.assertIsNone(re.match(r"^x{1,2}$", "xxx")) |
| 756 |
self.assertIsNone(re.match(r"^x{1,2}?$", "xxx")) |
| 757 |
|
| 758 |
self.assertTrue(re.match(r"^x{3}$", "xxx")) |
| 759 |
self.assertTrue(re.match(r"^x{1,3}$", "xxx")) |
| 760 |
self.assertTrue(re.match(r"^x{3,3}$", "xxx")) |
| 761 |
self.assertTrue(re.match(r"^x{1,4}$", "xxx")) |
| 762 |
self.assertTrue(re.match(r"^x{3,4}?$", "xxx")) |
| 763 |
self.assertTrue(re.match(r"^x{3}?$", "xxx")) |
| 764 |
self.assertTrue(re.match(r"^x{1,3}?$", "xxx")) |
| 765 |
self.assertTrue(re.match(r"^x{1,4}?$", "xxx")) |
| 766 |
self.assertTrue(re.match(r"^x{3,4}?$", "xxx")) |
| 767 |
|
| 768 |
self.assertIsNone(re.match(r"^x{}$", "xxx")) |
| 769 |
self.assertTrue(re.match(r"^x{}$", "x{}")) |
| 770 |
|
| 771 |
self.checkPatternError(r'x{2,1}', |
| 772 |
'min repeat greater than max repeat', 2) |
| 773 |
|
| 774 |
def test_getattr(self): |
| 775 |
self.assertEqual(re.compile("(?i)(a)(b)").pattern, "(?i)(a)(b)") |
| 776 |
self.assertEqual(re.compile("(?i)(a)(b)").flags, re.I | re.U) |
| 777 |
self.assertEqual(re.compile("(?i)(a)(b)").groups, 2) |
| 778 |
self.assertEqual(re.compile("(?i)(a)(b)").groupindex, {}) |
| 779 |
self.assertEqual(re.compile("(?i)(?P<first>a)(?P<other>b)").groupindex, |
| 780 |
{'first': 1, 'other': 2}) |
| 781 |
|
| 782 |
self.assertEqual(re.match("(a)", "a").pos, 0) |
| 783 |
self.assertEqual(re.match("(a)", "a").endpos, 1) |
| 784 |
self.assertEqual(re.match("(a)", "a").string, "a") |
| 785 |
self.assertEqual(re.match("(a)", "a").regs, ((0, 1), (0, 1))) |
| 786 |
self.assertTrue(re.match("(a)", "a").re) |
| 787 |
|
| 788 |
# Issue 14260. groupindex should be non-modifiable mapping. |
| 789 |
p = re.compile(r'(?i)(?P<first>a)(?P<other>b)') |
| 790 |
self.assertEqual(sorted(p.groupindex), ['first', 'other']) |
| 791 |
self.assertEqual(p.groupindex['other'], 2) |
| 792 |
with self.assertRaises(TypeError): |
| 793 |
p.groupindex['other'] = 0 |
| 794 |
self.assertEqual(p.groupindex['other'], 2) |
| 795 |
|
| 796 |
def test_special_escapes(self): |
| 797 |
self.assertEqual(re.search(r"\b(b.)\b", |
| 798 |
"abcd abc bcd bx").group(1), "bx") |
| 799 |
self.assertEqual(re.search(r"\B(b.)\B", |
| 800 |
"abc bcd bc abxd").group(1), "bx") |
| 801 |
self.assertEqual(re.search(r"\b(b.)\b", |
| 802 |
"abcd abc bcd bx", re.ASCII).group(1), "bx") |
| 803 |
self.assertEqual(re.search(r"\B(b.)\B", |
| 804 |
"abc bcd bc abxd", re.ASCII).group(1), "bx") |
| 805 |
self.assertEqual(re.search(r"^abc$", "\nabc\n", re.M).group(0), "abc") |
| 806 |
self.assertEqual(re.search(r"^\Aabc\z$", "abc", re.M).group(0), "abc") |
| 807 |
self.assertIsNone(re.search(r"^\Aabc\z$", "\nabc\n", re.M)) |
| 808 |
self.assertEqual(re.search(r"^\Aabc\Z$", "abc", re.M).group(0), "abc") |
| 809 |
self.assertIsNone(re.search(r"^\Aabc\Z$", "\nabc\n", re.M)) |
| 810 |
self.assertEqual(re.search(br"\b(b.)\b", |
| 811 |
b"abcd abc bcd bx").group(1), b"bx") |
| 812 |
self.assertEqual(re.search(br"\B(b.)\B", |
| 813 |
b"abc bcd bc abxd").group(1), b"bx") |
| 814 |
self.assertEqual(re.search(br"\b(b.)\b", |
| 815 |
b"abcd abc bcd bx", re.LOCALE).group(1), b"bx") |
| 816 |
self.assertEqual(re.search(br"\B(b.)\B", |
| 817 |
b"abc bcd bc abxd", re.LOCALE).group(1), b"bx") |
| 818 |
self.assertEqual(re.search(br"^abc$", b"\nabc\n", re.M).group(0), b"abc") |
| 819 |
self.assertEqual(re.search(br"^\Aabc\z$", b"abc", re.M).group(0), b"abc") |
| 820 |
self.assertIsNone(re.search(br"^\Aabc\z$", b"\nabc\n", re.M)) |
| 821 |
self.assertEqual(re.search(br"^\Aabc\Z$", b"abc", re.M).group(0), b"abc") |
| 822 |
self.assertIsNone(re.search(br"^\Aabc\Z$", b"\nabc\n", re.M)) |
| 823 |
self.assertEqual(re.search(r"\d\D\w\W\s\S", |
| 824 |
"1aa! a").group(0), "1aa! a") |
| 825 |
self.assertEqual(re.search(br"\d\D\w\W\s\S", |
| 826 |
b"1aa! a").group(0), b"1aa! a") |
| 827 |
self.assertEqual(re.search(r"\d\D\w\W\s\S", |
| 828 |
"1aa! a", re.ASCII).group(0), "1aa! a") |
| 829 |
self.assertEqual(re.search(br"\d\D\w\W\s\S", |
| 830 |
b"1aa! a", re.LOCALE).group(0), b"1aa! a") |
| 831 |
|
| 832 |
def test_other_escapes(self): |
| 833 |
self.checkPatternError("\\", 'bad escape (end of pattern)', 0) |
| 834 |
self.assertEqual(re.match(r"\(", '(').group(), '(') |
| 835 |
self.assertIsNone(re.match(r"\(", ')')) |
| 836 |
self.assertEqual(re.match(r"\\", '\\').group(), '\\') |
| 837 |
self.assertEqual(re.match(r"[\]]", ']').group(), ']') |
| 838 |
self.assertIsNone(re.match(r"[\]]", '[')) |
| 839 |
self.assertEqual(re.match(r"[a\-c]", '-').group(), '-') |
| 840 |
self.assertIsNone(re.match(r"[a\-c]", 'b')) |
| 841 |
self.assertEqual(re.match(r"[\^a]+", 'a^').group(), 'a^') |
| 842 |
self.assertIsNone(re.match(r"[\^a]+", 'b')) |
| 843 |
re.purge() # for warnings |
| 844 |
for c in 'ceghijklmopqyCEFGHIJKLMNOPQRTVXY': |
| 845 |
with self.subTest(c): |
| 846 |
self.assertRaises(re.PatternError, re.compile, '\\%c' % c) |
| 847 |
for c in 'ceghijklmopqyzABCEFGHIJKLMNOPQRTVXYZ': |
| 848 |
with self.subTest(c): |
| 849 |
self.assertRaises(re.PatternError, re.compile, '[\\%c]' % c) |
| 850 |
|
| 851 |
def test_named_unicode_escapes(self): |
| 852 |
# test individual Unicode named escapes |
| 853 |
self.assertTrue(re.match(r'\N{LESS-THAN SIGN}', '<')) |
| 854 |
self.assertTrue(re.match(r'\N{less-than sign}', '<')) |
| 855 |
self.assertIsNone(re.match(r'\N{LESS-THAN SIGN}', '>')) |
| 856 |
self.assertTrue(re.match(r'\N{SNAKE}', '\U0001f40d')) |
| 857 |
self.assertTrue(re.match(r'\N{ARABIC LIGATURE UIGHUR KIRGHIZ YEH WITH ' |
| 858 |
r'HAMZA ABOVE WITH ALEF MAKSURA ISOLATED FORM}', |
| 859 |
'\ufbf9')) |
| 860 |
self.assertTrue(re.match(r'[\N{LESS-THAN SIGN}-\N{GREATER-THAN SIGN}]', |
| 861 |
'=')) |
| 862 |
self.assertIsNone(re.match(r'[\N{LESS-THAN SIGN}-\N{GREATER-THAN SIGN}]', |
| 863 |
';')) |
| 864 |
|
| 865 |
# test errors in \N{name} handling - only valid names should pass |
| 866 |
self.checkPatternError(r'\N', 'missing {', 2) |
| 867 |
self.checkPatternError(r'[\N]', 'missing {', 3) |
| 868 |
self.checkPatternError(r'\N{', 'missing character name', 3) |
| 869 |
self.checkPatternError(r'[\N{', 'missing character name', 4) |
| 870 |
self.checkPatternError(r'\N{}', 'missing character name', 3) |
| 871 |
self.checkPatternError(r'[\N{}]', 'missing character name', 4) |
| 872 |
self.checkPatternError(r'\NSNAKE}', 'missing {', 2) |
| 873 |
self.checkPatternError(r'[\NSNAKE}]', 'missing {', 3) |
| 874 |
self.checkPatternError(r'\N{SNAKE', |
| 875 |
'missing }, unterminated name', 3) |
| 876 |
self.checkPatternError(r'[\N{SNAKE]', |
| 877 |
'missing }, unterminated name', 4) |
| 878 |
self.checkPatternError(r'[\N{SNAKE]}', |
| 879 |
"undefined character name 'SNAKE]'", 1) |
| 880 |
self.checkPatternError(r'\N{SPAM}', |
| 881 |
"undefined character name 'SPAM'", 0) |
| 882 |
self.checkPatternError(r'[\N{SPAM}]', |
| 883 |
"undefined character name 'SPAM'", 1) |
| 884 |
self.checkPatternError(r'\N{KEYCAP NUMBER SIGN}', |
| 885 |
"undefined character name 'KEYCAP NUMBER SIGN'", 0) |
| 886 |
self.checkPatternError(r'[\N{KEYCAP NUMBER SIGN}]', |
| 887 |
"undefined character name 'KEYCAP NUMBER SIGN'", 1) |
| 888 |
self.checkPatternError(br'\N{LESS-THAN SIGN}', r'bad escape \N', 0) |
| 889 |
self.checkPatternError(br'[\N{LESS-THAN SIGN}]', r'bad escape \N', 1) |
| 890 |
|
| 891 |
def test_word_boundaries(self): |
| 892 |
# See http://bugs.python.org/issue10713 |
| 893 |
self.assertEqual(re.search(r"\b(abc)\b", "abc").group(1), "abc") |
| 894 |
self.assertEqual(re.search(r"\b(abc)\b", "abc", re.ASCII).group(1), "abc") |
| 895 |
self.assertEqual(re.search(br"\b(abc)\b", b"abc").group(1), b"abc") |
| 896 |
self.assertEqual(re.search(br"\b(abc)\b", b"abc", re.LOCALE).group(1), b"abc") |
| 897 |
self.assertEqual(re.search(r"\b(ьюя)\b", "ьюя").group(1), "ьюя") |
| 898 |
self.assertIsNone(re.search(r"\b(ьюя)\b", "ьюя", re.ASCII)) |
| 899 |
# There's a word boundary between a word and a non-word. |
| 900 |
self.assertTrue(re.match(r".\b", "a=")) |
| 901 |
self.assertTrue(re.match(r".\b", "a=", re.ASCII)) |
| 902 |
self.assertTrue(re.match(br".\b", b"a=")) |
| 903 |
self.assertTrue(re.match(br".\b", b"a=", re.LOCALE)) |
| 904 |
self.assertTrue(re.match(r".\b", "я=")) |
| 905 |
self.assertIsNone(re.match(r".\b", "я=", re.ASCII)) |
| 906 |
# There's a word boundary between a non-word and a word. |
| 907 |
self.assertTrue(re.match(r".\b", "=a")) |
| 908 |
self.assertTrue(re.match(r".\b", "=a", re.ASCII)) |
| 909 |
self.assertTrue(re.match(br".\b", b"=a")) |
| 910 |
self.assertTrue(re.match(br".\b", b"=a", re.LOCALE)) |
| 911 |
self.assertTrue(re.match(r".\b", "=я")) |
| 912 |
self.assertIsNone(re.match(r".\b", "=я", re.ASCII)) |
| 913 |
# There is no word boundary inside a word. |
| 914 |
self.assertIsNone(re.match(r".\b", "ab")) |
| 915 |
self.assertIsNone(re.match(r".\b", "ab", re.ASCII)) |
| 916 |
self.assertIsNone(re.match(br".\b", b"ab")) |
| 917 |
self.assertIsNone(re.match(br".\b", b"ab", re.LOCALE)) |
| 918 |
self.assertIsNone(re.match(r".\b", "юя")) |
| 919 |
self.assertIsNone(re.match(r".\b", "юя", re.ASCII)) |
| 920 |
# There is no word boundary between a non-word characters. |
| 921 |
self.assertIsNone(re.match(r".\b", "=-")) |
| 922 |
self.assertIsNone(re.match(r".\b", "=-", re.ASCII)) |
| 923 |
self.assertIsNone(re.match(br".\b", b"=-")) |
| 924 |
self.assertIsNone(re.match(br".\b", b"=-", re.LOCALE)) |
| 925 |
# There is no non-boundary match between a word and a non-word. |
| 926 |
self.assertIsNone(re.match(r".\B", "a=")) |
| 927 |
self.assertIsNone(re.match(r".\B", "a=", re.ASCII)) |
| 928 |
self.assertIsNone(re.match(br".\B", b"a=")) |
| 929 |
self.assertIsNone(re.match(br".\B", b"a=", re.LOCALE)) |
| 930 |
self.assertIsNone(re.match(r".\B", "я=")) |
| 931 |
self.assertTrue(re.match(r".\B", "я=", re.ASCII)) |
| 932 |
# There is no non-boundary match between a non-word and a word. |
| 933 |
self.assertIsNone(re.match(r".\B", "=a")) |
| 934 |
self.assertIsNone(re.match(r".\B", "=a", re.ASCII)) |
| 935 |
self.assertIsNone(re.match(br".\B", b"=a")) |
| 936 |
self.assertIsNone(re.match(br".\B", b"=a", re.LOCALE)) |
| 937 |
self.assertIsNone(re.match(r".\B", "=я")) |
| 938 |
self.assertTrue(re.match(r".\B", "=я", re.ASCII)) |
| 939 |
# There's a non-boundary match inside a word. |
| 940 |
self.assertTrue(re.match(r".\B", "ab")) |
| 941 |
self.assertTrue(re.match(r".\B", "ab", re.ASCII)) |
| 942 |
self.assertTrue(re.match(br".\B", b"ab")) |
| 943 |
self.assertTrue(re.match(br".\B", b"ab", re.LOCALE)) |
| 944 |
self.assertTrue(re.match(r".\B", "юя")) |
| 945 |
self.assertTrue(re.match(r".\B", "юя", re.ASCII)) |
| 946 |
# There's a non-boundary match between a non-word characters. |
| 947 |
self.assertTrue(re.match(r".\B", "=-")) |
| 948 |
self.assertTrue(re.match(r".\B", "=-", re.ASCII)) |
| 949 |
self.assertTrue(re.match(br".\B", b"=-")) |
| 950 |
self.assertTrue(re.match(br".\B", b"=-", re.LOCALE)) |
| 951 |
# There's a word boundary at the start of a string. |
| 952 |
self.assertTrue(re.match(r"\b", "abc")) |
| 953 |
self.assertTrue(re.match(r"\b", "abc", re.ASCII)) |
| 954 |
self.assertTrue(re.match(br"\b", b"abc")) |
| 955 |
self.assertTrue(re.match(br"\b", b"abc", re.LOCALE)) |
| 956 |
self.assertTrue(re.match(r"\b", "ьюя")) |
| 957 |
self.assertIsNone(re.match(r"\b", "ьюя", re.ASCII)) |
| 958 |
# There's a word boundary at the end of a string. |
| 959 |
self.assertTrue(re.fullmatch(r".+\b", "abc")) |
| 960 |
self.assertTrue(re.fullmatch(r".+\b", "abc", re.ASCII)) |
| 961 |
self.assertTrue(re.fullmatch(br".+\b", b"abc")) |
| 962 |
self.assertTrue(re.fullmatch(br".+\b", b"abc", re.LOCALE)) |
| 963 |
self.assertTrue(re.fullmatch(r".+\b", "ьюя")) |
| 964 |
self.assertIsNone(re.search(r"\b", "ьюя", re.ASCII)) |
| 965 |
# A non-empty string includes a non-boundary zero-length match. |
| 966 |
self.assertEqual(re.search(r"\B", "abc").span(), (1, 1)) |
| 967 |
self.assertEqual(re.search(r"\B", "abc", re.ASCII).span(), (1, 1)) |
| 968 |
self.assertEqual(re.search(br"\B", b"abc").span(), (1, 1)) |
| 969 |
self.assertEqual(re.search(br"\B", b"abc", re.LOCALE).span(), (1, 1)) |
| 970 |
self.assertEqual(re.search(r"\B", "ьюя").span(), (1, 1)) |
| 971 |
self.assertEqual(re.search(r"\B", "ьюя", re.ASCII).span(), (0, 0)) |
| 972 |
# There is no non-boundary match at the start of a string. |
| 973 |
self.assertIsNone(re.match(r"\B", "abc")) |
| 974 |
self.assertIsNone(re.match(r"\B", "abc", re.ASCII)) |
| 975 |
self.assertIsNone(re.match(br"\B", b"abc")) |
| 976 |
self.assertIsNone(re.match(br"\B", b"abc", re.LOCALE)) |
| 977 |
self.assertIsNone(re.match(r"\B", "ьюя")) |
| 978 |
self.assertTrue(re.match(r"\B", "ьюя", re.ASCII)) |
| 979 |
# There is no non-boundary match at the end of a string. |
| 980 |
self.assertIsNone(re.fullmatch(r".+\B", "abc")) |
| 981 |
self.assertIsNone(re.fullmatch(r".+\B", "abc", re.ASCII)) |
| 982 |
self.assertIsNone(re.fullmatch(br".+\B", b"abc")) |
| 983 |
self.assertIsNone(re.fullmatch(br".+\B", b"abc", re.LOCALE)) |
| 984 |
self.assertIsNone(re.fullmatch(r".+\B", "ьюя")) |
| 985 |
self.assertTrue(re.fullmatch(r".+\B", "ьюя", re.ASCII)) |
| 986 |
# However, an empty string contains no word boundaries. |
| 987 |
self.assertIsNone(re.search(r"\b", "")) |
| 988 |
self.assertIsNone(re.search(r"\b", "", re.ASCII)) |
| 989 |
self.assertIsNone(re.search(br"\b", b"")) |
| 990 |
self.assertIsNone(re.search(br"\b", b"", re.LOCALE)) |
| 991 |
self.assertTrue(re.search(r"\B", "")) |
| 992 |
self.assertTrue(re.search(r"\B", "", re.ASCII)) |
| 993 |
self.assertTrue(re.search(br"\B", b"")) |
| 994 |
self.assertTrue(re.search(br"\B", b"", re.LOCALE)) |
| 995 |
# A single word-character string has two boundaries, but no |
| 996 |
# non-boundary gaps. |
| 997 |
self.assertEqual(len(re.findall(r"\b", "a")), 2) |
| 998 |
self.assertEqual(len(re.findall(r"\b", "a", re.ASCII)), 2) |
| 999 |
self.assertEqual(len(re.findall(br"\b", b"a")), 2) |
| 1000 |
self.assertEqual(len(re.findall(br"\b", b"a", re.LOCALE)), 2) |
| 1001 |
self.assertEqual(len(re.findall(r"\B", "a")), 0) |
| 1002 |
self.assertEqual(len(re.findall(r"\B", "a", re.ASCII)), 0) |
| 1003 |
self.assertEqual(len(re.findall(br"\B", b"a")), 0) |
| 1004 |
self.assertEqual(len(re.findall(br"\B", b"a", re.LOCALE)), 0) |
| 1005 |
# If there are no words, there are no boundaries |
| 1006 |
self.assertEqual(len(re.findall(r"\b", " ")), 0) |
| 1007 |
self.assertEqual(len(re.findall(r"\b", " ", re.ASCII)), 0) |
| 1008 |
self.assertEqual(len(re.findall(br"\b", b" ")), 0) |
| 1009 |
self.assertEqual(len(re.findall(br"\b", b" ", re.LOCALE)), 0) |
| 1010 |
self.assertEqual(len(re.findall(r"\b", " ")), 0) |
| 1011 |
self.assertEqual(len(re.findall(r"\b", " ", re.ASCII)), 0) |
| 1012 |
self.assertEqual(len(re.findall(br"\b", b" ")), 0) |
| 1013 |
self.assertEqual(len(re.findall(br"\b", b" ", re.LOCALE)), 0) |
| 1014 |
# Can match around the whitespace. |
| 1015 |
self.assertEqual(len(re.findall(r"\B", " ")), 2) |
| 1016 |
self.assertEqual(len(re.findall(r"\B", " ", re.ASCII)), 2) |
| 1017 |
self.assertEqual(len(re.findall(br"\B", b" ")), 2) |
| 1018 |
self.assertEqual(len(re.findall(br"\B", b" ", re.LOCALE)), 2) |
| 1019 |
|
| 1020 |
def test_bigcharset(self): |
| 1021 |
self.assertEqual(re.match("([\u2222\u2223])", |
| 1022 |
"\u2222").group(1), "\u2222") |
| 1023 |
r = '[%s]' % ''.join(map(chr, range(256, 2**16, 255))) |
| 1024 |
self.assertEqual(re.match(r, "\uff01").group(), "\uff01") |
| 1025 |
|
| 1026 |
def test_big_codesize(self): |
| 1027 |
# Issue #1160 |
| 1028 |
r = re.compile('|'.join(('%d'%x for x in range(10000)))) |
| 1029 |
self.assertTrue(r.match('1000')) |
| 1030 |
self.assertTrue(r.match('9999')) |
| 1031 |
|
| 1032 |
def test_anyall(self): |
| 1033 |
self.assertEqual(re.match("a.b", "a\nb", re.DOTALL).group(0), |
| 1034 |
"a\nb") |
| 1035 |
self.assertEqual(re.match("a.*b", "a\n\nb", re.DOTALL).group(0), |
| 1036 |
"a\n\nb") |
| 1037 |
|
| 1038 |
def test_lookahead(self): |
| 1039 |
self.assertEqual(re.match(r"(a(?=\s[^a]))", "a b").group(1), "a") |
| 1040 |
self.assertEqual(re.match(r"(a(?=\s[^a]*))", "a b").group(1), "a") |
| 1041 |
self.assertEqual(re.match(r"(a(?=\s[abc]))", "a b").group(1), "a") |
| 1042 |
self.assertEqual(re.match(r"(a(?=\s[abc]*))", "a bc").group(1), "a") |
| 1043 |
self.assertEqual(re.match(r"(a)(?=\s\1)", "a a").group(1), "a") |
| 1044 |
self.assertEqual(re.match(r"(a)(?=\s\1*)", "a aa").group(1), "a") |
| 1045 |
self.assertEqual(re.match(r"(a)(?=\s(abc|a))", "a a").group(1), "a") |
| 1046 |
|
| 1047 |
self.assertEqual(re.match(r"(a(?!\s[^a]))", "a a").group(1), "a") |
| 1048 |
self.assertEqual(re.match(r"(a(?!\s[abc]))", "a d").group(1), "a") |
| 1049 |
self.assertEqual(re.match(r"(a)(?!\s\1)", "a b").group(1), "a") |
| 1050 |
self.assertEqual(re.match(r"(a)(?!\s(abc|a))", "a b").group(1), "a") |
| 1051 |
|
| 1052 |
# Group reference. |
| 1053 |
self.assertTrue(re.match(r'(a)b(?=\1)a', 'aba')) |
| 1054 |
self.assertIsNone(re.match(r'(a)b(?=\1)c', 'abac')) |
| 1055 |
# Conditional group reference. |
| 1056 |
self.assertTrue(re.match(r'(?:(a)|(x))b(?=(?(2)x|c))c', 'abc')) |
| 1057 |
self.assertIsNone(re.match(r'(?:(a)|(x))b(?=(?(2)c|x))c', 'abc')) |
| 1058 |
self.assertTrue(re.match(r'(?:(a)|(x))b(?=(?(2)x|c))c', 'abc')) |
| 1059 |
self.assertIsNone(re.match(r'(?:(a)|(x))b(?=(?(1)b|x))c', 'abc')) |
| 1060 |
self.assertTrue(re.match(r'(?:(a)|(x))b(?=(?(1)c|x))c', 'abc')) |
| 1061 |
# Group used before defined. |
| 1062 |
self.assertTrue(re.match(r'(a)b(?=(?(2)x|c))(c)', 'abc')) |
| 1063 |
self.assertIsNone(re.match(r'(a)b(?=(?(2)b|x))(c)', 'abc')) |
| 1064 |
self.assertTrue(re.match(r'(a)b(?=(?(1)c|x))(c)', 'abc')) |
| 1065 |
|
| 1066 |
def test_lookbehind(self): |
| 1067 |
self.assertTrue(re.match(r'ab(?<=b)c', 'abc')) |
| 1068 |
self.assertIsNone(re.match(r'ab(?<=c)c', 'abc')) |
| 1069 |
self.assertIsNone(re.match(r'ab(?<!b)c', 'abc')) |
| 1070 |
self.assertTrue(re.match(r'ab(?<!c)c', 'abc')) |
| 1071 |
# Group reference. |
| 1072 |
self.assertTrue(re.match(r'(a)a(?<=\1)c', 'aac')) |
| 1073 |
self.assertIsNone(re.match(r'(a)b(?<=\1)a', 'abaa')) |
| 1074 |
self.assertIsNone(re.match(r'(a)a(?<!\1)c', 'aac')) |
| 1075 |
self.assertTrue(re.match(r'(a)b(?<!\1)a', 'abaa')) |
| 1076 |
# Conditional group reference. |
| 1077 |
self.assertIsNone(re.match(r'(?:(a)|(x))b(?<=(?(2)x|c))c', 'abc')) |
| 1078 |
self.assertIsNone(re.match(r'(?:(a)|(x))b(?<=(?(2)b|x))c', 'abc')) |
| 1079 |
self.assertTrue(re.match(r'(?:(a)|(x))b(?<=(?(2)x|b))c', 'abc')) |
| 1080 |
self.assertIsNone(re.match(r'(?:(a)|(x))b(?<=(?(1)c|x))c', 'abc')) |
| 1081 |
self.assertTrue(re.match(r'(?:(a)|(x))b(?<=(?(1)b|x))c', 'abc')) |
| 1082 |
# Group used before defined. |
| 1083 |
self.assertRaises(re.PatternError, re.compile, r'(a)b(?<=(?(2)b|x))(c)') |
| 1084 |
self.assertIsNone(re.match(r'(a)b(?<=(?(1)c|x))(c)', 'abc')) |
| 1085 |
self.assertTrue(re.match(r'(a)b(?<=(?(1)b|x))(c)', 'abc')) |
| 1086 |
# Group defined in the same lookbehind pattern |
| 1087 |
self.assertRaises(re.PatternError, re.compile, r'(a)b(?<=(.)\2)(c)') |
| 1088 |
self.assertRaises(re.PatternError, re.compile, r'(a)b(?<=(?P<a>.)(?P=a))(c)') |
| 1089 |
self.assertRaises(re.PatternError, re.compile, r'(a)b(?<=(a)(?(2)b|x))(c)') |
| 1090 |
self.assertRaises(re.PatternError, re.compile, r'(a)b(?<=(.)(?<=\2))(c)') |
| 1091 |
|
| 1092 |
def test_ignore_case(self): |
| 1093 |
self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC") |
| 1094 |
self.assertEqual(re.match(b"abc", b"ABC", re.I).group(0), b"ABC") |
| 1095 |
self.assertEqual(re.match(r"(a\s[^a])", "a b", re.I).group(1), "a b") |
| 1096 |
self.assertEqual(re.match(r"(a\s[^a]*)", "a bb", re.I).group(1), "a bb") |
| 1097 |
self.assertEqual(re.match(r"(a\s[abc])", "a b", re.I).group(1), "a b") |
| 1098 |
self.assertEqual(re.match(r"(a\s[abc]*)", "a bb", re.I).group(1), "a bb") |
| 1099 |
self.assertEqual(re.match(r"((a)\s\2)", "a a", re.I).group(1), "a a") |
| 1100 |
self.assertEqual(re.match(r"((a)\s\2*)", "a aa", re.I).group(1), "a aa") |
| 1101 |
self.assertEqual(re.match(r"((a)\s(abc|a))", "a a", re.I).group(1), "a a") |
| 1102 |
self.assertEqual(re.match(r"((a)\s(abc|a)*)", "a aa", re.I).group(1), "a aa") |
| 1103 |
|
| 1104 |
# Two different characters have the same lowercase. |
| 1105 |
assert 'K'.lower() == '\u212a'.lower() == 'k' # 'K' |
| 1106 |
self.assertTrue(re.match(r'K', '\u212a', re.I)) |
| 1107 |
self.assertTrue(re.match(r'k', '\u212a', re.I)) |
| 1108 |
self.assertTrue(re.match(r'\u212a', 'K', re.I)) |
| 1109 |
self.assertTrue(re.match(r'\u212a', 'k', re.I)) |
| 1110 |
|
| 1111 |
# Two different characters have the same uppercase. |
| 1112 |
assert 's'.upper() == '\u017f'.upper() == 'S' # 'ſ' |
| 1113 |
self.assertTrue(re.match(r'S', '\u017f', re.I)) |
| 1114 |
self.assertTrue(re.match(r's', '\u017f', re.I)) |
| 1115 |
self.assertTrue(re.match(r'\u017f', 'S', re.I)) |
| 1116 |
self.assertTrue(re.match(r'\u017f', 's', re.I)) |
| 1117 |
|
| 1118 |
# Two different characters have the same uppercase. Unicode 9.0+. |
| 1119 |
assert '\u0432'.upper() == '\u1c80'.upper() == '\u0412' # 'в', 'ᲀ', 'В' |
| 1120 |
self.assertTrue(re.match(r'\u0412', '\u0432', re.I)) |
| 1121 |
self.assertTrue(re.match(r'\u0412', '\u1c80', re.I)) |
| 1122 |
self.assertTrue(re.match(r'\u0432', '\u0412', re.I)) |
| 1123 |
self.assertTrue(re.match(r'\u0432', '\u1c80', re.I)) |
| 1124 |
self.assertTrue(re.match(r'\u1c80', '\u0412', re.I)) |
| 1125 |
self.assertTrue(re.match(r'\u1c80', '\u0432', re.I)) |
| 1126 |
|
| 1127 |
# Two different characters have the same multicharacter uppercase. |
| 1128 |
assert '\ufb05'.upper() == '\ufb06'.upper() == 'ST' # 'ſt', 'st' |
| 1129 |
self.assertTrue(re.match(r'\ufb05', '\ufb06', re.I)) |
| 1130 |
self.assertTrue(re.match(r'\ufb06', '\ufb05', re.I)) |
| 1131 |
|
| 1132 |
def test_ignore_case_set(self): |
| 1133 |
self.assertTrue(re.match(r'[19A]', 'A', re.I)) |
| 1134 |
self.assertTrue(re.match(r'[19a]', 'a', re.I)) |
| 1135 |
self.assertTrue(re.match(r'[19a]', 'A', re.I)) |
| 1136 |
self.assertTrue(re.match(r'[19A]', 'a', re.I)) |
| 1137 |
self.assertTrue(re.match(br'[19A]', b'A', re.I)) |
| 1138 |
self.assertTrue(re.match(br'[19a]', b'a', re.I)) |
| 1139 |
self.assertTrue(re.match(br'[19a]', b'A', re.I)) |
| 1140 |
self.assertTrue(re.match(br'[19A]', b'a', re.I)) |
| 1141 |
self.assertTrue(re.match(r'[19\xc7]', '\xc7', re.I)) |
| 1142 |
self.assertTrue(re.match(r'[19\xc7]', '\xe7', re.I)) |
| 1143 |
self.assertTrue(re.match(r'[19\xe7]', '\xc7', re.I)) |
| 1144 |
self.assertTrue(re.match(r'[19\xe7]', '\xe7', re.I)) |
| 1145 |
self.assertTrue(re.match(r'[19\u0400]', '\u0400', re.I)) |
| 1146 |
self.assertTrue(re.match(r'[19\u0400]', '\u0450', re.I)) |
| 1147 |
self.assertTrue(re.match(r'[19\u0450]', '\u0400', re.I)) |
| 1148 |
self.assertTrue(re.match(r'[19\u0450]', '\u0450', re.I)) |
| 1149 |
self.assertTrue(re.match(r'[19\U00010400]', '\U00010400', re.I)) |
| 1150 |
self.assertTrue(re.match(r'[19\U00010400]', '\U00010428', re.I)) |
| 1151 |
self.assertTrue(re.match(r'[19\U00010428]', '\U00010400', re.I)) |
| 1152 |
self.assertTrue(re.match(r'[19\U00010428]', '\U00010428', re.I)) |
| 1153 |
|
| 1154 |
self.assertTrue(re.match(br'[19A]', b'A', re.I)) |
| 1155 |
self.assertTrue(re.match(br'[19a]', b'a', re.I)) |
| 1156 |
self.assertTrue(re.match(br'[19a]', b'A', re.I)) |
| 1157 |
self.assertTrue(re.match(br'[19A]', b'a', re.I)) |
| 1158 |
self.assertTrue(re.match(r'[19A]', 'A', re.I|re.A)) |
| 1159 |
self.assertTrue(re.match(r'[19a]', 'a', re.I|re.A)) |
| 1160 |
self.assertTrue(re.match(r'[19a]', 'A', re.I|re.A)) |
| 1161 |
self.assertTrue(re.match(r'[19A]', 'a', re.I|re.A)) |
| 1162 |
self.assertTrue(re.match(r'[19\xc7]', '\xc7', re.I|re.A)) |
| 1163 |
self.assertIsNone(re.match(r'[19\xc7]', '\xe7', re.I|re.A)) |
| 1164 |
self.assertIsNone(re.match(r'[19\xe7]', '\xc7', re.I|re.A)) |
| 1165 |
self.assertTrue(re.match(r'[19\xe7]', '\xe7', re.I|re.A)) |
| 1166 |
self.assertTrue(re.match(r'[19\u0400]', '\u0400', re.I|re.A)) |
| 1167 |
self.assertIsNone(re.match(r'[19\u0400]', '\u0450', re.I|re.A)) |
| 1168 |
self.assertIsNone(re.match(r'[19\u0450]', '\u0400', re.I|re.A)) |
| 1169 |
self.assertTrue(re.match(r'[19\u0450]', '\u0450', re.I|re.A)) |
| 1170 |
self.assertTrue(re.match(r'[19\U00010400]', '\U00010400', re.I|re.A)) |
| 1171 |
self.assertIsNone(re.match(r'[19\U00010400]', '\U00010428', re.I|re.A)) |
| 1172 |
self.assertIsNone(re.match(r'[19\U00010428]', '\U00010400', re.I|re.A)) |
| 1173 |
self.assertTrue(re.match(r'[19\U00010428]', '\U00010428', re.I|re.A)) |
| 1174 |
|
| 1175 |
# Two different characters have the same lowercase. |
| 1176 |
assert 'K'.lower() == '\u212a'.lower() == 'k' # 'K' |
| 1177 |
self.assertTrue(re.match(r'[19K]', '\u212a', re.I)) |
| 1178 |
self.assertTrue(re.match(r'[19k]', '\u212a', re.I)) |
| 1179 |
self.assertTrue(re.match(r'[19\u212a]', 'K', re.I)) |
| 1180 |
self.assertTrue(re.match(r'[19\u212a]', 'k', re.I)) |
| 1181 |
|
| 1182 |
# Two different characters have the same uppercase. |
| 1183 |
assert 's'.upper() == '\u017f'.upper() == 'S' # 'ſ' |
| 1184 |
self.assertTrue(re.match(r'[19S]', '\u017f', re.I)) |
| 1185 |
self.assertTrue(re.match(r'[19s]', '\u017f', re.I)) |
| 1186 |
self.assertTrue(re.match(r'[19\u017f]', 'S', re.I)) |
| 1187 |
self.assertTrue(re.match(r'[19\u017f]', 's', re.I)) |
| 1188 |
|
| 1189 |
# Two different characters have the same uppercase. Unicode 9.0+. |
| 1190 |
assert '\u0432'.upper() == '\u1c80'.upper() == '\u0412' # 'в', 'ᲀ', 'В' |
| 1191 |
self.assertTrue(re.match(r'[19\u0412]', '\u0432', re.I)) |
| 1192 |
self.assertTrue(re.match(r'[19\u0412]', '\u1c80', re.I)) |
| 1193 |
self.assertTrue(re.match(r'[19\u0432]', '\u0412', re.I)) |
| 1194 |
self.assertTrue(re.match(r'[19\u0432]', '\u1c80', re.I)) |
| 1195 |
self.assertTrue(re.match(r'[19\u1c80]', '\u0412', re.I)) |
| 1196 |
self.assertTrue(re.match(r'[19\u1c80]', '\u0432', re.I)) |
| 1197 |
|
| 1198 |
# Two different characters have the same multicharacter uppercase. |
| 1199 |
assert '\ufb05'.upper() == '\ufb06'.upper() == 'ST' # 'ſt', 'st' |
| 1200 |
self.assertTrue(re.match(r'[19\ufb05]', '\ufb06', re.I)) |
| 1201 |
self.assertTrue(re.match(r'[19\ufb06]', '\ufb05', re.I)) |
| 1202 |
|
| 1203 |
def test_ignore_case_range(self): |
| 1204 |
# Issues #3511, #17381. |
| 1205 |
self.assertTrue(re.match(r'[9-a]', '_', re.I)) |
| 1206 |
self.assertIsNone(re.match(r'[9-A]', '_', re.I)) |
| 1207 |
self.assertTrue(re.match(br'[9-a]', b'_', re.I)) |
| 1208 |
self.assertIsNone(re.match(br'[9-A]', b'_', re.I)) |
| 1209 |
self.assertTrue(re.match(r'[\xc0-\xde]', '\xd7', re.I)) |
| 1210 |
self.assertTrue(re.match(r'[\xc0-\xde]', '\xe7', re.I)) |
| 1211 |
self.assertIsNone(re.match(r'[\xc0-\xde]', '\xf7', re.I)) |
| 1212 |
self.assertTrue(re.match(r'[\xe0-\xfe]', '\xf7', re.I)) |
| 1213 |
self.assertTrue(re.match(r'[\xe0-\xfe]', '\xc7', re.I)) |
| 1214 |
self.assertIsNone(re.match(r'[\xe0-\xfe]', '\xd7', re.I)) |
| 1215 |
self.assertTrue(re.match(r'[\u0430-\u045f]', '\u0450', re.I)) |
| 1216 |
self.assertTrue(re.match(r'[\u0430-\u045f]', '\u0400', re.I)) |
| 1217 |
self.assertTrue(re.match(r'[\u0400-\u042f]', '\u0450', re.I)) |
| 1218 |
self.assertTrue(re.match(r'[\u0400-\u042f]', '\u0400', re.I)) |
| 1219 |
self.assertTrue(re.match(r'[\U00010428-\U0001044f]', '\U00010428', re.I)) |
| 1220 |
self.assertTrue(re.match(r'[\U00010428-\U0001044f]', '\U00010400', re.I)) |
| 1221 |
self.assertTrue(re.match(r'[\U00010400-\U00010427]', '\U00010428', re.I)) |
| 1222 |
self.assertTrue(re.match(r'[\U00010400-\U00010427]', '\U00010400', re.I)) |
| 1223 |
|
| 1224 |
self.assertTrue(re.match(r'[\xc0-\xde]', '\xd7', re.I|re.A)) |
| 1225 |
self.assertIsNone(re.match(r'[\xc0-\xde]', '\xe7', re.I|re.A)) |
| 1226 |
self.assertTrue(re.match(r'[\xe0-\xfe]', '\xf7', re.I|re.A)) |
| 1227 |
self.assertIsNone(re.match(r'[\xe0-\xfe]', '\xc7', re.I|re.A)) |
| 1228 |
self.assertTrue(re.match(r'[\u0430-\u045f]', '\u0450', re.I|re.A)) |
| 1229 |
self.assertIsNone(re.match(r'[\u0430-\u045f]', '\u0400', re.I|re.A)) |
| 1230 |
self.assertIsNone(re.match(r'[\u0400-\u042f]', '\u0450', re.I|re.A)) |
| 1231 |
self.assertTrue(re.match(r'[\u0400-\u042f]', '\u0400', re.I|re.A)) |
| 1232 |
self.assertTrue(re.match(r'[\U00010428-\U0001044f]', '\U00010428', re.I|re.A)) |
| 1233 |
self.assertIsNone(re.match(r'[\U00010428-\U0001044f]', '\U00010400', re.I|re.A)) |
| 1234 |
self.assertIsNone(re.match(r'[\U00010400-\U00010427]', '\U00010428', re.I|re.A)) |
| 1235 |
self.assertTrue(re.match(r'[\U00010400-\U00010427]', '\U00010400', re.I|re.A)) |
| 1236 |
|
| 1237 |
self.assertTrue(re.match(r'[N-\x7f]', 'A', re.I|re.A)) |
| 1238 |
self.assertTrue(re.match(r'[n-\x7f]', 'Z', re.I|re.A)) |
| 1239 |
self.assertTrue(re.match(r'[N-\uffff]', 'A', re.I|re.A)) |
| 1240 |
self.assertTrue(re.match(r'[n-\uffff]', 'Z', re.I|re.A)) |
| 1241 |
self.assertTrue(re.match(r'[N-\U00010000]', 'A', re.I|re.A)) |
| 1242 |
self.assertTrue(re.match(r'[n-\U00010000]', 'Z', re.I|re.A)) |
| 1243 |
|
| 1244 |
# Two different characters have the same lowercase. |
| 1245 |
assert 'K'.lower() == '\u212a'.lower() == 'k' # 'K' |
| 1246 |
self.assertTrue(re.match(r'[J-M]', '\u212a', re.I)) |
| 1247 |
self.assertTrue(re.match(r'[j-m]', '\u212a', re.I)) |
| 1248 |
self.assertTrue(re.match(r'[\u2129-\u212b]', 'K', re.I)) |
| 1249 |
self.assertTrue(re.match(r'[\u2129-\u212b]', 'k', re.I)) |
| 1250 |
|
| 1251 |
# Two different characters have the same uppercase. |
| 1252 |
assert 's'.upper() == '\u017f'.upper() == 'S' # 'ſ' |
| 1253 |
self.assertTrue(re.match(r'[R-T]', '\u017f', re.I)) |
| 1254 |
self.assertTrue(re.match(r'[r-t]', '\u017f', re.I)) |
| 1255 |
self.assertTrue(re.match(r'[\u017e-\u0180]', 'S', re.I)) |
| 1256 |
self.assertTrue(re.match(r'[\u017e-\u0180]', 's', re.I)) |
| 1257 |
|
| 1258 |
# Two different characters have the same uppercase. Unicode 9.0+. |
| 1259 |
assert '\u0432'.upper() == '\u1c80'.upper() == '\u0412' # 'в', 'ᲀ', 'В' |
| 1260 |
self.assertTrue(re.match(r'[\u0411-\u0413]', '\u0432', re.I)) |
| 1261 |
self.assertTrue(re.match(r'[\u0411-\u0413]', '\u1c80', re.I)) |
| 1262 |
self.assertTrue(re.match(r'[\u0431-\u0433]', '\u0412', re.I)) |
| 1263 |
self.assertTrue(re.match(r'[\u0431-\u0433]', '\u1c80', re.I)) |
| 1264 |
self.assertTrue(re.match(r'[\u1c80-\u1c82]', '\u0412', re.I)) |
| 1265 |
self.assertTrue(re.match(r'[\u1c80-\u1c82]', '\u0432', re.I)) |
| 1266 |
|
| 1267 |
# Two different characters have the same multicharacter uppercase. |
| 1268 |
assert '\ufb05'.upper() == '\ufb06'.upper() == 'ST' # 'ſt', 'st' |
| 1269 |
self.assertTrue(re.match(r'[\ufb04-\ufb05]', '\ufb06', re.I)) |
| 1270 |
self.assertTrue(re.match(r'[\ufb06-\ufb07]', '\ufb05', re.I)) |
| 1271 |
|
| 1272 |
def test_category(self): |
| 1273 |
self.assertEqual(re.match(r"(\s)", " ").group(1), " ") |
| 1274 |
|
| 1275 |
def test_not_literal(self): |
| 1276 |
self.assertEqual(re.search(r"\s([^a])", " b").group(1), "b") |
| 1277 |
self.assertEqual(re.search(r"\s([^a]*)", " bb").group(1), "bb") |
| 1278 |
|
| 1279 |
def test_possible_set_operations(self): |
| 1280 |
s = bytes(range(128)).decode() |
| 1281 |
with self.assertWarnsRegex(FutureWarning, 'Possible set difference') as w: |
| 1282 |
p = re.compile(r'[0-9--1]') |
| 1283 |
self.assertEqual(w.filename, __file__) |
| 1284 |
self.assertEqual(p.findall(s), list('-./0123456789')) |
| 1285 |
with self.assertWarnsRegex(FutureWarning, 'Possible set difference') as w: |
| 1286 |
self.assertEqual(re.findall(r'[0-9--2]', s), list('-./0123456789')) |
| 1287 |
self.assertEqual(w.filename, __file__) |
| 1288 |
|
| 1289 |
self.assertEqual(re.findall(r'[--1]', s), list('-./01')) |
| 1290 |
|
| 1291 |
with self.assertWarnsRegex(FutureWarning, 'Possible set difference') as w: |
| 1292 |
p = re.compile(r'[%--1]') |
| 1293 |
self.assertEqual(w.filename, __file__) |
| 1294 |
self.assertEqual(p.findall(s), list("%&'()*+,-1")) |
| 1295 |
|
| 1296 |
with self.assertWarnsRegex(FutureWarning, 'Possible set difference ') as w: |
| 1297 |
p = re.compile(r'[%--]') |
| 1298 |
self.assertEqual(w.filename, __file__) |
| 1299 |
self.assertEqual(p.findall(s), list("%&'()*+,-")) |
| 1300 |
|
| 1301 |
with self.assertWarnsRegex(FutureWarning, 'Possible set intersection ') as w: |
| 1302 |
p = re.compile(r'[0-9&&1]') |
| 1303 |
self.assertEqual(w.filename, __file__) |
| 1304 |
self.assertEqual(p.findall(s), list('&0123456789')) |
| 1305 |
with self.assertWarnsRegex(FutureWarning, 'Possible set intersection ') as w: |
| 1306 |
self.assertEqual(re.findall(r'[0-8&&1]', s), list('&012345678')) |
| 1307 |
self.assertEqual(w.filename, __file__) |
| 1308 |
|
| 1309 |
with self.assertWarnsRegex(FutureWarning, 'Possible set intersection ') as w: |
| 1310 |
p = re.compile(r'[\d&&1]') |
| 1311 |
self.assertEqual(w.filename, __file__) |
| 1312 |
self.assertEqual(p.findall(s), list('&0123456789')) |
| 1313 |
|
| 1314 |
self.assertEqual(re.findall(r'[&&1]', s), list('&1')) |
| 1315 |
|
| 1316 |
with self.assertWarnsRegex(FutureWarning, 'Possible set union ') as w: |
| 1317 |
p = re.compile(r'[0-9||a]') |
| 1318 |
self.assertEqual(w.filename, __file__) |
| 1319 |
self.assertEqual(p.findall(s), list('0123456789a|')) |
| 1320 |
|
| 1321 |
with self.assertWarnsRegex(FutureWarning, 'Possible set union ') as w: |
| 1322 |
p = re.compile(r'[\d||a]') |
| 1323 |
self.assertEqual(w.filename, __file__) |
| 1324 |
self.assertEqual(p.findall(s), list('0123456789a|')) |
| 1325 |
|
| 1326 |
self.assertEqual(re.findall(r'[||1]', s), list('1|')) |
| 1327 |
|
| 1328 |
with self.assertWarnsRegex(FutureWarning, 'Possible set symmetric difference ') as w: |
| 1329 |
p = re.compile(r'[0-9~~1]') |
| 1330 |
self.assertEqual(w.filename, __file__) |
| 1331 |
self.assertEqual(p.findall(s), list('0123456789~')) |
| 1332 |
|
| 1333 |
with self.assertWarnsRegex(FutureWarning, 'Possible set symmetric difference ') as w: |
| 1334 |
p = re.compile(r'[\d~~1]') |
| 1335 |
self.assertEqual(w.filename, __file__) |
| 1336 |
self.assertEqual(p.findall(s), list('0123456789~')) |
| 1337 |
|
| 1338 |
self.assertEqual(re.findall(r'[~~1]', s), list('1~')) |
| 1339 |
|
| 1340 |
with self.assertWarnsRegex(FutureWarning, 'Possible nested set ') as w: |
| 1341 |
p = re.compile(r'[[0-9]|]') |
| 1342 |
self.assertEqual(w.filename, __file__) |
| 1343 |
self.assertEqual(p.findall(s), list('0123456789[]')) |
| 1344 |
with self.assertWarnsRegex(FutureWarning, 'Possible nested set ') as w: |
| 1345 |
self.assertEqual(re.findall(r'[[0-8]|]', s), list('012345678[]')) |
| 1346 |
self.assertEqual(w.filename, __file__) |
| 1347 |
|
| 1348 |
with self.assertWarnsRegex(FutureWarning, 'Possible nested set ') as w: |
| 1349 |
p = re.compile(r'[[:digit:]|]') |
| 1350 |
self.assertEqual(w.filename, __file__) |
| 1351 |
self.assertEqual(p.findall(s), list(':[]dgit')) |
| 1352 |
|
| 1353 |
def test_search_coverage(self): |
| 1354 |
self.assertEqual(re.search(r"\s(b)", " b").group(1), "b") |
| 1355 |
self.assertEqual(re.search(r"a\s", "a ").group(0), "a ") |
| 1356 |
|
| 1357 |
def assertMatch(self, pattern, text, match=None, span=None, |
| 1358 |
matcher=re.fullmatch): |
| 1359 |
if match is None and span is None: |
| 1360 |
# the pattern matches the whole text |
| 1361 |
match = text |
| 1362 |
span = (0, len(text)) |
| 1363 |
elif match is None or span is None: |
| 1364 |
raise ValueError('If match is not None, span should be specified ' |
| 1365 |
'(and vice versa).') |
| 1366 |
m = matcher(pattern, text) |
| 1367 |
self.assertTrue(m) |
| 1368 |
self.assertEqual(m.group(), match) |
| 1369 |
self.assertEqual(m.span(), span) |
| 1370 |
|
| 1371 |
LITERAL_CHARS = string.ascii_letters + string.digits + '!"%\',/:;<=>@_`' |
| 1372 |
|
| 1373 |
def test_re_escape(self): |
| 1374 |
p = ''.join(chr(i) for i in range(256)) |
| 1375 |
for c in p: |
| 1376 |
self.assertMatch(re.escape(c), c) |
| 1377 |
self.assertMatch('[' + re.escape(c) + ']', c) |
| 1378 |
self.assertMatch('(?x)' + re.escape(c), c) |
| 1379 |
self.assertMatch(re.escape(p), p) |
| 1380 |
for c in '-.]{}': |
| 1381 |
self.assertEqual(re.escape(c)[:1], '\\') |
| 1382 |
literal_chars = self.LITERAL_CHARS |
| 1383 |
self.assertEqual(re.escape(literal_chars), literal_chars) |
| 1384 |
|
| 1385 |
def test_re_escape_bytes(self): |
| 1386 |
p = bytes(range(256)) |
| 1387 |
for i in p: |
| 1388 |
b = bytes([i]) |
| 1389 |
self.assertMatch(re.escape(b), b) |
| 1390 |
self.assertMatch(b'[' + re.escape(b) + b']', b) |
| 1391 |
self.assertMatch(b'(?x)' + re.escape(b), b) |
| 1392 |
self.assertMatch(re.escape(p), p) |
| 1393 |
for i in b'-.]{}': |
| 1394 |
b = bytes([i]) |
| 1395 |
self.assertEqual(re.escape(b)[:1], b'\\') |
| 1396 |
literal_chars = self.LITERAL_CHARS.encode('ascii') |
| 1397 |
self.assertEqual(re.escape(literal_chars), literal_chars) |
| 1398 |
|
| 1399 |
def test_re_escape_non_ascii(self): |
| 1400 |
s = 'xxx\u2620\u2620\u2620xxx' |
| 1401 |
s_escaped = re.escape(s) |
| 1402 |
self.assertEqual(s_escaped, s) |
| 1403 |
self.assertMatch(s_escaped, s) |
| 1404 |
self.assertMatch('.%s+.' % re.escape('\u2620'), s, |
| 1405 |
'x\u2620\u2620\u2620x', (2, 7), re.search) |
| 1406 |
|
| 1407 |
def test_re_escape_non_ascii_bytes(self): |
| 1408 |
b = 'y\u2620y\u2620y'.encode('utf-8') |
| 1409 |
b_escaped = re.escape(b) |
| 1410 |
self.assertEqual(b_escaped, b) |
| 1411 |
self.assertMatch(b_escaped, b) |
| 1412 |
res = re.findall(re.escape('\u2620'.encode('utf-8')), b) |
| 1413 |
self.assertEqual(len(res), 2) |
| 1414 |
|
| 1415 |
def test_pickling(self): |
| 1416 |
import pickle |
| 1417 |
oldpat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)', re.UNICODE) |
| 1418 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1419 |
pickled = pickle.dumps(oldpat, proto) |
| 1420 |
newpat = pickle.loads(pickled) |
| 1421 |
self.assertEqual(newpat, oldpat) |
| 1422 |
# current pickle expects the _compile() reconstructor in re module |
| 1423 |
from re import _compile # noqa: F401 |
| 1424 |
|
| 1425 |
def test_copying(self): |
| 1426 |
import copy |
| 1427 |
p = re.compile(r'(?P<int>\d+)(?:\.(?P<frac>\d*))?') |
| 1428 |
self.assertIs(copy.copy(p), p) |
| 1429 |
self.assertIs(copy.deepcopy(p), p) |
| 1430 |
m = p.match('12.34') |
| 1431 |
self.assertIs(copy.copy(m), m) |
| 1432 |
self.assertIs(copy.deepcopy(m), m) |
| 1433 |
|
| 1434 |
def test_constants(self): |
| 1435 |
self.assertEqual(re.I, re.IGNORECASE) |
| 1436 |
self.assertEqual(re.L, re.LOCALE) |
| 1437 |
self.assertEqual(re.M, re.MULTILINE) |
| 1438 |
self.assertEqual(re.S, re.DOTALL) |
| 1439 |
self.assertEqual(re.X, re.VERBOSE) |
| 1440 |
|
| 1441 |
def test_flags(self): |
| 1442 |
for flag in [re.I, re.M, re.X, re.S, re.A, re.U]: |
| 1443 |
self.assertTrue(re.compile('^pattern$', flag)) |
| 1444 |
for flag in [re.I, re.M, re.X, re.S, re.A, re.L]: |
| 1445 |
self.assertTrue(re.compile(b'^pattern$', flag)) |
| 1446 |
|
| 1447 |
def test_sre_character_literals(self): |
| 1448 |
for i in [0, 8, 16, 32, 64, 127, 128, 255, 256, 0xFFFF, 0x10000, 0x10FFFF]: |
| 1449 |
if i < 256: |
| 1450 |
self.assertTrue(re.match(r"\%03o" % i, chr(i))) |
| 1451 |
self.assertTrue(re.match(r"\%03o0" % i, chr(i)+"0")) |
| 1452 |
self.assertTrue(re.match(r"\%03o8" % i, chr(i)+"8")) |
| 1453 |
self.assertTrue(re.match(r"\x%02x" % i, chr(i))) |
| 1454 |
self.assertTrue(re.match(r"\x%02x0" % i, chr(i)+"0")) |
| 1455 |
self.assertTrue(re.match(r"\x%02xz" % i, chr(i)+"z")) |
| 1456 |
if i < 0x10000: |
| 1457 |
self.assertTrue(re.match(r"\u%04x" % i, chr(i))) |
| 1458 |
self.assertTrue(re.match(r"\u%04x0" % i, chr(i)+"0")) |
| 1459 |
self.assertTrue(re.match(r"\u%04xz" % i, chr(i)+"z")) |
| 1460 |
self.assertTrue(re.match(r"\U%08x" % i, chr(i))) |
| 1461 |
self.assertTrue(re.match(r"\U%08x0" % i, chr(i)+"0")) |
| 1462 |
self.assertTrue(re.match(r"\U%08xz" % i, chr(i)+"z")) |
| 1463 |
self.assertTrue(re.match(r"\0", "\000")) |
| 1464 |
self.assertTrue(re.match(r"\08", "\0008")) |
| 1465 |
self.assertTrue(re.match(r"\01", "\001")) |
| 1466 |
self.assertTrue(re.match(r"\018", "\0018")) |
| 1467 |
self.checkPatternError(r"\567", |
| 1468 |
r'octal escape value \567 outside of ' |
| 1469 |
r'range 0-0o377', 0) |
| 1470 |
self.checkPatternError(r"\911", 'invalid group reference 91', 1) |
| 1471 |
self.checkPatternError(r"\x1", r'incomplete escape \x1', 0) |
| 1472 |
self.checkPatternError(r"\x1z", r'incomplete escape \x1', 0) |
| 1473 |
self.checkPatternError(r"\u123", r'incomplete escape \u123', 0) |
| 1474 |
self.checkPatternError(r"\u123z", r'incomplete escape \u123', 0) |
| 1475 |
self.checkPatternError(r"\U0001234", r'incomplete escape \U0001234', 0) |
| 1476 |
self.checkPatternError(r"\U0001234z", r'incomplete escape \U0001234', 0) |
| 1477 |
self.checkPatternError(r"\U00110000", r'bad escape \U00110000', 0) |
| 1478 |
|
| 1479 |
def test_sre_character_class_literals(self): |
| 1480 |
for i in [0, 8, 16, 32, 64, 127, 128, 255, 256, 0xFFFF, 0x10000, 0x10FFFF]: |
| 1481 |
if i < 256: |
| 1482 |
self.assertTrue(re.match(r"[\%o]" % i, chr(i))) |
| 1483 |
self.assertTrue(re.match(r"[\%o8]" % i, chr(i))) |
| 1484 |
self.assertTrue(re.match(r"[\%03o]" % i, chr(i))) |
| 1485 |
self.assertTrue(re.match(r"[\%03o0]" % i, chr(i))) |
| 1486 |
self.assertTrue(re.match(r"[\%03o8]" % i, chr(i))) |
| 1487 |
self.assertTrue(re.match(r"[\x%02x]" % i, chr(i))) |
| 1488 |
self.assertTrue(re.match(r"[\x%02x0]" % i, chr(i))) |
| 1489 |
self.assertTrue(re.match(r"[\x%02xz]" % i, chr(i))) |
| 1490 |
if i < 0x10000: |
| 1491 |
self.assertTrue(re.match(r"[\u%04x]" % i, chr(i))) |
| 1492 |
self.assertTrue(re.match(r"[\u%04x0]" % i, chr(i))) |
| 1493 |
self.assertTrue(re.match(r"[\u%04xz]" % i, chr(i))) |
| 1494 |
self.assertTrue(re.match(r"[\U%08x]" % i, chr(i))) |
| 1495 |
self.assertTrue(re.match(r"[\U%08x0]" % i, chr(i)+"0")) |
| 1496 |
self.assertTrue(re.match(r"[\U%08xz]" % i, chr(i)+"z")) |
| 1497 |
self.checkPatternError(r"[\567]", |
| 1498 |
r'octal escape value \567 outside of ' |
| 1499 |
r'range 0-0o377', 1) |
| 1500 |
self.checkPatternError(r"[\911]", r'bad escape \9', 1) |
| 1501 |
self.checkPatternError(r"[\x1z]", r'incomplete escape \x1', 1) |
| 1502 |
self.checkPatternError(r"[\u123z]", r'incomplete escape \u123', 1) |
| 1503 |
self.checkPatternError(r"[\U0001234z]", r'incomplete escape \U0001234', 1) |
| 1504 |
self.checkPatternError(r"[\U00110000]", r'bad escape \U00110000', 1) |
| 1505 |
self.assertTrue(re.match(r"[\U0001d49c-\U0001d4b5]", "\U0001d49e")) |
| 1506 |
|
| 1507 |
def test_sre_byte_literals(self): |
| 1508 |
for i in [0, 8, 16, 32, 64, 127, 128, 255]: |
| 1509 |
self.assertTrue(re.match((r"\%03o" % i).encode(), bytes([i]))) |
| 1510 |
self.assertTrue(re.match((r"\%03o0" % i).encode(), bytes([i])+b"0")) |
| 1511 |
self.assertTrue(re.match((r"\%03o8" % i).encode(), bytes([i])+b"8")) |
| 1512 |
self.assertTrue(re.match((r"\x%02x" % i).encode(), bytes([i]))) |
| 1513 |
self.assertTrue(re.match((r"\x%02x0" % i).encode(), bytes([i])+b"0")) |
| 1514 |
self.assertTrue(re.match((r"\x%02xz" % i).encode(), bytes([i])+b"z")) |
| 1515 |
self.assertRaises(re.PatternError, re.compile, br"\u1234") |
| 1516 |
self.assertRaises(re.PatternError, re.compile, br"\U00012345") |
| 1517 |
self.assertTrue(re.match(br"\0", b"\000")) |
| 1518 |
self.assertTrue(re.match(br"\08", b"\0008")) |
| 1519 |
self.assertTrue(re.match(br"\01", b"\001")) |
| 1520 |
self.assertTrue(re.match(br"\018", b"\0018")) |
| 1521 |
self.checkPatternError(br"\567", |
| 1522 |
r'octal escape value \567 outside of ' |
| 1523 |
r'range 0-0o377', 0) |
| 1524 |
self.checkPatternError(br"\911", 'invalid group reference 91', 1) |
| 1525 |
self.checkPatternError(br"\x1", r'incomplete escape \x1', 0) |
| 1526 |
self.checkPatternError(br"\x1z", r'incomplete escape \x1', 0) |
| 1527 |
|
| 1528 |
def test_sre_byte_class_literals(self): |
| 1529 |
for i in [0, 8, 16, 32, 64, 127, 128, 255]: |
| 1530 |
self.assertTrue(re.match((r"[\%o]" % i).encode(), bytes([i]))) |
| 1531 |
self.assertTrue(re.match((r"[\%o8]" % i).encode(), bytes([i]))) |
| 1532 |
self.assertTrue(re.match((r"[\%03o]" % i).encode(), bytes([i]))) |
| 1533 |
self.assertTrue(re.match((r"[\%03o0]" % i).encode(), bytes([i]))) |
| 1534 |
self.assertTrue(re.match((r"[\%03o8]" % i).encode(), bytes([i]))) |
| 1535 |
self.assertTrue(re.match((r"[\x%02x]" % i).encode(), bytes([i]))) |
| 1536 |
self.assertTrue(re.match((r"[\x%02x0]" % i).encode(), bytes([i]))) |
| 1537 |
self.assertTrue(re.match((r"[\x%02xz]" % i).encode(), bytes([i]))) |
| 1538 |
self.assertRaises(re.PatternError, re.compile, br"[\u1234]") |
| 1539 |
self.assertRaises(re.PatternError, re.compile, br"[\U00012345]") |
| 1540 |
self.checkPatternError(br"[\567]", |
| 1541 |
r'octal escape value \567 outside of ' |
| 1542 |
r'range 0-0o377', 1) |
| 1543 |
self.checkPatternError(br"[\911]", r'bad escape \9', 1) |
| 1544 |
self.checkPatternError(br"[\x1z]", r'incomplete escape \x1', 1) |
| 1545 |
|
| 1546 |
def test_character_set_errors(self): |
| 1547 |
self.checkPatternError(r'[', 'unterminated character set', 0) |
| 1548 |
self.checkPatternError(r'[^', 'unterminated character set', 0) |
| 1549 |
self.checkPatternError(r'[a', 'unterminated character set', 0) |
| 1550 |
# bug 545855 -- This pattern failed to cause a compile error as it |
| 1551 |
# should, instead provoking a TypeError. |
| 1552 |
self.checkPatternError(r"[a-", 'unterminated character set', 0) |
| 1553 |
self.checkPatternError(r"[\w-b]", r'bad character range \w-b', 1) |
| 1554 |
self.checkPatternError(r"[a-\w]", r'bad character range a-\w', 1) |
| 1555 |
self.checkPatternError(r"[b-a]", 'bad character range b-a', 1) |
| 1556 |
|
| 1557 |
def test_bug_113254(self): |
| 1558 |
self.assertEqual(re.match(r'(a)|(b)', 'b').start(1), -1) |
| 1559 |
self.assertEqual(re.match(r'(a)|(b)', 'b').end(1), -1) |
| 1560 |
self.assertEqual(re.match(r'(a)|(b)', 'b').span(1), (-1, -1)) |
| 1561 |
|
| 1562 |
def test_bug_527371(self): |
| 1563 |
# bug described in patches 527371/672491 |
| 1564 |
self.assertIsNone(re.match(r'(a)?a','a').lastindex) |
| 1565 |
self.assertEqual(re.match(r'(a)(b)?b','ab').lastindex, 1) |
| 1566 |
self.assertEqual(re.match(r'(?P<a>a)(?P<b>b)?b','ab').lastgroup, 'a') |
| 1567 |
self.assertEqual(re.match(r"(?P<a>a(b))", "ab").lastgroup, 'a') |
| 1568 |
self.assertEqual(re.match(r"((a))", "a").lastindex, 1) |
| 1569 |
|
| 1570 |
def test_bug_418626(self): |
| 1571 |
# bugs 418626 at al. -- Testing Greg Chapman's addition of op code |
| 1572 |
# SRE_OP_MIN_REPEAT_ONE for eliminating recursion on simple uses of |
| 1573 |
# pattern '*?' on a long string. |
| 1574 |
self.assertEqual(re.match('.*?c', 10000*'ab'+'cd').end(0), 20001) |
| 1575 |
self.assertEqual(re.match('.*?cd', 5000*'ab'+'c'+5000*'ab'+'cde').end(0), |
| 1576 |
20003) |
| 1577 |
self.assertEqual(re.match('.*?cd', 20000*'abc'+'de').end(0), 60001) |
| 1578 |
# non-simple '*?' still used to hit the recursion limit, before the |
| 1579 |
# non-recursive scheme was implemented. |
| 1580 |
self.assertEqual(re.search('(a|b)*?c', 10000*'ab'+'cd').end(0), 20001) |
| 1581 |
|
| 1582 |
def test_bug_612074(self): |
| 1583 |
pat="["+re.escape("\u2039")+"]" |
| 1584 |
self.assertEqual(re.compile(pat) and 1, 1) |
| 1585 |
|
| 1586 |
def test_stack_overflow(self): |
| 1587 |
# nasty cases that used to overflow the straightforward recursive |
| 1588 |
# implementation of repeated groups. |
| 1589 |
self.assertEqual(re.match('(x)*', 50000*'x').group(1), 'x') |
| 1590 |
self.assertEqual(re.match('(x)*y', 50000*'x'+'y').group(1), 'x') |
| 1591 |
self.assertEqual(re.match('(x)*?y', 50000*'x'+'y').group(1), 'x') |
| 1592 |
|
| 1593 |
def test_nothing_to_repeat(self): |
| 1594 |
for reps in '*', '+', '?', '{1,2}': |
| 1595 |
for mod in '', '?': |
| 1596 |
self.checkPatternError('%s%s' % (reps, mod), |
| 1597 |
'nothing to repeat', 0) |
| 1598 |
self.checkPatternError('(?:%s%s)' % (reps, mod), |
| 1599 |
'nothing to repeat', 3) |
| 1600 |
|
| 1601 |
def test_multiple_repeat(self): |
| 1602 |
for outer_reps in '*', '+', '?', '{1,2}': |
| 1603 |
for outer_mod in '', '?', '+': |
| 1604 |
outer_op = outer_reps + outer_mod |
| 1605 |
for inner_reps in '*', '+', '?', '{1,2}': |
| 1606 |
for inner_mod in '', '?', '+': |
| 1607 |
if inner_mod + outer_reps in ('?', '+'): |
| 1608 |
continue |
| 1609 |
inner_op = inner_reps + inner_mod |
| 1610 |
self.checkPatternError(r'x%s%s' % (inner_op, outer_op), |
| 1611 |
'multiple repeat', 1 + len(inner_op)) |
| 1612 |
|
| 1613 |
def test_unlimited_zero_width_repeat(self): |
| 1614 |
# Issue #9669 |
| 1615 |
self.assertIsNone(re.match(r'(?:a?)*y', 'z')) |
| 1616 |
self.assertIsNone(re.match(r'(?:a?)+y', 'z')) |
| 1617 |
self.assertIsNone(re.match(r'(?:a?){2,}y', 'z')) |
| 1618 |
self.assertIsNone(re.match(r'(?:a?)*?y', 'z')) |
| 1619 |
self.assertIsNone(re.match(r'(?:a?)+?y', 'z')) |
| 1620 |
self.assertIsNone(re.match(r'(?:a?){2,}?y', 'z')) |
| 1621 |
|
| 1622 |
def test_scanner(self): |
| 1623 |
def s_ident(scanner, token): return token |
| 1624 |
def s_operator(scanner, token): return "op%s" % token |
| 1625 |
def s_float(scanner, token): return float(token) |
| 1626 |
def s_int(scanner, token): return int(token) |
| 1627 |
|
| 1628 |
scanner = Scanner([ |
| 1629 |
(r"[a-zA-Z_]\w*", s_ident), |
| 1630 |
(r"\d+\.\d*", s_float), |
| 1631 |
(r"\d+", s_int), |
| 1632 |
(r"=|\+|-|\*|/", s_operator), |
| 1633 |
(r"\s+", None), |
| 1634 |
]) |
| 1635 |
|
| 1636 |
self.assertTrue(scanner.scanner.scanner("").pattern) |
| 1637 |
|
| 1638 |
self.assertEqual(scanner.scan("sum = 3*foo + 312.50 + bar"), |
| 1639 |
(['sum', 'op=', 3, 'op*', 'foo', 'op+', 312.5, |
| 1640 |
'op+', 'bar'], '')) |
| 1641 |
|
| 1642 |
def test_bug_448951(self): |
| 1643 |
# bug 448951 (similar to 429357, but with single char match) |
| 1644 |
# (Also test greedy matches.) |
| 1645 |
for op in '','?','*': |
| 1646 |
self.assertEqual(re.match(r'((.%s):)?z'%op, 'z').groups(), |
| 1647 |
(None, None)) |
| 1648 |
self.assertEqual(re.match(r'((.%s):)?z'%op, 'a:z').groups(), |
| 1649 |
('a:', 'a')) |
| 1650 |
|
| 1651 |
def test_bug_725106(self): |
| 1652 |
# capturing groups in alternatives in repeats |
| 1653 |
self.assertEqual(re.match('^((a)|b)*', 'abc').groups(), |
| 1654 |
('b', 'a')) |
| 1655 |
self.assertEqual(re.match('^(([ab])|c)*', 'abc').groups(), |
| 1656 |
('c', 'b')) |
| 1657 |
self.assertEqual(re.match('^((d)|[ab])*', 'abc').groups(), |
| 1658 |
('b', None)) |
| 1659 |
self.assertEqual(re.match('^((a)c|[ab])*', 'abc').groups(), |
| 1660 |
('b', None)) |
| 1661 |
self.assertEqual(re.match('^((a)|b)*?c', 'abc').groups(), |
| 1662 |
('b', 'a')) |
| 1663 |
self.assertEqual(re.match('^(([ab])|c)*?d', 'abcd').groups(), |
| 1664 |
('c', 'b')) |
| 1665 |
self.assertEqual(re.match('^((d)|[ab])*?c', 'abc').groups(), |
| 1666 |
('b', None)) |
| 1667 |
self.assertEqual(re.match('^((a)c|[ab])*?c', 'abc').groups(), |
| 1668 |
('b', None)) |
| 1669 |
|
| 1670 |
def test_bug_725149(self): |
| 1671 |
# mark_stack_base restoring before restoring marks |
| 1672 |
self.assertEqual(re.match('(a)(?:(?=(b)*)c)*', 'abb').groups(), |
| 1673 |
('a', None)) |
| 1674 |
self.assertEqual(re.match('(a)((?!(b)*))*', 'abb').groups(), |
| 1675 |
('a', None, None)) |
| 1676 |
|
| 1677 |
def test_bug_764548(self): |
| 1678 |
# bug 764548, re.compile() barfs on str/unicode subclasses |
| 1679 |
class my_unicode(str): pass |
| 1680 |
pat = re.compile(my_unicode("abc")) |
| 1681 |
self.assertIsNone(pat.match("xyz")) |
| 1682 |
|
| 1683 |
def test_finditer(self): |
| 1684 |
iter = re.finditer(r":+", "a:b::c:::d") |
| 1685 |
self.assertEqual([item.group(0) for item in iter], |
| 1686 |
[":", "::", ":::"]) |
| 1687 |
|
| 1688 |
pat = re.compile(r":+") |
| 1689 |
iter = pat.finditer("a:b::c:::d", 1, 10) |
| 1690 |
self.assertEqual([item.group(0) for item in iter], |
| 1691 |
[":", "::", ":::"]) |
| 1692 |
|
| 1693 |
pat = re.compile(r":+") |
| 1694 |
iter = pat.finditer("a:b::c:::d", pos=1, endpos=10) |
| 1695 |
self.assertEqual([item.group(0) for item in iter], |
| 1696 |
[":", "::", ":::"]) |
| 1697 |
|
| 1698 |
pat = re.compile(r":+") |
| 1699 |
iter = pat.finditer("a:b::c:::d", endpos=10, pos=1) |
| 1700 |
self.assertEqual([item.group(0) for item in iter], |
| 1701 |
[":", "::", ":::"]) |
| 1702 |
|
| 1703 |
pat = re.compile(r":+") |
| 1704 |
iter = pat.finditer("a:b::c:::d", pos=3, endpos=8) |
| 1705 |
self.assertEqual([item.group(0) for item in iter], |
| 1706 |
["::", "::"]) |
| 1707 |
|
| 1708 |
def test_bug_926075(self): |
| 1709 |
self.assertIsNot(re.compile('bug_926075'), |
| 1710 |
re.compile(b'bug_926075')) |
| 1711 |
|
| 1712 |
def test_bug_931848(self): |
| 1713 |
pattern = "[\u002E\u3002\uFF0E\uFF61]" |
| 1714 |
self.assertEqual(re.compile(pattern).split("a.b.c"), |
| 1715 |
['a','b','c']) |
| 1716 |
|
| 1717 |
def test_bug_581080(self): |
| 1718 |
iter = re.finditer(r"\s", "a b") |
| 1719 |
self.assertEqual(next(iter).span(), (1,2)) |
| 1720 |
self.assertRaises(StopIteration, next, iter) |
| 1721 |
|
| 1722 |
scanner = re.compile(r"\s").scanner("a b") |
| 1723 |
self.assertEqual(scanner.search().span(), (1, 2)) |
| 1724 |
self.assertIsNone(scanner.search()) |
| 1725 |
|
| 1726 |
def test_bug_817234(self): |
| 1727 |
iter = re.finditer(r".*", "asdf") |
| 1728 |
self.assertEqual(next(iter).span(), (0, 4)) |
| 1729 |
self.assertEqual(next(iter).span(), (4, 4)) |
| 1730 |
self.assertRaises(StopIteration, next, iter) |
| 1731 |
|
| 1732 |
def test_bug_6561(self): |
| 1733 |
# '\d' should match characters in Unicode category 'Nd' |
| 1734 |
# (Number, Decimal Digit), but not those in 'Nl' (Number, |
| 1735 |
# Letter) or 'No' (Number, Other). |
| 1736 |
decimal_digits = [ |
| 1737 |
'\u0037', # '\N{DIGIT SEVEN}', category 'Nd' |
| 1738 |
'\u0e58', # '\N{THAI DIGIT SIX}', category 'Nd' |
| 1739 |
'\uff10', # '\N{FULLWIDTH DIGIT ZERO}', category 'Nd' |
| 1740 |
] |
| 1741 |
for x in decimal_digits: |
| 1742 |
self.assertEqual(re.match(r'^\d$', x).group(0), x) |
| 1743 |
|
| 1744 |
not_decimal_digits = [ |
| 1745 |
'\u2165', # '\N{ROMAN NUMERAL SIX}', category 'Nl' |
| 1746 |
'\u3039', # '\N{HANGZHOU NUMERAL TWENTY}', category 'Nl' |
| 1747 |
'\u2082', # '\N{SUBSCRIPT TWO}', category 'No' |
| 1748 |
'\u32b4', # '\N{CIRCLED NUMBER THIRTY NINE}', category 'No' |
| 1749 |
] |
| 1750 |
for x in not_decimal_digits: |
| 1751 |
self.assertIsNone(re.match(r'^\d$', x)) |
| 1752 |
|
| 1753 |
@warnings_helper.ignore_warnings(category=DeprecationWarning) # gh-80480 array('u') |
| 1754 |
def test_empty_array(self): |
| 1755 |
# SF buf 1647541 |
| 1756 |
import array |
| 1757 |
for typecode in 'bBhuwHiIlLfd': |
| 1758 |
a = array.array(typecode) |
| 1759 |
self.assertIsNone(re.compile(b"bla").match(a)) |
| 1760 |
self.assertEqual(re.compile(b"").match(a).groups(), ()) |
| 1761 |
|
| 1762 |
def test_inline_flags(self): |
| 1763 |
# Bug #1700 |
| 1764 |
upper_char = '\u1ea0' # Latin Capital Letter A with Dot Below |
| 1765 |
lower_char = '\u1ea1' # Latin Small Letter A with Dot Below |
| 1766 |
|
| 1767 |
p = re.compile('.' + upper_char, re.I | re.S) |
| 1768 |
q = p.match('\n' + lower_char) |
| 1769 |
self.assertTrue(q) |
| 1770 |
|
| 1771 |
p = re.compile('.' + lower_char, re.I | re.S) |
| 1772 |
q = p.match('\n' + upper_char) |
| 1773 |
self.assertTrue(q) |
| 1774 |
|
| 1775 |
p = re.compile('(?i).' + upper_char, re.S) |
| 1776 |
q = p.match('\n' + lower_char) |
| 1777 |
self.assertTrue(q) |
| 1778 |
|
| 1779 |
p = re.compile('(?i).' + lower_char, re.S) |
| 1780 |
q = p.match('\n' + upper_char) |
| 1781 |
self.assertTrue(q) |
| 1782 |
|
| 1783 |
p = re.compile('(?is).' + upper_char) |
| 1784 |
q = p.match('\n' + lower_char) |
| 1785 |
self.assertTrue(q) |
| 1786 |
|
| 1787 |
p = re.compile('(?is).' + lower_char) |
| 1788 |
q = p.match('\n' + upper_char) |
| 1789 |
self.assertTrue(q) |
| 1790 |
|
| 1791 |
p = re.compile('(?s)(?i).' + upper_char) |
| 1792 |
q = p.match('\n' + lower_char) |
| 1793 |
self.assertTrue(q) |
| 1794 |
|
| 1795 |
p = re.compile('(?s)(?i).' + lower_char) |
| 1796 |
q = p.match('\n' + upper_char) |
| 1797 |
self.assertTrue(q) |
| 1798 |
|
| 1799 |
self.assertTrue(re.match('(?ix) ' + upper_char, lower_char)) |
| 1800 |
self.assertTrue(re.match('(?ix) ' + lower_char, upper_char)) |
| 1801 |
self.assertTrue(re.match(' (?i) ' + upper_char, lower_char, re.X)) |
| 1802 |
self.assertTrue(re.match('(?x) (?i) ' + upper_char, lower_char)) |
| 1803 |
self.assertTrue(re.match(' (?x) (?i) ' + upper_char, lower_char, re.X)) |
| 1804 |
|
| 1805 |
msg = "global flags not at the start of the expression" |
| 1806 |
self.checkPatternError(upper_char + '(?i)', msg, 1) |
| 1807 |
|
| 1808 |
# bpo-30605: Compiling a bytes instance regex was throwing a BytesWarning |
| 1809 |
with warnings.catch_warnings(): |
| 1810 |
warnings.simplefilter('error', BytesWarning) |
| 1811 |
self.checkPatternError(b'A(?i)', msg, 1) |
| 1812 |
|
| 1813 |
self.checkPatternError('(?s).(?i)' + upper_char, msg, 5) |
| 1814 |
self.checkPatternError('(?i) ' + upper_char + ' (?x)', msg, 7) |
| 1815 |
self.checkPatternError(' (?x) (?i) ' + upper_char, msg, 1) |
| 1816 |
self.checkPatternError('^(?i)' + upper_char, msg, 1) |
| 1817 |
self.checkPatternError('$|(?i)' + upper_char, msg, 2) |
| 1818 |
self.checkPatternError('(?:(?i)' + upper_char + ')', msg, 3) |
| 1819 |
self.checkPatternError('(^)?(?(1)(?i)' + upper_char + ')', msg, 9) |
| 1820 |
self.checkPatternError('($)?(?(1)|(?i)' + upper_char + ')', msg, 10) |
| 1821 |
|
| 1822 |
|
| 1823 |
def test_dollar_matches_twice(self): |
| 1824 |
r"""Test that $ does not include \n |
| 1825 |
$ matches the end of string, and just before the terminating \n""" |
| 1826 |
pattern = re.compile('$') |
| 1827 |
self.assertEqual(pattern.sub('#', 'a\nb\n'), 'a\nb#\n#') |
| 1828 |
self.assertEqual(pattern.sub('#', 'a\nb\nc'), 'a\nb\nc#') |
| 1829 |
self.assertEqual(pattern.sub('#', '\n'), '#\n#') |
| 1830 |
|
| 1831 |
pattern = re.compile('$', re.MULTILINE) |
| 1832 |
self.assertEqual(pattern.sub('#', 'a\nb\n' ), 'a#\nb#\n#' ) |
| 1833 |
self.assertEqual(pattern.sub('#', 'a\nb\nc'), 'a#\nb#\nc#') |
| 1834 |
self.assertEqual(pattern.sub('#', '\n'), '#\n#') |
| 1835 |
|
| 1836 |
def test_bytes_str_mixing(self): |
| 1837 |
# Mixing str and bytes is disallowed |
| 1838 |
pat = re.compile('.') |
| 1839 |
bpat = re.compile(b'.') |
| 1840 |
self.assertRaises(TypeError, pat.match, b'b') |
| 1841 |
self.assertRaises(TypeError, bpat.match, 'b') |
| 1842 |
self.assertRaises(TypeError, pat.sub, b'b', 'c') |
| 1843 |
self.assertRaises(TypeError, pat.sub, 'b', b'c') |
| 1844 |
self.assertRaises(TypeError, pat.sub, b'b', b'c') |
| 1845 |
self.assertRaises(TypeError, bpat.sub, b'b', 'c') |
| 1846 |
self.assertRaises(TypeError, bpat.sub, 'b', b'c') |
| 1847 |
self.assertRaises(TypeError, bpat.sub, 'b', 'c') |
| 1848 |
|
| 1849 |
def test_ascii_and_unicode_flag(self): |
| 1850 |
# String patterns |
| 1851 |
for flags in (0, re.UNICODE): |
| 1852 |
pat = re.compile('\xc0', flags | re.IGNORECASE) |
| 1853 |
self.assertTrue(pat.match('\xe0')) |
| 1854 |
pat = re.compile(r'\w', flags) |
| 1855 |
self.assertTrue(pat.match('\xe0')) |
| 1856 |
pat = re.compile('\xc0', re.ASCII | re.IGNORECASE) |
| 1857 |
self.assertIsNone(pat.match('\xe0')) |
| 1858 |
pat = re.compile('(?a)\xc0', re.IGNORECASE) |
| 1859 |
self.assertIsNone(pat.match('\xe0')) |
| 1860 |
pat = re.compile(r'\w', re.ASCII) |
| 1861 |
self.assertIsNone(pat.match('\xe0')) |
| 1862 |
pat = re.compile(r'(?a)\w') |
| 1863 |
self.assertIsNone(pat.match('\xe0')) |
| 1864 |
# Bytes patterns |
| 1865 |
for flags in (0, re.ASCII): |
| 1866 |
pat = re.compile(b'\xc0', flags | re.IGNORECASE) |
| 1867 |
self.assertIsNone(pat.match(b'\xe0')) |
| 1868 |
pat = re.compile(br'\w', flags) |
| 1869 |
self.assertIsNone(pat.match(b'\xe0')) |
| 1870 |
# Incompatibilities |
| 1871 |
self.assertRaises(ValueError, re.compile, br'\w', re.UNICODE) |
| 1872 |
self.assertRaises(re.PatternError, re.compile, br'(?u)\w') |
| 1873 |
self.assertRaises(ValueError, re.compile, r'\w', re.UNICODE | re.ASCII) |
| 1874 |
self.assertRaises(ValueError, re.compile, r'(?u)\w', re.ASCII) |
| 1875 |
self.assertRaises(ValueError, re.compile, r'(?a)\w', re.UNICODE) |
| 1876 |
self.assertRaises(re.PatternError, re.compile, r'(?au)\w') |
| 1877 |
|
| 1878 |
def test_locale_flag(self): |
| 1879 |
enc = locale.getpreferredencoding() |
| 1880 |
# Search non-ASCII letter |
| 1881 |
for i in range(128, 256): |
| 1882 |
try: |
| 1883 |
c = bytes([i]).decode(enc) |
| 1884 |
sletter = c.lower() |
| 1885 |
if sletter == c: continue |
| 1886 |
bletter = sletter.encode(enc) |
| 1887 |
if len(bletter) != 1: continue |
| 1888 |
if bletter.decode(enc) != sletter: continue |
| 1889 |
bpat = re.escape(bytes([i])) |
| 1890 |
break |
| 1891 |
except (UnicodeError, TypeError): |
| 1892 |
pass |
| 1893 |
else: |
| 1894 |
bletter = None |
| 1895 |
bpat = b'A' |
| 1896 |
# Bytes patterns |
| 1897 |
pat = re.compile(bpat, re.LOCALE | re.IGNORECASE) |
| 1898 |
if bletter: |
| 1899 |
self.assertTrue(pat.match(bletter)) |
| 1900 |
pat = re.compile(b'(?L)' + bpat, re.IGNORECASE) |
| 1901 |
if bletter: |
| 1902 |
self.assertTrue(pat.match(bletter)) |
| 1903 |
pat = re.compile(bpat, re.IGNORECASE) |
| 1904 |
if bletter: |
| 1905 |
self.assertIsNone(pat.match(bletter)) |
| 1906 |
pat = re.compile(br'\w', re.LOCALE) |
| 1907 |
if bletter: |
| 1908 |
self.assertTrue(pat.match(bletter)) |
| 1909 |
pat = re.compile(br'(?L)\w') |
| 1910 |
if bletter: |
| 1911 |
self.assertTrue(pat.match(bletter)) |
| 1912 |
pat = re.compile(br'\w') |
| 1913 |
if bletter: |
| 1914 |
self.assertIsNone(pat.match(bletter)) |
| 1915 |
# Incompatibilities |
| 1916 |
self.assertRaises(ValueError, re.compile, '', re.LOCALE) |
| 1917 |
self.assertRaises(re.PatternError, re.compile, '(?L)') |
| 1918 |
self.assertRaises(ValueError, re.compile, b'', re.LOCALE | re.ASCII) |
| 1919 |
self.assertRaises(ValueError, re.compile, b'(?L)', re.ASCII) |
| 1920 |
self.assertRaises(ValueError, re.compile, b'(?a)', re.LOCALE) |
| 1921 |
self.assertRaises(re.PatternError, re.compile, b'(?aL)') |
| 1922 |
|
| 1923 |
def test_scoped_flags(self): |
| 1924 |
self.assertTrue(re.match(r'(?i:a)b', 'Ab')) |
| 1925 |
self.assertIsNone(re.match(r'(?i:a)b', 'aB')) |
| 1926 |
self.assertIsNone(re.match(r'(?-i:a)b', 'Ab', re.IGNORECASE)) |
| 1927 |
self.assertTrue(re.match(r'(?-i:a)b', 'aB', re.IGNORECASE)) |
| 1928 |
self.assertIsNone(re.match(r'(?i:(?-i:a)b)', 'Ab')) |
| 1929 |
self.assertTrue(re.match(r'(?i:(?-i:a)b)', 'aB')) |
| 1930 |
|
| 1931 |
self.assertTrue(re.match(r'\w(?a:\W)\w', '\xe0\xe0\xe0')) |
| 1932 |
self.assertTrue(re.match(r'(?a:\W(?u:\w)\W)', '\xe0\xe0\xe0')) |
| 1933 |
self.assertTrue(re.match(r'\W(?u:\w)\W', '\xe0\xe0\xe0', re.ASCII)) |
| 1934 |
|
| 1935 |
self.checkPatternError(r'(?a)(?-a:\w)', |
| 1936 |
"bad inline flags: cannot turn off flags 'a', 'u' and 'L'", 8) |
| 1937 |
self.checkPatternError(r'(?i-i:a)', |
| 1938 |
'bad inline flags: flag turned on and off', 5) |
| 1939 |
self.checkPatternError(r'(?au:a)', |
| 1940 |
"bad inline flags: flags 'a', 'u' and 'L' are incompatible", 4) |
| 1941 |
self.checkPatternError(br'(?aL:a)', |
| 1942 |
"bad inline flags: flags 'a', 'u' and 'L' are incompatible", 4) |
| 1943 |
|
| 1944 |
self.checkPatternError(r'(?-', 'missing flag', 3) |
| 1945 |
self.checkPatternError(r'(?-+', 'missing flag', 3) |
| 1946 |
self.checkPatternError(r'(?-z', 'unknown flag', 3) |
| 1947 |
self.checkPatternError(r'(?-i', 'missing :', 4) |
| 1948 |
self.checkPatternError(r'(?-i)', 'missing :', 4) |
| 1949 |
self.checkPatternError(r'(?-i+', 'missing :', 4) |
| 1950 |
self.checkPatternError(r'(?-iz', 'unknown flag', 4) |
| 1951 |
self.checkPatternError(r'(?i:', 'missing ), unterminated subpattern', 0) |
| 1952 |
self.checkPatternError(r'(?i', 'missing -, : or )', 3) |
| 1953 |
self.checkPatternError(r'(?i+', 'missing -, : or )', 3) |
| 1954 |
self.checkPatternError(r'(?iz', 'unknown flag', 3) |
| 1955 |
|
| 1956 |
def test_ignore_spaces(self): |
| 1957 |
for space in " \t\n\r\v\f": |
| 1958 |
self.assertTrue(re.fullmatch(space + 'a', 'a', re.VERBOSE)) |
| 1959 |
for space in b" ", b"\t", b"\n", b"\r", b"\v", b"\f": |
| 1960 |
self.assertTrue(re.fullmatch(space + b'a', b'a', re.VERBOSE)) |
| 1961 |
self.assertTrue(re.fullmatch('(?x) a', 'a')) |
| 1962 |
self.assertTrue(re.fullmatch(' (?x) a', 'a', re.VERBOSE)) |
| 1963 |
self.assertTrue(re.fullmatch('(?x) (?x) a', 'a')) |
| 1964 |
self.assertTrue(re.fullmatch(' a(?x: b) c', ' ab c')) |
| 1965 |
self.assertTrue(re.fullmatch(' a(?-x: b) c', 'a bc', re.VERBOSE)) |
| 1966 |
self.assertTrue(re.fullmatch('(?x) a(?-x: b) c', 'a bc')) |
| 1967 |
self.assertTrue(re.fullmatch('(?x) a| b', 'a')) |
| 1968 |
self.assertTrue(re.fullmatch('(?x) a| b', 'b')) |
| 1969 |
|
| 1970 |
def test_comments(self): |
| 1971 |
self.assertTrue(re.fullmatch('#x\na', 'a', re.VERBOSE)) |
| 1972 |
self.assertTrue(re.fullmatch(b'#x\na', b'a', re.VERBOSE)) |
| 1973 |
self.assertTrue(re.fullmatch('(?x)#x\na', 'a')) |
| 1974 |
self.assertTrue(re.fullmatch('#x\n(?x)#y\na', 'a', re.VERBOSE)) |
| 1975 |
self.assertTrue(re.fullmatch('(?x)#x\n(?x)#y\na', 'a')) |
| 1976 |
self.assertTrue(re.fullmatch('#x\na(?x:#y\nb)#z\nc', '#x\nab#z\nc')) |
| 1977 |
self.assertTrue(re.fullmatch('#x\na(?-x:#y\nb)#z\nc', 'a#y\nbc', |
| 1978 |
re.VERBOSE)) |
| 1979 |
self.assertTrue(re.fullmatch('(?x)#x\na(?-x:#y\nb)#z\nc', 'a#y\nbc')) |
| 1980 |
self.assertTrue(re.fullmatch('(?x)#x\na|#y\nb', 'a')) |
| 1981 |
self.assertTrue(re.fullmatch('(?x)#x\na|#y\nb', 'b')) |
| 1982 |
|
| 1983 |
def test_bug_6509(self): |
| 1984 |
# Replacement strings of both types must parse properly. |
| 1985 |
# all strings |
| 1986 |
pat = re.compile(r'a(\w)') |
| 1987 |
self.assertEqual(pat.sub('b\\1', 'ac'), 'bc') |
| 1988 |
pat = re.compile('a(.)') |
| 1989 |
self.assertEqual(pat.sub('b\\1', 'a\u1234'), 'b\u1234') |
| 1990 |
pat = re.compile('..') |
| 1991 |
self.assertEqual(pat.sub(lambda m: 'str', 'a5'), 'str') |
| 1992 |
|
| 1993 |
# all bytes |
| 1994 |
pat = re.compile(br'a(\w)') |
| 1995 |
self.assertEqual(pat.sub(b'b\\1', b'ac'), b'bc') |
| 1996 |
pat = re.compile(b'a(.)') |
| 1997 |
self.assertEqual(pat.sub(b'b\\1', b'a\xCD'), b'b\xCD') |
| 1998 |
pat = re.compile(b'..') |
| 1999 |
self.assertEqual(pat.sub(lambda m: b'bytes', b'a5'), b'bytes') |
| 2000 |
|
| 2001 |
def test_search_dot_unicode(self): |
| 2002 |
self.assertTrue(re.search("123.*-", '123abc-')) |
| 2003 |
self.assertTrue(re.search("123.*-", '123\xe9-')) |
| 2004 |
self.assertTrue(re.search("123.*-", '123\u20ac-')) |
| 2005 |
self.assertTrue(re.search("123.*-", '123\U0010ffff-')) |
| 2006 |
self.assertTrue(re.search("123.*-", '123\xe9\u20ac\U0010ffff-')) |
| 2007 |
|
| 2008 |
def test_compile(self): |
| 2009 |
# Test return value when given string and pattern as parameter |
| 2010 |
pattern = re.compile('random pattern') |
| 2011 |
self.assertIsInstance(pattern, re.Pattern) |
| 2012 |
same_pattern = re.compile(pattern) |
| 2013 |
self.assertIsInstance(same_pattern, re.Pattern) |
| 2014 |
self.assertIs(same_pattern, pattern) |
| 2015 |
# Test behaviour when not given a string or pattern as parameter |
| 2016 |
self.assertRaises(TypeError, re.compile, 0) |
| 2017 |
|
| 2018 |
@bigmemtest(size=_2G, memuse=1) |
| 2019 |
def test_large_search(self, size): |
| 2020 |
# Issue #10182: indices were 32-bit-truncated. |
| 2021 |
s = 'a' * size |
| 2022 |
m = re.search('$', s) |
| 2023 |
self.assertIsNotNone(m) |
| 2024 |
self.assertEqual(m.start(), size) |
| 2025 |
self.assertEqual(m.end(), size) |
| 2026 |
|
| 2027 |
# The huge memuse is because of re.sub() using a list and a join() |
| 2028 |
# to create the replacement result. |
| 2029 |
@bigmemtest(size=_2G, memuse=16 + 2) |
| 2030 |
def test_large_subn(self, size): |
| 2031 |
# Issue #10182: indices were 32-bit-truncated. |
| 2032 |
s = 'a' * size |
| 2033 |
r, n = re.subn('', '', s) |
| 2034 |
self.assertEqual(r, s) |
| 2035 |
self.assertEqual(n, size + 1) |
| 2036 |
|
| 2037 |
def test_bug_16688(self): |
| 2038 |
# Issue 16688: Backreferences make case-insensitive regex fail on |
| 2039 |
# non-ASCII strings. |
| 2040 |
self.assertEqual(re.findall(r"(?i)(a)\1", "aa \u0100"), ['a']) |
| 2041 |
self.assertEqual(re.match(r"(?s).{1,3}", "\u0100\u0100").span(), (0, 2)) |
| 2042 |
|
| 2043 |
def test_repeat_minmax_overflow(self): |
| 2044 |
# Issue #13169 |
| 2045 |
string = "x" * 100000 |
| 2046 |
self.assertEqual(re.match(r".{65535}", string).span(), (0, 65535)) |
| 2047 |
self.assertEqual(re.match(r".{,65535}", string).span(), (0, 65535)) |
| 2048 |
self.assertEqual(re.match(r".{65535,}?", string).span(), (0, 65535)) |
| 2049 |
self.assertEqual(re.match(r".{65536}", string).span(), (0, 65536)) |
| 2050 |
self.assertEqual(re.match(r".{,65536}", string).span(), (0, 65536)) |
| 2051 |
self.assertEqual(re.match(r".{65536,}?", string).span(), (0, 65536)) |
| 2052 |
# 2**128 should be big enough to overflow both SRE_CODE and Py_ssize_t. |
| 2053 |
self.assertRaises(OverflowError, re.compile, r".{%d}" % 2**128) |
| 2054 |
self.assertRaises(OverflowError, re.compile, r".{,%d}" % 2**128) |
| 2055 |
self.assertRaises(OverflowError, re.compile, r".{%d,}?" % 2**128) |
| 2056 |
self.assertRaises(OverflowError, re.compile, r".{%d,%d}" % (2**129, 2**128)) |
| 2057 |
|
| 2058 |
def test_look_behind_overflow(self): |
| 2059 |
string = "x" * 2_500_000 |
| 2060 |
p1 = r"(?<=((.{%d}){%d}){%d})" |
| 2061 |
p2 = r"(?<!((.{%d}){%d}){%d})" |
| 2062 |
# Test that the templates are valid and look-behind with width 2**21 |
| 2063 |
# (larger than sys.maxunicode) are supported. |
| 2064 |
self.assertEqual(re.search(p1 % (2**7, 2**7, 2**7), string).span(), |
| 2065 |
(2**21, 2**21)) |
| 2066 |
self.assertEqual(re.search(p2 % (2**7, 2**7, 2**7), string).span(), |
| 2067 |
(0, 0)) |
| 2068 |
# Test that 2**22 is accepted as a repetition number and look-behind |
| 2069 |
# width. |
| 2070 |
re.compile(p1 % (2**22, 1, 1)) |
| 2071 |
re.compile(p1 % (1, 2**22, 1)) |
| 2072 |
re.compile(p1 % (1, 1, 2**22)) |
| 2073 |
re.compile(p2 % (2**22, 1, 1)) |
| 2074 |
re.compile(p2 % (1, 2**22, 1)) |
| 2075 |
re.compile(p2 % (1, 1, 2**22)) |
| 2076 |
# But 2**66 is too large for look-behind width. |
| 2077 |
errmsg = "looks too much behind" |
| 2078 |
self.assertRaisesRegex(re.error, errmsg, re.compile, p1 % (2**22, 2**22, 2**22)) |
| 2079 |
self.assertRaisesRegex(re.error, errmsg, re.compile, p2 % (2**22, 2**22, 2**22)) |
| 2080 |
|
| 2081 |
def test_backref_group_name_in_exception(self): |
| 2082 |
# Issue 17341: Poor error message when compiling invalid regex |
| 2083 |
self.checkPatternError('(?P=<foo>)', |
| 2084 |
"bad character in group name '<foo>'", 4) |
| 2085 |
|
| 2086 |
def test_group_name_in_exception(self): |
| 2087 |
# Issue 17341: Poor error message when compiling invalid regex |
| 2088 |
self.checkPatternError('(?P<?foo>)', |
| 2089 |
"bad character in group name '?foo'", 4) |
| 2090 |
|
| 2091 |
def test_issue17998(self): |
| 2092 |
for reps in '*', '+', '?', '{1}': |
| 2093 |
for mod in '', '?': |
| 2094 |
pattern = '.' + reps + mod + 'yz' |
| 2095 |
self.assertEqual(re.compile(pattern, re.S).findall('xyz'), |
| 2096 |
['xyz'], msg=pattern) |
| 2097 |
pattern = pattern.encode() |
| 2098 |
self.assertEqual(re.compile(pattern, re.S).findall(b'xyz'), |
| 2099 |
[b'xyz'], msg=pattern) |
| 2100 |
|
| 2101 |
def test_match_repr(self): |
| 2102 |
for string in '[abracadabra]', S('[abracadabra]'): |
| 2103 |
m = re.search(r'(.+)(.*?)\1', string) |
| 2104 |
pattern = r"<(%s\.)?%s object; span=\(1, 12\), match='abracadabra'>" % ( |
| 2105 |
type(m).__module__, type(m).__qualname__ |
| 2106 |
) |
| 2107 |
self.assertRegex(repr(m), pattern) |
| 2108 |
for string in (b'[abracadabra]', B(b'[abracadabra]'), |
| 2109 |
bytearray(b'[abracadabra]'), |
| 2110 |
memoryview(b'[abracadabra]')): |
| 2111 |
m = re.search(br'(.+)(.*?)\1', string) |
| 2112 |
pattern = r"<(%s\.)?%s object; span=\(1, 12\), match=b'abracadabra'>" % ( |
| 2113 |
type(m).__module__, type(m).__qualname__ |
| 2114 |
) |
| 2115 |
self.assertRegex(repr(m), pattern) |
| 2116 |
|
| 2117 |
first, second = list(re.finditer("(aa)|(bb)", "aa bb")) |
| 2118 |
pattern = r"<(%s\.)?%s object; span=\(0, 2\), match='aa'>" % ( |
| 2119 |
type(second).__module__, type(second).__qualname__ |
| 2120 |
) |
| 2121 |
self.assertRegex(repr(first), pattern) |
| 2122 |
pattern = r"<(%s\.)?%s object; span=\(3, 5\), match='bb'>" % ( |
| 2123 |
type(second).__module__, type(second).__qualname__ |
| 2124 |
) |
| 2125 |
self.assertRegex(repr(second), pattern) |
| 2126 |
|
| 2127 |
def test_zerowidth(self): |
| 2128 |
# Issues 852532, 1647489, 3262, 25054. |
| 2129 |
self.assertEqual(re.split(r"\b", "a::bc"), ['', 'a', '::', 'bc', '']) |
| 2130 |
self.assertEqual(re.split(r"\b|:+", "a::bc"), ['', 'a', '', '', 'bc', '']) |
| 2131 |
self.assertEqual(re.split(r"(?<!\w)(?=\w)|:+", "a::bc"), ['', 'a', '', 'bc']) |
| 2132 |
self.assertEqual(re.split(r"(?<=\w)(?!\w)|:+", "a::bc"), ['a', '', 'bc', '']) |
| 2133 |
|
| 2134 |
self.assertEqual(re.sub(r"\b", "-", "a::bc"), '-a-::-bc-') |
| 2135 |
self.assertEqual(re.sub(r"\b|:+", "-", "a::bc"), '-a---bc-') |
| 2136 |
self.assertEqual(re.sub(r"(\b|:+)", r"[\1]", "a::bc"), '[]a[][::][]bc[]') |
| 2137 |
|
| 2138 |
self.assertEqual(re.findall(r"\b|:+", "a::bc"), ['', '', '::', '', '']) |
| 2139 |
self.assertEqual(re.findall(r"\b|\w+", "a::bc"), |
| 2140 |
['', 'a', '', '', 'bc', '']) |
| 2141 |
|
| 2142 |
self.assertEqual([m.span() for m in re.finditer(r"\b|:+", "a::bc")], |
| 2143 |
[(0, 0), (1, 1), (1, 3), (3, 3), (5, 5)]) |
| 2144 |
self.assertEqual([m.span() for m in re.finditer(r"\b|\w+", "a::bc")], |
| 2145 |
[(0, 0), (0, 1), (1, 1), (3, 3), (3, 5), (5, 5)]) |
| 2146 |
|
| 2147 |
def test_bug_2537(self): |
| 2148 |
# issue 2537: empty submatches |
| 2149 |
for outer_op in ('{0,}', '*', '+', '{1,187}'): |
| 2150 |
for inner_op in ('{0,}', '*', '?'): |
| 2151 |
r = re.compile("^((x|y)%s)%s" % (inner_op, outer_op)) |
| 2152 |
m = r.match("xyyzy") |
| 2153 |
self.assertEqual(m.group(0), "xyy") |
| 2154 |
self.assertEqual(m.group(1), "") |
| 2155 |
self.assertEqual(m.group(2), "y") |
| 2156 |
|
| 2157 |
def test_keyword_parameters(self): |
| 2158 |
# Issue #20283: Accepting the string keyword parameter. |
| 2159 |
pat = re.compile(r'(ab)') |
| 2160 |
self.assertEqual( |
| 2161 |
pat.match(string='abracadabra', pos=7, endpos=10).span(), (7, 9)) |
| 2162 |
self.assertEqual( |
| 2163 |
pat.fullmatch(string='abracadabra', pos=7, endpos=9).span(), (7, 9)) |
| 2164 |
self.assertEqual( |
| 2165 |
pat.search(string='abracadabra', pos=3, endpos=10).span(), (7, 9)) |
| 2166 |
self.assertEqual( |
| 2167 |
pat.findall(string='abracadabra', pos=3, endpos=10), ['ab']) |
| 2168 |
self.assertEqual( |
| 2169 |
pat.split(string='abracadabra', maxsplit=1), |
| 2170 |
['', 'ab', 'racadabra']) |
| 2171 |
self.assertEqual( |
| 2172 |
pat.scanner(string='abracadabra', pos=3, endpos=10).search().span(), |
| 2173 |
(7, 9)) |
| 2174 |
|
| 2175 |
def test_bug_20998(self): |
| 2176 |
# Issue #20998: Fullmatch of repeated single character pattern |
| 2177 |
# with ignore case. |
| 2178 |
self.assertEqual(re.fullmatch('[a-c]+', 'ABC', re.I).span(), (0, 3)) |
| 2179 |
|
| 2180 |
@unittest.skipIf(linked_to_musl(), "musl libc issue, bpo-46390") |
| 2181 |
@unittest.skipIf(sys.platform.startswith("sunos"), |
| 2182 |
"test doesn't work on Solaris, gh-91214") |
| 2183 |
def test_locale_caching(self): |
| 2184 |
# Issue #22410 |
| 2185 |
oldlocale = locale.setlocale(locale.LC_CTYPE) |
| 2186 |
self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale) |
| 2187 |
for loc in 'en_US.iso88591', 'en_US.utf8': |
| 2188 |
try: |
| 2189 |
locale.setlocale(locale.LC_CTYPE, loc) |
| 2190 |
except locale.Error: |
| 2191 |
# Unsupported locale on this system |
| 2192 |
self.skipTest('test needs %s locale' % loc) |
| 2193 |
|
| 2194 |
re.purge() |
| 2195 |
self.check_en_US_iso88591() |
| 2196 |
self.check_en_US_utf8() |
| 2197 |
re.purge() |
| 2198 |
self.check_en_US_utf8() |
| 2199 |
self.check_en_US_iso88591() |
| 2200 |
|
| 2201 |
def check_en_US_iso88591(self): |
| 2202 |
locale.setlocale(locale.LC_CTYPE, 'en_US.iso88591') |
| 2203 |
self.assertTrue(re.match(b'\xc5\xe5', b'\xc5\xe5', re.L|re.I)) |
| 2204 |
self.assertTrue(re.match(b'\xc5', b'\xe5', re.L|re.I)) |
| 2205 |
self.assertTrue(re.match(b'\xe5', b'\xc5', re.L|re.I)) |
| 2206 |
self.assertTrue(re.match(b'(?Li)\xc5\xe5', b'\xc5\xe5')) |
| 2207 |
self.assertTrue(re.match(b'(?Li)\xc5', b'\xe5')) |
| 2208 |
self.assertTrue(re.match(b'(?Li)\xe5', b'\xc5')) |
| 2209 |
|
| 2210 |
def check_en_US_utf8(self): |
| 2211 |
locale.setlocale(locale.LC_CTYPE, 'en_US.utf8') |
| 2212 |
self.assertTrue(re.match(b'\xc5\xe5', b'\xc5\xe5', re.L|re.I)) |
| 2213 |
self.assertIsNone(re.match(b'\xc5', b'\xe5', re.L|re.I)) |
| 2214 |
self.assertIsNone(re.match(b'\xe5', b'\xc5', re.L|re.I)) |
| 2215 |
self.assertTrue(re.match(b'(?Li)\xc5\xe5', b'\xc5\xe5')) |
| 2216 |
self.assertIsNone(re.match(b'(?Li)\xc5', b'\xe5')) |
| 2217 |
self.assertIsNone(re.match(b'(?Li)\xe5', b'\xc5')) |
| 2218 |
|
| 2219 |
@unittest.skipIf(linked_to_musl(), "musl libc issue, bpo-46390") |
| 2220 |
@unittest.skipIf(sys.platform.startswith("sunos"), |
| 2221 |
"test doesn't work on Solaris, gh-91214") |
| 2222 |
def test_locale_compiled(self): |
| 2223 |
oldlocale = locale.setlocale(locale.LC_CTYPE) |
| 2224 |
self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale) |
| 2225 |
for loc in 'en_US.iso88591', 'en_US.utf8': |
| 2226 |
try: |
| 2227 |
locale.setlocale(locale.LC_CTYPE, loc) |
| 2228 |
except locale.Error: |
| 2229 |
# Unsupported locale on this system |
| 2230 |
self.skipTest('test needs %s locale' % loc) |
| 2231 |
|
| 2232 |
locale.setlocale(locale.LC_CTYPE, 'en_US.iso88591') |
| 2233 |
p1 = re.compile(b'\xc5\xe5', re.L|re.I) |
| 2234 |
p2 = re.compile(b'[a\xc5][a\xe5]', re.L|re.I) |
| 2235 |
p3 = re.compile(b'[az\xc5][az\xe5]', re.L|re.I) |
| 2236 |
p4 = re.compile(b'[^\xc5][^\xe5]', re.L|re.I) |
| 2237 |
for p in p1, p2, p3: |
| 2238 |
self.assertTrue(p.match(b'\xc5\xe5')) |
| 2239 |
self.assertTrue(p.match(b'\xe5\xe5')) |
| 2240 |
self.assertTrue(p.match(b'\xc5\xc5')) |
| 2241 |
self.assertIsNone(p4.match(b'\xe5\xc5')) |
| 2242 |
self.assertIsNone(p4.match(b'\xe5\xe5')) |
| 2243 |
self.assertIsNone(p4.match(b'\xc5\xc5')) |
| 2244 |
|
| 2245 |
locale.setlocale(locale.LC_CTYPE, 'en_US.utf8') |
| 2246 |
for p in p1, p2, p3: |
| 2247 |
self.assertTrue(p.match(b'\xc5\xe5')) |
| 2248 |
self.assertIsNone(p.match(b'\xe5\xe5')) |
| 2249 |
self.assertIsNone(p.match(b'\xc5\xc5')) |
| 2250 |
self.assertTrue(p4.match(b'\xe5\xc5')) |
| 2251 |
self.assertIsNone(p4.match(b'\xe5\xe5')) |
| 2252 |
self.assertIsNone(p4.match(b'\xc5\xc5')) |
| 2253 |
|
| 2254 |
def test_error(self): |
| 2255 |
with self.assertRaises(re.PatternError) as cm: |
| 2256 |
re.compile('(\u20ac))') |
| 2257 |
err = cm.exception |
| 2258 |
self.assertIsInstance(err.pattern, str) |
| 2259 |
self.assertEqual(err.pattern, '(\u20ac))') |
| 2260 |
self.assertEqual(err.pos, 3) |
| 2261 |
self.assertEqual(err.lineno, 1) |
| 2262 |
self.assertEqual(err.colno, 4) |
| 2263 |
self.assertIn(err.msg, str(err)) |
| 2264 |
self.assertIn(' at position 3', str(err)) |
| 2265 |
self.assertNotIn(' at position 3', err.msg) |
| 2266 |
# Bytes pattern |
| 2267 |
with self.assertRaises(re.PatternError) as cm: |
| 2268 |
re.compile(b'(\xa4))') |
| 2269 |
err = cm.exception |
| 2270 |
self.assertIsInstance(err.pattern, bytes) |
| 2271 |
self.assertEqual(err.pattern, b'(\xa4))') |
| 2272 |
self.assertEqual(err.pos, 3) |
| 2273 |
# Multiline pattern |
| 2274 |
with self.assertRaises(re.PatternError) as cm: |
| 2275 |
re.compile(""" |
| 2276 |
( |
| 2277 |
abc |
| 2278 |
) |
| 2279 |
) |
| 2280 |
( |
| 2281 |
""", re.VERBOSE) |
| 2282 |
err = cm.exception |
| 2283 |
self.assertEqual(err.pos, 77) |
| 2284 |
self.assertEqual(err.lineno, 5) |
| 2285 |
self.assertEqual(err.colno, 17) |
| 2286 |
self.assertIn(err.msg, str(err)) |
| 2287 |
self.assertIn(' at position 77', str(err)) |
| 2288 |
self.assertIn('(line 5, column 17)', str(err)) |
| 2289 |
|
| 2290 |
def test_misc_errors(self): |
| 2291 |
self.checkPatternError(r'(', 'missing ), unterminated subpattern', 0) |
| 2292 |
self.checkPatternError(r'((a|b)', 'missing ), unterminated subpattern', 0) |
| 2293 |
self.checkPatternError(r'(a|b))', 'unbalanced parenthesis', 5) |
| 2294 |
self.checkPatternError(r'(?P', 'unexpected end of pattern', 3) |
| 2295 |
self.checkPatternError(r'(?z)', 'unknown extension ?z', 1) |
| 2296 |
self.checkPatternError(r'(?iz)', 'unknown flag', 3) |
| 2297 |
self.checkPatternError(r'(?i', 'missing -, : or )', 3) |
| 2298 |
self.checkPatternError(r'(?#abc', 'missing ), unterminated comment', 0) |
| 2299 |
self.checkPatternError(r'(?<', 'unexpected end of pattern', 3) |
| 2300 |
self.checkPatternError(r'(?<>)', 'unknown extension ?<>', 1) |
| 2301 |
self.checkPatternError(r'(?', 'unexpected end of pattern', 2) |
| 2302 |
|
| 2303 |
def test_enum(self): |
| 2304 |
# Issue #28082: Check that str(flag) returns a human readable string |
| 2305 |
# instead of an integer |
| 2306 |
self.assertIn('ASCII', str(re.A)) |
| 2307 |
self.assertIn('DOTALL', str(re.S)) |
| 2308 |
|
| 2309 |
def test_pattern_compare(self): |
| 2310 |
pattern1 = re.compile('abc', re.IGNORECASE) |
| 2311 |
|
| 2312 |
# equal to itself |
| 2313 |
self.assertEqual(pattern1, pattern1) |
| 2314 |
self.assertFalse(pattern1 != pattern1) |
| 2315 |
|
| 2316 |
# equal |
| 2317 |
re.purge() |
| 2318 |
pattern2 = re.compile('abc', re.IGNORECASE) |
| 2319 |
self.assertEqual(hash(pattern2), hash(pattern1)) |
| 2320 |
self.assertEqual(pattern2, pattern1) |
| 2321 |
|
| 2322 |
# not equal: different pattern |
| 2323 |
re.purge() |
| 2324 |
pattern3 = re.compile('XYZ', re.IGNORECASE) |
| 2325 |
# Don't test hash(pattern3) != hash(pattern1) because there is no |
| 2326 |
# warranty that hash values are different |
| 2327 |
self.assertNotEqual(pattern3, pattern1) |
| 2328 |
|
| 2329 |
# not equal: different flag (flags=0) |
| 2330 |
re.purge() |
| 2331 |
pattern4 = re.compile('abc') |
| 2332 |
self.assertNotEqual(pattern4, pattern1) |
| 2333 |
|
| 2334 |
# only == and != comparison operators are supported |
| 2335 |
with self.assertRaises(TypeError): |
| 2336 |
pattern1 < pattern2 |
| 2337 |
|
| 2338 |
def test_pattern_compare_bytes(self): |
| 2339 |
pattern1 = re.compile(b'abc') |
| 2340 |
|
| 2341 |
# equal: test bytes patterns |
| 2342 |
re.purge() |
| 2343 |
pattern2 = re.compile(b'abc') |
| 2344 |
self.assertEqual(hash(pattern2), hash(pattern1)) |
| 2345 |
self.assertEqual(pattern2, pattern1) |
| 2346 |
|
| 2347 |
# not equal: pattern of a different types (str vs bytes), |
| 2348 |
# comparison must not raise a BytesWarning |
| 2349 |
re.purge() |
| 2350 |
pattern3 = re.compile('abc') |
| 2351 |
with warnings.catch_warnings(): |
| 2352 |
warnings.simplefilter('error', BytesWarning) |
| 2353 |
self.assertNotEqual(pattern3, pattern1) |
| 2354 |
|
| 2355 |
def test_bug_29444(self): |
| 2356 |
s = bytearray(b'abcdefgh') |
| 2357 |
m = re.search(b'[a-h]+', s) |
| 2358 |
m2 = re.search(b'[e-h]+', s) |
| 2359 |
self.assertEqual(m.group(), b'abcdefgh') |
| 2360 |
self.assertEqual(m2.group(), b'efgh') |
| 2361 |
s[:] = b'xyz' |
| 2362 |
self.assertEqual(m.group(), b'xyz') |
| 2363 |
self.assertEqual(m2.group(), b'') |
| 2364 |
|
| 2365 |
def test_bug_34294(self): |
| 2366 |
# Issue 34294: wrong capturing groups |
| 2367 |
|
| 2368 |
# exists since Python 2 |
| 2369 |
s = "a\tx" |
| 2370 |
p = r"\b(?=(\t)|(x))x" |
| 2371 |
self.assertEqual(re.search(p, s).groups(), (None, 'x')) |
| 2372 |
|
| 2373 |
# introduced in Python 3.7.0 |
| 2374 |
s = "ab" |
| 2375 |
p = r"(?=(.)(.)?)" |
| 2376 |
self.assertEqual(re.findall(p, s), |
| 2377 |
[('a', 'b'), ('b', '')]) |
| 2378 |
self.assertEqual([m.groups() for m in re.finditer(p, s)], |
| 2379 |
[('a', 'b'), ('b', None)]) |
| 2380 |
|
| 2381 |
# test-cases provided by issue34294, introduced in Python 3.7.0 |
| 2382 |
p = r"(?=<(?P<tag>\w+)/?>(?:(?P<text>.+?)</(?P=tag)>)?)" |
| 2383 |
s = "<test><foo2/></test>" |
| 2384 |
self.assertEqual(re.findall(p, s), |
| 2385 |
[('test', '<foo2/>'), ('foo2', '')]) |
| 2386 |
self.assertEqual([m.groupdict() for m in re.finditer(p, s)], |
| 2387 |
[{'tag': 'test', 'text': '<foo2/>'}, |
| 2388 |
{'tag': 'foo2', 'text': None}]) |
| 2389 |
s = "<test>Hello</test><foo/>" |
| 2390 |
self.assertEqual([m.groupdict() for m in re.finditer(p, s)], |
| 2391 |
[{'tag': 'test', 'text': 'Hello'}, |
| 2392 |
{'tag': 'foo', 'text': None}]) |
| 2393 |
s = "<test>Hello</test><foo/><foo/>" |
| 2394 |
self.assertEqual([m.groupdict() for m in re.finditer(p, s)], |
| 2395 |
[{'tag': 'test', 'text': 'Hello'}, |
| 2396 |
{'tag': 'foo', 'text': None}, |
| 2397 |
{'tag': 'foo', 'text': None}]) |
| 2398 |
|
| 2399 |
def test_MARK_PUSH_macro_bug(self): |
| 2400 |
# issue35859, MARK_PUSH() macro didn't protect MARK-0 if it |
| 2401 |
# was the only available mark. |
| 2402 |
self.assertEqual(re.match(r'(ab|a)*?b', 'ab').groups(), ('a',)) |
| 2403 |
self.assertEqual(re.match(r'(ab|a)+?b', 'ab').groups(), ('a',)) |
| 2404 |
self.assertEqual(re.match(r'(ab|a){0,2}?b', 'ab').groups(), ('a',)) |
| 2405 |
self.assertEqual(re.match(r'(.b|a)*?b', 'ab').groups(), ('a',)) |
| 2406 |
|
| 2407 |
def test_MIN_UNTIL_mark_bug(self): |
| 2408 |
# Fixed in issue35859, reported in issue9134. |
| 2409 |
# JUMP_MIN_UNTIL_2 should MARK_PUSH() if in a repeat |
| 2410 |
s = 'axxzbcz' |
| 2411 |
p = r'(?:(?:a|bc)*?(xx)??z)*' |
| 2412 |
self.assertEqual(re.match(p, s).groups(), ('xx',)) |
| 2413 |
|
| 2414 |
# test-case provided by issue9134 |
| 2415 |
s = 'xtcxyzxc' |
| 2416 |
p = r'((x|yz)+?(t)??c)*' |
| 2417 |
m = re.match(p, s) |
| 2418 |
self.assertEqual(m.span(), (0, 8)) |
| 2419 |
self.assertEqual(m.span(2), (6, 7)) |
| 2420 |
self.assertEqual(m.groups(), ('xyzxc', 'x', 't')) |
| 2421 |
|
| 2422 |
def test_REPEAT_ONE_mark_bug(self): |
| 2423 |
# issue35859 |
| 2424 |
# JUMP_REPEAT_ONE_1 should MARK_PUSH() if in a repeat |
| 2425 |
s = 'aabaab' |
| 2426 |
p = r'(?:[^b]*a(?=(b)|(a))ab)*' |
| 2427 |
m = re.match(p, s) |
| 2428 |
self.assertEqual(m.span(), (0, 6)) |
| 2429 |
self.assertEqual(m.span(2), (4, 5)) |
| 2430 |
self.assertEqual(m.groups(), (None, 'a')) |
| 2431 |
|
| 2432 |
# JUMP_REPEAT_ONE_2 should MARK_PUSH() if in a repeat |
| 2433 |
s = 'abab' |
| 2434 |
p = r'(?:[^b]*(?=(b)|(a))ab)*' |
| 2435 |
m = re.match(p, s) |
| 2436 |
self.assertEqual(m.span(), (0, 4)) |
| 2437 |
self.assertEqual(m.span(2), (2, 3)) |
| 2438 |
self.assertEqual(m.groups(), (None, 'a')) |
| 2439 |
|
| 2440 |
self.assertEqual(re.match(r'(ab?)*?b', 'ab').groups(), ('a',)) |
| 2441 |
|
| 2442 |
def test_MIN_REPEAT_ONE_mark_bug(self): |
| 2443 |
# issue35859 |
| 2444 |
# JUMP_MIN_REPEAT_ONE should MARK_PUSH() if in a repeat |
| 2445 |
s = 'abab' |
| 2446 |
p = r'(?:.*?(?=(a)|(b))b)*' |
| 2447 |
m = re.match(p, s) |
| 2448 |
self.assertEqual(m.span(), (0, 4)) |
| 2449 |
self.assertEqual(m.span(2), (3, 4)) |
| 2450 |
self.assertEqual(m.groups(), (None, 'b')) |
| 2451 |
|
| 2452 |
s = 'axxzaz' |
| 2453 |
p = r'(?:a*?(xx)??z)*' |
| 2454 |
self.assertEqual(re.match(p, s).groups(), ('xx',)) |
| 2455 |
|
| 2456 |
def test_ASSERT_NOT_mark_bug(self): |
| 2457 |
# Fixed in issue35859, reported in issue725149. |
| 2458 |
# JUMP_ASSERT_NOT should LASTMARK_SAVE() |
| 2459 |
self.assertEqual(re.match(r'(?!(..)c)', 'ab').groups(), (None,)) |
| 2460 |
|
| 2461 |
# JUMP_ASSERT_NOT should MARK_PUSH() if in a repeat |
| 2462 |
m = re.match(r'((?!(ab)c)(.))*', 'abab') |
| 2463 |
self.assertEqual(m.span(), (0, 4)) |
| 2464 |
self.assertEqual(m.span(1), (3, 4)) |
| 2465 |
self.assertEqual(m.span(3), (3, 4)) |
| 2466 |
self.assertEqual(m.groups(), ('b', None, 'b')) |
| 2467 |
|
| 2468 |
def test_bug_40736(self): |
| 2469 |
with self.assertRaisesRegex(TypeError, "got 'int'"): |
| 2470 |
re.search("x*", 5) |
| 2471 |
with self.assertRaisesRegex(TypeError, "got 'type'"): |
| 2472 |
re.search("x*", type) |
| 2473 |
|
| 2474 |
# gh-117594: The test is not slow by itself, but it relies on |
| 2475 |
# the absolute computation time and can fail on very slow computers. |
| 2476 |
@requires_resource('cpu') |
| 2477 |
def test_search_anchor_at_beginning(self): |
| 2478 |
s = 'x'*10**7 |
| 2479 |
with Stopwatch() as stopwatch: |
| 2480 |
for p in r'\Ay', r'^y': |
| 2481 |
self.assertIsNone(re.search(p, s)) |
| 2482 |
self.assertEqual(re.split(p, s), [s]) |
| 2483 |
self.assertEqual(re.findall(p, s), []) |
| 2484 |
self.assertEqual(list(re.finditer(p, s)), []) |
| 2485 |
self.assertEqual(re.sub(p, '', s), s) |
| 2486 |
# Without optimization it takes 1 second on my computer. |
| 2487 |
# With optimization -- 0.0003 seconds. |
| 2488 |
self.assertLess(stopwatch.seconds, 0.1) |
| 2489 |
|
| 2490 |
def test_possessive_quantifiers(self): |
| 2491 |
"""Test Possessive Quantifiers |
| 2492 |
Test quantifiers of the form @+ for some repetition operator @, |
| 2493 |
e.g. x{3,5}+ meaning match from 3 to 5 greadily and proceed |
| 2494 |
without creating a stack frame for rolling the stack back and |
| 2495 |
trying 1 or more fewer matches.""" |
| 2496 |
self.assertIsNone(re.match('e*+e', 'eeee')) |
| 2497 |
self.assertEqual(re.match('e++a', 'eeea').group(0), 'eeea') |
| 2498 |
self.assertEqual(re.match('e?+a', 'ea').group(0), 'ea') |
| 2499 |
self.assertEqual(re.match('e{2,4}+a', 'eeea').group(0), 'eeea') |
| 2500 |
self.assertIsNone(re.match('(.)++.', 'ee')) |
| 2501 |
self.assertEqual(re.match('(ae)*+a', 'aea').groups(), ('ae',)) |
| 2502 |
self.assertEqual(re.match('([ae][ae])?+a', 'aea').groups(), |
| 2503 |
('ae',)) |
| 2504 |
self.assertEqual(re.match('(e?){2,4}+a', 'eeea').groups(), |
| 2505 |
('',)) |
| 2506 |
self.assertEqual(re.match('()*+a', 'a').groups(), ('',)) |
| 2507 |
self.assertEqual(re.search('x*+', 'axx').span(), (0, 0)) |
| 2508 |
self.assertEqual(re.search('x++', 'axx').span(), (1, 3)) |
| 2509 |
self.assertEqual(re.match('a*+', 'xxx').span(), (0, 0)) |
| 2510 |
self.assertEqual(re.match('x*+', 'xxxa').span(), (0, 3)) |
| 2511 |
self.assertIsNone(re.match('a++', 'xxx')) |
| 2512 |
self.assertIsNone(re.match(r"^(\w){1}+$", "abc")) |
| 2513 |
self.assertIsNone(re.match(r"^(\w){1,2}+$", "abc")) |
| 2514 |
|
| 2515 |
self.assertEqual(re.match(r"^(\w){3}+$", "abc").group(1), "c") |
| 2516 |
self.assertEqual(re.match(r"^(\w){1,3}+$", "abc").group(1), "c") |
| 2517 |
self.assertEqual(re.match(r"^(\w){1,4}+$", "abc").group(1), "c") |
| 2518 |
|
| 2519 |
self.assertIsNone(re.match("^x{1}+$", "xxx")) |
| 2520 |
self.assertIsNone(re.match("^x{1,2}+$", "xxx")) |
| 2521 |
|
| 2522 |
self.assertTrue(re.match("^x{3}+$", "xxx")) |
| 2523 |
self.assertTrue(re.match("^x{1,3}+$", "xxx")) |
| 2524 |
self.assertTrue(re.match("^x{1,4}+$", "xxx")) |
| 2525 |
|
| 2526 |
self.assertIsNone(re.match("^x{}+$", "xxx")) |
| 2527 |
self.assertTrue(re.match("^x{}+$", "x{}")) |
| 2528 |
|
| 2529 |
def test_fullmatch_possessive_quantifiers(self): |
| 2530 |
self.assertTrue(re.fullmatch(r'a++', 'a')) |
| 2531 |
self.assertTrue(re.fullmatch(r'a*+', 'a')) |
| 2532 |
self.assertTrue(re.fullmatch(r'a?+', 'a')) |
| 2533 |
self.assertTrue(re.fullmatch(r'a{1,3}+', 'a')) |
| 2534 |
self.assertIsNone(re.fullmatch(r'a++', 'ab')) |
| 2535 |
self.assertIsNone(re.fullmatch(r'a*+', 'ab')) |
| 2536 |
self.assertIsNone(re.fullmatch(r'a?+', 'ab')) |
| 2537 |
self.assertIsNone(re.fullmatch(r'a{1,3}+', 'ab')) |
| 2538 |
self.assertTrue(re.fullmatch(r'a++b', 'ab')) |
| 2539 |
self.assertTrue(re.fullmatch(r'a*+b', 'ab')) |
| 2540 |
self.assertTrue(re.fullmatch(r'a?+b', 'ab')) |
| 2541 |
self.assertTrue(re.fullmatch(r'a{1,3}+b', 'ab')) |
| 2542 |
|
| 2543 |
self.assertTrue(re.fullmatch(r'(?:ab)++', 'ab')) |
| 2544 |
self.assertTrue(re.fullmatch(r'(?:ab)*+', 'ab')) |
| 2545 |
self.assertTrue(re.fullmatch(r'(?:ab)?+', 'ab')) |
| 2546 |
self.assertTrue(re.fullmatch(r'(?:ab){1,3}+', 'ab')) |
| 2547 |
self.assertIsNone(re.fullmatch(r'(?:ab)++', 'abc')) |
| 2548 |
self.assertIsNone(re.fullmatch(r'(?:ab)*+', 'abc')) |
| 2549 |
self.assertIsNone(re.fullmatch(r'(?:ab)?+', 'abc')) |
| 2550 |
self.assertIsNone(re.fullmatch(r'(?:ab){1,3}+', 'abc')) |
| 2551 |
self.assertTrue(re.fullmatch(r'(?:ab)++c', 'abc')) |
| 2552 |
self.assertTrue(re.fullmatch(r'(?:ab)*+c', 'abc')) |
| 2553 |
self.assertTrue(re.fullmatch(r'(?:ab)?+c', 'abc')) |
| 2554 |
self.assertTrue(re.fullmatch(r'(?:ab){1,3}+c', 'abc')) |
| 2555 |
|
| 2556 |
def test_findall_possessive_quantifiers(self): |
| 2557 |
self.assertEqual(re.findall(r'a++', 'aab'), ['aa']) |
| 2558 |
self.assertEqual(re.findall(r'a*+', 'aab'), ['aa', '', '']) |
| 2559 |
self.assertEqual(re.findall(r'a?+', 'aab'), ['a', 'a', '', '']) |
| 2560 |
self.assertEqual(re.findall(r'a{1,3}+', 'aab'), ['aa']) |
| 2561 |
|
| 2562 |
self.assertEqual(re.findall(r'(?:ab)++', 'ababc'), ['abab']) |
| 2563 |
self.assertEqual(re.findall(r'(?:ab)*+', 'ababc'), ['abab', '', '']) |
| 2564 |
self.assertEqual(re.findall(r'(?:ab)?+', 'ababc'), ['ab', 'ab', '', '']) |
| 2565 |
self.assertEqual(re.findall(r'(?:ab){1,3}+', 'ababc'), ['abab']) |
| 2566 |
|
| 2567 |
def test_atomic_grouping(self): |
| 2568 |
"""Test Atomic Grouping |
| 2569 |
Test non-capturing groups of the form (?>...), which does |
| 2570 |
not maintain any stack point created within the group once the |
| 2571 |
group is finished being evaluated.""" |
| 2572 |
pattern1 = re.compile(r'a(?>bc|b)c') |
| 2573 |
self.assertIsNone(pattern1.match('abc')) |
| 2574 |
self.assertTrue(pattern1.match('abcc')) |
| 2575 |
self.assertIsNone(re.match(r'(?>.*).', 'abc')) |
| 2576 |
self.assertTrue(re.match(r'(?>x)++', 'xxx')) |
| 2577 |
self.assertTrue(re.match(r'(?>x++)', 'xxx')) |
| 2578 |
self.assertIsNone(re.match(r'(?>x)++x', 'xxx')) |
| 2579 |
self.assertIsNone(re.match(r'(?>x++)x', 'xxx')) |
| 2580 |
|
| 2581 |
def test_fullmatch_atomic_grouping(self): |
| 2582 |
self.assertTrue(re.fullmatch(r'(?>a+)', 'a')) |
| 2583 |
self.assertTrue(re.fullmatch(r'(?>a*)', 'a')) |
| 2584 |
self.assertTrue(re.fullmatch(r'(?>a?)', 'a')) |
| 2585 |
self.assertTrue(re.fullmatch(r'(?>a{1,3})', 'a')) |
| 2586 |
self.assertIsNone(re.fullmatch(r'(?>a+)', 'ab')) |
| 2587 |
self.assertIsNone(re.fullmatch(r'(?>a*)', 'ab')) |
| 2588 |
self.assertIsNone(re.fullmatch(r'(?>a?)', 'ab')) |
| 2589 |
self.assertIsNone(re.fullmatch(r'(?>a{1,3})', 'ab')) |
| 2590 |
self.assertTrue(re.fullmatch(r'(?>a+)b', 'ab')) |
| 2591 |
self.assertTrue(re.fullmatch(r'(?>a*)b', 'ab')) |
| 2592 |
self.assertTrue(re.fullmatch(r'(?>a?)b', 'ab')) |
| 2593 |
self.assertTrue(re.fullmatch(r'(?>a{1,3})b', 'ab')) |
| 2594 |
|
| 2595 |
self.assertTrue(re.fullmatch(r'(?>(?:ab)+)', 'ab')) |
| 2596 |
self.assertTrue(re.fullmatch(r'(?>(?:ab)*)', 'ab')) |
| 2597 |
self.assertTrue(re.fullmatch(r'(?>(?:ab)?)', 'ab')) |
| 2598 |
self.assertTrue(re.fullmatch(r'(?>(?:ab){1,3})', 'ab')) |
| 2599 |
self.assertIsNone(re.fullmatch(r'(?>(?:ab)+)', 'abc')) |
| 2600 |
self.assertIsNone(re.fullmatch(r'(?>(?:ab)*)', 'abc')) |
| 2601 |
self.assertIsNone(re.fullmatch(r'(?>(?:ab)?)', 'abc')) |
| 2602 |
self.assertIsNone(re.fullmatch(r'(?>(?:ab){1,3})', 'abc')) |
| 2603 |
self.assertTrue(re.fullmatch(r'(?>(?:ab)+)c', 'abc')) |
| 2604 |
self.assertTrue(re.fullmatch(r'(?>(?:ab)*)c', 'abc')) |
| 2605 |
self.assertTrue(re.fullmatch(r'(?>(?:ab)?)c', 'abc')) |
| 2606 |
self.assertTrue(re.fullmatch(r'(?>(?:ab){1,3})c', 'abc')) |
| 2607 |
|
| 2608 |
def test_findall_atomic_grouping(self): |
| 2609 |
self.assertEqual(re.findall(r'(?>a+)', 'aab'), ['aa']) |
| 2610 |
self.assertEqual(re.findall(r'(?>a*)', 'aab'), ['aa', '', '']) |
| 2611 |
self.assertEqual(re.findall(r'(?>a?)', 'aab'), ['a', 'a', '', '']) |
| 2612 |
self.assertEqual(re.findall(r'(?>a{1,3})', 'aab'), ['aa']) |
| 2613 |
|
| 2614 |
self.assertEqual(re.findall(r'(?>(?:ab)+)', 'ababc'), ['abab']) |
| 2615 |
self.assertEqual(re.findall(r'(?>(?:ab)*)', 'ababc'), ['abab', '', '']) |
| 2616 |
self.assertEqual(re.findall(r'(?>(?:ab)?)', 'ababc'), ['ab', 'ab', '', '']) |
| 2617 |
self.assertEqual(re.findall(r'(?>(?:ab){1,3})', 'ababc'), ['abab']) |
| 2618 |
|
| 2619 |
def test_bug_gh91616(self): |
| 2620 |
self.assertTrue(re.fullmatch(r'(?s:(?>.*?\.).*)\z', "a.txt")) # reproducer |
| 2621 |
self.assertTrue(re.fullmatch(r'(?s:(?=(?P<g0>.*?\.))(?P=g0).*)\z', "a.txt")) |
| 2622 |
|
| 2623 |
def test_bug_gh100061(self): |
| 2624 |
# gh-100061 |
| 2625 |
self.assertEqual(re.match('(?>(?:.(?!D))+)', 'ABCDE').span(), (0, 2)) |
| 2626 |
self.assertEqual(re.match('(?:.(?!D))++', 'ABCDE').span(), (0, 2)) |
| 2627 |
self.assertEqual(re.match('(?>(?:.(?!D))*)', 'ABCDE').span(), (0, 2)) |
| 2628 |
self.assertEqual(re.match('(?:.(?!D))*+', 'ABCDE').span(), (0, 2)) |
| 2629 |
self.assertEqual(re.match('(?>(?:.(?!D))?)', 'CDE').span(), (0, 0)) |
| 2630 |
self.assertEqual(re.match('(?:.(?!D))?+', 'CDE').span(), (0, 0)) |
| 2631 |
self.assertEqual(re.match('(?>(?:.(?!D)){1,3})', 'ABCDE').span(), (0, 2)) |
| 2632 |
self.assertEqual(re.match('(?:.(?!D)){1,3}+', 'ABCDE').span(), (0, 2)) |
| 2633 |
# gh-106052 |
| 2634 |
self.assertEqual(re.match("(?>(?:ab?c)+)", "aca").span(), (0, 2)) |
| 2635 |
self.assertEqual(re.match("(?:ab?c)++", "aca").span(), (0, 2)) |
| 2636 |
self.assertEqual(re.match("(?>(?:ab?c)*)", "aca").span(), (0, 2)) |
| 2637 |
self.assertEqual(re.match("(?:ab?c)*+", "aca").span(), (0, 2)) |
| 2638 |
self.assertEqual(re.match("(?>(?:ab?c)?)", "a").span(), (0, 0)) |
| 2639 |
self.assertEqual(re.match("(?:ab?c)?+", "a").span(), (0, 0)) |
| 2640 |
self.assertEqual(re.match("(?>(?:ab?c){1,3})", "aca").span(), (0, 2)) |
| 2641 |
self.assertEqual(re.match("(?:ab?c){1,3}+", "aca").span(), (0, 2)) |
| 2642 |
|
| 2643 |
def test_bug_gh101955(self): |
| 2644 |
# Possessive quantifier with nested alternative with capture groups |
| 2645 |
self.assertEqual(re.match('((x)|y|z)*+', 'xyz').groups(), ('z', 'x')) |
| 2646 |
self.assertEqual(re.match('((x)|y|z){3}+', 'xyz').groups(), ('z', 'x')) |
| 2647 |
self.assertEqual(re.match('((x)|y|z){3,}+', 'xyz').groups(), ('z', 'x')) |
| 2648 |
|
| 2649 |
@unittest.skipIf(multiprocessing is None, 'test requires multiprocessing') |
| 2650 |
def test_regression_gh94675(self): |
| 2651 |
pattern = re.compile(r'(?<=[({}])(((//[^\n]*)?[\n])([\000-\040])*)*' |
| 2652 |
r'((/[^/\[\n]*(([^\n]|(\[\n]*(]*)*\]))' |
| 2653 |
r'[^/\[]*)*/))((((//[^\n]*)?[\n])' |
| 2654 |
r'([\000-\040]|(/\*[^*]*\*+' |
| 2655 |
r'([^/*]\*+)*/))*)+(?=[^\000-\040);\]}]))') |
| 2656 |
input_js = '''a(function() { |
| 2657 |
/////////////////////////////////////////////////////////////////// |
| 2658 |
});''' |
| 2659 |
p = multiprocessing.Process(target=pattern.sub, args=('', input_js)) |
| 2660 |
p.start() |
| 2661 |
p.join(SHORT_TIMEOUT) |
| 2662 |
try: |
| 2663 |
self.assertFalse(p.is_alive(), 'pattern.sub() timed out') |
| 2664 |
finally: |
| 2665 |
if p.is_alive(): |
| 2666 |
p.terminate() |
| 2667 |
p.join() |
| 2668 |
|
| 2669 |
def test_fail(self): |
| 2670 |
self.assertEqual(re.search(r'12(?!)|3', '123')[0], '3') |
| 2671 |
|
| 2672 |
def test_character_set_any(self): |
| 2673 |
# The union of complementary character sets matches any character |
| 2674 |
# and is equivalent to "(?s:.)". |
| 2675 |
s = '1x\n' |
| 2676 |
for p in r'[\s\S]', r'[\d\D]', r'[\w\W]', r'[\S\s]', r'\s|\S': |
| 2677 |
with self.subTest(pattern=p): |
| 2678 |
self.assertEqual(re.findall(p, s), list(s)) |
| 2679 |
self.assertEqual(re.fullmatch('(?:' + p + ')+', s).group(), s) |
| 2680 |
|
| 2681 |
def test_character_set_none(self): |
| 2682 |
# Negation of the union of complementary character sets does not match |
| 2683 |
# any character. |
| 2684 |
s = '1x\n' |
| 2685 |
for p in r'[^\s\S]', r'[^\d\D]', r'[^\w\W]', r'[^\S\s]': |
| 2686 |
with self.subTest(pattern=p): |
| 2687 |
self.assertIsNone(re.search(p, s)) |
| 2688 |
self.assertIsNone(re.search('(?s:.)' + p, s)) |
| 2689 |
|
| 2690 |
def check_interrupt(self, pattern, string, maxcount): |
| 2691 |
class Interrupt(Exception): |
| 2692 |
pass |
| 2693 |
p = re.compile(pattern) |
| 2694 |
for n in range(maxcount): |
| 2695 |
try: |
| 2696 |
p._fail_after(n, Interrupt) |
| 2697 |
p.match(string) |
| 2698 |
return n |
| 2699 |
except Interrupt: |
| 2700 |
pass |
| 2701 |
finally: |
| 2702 |
p._fail_after(-1, None) |
| 2703 |
|
| 2704 |
@unittest.skipUnless(hasattr(re.Pattern, '_fail_after'), 'requires debug build') |
| 2705 |
def test_memory_leaks(self): |
| 2706 |
self.check_interrupt(r'(.)*:', 'abc:', 100) |
| 2707 |
self.check_interrupt(r'([^:])*?:', 'abc:', 100) |
| 2708 |
self.check_interrupt(r'([^:])*+:', 'abc:', 100) |
| 2709 |
self.check_interrupt(r'(.){2,4}:', 'abc:', 100) |
| 2710 |
self.check_interrupt(r'([^:]){2,4}?:', 'abc:', 100) |
| 2711 |
self.check_interrupt(r'([^:]){2,4}+:', 'abc:', 100) |
| 2712 |
|
| 2713 |
|
| 2714 |
def get_debug_out(pat): |
| 2715 |
with captured_stdout() as out: |
| 2716 |
re.compile(pat, re.DEBUG) |
| 2717 |
return out.getvalue() |
| 2718 |
|
| 2719 |
|
| 2720 |
@cpython_only |
| 2721 |
class DebugTests(unittest.TestCase): |
| 2722 |
maxDiff = None |
| 2723 |
|
| 2724 |
def test_debug_flag(self): |
| 2725 |
pat = r'(\.)(?:[ch]|py)(?(1)$|: )' |
| 2726 |
dump = '''\ |
| 2727 |
SUBPATTERN 1 0 0 |
| 2728 |
LITERAL 46 |
| 2729 |
BRANCH |
| 2730 |
IN |
| 2731 |
LITERAL 99 |
| 2732 |
LITERAL 104 |
| 2733 |
OR |
| 2734 |
LITERAL 112 |
| 2735 |
LITERAL 121 |
| 2736 |
GROUPREF_EXISTS 1 |
| 2737 |
AT AT_END |
| 2738 |
ELSE |
| 2739 |
LITERAL 58 |
| 2740 |
LITERAL 32 |
| 2741 |
|
| 2742 |
0. INFO 8 0b1 2 5 (to 9) |
| 2743 |
prefix_skip 0 |
| 2744 |
prefix [0x2e] ('.') |
| 2745 |
overlap [0] |
| 2746 |
9: MARK 0 |
| 2747 |
11. LITERAL 0x2e ('.') |
| 2748 |
13. MARK 1 |
| 2749 |
15. BRANCH 10 (to 26) |
| 2750 |
17. IN 6 (to 24) |
| 2751 |
19. LITERAL 0x63 ('c') |
| 2752 |
21. LITERAL 0x68 ('h') |
| 2753 |
23. FAILURE |
| 2754 |
24: JUMP 9 (to 34) |
| 2755 |
26: branch 7 (to 33) |
| 2756 |
27. LITERAL 0x70 ('p') |
| 2757 |
29. LITERAL 0x79 ('y') |
| 2758 |
31. JUMP 2 (to 34) |
| 2759 |
33: FAILURE |
| 2760 |
34: GROUPREF_EXISTS 0 6 (to 41) |
| 2761 |
37. AT END |
| 2762 |
39. JUMP 5 (to 45) |
| 2763 |
41: LITERAL 0x3a (':') |
| 2764 |
43. LITERAL 0x20 (' ') |
| 2765 |
45: SUCCESS |
| 2766 |
''' |
| 2767 |
self.assertEqual(get_debug_out(pat), dump) |
| 2768 |
# Debug output is output again even a second time (bypassing |
| 2769 |
# the cache -- issue #20426). |
| 2770 |
self.assertEqual(get_debug_out(pat), dump) |
| 2771 |
|
| 2772 |
def test_atomic_group(self): |
| 2773 |
self.assertEqual(get_debug_out(r'(?>ab?)'), '''\ |
| 2774 |
ATOMIC_GROUP |
| 2775 |
LITERAL 97 |
| 2776 |
MAX_REPEAT 0 1 |
| 2777 |
LITERAL 98 |
| 2778 |
|
| 2779 |
0. INFO 4 0b0 1 2 (to 5) |
| 2780 |
5: ATOMIC_GROUP 11 (to 17) |
| 2781 |
7. LITERAL 0x61 ('a') |
| 2782 |
9. REPEAT_ONE 6 0 1 (to 16) |
| 2783 |
13. LITERAL 0x62 ('b') |
| 2784 |
15. SUCCESS |
| 2785 |
16: SUCCESS |
| 2786 |
17: SUCCESS |
| 2787 |
''') |
| 2788 |
|
| 2789 |
def test_possesive_repeat_one(self): |
| 2790 |
self.assertEqual(get_debug_out(r'a?+'), '''\ |
| 2791 |
POSSESSIVE_REPEAT 0 1 |
| 2792 |
LITERAL 97 |
| 2793 |
|
| 2794 |
0. INFO 4 0b0 0 1 (to 5) |
| 2795 |
5: POSSESSIVE_REPEAT_ONE 6 0 1 (to 12) |
| 2796 |
9. LITERAL 0x61 ('a') |
| 2797 |
11. SUCCESS |
| 2798 |
12: SUCCESS |
| 2799 |
''') |
| 2800 |
|
| 2801 |
def test_possesive_repeat(self): |
| 2802 |
self.assertEqual(get_debug_out(r'(?:ab)?+'), '''\ |
| 2803 |
POSSESSIVE_REPEAT 0 1 |
| 2804 |
LITERAL 97 |
| 2805 |
LITERAL 98 |
| 2806 |
|
| 2807 |
0. INFO 4 0b0 0 2 (to 5) |
| 2808 |
5: POSSESSIVE_REPEAT 7 0 1 (to 13) |
| 2809 |
9. LITERAL 0x61 ('a') |
| 2810 |
11. LITERAL 0x62 ('b') |
| 2811 |
13: SUCCESS |
| 2812 |
14. SUCCESS |
| 2813 |
''') |
| 2814 |
|
| 2815 |
|
| 2816 |
class PatternReprTests(unittest.TestCase): |
| 2817 |
def check(self, pattern, expected): |
| 2818 |
self.assertEqual(repr(re.compile(pattern)), expected) |
| 2819 |
|
| 2820 |
def check_flags(self, pattern, flags, expected): |
| 2821 |
self.assertEqual(repr(re.compile(pattern, flags)), expected) |
| 2822 |
|
| 2823 |
def test_without_flags(self): |
| 2824 |
self.check('random pattern', |
| 2825 |
"re.compile('random pattern')") |
| 2826 |
|
| 2827 |
def test_single_flag(self): |
| 2828 |
self.check_flags('random pattern', re.IGNORECASE, |
| 2829 |
"re.compile('random pattern', re.IGNORECASE)") |
| 2830 |
|
| 2831 |
def test_multiple_flags(self): |
| 2832 |
self.check_flags('random pattern', re.I|re.S|re.X, |
| 2833 |
"re.compile('random pattern', " |
| 2834 |
"re.IGNORECASE|re.DOTALL|re.VERBOSE)") |
| 2835 |
|
| 2836 |
def test_unicode_flag(self): |
| 2837 |
self.check_flags('random pattern', re.U, |
| 2838 |
"re.compile('random pattern')") |
| 2839 |
self.check_flags('random pattern', re.I|re.S|re.U, |
| 2840 |
"re.compile('random pattern', " |
| 2841 |
"re.IGNORECASE|re.DOTALL)") |
| 2842 |
|
| 2843 |
def test_inline_flags(self): |
| 2844 |
self.check('(?i)pattern', |
| 2845 |
"re.compile('(?i)pattern', re.IGNORECASE)") |
| 2846 |
|
| 2847 |
def test_unknown_flags(self): |
| 2848 |
self.check_flags('random pattern', 0x123000, |
| 2849 |
"re.compile('random pattern', 0x123000)") |
| 2850 |
self.check_flags('random pattern', 0x123000|re.I, |
| 2851 |
"re.compile('random pattern', re.IGNORECASE|0x123000)") |
| 2852 |
|
| 2853 |
def test_bytes(self): |
| 2854 |
self.check(b'bytes pattern', |
| 2855 |
"re.compile(b'bytes pattern')") |
| 2856 |
self.check_flags(b'bytes pattern', re.A, |
| 2857 |
"re.compile(b'bytes pattern', re.ASCII)") |
| 2858 |
|
| 2859 |
def test_locale(self): |
| 2860 |
self.check_flags(b'bytes pattern', re.L, |
| 2861 |
"re.compile(b'bytes pattern', re.LOCALE)") |
| 2862 |
|
| 2863 |
def test_quotes(self): |
| 2864 |
self.check('random "double quoted" pattern', |
| 2865 |
'''re.compile('random "double quoted" pattern')''') |
| 2866 |
self.check("random 'single quoted' pattern", |
| 2867 |
'''re.compile("random 'single quoted' pattern")''') |
| 2868 |
self.check('''both 'single' and "double" quotes''', |
| 2869 |
'''re.compile('both \\'single\\' and "double" quotes')''') |
| 2870 |
|
| 2871 |
def test_long_pattern(self): |
| 2872 |
pattern = 'Very %spattern' % ('long ' * 1000) |
| 2873 |
r = repr(re.compile(pattern)) |
| 2874 |
self.assertLess(len(r), 300) |
| 2875 |
self.assertStartsWith(r, "re.compile('Very long long lon") |
| 2876 |
r = repr(re.compile(pattern, re.I)) |
| 2877 |
self.assertLess(len(r), 300) |
| 2878 |
self.assertStartsWith(r, "re.compile('Very long long lon") |
| 2879 |
self.assertEndsWith(r, ", re.IGNORECASE)") |
| 2880 |
|
| 2881 |
def test_flags_repr(self): |
| 2882 |
self.assertEqual(repr(re.I), "re.IGNORECASE") |
| 2883 |
self.assertEqual(repr(re.I|re.S|re.X), |
| 2884 |
"re.IGNORECASE|re.DOTALL|re.VERBOSE") |
| 2885 |
self.assertEqual(repr(re.I|re.S|re.X|(1<<20)), |
| 2886 |
"re.IGNORECASE|re.DOTALL|re.VERBOSE|0x100000") |
| 2887 |
self.assertEqual( |
| 2888 |
repr(~re.I), |
| 2889 |
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DOTALL|re.VERBOSE|re.DEBUG|0x1") |
| 2890 |
self.assertEqual(repr(~(re.I|re.S|re.X)), |
| 2891 |
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DEBUG|0x1") |
| 2892 |
self.assertEqual(repr(~(re.I|re.S|re.X|(1<<20))), |
| 2893 |
"re.ASCII|re.LOCALE|re.UNICODE|re.MULTILINE|re.DEBUG|0xffe01") |
| 2894 |
|
| 2895 |
|
| 2896 |
class ImplementationTest(unittest.TestCase): |
| 2897 |
""" |
| 2898 |
Test implementation details of the re module. |
| 2899 |
""" |
| 2900 |
|
| 2901 |
@cpython_only |
| 2902 |
def test_immutable(self): |
| 2903 |
# bpo-43908: check that re types are immutable |
| 2904 |
with self.assertRaises(TypeError): |
| 2905 |
re.Match.foo = 1 |
| 2906 |
with self.assertRaises(TypeError): |
| 2907 |
re.Pattern.foo = 1 |
| 2908 |
with self.assertRaises(TypeError): |
| 2909 |
pat = re.compile("") |
| 2910 |
tp = type(pat.scanner("")) |
| 2911 |
tp.foo = 1 |
| 2912 |
|
| 2913 |
def test_overlap_table(self): |
| 2914 |
f = re._compiler._generate_overlap_table |
| 2915 |
self.assertEqual(f(""), []) |
| 2916 |
self.assertEqual(f("a"), [0]) |
| 2917 |
self.assertEqual(f("abcd"), [0, 0, 0, 0]) |
| 2918 |
self.assertEqual(f("aaaa"), [0, 1, 2, 3]) |
| 2919 |
self.assertEqual(f("ababba"), [0, 0, 1, 2, 0, 1]) |
| 2920 |
self.assertEqual(f("abcabdac"), [0, 0, 0, 1, 2, 0, 1, 0]) |
| 2921 |
|
| 2922 |
def test_signedness(self): |
| 2923 |
self.assertGreaterEqual(re._compiler.MAXREPEAT, 0) |
| 2924 |
self.assertGreaterEqual(re._compiler.MAXGROUPS, 0) |
| 2925 |
|
| 2926 |
@cpython_only |
| 2927 |
def test_disallow_instantiation(self): |
| 2928 |
# Ensure that the type disallows instantiation (bpo-43916) |
| 2929 |
check_disallow_instantiation(self, re.Match) |
| 2930 |
check_disallow_instantiation(self, re.Pattern) |
| 2931 |
pat = re.compile("") |
| 2932 |
check_disallow_instantiation(self, type(pat.scanner(""))) |
| 2933 |
|
| 2934 |
def test_deprecated_modules(self): |
| 2935 |
deprecated = { |
| 2936 |
'sre_compile': ['compile', 'error', |
| 2937 |
'SRE_FLAG_IGNORECASE', 'SUBPATTERN', |
| 2938 |
'_compile_info'], |
| 2939 |
'sre_constants': ['error', 'SRE_FLAG_IGNORECASE', 'SUBPATTERN', |
| 2940 |
'_NamedIntConstant'], |
| 2941 |
'sre_parse': ['SubPattern', 'parse', |
| 2942 |
'SRE_FLAG_IGNORECASE', 'SUBPATTERN', |
| 2943 |
'_parse_sub'], |
| 2944 |
} |
| 2945 |
for name in deprecated: |
| 2946 |
with self.subTest(module=name): |
| 2947 |
sys.modules.pop(name, None) |
| 2948 |
with self.assertWarns(DeprecationWarning) as w: |
| 2949 |
__import__(name) |
| 2950 |
self.assertEqual(str(w.warning), |
| 2951 |
f"module {name!r} is deprecated") |
| 2952 |
self.assertEqual(w.filename, __file__) |
| 2953 |
self.assertIn(name, sys.modules) |
| 2954 |
mod = sys.modules[name] |
| 2955 |
self.assertEqual(mod.__name__, name) |
| 2956 |
self.assertEqual(mod.__package__, '') |
| 2957 |
for attr in deprecated[name]: |
| 2958 |
self.assertHasAttr(mod, attr) |
| 2959 |
del sys.modules[name] |
| 2960 |
|
| 2961 |
@cpython_only |
| 2962 |
def test_case_helpers(self): |
| 2963 |
import _sre |
| 2964 |
for i in range(128): |
| 2965 |
c = chr(i) |
| 2966 |
lo = ord(c.lower()) |
| 2967 |
self.assertEqual(_sre.ascii_tolower(i), lo) |
| 2968 |
self.assertEqual(_sre.unicode_tolower(i), lo) |
| 2969 |
iscased = c in string.ascii_letters |
| 2970 |
self.assertEqual(_sre.ascii_iscased(i), iscased) |
| 2971 |
self.assertEqual(_sre.unicode_iscased(i), iscased) |
| 2972 |
|
| 2973 |
for i in list(range(128, 0x1000)) + [0x10400, 0x10428]: |
| 2974 |
c = chr(i) |
| 2975 |
self.assertEqual(_sre.ascii_tolower(i), i) |
| 2976 |
if i != 0x0130: |
| 2977 |
self.assertEqual(_sre.unicode_tolower(i), ord(c.lower())) |
| 2978 |
iscased = c != c.lower() or c != c.upper() |
| 2979 |
self.assertFalse(_sre.ascii_iscased(i)) |
| 2980 |
self.assertEqual(_sre.unicode_iscased(i), |
| 2981 |
c != c.lower() or c != c.upper()) |
| 2982 |
|
| 2983 |
self.assertEqual(_sre.ascii_tolower(0x0130), 0x0130) |
| 2984 |
self.assertEqual(_sre.unicode_tolower(0x0130), ord('i')) |
| 2985 |
self.assertFalse(_sre.ascii_iscased(0x0130)) |
| 2986 |
self.assertTrue(_sre.unicode_iscased(0x0130)) |
| 2987 |
|
| 2988 |
@cpython_only |
| 2989 |
def test_dealloc(self): |
| 2990 |
# issue 3299: check for segfault in debug build |
| 2991 |
import _sre |
| 2992 |
# the overflow limit is different on wide and narrow builds and it |
| 2993 |
# depends on the definition of SRE_CODE (see sre.h). |
| 2994 |
# 2**128 should be big enough to overflow on both. For smaller values |
| 2995 |
# a RuntimeError is raised instead of OverflowError. |
| 2996 |
long_overflow = 2**128 |
| 2997 |
self.assertRaises(TypeError, re.finditer, "a", {}) |
| 2998 |
with self.assertRaises(OverflowError): |
| 2999 |
_sre.compile("abc", 0, [long_overflow], 0, {}, ()) |
| 3000 |
with self.assertRaises(TypeError): |
| 3001 |
_sre.compile({}, 0, [], 0, [], []) |
| 3002 |
# gh-110590: `TypeError` was overwritten with `OverflowError`: |
| 3003 |
with self.assertRaises(TypeError): |
| 3004 |
_sre.compile('', 0, ['abc'], 0, {}, ()) |
| 3005 |
|
| 3006 |
@cpython_only |
| 3007 |
def test_repeat_minmax_overflow_maxrepeat(self): |
| 3008 |
try: |
| 3009 |
from _sre import MAXREPEAT |
| 3010 |
except ImportError: |
| 3011 |
self.skipTest('requires _sre.MAXREPEAT constant') |
| 3012 |
string = "x" * 100000 |
| 3013 |
self.assertIsNone(re.match(r".{%d}" % (MAXREPEAT - 1), string)) |
| 3014 |
self.assertEqual(re.match(r".{,%d}" % (MAXREPEAT - 1), string).span(), |
| 3015 |
(0, 100000)) |
| 3016 |
self.assertIsNone(re.match(r".{%d,}?" % (MAXREPEAT - 1), string)) |
| 3017 |
self.assertRaises(OverflowError, re.compile, r".{%d}" % MAXREPEAT) |
| 3018 |
self.assertRaises(OverflowError, re.compile, r".{,%d}" % MAXREPEAT) |
| 3019 |
self.assertRaises(OverflowError, re.compile, r".{%d,}?" % MAXREPEAT) |
| 3020 |
|
| 3021 |
@cpython_only |
| 3022 |
def test_sre_template_invalid_group_index(self): |
| 3023 |
# see gh-106524 |
| 3024 |
import _sre |
| 3025 |
with self.assertRaises(TypeError) as cm: |
| 3026 |
_sre.template("", ["", -1, ""]) |
| 3027 |
self.assertIn("invalid template", str(cm.exception)) |
| 3028 |
with self.assertRaises(TypeError) as cm: |
| 3029 |
_sre.template("", ["", (), ""]) |
| 3030 |
self.assertIn("an integer is required", str(cm.exception)) |
| 3031 |
|
| 3032 |
|
| 3033 |
class ExternalTests(unittest.TestCase): |
| 3034 |
|
| 3035 |
def test_re_benchmarks(self): |
| 3036 |
're_tests benchmarks' |
| 3037 |
from test.re_tests import benchmarks |
| 3038 |
for pattern, s in benchmarks: |
| 3039 |
with self.subTest(pattern=pattern, string=s): |
| 3040 |
p = re.compile(pattern) |
| 3041 |
self.assertTrue(p.search(s)) |
| 3042 |
self.assertTrue(p.match(s)) |
| 3043 |
self.assertTrue(p.fullmatch(s)) |
| 3044 |
s2 = ' '*10000 + s + ' '*10000 |
| 3045 |
self.assertTrue(p.search(s2)) |
| 3046 |
self.assertTrue(p.match(s2, 10000)) |
| 3047 |
self.assertTrue(p.match(s2, 10000, 10000 + len(s))) |
| 3048 |
self.assertTrue(p.fullmatch(s2, 10000, 10000 + len(s))) |
| 3049 |
|
| 3050 |
def test_re_tests(self): |
| 3051 |
're_tests test suite' |
| 3052 |
from test.re_tests import tests, FAIL, SYNTAX_ERROR |
| 3053 |
for t in tests: |
| 3054 |
pattern = s = outcome = repl = expected = None |
| 3055 |
if len(t) == 5: |
| 3056 |
pattern, s, outcome, repl, expected = t |
| 3057 |
elif len(t) == 3: |
| 3058 |
pattern, s, outcome = t |
| 3059 |
else: |
| 3060 |
raise ValueError('Test tuples should have 3 or 5 fields', t) |
| 3061 |
|
| 3062 |
with self.subTest(pattern=pattern, string=s): |
| 3063 |
if outcome == SYNTAX_ERROR: # Expected a syntax error |
| 3064 |
with self.assertRaises(re.PatternError): |
| 3065 |
re.compile(pattern) |
| 3066 |
continue |
| 3067 |
|
| 3068 |
obj = re.compile(pattern) |
| 3069 |
result = obj.search(s) |
| 3070 |
if outcome == FAIL: |
| 3071 |
self.assertIsNone(result, 'Succeeded incorrectly') |
| 3072 |
continue |
| 3073 |
|
| 3074 |
with self.subTest(): |
| 3075 |
self.assertTrue(result, 'Failed incorrectly') |
| 3076 |
# Matched, as expected, so now we compute the |
| 3077 |
# result string and compare it to our expected result. |
| 3078 |
start, end = result.span(0) |
| 3079 |
vardict = {'found': result.group(0), |
| 3080 |
'groups': result.group(), |
| 3081 |
'flags': result.re.flags} |
| 3082 |
for i in range(1, 100): |
| 3083 |
try: |
| 3084 |
gi = result.group(i) |
| 3085 |
# Special hack because else the string concat fails: |
| 3086 |
if gi is None: |
| 3087 |
gi = "None" |
| 3088 |
except IndexError: |
| 3089 |
gi = "Error" |
| 3090 |
vardict['g%d' % i] = gi |
| 3091 |
for i in result.re.groupindex.keys(): |
| 3092 |
try: |
| 3093 |
gi = result.group(i) |
| 3094 |
if gi is None: |
| 3095 |
gi = "None" |
| 3096 |
except IndexError: |
| 3097 |
gi = "Error" |
| 3098 |
vardict[i] = gi |
| 3099 |
self.assertEqual(eval(repl, vardict), expected, |
| 3100 |
'grouping error') |
| 3101 |
|
| 3102 |
# Try the match with both pattern and string converted to |
| 3103 |
# bytes, and check that it still succeeds. |
| 3104 |
try: |
| 3105 |
bpat = bytes(pattern, "ascii") |
| 3106 |
bs = bytes(s, "ascii") |
| 3107 |
except UnicodeEncodeError: |
| 3108 |
# skip non-ascii tests |
| 3109 |
pass |
| 3110 |
else: |
| 3111 |
with self.subTest('bytes pattern match'): |
| 3112 |
obj = re.compile(bpat) |
| 3113 |
self.assertTrue(obj.search(bs)) |
| 3114 |
|
| 3115 |
# Try the match with LOCALE enabled, and check that it |
| 3116 |
# still succeeds. |
| 3117 |
with self.subTest('locale-sensitive match'): |
| 3118 |
obj = re.compile(bpat, re.LOCALE) |
| 3119 |
result = obj.search(bs) |
| 3120 |
if result is None: |
| 3121 |
print('=== Fails on locale-sensitive match', t) |
| 3122 |
|
| 3123 |
# Try the match with the search area limited to the extent |
| 3124 |
# of the match and see if it still succeeds. \B will |
| 3125 |
# break (because it won't match at the end or start of a |
| 3126 |
# string), so we'll ignore patterns that feature it. |
| 3127 |
if (pattern[:2] != r'\B' and pattern[-2:] != r'\B' |
| 3128 |
and result is not None): |
| 3129 |
with self.subTest('range-limited match'): |
| 3130 |
obj = re.compile(pattern) |
| 3131 |
self.assertTrue(obj.search(s, start, end + 1)) |
| 3132 |
|
| 3133 |
# Try the match with IGNORECASE enabled, and check that it |
| 3134 |
# still succeeds. |
| 3135 |
with self.subTest('case-insensitive match'): |
| 3136 |
obj = re.compile(pattern, re.IGNORECASE) |
| 3137 |
self.assertTrue(obj.search(s)) |
| 3138 |
|
| 3139 |
# Try the match with UNICODE locale enabled, and check |
| 3140 |
# that it still succeeds. |
| 3141 |
with self.subTest('unicode-sensitive match'): |
| 3142 |
obj = re.compile(pattern, re.UNICODE) |
| 3143 |
self.assertTrue(obj.search(s)) |
| 3144 |
|
| 3145 |
|
| 3146 |
if __name__ == "__main__": |
| 3147 |
unittest.main() |