summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAndrew M. Kuchling <amk@amk.ca>2005-06-02 13:40:12 (GMT)
committerAndrew M. Kuchling <amk@amk.ca>2005-06-02 13:40:12 (GMT)
commit6ed5f30f94b262c6ba266188fb934aaf4359abe7 (patch)
tree800496c8951b53e2f258fff5849d0d5ef53f2316 /Lib
parent270dfd86b41d90b7b27c1f61fe87a118bebd12ac (diff)
downloadcpython-6ed5f30f94b262c6ba266188fb934aaf4359abe7.zip
cpython-6ed5f30f94b262c6ba266188fb934aaf4359abe7.tar.gz
cpython-6ed5f30f94b262c6ba266188fb934aaf4359abe7.tar.bz2
[Bug #1177831] Fix (?(id)yes|no) for a group other than the first one, and add a test case
Diffstat (limited to 'Lib')
-rw-r--r--Lib/sre_compile.py2
-rw-r--r--Lib/test/test_re.py10
2 files changed, 11 insertions, 1 deletions
diff --git a/Lib/sre_compile.py b/Lib/sre_compile.py
index 27ab1fe..af06d88 100644
--- a/Lib/sre_compile.py
+++ b/Lib/sre_compile.py
@@ -156,7 +156,7 @@ def _compile(code, pattern, flags):
emit(av-1)
elif op is GROUPREF_EXISTS:
emit(OPCODES[op])
- emit((av[0]-1)*2)
+ emit(av[0]-1)
skipyes = _len(code); emit(0)
_compile(code, av[1], flags)
if av[2]:
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
index 07bc63b..c86f502 100644
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -235,6 +235,16 @@ class ReTests(unittest.TestCase):
self.assertEqual(re.match('^(?:(a)|c)((?(1)|d))$', 'a').groups(),
('a', ''))
+ # Tests for bug #1177831: exercise groups other than the first group
+ p = re.compile('(?P<g1>a)(?P<g2>b)?((?(g2)c|d))')
+ self.assertEqual(p.match('abc').groups(),
+ ('a', 'b', 'c'))
+ self.assertEqual(p.match('ad').groups(),
+ ('a', None, 'd'))
+ self.assertEqual(p.match('abd'), None)
+ self.assertEqual(p.match('ac'), None)
+
+
def test_re_groupref(self):
self.assertEqual(re.match(r'^(\|)?([^()]+)\1$', '|a|').groups(),
('|', 'a'))