PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/rpython/rlib/rsre/test/test_re.py

https://bitbucket.org/timfel/pypy
Python | 664 lines | 632 code | 18 blank | 14 comment | 3 complexity | 98d24010fc325412e8b95ec601b41cf5 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-3.0, BSD-3-Clause
  1. import sys, os, py
  2. from rpython.rlib.rsre.test.test_match import get_code
  3. from rpython.rlib.rsre import rsre_re as re
  4. class TestRe:
  5. def test_search_star_plus(self):
  6. assert re.search('x*', 'axx').span(0) == (0, 0)
  7. assert re.search('x*', 'axx').span() == (0, 0)
  8. assert re.search('x+', 'axx').span(0) == (1, 3)
  9. assert re.search('x+', 'axx').span() == (1, 3)
  10. assert re.search('x', 'aaa') == None
  11. assert re.match('a*', 'xxx').span(0) == (0, 0)
  12. assert re.match('a*', 'xxx').span() == (0, 0)
  13. assert re.match('x*', 'xxxa').span(0) == (0, 3)
  14. assert re.match('x*', 'xxxa').span() == (0, 3)
  15. assert re.match('a+', 'xxx') == None
  16. def bump_num(self, matchobj):
  17. int_value = int(matchobj.group(0))
  18. return str(int_value + 1)
  19. def test_basic_re_sub(self):
  20. assert re.sub("(?i)b+", "x", "bbbb BBBB") == 'x x'
  21. assert re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y') == (
  22. '9.3 -3 24x100y')
  23. assert re.sub(r'\d+', self.bump_num, '08.2 -2 23x99y', 3) == (
  24. '9.3 -3 23x99y')
  25. assert re.sub('.', lambda m: r"\n", 'x') == '\\n'
  26. assert re.sub('.', r"\n", 'x') == '\n'
  27. s = r"\1\1"
  28. assert re.sub('(.)', s, 'x') == 'xx'
  29. assert re.sub('(.)', re.escape(s), 'x') == s
  30. assert re.sub('(.)', lambda m: s, 'x') == s
  31. assert re.sub('(?P<a>x)', '\g<a>\g<a>', 'xx') == 'xxxx'
  32. assert re.sub('(?P<a>x)', '\g<a>\g<1>', 'xx') == 'xxxx'
  33. assert re.sub('(?P<unk>x)', '\g<unk>\g<unk>', 'xx') == 'xxxx'
  34. assert re.sub('(?P<unk>x)', '\g<1>\g<1>', 'xx') == 'xxxx'
  35. assert re.sub('a',r'\t\n\v\r\f\a\b\B\Z\a\A\w\W\s\S\d\D','a') == (
  36. '\t\n\v\r\f\a\b\\B\\Z\a\\A\\w\\W\\s\\S\\d\\D')
  37. assert re.sub('a', '\t\n\v\r\f\a', 'a') == '\t\n\v\r\f\a'
  38. assert re.sub('a', '\t\n\v\r\f\a', 'a') == (
  39. (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)))
  40. assert re.sub('^\s*', 'X', 'test') == 'Xtest'
  41. def test_bug_449964(self):
  42. # fails for group followed by other escape
  43. assert re.sub(r'(?P<unk>x)', '\g<1>\g<1>\\b', 'xx') == (
  44. 'xx\bxx\b')
  45. def test_bug_449000(self):
  46. # Test for sub() on escaped characters
  47. assert re.sub(r'\r\n', r'\n', 'abc\r\ndef\r\n') == (
  48. 'abc\ndef\n')
  49. assert re.sub('\r\n', r'\n', 'abc\r\ndef\r\n') == (
  50. 'abc\ndef\n')
  51. assert re.sub(r'\r\n', '\n', 'abc\r\ndef\r\n') == (
  52. 'abc\ndef\n')
  53. assert re.sub('\r\n', '\n', 'abc\r\ndef\r\n') == (
  54. 'abc\ndef\n')
  55. def test_bug_1140(self):
  56. # re.sub(x, y, u'') should return u'', not '', and
  57. # re.sub(x, y, '') should return '', not u''.
  58. # Also:
  59. # re.sub(x, y, unicode(x)) should return unicode(y), and
  60. # re.sub(x, y, str(x)) should return
  61. # str(y) if isinstance(y, str) else unicode(y).
  62. for x in 'x', u'x':
  63. for y in 'y', u'y':
  64. z = re.sub(x, y, u'')
  65. assert z == u''
  66. assert type(z) == unicode
  67. #
  68. z = re.sub(x, y, '')
  69. assert z == ''
  70. assert type(z) == str
  71. #
  72. z = re.sub(x, y, unicode(x))
  73. assert z == y
  74. assert type(z) == unicode
  75. #
  76. z = re.sub(x, y, str(x))
  77. assert z == y
  78. assert type(z) == type(y)
  79. def test_sub_template_numeric_escape(self):
  80. # bug 776311 and friends
  81. assert re.sub('x', r'\0', 'x') == '\0'
  82. assert re.sub('x', r'\000', 'x') == '\000'
  83. assert re.sub('x', r'\001', 'x') == '\001'
  84. assert re.sub('x', r'\008', 'x') == '\0' + '8'
  85. assert re.sub('x', r'\009', 'x') == '\0' + '9'
  86. assert re.sub('x', r'\111', 'x') == '\111'
  87. assert re.sub('x', r'\117', 'x') == '\117'
  88. assert re.sub('x', r'\1111', 'x') == '\1111'
  89. assert re.sub('x', r'\1111', 'x') == '\111' + '1'
  90. assert re.sub('x', r'\00', 'x') == '\x00'
  91. assert re.sub('x', r'\07', 'x') == '\x07'
  92. assert re.sub('x', r'\08', 'x') == '\0' + '8'
  93. assert re.sub('x', r'\09', 'x') == '\0' + '9'
  94. assert re.sub('x', r'\0a', 'x') == '\0' + 'a'
  95. assert re.sub('x', r'\400', 'x') == '\0'
  96. assert re.sub('x', r'\777', 'x') == '\377'
  97. py.test.raises(re.error, re.sub, 'x', r'\1', 'x')
  98. py.test.raises(re.error, re.sub, 'x', r'\8', 'x')
  99. py.test.raises(re.error, re.sub, 'x', r'\9', 'x')
  100. py.test.raises(re.error, re.sub, 'x', r'\11', 'x')
  101. py.test.raises(re.error, re.sub, 'x', r'\18', 'x')
  102. py.test.raises(re.error, re.sub, 'x', r'\1a', 'x')
  103. py.test.raises(re.error, re.sub, 'x', r'\90', 'x')
  104. py.test.raises(re.error, re.sub, 'x', r'\99', 'x')
  105. py.test.raises(re.error, re.sub, 'x', r'\118', 'x') # r'\11' + '8'
  106. py.test.raises(re.error, re.sub, 'x', r'\11a', 'x')
  107. py.test.raises(re.error, re.sub, 'x', r'\181', 'x') # r'\18' + '1'
  108. py.test.raises(re.error, re.sub, 'x', r'\800', 'x') # r'\80' + '0'
  109. # in python2.3 (etc), these loop endlessly in sre_parser.py
  110. assert re.sub('(((((((((((x)))))))))))', r'\11', 'x') == 'x'
  111. assert re.sub('((((((((((y))))))))))(.)', r'\118', 'xyz') == (
  112. 'xz8')
  113. assert re.sub('((((((((((y))))))))))(.)', r'\11a', 'xyz') == (
  114. 'xza')
  115. def test_qualified_re_sub(self):
  116. assert re.sub('a', 'b', 'aaaaa') == 'bbbbb'
  117. assert re.sub('a', 'b', 'aaaaa', 1) == 'baaaa'
  118. def test_bug_114660(self):
  119. assert re.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello there') == (
  120. 'hello there')
  121. def test_bug_462270(self):
  122. # Test for empty sub() behaviour, see SF bug #462270
  123. assert re.sub('x*', '-', 'abxd') == '-a-b-d-'
  124. assert re.sub('x+', '-', 'abxd') == 'ab-d'
  125. def test_symbolic_refs(self):
  126. py.test.raises(re.error, re.sub, '(?P<a>x)', '\g<a', 'xx')
  127. py.test.raises(re.error, re.sub, '(?P<a>x)', '\g<', 'xx')
  128. py.test.raises(re.error, re.sub, '(?P<a>x)', '\g', 'xx')
  129. py.test.raises(re.error, re.sub, '(?P<a>x)', '\g<a a>', 'xx')
  130. py.test.raises(re.error, re.sub, '(?P<a>x)', '\g<1a1>', 'xx')
  131. py.test.raises(IndexError, re.sub, '(?P<a>x)', '\g<ab>', 'xx')
  132. py.test.raises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')
  133. py.test.raises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\\2', 'xx')
  134. py.test.raises(re.error, re.sub, '(?P<a>x)', '\g<-1>', 'xx')
  135. def test_re_subn(self):
  136. assert re.subn("(?i)b+", "x", "bbbb BBBB") == ('x x', 2)
  137. assert re.subn("b+", "x", "bbbb BBBB") == ('x BBBB', 1)
  138. assert re.subn("b+", "x", "xyz") == ('xyz', 0)
  139. assert re.subn("b*", "x", "xyz") == ('xxxyxzx', 4)
  140. assert re.subn("b*", "x", "xyz", 2) == ('xxxyz', 2)
  141. def test_re_split(self):
  142. assert re.split(":", ":a:b::c") == ['', 'a', 'b', '', 'c']
  143. assert re.split(":*", ":a:b::c") == ['', 'a', 'b', 'c']
  144. assert re.split("(:*)", ":a:b::c") == (
  145. ['', ':', 'a', ':', 'b', '::', 'c'])
  146. assert re.split("(?::*)", ":a:b::c") == ['', 'a', 'b', 'c']
  147. assert re.split("(:)*", ":a:b::c") == (
  148. ['', ':', 'a', ':', 'b', ':', 'c'])
  149. assert re.split("([b:]+)", ":a:b::c") == (
  150. ['', ':', 'a', ':b::', 'c'])
  151. assert re.split("(b)|(:+)", ":a:b::c") == (
  152. ['', None, ':', 'a', None, ':', '', 'b', None, '',
  153. None, '::', 'c'])
  154. assert re.split("(?:b)|(?::+)", ":a:b::c") == (
  155. ['', 'a', '', '', 'c'])
  156. def test_qualified_re_split(self):
  157. assert re.split(":", ":a:b::c", 2) == ['', 'a', 'b::c']
  158. assert re.split(':', 'a:b:c:d', 2) == ['a', 'b', 'c:d']
  159. assert re.split("(:)", ":a:b::c", 2) == (
  160. ['', ':', 'a', ':', 'b::c'])
  161. assert re.split("(:*)", ":a:b::c", 2) == (
  162. ['', ':', 'a', ':', 'b::c'])
  163. def test_re_findall(self):
  164. assert re.findall(":+", "abc") == []
  165. assert re.findall(":+", "a:b::c:::d") == [":", "::", ":::"]
  166. assert re.findall("(:+)", "a:b::c:::d") == [":", "::", ":::"]
  167. def test_re_findall_2(self):
  168. py.test.skip("findall() returning groups is not RPython")
  169. assert re.findall("(:)(:*)", "a:b::c:::d") == [(":", ""),
  170. (":", ":"),
  171. (":", "::")]
  172. def test_bug_117612(self):
  173. py.test.skip("findall() returning groups is not RPython")
  174. assert re.findall(r"(a|(b))", "aba") == (
  175. [("a", ""),("b", "b"),("a", "")])
  176. def test_re_match(self):
  177. assert re.match('a', 'a').groups() == ()
  178. assert re.match('(a)', 'a').groups() == ('a',)
  179. assert re.match(r'(a)', 'a').group(0) == 'a'
  180. assert re.match(r'(a)', 'a').group(1) == 'a'
  181. #assert re.match(r'(a)', 'a').group(1, 1) == ('a', 'a')
  182. pat = re.compile('((a)|(b))(c)?')
  183. assert pat.match('a').groups() == ('a', 'a', None, None)
  184. assert pat.match('b').groups() == ('b', None, 'b', None)
  185. assert pat.match('ac').groups() == ('a', 'a', None, 'c')
  186. assert pat.match('bc').groups() == ('b', None, 'b', 'c')
  187. assert pat.match('bc').groups("") == ('b', "", 'b', 'c')
  188. # A single group
  189. m = re.match('(a)', 'a')
  190. assert m.group(0) == 'a'
  191. assert m.group(0) == 'a'
  192. assert m.group(1) == 'a'
  193. #assert m.group(1, 1) == ('a', 'a')
  194. pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
  195. #assert pat.match('a').group(1, 2, 3) == ('a', None, None)
  196. #assert pat.match('b').group('a1', 'b2', 'c3') == (
  197. # (None, 'b', None))
  198. #assert pat.match('ac').group(1, 'b2', 3) == ('a', None, 'c')
  199. def test_bug_923(self):
  200. # Issue923: grouping inside optional lookahead problem
  201. assert re.match(r'a(?=(b))?', "ab").groups() == ("b",)
  202. assert re.match(r'(a(?=(b))?)', "ab").groups() == ('a', 'b')
  203. assert re.match(r'(a)(?=(b))?', "ab").groups() == ('a', 'b')
  204. assert re.match(r'(?P<g1>a)(?=(?P<g2>b))?', "ab").groupdict() == {'g1': 'a', 'g2': 'b'}
  205. def test_re_groupref_exists(self):
  206. assert re.match('^(\()?([^()]+)(?(1)\))$', '(a)').groups() == (
  207. ('(', 'a'))
  208. assert re.match('^(\()?([^()]+)(?(1)\))$', 'a').groups() == (
  209. (None, 'a'))
  210. assert re.match('^(\()?([^()]+)(?(1)\))$', 'a)') == None
  211. assert re.match('^(\()?([^()]+)(?(1)\))$', '(a') == None
  212. assert re.match('^(?:(a)|c)((?(1)b|d))$', 'ab').groups() == (
  213. ('a', 'b'))
  214. assert re.match('^(?:(a)|c)((?(1)b|d))$', 'cd').groups() == (
  215. (None, 'd'))
  216. assert re.match('^(?:(a)|c)((?(1)|d))$', 'cd').groups() == (
  217. (None, 'd'))
  218. assert re.match('^(?:(a)|c)((?(1)|d))$', 'a').groups() == (
  219. ('a', ''))
  220. # Tests for bug #1177831: exercise groups other than the first group
  221. p = re.compile('(?P<g1>a)(?P<g2>b)?((?(g2)c|d))')
  222. assert p.match('abc').groups() == (
  223. ('a', 'b', 'c'))
  224. assert p.match('ad').groups() == (
  225. ('a', None, 'd'))
  226. assert p.match('abd') == None
  227. assert p.match('ac') == None
  228. def test_re_groupref(self):
  229. assert re.match(r'^(\|)?([^()]+)\1$', '|a|').groups() == (
  230. ('|', 'a'))
  231. assert re.match(r'^(\|)?([^()]+)\1?$', 'a').groups() == (
  232. (None, 'a'))
  233. assert re.match(r'^(\|)?([^()]+)\1$', 'a|') == None
  234. assert re.match(r'^(\|)?([^()]+)\1$', '|a') == None
  235. assert re.match(r'^(?:(a)|c)(\1)$', 'aa').groups() == (
  236. ('a', 'a'))
  237. assert re.match(r'^(?:(a)|c)(\1)?$', 'c').groups() == (
  238. (None, None))
  239. def test_groupdict(self):
  240. assert re.match('(?P<first>first) (?P<second>second)',
  241. 'first second').groupdict() == (
  242. {'first':'first', 'second':'second'})
  243. def test_expand(self):
  244. assert (re.match("(?P<first>first) (?P<second>second)",
  245. "first second")
  246. .expand(r"\2 \1 \g<second> \g<first>")) == (
  247. "second first second first")
  248. def test_repeat_minmax(self):
  249. assert re.match("^(\w){1}$", "abc") == None
  250. assert re.match("^(\w){1}?$", "abc") == None
  251. assert re.match("^(\w){1,2}$", "abc") == None
  252. assert re.match("^(\w){1,2}?$", "abc") == None
  253. assert re.match("^(\w){3}$", "abc").group(1) == "c"
  254. assert re.match("^(\w){1,3}$", "abc").group(1) == "c"
  255. assert re.match("^(\w){1,4}$", "abc").group(1) == "c"
  256. assert re.match("^(\w){3,4}?$", "abc").group(1) == "c"
  257. assert re.match("^(\w){3}?$", "abc").group(1) == "c"
  258. assert re.match("^(\w){1,3}?$", "abc").group(1) == "c"
  259. assert re.match("^(\w){1,4}?$", "abc").group(1) == "c"
  260. assert re.match("^(\w){3,4}?$", "abc").group(1) == "c"
  261. assert re.match("^x{1}$", "xxx") == None
  262. assert re.match("^x{1}?$", "xxx") == None
  263. assert re.match("^x{1,2}$", "xxx") == None
  264. assert re.match("^x{1,2}?$", "xxx") == None
  265. assert re.match("^x{3}$", "xxx") != None
  266. assert re.match("^x{1,3}$", "xxx") != None
  267. assert re.match("^x{1,4}$", "xxx") != None
  268. assert re.match("^x{3,4}?$", "xxx") != None
  269. assert re.match("^x{3}?$", "xxx") != None
  270. assert re.match("^x{1,3}?$", "xxx") != None
  271. assert re.match("^x{1,4}?$", "xxx") != None
  272. assert re.match("^x{3,4}?$", "xxx") != None
  273. assert re.match("^x{}$", "xxx") == None
  274. assert re.match("^x{}$", "x{}") != None
  275. def test_getattr(self):
  276. assert re.match("(a)", "a").pos == 0
  277. assert re.match("(a)", "a").endpos == 1
  278. assert re.match("(a)", "a").string == "a"
  279. assert re.match("(a)", "a").regs == ((0, 1), (0, 1))
  280. assert re.match("(a)", "a").re != None
  281. def test_special_escapes(self):
  282. assert re.search(r"\b(b.)\b",
  283. "abcd abc bcd bx").group(1) == "bx"
  284. assert re.search(r"\B(b.)\B",
  285. "abc bcd bc abxd").group(1) == "bx"
  286. assert re.search(r"\b(b.)\b",
  287. "abcd abc bcd bx", re.LOCALE).group(1) == "bx"
  288. assert re.search(r"\B(b.)\B",
  289. "abc bcd bc abxd", re.LOCALE).group(1) == "bx"
  290. assert re.search(r"\b(b.)\b",
  291. "abcd abc bcd bx", re.UNICODE).group(1) == "bx"
  292. assert re.search(r"\B(b.)\B",
  293. "abc bcd bc abxd", re.UNICODE).group(1) == "bx"
  294. assert re.search(r"^abc$", "\nabc\n", re.M).group(0) == "abc"
  295. assert re.search(r"^\Aabc\Z$", "abc", re.M).group(0) == "abc"
  296. assert re.search(r"^\Aabc\Z$", "\nabc\n", re.M) == None
  297. assert re.search(r"\b(b.)\b",
  298. u"abcd abc bcd bx").group(1) == "bx"
  299. assert re.search(r"\B(b.)\B",
  300. u"abc bcd bc abxd").group(1) == "bx"
  301. assert re.search(r"^abc$", u"\nabc\n", re.M).group(0) == "abc"
  302. assert re.search(r"^\Aabc\Z$", u"abc", re.M).group(0) == "abc"
  303. assert re.search(r"^\Aabc\Z$", u"\nabc\n", re.M) == None
  304. assert re.search(r"\d\D\w\W\s\S",
  305. "1aa! a").group(0) == "1aa! a"
  306. assert re.search(r"\d\D\w\W\s\S",
  307. "1aa! a", re.LOCALE).group(0) == "1aa! a"
  308. assert re.search(r"\d\D\w\W\s\S",
  309. "1aa! a", re.UNICODE).group(0) == "1aa! a"
  310. def test_ignore_case(self):
  311. assert re.match("abc", "ABC", re.I).group(0) == "ABC"
  312. assert re.match("abc", u"ABC", re.I).group(0) == "ABC"
  313. def test_bigcharset(self):
  314. assert re.match(u"([\u2222\u2223])",
  315. u"\u2222").group(1) == u"\u2222"
  316. assert re.match(u"([\u2222\u2223])",
  317. u"\u2222", re.UNICODE).group(1) == u"\u2222"
  318. def test_anyall(self):
  319. assert re.match("a.b", "a\nb", re.DOTALL).group(0) == (
  320. "a\nb")
  321. assert re.match("a.*b", "a\n\nb", re.DOTALL).group(0) == (
  322. "a\n\nb")
  323. def test_non_consuming(self):
  324. assert re.match("(a(?=\s[^a]))", "a b").group(1) == "a"
  325. assert re.match("(a(?=\s[^a]*))", "a b").group(1) == "a"
  326. assert re.match("(a(?=\s[abc]))", "a b").group(1) == "a"
  327. assert re.match("(a(?=\s[abc]*))", "a bc").group(1) == "a"
  328. assert re.match(r"(a)(?=\s\1)", "a a").group(1) == "a"
  329. assert re.match(r"(a)(?=\s\1*)", "a aa").group(1) == "a"
  330. assert re.match(r"(a)(?=\s(abc|a))", "a a").group(1) == "a"
  331. assert re.match(r"(a(?!\s[^a]))", "a a").group(1) == "a"
  332. assert re.match(r"(a(?!\s[abc]))", "a d").group(1) == "a"
  333. assert re.match(r"(a)(?!\s\1)", "a b").group(1) == "a"
  334. assert re.match(r"(a)(?!\s(abc|a))", "a b").group(1) == "a"
  335. def test_ignore_case(self):
  336. assert re.match(r"(a\s[^a])", "a b", re.I).group(1) == "a b"
  337. assert re.match(r"(a\s[^a]*)", "a bb", re.I).group(1) == "a bb"
  338. assert re.match(r"(a\s[abc])", "a b", re.I).group(1) == "a b"
  339. assert re.match(r"(a\s[abc]*)", "a bb", re.I).group(1) == "a bb"
  340. assert re.match(r"((a)\s\2)", "a a", re.I).group(1) == "a a"
  341. assert re.match(r"((a)\s\2*)", "a aa", re.I).group(1) == "a aa"
  342. assert re.match(r"((a)\s(abc|a))", "a a", re.I).group(1) == "a a"
  343. assert re.match(r"((a)\s(abc|a)*)", "a aa", re.I).group(1) == "a aa"
  344. def test_category(self):
  345. assert re.match(r"(\s)", " ").group(1) == " "
  346. def test_getlower(self):
  347. import _sre
  348. assert _sre.getlower(ord('A'), 0) == ord('a')
  349. assert _sre.getlower(ord('A'), re.LOCALE) == ord('a')
  350. assert _sre.getlower(ord('A'), re.UNICODE) == ord('a')
  351. assert re.match("abc", "ABC", re.I).group(0) == "ABC"
  352. assert re.match("abc", u"ABC", re.I).group(0) == "ABC"
  353. def test_not_literal(self):
  354. assert re.search("\s([^a])", " b").group(1) == "b"
  355. assert re.search("\s([^a]*)", " bb").group(1) == "bb"
  356. def test_search_coverage(self):
  357. assert re.search("\s(b)", " b").group(1) == "b"
  358. assert re.search("a\s", "a ").group(0) == "a "
  359. def test_re_escape(self):
  360. p=""
  361. for i in range(0, 256):
  362. p = p + chr(i)
  363. assert re.match(re.escape(chr(i)), chr(i)) is not None
  364. assert re.match(re.escape(chr(i)), chr(i)).span() == (0,1)
  365. pat=re.compile(re.escape(p))
  366. assert pat.match(p) is not None
  367. assert pat.match(p).span() == (0,256)
  368. def test_pickling(self):
  369. import pickle
  370. self.pickle_test(pickle)
  371. import cPickle
  372. self.pickle_test(cPickle)
  373. # old pickles expect the _compile() reconstructor in sre module
  374. import warnings
  375. original_filters = warnings.filters[:]
  376. try:
  377. warnings.filterwarnings("ignore", "The sre module is deprecated",
  378. DeprecationWarning)
  379. from sre import _compile
  380. finally:
  381. warnings.filters = original_filters
  382. def pickle_test(self, pickle):
  383. oldpat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
  384. s = pickle.dumps(oldpat)
  385. newpat = pickle.loads(s)
  386. # Not using object identity for _sre.py, since some Python builds do
  387. # not seem to preserve that in all cases (observed on an UCS-4 build
  388. # of 2.4.1).
  389. #self.assertEqual(oldpat, newpat)
  390. assert oldpat.__dict__ == newpat.__dict__
  391. def test_constants(self):
  392. assert re.I == re.IGNORECASE
  393. assert re.L == re.LOCALE
  394. assert re.M == re.MULTILINE
  395. assert re.S == re.DOTALL
  396. assert re.X == re.VERBOSE
  397. def test_flags(self):
  398. for flag in [re.I, re.M, re.X, re.S, re.L]:
  399. assert re.compile('^pattern$', flag) != None
  400. def test_sre_character_literals(self):
  401. for i in [0, 8, 16, 32, 64, 127, 128, 255]:
  402. assert re.match(r"\%03o" % i, chr(i)) != None
  403. assert re.match(r"\%03o0" % i, chr(i)+"0") != None
  404. assert re.match(r"\%03o8" % i, chr(i)+"8") != None
  405. assert re.match(r"\x%02x" % i, chr(i)) != None
  406. assert re.match(r"\x%02x0" % i, chr(i)+"0") != None
  407. assert re.match(r"\x%02xz" % i, chr(i)+"z") != None
  408. py.test.raises(re.error, re.match, "\911", "")
  409. def test_sre_character_class_literals(self):
  410. for i in [0, 8, 16, 32, 64, 127, 128, 255]:
  411. assert re.match(r"[\%03o]" % i, chr(i)) != None
  412. assert re.match(r"[\%03o0]" % i, chr(i)) != None
  413. assert re.match(r"[\%03o8]" % i, chr(i)) != None
  414. assert re.match(r"[\x%02x]" % i, chr(i)) != None
  415. assert re.match(r"[\x%02x0]" % i, chr(i)) != None
  416. assert re.match(r"[\x%02xz]" % i, chr(i)) != None
  417. py.test.raises(re.error, re.match, "[\911]", "")
  418. def test_bug_113254(self):
  419. assert re.match(r'(a)|(b)', 'b').start(1) == -1
  420. assert re.match(r'(a)|(b)', 'b').end(1) == -1
  421. assert re.match(r'(a)|(b)', 'b').span(1) == (-1, -1)
  422. def test_bug_527371(self):
  423. # bug described in patches 527371/672491
  424. assert re.match(r'(a)?a','a').lastindex == None
  425. assert re.match(r'(a)(b)?b','ab').lastindex == 1
  426. assert re.match(r'(?P<a>a)(?P<b>b)?b','ab').lastgroup == 'a'
  427. assert re.match("(?P<a>a(b))", "ab").lastgroup == 'a'
  428. assert re.match("((a))", "a").lastindex == 1
  429. def test_bug_545855(self):
  430. # bug 545855 -- This pattern failed to cause a compile error as it
  431. # should, instead provoking a TypeError.
  432. py.test.raises(re.error, re.compile, 'foo[a-')
  433. def test_bug_418626(self):
  434. # bugs 418626 at al. -- Testing Greg Chapman's addition of op code
  435. # SRE_OP_MIN_REPEAT_ONE for eliminating recursion on simple uses of
  436. # pattern '*?' on a long string.
  437. assert re.match('.*?c', 10000*'ab'+'cd').end(0) == 20001
  438. assert re.match('.*?cd', 5000*'ab'+'c'+5000*'ab'+'cde').end(0) == (
  439. 20003)
  440. assert re.match('.*?cd', 20000*'abc'+'de').end(0) == 60001
  441. # non-simple '*?' still used to hit the recursion limit, before the
  442. # non-recursive scheme was implemented.
  443. assert re.search('(a|b)*?c', 10000*'ab'+'cd').end(0) == 20001
  444. def test_bug_612074(self):
  445. pat=u"["+re.escape(u"\u2039")+u"]"
  446. assert re.compile(pat) and 1 == 1
  447. def test_stack_overflow(self):
  448. # nasty cases that used to overflow the straightforward recursive
  449. # implementation of repeated groups.
  450. assert re.match('(x)*', 50000*'x').group(1) == 'x'
  451. assert re.match('(x)*y', 50000*'x'+'y').group(1) == 'x'
  452. assert re.match('(x)*?y', 50000*'x'+'y').group(1) == 'x'
  453. def test_scanner(self):
  454. def s_ident(scanner, token): return token
  455. def s_operator(scanner, token): return "op%s" % token
  456. def s_float(scanner, token): return float(token)
  457. def s_int(scanner, token): return int(token)
  458. scanner = re.Scanner([
  459. (r"[a-zA-Z_]\w*", s_ident),
  460. (r"\d+\.\d*", s_float),
  461. (r"\d+", s_int),
  462. (r"=|\+|-|\*|/", s_operator),
  463. (r"\s+", None),
  464. ])
  465. assert scanner.scanner.scanner("").pattern != None
  466. assert scanner.scan("sum = 3*foo + 312.50 + bar") == (
  467. (['sum', 'op=', 3, 'op*', 'foo', 'op+', 312.5,
  468. 'op+', 'bar'], ''))
  469. def test_bug_448951(self):
  470. # bug 448951 (similar to 429357, but with single char match)
  471. # (Also test greedy matches.)
  472. for op in '','?','*':
  473. assert re.match(r'((.%s):)?z'%op, 'z').groups() == (
  474. (None, None))
  475. assert re.match(r'((.%s):)?z'%op, 'a:z').groups() == (
  476. ('a:', 'a'))
  477. def test_bug_725106(self):
  478. # capturing groups in alternatives in repeats
  479. assert re.match('^((a)|b)*', 'abc').groups() == (
  480. ('b', 'a'))
  481. assert re.match('^(([ab])|c)*', 'abc').groups() == (
  482. ('c', 'b'))
  483. assert re.match('^((d)|[ab])*', 'abc').groups() == (
  484. ('b', None))
  485. assert re.match('^((a)c|[ab])*', 'abc').groups() == (
  486. ('b', None))
  487. assert re.match('^((a)|b)*?c', 'abc').groups() == (
  488. ('b', 'a'))
  489. assert re.match('^(([ab])|c)*?d', 'abcd').groups() == (
  490. ('c', 'b'))
  491. assert re.match('^((d)|[ab])*?c', 'abc').groups() == (
  492. ('b', None))
  493. assert re.match('^((a)c|[ab])*?c', 'abc').groups() == (
  494. ('b', None))
  495. def test_bug_725149(self):
  496. # mark_stack_base restoring before restoring marks
  497. assert re.match('(a)(?:(?=(b)*)c)*', 'abb').groups() == (
  498. ('a', None))
  499. assert re.match('(a)((?!(b)*))*', 'abb').groups() == (
  500. ('a', None, None))
  501. def test_bug_764548(self):
  502. # bug 764548, re.compile() barfs on str/unicode subclasses
  503. try:
  504. unicode
  505. except NameError:
  506. return # no problem if we have no unicode
  507. class my_unicode(unicode): pass
  508. pat = re.compile(my_unicode("abc"))
  509. assert pat.match("xyz") == None
  510. def test_finditer(self):
  511. iter = re.finditer(r":+", "a:b::c:::d")
  512. assert [item.group(0) for item in iter] == (
  513. [":", "::", ":::"])
  514. def test_bug_926075(self):
  515. try:
  516. unicode
  517. except NameError:
  518. return # no problem if we have no unicode
  519. assert (re.compile('bug_926075') is not
  520. re.compile(eval("u'bug_926075'")))
  521. def test_bug_931848(self):
  522. try:
  523. unicode
  524. except NameError:
  525. pass
  526. pattern = eval('u"[\u002E\u3002\uFF0E\uFF61]"')
  527. assert re.compile(pattern).split("a.b.c") == (
  528. ['a','b','c'])
  529. def test_bug_581080(self):
  530. iter = re.finditer(r"\s", "a b")
  531. assert iter.next().span() == (1,2)
  532. py.test.raises(StopIteration, iter.next)
  533. if 0: # XXX
  534. scanner = re.compile(r"\s").scanner("a b")
  535. assert scanner.search().span() == (1, 2)
  536. assert scanner.search() == None
  537. def test_bug_817234(self):
  538. iter = re.finditer(r".*", "asdf")
  539. assert iter.next().span() == (0, 4)
  540. assert iter.next().span() == (4, 4)
  541. py.test.raises(StopIteration, iter.next)
  542. def test_empty_array(self):
  543. # SF buf 1647541
  544. import array
  545. for typecode in 'cbBuhHiIlLfd':
  546. a = array.array(typecode)
  547. assert re.compile("bla").match(a) == None
  548. assert re.compile("").match(a).groups() == ()
  549. def test_inline_flags(self):
  550. # Bug #1700
  551. upper_char = unichr(0x1ea0) # Latin Capital Letter A with Dot Bellow
  552. lower_char = unichr(0x1ea1) # Latin Small Letter A with Dot Bellow
  553. p = re.compile(upper_char, re.I | re.U)
  554. q = p.match(lower_char)
  555. assert q != None
  556. p = re.compile(lower_char, re.I | re.U)
  557. q = p.match(upper_char)
  558. assert q != None
  559. p = re.compile('(?i)' + upper_char, re.U)
  560. q = p.match(lower_char)
  561. assert q != None
  562. p = re.compile('(?i)' + lower_char, re.U)
  563. q = p.match(upper_char)
  564. assert q != None
  565. p = re.compile('(?iu)' + upper_char)
  566. q = p.match(lower_char)
  567. assert q != None
  568. p = re.compile('(?iu)' + lower_char)
  569. q = p.match(upper_char)
  570. assert q != None