]> granicus.if.org Git - python/commitdiff
Issue #20426: When passing the re.DEBUG flag, re.compile() displays the debug output...
authorAntoine Pitrou <solipsis@pitrou.net>
Mon, 3 Feb 2014 20:01:35 +0000 (21:01 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Mon, 3 Feb 2014 20:01:35 +0000 (21:01 +0100)
1  2 
Lib/re.py
Lib/test/test_re.py
Misc/NEWS

diff --cc Lib/re.py
Simple merge
index a4e11c6746ccbf3dcaaea3fb6e20e0f844eda89e,1c6f45dfa652522ba8b33c4230320d58dbbe633b..a229e235ca202a22daa5411bcd483ae8ba221171
@@@ -1193,84 -1064,19 +1193,96 @@@ class ReTests(unittest.TestCase)
                  self.assertEqual(m.group(1), "")
                  self.assertEqual(m.group(2), "y")
  
+     def test_debug_flag(self):
+         with captured_stdout() as out:
+             re.compile('foo', re.DEBUG)
+         self.assertEqual(out.getvalue().splitlines(),
+                          ['literal 102 ', 'literal 111 ', 'literal 111 '])
+         # Debug output is output again even a second time (bypassing
+         # the cache -- issue #20426).
+         with captured_stdout() as out:
+             re.compile('foo', re.DEBUG)
+         self.assertEqual(out.getvalue().splitlines(),
+                          ['literal 102 ', 'literal 111 ', 'literal 111 '])
  
 +class PatternReprTests(unittest.TestCase):
 +    def check(self, pattern, expected):
 +        self.assertEqual(repr(re.compile(pattern)), expected)
 +
 +    def check_flags(self, pattern, flags, expected):
 +        self.assertEqual(repr(re.compile(pattern, flags)), expected)
 +
 +    def test_without_flags(self):
 +        self.check('random pattern',
 +                   "re.compile('random pattern')")
 +
 +    def test_single_flag(self):
 +        self.check_flags('random pattern', re.IGNORECASE,
 +            "re.compile('random pattern', re.IGNORECASE)")
 +
 +    def test_multiple_flags(self):
 +        self.check_flags('random pattern', re.I|re.S|re.X,
 +            "re.compile('random pattern', "
 +            "re.IGNORECASE|re.DOTALL|re.VERBOSE)")
 +
 +    def test_unicode_flag(self):
 +        self.check_flags('random pattern', re.U,
 +                         "re.compile('random pattern')")
 +        self.check_flags('random pattern', re.I|re.S|re.U,
 +                         "re.compile('random pattern', "
 +                         "re.IGNORECASE|re.DOTALL)")
 +
 +    def test_inline_flags(self):
 +        self.check('(?i)pattern',
 +                   "re.compile('(?i)pattern', re.IGNORECASE)")
 +
 +    def test_unknown_flags(self):
 +        self.check_flags('random pattern', 0x123000,
 +                         "re.compile('random pattern', 0x123000)")
 +        self.check_flags('random pattern', 0x123000|re.I,
 +            "re.compile('random pattern', re.IGNORECASE|0x123000)")
 +
 +    def test_bytes(self):
 +        self.check(b'bytes pattern',
 +                   "re.compile(b'bytes pattern')")
 +        self.check_flags(b'bytes pattern', re.A,
 +                         "re.compile(b'bytes pattern', re.ASCII)")
 +
 +    def test_quotes(self):
 +        self.check('random "double quoted" pattern',
 +            '''re.compile('random "double quoted" pattern')''')
 +        self.check("random 'single quoted' pattern",
 +            '''re.compile("random 'single quoted' pattern")''')
 +        self.check('''both 'single' and "double" quotes''',
 +            '''re.compile('both \\'single\\' and "double" quotes')''')
 +
 +    def test_long_pattern(self):
 +        pattern = 'Very %spattern' % ('long ' * 1000)
 +        r = repr(re.compile(pattern))
 +        self.assertLess(len(r), 300)
 +        self.assertEqual(r[:30], "re.compile('Very long long lon")
 +        r = repr(re.compile(pattern, re.I))
 +        self.assertLess(len(r), 300)
 +        self.assertEqual(r[:30], "re.compile('Very long long lon")
 +        self.assertEqual(r[-16:], ", re.IGNORECASE)")
 +
 +
 +class ImplementationTest(unittest.TestCase):
 +    """
 +    Test implementation details of the re module.
 +    """
 +
 +    def test_overlap_table(self):
 +        f = sre_compile._generate_overlap_table
 +        self.assertEqual(f(""), [])
 +        self.assertEqual(f("a"), [0])
 +        self.assertEqual(f("abcd"), [0, 0, 0, 0])
 +        self.assertEqual(f("aaaa"), [0, 1, 2, 3])
 +        self.assertEqual(f("ababba"), [0, 0, 1, 2, 0, 1])
 +        self.assertEqual(f("abcabdac"), [0, 0, 0, 1, 2, 0, 1, 0])
 +
 +
  def run_re_tests():
      from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR
      if verbose:
diff --cc Misc/NEWS
Simple merge