diff -r 8b1ac1a3d007 Doc/library/re.rst --- a/Doc/library/re.rst Wed Oct 08 22:32:50 2014 +0300 +++ b/Doc/library/re.rst Thu Oct 09 11:45:02 2014 +0300 @@ -234,6 +234,9 @@ The special characters are: If there are non-whitespace characters before the flag, the results are undefined. + .. deprecated-removed:: 3.5 3.7 + Flags should not be used in nested subexpression. + ``(?:...)`` A non-capturing version of regular parentheses. Matches whatever regular expression is inside the parentheses, but the substring matched by the group diff -r 8b1ac1a3d007 Lib/sre_parse.py --- a/Lib/sre_parse.py Wed Oct 08 22:32:50 2014 +0300 +++ b/Lib/sre_parse.py Thu Oct 09 11:45:02 2014 +0300 @@ -377,7 +377,7 @@ def _parse_sub(source, state, nested=1): itemsappend = items.append sourcematch = source.match while 1: - itemsappend(_parse(source, state)) + itemsappend(_parse(source, state, nested)) if sourcematch("|"): continue if not nested: @@ -430,9 +430,9 @@ def _parse_sub(source, state, nested=1): return subpattern def _parse_sub_cond(source, state, condgroup): - item_yes = _parse(source, state) + item_yes = _parse(source, state, 1) if source.match("|"): - item_no = _parse(source, state) + item_no = _parse(source, state, 1) if source.match("|"): raise error("conditional backref with more than two branches") else: @@ -448,7 +448,7 @@ def _parse_sub_cond(source, state, condg _LOOKBEHINDASSERTCHARS = set("=!") _REPEATCODES = set([MIN_REPEAT, MAX_REPEAT]) -def _parse(source, state): +def _parse(source, state, nested): # parse a simple pattern subpattern = SubPattern(state) @@ -707,6 +707,11 @@ def _parse(source, state): raise error("the group number is too large") else: # flags + if nested: + import warnings + warnings.warn('Flags in nested suppattern. ' + 'Will be error in 3.7', + DeprecationWarning, stacklevel=7) if not source.next in FLAGS: raise error("unexpected end of pattern") while source.next in FLAGS: diff -r 8b1ac1a3d007 Lib/test/test_re.py --- a/Lib/test/test_re.py Wed Oct 08 22:32:50 2014 +0300 +++ b/Lib/test/test_re.py Thu Oct 09 11:45:02 2014 +0300 @@ -1001,6 +1001,16 @@ class ReTests(unittest.TestCase): q = p.match(upper_char) self.assertTrue(q) + def test_nested_inline_flags(self): + with self.assertWarns(DeprecationWarning): + p = re.compile('(?:(?i))') + with self.assertWarns(DeprecationWarning): + p = re.compile('()(?(1)(?i))') + with self.assertWarns(DeprecationWarning): + p = re.compile('()(?(1)|(?i))') + with self.assertWarns(DeprecationWarning): + p = re.compile('(?=(?i))') + def test_dollar_matches_twice(self): "$ matches the end of string, and just before the terminating \n" pattern = re.compile('$')