PageRenderTime 62ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/rlib/rsre/test/test_re.py

https://bitbucket.org/SeanTater/pypy-bugfix-st
Python | 660 lines | 628 code | 18 blank | 14 comment | 3 complexity | 16f37ce588320cd7e67235d018a2b486 MD5 | raw file
  1. import sys, os
  2. from pypy.rlib.rsre.test.test_match import get_code
  3. from pypy.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. raises(re.error, re.sub, 'x', r'\1', 'x')
  98. raises(re.error, re.sub, 'x', r'\8', 'x')
  99. raises(re.error, re.sub, 'x', r'\9', 'x')
  100. raises(re.error, re.sub, 'x', r'\11', 'x')
  101. raises(re.error, re.sub, 'x', r'\18', 'x')
  102. raises(re.error, re.sub, 'x', r'\1a', 'x')
  103. raises(re.error, re.sub, 'x', r'\90', 'x')
  104. raises(re.error, re.sub, 'x', r'\99', 'x')
  105. raises(re.error, re.sub, 'x', r'\118', 'x') # r'\11' + '8'
  106. raises(re.error, re.sub, 'x', r'\11a', 'x')
  107. raises(re.error, re.sub, 'x', r'\181', 'x') # r'\18' + '1'
  108. 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. raises(re.error, re.sub, '(?P<a>x)', '\g<a', 'xx')
  127. raises(re.error, re.sub, '(?P<a>x)', '\g<', 'xx')
  128. raises(re.error, re.sub, '(?P<a>x)', '\g', 'xx')
  129. raises(re.error, re.sub, '(?P<a>x)', '\g<a a>', 'xx')
  130. raises(re.error, re.sub, '(?P<a>x)', '\g<1a1>', 'xx')
  131. raises(IndexError, re.sub, '(?P<a>x)', '\g<ab>', 'xx')
  132. raises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\g<b>', 'xx')
  133. raises(re.error, re.sub, '(?P<a>x)|(?P<b>y)', '\\2', 'xx')
  134. 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. assert re.findall("(:)(:*)", "a:b::c:::d") == [(":", ""),
  168. (":", ":"),
  169. (":", "::")]
  170. def test_bug_117612(self):
  171. assert re.findall(r"(a|(b))", "aba") == (
  172. [("a", ""),("b", "b"),("a", "")])
  173. def test_re_match(self):
  174. assert re.match('a', 'a').groups() == ()
  175. assert re.match('(a)', 'a').groups() == ('a',)
  176. assert re.match(r'(a)', 'a').group(0) == 'a'
  177. assert re.match(r'(a)', 'a').group(1) == 'a'
  178. #assert re.match(r'(a)', 'a').group(1, 1) == ('a', 'a')
  179. pat = re.compile('((a)|(b))(c)?')
  180. assert pat.match('a').groups() == ('a', 'a', None, None)
  181. assert pat.match('b').groups() == ('b', None, 'b', None)
  182. assert pat.match('ac').groups() == ('a', 'a', None, 'c')
  183. assert pat.match('bc').groups() == ('b', None, 'b', 'c')
  184. assert pat.match('bc').groups("") == ('b', "", 'b', 'c')
  185. # A single group
  186. m = re.match('(a)', 'a')
  187. assert m.group(0) == 'a'
  188. assert m.group(0) == 'a'
  189. assert m.group(1) == 'a'
  190. #assert m.group(1, 1) == ('a', 'a')
  191. pat = re.compile('(?:(?P<a1>a)|(?P<b2>b))(?P<c3>c)?')
  192. #assert pat.match('a').group(1, 2, 3) == ('a', None, None)
  193. #assert pat.match('b').group('a1', 'b2', 'c3') == (
  194. # (None, 'b', None))
  195. #assert pat.match('ac').group(1, 'b2', 3) == ('a', None, 'c')
  196. def test_bug_923(self):
  197. # Issue923: grouping inside optional lookahead problem
  198. assert re.match(r'a(?=(b))?', "ab").groups() == ("b",)
  199. assert re.match(r'(a(?=(b))?)', "ab").groups() == ('a', 'b')
  200. assert re.match(r'(a)(?=(b))?', "ab").groups() == ('a', 'b')
  201. assert re.match(r'(?P<g1>a)(?=(?P<g2>b))?', "ab").groupdict() == {'g1': 'a', 'g2': 'b'}
  202. def test_re_groupref_exists(self):
  203. assert re.match('^(\()?([^()]+)(?(1)\))$', '(a)').groups() == (
  204. ('(', 'a'))
  205. assert re.match('^(\()?([^()]+)(?(1)\))$', 'a').groups() == (
  206. (None, 'a'))
  207. assert re.match('^(\()?([^()]+)(?(1)\))$', 'a)') == None
  208. assert re.match('^(\()?([^()]+)(?(1)\))$', '(a') == None
  209. assert re.match('^(?:(a)|c)((?(1)b|d))$', 'ab').groups() == (
  210. ('a', 'b'))
  211. assert re.match('^(?:(a)|c)((?(1)b|d))$', 'cd').groups() == (
  212. (None, 'd'))
  213. assert re.match('^(?:(a)|c)((?(1)|d))$', 'cd').groups() == (
  214. (None, 'd'))
  215. assert re.match('^(?:(a)|c)((?(1)|d))$', 'a').groups() == (
  216. ('a', ''))
  217. # Tests for bug #1177831: exercise groups other than the first group
  218. p = re.compile('(?P<g1>a)(?P<g2>b)?((?(g2)c|d))')
  219. assert p.match('abc').groups() == (
  220. ('a', 'b', 'c'))
  221. assert p.match('ad').groups() == (
  222. ('a', None, 'd'))
  223. assert p.match('abd') == None
  224. assert p.match('ac') == None
  225. def test_re_groupref(self):
  226. assert re.match(r'^(\|)?([^()]+)\1$', '|a|').groups() == (
  227. ('|', 'a'))
  228. assert re.match(r'^(\|)?([^()]+)\1?$', 'a').groups() == (
  229. (None, 'a'))
  230. assert re.match(r'^(\|)?([^()]+)\1$', 'a|') == None
  231. assert re.match(r'^(\|)?([^()]+)\1$', '|a') == None
  232. assert re.match(r'^(?:(a)|c)(\1)$', 'aa').groups() == (
  233. ('a', 'a'))
  234. assert re.match(r'^(?:(a)|c)(\1)?$', 'c').groups() == (
  235. (None, None))
  236. def test_groupdict(self):
  237. assert re.match('(?P<first>first) (?P<second>second)',
  238. 'first second').groupdict() == (
  239. {'first':'first', 'second':'second'})
  240. def test_expand(self):
  241. assert (re.match("(?P<first>first) (?P<second>second)",
  242. "first second")
  243. .expand(r"\2 \1 \g<second> \g<first>")) == (
  244. "second first second first")
  245. def test_repeat_minmax(self):
  246. assert re.match("^(\w){1}$", "abc") == None
  247. assert re.match("^(\w){1}?$", "abc") == None
  248. assert re.match("^(\w){1,2}$", "abc") == None
  249. assert re.match("^(\w){1,2}?$", "abc") == None
  250. assert re.match("^(\w){3}$", "abc").group(1) == "c"
  251. assert re.match("^(\w){1,3}$", "abc").group(1) == "c"
  252. assert re.match("^(\w){1,4}$", "abc").group(1) == "c"
  253. assert re.match("^(\w){3,4}?$", "abc").group(1) == "c"
  254. assert re.match("^(\w){3}?$", "abc").group(1) == "c"
  255. assert re.match("^(\w){1,3}?$", "abc").group(1) == "c"
  256. assert re.match("^(\w){1,4}?$", "abc").group(1) == "c"
  257. assert re.match("^(\w){3,4}?$", "abc").group(1) == "c"
  258. assert re.match("^x{1}$", "xxx") == None
  259. assert re.match("^x{1}?$", "xxx") == None
  260. assert re.match("^x{1,2}$", "xxx") == None
  261. assert re.match("^x{1,2}?$", "xxx") == None
  262. assert re.match("^x{3}$", "xxx") != None
  263. assert re.match("^x{1,3}$", "xxx") != None
  264. assert re.match("^x{1,4}$", "xxx") != None
  265. assert re.match("^x{3,4}?$", "xxx") != None
  266. assert re.match("^x{3}?$", "xxx") != None
  267. assert re.match("^x{1,3}?$", "xxx") != None
  268. assert re.match("^x{1,4}?$", "xxx") != None
  269. assert re.match("^x{3,4}?$", "xxx") != None
  270. assert re.match("^x{}$", "xxx") == None
  271. assert re.match("^x{}$", "x{}") != None
  272. def test_getattr(self):
  273. assert re.match("(a)", "a").pos == 0
  274. assert re.match("(a)", "a").endpos == 1
  275. assert re.match("(a)", "a").string == "a"
  276. assert re.match("(a)", "a").regs == ((0, 1), (0, 1))
  277. assert re.match("(a)", "a").re != None
  278. def test_special_escapes(self):
  279. assert re.search(r"\b(b.)\b",
  280. "abcd abc bcd bx").group(1) == "bx"
  281. assert re.search(r"\B(b.)\B",
  282. "abc bcd bc abxd").group(1) == "bx"
  283. assert re.search(r"\b(b.)\b",
  284. "abcd abc bcd bx", re.LOCALE).group(1) == "bx"
  285. assert re.search(r"\B(b.)\B",
  286. "abc bcd bc abxd", re.LOCALE).group(1) == "bx"
  287. assert re.search(r"\b(b.)\b",
  288. "abcd abc bcd bx", re.UNICODE).group(1) == "bx"
  289. assert re.search(r"\B(b.)\B",
  290. "abc bcd bc abxd", re.UNICODE).group(1) == "bx"
  291. assert re.search(r"^abc$", "\nabc\n", re.M).group(0) == "abc"
  292. assert re.search(r"^\Aabc\Z$", "abc", re.M).group(0) == "abc"
  293. assert re.search(r"^\Aabc\Z$", "\nabc\n", re.M) == None
  294. assert re.search(r"\b(b.)\b",
  295. u"abcd abc bcd bx").group(1) == "bx"
  296. assert re.search(r"\B(b.)\B",
  297. u"abc bcd bc abxd").group(1) == "bx"
  298. assert re.search(r"^abc$", u"\nabc\n", re.M).group(0) == "abc"
  299. assert re.search(r"^\Aabc\Z$", u"abc", re.M).group(0) == "abc"
  300. assert re.search(r"^\Aabc\Z$", u"\nabc\n", re.M) == None
  301. assert re.search(r"\d\D\w\W\s\S",
  302. "1aa! a").group(0) == "1aa! a"
  303. assert re.search(r"\d\D\w\W\s\S",
  304. "1aa! a", re.LOCALE).group(0) == "1aa! a"
  305. assert re.search(r"\d\D\w\W\s\S",
  306. "1aa! a", re.UNICODE).group(0) == "1aa! a"
  307. def test_ignore_case(self):
  308. assert re.match("abc", "ABC", re.I).group(0) == "ABC"
  309. assert re.match("abc", u"ABC", re.I).group(0) == "ABC"
  310. def test_bigcharset(self):
  311. assert re.match(u"([\u2222\u2223])",
  312. u"\u2222").group(1) == u"\u2222"
  313. assert re.match(u"([\u2222\u2223])",
  314. u"\u2222", re.UNICODE).group(1) == u"\u2222"
  315. def test_anyall(self):
  316. assert re.match("a.b", "a\nb", re.DOTALL).group(0) == (
  317. "a\nb")
  318. assert re.match("a.*b", "a\n\nb", re.DOTALL).group(0) == (
  319. "a\n\nb")
  320. def test_non_consuming(self):
  321. assert re.match("(a(?=\s[^a]))", "a b").group(1) == "a"
  322. assert re.match("(a(?=\s[^a]*))", "a b").group(1) == "a"
  323. assert re.match("(a(?=\s[abc]))", "a b").group(1) == "a"
  324. assert re.match("(a(?=\s[abc]*))", "a bc").group(1) == "a"
  325. assert re.match(r"(a)(?=\s\1)", "a a").group(1) == "a"
  326. assert re.match(r"(a)(?=\s\1*)", "a aa").group(1) == "a"
  327. assert re.match(r"(a)(?=\s(abc|a))", "a a").group(1) == "a"
  328. assert re.match(r"(a(?!\s[^a]))", "a a").group(1) == "a"
  329. assert re.match(r"(a(?!\s[abc]))", "a d").group(1) == "a"
  330. assert re.match(r"(a)(?!\s\1)", "a b").group(1) == "a"
  331. assert re.match(r"(a)(?!\s(abc|a))", "a b").group(1) == "a"
  332. def test_ignore_case(self):
  333. assert re.match(r"(a\s[^a])", "a b", re.I).group(1) == "a b"
  334. assert re.match(r"(a\s[^a]*)", "a bb", re.I).group(1) == "a bb"
  335. assert re.match(r"(a\s[abc])", "a b", re.I).group(1) == "a b"
  336. assert re.match(r"(a\s[abc]*)", "a bb", re.I).group(1) == "a bb"
  337. assert re.match(r"((a)\s\2)", "a a", re.I).group(1) == "a a"
  338. assert re.match(r"((a)\s\2*)", "a aa", re.I).group(1) == "a aa"
  339. assert re.match(r"((a)\s(abc|a))", "a a", re.I).group(1) == "a a"
  340. assert re.match(r"((a)\s(abc|a)*)", "a aa", re.I).group(1) == "a aa"
  341. def test_category(self):
  342. assert re.match(r"(\s)", " ").group(1) == " "
  343. def test_getlower(self):
  344. import _sre
  345. assert _sre.getlower(ord('A'), 0) == ord('a')
  346. assert _sre.getlower(ord('A'), re.LOCALE) == ord('a')
  347. assert _sre.getlower(ord('A'), re.UNICODE) == ord('a')
  348. assert re.match("abc", "ABC", re.I).group(0) == "ABC"
  349. assert re.match("abc", u"ABC", re.I).group(0) == "ABC"
  350. def test_not_literal(self):
  351. assert re.search("\s([^a])", " b").group(1) == "b"
  352. assert re.search("\s([^a]*)", " bb").group(1) == "bb"
  353. def test_search_coverage(self):
  354. assert re.search("\s(b)", " b").group(1) == "b"
  355. assert re.search("a\s", "a ").group(0) == "a "
  356. def test_re_escape(self):
  357. p=""
  358. for i in range(0, 256):
  359. p = p + chr(i)
  360. assert re.match(re.escape(chr(i)), chr(i)) is not None
  361. assert re.match(re.escape(chr(i)), chr(i)).span() == (0,1)
  362. pat=re.compile(re.escape(p))
  363. assert pat.match(p) is not None
  364. assert pat.match(p).span() == (0,256)
  365. def test_pickling(self):
  366. import pickle
  367. self.pickle_test(pickle)
  368. import cPickle
  369. self.pickle_test(cPickle)
  370. # old pickles expect the _compile() reconstructor in sre module
  371. import warnings
  372. original_filters = warnings.filters[:]
  373. try:
  374. warnings.filterwarnings("ignore", "The sre module is deprecated",
  375. DeprecationWarning)
  376. from sre import _compile
  377. finally:
  378. warnings.filters = original_filters
  379. def pickle_test(self, pickle):
  380. oldpat = re.compile('a(?:b|(c|e){1,2}?|d)+?(.)')
  381. s = pickle.dumps(oldpat)
  382. newpat = pickle.loads(s)
  383. # Not using object identity for _sre.py, since some Python builds do
  384. # not seem to preserve that in all cases (observed on an UCS-4 build
  385. # of 2.4.1).
  386. #self.assertEqual(oldpat, newpat)
  387. assert oldpat.__dict__ == newpat.__dict__
  388. def test_constants(self):
  389. assert re.I == re.IGNORECASE
  390. assert re.L == re.LOCALE
  391. assert re.M == re.MULTILINE
  392. assert re.S == re.DOTALL
  393. assert re.X == re.VERBOSE
  394. def test_flags(self):
  395. for flag in [re.I, re.M, re.X, re.S, re.L]:
  396. assert re.compile('^pattern$', flag) != None
  397. def test_sre_character_literals(self):
  398. for i in [0, 8, 16, 32, 64, 127, 128, 255]:
  399. assert re.match(r"\%03o" % i, chr(i)) != None
  400. assert re.match(r"\%03o0" % i, chr(i)+"0") != None
  401. assert re.match(r"\%03o8" % i, chr(i)+"8") != None
  402. assert re.match(r"\x%02x" % i, chr(i)) != None
  403. assert re.match(r"\x%02x0" % i, chr(i)+"0") != None
  404. assert re.match(r"\x%02xz" % i, chr(i)+"z") != None
  405. raises(re.error, re.match, "\911", "")
  406. def test_sre_character_class_literals(self):
  407. for i in [0, 8, 16, 32, 64, 127, 128, 255]:
  408. assert re.match(r"[\%03o]" % i, chr(i)) != None
  409. assert re.match(r"[\%03o0]" % i, chr(i)) != None
  410. assert re.match(r"[\%03o8]" % i, chr(i)) != None
  411. assert re.match(r"[\x%02x]" % i, chr(i)) != None
  412. assert re.match(r"[\x%02x0]" % i, chr(i)) != None
  413. assert re.match(r"[\x%02xz]" % i, chr(i)) != None
  414. raises(re.error, re.match, "[\911]", "")
  415. def test_bug_113254(self):
  416. assert re.match(r'(a)|(b)', 'b').start(1) == -1
  417. assert re.match(r'(a)|(b)', 'b').end(1) == -1
  418. assert re.match(r'(a)|(b)', 'b').span(1) == (-1, -1)
  419. def test_bug_527371(self):
  420. # bug described in patches 527371/672491
  421. assert re.match(r'(a)?a','a').lastindex == None
  422. assert re.match(r'(a)(b)?b','ab').lastindex == 1
  423. assert re.match(r'(?P<a>a)(?P<b>b)?b','ab').lastgroup == 'a'
  424. assert re.match("(?P<a>a(b))", "ab").lastgroup == 'a'
  425. assert re.match("((a))", "a").lastindex == 1
  426. def test_bug_545855(self):
  427. # bug 545855 -- This pattern failed to cause a compile error as it
  428. # should, instead provoking a TypeError.
  429. raises(re.error, re.compile, 'foo[a-')
  430. def test_bug_418626(self):
  431. # bugs 418626 at al. -- Testing Greg Chapman's addition of op code
  432. # SRE_OP_MIN_REPEAT_ONE for eliminating recursion on simple uses of
  433. # pattern '*?' on a long string.
  434. assert re.match('.*?c', 10000*'ab'+'cd').end(0) == 20001
  435. assert re.match('.*?cd', 5000*'ab'+'c'+5000*'ab'+'cde').end(0) == (
  436. 20003)
  437. assert re.match('.*?cd', 20000*'abc'+'de').end(0) == 60001
  438. # non-simple '*?' still used to hit the recursion limit, before the
  439. # non-recursive scheme was implemented.
  440. assert re.search('(a|b)*?c', 10000*'ab'+'cd').end(0) == 20001
  441. def test_bug_612074(self):
  442. pat=u"["+re.escape(u"\u2039")+u"]"
  443. assert re.compile(pat) and 1 == 1
  444. def test_stack_overflow(self):
  445. # nasty cases that used to overflow the straightforward recursive
  446. # implementation of repeated groups.
  447. assert re.match('(x)*', 50000*'x').group(1) == 'x'
  448. assert re.match('(x)*y', 50000*'x'+'y').group(1) == 'x'
  449. assert re.match('(x)*?y', 50000*'x'+'y').group(1) == 'x'
  450. def test_scanner(self):
  451. def s_ident(scanner, token): return token
  452. def s_operator(scanner, token): return "op%s" % token
  453. def s_float(scanner, token): return float(token)
  454. def s_int(scanner, token): return int(token)
  455. scanner = re.Scanner([
  456. (r"[a-zA-Z_]\w*", s_ident),
  457. (r"\d+\.\d*", s_float),
  458. (r"\d+", s_int),
  459. (r"=|\+|-|\*|/", s_operator),
  460. (r"\s+", None),
  461. ])
  462. assert scanner.scanner.scanner("").pattern != None
  463. assert scanner.scan("sum = 3*foo + 312.50 + bar") == (
  464. (['sum', 'op=', 3, 'op*', 'foo', 'op+', 312.5,
  465. 'op+', 'bar'], ''))
  466. def test_bug_448951(self):
  467. # bug 448951 (similar to 429357, but with single char match)
  468. # (Also test greedy matches.)
  469. for op in '','?','*':
  470. assert re.match(r'((.%s):)?z'%op, 'z').groups() == (
  471. (None, None))
  472. assert re.match(r'((.%s):)?z'%op, 'a:z').groups() == (
  473. ('a:', 'a'))
  474. def test_bug_725106(self):
  475. # capturing groups in alternatives in repeats
  476. assert re.match('^((a)|b)*', 'abc').groups() == (
  477. ('b', 'a'))
  478. assert re.match('^(([ab])|c)*', 'abc').groups() == (
  479. ('c', 'b'))
  480. assert re.match('^((d)|[ab])*', 'abc').groups() == (
  481. ('b', None))
  482. assert re.match('^((a)c|[ab])*', 'abc').groups() == (
  483. ('b', None))
  484. assert re.match('^((a)|b)*?c', 'abc').groups() == (
  485. ('b', 'a'))
  486. assert re.match('^(([ab])|c)*?d', 'abcd').groups() == (
  487. ('c', 'b'))
  488. assert re.match('^((d)|[ab])*?c', 'abc').groups() == (
  489. ('b', None))
  490. assert re.match('^((a)c|[ab])*?c', 'abc').groups() == (
  491. ('b', None))
  492. def test_bug_725149(self):
  493. # mark_stack_base restoring before restoring marks
  494. assert re.match('(a)(?:(?=(b)*)c)*', 'abb').groups() == (
  495. ('a', None))
  496. assert re.match('(a)((?!(b)*))*', 'abb').groups() == (
  497. ('a', None, None))
  498. def test_bug_764548(self):
  499. # bug 764548, re.compile() barfs on str/unicode subclasses
  500. try:
  501. unicode
  502. except NameError:
  503. return # no problem if we have no unicode
  504. class my_unicode(unicode): pass
  505. pat = re.compile(my_unicode("abc"))
  506. assert pat.match("xyz") == None
  507. def test_finditer(self):
  508. iter = re.finditer(r":+", "a:b::c:::d")
  509. assert [item.group(0) for item in iter] == (
  510. [":", "::", ":::"])
  511. def test_bug_926075(self):
  512. try:
  513. unicode
  514. except NameError:
  515. return # no problem if we have no unicode
  516. assert (re.compile('bug_926075') is not
  517. re.compile(eval("u'bug_926075'")))
  518. def test_bug_931848(self):
  519. try:
  520. unicode
  521. except NameError:
  522. pass
  523. pattern = eval('u"[\u002E\u3002\uFF0E\uFF61]"')
  524. assert re.compile(pattern).split("a.b.c") == (
  525. ['a','b','c'])
  526. def test_bug_581080(self):
  527. iter = re.finditer(r"\s", "a b")
  528. assert iter.next().span() == (1,2)
  529. raises(StopIteration, iter.next)
  530. if 0: # XXX
  531. scanner = re.compile(r"\s").scanner("a b")
  532. assert scanner.search().span() == (1, 2)
  533. assert scanner.search() == None
  534. def test_bug_817234(self):
  535. iter = re.finditer(r".*", "asdf")
  536. assert iter.next().span() == (0, 4)
  537. assert iter.next().span() == (4, 4)
  538. raises(StopIteration, iter.next)
  539. def test_empty_array(self):
  540. # SF buf 1647541
  541. import array
  542. for typecode in 'cbBuhHiIlLfd':
  543. a = array.array(typecode)
  544. assert re.compile("bla").match(a) == None
  545. assert re.compile("").match(a).groups() == ()
  546. def test_inline_flags(self):
  547. # Bug #1700
  548. upper_char = unichr(0x1ea0) # Latin Capital Letter A with Dot Bellow
  549. lower_char = unichr(0x1ea1) # Latin Small Letter A with Dot Bellow
  550. p = re.compile(upper_char, re.I | re.U)
  551. q = p.match(lower_char)
  552. assert q != None
  553. p = re.compile(lower_char, re.I | re.U)
  554. q = p.match(upper_char)
  555. assert q != None
  556. p = re.compile('(?i)' + upper_char, re.U)
  557. q = p.match(lower_char)
  558. assert q != None
  559. p = re.compile('(?i)' + lower_char, re.U)
  560. q = p.match(upper_char)
  561. assert q != None
  562. p = re.compile('(?iu)' + upper_char)
  563. q = p.match(lower_char)
  564. assert q != None
  565. p = re.compile('(?iu)' + lower_char)
  566. q = p.match(upper_char)
  567. assert q != None