PageRenderTime 37ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/test/test_re.py

https://bitbucket.org/mirror/cpython/
Python | 1869 lines | 1817 code | 34 blank | 18 comment | 18 complexity | a90bba6abd28161c18061a8cb0794575 MD5 | raw file
Possible License(s): Unlicense, 0BSD, BSD-3-Clause
  1. from test.support import verbose, run_unittest, gc_collect, bigmemtest, _2G, \
  2. cpython_only, captured_stdout
  3. import io
  4. import locale
  5. import re
  6. from re import Scanner
  7. import sre_compile
  8. import sre_constants
  9. import sys
  10. import string
  11. import traceback
  12. import unittest
  13. from weakref import proxy
  14. # Misc tests from Tim Peters' re.doc
  15. # WARNING: Don't change details in these tests if you don't know
  16. # what you're doing. Some of these tests were carefully modeled to
  17. # cover most of the code.
  18. class S(str):
  19. def __getitem__(self, index):
  20. return S(super().__getitem__(index))
  21. class B(bytes):
  22. def __getitem__(self, index):
  23. return B(super().__getitem__(index))
  24. class ReTests(unittest.TestCase):
  25. def assertTypedEqual(self, actual, expect, msg=None):
  26. self.assertEqual(actual, expect, msg)
  27. def recurse(actual, expect):
  28. if isinstance(expect, (tuple, list)):
  29. for x, y in zip(actual, expect):
  30. recurse(x, y)
  31. else:
  32. self.assertIs(type(actual), type(expect), msg)
  33. recurse(actual, expect)
  34. def checkPatternError(self, pattern, errmsg, pos=None):
  35. with self.assertRaises(re.error) as cm:
  36. re.compile(pattern)
  37. with self.subTest(pattern=pattern):
  38. err = cm.exception
  39. self.assertEqual(err.msg, errmsg)
  40. if pos is not None:
  41. self.assertEqual(err.pos, pos)
  42. def checkTemplateError(self, pattern, repl, string, errmsg, pos=None):
  43. with self.assertRaises(re.error) as cm:
  44. re.sub(pattern, repl, string)
  45. with self.subTest(pattern=pattern, repl=repl):
  46. err = cm.exception
  47. self.assertEqual(err.msg, errmsg)
  48. if pos is not None:
  49. self.assertEqual(err.pos, pos)
  50. def test_keep_buffer(self):
  51. # See bug 14212
  52. b = bytearray(b'x')
  53. it = re.finditer(b'a', b)
  54. with self.assertRaises(BufferError):
  55. b.extend(b'x'*400)
  56. list(it)
  57. del it
  58. gc_collect()
  59. b.extend(b'x'*400)
  60. def test_weakref(self):
  61. s = 'QabbbcR'
  62. x = re.compile('ab+c')
  63. y = proxy(x)
  64. self.assertEqual(x.findall('QabbbcR'), y.findall('QabbbcR'))
  65. def test_search_star_plus(self):
  66. self.assertEqual(re.search('x*', 'axx').span(0), (0, 0))
  67. self.assertEqual(re.search('x*', 'axx').span(), (0, 0))
  68. self.assertEqual(re.search('x+', 'axx').span(0), (1, 3))
  69. self.assertEqual(re.search('x+', 'axx').span(), (1, 3))
  70. self.assertIsNone(re.search('x', 'aaa'))
  71. self.assertEqual(re.match('a*', 'xxx').span(0), (0, 0))
  72. self.assertEqual(re.match('a*', 'xxx').span(), (0, 0))
  73. self.assertEqual(re.match('x*', 'xxxa').span(0), (0, 3))
  74. self.assertEqual(re.match('x*', 'xxxa').span(), (0, 3))
  75. self.assertIsNone(re.match('a+', 'xxx'))
  76. def bump_num(self, matchobj):
  77. int_value = int(matchobj.group(0))
  78. return str(int_value + 1)
  79. def test_basic_re_sub(self):
  80. self.assertTypedEqual(re.sub('y', 'a', 'xyz'), 'xaz')
  81. self.assertTypedEqual(re.sub('y', S('a'), S('xyz')), 'xaz')
  82. self.assertTypedEqual(re.sub(b'y', b'a', b'xyz'), b'xaz')
  83. self.assertTypedEqual(re.sub(b'y', B(b'a'), B(b'xyz')), b'xaz')
  84. self.assertTypedEqual(re.sub(b'y', bytearray(b'a'), bytearray(b'xyz')), b'xaz')
  85. self.assertTypedEqual(re.sub(b'y', memoryview(b'a'), memoryview(b'xyz')), b'xaz')
  86. for y in ("\xe0", "\u0430", "\U0001d49c"):
  87. self.assertEqual(re.sub(y, 'a', 'x%sz' % y), 'xaz')
  88. self.assertEqual(re.sub("(?i)b+", "x", "bbbb BBBB"), 'x x')
  89. self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y'),
  90. '9.3 -3 24x100y')
  91. self.assertEqual(re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y', count=3),
  92. '9.3 -3 23x99y')
  93. self.assertEqual(re.sub('.', lambda m: r"\n", 'x'), '\\n')
  94. self.assertEqual(re.sub('.', r"\n", 'x'), '\n')
  95. s = r"\1\1"
  96. self.assertEqual(re.sub('(.)', s, 'x'), 'xx')
  97. self.assertEqual(re.sub('(.)', re.escape(s), 'x'), s)
  98. self.assertEqual(re.sub('(.)', lambda m: s, 'x'), s)
  99. self.assertEqual(re.sub('(?P<a>x)', '\g<a>\g<a>', 'xx'), 'xxxx')
  100. self.assertEqual(re.sub('(?P<a>x)', '\g<a>\g<1>', 'xx'), 'xxxx')
  101. self.assertEqual(re.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx'), 'xxxx')
  102. self.assertEqual(re.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx'), 'xxxx')
  103. self.assertEqual(re.sub('a', r'\t\n\v\r\f\a\b', 'a'), '\t\n\v\r\f\a\b')
  104. self.assertEqual(re.sub('a', '\t\n\v\r\f\a\b', 'a'), '\t\n\v\r\f\a\b')
  105. self.assertEqual(re.sub('a', '\t\n\v\r\f\a\b', 'a'),
  106. (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)+chr(8)))
  107. for c in 'cdehijklmopqsuwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ':
  108. with self.subTest(c):
  109. with self.assertRaises(re.error):
  110. self.assertEqual(re.sub('a', '\\' + c, 'a'), '\\' + c)
  111. self.assertEqual(re.sub('^\s*', 'X', 'test'), 'Xtest')
  112. def test_bug_449964(self):
  113. # fails for group followed by other escape
  114. self.assertEqual(re.sub(r'(?P<unk>x)', '\g<1>\g<1>\\b', 'xx'),
  115. 'xx\bxx\b')
  116. def test_bug_449000(self):
  117. # Test for sub() on escaped characters
  118. self.assertEqual(re.sub(r'\r\n', r'\n', 'abc\r\ndef\r\n'),
  119. 'abc\ndef\n')
  120. self.assertEqual(re.sub('\r\n', r'\n', 'abc\r\ndef\r\n'),
  121. 'abc\ndef\n')
  122. self.assertEqual(re.sub(r'\r\n', '\n', 'abc\r\ndef\r\n'),
  123. 'abc\ndef\n')
  124. self.assertEqual(re.sub('\r\n', '\n', 'abc\r\ndef\r\n'),
  125. 'abc\ndef\n')
  126. def test_bug_1661(self):
  127. # Verify that flags do not get silently ignored with compiled patterns
  128. pattern = re.compile('.')
  129. self.assertRaises(ValueError, re.match, pattern, 'A', re.I)
  130. self.assertRaises(ValueError, re.search, pattern, 'A', re.I)
  131. self.assertRaises(ValueError, re.findall, pattern, 'A', re.I)
  132. self.assertRaises(ValueError, re.compile, pattern, re.I)
  133. def test_bug_3629(self):
  134. # A regex that triggered a bug in the sre-code validator
  135. re.compile("(?P<quote>)(?(quote))")
  136. def test_sub_template_numeric_escape(self):
  137. # bug 776311 and friends
  138. self.assertEqual(re.sub('x', r'\0', 'x'), '\0')
  139. self.assertEqual(re.sub('x', r'\000', 'x'), '\000')
  140. self.assertEqual(re.sub('x', r'\001', 'x'), '\001')
  141. self.assertEqual(re.sub('x', r'\008', 'x'), '\0' + '8')
  142. self.assertEqual(re.sub('x', r'\009', 'x'), '\0' + '9')
  143. self.assertEqual(re.sub('x', r'\111', 'x'), '\111')
  144. self.assertEqual(re.sub('x', r'\117', 'x'), '\117')
  145. self.assertEqual(re.sub('x', r'\377', 'x'), '\377')
  146. self.assertEqual(re.sub('x', r'\1111', 'x'), '\1111')
  147. self.assertEqual(re.sub('x', r'\1111', 'x'), '\111' + '1')
  148. self.assertEqual(re.sub('x', r'\00', 'x'), '\x00')
  149. self.assertEqual(re.sub('x', r'\07', 'x'), '\x07')
  150. self.assertEqual(re.sub('x', r'\08', 'x'), '\0' + '8')
  151. self.assertEqual(re.sub('x', r'\09', 'x'), '\0' + '9')
  152. self.assertEqual(re.sub('x', r'\0a', 'x'), '\0' + 'a')
  153. self.checkTemplateError('x', r'\400', 'x',
  154. r'octal escape value \400 outside of '
  155. r'range 0-0o377', 0)
  156. self.checkTemplateError('x', r'\777', 'x',
  157. r'octal escape value \777 outside of '
  158. r'range 0-0o377', 0)
  159. self.checkTemplateError('x', r'\1', 'x', 'invalid group reference')
  160. self.checkTemplateError('x', r'\8', 'x', 'invalid group reference')
  161. self.checkTemplateError('x', r'\9', 'x', 'invalid group reference')
  162. self.checkTemplateError('x', r'\11', 'x', 'invalid group reference')
  163. self.checkTemplateError('x', r'\18', 'x', 'invalid group reference')
  164. self.checkTemplateError('x', r'\1a', 'x', 'invalid group reference')
  165. self.checkTemplateError('x', r'\90', 'x', 'invalid group reference')
  166. self.checkTemplateError('x', r'\99', 'x', 'invalid group reference')
  167. self.checkTemplateError('x', r'\118', 'x', 'invalid group reference') # r'\11' + '8'
  168. self.checkTemplateError('x', r'\11a', 'x', 'invalid group reference')
  169. self.checkTemplateError('x', r'\181', 'x', 'invalid group reference') # r'\18' + '1'
  170. self.checkTemplateError('x', r'\800', 'x', 'invalid group reference') # r'\80' + '0'
  171. # in python2.3 (etc), these loop endlessly in sre_parser.py
  172. self.assertEqual(re.sub('(((((((((((x)))))))))))', r'\11', 'x'), 'x')
  173. self.assertEqual(re.sub('((((((((((y))))))))))(.)', r'\118', 'xyz'),
  174. 'xz8')
  175. self.assertEqual(re.sub('((((((((((y))))))))))(.)', r'\11a', 'xyz'),
  176. 'xza')
  177. def test_qualified_re_sub(self):
  178. self.assertEqual(re.sub('a', 'b', 'aaaaa'), 'bbbbb')
  179. self.assertEqual(re.sub('a', 'b', 'aaaaa', count=1), 'baaaa')
  180. def test_bug_114660(self):
  181. self.assertEqual(re.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello there'),
  182. 'hello there')
  183. def test_bug_462270(self):
  184. # Test for empty sub() behaviour, see SF bug #462270
  185. self.assertEqual(re.sub('x*', '-', 'abxd'), '-a-b-d-')
  186. self.assertEqual(re.sub('x+', '-', 'abxd'), 'ab-d')
  187. def test_symbolic_groups(self):
  188. re.compile('(?P<a>x)(?P=a)(?(a)y)')
  189. re.compile('(?P<a1>x)(?P=a1)(?(a1)y)')
  190. re.compile('(?P<a1>x)\1(?(1)y)')
  191. self.checkPatternError('(?P<a>)(?P<a>)',
  192. "redefinition of group name 'a' as group 2; "
  193. "was group 1")
  194. self.checkPatternError('(?P<a>(?P=a))',
  195. "cannot refer to an open group", 10)
  196. self.checkPatternError('(?Pxy)', 'unknown extension ?Px')
  197. self.checkPatternError('(?P<a>)(?P=a', 'missing ), unterminated name', 11)
  198. self.checkPatternError('(?P=', 'missing group name', 4)
  199. self.checkPatternError('(?P=)', 'missing group name', 4)
  200. self.checkPatternError('(?P=1)', "bad character in group name '1'", 4)
  201. self.checkPatternError('(?P=a)', "unknown group name 'a'")
  202. self.checkPatternError('(?P=a1)', "unknown group name 'a1'")
  203. self.checkPatternError('(?P=a.)', "bad character in group name 'a.'", 4)
  204. self.checkPatternError('(?P<)', 'missing >, unterminated name', 4)
  205. self.checkPatternError('(?P<a', 'missing >, unterminated name', 4)
  206. self.checkPatternError('(?P<', 'missing group name', 4)
  207. self.checkPatternError('(?P<>)', 'missing group name', 4)
  208. self.checkPatternError(r'(?P<1>)', "bad character in group name '1'", 4)
  209. self.checkPatternError(r'(?P<a.>)', "bad character in group name 'a.'", 4)
  210. self.checkPatternError(r'(?(', 'missing group name', 3)
  211. self.checkPatternError(r'(?())', 'missing group name', 3)
  212. self.checkPatternError(r'(?(a))', "unknown group name 'a'", 3)
  213. self.checkPatternError(r'(?(-1))', "bad character in group name '-1'", 3)
  214. self.checkPatternError(r'(?(1a))', "bad character in group name '1a'", 3)
  215. self.checkPatternError(r'(?(a.))', "bad character in group name 'a.'", 3)
  216. # New valid/invalid identifiers in Python 3
  217. re.compile('(?P<Âľ>x)(?P=Âľ)(?(Âľ)y)')
  218. re.compile('(?P<𝔘𝔫𝔦𝔠𝔬𝔡𝔢>x)(?P=𝔘𝔫𝔦𝔠𝔬𝔡𝔢)(?(𝔘𝔫𝔦𝔠𝔬𝔡𝔢)y)')
  219. self.checkPatternError('(?P<Š>x)', "bad character in group name 'Š'", 4)
  220. # Support > 100 groups.
  221. pat = '|'.join('x(?P<a%d>%x)y' % (i, i) for i in range(1, 200 + 1))
  222. pat = '(?:%s)(?(200)z|t)' % pat
  223. self.assertEqual(re.match(pat, 'xc8yz').span(), (0, 5))
  224. def test_symbolic_refs(self):
  225. self.checkTemplateError('(?P<a>x)', '\g<a', 'xx',
  226. 'missing >, unterminated name', 3)
  227. self.checkTemplateError('(?P<a>x)', '\g<', 'xx',
  228. 'missing group name', 3)
  229. self.checkTemplateError('(?P<a>x)', '\g', 'xx', 'missing <', 2)
  230. self.checkTemplateError('(?P<a>x)', '\g<a a>', 'xx',
  231. "bad character in group name 'a a'", 3)
  232. self.checkTemplateError('(?P<a>x)', '\g<>', 'xx',
  233. 'missing group name', 3)
  234. self.checkTemplateError('(?P<a>x)', '\g<1a1>', 'xx',
  235. "bad character in group name '1a1'", 3)
  236. self.checkTemplateError('(?P<a>x)', r'\g<2>', 'xx',
  237. 'invalid group reference')
  238. self.checkTemplateError('(?P<a>x)', r'\2', 'xx',
  239. 'invalid group reference')
  240. with self.assertRaisesRegex(IndexError, "unknown group name 'ab'"):
  241. re.sub('(?P<a>x)', '\g<ab>', 'xx')
  242. self.assertEqual(re.sub('(?P<a>x)|(?P<b>y)', r'\g<b>', 'xx'), '')
  243. self.assertEqual(re.sub('(?P<a>x)|(?P<b>y)', r'\2', 'xx'), '')
  244. self.checkTemplateError('(?P<a>x)', '\g<-1>', 'xx',
  245. "bad character in group name '-1'", 3)
  246. # New valid/invalid identifiers in Python 3
  247. self.assertEqual(re.sub('(?P<Âľ>x)', r'\g<Âľ>', 'xx'), 'xx')
  248. self.assertEqual(re.sub('(?P<𝔘𝔫𝔦𝔠𝔬𝔡𝔢>x)', r'\g<𝔘𝔫𝔦𝔠𝔬𝔡𝔢>', 'xx'), 'xx')
  249. self.checkTemplateError('(?P<a>x)', '\g<Š>', 'xx',
  250. "bad character in group name 'Š'", 3)
  251. # Support > 100 groups.
  252. pat = '|'.join('x(?P<a%d>%x)y' % (i, i) for i in range(1, 200 + 1))
  253. self.assertEqual(re.sub(pat, '\g<200>', 'xc8yzxc8y'), 'c8zc8')
  254. def test_re_subn(self):
  255. self.assertEqual(re.subn("(?i)b+", "x", "bbbb BBBB"), ('x x', 2))
  256. self.assertEqual(re.subn("b+", "x", "bbbb BBBB"), ('x BBBB', 1))
  257. self.assertEqual(re.subn("b+", "x", "xyz"), ('xyz', 0))
  258. self.assertEqual(re.subn("b*", "x", "xyz"), ('xxxyxzx', 4))
  259. self.assertEqual(re.subn("b*", "x", "xyz", count=2), ('xxxyz', 2))
  260. def test_re_split(self):
  261. for string in ":a:b::c", S(":a:b::c"):
  262. self.assertTypedEqual(re.split(":", string),
  263. ['', 'a', 'b', '', 'c'])
  264. self.assertTypedEqual(re.split(":+", string),
  265. ['', 'a', 'b', 'c'])
  266. self.assertTypedEqual(re.split("(:+)", string),
  267. ['', ':', 'a', ':', 'b', '::', 'c'])
  268. for string in (b":a:b::c", B(b":a:b::c"), bytearray(b":a:b::c"),
  269. memoryview(b":a:b::c")):
  270. self.assertTypedEqual(re.split(b":", string),
  271. [b'', b'a', b'b', b'', b'c'])
  272. self.assertTypedEqual(re.split(b":+", string),
  273. [b'', b'a', b'b', b'c'])
  274. self.assertTypedEqual(re.split(b"(:+)", string),
  275. [b'', b':', b'a', b':', b'b', b'::', b'c'])
  276. for a, b, c in ("\xe0\xdf\xe7", "\u0430\u0431\u0432",
  277. "\U0001d49c\U0001d49e\U0001d4b5"):
  278. string = ":%s:%s::%s" % (a, b, c)
  279. self.assertEqual(re.split(":", string), ['', a, b, '', c])
  280. self.assertEqual(re.split(":+", string), ['', a, b, c])
  281. self.assertEqual(re.split("(:+)", string),
  282. ['', ':', a, ':', b, '::', c])
  283. self.assertEqual(re.split("(?::+)", ":a:b::c"), ['', 'a', 'b', 'c'])
  284. self.assertEqual(re.split("(:)+", ":a:b::c"),
  285. ['', ':', 'a', ':', 'b', ':', 'c'])
  286. self.assertEqual(re.split("([b:]+)", ":a:b::c"),
  287. ['', ':', 'a', ':b::', 'c'])
  288. self.assertEqual(re.split("(b)|(:+)", ":a:b::c"),
  289. ['', None, ':', 'a', None, ':', '', 'b', None, '',
  290. None, '::', 'c'])
  291. self.assertEqual(re.split("(?:b)|(?::+)", ":a:b::c"),
  292. ['', 'a', '', '', 'c'])
  293. for sep, expected in [
  294. (':*', ['', 'a', 'b', 'c']),
  295. ('(?::*)', ['', 'a', 'b', 'c']),
  296. ('(:*)', ['', ':', 'a', ':', 'b', '::', 'c']),
  297. ('(:)*', ['', ':', 'a', ':', 'b', ':', 'c']),
  298. ]:
  299. with self.subTest(sep=sep), self.assertWarns(FutureWarning):
  300. self.assertTypedEqual(re.split(sep, ':a:b::c'), expected)
  301. for sep, expected in [
  302. ('', [':a:b::c']),
  303. (r'\b', [':a:b::c']),
  304. (r'(?=:)', [':a:b::c']),
  305. (r'(?<=:)', [':a:b::c']),
  306. ]:
  307. with self.subTest(sep=sep), self.assertRaises(ValueError):
  308. self.assertTypedEqual(re.split(sep, ':a:b::c'), expected)
  309. def test_qualified_re_split(self):
  310. self.assertEqual(re.split(":", ":a:b::c", maxsplit=2), ['', 'a', 'b::c'])
  311. self.assertEqual(re.split(':', 'a:b:c:d', maxsplit=2), ['a', 'b', 'c:d'])
  312. self.assertEqual(re.split("(:)", ":a:b::c", maxsplit=2),
  313. ['', ':', 'a', ':', 'b::c'])
  314. self.assertEqual(re.split("(:+)", ":a:b::c", maxsplit=2),
  315. ['', ':', 'a', ':', 'b::c'])
  316. with self.assertWarns(FutureWarning):
  317. self.assertEqual(re.split("(:*)", ":a:b::c", maxsplit=2),
  318. ['', ':', 'a', ':', 'b::c'])
  319. def test_re_findall(self):
  320. self.assertEqual(re.findall(":+", "abc"), [])
  321. for string in "a:b::c:::d", S("a:b::c:::d"):
  322. self.assertTypedEqual(re.findall(":+", string),
  323. [":", "::", ":::"])
  324. self.assertTypedEqual(re.findall("(:+)", string),
  325. [":", "::", ":::"])
  326. self.assertTypedEqual(re.findall("(:)(:*)", string),
  327. [(":", ""), (":", ":"), (":", "::")])
  328. for string in (b"a:b::c:::d", B(b"a:b::c:::d"), bytearray(b"a:b::c:::d"),
  329. memoryview(b"a:b::c:::d")):
  330. self.assertTypedEqual(re.findall(b":+", string),
  331. [b":", b"::", b":::"])
  332. self.assertTypedEqual(re.findall(b"(:+)", string),
  333. [b":", b"::", b":::"])
  334. self.assertTypedEqual(re.findall(b"(:)(:*)", string),
  335. [(b":", b""), (b":", b":"), (b":", b"::")])
  336. for x in ("\xe0", "\u0430", "\U0001d49c"):
  337. xx = x * 2
  338. xxx = x * 3
  339. string = "a%sb%sc%sd" % (x, xx, xxx)
  340. self.assertEqual(re.findall("%s+" % x, string), [x, xx, xxx])
  341. self.assertEqual(re.findall("(%s+)" % x, string), [x, xx, xxx])
  342. self.assertEqual(re.findall("(%s)(%s*)" % (x, x), string),
  343. [(x, ""), (x, x), (x, xx)])
  344. def test_bug_117612(self):
  345. self.assertEqual(re.findall(r"(a|(b))", "aba"),
  346. [("a", ""),("b", "b"),("a", "")])
  347. def test_re_match(self):
  348. for string in 'a', S('a'):
  349. self.assertEqual(re.match('a', string).groups(), ())
  350. self.assertEqual(re.match('(a)', string).groups(), ('a',))
  351. self.assertEqual(re.match('(a)', string).group(0), 'a')
  352. self.assertEqual(re.match('(a)', string).group(1), 'a')
  353. self.assertEqual(re.match('(a)', string).group(1, 1), ('a', 'a'))
  354. for string in b'a', B(b'a'), bytearray(b'a'), memoryview(b'a'):
  355. self.assertEqual(re.match(b'a', string).groups(), ())
  356. self.assertEqual(re.match(b'(a)', string).groups(), (b'a',))
  357. self.assertEqual(re.match(b'(a)', string).group(0), b'a')
  358. self.assertEqual(re.match(b'(a)', string).group(1), b'a')
  359. self.assertEqual(re.match(b'(a)', string).group(1, 1), (b'a', b'a'))
  360. for a in ("\xe0", "\u0430", "\U0001d49c"):
  361. self.assertEqual(re.match(a, a).groups(), ())
  362. self.assertEqual(re.match('(%s)' % a, a).groups(), (a,))
  363. self.assertEqual(re.match('(%s)' % a, a).group(0), a)
  364. self.assertEqual(re.match('(%s)' % a, a).group(1), a)
  365. self.assertEqual(re.match('(%s)' % a, a).group(1, 1), (a, a))
  366. pat = re.compile('((a)|(b))(c)?')
  367. self.assertEqual(pat.match('a').groups(), ('a', 'a', None, None))
  368. self.assertEqual(pat.match('b').groups(), ('b', None, 'b', None))
  369. self.assertEqual(pat.match('ac').groups(), ('a', 'a', None, 'c'))
  370. self.assertEqual(pat.match('bc').groups(), ('b', None, 'b', 'c'))
  371. self.assertEqual(pat.match('bc').groups(""), ('b', "", 'b', 'c'))
  372. pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
  373. self.assertEqual(pat.match('a').group(1, 2, 3), ('a', None, None))
  374. self.assertEqual(pat.match('b').group('a1', 'b2', 'c3'),
  375. (None, 'b', None))
  376. self.assertEqual(pat.match('ac').group(1, 'b2', 3), ('a', None, 'c'))
  377. def test_group(self):
  378. class Index:
  379. def __init__(self, value):
  380. self.value = value
  381. def __index__(self):
  382. return self.value
  383. # A single group
  384. m = re.match('(a)(b)', 'ab')
  385. self.assertEqual(m.group(), 'ab')
  386. self.assertEqual(m.group(0), 'ab')
  387. self.assertEqual(m.group(1), 'a')
  388. self.assertEqual(m.group(Index(1)), 'a')
  389. self.assertRaises(IndexError, m.group, -1)
  390. self.assertRaises(IndexError, m.group, 3)
  391. self.assertRaises(IndexError, m.group, 1<<1000)
  392. self.assertRaises(IndexError, m.group, Index(1<<1000))
  393. self.assertRaises(IndexError, m.group, 'x')
  394. # Multiple groups
  395. self.assertEqual(m.group(2, 1), ('b', 'a'))
  396. self.assertEqual(m.group(Index(2), Index(1)), ('b', 'a'))
  397. def test_re_fullmatch(self):
  398. # Issue 16203: Proposal: add re.fullmatch() method.
  399. self.assertEqual(re.fullmatch(r"a", "a").span(), (0, 1))
  400. for string in "ab", S("ab"):
  401. self.assertEqual(re.fullmatch(r"a|ab", string).span(), (0, 2))
  402. for string in b"ab", B(b"ab"), bytearray(b"ab"), memoryview(b"ab"):
  403. self.assertEqual(re.fullmatch(br"a|ab", string).span(), (0, 2))
  404. for a, b in "\xe0\xdf", "\u0430\u0431", "\U0001d49c\U0001d49e":
  405. r = r"%s|%s" % (a, a + b)
  406. self.assertEqual(re.fullmatch(r, a + b).span(), (0, 2))
  407. self.assertEqual(re.fullmatch(r".*?$", "abc").span(), (0, 3))
  408. self.assertEqual(re.fullmatch(r".*?", "abc").span(), (0, 3))
  409. self.assertEqual(re.fullmatch(r"a.*?b", "ab").span(), (0, 2))
  410. self.assertEqual(re.fullmatch(r"a.*?b", "abb").span(), (0, 3))
  411. self.assertEqual(re.fullmatch(r"a.*?b", "axxb").span(), (0, 4))
  412. self.assertIsNone(re.fullmatch(r"a+", "ab"))
  413. self.assertIsNone(re.fullmatch(r"abc$", "abc\n"))
  414. self.assertIsNone(re.fullmatch(r"abc\Z", "abc\n"))
  415. self.assertIsNone(re.fullmatch(r"(?m)abc$", "abc\n"))
  416. self.assertEqual(re.fullmatch(r"ab(?=c)cd", "abcd").span(), (0, 4))
  417. self.assertEqual(re.fullmatch(r"ab(?<=b)cd", "abcd").span(), (0, 4))
  418. self.assertEqual(re.fullmatch(r"(?=a|ab)ab", "ab").span(), (0, 2))
  419. self.assertEqual(
  420. re.compile(r"bc").fullmatch("abcd", pos=1, endpos=3).span(), (1, 3))
  421. self.assertEqual(
  422. re.compile(r".*?$").fullmatch("abcd", pos=1, endpos=3).span(), (1, 3))
  423. self.assertEqual(
  424. re.compile(r".*?").fullmatch("abcd", pos=1, endpos=3).span(), (1, 3))
  425. def test_re_groupref_exists(self):
  426. self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', '(a)').groups(),
  427. ('(', 'a'))
  428. self.assertEqual(re.match('^(\()?([^()]+)(?(1)\))$', 'a').groups(),
  429. (None, 'a'))
  430. self.assertIsNone(re.match('^(\()?([^()]+)(?(1)\))$', 'a)'))
  431. self.assertIsNone(re.match('^(\()?([^()]+)(?(1)\))$', '(a'))
  432. self.assertEqual(re.match('^(?:(a)|c)((?(1)b|d))$', 'ab').groups(),
  433. ('a', 'b'))
  434. self.assertEqual(re.match('^(?:(a)|c)((?(1)b|d))$', 'cd').groups(),
  435. (None, 'd'))
  436. self.assertEqual(re.match('^(?:(a)|c)((?(1)|d))$', 'cd').groups(),
  437. (None, 'd'))
  438. self.assertEqual(re.match('^(?:(a)|c)((?(1)|d))$', 'a').groups(),
  439. ('a', ''))
  440. # Tests for bug #1177831: exercise groups other than the first group
  441. p = re.compile('(?P<g1>a)(?P<g2>b)?((?(g2)c|d))')
  442. self.assertEqual(p.match('abc').groups(),
  443. ('a', 'b', 'c'))
  444. self.assertEqual(p.match('ad').groups(),
  445. ('a', None, 'd'))
  446. self.assertIsNone(p.match('abd'))
  447. self.assertIsNone(p.match('ac'))
  448. # Support > 100 groups.
  449. pat = '|'.join('x(?P<a%d>%x)y' % (i, i) for i in range(1, 200 + 1))
  450. pat = '(?:%s)(?(200)z)' % pat
  451. self.assertEqual(re.match(pat, 'xc8yz').span(), (0, 5))
  452. self.checkPatternError(r'(?P<a>)(?(0))', 'bad group number', 10)
  453. self.checkPatternError(r'()(?(1)a|b',
  454. 'missing ), unterminated subpattern', 2)
  455. self.checkPatternError(r'()(?(1)a|b|c)',
  456. 'conditional backref with more than '
  457. 'two branches', 10)
  458. def test_re_groupref_overflow(self):
  459. self.checkTemplateError('()', '\g<%s>' % sre_constants.MAXGROUPS, 'xx',
  460. 'invalid group reference', 3)
  461. self.checkPatternError(r'(?P<a>)(?(%d))' % sre_constants.MAXGROUPS,
  462. 'invalid group reference', 10)
  463. def test_re_groupref(self):
  464. self.assertEqual(re.match(r'^(\|)?([^()]+)\1$', '|a|').groups(),
  465. ('|', 'a'))
  466. self.assertEqual(re.match(r'^(\|)?([^()]+)\1?$', 'a').groups(),
  467. (None, 'a'))
  468. self.assertIsNone(re.match(r'^(\|)?([^()]+)\1$', 'a|'))
  469. self.assertIsNone(re.match(r'^(\|)?([^()]+)\1$', '|a'))
  470. self.assertEqual(re.match(r'^(?:(a)|c)(\1)$', 'aa').groups(),
  471. ('a', 'a'))
  472. self.assertEqual(re.match(r'^(?:(a)|c)(\1)?$', 'c').groups(),
  473. (None, None))
  474. self.checkPatternError(r'(abc\1)', 'cannot refer to an open group', 4)
  475. def test_groupdict(self):
  476. self.assertEqual(re.match('(?P<first>first) (?P<second>second)',
  477. 'first second').groupdict(),
  478. {'first':'first', 'second':'second'})
  479. def test_expand(self):
  480. self.assertEqual(re.match("(?P<first>first) (?P<second>second)",
  481. "first second")
  482. .expand(r"\2 \1 \g<second> \g<first>"),
  483. "second first second first")
  484. self.assertEqual(re.match("(?P<first>first)|(?P<second>second)",
  485. "first")
  486. .expand(r"\2 \g<second>"),
  487. " ")
  488. def test_repeat_minmax(self):
  489. self.assertIsNone(re.match("^(\w){1}$", "abc"))
  490. self.assertIsNone(re.match("^(\w){1}?$", "abc"))
  491. self.assertIsNone(re.match("^(\w){1,2}$", "abc"))
  492. self.assertIsNone(re.match("^(\w){1,2}?$", "abc"))
  493. self.assertEqual(re.match("^(\w){3}$", "abc").group(1), "c")
  494. self.assertEqual(re.match("^(\w){1,3}$", "abc").group(1), "c")
  495. self.assertEqual(re.match("^(\w){1,4}$", "abc").group(1), "c")
  496. self.assertEqual(re.match("^(\w){3,4}?$", "abc").group(1), "c")
  497. self.assertEqual(re.match("^(\w){3}?$", "abc").group(1), "c")
  498. self.assertEqual(re.match("^(\w){1,3}?$", "abc").group(1), "c")
  499. self.assertEqual(re.match("^(\w){1,4}?$", "abc").group(1), "c")
  500. self.assertEqual(re.match("^(\w){3,4}?$", "abc").group(1), "c")
  501. self.assertIsNone(re.match("^x{1}$", "xxx"))
  502. self.assertIsNone(re.match("^x{1}?$", "xxx"))
  503. self.assertIsNone(re.match("^x{1,2}$", "xxx"))
  504. self.assertIsNone(re.match("^x{1,2}?$", "xxx"))
  505. self.assertTrue(re.match("^x{3}$", "xxx"))
  506. self.assertTrue(re.match("^x{1,3}$", "xxx"))
  507. self.assertTrue(re.match("^x{3,3}$", "xxx"))
  508. self.assertTrue(re.match("^x{1,4}$", "xxx"))
  509. self.assertTrue(re.match("^x{3,4}?$", "xxx"))
  510. self.assertTrue(re.match("^x{3}?$", "xxx"))
  511. self.assertTrue(re.match("^x{1,3}?$", "xxx"))
  512. self.assertTrue(re.match("^x{1,4}?$", "xxx"))
  513. self.assertTrue(re.match("^x{3,4}?$", "xxx"))
  514. self.assertIsNone(re.match("^x{}$", "xxx"))
  515. self.assertTrue(re.match("^x{}$", "x{}"))
  516. self.checkPatternError(r'x{2,1}',
  517. 'min repeat greater than max repeat', 2)
  518. def test_getattr(self):
  519. self.assertEqual(re.compile("(?i)(a)(b)").pattern, "(?i)(a)(b)")
  520. self.assertEqual(re.compile("(?i)(a)(b)").flags, re.I | re.U)
  521. self.assertEqual(re.compile("(?i)(a)(b)").groups, 2)
  522. self.assertEqual(re.compile("(?i)(a)(b)").groupindex, {})
  523. self.assertEqual(re.compile("(?i)(?P<first>a)(?P<other>b)").groupindex,
  524. {'first': 1, 'other': 2})
  525. self.assertEqual(re.match("(a)", "a").pos, 0)
  526. self.assertEqual(re.match("(a)", "a").endpos, 1)
  527. self.assertEqual(re.match("(a)", "a").string, "a")
  528. self.assertEqual(re.match("(a)", "a").regs, ((0, 1), (0, 1)))
  529. self.assertTrue(re.match("(a)", "a").re)
  530. # Issue 14260. groupindex should be non-modifiable mapping.
  531. p = re.compile(r'(?i)(?P<first>a)(?P<other>b)')
  532. self.assertEqual(sorted(p.groupindex), ['first', 'other'])
  533. self.assertEqual(p.groupindex['other'], 2)
  534. with self.assertRaises(TypeError):
  535. p.groupindex['other'] = 0
  536. self.assertEqual(p.groupindex['other'], 2)
  537. def test_special_escapes(self):
  538. self.assertEqual(re.search(r"\b(b.)\b",
  539. "abcd abc bcd bx").group(1), "bx")
  540. self.assertEqual(re.search(r"\B(b.)\B",
  541. "abc bcd bc abxd").group(1), "bx")
  542. self.assertEqual(re.search(r"\b(b.)\b",
  543. "abcd abc bcd bx", re.ASCII).group(1), "bx")
  544. self.assertEqual(re.search(r"\B(b.)\B",
  545. "abc bcd bc abxd", re.ASCII).group(1), "bx")
  546. self.assertEqual(re.search(r"^abc$", "\nabc\n", re.M).group(0), "abc")
  547. self.assertEqual(re.search(r"^\Aabc\Z$", "abc", re.M).group(0), "abc")
  548. self.assertIsNone(re.search(r"^\Aabc\Z$", "\nabc\n", re.M))
  549. self.assertEqual(re.search(br"\b(b.)\b",
  550. b"abcd abc bcd bx").group(1), b"bx")
  551. self.assertEqual(re.search(br"\B(b.)\B",
  552. b"abc bcd bc abxd").group(1), b"bx")
  553. self.assertEqual(re.search(br"\b(b.)\b",
  554. b"abcd abc bcd bx", re.LOCALE).group(1), b"bx")
  555. self.assertEqual(re.search(br"\B(b.)\B",
  556. b"abc bcd bc abxd", re.LOCALE).group(1), b"bx")
  557. self.assertEqual(re.search(br"^abc$", b"\nabc\n", re.M).group(0), b"abc")
  558. self.assertEqual(re.search(br"^\Aabc\Z$", b"abc", re.M).group(0), b"abc")
  559. self.assertIsNone(re.search(br"^\Aabc\Z$", b"\nabc\n", re.M))
  560. self.assertEqual(re.search(r"\d\D\w\W\s\S",
  561. "1aa! a").group(0), "1aa! a")
  562. self.assertEqual(re.search(br"\d\D\w\W\s\S",
  563. b"1aa! a").group(0), b"1aa! a")
  564. self.assertEqual(re.search(r"\d\D\w\W\s\S",
  565. "1aa! a", re.ASCII).group(0), "1aa! a")
  566. self.assertEqual(re.search(br"\d\D\w\W\s\S",
  567. b"1aa! a", re.LOCALE).group(0), b"1aa! a")
  568. def test_other_escapes(self):
  569. self.checkPatternError("\\", 'bad escape (end of pattern)', 0)
  570. self.assertEqual(re.match(r"\(", '(').group(), '(')
  571. self.assertIsNone(re.match(r"\(", ')'))
  572. self.assertEqual(re.match(r"\\", '\\').group(), '\\')
  573. self.assertEqual(re.match(r"[\]]", ']').group(), ']')
  574. self.assertIsNone(re.match(r"[\]]", '['))
  575. self.assertEqual(re.match(r"[a\-c]", '-').group(), '-')
  576. self.assertIsNone(re.match(r"[a\-c]", 'b'))
  577. self.assertEqual(re.match(r"[\^a]+", 'a^').group(), 'a^')
  578. self.assertIsNone(re.match(r"[\^a]+", 'b'))
  579. re.purge() # for warnings
  580. for c in 'ceghijklmopqyzCEFGHIJKLMNOPQRTVXY':
  581. with self.subTest(c):
  582. self.assertRaises(re.error, re.compile, '\\%c' % c)
  583. for c in 'ceghijklmopqyzABCEFGHIJKLMNOPQRTVXYZ':
  584. with self.subTest(c):
  585. self.assertRaises(re.error, re.compile, '[\\%c]' % c)
  586. def test_string_boundaries(self):
  587. # See http://bugs.python.org/issue10713
  588. self.assertEqual(re.search(r"\b(abc)\b", "abc").group(1),
  589. "abc")
  590. # There's a word boundary at the start of a string.
  591. self.assertTrue(re.match(r"\b", "abc"))
  592. # A non-empty string includes a non-boundary zero-length match.
  593. self.assertTrue(re.search(r"\B", "abc"))
  594. # There is no non-boundary match at the start of a string.
  595. self.assertFalse(re.match(r"\B", "abc"))
  596. # However, an empty string contains no word boundaries, and also no
  597. # non-boundaries.
  598. self.assertIsNone(re.search(r"\B", ""))
  599. # This one is questionable and different from the perlre behaviour,
  600. # but describes current behavior.
  601. self.assertIsNone(re.search(r"\b", ""))
  602. # A single word-character string has two boundaries, but no
  603. # non-boundary gaps.
  604. self.assertEqual(len(re.findall(r"\b", "a")), 2)
  605. self.assertEqual(len(re.findall(r"\B", "a")), 0)
  606. # If there are no words, there are no boundaries
  607. self.assertEqual(len(re.findall(r"\b", " ")), 0)
  608. self.assertEqual(len(re.findall(r"\b", " ")), 0)
  609. # Can match around the whitespace.
  610. self.assertEqual(len(re.findall(r"\B", " ")), 2)
  611. def test_bigcharset(self):
  612. self.assertEqual(re.match("([\u2222\u2223])",
  613. "\u2222").group(1), "\u2222")
  614. r = '[%s]' % ''.join(map(chr, range(256, 2**16, 255)))
  615. self.assertEqual(re.match(r, "\uff01").group(), "\uff01")
  616. def test_big_codesize(self):
  617. # Issue #1160
  618. r = re.compile('|'.join(('%d'%x for x in range(10000))))
  619. self.assertTrue(r.match('1000'))
  620. self.assertTrue(r.match('9999'))
  621. def test_anyall(self):
  622. self.assertEqual(re.match("a.b", "a\nb", re.DOTALL).group(0),
  623. "a\nb")
  624. self.assertEqual(re.match("a.*b", "a\n\nb", re.DOTALL).group(0),
  625. "a\n\nb")
  626. def test_lookahead(self):
  627. self.assertEqual(re.match("(a(?=\s[^a]))", "a b").group(1), "a")
  628. self.assertEqual(re.match("(a(?=\s[^a]*))", "a b").group(1), "a")
  629. self.assertEqual(re.match("(a(?=\s[abc]))", "a b").group(1), "a")
  630. self.assertEqual(re.match("(a(?=\s[abc]*))", "a bc").group(1), "a")
  631. self.assertEqual(re.match(r"(a)(?=\s\1)", "a a").group(1), "a")
  632. self.assertEqual(re.match(r"(a)(?=\s\1*)", "a aa").group(1), "a")
  633. self.assertEqual(re.match(r"(a)(?=\s(abc|a))", "a a").group(1), "a")
  634. self.assertEqual(re.match(r"(a(?!\s[^a]))", "a a").group(1), "a")
  635. self.assertEqual(re.match(r"(a(?!\s[abc]))", "a d").group(1), "a")
  636. self.assertEqual(re.match(r"(a)(?!\s\1)", "a b").group(1), "a")
  637. self.assertEqual(re.match(r"(a)(?!\s(abc|a))", "a b").group(1), "a")
  638. # Group reference.
  639. self.assertTrue(re.match(r'(a)b(?=\1)a', 'aba'))
  640. self.assertIsNone(re.match(r'(a)b(?=\1)c', 'abac'))
  641. # Conditional group reference.
  642. self.assertTrue(re.match(r'(?:(a)|(x))b(?=(?(2)x|c))c', 'abc'))
  643. self.assertIsNone(re.match(r'(?:(a)|(x))b(?=(?(2)c|x))c', 'abc'))
  644. self.assertTrue(re.match(r'(?:(a)|(x))b(?=(?(2)x|c))c', 'abc'))
  645. self.assertIsNone(re.match(r'(?:(a)|(x))b(?=(?(1)b|x))c', 'abc'))
  646. self.assertTrue(re.match(r'(?:(a)|(x))b(?=(?(1)c|x))c', 'abc'))
  647. # Group used before defined.
  648. self.assertTrue(re.match(r'(a)b(?=(?(2)x|c))(c)', 'abc'))
  649. self.assertIsNone(re.match(r'(a)b(?=(?(2)b|x))(c)', 'abc'))
  650. self.assertTrue(re.match(r'(a)b(?=(?(1)c|x))(c)', 'abc'))
  651. def test_lookbehind(self):
  652. self.assertTrue(re.match(r'ab(?<=b)c', 'abc'))
  653. self.assertIsNone(re.match(r'ab(?<=c)c', 'abc'))
  654. self.assertIsNone(re.match(r'ab(?<!b)c', 'abc'))
  655. self.assertTrue(re.match(r'ab(?<!c)c', 'abc'))
  656. # Group reference.
  657. self.assertTrue(re.match(r'(a)a(?<=\1)c', 'aac'))
  658. self.assertIsNone(re.match(r'(a)b(?<=\1)a', 'abaa'))
  659. self.assertIsNone(re.match(r'(a)a(?<!\1)c', 'aac'))
  660. self.assertTrue(re.match(r'(a)b(?<!\1)a', 'abaa'))
  661. # Conditional group reference.
  662. self.assertIsNone(re.match(r'(?:(a)|(x))b(?<=(?(2)x|c))c', 'abc'))
  663. self.assertIsNone(re.match(r'(?:(a)|(x))b(?<=(?(2)b|x))c', 'abc'))
  664. self.assertTrue(re.match(r'(?:(a)|(x))b(?<=(?(2)x|b))c', 'abc'))
  665. self.assertIsNone(re.match(r'(?:(a)|(x))b(?<=(?(1)c|x))c', 'abc'))
  666. self.assertTrue(re.match(r'(?:(a)|(x))b(?<=(?(1)b|x))c', 'abc'))
  667. # Group used before defined.
  668. self.assertRaises(re.error, re.compile, r'(a)b(?<=(?(2)b|x))(c)')
  669. self.assertIsNone(re.match(r'(a)b(?<=(?(1)c|x))(c)', 'abc'))
  670. self.assertTrue(re.match(r'(a)b(?<=(?(1)b|x))(c)', 'abc'))
  671. # Group defined in the same lookbehind pattern
  672. self.assertRaises(re.error, re.compile, r'(a)b(?<=(.)\2)(c)')
  673. self.assertRaises(re.error, re.compile, r'(a)b(?<=(?P<a>.)(?P=a))(c)')
  674. self.assertRaises(re.error, re.compile, r'(a)b(?<=(a)(?(2)b|x))(c)')
  675. self.assertRaises(re.error, re.compile, r'(a)b(?<=(.)(?<=\2))(c)')
  676. def test_ignore_case(self):
  677. self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC")
  678. self.assertEqual(re.match(b"abc", b"ABC", re.I).group(0), b"ABC")
  679. self.assertEqual(re.match(r"(a\s[^a])", "a b", re.I).group(1), "a b")
  680. self.assertEqual(re.match(r"(a\s[^a]*)", "a bb", re.I).group(1), "a bb")
  681. self.assertEqual(re.match(r"(a\s[abc])", "a b", re.I).group(1), "a b")
  682. self.assertEqual(re.match(r"(a\s[abc]*)", "a bb", re.I).group(1), "a bb")
  683. self.assertEqual(re.match(r"((a)\s\2)", "a a", re.I).group(1), "a a")
  684. self.assertEqual(re.match(r"((a)\s\2*)", "a aa", re.I).group(1), "a aa")
  685. self.assertEqual(re.match(r"((a)\s(abc|a))", "a a", re.I).group(1), "a a")
  686. self.assertEqual(re.match(r"((a)\s(abc|a)*)", "a aa", re.I).group(1), "a aa")
  687. assert '\u212a'.lower() == 'k' # 'K'
  688. self.assertTrue(re.match(r'K', '\u212a', re.I))
  689. self.assertTrue(re.match(r'k', '\u212a', re.I))
  690. self.assertTrue(re.match(r'\u212a', 'K', re.I))
  691. self.assertTrue(re.match(r'\u212a', 'k', re.I))
  692. assert '\u017f'.upper() == 'S' # 'Ĺż'
  693. self.assertTrue(re.match(r'S', '\u017f', re.I))
  694. self.assertTrue(re.match(r's', '\u017f', re.I))
  695. self.assertTrue(re.match(r'\u017f', 'S', re.I))
  696. self.assertTrue(re.match(r'\u017f', 's', re.I))
  697. assert '\ufb05'.upper() == '\ufb06'.upper() == 'ST' # 'ſt', 'st'
  698. self.assertTrue(re.match(r'\ufb05', '\ufb06', re.I))
  699. self.assertTrue(re.match(r'\ufb06', '\ufb05', re.I))
  700. def test_ignore_case_set(self):
  701. self.assertTrue(re.match(r'[19A]', 'A', re.I))
  702. self.assertTrue(re.match(r'[19a]', 'a', re.I))
  703. self.assertTrue(re.match(r'[19a]', 'A', re.I))
  704. self.assertTrue(re.match(r'[19A]', 'a', re.I))
  705. self.assertTrue(re.match(br'[19A]', b'A', re.I))
  706. self.assertTrue(re.match(br'[19a]', b'a', re.I))
  707. self.assertTrue(re.match(br'[19a]', b'A', re.I))
  708. self.assertTrue(re.match(br'[19A]', b'a', re.I))
  709. assert '\u212a'.lower() == 'k' # 'K'
  710. self.assertTrue(re.match(r'[19K]', '\u212a', re.I))
  711. self.assertTrue(re.match(r'[19k]', '\u212a', re.I))
  712. self.assertTrue(re.match(r'[19\u212a]', 'K', re.I))
  713. self.assertTrue(re.match(r'[19\u212a]', 'k', re.I))
  714. assert '\u017f'.upper() == 'S' # 'Ĺż'
  715. self.assertTrue(re.match(r'[19S]', '\u017f', re.I))
  716. self.assertTrue(re.match(r'[19s]', '\u017f', re.I))
  717. self.assertTrue(re.match(r'[19\u017f]', 'S', re.I))
  718. self.assertTrue(re.match(r'[19\u017f]', 's', re.I))
  719. assert '\ufb05'.upper() == '\ufb06'.upper() == 'ST' # 'ſt', 'st'
  720. self.assertTrue(re.match(r'[19\ufb05]', '\ufb06', re.I))
  721. self.assertTrue(re.match(r'[19\ufb06]', '\ufb05', re.I))
  722. def test_ignore_case_range(self):
  723. # Issues #3511, #17381.
  724. self.assertTrue(re.match(r'[9-a]', '_', re.I))
  725. self.assertIsNone(re.match(r'[9-A]', '_', re.I))
  726. self.assertTrue(re.match(br'[9-a]', b'_', re.I))
  727. self.assertIsNone(re.match(br'[9-A]', b'_', re.I))
  728. self.assertTrue(re.match(r'[\xc0-\xde]', '\xd7', re.I))
  729. self.assertIsNone(re.match(r'[\xc0-\xde]', '\xf7', re.I))
  730. self.assertTrue(re.match(r'[\xe0-\xfe]', '\xf7', re.I))
  731. self.assertIsNone(re.match(r'[\xe0-\xfe]', '\xd7', re.I))
  732. self.assertTrue(re.match(r'[\u0430-\u045f]', '\u0450', re.I))
  733. self.assertTrue(re.match(r'[\u0430-\u045f]', '\u0400', re.I))
  734. self.assertTrue(re.match(r'[\u0400-\u042f]', '\u0450', re.I))
  735. self.assertTrue(re.match(r'[\u0400-\u042f]', '\u0400', re.I))
  736. self.assertTrue(re.match(r'[\U00010428-\U0001044f]', '\U00010428', re.I))
  737. self.assertTrue(re.match(r'[\U00010428-\U0001044f]', '\U00010400', re.I))
  738. self.assertTrue(re.match(r'[\U00010400-\U00010427]', '\U00010428', re.I))
  739. self.assertTrue(re.match(r'[\U00010400-\U00010427]', '\U00010400', re.I))
  740. assert '\u212a'.lower() == 'k' # 'K'
  741. self.assertTrue(re.match(r'[J-M]', '\u212a', re.I))
  742. self.assertTrue(re.match(r'[j-m]', '\u212a', re.I))
  743. self.assertTrue(re.match(r'[\u2129-\u212b]', 'K', re.I))
  744. self.assertTrue(re.match(r'[\u2129-\u212b]', 'k', re.I))
  745. assert '\u017f'.upper() == 'S' # 'Ĺż'
  746. self.assertTrue(re.match(r'[R-T]', '\u017f', re.I))
  747. self.assertTrue(re.match(r'[r-t]', '\u017f', re.I))
  748. self.assertTrue(re.match(r'[\u017e-\u0180]', 'S', re.I))
  749. self.assertTrue(re.match(r'[\u017e-\u0180]', 's', re.I))
  750. assert '\ufb05'.upper() == '\ufb06'.upper() == 'ST' # 'ſt', 'st'
  751. self.assertTrue(re.match(r'[\ufb04-\ufb05]', '\ufb06', re.I))
  752. self.assertTrue(re.match(r'[\ufb06-\ufb07]', '\ufb05', re.I))
  753. def test_category(self):
  754. self.assertEqual(re.match(r"(\s)", " ").group(1), " ")
  755. def test_getlower(self):
  756. import _sre
  757. self.assertEqual(_sre.getlower(ord('A'), 0), ord('a'))
  758. self.assertEqual(_sre.getlower(ord('A'), re.LOCALE), ord('a'))
  759. self.assertEqual(_sre.getlower(ord('A'), re.UNICODE), ord('a'))
  760. self.assertEqual(_sre.getlower(ord('A'), re.ASCII), ord('a'))
  761. self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC")
  762. self.assertEqual(re.match(b"abc", b"ABC", re.I).group(0), b"ABC")
  763. self.assertEqual(re.match("abc", "ABC", re.I|re.A).group(0), "ABC")
  764. self.assertEqual(re.match(b"abc", b"ABC", re.I|re.L).group(0), b"ABC")
  765. def test_not_literal(self):
  766. self.assertEqual(re.search("\s([^a])", " b").group(1), "b")
  767. self.assertEqual(re.search("\s([^a]*)", " bb").group(1), "bb")
  768. def test_search_coverage(self):
  769. self.assertEqual(re.search("\s(b)", " b").group(1), "b")
  770. self.assertEqual(re.search("a\s", "a ").group(0), "a ")
  771. def assertMatch(self, pattern, text, match=None, span=None,
  772. matcher=re.match):
  773. if match is None and span is None:
  774. # the pattern matches the whole text
  775. match = text
  776. span = (0, len(text))
  777. elif match is None or span is None:
  778. raise ValueError('If match is not None, span should be specified '
  779. '(and vice versa).')
  780. m = matcher(pattern, text)
  781. self.assertTrue(m)
  782. self.assertEqual(m.group(), match)
  783. self.assertEqual(m.span(), span)
  784. def test_re_escape(self):
  785. alnum_chars = string.ascii_letters + string.digits + '_'
  786. p = ''.join(chr(i) for i in range(256))
  787. for c in p:
  788. if c in alnum_chars:
  789. self.assertEqual(re.escape(c), c)
  790. elif c == '\x00':
  791. self.assertEqual(re.escape(c), '\\000')
  792. else:
  793. self.assertEqual(re.escape(c), '\\' + c)
  794. self.assertMatch(re.escape(c), c)
  795. self.assertMatch(re.escape(p), p)
  796. def test_re_escape_byte(self):
  797. alnum_chars = (string.ascii_letters + string.digits + '_').encode('ascii')
  798. p = bytes(range(256))
  799. for i in p:
  800. b = bytes([i])
  801. if b in alnum_chars:
  802. self.assertEqual(re.escape(b), b)
  803. elif i == 0:
  804. self.assertEqual(re.escape(b), b'\\000')
  805. else:
  806. self.assertEqual(re.escape(b), b'\\' + b)
  807. self.assertMatch(re.escape(b), b)
  808. self.assertMatch(re.escape(p), p)
  809. def test_re_escape_non_ascii(self):
  810. s = 'xxx\u2620\u2620\u2620xxx'
  811. s_escaped = re.escape(s)
  812. self.assertEqual(s_escaped, 'xxx\\\u2620\\\u2620\\\u2620xxx')
  813. self.assertMatch(s_escaped, s)
  814. self.assertMatch('.%s+.' % re.escape('\u2620'), s,
  815. 'x\u2620\u2620\u2620x', (2, 7), re.search)
  816. def test_re_escape_non_ascii_bytes(self):
  817. b = 'y\u2620y\u2620y'.encode('utf-8')
  818. b_escaped = re.escape(b)
  819. self.assertEqual(b_escaped, b'y\\\xe2\\\x98\\\xa0y\\\xe2\\\x98\\\xa0y')
  820. self.assertMatch(b_escaped, b)
  821. res = re.findall(re.escape('\u2620'.encode('utf-8')), b)
  822. self.assertEqual(len(res), 2)
  823. def test_pickling(self):
  824. import pickle
  825. oldpat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)', re.UNICODE)
  826. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  827. pickled = pickle.dumps(oldpat, proto)
  828. newpat = pickle.loads(pickled)
  829. self.assertEqual(newpat, oldpat)
  830. # current pickle expects the _compile() reconstructor in re module
  831. from re import _compile
  832. def test_constants(self):
  833. self.assertEqual(re.I, re.IGNORECASE)
  834. self.assertEqual(re.L, re.LOCALE)
  835. self.assertEqual(re.M, re.MULTILINE)
  836. self.assertEqual(re.S, re.DOTALL)
  837. self.assertEqual(re.X, re.VERBOSE)
  838. def test_flags(self):
  839. for flag in [re.I, re.M, re.X, re.S, re.A, re.U]:
  840. self.assertTrue(re.compile('^pattern$', flag))
  841. for flag in [re.I, re.M, re.X, re.S, re.A, re.L]:
  842. self.assertTrue(re.compile(b'^pattern$', flag))
  843. def test_sre_character_literals(self):
  844. for i in [0, 8, 16, 32, 64, 127, 128, 255, 256, 0xFFFF, 0x10000, 0x10FFFF]:
  845. if i < 256:
  846. self.assertTrue(re.match(r"\%03o" % i, chr(i)))
  847. self.assertTrue(re.match(r"\%03o0" % i, chr(i)+"0"))
  848. self.assertTrue(re.match(r"\%03o8" % i, chr(i)+"8"))
  849. self.assertTrue(re.match(r"\x%02x" % i, chr(i)))
  850. self.assertTrue(re.match(r"\x%02x0" % i, chr(i)+"0"))
  851. self.assertTrue(re.match(r"\x%02xz" % i, chr(i)+"z"))
  852. if i < 0x10000:
  853. self.assertTrue(re.match(r"\u%04x" % i, chr(i)))
  854. self.assertTrue(re.match(r"\u%04x0" % i, chr(i)+"0"))
  855. self.assertTrue(re.match(r"\u%04xz" % i, chr(i)+"z"))
  856. self.assertTrue(re.match(r"\U%08x" % i, chr(i)))
  857. self.assertTrue(re.match(r"\U%08x0" % i, chr(i)+"0"))
  858. self.assertTrue(re.match(r"\U%08xz" % i, chr(i)+"z"))
  859. self.assertTrue(re.match(r"\0", "\000"))
  860. self.assertTrue(re.match(r"\08", "\0008"))
  861. self.assertTrue(re.match(r"\01", "\001"))
  862. self.assertTrue(re.match(r"\018", "\0018"))
  863. self.checkPatternError(r"\567",
  864. r'octal escape value \567 outside of '
  865. r'range 0-0o377', 0)
  866. self.checkPatternError(r"\911", 'invalid group reference', 0)
  867. self.checkPatternError(r"\x1", r'incomplete escape \x1', 0)
  868. self.checkPatternError(r"\x1z", r'incomplete escape \x1', 0)
  869. self.checkPatternError(r"\u123", r'incomplete escape \u123', 0)
  870. self.checkPatternError(r"\u123z", r'incomplete escape \u123', 0)
  871. self.checkPatternError(r"\U0001234", r'incomplete escape \U0001234', 0)
  872. self.checkPatternError(r"\U0001234z", r'incomplete escape \U0001234', 0)
  873. self.checkPatternError(r"\U00110000", r'bad escape \U00110000', 0)
  874. def test_sre_character_class_literals(self):
  875. for i in [0, 8, 16, 32, 64, 127, 128, 255, 256, 0xFFFF, 0x10000, 0x10FFFF]:
  876. if i < 256:
  877. self.assertTrue(re.match(r"[\%o]" % i, chr(i)))
  878. self.assertTrue(re.match(r"[\%o8]" % i, chr(i)))
  879. self.assertTrue(re.match(r"[\%03o]" % i, chr(i)))
  880. self.assertTrue(re.match(r"[\%03o0]" % i, chr(i)))
  881. self.assertTrue(re.match(r"[\%03o8]" % i, chr(i)))
  882. self.assertTrue(re.match(r"[\x%02x]" % i, chr(i)))
  883. self.assertTrue(re.match(r"[\x%02x0]" % i, chr(i)))
  884. self.assertTrue(re.match(r"[\x%02xz]" % i, chr(i)))
  885. if i < 0x10000:
  886. self.assertTrue(re.match(r"[\u%04x]" % i, chr(i)))
  887. self.assertTrue(re.match(r"[\u%04x0]" % i, chr(i)))
  888. self.assertTrue(re.match(r"[\u%04xz]" % i, chr(i)))
  889. self.assertTrue(re.match(r"[\U%08x]" % i, chr(i)))
  890. self.assertTrue(re.match(r"[\U%08x0]" % i, chr(i)+"0"))
  891. self.assertTrue(re.match(r"[\U%08xz]" % i, chr(i)+"z"))
  892. self.checkPatternError(r"[\567]",
  893. r'octal escape value \567 outside of '
  894. r'range 0-0o377', 1)
  895. self.checkPatternError(r"[\911]", r'bad escape \9', 1)
  896. self.checkPatternError(r"[\x1z]", r'incomplete escape \x1', 1)
  897. self.checkPatternError(r"[\u123z]", r'incomplete escape \u123', 1)
  898. self.checkPatternError(r"[\U0001234z]", r'incomplete escape \U0001234', 1)
  899. self.checkPatternError(r"[\U00110000]", r'bad escape \U00110000', 1)
  900. self.assertTrue(re.match(r"[\U0001d49c-\U0001d4b5]", "\U0001d49e"))
  901. def test_sre_byte_literals(self):
  902. for i in [0, 8, 16, 32, 64, 127, 128, 255]:
  903. self.assertTrue(re.match((r"\%03o" % i).encode(), bytes([i])))
  904. self.assertTrue(re.match((r"\%03o0" % i).encode(), bytes([i])+b"0"))
  905. self.assertTrue(re.match((r"\%03o8" % i).encode(), bytes([i])+b"8"))
  906. self.assertTrue(re.match((r"\x%02x" % i).encode(), bytes([i])))
  907. self.assertTrue(re.match((r"\x%02x0" % i).encode(), bytes([i])+b"0"))
  908. self.assertTrue(re.match((r"\x%02xz" % i).encode(), bytes([i])+b"z"))
  909. self.assertRaises(re.error, re.compile, br"\u1234")
  910. self.assertRaises(re.error, re.compile, br"\U00012345")
  911. self.assertTrue(re.match(br"\0", b"\000"))
  912. self.assertTrue(re.match(br"\08", b"\0008"))
  913. self.assertTrue(re.match(br"\01", b"\001"))
  914. self.assertTrue(re.match(br"\018", b"\0018"))
  915. self.checkPatternError(br"\567",
  916. r'octal escape value \567 outside of '
  917. r'range 0-0o377', 0)
  918. self.checkPatternError(br"\911", 'invalid group reference', 0)
  919. self.checkPatternError(br"\x1", r'incomplete escape \x1', 0)
  920. self.checkPatternError(br"\x1z", r'incomplete escape \x1', 0)
  921. def test_sre_byte_class_literals(self):
  922. for i in [0, 8, 16, 32, 64, 127, 128, 255]:
  923. self.assertTrue(re.match((r"[\%o]" % i).encode(), bytes([i])))
  924. self.assertTrue(re.match((r"[\%o8]" % i).encode(), bytes([i])))
  925. self.assertTrue(re.match((r"[\%03o]" % i).encode(), bytes([i])))
  926. self.assertTrue(re.match((r"[\%03o0]" % i).encode(), bytes([i])))
  927. self.assertTrue(re.match((r"[\%03o8]" % i).encode(), bytes([i])))
  928. self.assertTrue(re.match((r"[\x%02x]" % i).encode(), bytes([i])))
  929. self.assertTrue(re.match((r"[\x%02x0]" % i).encode(), bytes([i])))
  930. self.assertTrue(re.match((r"[\x%02xz]" % i).encode(), bytes([i])))
  931. self.assertRaises(re.error, re.compile, br"[\u1234]")
  932. self.assertRaises(re.error, re.compile, br"[\U00012345]")
  933. self.checkPatternError(br"[\567]",
  934. r'octal escape value \567 outside of '
  935. r'range 0-0o377', 1)
  936. self.checkPatternError(br"[\911]", r'bad escape \9', 1)
  937. self.checkPatternError(br"[\x1z]", r'incomplete escape \x1', 1)
  938. def test_character_set_errors(self):
  939. self.checkPatternError(r'[', 'unterminated character set', 0)
  940. self.checkPatternError(r'[^', 'unterminated character set', 0)
  941. self.checkPatternError(r'[a', 'unterminated character set', 0)
  942. # bug 545855 -- This pattern failed to cause a compile error as it
  943. # should, instead provoking a TypeError.
  944. self.checkPatternError(r"[a-", 'unterminated character set', 0)
  945. self.checkPatternError(r"[\w-b]", r'bad character range \w-b', 1)
  946. self.checkPatternError(r"[a-\w]", r'bad character range a-\w', 1)
  947. self.checkPatternError(r"[b-a]", 'bad character range b-a', 1)
  948. def test_bug_113254(self):
  949. self.assertEqual(re.match(r'(a)|(b)', 'b').start(1), -1)
  950. self.assertEqual(re.match(r'(a)|(b)', 'b').end(1), -1)
  951. self.assertEqual(re.match(r'(a)|(b)', 'b').span(1), (-1, -1))
  952. def test_bug_527371(self):
  953. # bug described in patches 527371/672491
  954. self.assertIsNone(re.match(r'(a)?a','a').lastindex)
  955. self.assertEqual(re.match(r'(a)(b)?b','ab').lastindex, 1)
  956. self.assertEqual(re.match(r'(?P<a>a)(?P<b>b)?b','ab').lastgroup, 'a')
  957. self.assertEqual(re.match("(?P<a>a(b))", "ab").lastgroup, 'a')
  958. self.assertEqual(re.match("((a))", "a").lastindex, 1)
  959. def test_bug_418626(self):
  960. # bugs 418626 at al. -- Testing Greg Chapman's addition of op code
  961. # SRE_OP_MIN_REPEAT_ONE for eliminating recursion on simple uses of
  962. # pattern '*?' on a long string.
  963. self.assertEqual(re.match('.*?c', 10000*'ab'+'cd').end(0), 20001)
  964. self.assertEqual(re.match('.*?cd', 5000*'ab'+'c'+5000*'ab'+'cde').end(0),
  965. 20003)
  966. self.assertEqual(re.match('.*?cd', 20000*'abc'+'de').end(0), 60001)
  967. # non-simple '*?' still used to hit the recursion limit, before the
  968. # non-recursive scheme was implemented.
  969. self.assertEqual(re.search('(a|b)*?c', 10000*'ab'+'cd').end(0), 20001)
  970. def test_bug_612074(self):
  971. pat="["+re.escape("\u2039")+"]"
  972. self.assertEqual(re.compile(pat) and 1, 1)
  973. def test_stack_overflow(self):
  974. # nasty cases that used to overflow the straightforward recursive
  975. # implementation of repeated groups.
  976. self.assertEqual(re.match('(x)*', 50000*'x').group(1), 'x')
  977. self.assertEqual(re.match('(x)*y', 50000*'x'+'y').group(1), 'x')
  978. self.assertEqual(re.match('(x)*?y', 50000*'x'+'y').group(1), 'x')
  979. def test_nothing_to_repeat(self):
  980. for reps in '*', '+', '?', '{1,2}':
  981. for mod in '', '?':
  982. self.checkPatternError('%s%s' % (reps, mod),
  983. 'nothing to repeat', 0)
  984. self.checkPatternError('(?:%s%s)' % (reps, mod),
  985. 'nothing to repeat', 3)
  986. def test_multiple_repeat(self):
  987. for outer_reps in '*', '+', '{1,2}':
  988. for outer_mod in '', '?':
  989. outer_op = outer_reps + outer_mod
  990. for inner_reps in '*', '+', '?', '{1,2}':
  991. for inner_mod in '', '?':
  992. inner_op = inner_reps + inner_mod
  993. self.checkPatternError(r'x%s%s' % (inner_op, outer_op),
  994. 'multiple repeat', 1 + len(inner_op))
  995. def test_unlimited_zero_width_repeat(self):
  996. # Issue #9669
  997. self.assertIsNone(re.match(r'(?:a?)*y', 'z'))
  998. self.assertIsNone(re.match(r'(?:a?)+y', 'z'))
  999. self.assertIsNone(re.match(r'(?:a?){2,}y', 'z'))
  1000. self.assertIsNone(re.match(r'(?:a?)*?y', 'z'))
  1001. self.assertIsNone(re.match(r'(?:a?)+?y', 'z'))
  1002. self.assertIsNone(re.match(r'(?:a?){2,}?y', 'z'))
  1003. def test_scanner(self):
  1004. def s_ident(scanner, token): return token
  1005. def s_operator(scanner, token): return "op%s" % token
  1006. def s_float(scanner, token): return float(token)
  1007. def s_int(scanner, token): return int(token)
  1008. scanner = Scanner([
  1009. (r"[a-zA-Z_]\w*", s_ident),
  1010. (r"\d+\.\d*", s_float),
  1011. (r"\d+", s_int),
  1012. (r"=|\+|-|\*|/", s_operator),
  1013. (r"\s+", None),
  1014. ])
  1015. self.assertTrue(scanner.scanner.scanner("").pattern)
  1016. self.assertEqual(scanner.scan("sum = 3*foo + 312.50 + bar"),
  1017. (['sum', 'op=', 3, 'op*', 'foo', 'op+', 312.5,
  1018. 'op+', 'bar'], ''))
  1019. def test_bug_448951(self):
  1020. # bug 448951 (similar to 429357, but with single char match)
  1021. # (Also test greedy matches.)
  1022. for op in '','?','*':
  1023. self.assertEqual(re.match(r'((.%s):)?z'%op, 'z').groups(),
  1024. (None, None))
  1025. self.assertEqual(re.match(r'((.%s):)?z'%op, 'a:z').groups(),
  1026. ('a:', 'a'))
  1027. def test_bug_725106(self):
  1028. # capturing groups in alternatives in repeats
  1029. self.assertEqual(re.match('^((a)|b)*', 'abc').groups(),
  1030. ('b', 'a'))
  1031. self.assertEqual(re.match('^(([ab])|c)*', 'abc').groups(),
  1032. ('c', 'b'))
  1033. self.assertEqual(re.match('^((d)|[ab])*', 'abc').groups(),
  1034. ('b', None))
  1035. self.assertEqual(re.match('^((a)c|[ab])*', 'abc').groups(),
  1036. ('b', None))
  1037. self.assertEqual(re.match('^((a)|b)*?c', 'abc').groups(),
  1038. ('b', 'a'))
  1039. self.assertEqual(re.match('^(([ab])|c)*?d', 'abcd').groups(),
  1040. ('c', 'b'))
  1041. self.assertEqual(re.match('^((d)|[ab])*?c', 'abc').groups(),
  1042. ('b', None))
  1043. self.assertEqual(re.match('^((a)c|[ab])*?c', 'abc').groups(),
  1044. ('b', None))
  1045. def test_bug_725149(self):
  1046. # mark_stack_base restoring before restoring marks
  1047. self.assertEqual(re.match('(a)(?:(?=(b)*)c)*', 'abb').groups(),
  1048. ('a', None))
  1049. self.assertEqual(re.match('(a)((?!(b)*))*', 'abb').groups(),
  1050. ('a', None, None))
  1051. def test_bug_764548(self):
  1052. # bug 764548, re.compile() barfs on str/unicode subclasses
  1053. class my_unicode(str): pass
  1054. pat = re.compile(my_unicode("abc"))
  1055. self.assertIsNone(pat.match("xyz"))
  1056. def test_finditer(self):
  1057. iter = re.finditer(r":+", "a:b::c:::d")
  1058. self.assertEqual([item.group(0) for item in iter],
  1059. [":", "::", ":::"])
  1060. pat = re.compile(r":+")
  1061. iter = pat.finditer("a:b::c:::d", 1, 10)
  1062. self.assertEqual([item.group(0) for item in iter],
  1063. [":", "::", ":::"])
  1064. pat = re.compile(r":+")
  1065. iter = pat.finditer("a:b::c:::d", pos=1, endpos=10)
  1066. self.assertEqual([item.group(0) for item in iter],
  1067. [":", "::", ":::"])
  1068. pat = re.compile(r":+")
  1069. iter = pat.finditer("a:b::c:::d", endpos=10, pos=1)
  1070. self.assertEqual([item.group(0) for item in iter],
  1071. [":", "::", ":::"])
  1072. pat = re.compile(r":+")
  1073. iter = pat.finditer("a:b::c:::d", pos=3, endpos=8)
  1074. self.assertEqual([item.group(0) for item in iter],
  1075. ["::", "::"])
  1076. def test_bug_926075(self):
  1077. self.assertIsNot(re.compile('bug_926075'),
  1078. re.compile(b'bug_926075'))
  1079. def test_bug_931848(self):
  1080. pattern = "[\u002E\u3002\uFF0E\uFF61]"
  1081. self.assertEqual(re.compile(pattern).split("a.b.c"),
  1082. ['a','b','c'])
  1083. def test_bug_581080(self):
  1084. iter = re.finditer(r"\s", "a b")
  1085. self.assertEqual(next(iter).span(), (1,2))
  1086. self.assertRaises(StopIteration, next, iter)
  1087. scanner = re.compile(r"\s").scanner("a b")
  1088. self.assertEqual(scanner.search().span(), (1, 2))
  1089. self.assertIsNone(scanner.search())
  1090. def test_bug_817234(self):
  1091. iter = re.finditer(r".*", "asdf")
  1092. self.assertEqual(next(iter).span(), (0, 4))
  1093. self.assertEqual(next(iter).span(), (4, 4))
  1094. self.assertRaises(StopIteration, next, iter)
  1095. def test_bug_6561(self):
  1096. # '\d' should match characters in Unicode category 'Nd'
  1097. # (Number, Decimal Digit), but not those in 'Nl' (Number,
  1098. # Letter) or 'No' (Number, Other).
  1099. decimal_digits = [
  1100. '\u0037', # '\N{DIGIT SEVEN}', category 'Nd'
  1101. '\u0e58', # '\N{THAI DIGIT SIX}', category 'Nd'
  1102. '\uff10', # '\N{FULLWIDTH DIGIT ZERO}', category 'Nd'
  1103. ]
  1104. for x in decimal_digits:
  1105. self.assertEqual(re.match('^\d$', x).group(0), x)
  1106. not_decimal_digits = [
  1107. '\u2165', # '\N{ROMAN NUMERAL SIX}', category 'Nl'
  1108. '\u3039', # '\N{HANGZHOU NUMERAL TWENTY}', category 'Nl'
  1109. '\u2082', # '\N{SUBSCRIPT TWO}', category 'No'
  1110. '\u32b4', # '\N{CIRCLED NUMBER THIRTY NINE}', category 'No'
  1111. ]
  1112. for x in not_decimal_digits:
  1113. self.assertIsNone(re.match('^\d$', x))
  1114. def test_empty_array(self):
  1115. # SF buf 1647541
  1116. import array
  1117. for typecode in 'bBuhHiIlLfd':
  1118. a = array.array(typecode)
  1119. self.assertIsNone(re.compile(b"bla").match(a))
  1120. self.assertEqual(re.compile(b"").match(a).groups(), ())
  1121. def test_inline_flags(self):
  1122. # Bug #1700
  1123. upper_char = '\u1ea0' # Latin Capital Letter A with Dot Below
  1124. lower_char = '\u1ea1' # Latin Small Letter A with Dot Below
  1125. p = re.compile(upper_char, re.I | re.U)
  1126. q = p.match(lower_char)
  1127. self.assertTrue(q)
  1128. p = re.compile(lower_char, re.I | re.U)
  1129. q = p.match(upper_char)
  1130. self.assertTrue(q)
  1131. p = re.compile('(?i)' + upper_char, re.U)
  1132. q = p.match(lower_char)
  1133. self.assertTrue(q)
  1134. p = re.compile('(?i)' + lower_char, re.U)
  1135. q = p.match(upper_char)
  1136. self.assertTrue(q)
  1137. p = re.compile('(?iu)' + upper_char)
  1138. q = p.match(lower_char)
  1139. self.assertTrue(q)
  1140. p = re.compile('(?iu)' + lower_char)
  1141. q = p.match(upper_char)
  1142. self.assertTrue(q)
  1143. def test_dollar_matches_twice(self):
  1144. "$ matches the end of string, and just before the terminating \n"
  1145. pattern = re.compile('$')
  1146. self.assertEqual(pattern.sub('#', 'a\nb\n'), 'a\nb#\n#')
  1147. self.assertEqual(pattern.sub('#', 'a\nb\nc'), 'a\nb\nc#')
  1148. self.assertEqual(pattern.sub('#', '\n'), '#\n#')
  1149. pattern = re.compile('$', re.MULTILINE)
  1150. self.assertEqual(pattern.sub('#', 'a\nb\n' ), 'a#\nb#\n#' )
  1151. self.assertEqual(pattern.sub('#', 'a\nb\nc'), 'a#\nb#\nc#')
  1152. self.assertEqual(pattern.sub('#', '\n'), '#\n#')
  1153. def test_bytes_str_mixing(self):
  1154. # Mixing str and bytes is disallowed
  1155. pat = re.compile('.')
  1156. bpat = re.compile(b'.')
  1157. self.assertRaises(TypeError, pat.match, b'b')
  1158. self.assertRaises(TypeError, bpat.match, 'b')
  1159. self.assertRaises(TypeError, pat.sub, b'b', 'c')
  1160. self.assertRaises(TypeError, pat.sub, 'b', b'c')
  1161. self.assertRaises(TypeError, pat.sub, b'b', b'c')
  1162. self.assertRaises(TypeError, bpat.sub, b'b', 'c')
  1163. self.assertRaises(TypeError, bpat.sub, 'b', b'c')
  1164. self.assertRaises(TypeError, bpat.sub, 'b', 'c')
  1165. def test_ascii_and_unicode_flag(self):
  1166. # String patterns
  1167. for flags in (0, re.UNICODE):
  1168. pat = re.compile('\xc0', flags | re.IGNORECASE)
  1169. self.assertTrue(pat.match('\xe0'))
  1170. pat = re.compile('\w', flags)
  1171. self.assertTrue(pat.match('\xe0'))
  1172. pat = re.compile('\xc0', re.ASCII | re.IGNORECASE)
  1173. self.assertIsNone(pat.match('\xe0'))
  1174. pat = re.compile('(?a)\xc0', re.IGNORECASE)
  1175. self.assertIsNone(pat.match('\xe0'))
  1176. pat = re.compile('\w', re.ASCII)
  1177. self.assertIsNone(pat.match('\xe0'))
  1178. pat = re.compile('(?a)\w')
  1179. self.assertIsNone(pat.match('\xe0'))
  1180. # Bytes patterns
  1181. for flags in (0, re.ASCII):
  1182. pat = re.compile(b'\xc0', flags | re.IGNORECASE)
  1183. self.assertIsNone(pat.match(b'\xe0'))
  1184. pat = re.compile(b'\w', flags)
  1185. self.assertIsNone(pat.match(b'\xe0'))
  1186. # Incompatibilities
  1187. self.assertRaises(ValueError, re.compile, b'\w', re.UNICODE)
  1188. self.assertRaises(ValueError, re.compile, b'(?u)\w')
  1189. self.assertRaises(ValueError, re.compile, '\w', re.UNICODE | re.ASCII)
  1190. self.assertRaises(ValueError, re.compile, '(?u)\w', re.ASCII)
  1191. self.assertRaises(ValueError, re.compile, '(?a)\w', re.UNICODE)
  1192. self.assertRaises(ValueError, re.compile, '(?au)\w')
  1193. def test_locale_flag(self):
  1194. import locale
  1195. _, enc = locale.getlocale(locale.LC_CTYPE)
  1196. # Search non-ASCII letter
  1197. for i in range(128, 256):
  1198. try:
  1199. c = bytes([i]).decode(enc)
  1200. sletter = c.lower()
  1201. if sletter == c: continue
  1202. bletter = sletter.encode(enc)
  1203. if len(bletter) != 1: continue
  1204. if bletter.decode(enc) != sletter: continue
  1205. bpat = re.escape(bytes([i]))
  1206. break
  1207. except (UnicodeError, TypeError):
  1208. pass
  1209. else:
  1210. bletter = None
  1211. bpat = b'A'
  1212. # Bytes patterns
  1213. pat = re.compile(bpat, re.LOCALE | re.IGNORECASE)
  1214. if bletter:
  1215. self.assertTrue(pat.match(bletter))
  1216. pat = re.compile(b'(?L)' + bpat, re.IGNORECASE)
  1217. if bletter:
  1218. self.assertTrue(pat.match(bletter))
  1219. pat = re.compile(bpat, re.IGNORECASE)
  1220. if bletter:
  1221. self.assertIsNone(pat.match(bletter))
  1222. pat = re.compile(b'\w', re.LOCALE)
  1223. if bletter:
  1224. self.assertTrue(pat.match(bletter))
  1225. pat = re.compile(b'(?L)\w')
  1226. if bletter:
  1227. self.assertTrue(pat.match(bletter))
  1228. pat = re.compile(b'\w')
  1229. if bletter:
  1230. self.assertIsNone(pat.match(bletter))
  1231. # Incompatibilities
  1232. self.assertRaises(ValueError, re.compile, '', re.LOCALE)
  1233. self.assertRaises(ValueError, re.compile, '(?L)')
  1234. self.assertRaises(ValueError, re.compile, b'', re.LOCALE | re.ASCII)
  1235. self.assertRaises(ValueError, re.compile, b'(?L)', re.ASCII)
  1236. self.assertRaises(ValueError, re.compile, b'(?a)', re.LOCALE)
  1237. self.assertRaises(ValueError, re.compile, b'(?aL)')
  1238. def test_bug_6509(self):
  1239. # Replacement strings of both types must parse properly.
  1240. # all strings
  1241. pat = re.compile('a(\w)')
  1242. self.assertEqual(pat.sub('b\\1', 'ac'), 'bc')
  1243. pat = re.compile('a(.)')
  1244. self.assertEqual(pat.sub('b\\1', 'a\u1234'), 'b\u1234')
  1245. pat = re.compile('..')
  1246. self.assertEqual(pat.sub(lambda m: 'str', 'a5'), 'str')
  1247. # all bytes
  1248. pat = re.compile(b'a(\w)')
  1249. self.assertEqual(pat.sub(b'b\\1', b'ac'), b'bc')
  1250. pat = re.compile(b'a(.)')
  1251. self.assertEqual(pat.sub(b'b\\1', b'a\xCD'), b'b\xCD')
  1252. pat = re.compile(b'..')
  1253. self.assertEqual(pat.sub(lambda m: b'bytes', b'a5'), b'bytes')
  1254. def test_dealloc(self):
  1255. # issue 3299: check for segfault in debug build
  1256. import _sre
  1257. # the overflow limit is different on wide and narrow builds and it
  1258. # depends on the definition of SRE_CODE (see sre.h).
  1259. # 2**128 should be big enough to overflow on both. For smaller values
  1260. # a RuntimeError is raised instead of OverflowError.
  1261. long_overflow = 2**128
  1262. self.assertRaises(TypeError, re.finditer, "a", {})
  1263. with self.assertRaises(OverflowError):
  1264. _sre.compile("abc", 0, [long_overflow], 0, [], [])
  1265. with self.assertRaises(TypeError):
  1266. _sre.compile({}, 0, [], 0, [], [])
  1267. def test_search_dot_unicode(self):
  1268. self.assertTrue(re.search("123.*-", '123abc-'))
  1269. self.assertTrue(re.search("123.*-", '123\xe9-'))
  1270. self.assertTrue(re.search("123.*-", '123\u20ac-'))
  1271. self.assertTrue(re.search("123.*-", '123\U0010ffff-'))
  1272. self.assertTrue(re.search("123.*-", '123\xe9\u20ac\U0010ffff-'))
  1273. def test_compile(self):
  1274. # Test return value when given string and pattern as parameter
  1275. pattern = re.compile('random pattern')
  1276. self.assertIsInstance(pattern, re._pattern_type)
  1277. same_pattern = re.compile(pattern)
  1278. self.assertIsInstance(same_pattern, re._pattern_type)
  1279. self.assertIs(same_pattern, pattern)
  1280. # Test behaviour when not given a string or pattern as parameter
  1281. self.assertRaises(TypeError, re.compile, 0)
  1282. @bigmemtest(size=_2G, memuse=1)
  1283. def test_large_search(self, size):
  1284. # Issue #10182: indices were 32-bit-truncated.
  1285. s = 'a' * size
  1286. m = re.search('$', s)
  1287. self.assertIsNotNone(m)
  1288. self.assertEqual(m.start(), size)
  1289. self.assertEqual(m.end(), size)
  1290. # The huge memuse is because of re.sub() using a list and a join()
  1291. # to create the replacement result.
  1292. @bigmemtest(size=_2G, memuse=16 + 2)
  1293. def test_large_subn(self, size):
  1294. # Issue #10182: indices were 32-bit-truncated.
  1295. s = 'a' * size
  1296. r, n = re.subn('', '', s)
  1297. self.assertEqual(r, s)
  1298. self.assertEqual(n, size + 1)
  1299. def test_bug_16688(self):
  1300. # Issue 16688: Backreferences make case-insensitive regex fail on
  1301. # non-ASCII strings.
  1302. self.assertEqual(re.findall(r"(?i)(a)\1", "aa \u0100"), ['a'])
  1303. self.assertEqual(re.match(r"(?s).{1,3}", "\u0100\u0100").span(), (0, 2))
  1304. def test_repeat_minmax_overflow(self):
  1305. # Issue #13169
  1306. string = "x" * 100000
  1307. self.assertEqual(re.match(r".{65535}", string).span(), (0, 65535))
  1308. self.assertEqual(re.match(r".{,65535}", string).span(), (0, 65535))
  1309. self.assertEqual(re.match(r".{65535,}?", string).span(), (0, 65535))
  1310. self.assertEqual(re.match(r".{65536}", string).span(), (0, 65536))
  1311. self.assertEqual(re.match(r".{,65536}", string).span(), (0, 65536))
  1312. self.assertEqual(re.match(r".{65536,}?", string).span(), (0, 65536))
  1313. # 2**128 should be big enough to overflow both SRE_CODE and Py_ssize_t.
  1314. self.assertRaises(OverflowError, re.compile, r".{%d}" % 2**128)
  1315. self.assertRaises(OverflowError, re.compile, r".{,%d}" % 2**128)
  1316. self.assertRaises(OverflowError, re.compile, r".{%d,}?" % 2**128)
  1317. self.assertRaises(OverflowError, re.compile, r".{%d,%d}" % (2**129, 2**128))
  1318. @cpython_only
  1319. def test_repeat_minmax_overflow_maxrepeat(self):
  1320. try:
  1321. from _sre import MAXREPEAT
  1322. except ImportError:
  1323. self.skipTest('requires _sre.MAXREPEAT constant')
  1324. string = "x" * 100000
  1325. self.assertIsNone(re.match(r".{%d}" % (MAXREPEAT - 1), string))
  1326. self.assertEqual(re.match(r".{,%d}" % (MAXREPEAT - 1), string).span(),
  1327. (0, 100000))
  1328. self.assertIsNone(re.match(r".{%d,}?" % (MAXREPEAT - 1), string))
  1329. self.assertRaises(OverflowError, re.compile, r".{%d}" % MAXREPEAT)
  1330. self.assertRaises(OverflowError, re.compile, r".{,%d}" % MAXREPEAT)
  1331. self.assertRaises(OverflowError, re.compile, r".{%d,}?" % MAXREPEAT)
  1332. def test_backref_group_name_in_exception(self):
  1333. # Issue 17341: Poor error message when compiling invalid regex
  1334. self.checkPatternError('(?P=<foo>)',
  1335. "bad character in group name '<foo>'", 4)
  1336. def test_group_name_in_exception(self):
  1337. # Issue 17341: Poor error message when compiling invalid regex
  1338. self.checkPatternError('(?P<?foo>)',
  1339. "bad character in group name '?foo'", 4)
  1340. def test_issue17998(self):
  1341. for reps in '*', '+', '?', '{1}':
  1342. for mod in '', '?':
  1343. pattern = '.' + reps + mod + 'yz'
  1344. self.assertEqual(re.compile(pattern, re.S).findall('xyz'),
  1345. ['xyz'], msg=pattern)
  1346. pattern = pattern.encode()
  1347. self.assertEqual(re.compile(pattern, re.S).findall(b'xyz'),
  1348. [b'xyz'], msg=pattern)
  1349. def test_match_repr(self):
  1350. for string in '[abracadabra]', S('[abracadabra]'):
  1351. m = re.search(r'(.+)(.*?)\1', string)
  1352. self.assertEqual(repr(m), "<%s.%s object; "
  1353. "span=(1, 12), match='abracadabra'>" %
  1354. (type(m).__module__, type(m).__qualname__))
  1355. for string in (b'[abracadabra]', B(b'[abracadabra]'),
  1356. bytearray(b'[abracadabra]'),
  1357. memoryview(b'[abracadabra]')):
  1358. m = re.search(rb'(.+)(.*?)\1', string)
  1359. self.assertEqual(repr(m), "<%s.%s object; "
  1360. "span=(1, 12), match=b'abracadabra'>" %
  1361. (type(m).__module__, type(m).__qualname__))
  1362. first, second = list(re.finditer("(aa)|(bb)", "aa bb"))
  1363. self.assertEqual(repr(first), "<%s.%s object; "
  1364. "span=(0, 2), match='aa'>" %
  1365. (type(second).__module__, type(first).__qualname__))
  1366. self.assertEqual(repr(second), "<%s.%s object; "
  1367. "span=(3, 5), match='bb'>" %
  1368. (type(second).__module__, type(second).__qualname__))
  1369. def test_bug_2537(self):
  1370. # issue 2537: empty submatches
  1371. for outer_op in ('{0,}', '*', '+', '{1,187}'):
  1372. for inner_op in ('{0,}', '*', '?'):
  1373. r = re.compile("^((x|y)%s)%s" % (inner_op, outer_op))
  1374. m = r.match("xyyzy")
  1375. self.assertEqual(m.group(0), "xyy")
  1376. self.assertEqual(m.group(1), "")
  1377. self.assertEqual(m.group(2), "y")
  1378. def test_debug_flag(self):
  1379. pat = r'(\.)(?:[ch]|py)(?(1)$|: )'
  1380. with captured_stdout() as out:
  1381. re.compile(pat, re.DEBUG)
  1382. dump = '''\
  1383. SUBPATTERN 1
  1384. LITERAL 46
  1385. SUBPATTERN None
  1386. BRANCH
  1387. IN
  1388. LITERAL 99
  1389. LITERAL 104
  1390. OR
  1391. LITERAL 112
  1392. LITERAL 121
  1393. SUBPATTERN None
  1394. GROUPREF_EXISTS 1
  1395. AT AT_END
  1396. ELSE
  1397. LITERAL 58
  1398. LITERAL 32
  1399. '''
  1400. self.assertEqual(out.getvalue(), dump)
  1401. # Debug output is output again even a second time (bypassing
  1402. # the cache -- issue #20426).
  1403. with captured_stdout() as out:
  1404. re.compile(pat, re.DEBUG)
  1405. self.assertEqual(out.getvalue(), dump)
  1406. def test_keyword_parameters(self):
  1407. # Issue #20283: Accepting the string keyword parameter.
  1408. pat = re.compile(r'(ab)')
  1409. self.assertEqual(
  1410. pat.match(string='abracadabra', pos=7, endpos=10).span(), (7, 9))
  1411. self.assertEqual(
  1412. pat.fullmatch(string='abracadabra', pos=7, endpos=9).span(), (7, 9))
  1413. self.assertEqual(
  1414. pat.search(string='abracadabra', pos=3, endpos=10).span(), (7, 9))
  1415. self.assertEqual(
  1416. pat.findall(string='abracadabra', pos=3, endpos=10), ['ab'])
  1417. self.assertEqual(
  1418. pat.split(string='abracadabra', maxsplit=1),
  1419. ['', 'ab', 'racadabra'])
  1420. self.assertEqual(
  1421. pat.scanner(string='abracadabra', pos=3, endpos=10).search().span(),
  1422. (7, 9))
  1423. def test_bug_20998(self):
  1424. # Issue #20998: Fullmatch of repeated single character pattern
  1425. # with ignore case.
  1426. self.assertEqual(re.fullmatch('[a-c]+', 'ABC', re.I).span(), (0, 3))
  1427. def test_locale_caching(self):
  1428. # Issue #22410
  1429. oldlocale = locale.setlocale(locale.LC_CTYPE)
  1430. self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale)
  1431. for loc in 'en_US.iso88591', 'en_US.utf8':
  1432. try:
  1433. locale.setlocale(locale.LC_CTYPE, loc)
  1434. except locale.Error:
  1435. # Unsupported locale on this system
  1436. self.skipTest('test needs %s locale' % loc)
  1437. re.purge()
  1438. self.check_en_US_iso88591()
  1439. self.check_en_US_utf8()
  1440. re.purge()
  1441. self.check_en_US_utf8()
  1442. self.check_en_US_iso88591()
  1443. def check_en_US_iso88591(self):
  1444. locale.setlocale(locale.LC_CTYPE, 'en_US.iso88591')
  1445. self.assertTrue(re.match(b'\xc5\xe5', b'\xc5\xe5', re.L|re.I))
  1446. self.assertTrue(re.match(b'\xc5', b'\xe5', re.L|re.I))
  1447. self.assertTrue(re.match(b'\xe5', b'\xc5', re.L|re.I))
  1448. self.assertTrue(re.match(b'(?Li)\xc5\xe5', b'\xc5\xe5'))
  1449. self.assertTrue(re.match(b'(?Li)\xc5', b'\xe5'))
  1450. self.assertTrue(re.match(b'(?Li)\xe5', b'\xc5'))
  1451. def check_en_US_utf8(self):
  1452. locale.setlocale(locale.LC_CTYPE, 'en_US.utf8')
  1453. self.assertTrue(re.match(b'\xc5\xe5', b'\xc5\xe5', re.L|re.I))
  1454. self.assertIsNone(re.match(b'\xc5', b'\xe5', re.L|re.I))
  1455. self.assertIsNone(re.match(b'\xe5', b'\xc5', re.L|re.I))
  1456. self.assertTrue(re.match(b'(?Li)\xc5\xe5', b'\xc5\xe5'))
  1457. self.assertIsNone(re.match(b'(?Li)\xc5', b'\xe5'))
  1458. self.assertIsNone(re.match(b'(?Li)\xe5', b'\xc5'))
  1459. def test_error(self):
  1460. with self.assertRaises(re.error) as cm:
  1461. re.compile('(\u20ac))')
  1462. err = cm.exception
  1463. self.assertIsInstance(err.pattern, str)
  1464. self.assertEqual(err.pattern, '(\u20ac))')
  1465. self.assertEqual(err.pos, 3)
  1466. self.assertEqual(err.lineno, 1)
  1467. self.assertEqual(err.colno, 4)
  1468. self.assertIn(err.msg, str(err))
  1469. self.assertIn(' at position 3', str(err))
  1470. self.assertNotIn(' at position 3', err.msg)
  1471. # Bytes pattern
  1472. with self.assertRaises(re.error) as cm:
  1473. re.compile(b'(\xa4))')
  1474. err = cm.exception
  1475. self.assertIsInstance(err.pattern, bytes)
  1476. self.assertEqual(err.pattern, b'(\xa4))')
  1477. self.assertEqual(err.pos, 3)
  1478. # Multiline pattern
  1479. with self.assertRaises(re.error) as cm:
  1480. re.compile("""
  1481. (
  1482. abc
  1483. )
  1484. )
  1485. (
  1486. """, re.VERBOSE)
  1487. err = cm.exception
  1488. self.assertEqual(err.pos, 77)
  1489. self.assertEqual(err.lineno, 5)
  1490. self.assertEqual(err.colno, 17)
  1491. self.assertIn(err.msg, str(err))
  1492. self.assertIn(' at position 77', str(err))
  1493. self.assertIn('(line 5, column 17)', str(err))
  1494. def test_misc_errors(self):
  1495. self.checkPatternError(r'(', 'missing ), unterminated subpattern', 0)
  1496. self.checkPatternError(r'((a|b)', 'missing ), unterminated subpattern', 0)
  1497. self.checkPatternError(r'(a|b))', 'unbalanced parenthesis', 5)
  1498. self.checkPatternError(r'(?P', 'unexpected end of pattern', 3)
  1499. self.checkPatternError(r'(?z)', 'unknown extension ?z', 1)
  1500. self.checkPatternError(r'(?iz)', 'unknown flag', 3)
  1501. self.checkPatternError(r'(?i', 'missing )', 3)
  1502. self.checkPatternError(r'(?#abc', 'missing ), unterminated comment', 0)
  1503. self.checkPatternError(r'(?<', 'unexpected end of pattern', 3)
  1504. self.checkPatternError(r'(?<>)', 'unknown extension ?<>', 1)
  1505. self.checkPatternError(r'(?', 'unexpected end of pattern', 2)
  1506. class PatternReprTests(unittest.TestCase):
  1507. def check(self, pattern, expected):
  1508. self.assertEqual(repr(re.compile(pattern)), expected)
  1509. def check_flags(self, pattern, flags, expected):
  1510. self.assertEqual(repr(re.compile(pattern, flags)), expected)
  1511. def test_without_flags(self):
  1512. self.check('random pattern',
  1513. "re.compile('random pattern')")
  1514. def test_single_flag(self):
  1515. self.check_flags('random pattern', re.IGNORECASE,
  1516. "re.compile('random pattern', re.IGNORECASE)")
  1517. def test_multiple_flags(self):
  1518. self.check_flags('random pattern', re.I|re.S|re.X,
  1519. "re.compile('random pattern', "
  1520. "re.IGNORECASE|re.DOTALL|re.VERBOSE)")
  1521. def test_unicode_flag(self):
  1522. self.check_flags('random pattern', re.U,
  1523. "re.compile('random pattern')")
  1524. self.check_flags('random pattern', re.I|re.S|re.U,
  1525. "re.compile('random pattern', "
  1526. "re.IGNORECASE|re.DOTALL)")
  1527. def test_inline_flags(self):
  1528. self.check('(?i)pattern',
  1529. "re.compile('(?i)pattern', re.IGNORECASE)")
  1530. def test_unknown_flags(self):
  1531. self.check_flags('random pattern', 0x123000,
  1532. "re.compile('random pattern', 0x123000)")
  1533. self.check_flags('random pattern', 0x123000|re.I,
  1534. "re.compile('random pattern', re.IGNORECASE|0x123000)")
  1535. def test_bytes(self):
  1536. self.check(b'bytes pattern',
  1537. "re.compile(b'bytes pattern')")
  1538. self.check_flags(b'bytes pattern', re.A,
  1539. "re.compile(b'bytes pattern', re.ASCII)")
  1540. def test_locale(self):
  1541. self.check_flags(b'bytes pattern', re.L,
  1542. "re.compile(b'bytes pattern', re.LOCALE)")
  1543. def test_quotes(self):
  1544. self.check('random "double quoted" pattern',
  1545. '''re.compile('random "double quoted" pattern')''')
  1546. self.check("random 'single quoted' pattern",
  1547. '''re.compile("random 'single quoted' pattern")''')
  1548. self.check('''both 'single' and "double" quotes''',
  1549. '''re.compile('both \\'single\\' and "double" quotes')''')
  1550. def test_long_pattern(self):
  1551. pattern = 'Very %spattern' % ('long ' * 1000)
  1552. r = repr(re.compile(pattern))
  1553. self.assertLess(len(r), 300)
  1554. self.assertEqual(r[:30], "re.compile('Very long long lon")
  1555. r = repr(re.compile(pattern, re.I))
  1556. self.assertLess(len(r), 300)
  1557. self.assertEqual(r[:30], "re.compile('Very long long lon")
  1558. self.assertEqual(r[-16:], ", re.IGNORECASE)")
  1559. class ImplementationTest(unittest.TestCase):
  1560. """
  1561. Test implementation details of the re module.
  1562. """
  1563. def test_overlap_table(self):
  1564. f = sre_compile._generate_overlap_table
  1565. self.assertEqual(f(""), [])
  1566. self.assertEqual(f("a"), [0])
  1567. self.assertEqual(f("abcd"), [0, 0, 0, 0])
  1568. self.assertEqual(f("aaaa"), [0, 1, 2, 3])
  1569. self.assertEqual(f("ababba"), [0, 0, 1, 2, 0, 1])
  1570. self.assertEqual(f("abcabdac"), [0, 0, 0, 1, 2, 0, 1, 0])
  1571. class ExternalTests(unittest.TestCase):
  1572. def test_re_benchmarks(self):
  1573. 're_tests benchmarks'
  1574. from test.re_tests import benchmarks
  1575. for pattern, s in benchmarks:
  1576. with self.subTest(pattern=pattern, string=s):
  1577. p = re.compile(pattern)
  1578. self.assertTrue(p.search(s))
  1579. self.assertTrue(p.match(s))
  1580. self.assertTrue(p.fullmatch(s))
  1581. s2 = ' '*10000 + s + ' '*10000
  1582. self.assertTrue(p.search(s2))
  1583. self.assertTrue(p.match(s2, 10000))
  1584. self.assertTrue(p.match(s2, 10000, 10000 + len(s)))
  1585. self.assertTrue(p.fullmatch(s2, 10000, 10000 + len(s)))
  1586. def test_re_tests(self):
  1587. 're_tests test suite'
  1588. from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR
  1589. for t in tests:
  1590. pattern = s = outcome = repl = expected = None
  1591. if len(t) == 5:
  1592. pattern, s, outcome, repl, expected = t
  1593. elif len(t) == 3:
  1594. pattern, s, outcome = t
  1595. else:
  1596. raise ValueError('Test tuples should have 3 or 5 fields', t)
  1597. with self.subTest(pattern=pattern, string=s):
  1598. if outcome == SYNTAX_ERROR: # Expected a syntax error
  1599. with self.assertRaises(re.error):
  1600. re.compile(pattern)
  1601. continue
  1602. obj = re.compile(pattern)
  1603. result = obj.search(s)
  1604. if outcome == FAIL:
  1605. self.assertIsNone(result, 'Succeeded incorrectly')
  1606. continue
  1607. with self.subTest():
  1608. self.assertTrue(result, 'Failed incorrectly')
  1609. # Matched, as expected, so now we compute the
  1610. # result string and compare it to our expected result.
  1611. start, end = result.span(0)
  1612. vardict = {'found': result.group(0),
  1613. 'groups': result.group(),
  1614. 'flags': result.re.flags}
  1615. for i in range(1, 100):
  1616. try:
  1617. gi = result.group(i)
  1618. # Special hack because else the string concat fails:
  1619. if gi is None:
  1620. gi = "None"
  1621. except IndexError:
  1622. gi = "Error"
  1623. vardict['g%d' % i] = gi
  1624. for i in result.re.groupindex.keys():
  1625. try:
  1626. gi = result.group(i)
  1627. if gi is None:
  1628. gi = "None"
  1629. except IndexError:
  1630. gi = "Error"
  1631. vardict[i] = gi
  1632. self.assertEqual(eval(repl, vardict), expected,
  1633. 'grouping error')
  1634. # Try the match with both pattern and string converted to
  1635. # bytes, and check that it still succeeds.
  1636. try:
  1637. bpat = bytes(pattern, "ascii")
  1638. bs = bytes(s, "ascii")
  1639. except UnicodeEncodeError:
  1640. # skip non-ascii tests
  1641. pass
  1642. else:
  1643. with self.subTest('bytes pattern match'):
  1644. obj = re.compile(bpat)
  1645. self.assertTrue(obj.search(bs))
  1646. # Try the match with LOCALE enabled, and check that it
  1647. # still succeeds.
  1648. with self.subTest('locale-sensitive match'):
  1649. obj = re.compile(bpat, re.LOCALE)
  1650. result = obj.search(bs)
  1651. if result is None:
  1652. print('=== Fails on locale-sensitive match', t)
  1653. # Try the match with the search area limited to the extent
  1654. # of the match and see if it still succeeds. \B will
  1655. # break (because it won't match at the end or start of a
  1656. # string), so we'll ignore patterns that feature it.
  1657. if (pattern[:2] != r'\B' and pattern[-2:] != r'\B'
  1658. and result is not None):
  1659. with self.subTest('range-limited match'):
  1660. obj = re.compile(pattern)
  1661. self.assertTrue(obj.search(s, start, end + 1))
  1662. # Try the match with IGNORECASE enabled, and check that it
  1663. # still succeeds.
  1664. with self.subTest('case-insensitive match'):
  1665. obj = re.compile(pattern, re.IGNORECASE)
  1666. self.assertTrue(obj.search(s))
  1667. # Try the match with UNICODE locale enabled, and check
  1668. # that it still succeeds.
  1669. with self.subTest('unicode-sensitive match'):
  1670. obj = re.compile(pattern, re.UNICODE)
  1671. self.assertTrue(obj.search(s))
  1672. if __name__ == "__main__":
  1673. unittest.main()