cpython: 5387095b8675 (original) (raw)

Mercurial > cpython

changeset 94715:5387095b8675 2.7

Issues #814253, #9179: Warnings now are raised when group references and conditional group references are used in lookbehind assertions in regular expressions. [#814253]

Serhiy Storchaka storchaka@gmail.com
date Sat, 21 Feb 2015 12:08:36 +0200
parents af8089217cc6
children 4d8e37e54a7d
files Doc/library/re.rst Lib/sre_parse.py Lib/test/test_re.py Misc/NEWS
diffstat 4 files changed, 70 insertions(+), 6 deletions(-)[+] [-] Doc/library/re.rst 7 Lib/sre_parse.py 20 Lib/test/test_re.py 45 Misc/NEWS 4

line wrap: on

line diff

--- a/Doc/library/re.rst +++ b/Doc/library/re.rst @@ -276,7 +276,9 @@ The special characters are: assertion`. (?<=abc)def will find a match in abcdef, since the lookbehind will back up 3 characters and check if the contained pattern matches. The contained pattern must only match strings of some fixed length, meaning that

(?(id/name)yes-pattern|no-pattern)

--- a/Lib/sre_parse.py +++ b/Lib/sre_parse.py @@ -69,6 +69,8 @@ class Pattern: self.open = [] self.groups = 1 self.groupdict = {}

+ def opengroup(self, name=None): gid = self.groups self.groups = gid + 1 @@ -299,6 +301,11 @@ def _escape(source, escape, state): if group < state.groups: if not state.checkgroup(group): raise error, "cannot refer to open group"

@@ -578,6 +585,11 @@ def _parse(source, state): if gid is None: msg = "unknown group name: {0!r}".format(name) raise error(msg)

@@ -606,7 +618,10 @@ def _parse(source, state): raise error, "syntax error" dir = -1 # lookbehind char = sourceget()

@@ -637,6 +652,11 @@ def _parse(source, state): condgroup = int(condname) except ValueError: raise error, "bad character in group name"

--- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -1,7 +1,9 @@

-- coding: utf-8 --

-from test.test_support import verbose, run_unittest, import_module -from test.test_support import precisionbigmemtest, _2G, cpython_only -from test.test_support import captured_stdout, have_unicode, requires_unicode, u +from test.test_support import (

import locale import re from re import Scanner @@ -449,7 +451,7 @@ class ReTests(unittest.TestCase): self.assertEqual(re.match("a.*b", "a\n\nb", re.DOTALL).group(0), "a\n\nb")

@@ -463,6 +465,41 @@ class ReTests(unittest.TestCase): self.assertEqual(re.match(r"(a)(?!\s\1)", "a b").group(1), "a") self.assertEqual(re.match(r"(a)(?!\s(abc|a))", "a b").group(1), "a")

+

+ def test_ignore_case(self): self.assertEqual(re.match("abc", "ABC", re.I).group(0), "ABC") self.assertEqual(re.match("abc", u"ABC", re.I).group(0), "ABC")

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -18,6 +18,10 @@ Core and Builtins Library ------- +- Issues #814253, #9179: Warnings now are raised when group references and