[Python-bugs-list] [ python-Bugs-545855 ] Wrong exception from re.compile()
SourceForge.net
noreply@sourceforge.net
Fri, 16 May 2003 18:53:02 -0700
Bugs item #545855, was opened at 2002-04-18 15:22
Message generated for change (Comment added) made by bcannon
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=545855&group_id=5470
Category: Regular Expressions
Group: Python 2.2
>Status: Closed
>Resolution: Out of Date
Priority: 5
Submitted By: Barry A. Warsaw (bwarsaw)
Assigned to: Fredrik Lundh (effbot)
Summary: Wrong exception from re.compile()
Initial Comment:
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 "<stdin>", 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
----------------------------------------------------------------------
>Comment By: Brett Cannon (bcannon)
Date: 2003-05-16 18:53
Message:
Logged In: YES
user_id=357491
This is fixed in 2.2.2 and 2.3b1. Closing as out of date.
----------------------------------------------------------------------
Comment By: Michael Stone (mbrierst)
Date: 2003-02-04 12:02
Message:
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)
+ else:
+ raise error, "unexpected end of regular expression"
if code1[0] != LITERAL or code2[0] != LITERAL:
raise error, "bad character range"
lo = code1[1]
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=545855&group_id=5470