diff options
author | Fredrik Lundh <fredrik@pythonware.com> | 2000-10-28 19:30:41 (GMT) |
---|---|---|
committer | Fredrik Lundh <fredrik@pythonware.com> | 2000-10-28 19:30:41 (GMT) |
commit | ebc37b28fa3fd66336116447b7c2b9b1c2614630 (patch) | |
tree | e977fafcc576a01ac2e489d556990628dd5f4f35 /Lib | |
parent | 8fdb6383dc6ade2eba4c402af571f689bee2b610 (diff) | |
download | cpython-ebc37b28fa3fd66336116447b7c2b9b1c2614630.zip cpython-ebc37b28fa3fd66336116447b7c2b9b1c2614630.tar.gz cpython-ebc37b28fa3fd66336116447b7c2b9b1c2614630.tar.bz2 |
-- properly reset groups in findall (bug #117612)
-- fixed negative lookbehind to work correctly at the beginning
of the target string (bug #117242)
-- improved syntax check; you can no longer refer to a group
inside itself (bug #110866)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/sre_parse.py | 14 | ||||
-rw-r--r-- | Lib/test/test_sre.py | 3 |
2 files changed, 15 insertions, 2 deletions
diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py index 7c36d4f..5334e06 100644 --- a/Lib/sre_parse.py +++ b/Lib/sre_parse.py @@ -62,14 +62,20 @@ class Pattern: # master pattern object. keeps track of global attributes def __init__(self): self.flags = 0 + self.open = [] self.groups = 1 self.groupdict = {} - def getgroup(self, name=None): + def opengroup(self, name=None): gid = self.groups self.groups = gid + 1 if name: self.groupdict[name] = gid + self.open.append(gid) return gid + def closegroup(self, gid): + self.open.remove(gid) + def checkgroup(self, gid): + return gid < self.groups and gid not in self.open class SubPattern: # a subpattern, in intermediate form @@ -278,6 +284,8 @@ def _escape(source, escape, state): # got at least one decimal digit; this is a group reference group = _group(escape, state.groups) if group: + if not state.checkgroup(group): + raise error, "cannot refer to open group" return GROUPREF, group raise ValueError if len(escape) == 2: @@ -547,10 +555,12 @@ def _parse(source, state): # anonymous group group = None else: - group = state.getgroup(name) + group = state.opengroup(name) p = _parse_sub(source, state) if not source.match(")"): raise error, "unbalanced parenthesis" + if group is not None: + state.closegroup(group) subpattern.append((SUBPATTERN, (group, p))) else: while 1: diff --git a/Lib/test/test_sre.py b/Lib/test/test_sre.py index 373efa0..b9692a1 100644 --- a/Lib/test/test_sre.py +++ b/Lib/test/test_sre.py @@ -167,6 +167,9 @@ test(r"""sre.findall(r"(:)(:*)", "a:b::c:::d")""", [(":", ""), (":", ":"), (":", "::")]) test(r"""sre.findall(r"(a)|(b)", "abc")""", [("a", ""), ("", "b")]) +# bug 117612 +test(r"""sre.findall(r"(a|(b))", "aba")""", [("a", ""),("b", "b"),("a", "")]) + if verbose: print "Running tests on sre.match" |