Issue 545855: Wrong exception from re.compile() (original) (raw)

re.compile('foo[a-') raises a TypeError exception instead of re.error.

Python 2.2.1 (#1, Apr 4 2002, 17:22:15) [GCC 2.96 20000731 (Mandrake Linux 8.2 2.96-0.76mdk)] on linux2 Type "help", "copyright", "credits" or "license" for more information.

import re re.compile('foo[a-') Traceback (most recent call last): File "", line 1, in ? File "/usr/local/lib/python2.2/sre.py", line 178, in compile return _compile(pattern, flags) File "/usr/local/lib/python2.2/sre.py", line 226, in _compile p = sre_compile.compile(pattern, flags) File "/usr/local/lib/python2.2/sre_compile.py", line 430, in compile p = sre_parse.parse(p, flags) File "/usr/local/lib/python2.2/sre_parse.py", line 623, in parse p = _parse_sub(source, pattern, 0) File "/usr/local/lib/python2.2/sre_parse.py", line 318, in _parse_sub items.append(_parse(source, state)) File "/usr/local/lib/python2.2/sre_parse.py", line 424, in _parse if this[0] == "\": TypeError: unsubscriptable object

Logged In: YES user_id=670441

Patch below fixes the problem. Didn't check for end of expression in one case.

Index: dist/src/Lib/sre_parse.py

RCS file: /cvsroot/python/python/dist/src/Lib/sre_parse.py,v retrieving revision 1.55 diff -c -r1.55 sre_parse.py *** dist/src/Lib/sre_parse.py 2 Jun 2002 00:40:05 -0000 1.55 --- dist/src/Lib/sre_parse.py 4 Feb 2003 19:53:14 -0000


*** 420,429 **** set.append((LITERAL, ord("-"))) break else: ! if this[0] == "\": code2 = _class_escape(source, this) ! else: code2 = LITERAL, ord(this) if code1[0] != LITERAL or code2[0] != LITERAL: raise error, "bad character range" lo = code1[1] --- 420,431 ---- set.append((LITERAL, ord("-"))) break else: ! if this and this[0] == "\": code2 = _class_escape(source, this) ! elif this: code2 = LITERAL, ord(this)