[ Web Proxy ]
URL:
Viewing: https://bugs.python.org/file44825/re_check_flags_type.patch [Back]  [Original]

diff -r da2c96cf2ce6 Lib/re.py
--- a/Lib/re.py	Sun Sep 25 20:39:29 2016 +0300
+++ b/Lib/re.py	Sun Sep 25 20:50:41 2016 +0300
@@ -188,6 +188,8 @@ def sub(pattern, repl, string, count=0, 
     if a string, backslash escapes in it are processed.  If it is
     a callable, it's passed the match object and must return
     a replacement string to be used."""
+    if isinstance(count, RegexFlag):
+        raise TypeError("flags argument is used as count")
     return _compile(pattern, flags).sub(repl, string, count)
 
 def subn(pattern, repl, string, count=0, flags=0):
@@ -199,6 +201,8 @@ def subn(pattern, repl, string, count=0,
     callable; if a string, backslash escapes in it are processed.
     If it is a callable, it's passed the match object and must
     return a replacement string to be used."""
+    if isinstance(count, RegexFlag):
+        raise TypeError("flags argument is used as count")
     return _compile(pattern, flags).subn(repl, string, count)
 
 def split(pattern, string, maxsplit=0, flags=0):
@@ -209,6 +213,8 @@ def split(pattern, string, maxsplit=0, f
     list.  If maxsplit is nonzero, at most maxsplit splits occur,
     and the remainder of the string is returned as the final element
     of the list."""
+    if isinstance(maxsplit, RegexFlag):
+        raise TypeError("flags argument is used as maxsplit")
     return _compile(pattern, flags).split(string, maxsplit)
 
 def findall(pattern, string, flags=0):
diff -r da2c96cf2ce6 Lib/test/test_re.py
--- a/Lib/test/test_re.py	Sun Sep 25 20:39:29 2016 +0300
+++ b/Lib/test/test_re.py	Sun Sep 25 20:50:41 2016 +0300
@@ -211,6 +211,11 @@ class ReTests(unittest.TestCase):
         self.assertEqual(re.sub('a', 'b', 'aaaaa', 1), 'baaaa')
         self.assertEqual(re.sub('a', 'b', 'aaaaa', count=1), 'baaaa')
 
+    def test_misuse_flags(self):
+        self.assertRaises(TypeError, re.sub, 'a', 'b', 'aaaaa', re.I)
+        self.assertRaises(TypeError, re.subn, "b*", "x", "xyz", re.I)
+        self.assertRaises(TypeError, re.split, ":", ":a:b::c", re.I)
+
     def test_bug_114660(self):
         self.assertEqual(re.sub(r'(\S)\s+(\S)', r'\1 \2', 'hello  there'),
                          'hello there')

Web Proxy Viewer  |  New URL  |  Original Page