uncaught TypeError exception in re module or redemo.py

Kevin Altis altis at semi-retired.com
Tue Jul 9 02:26:08 EDT 2002


Hi,
I ported the tkinter redemo.py code to PythonCard today. While doing the
port I was trying various regular expression patterns and came across an
uncaught exception in the recompile method:

    def recompile(self, event=None):
        try:
            self.compiled = re.compile(self.regexdisplay.get(),
                                       self.getflags())
            bg = self.promptdisplay['background']
            self.statusdisplay.config(text="", background=bg)
        except re.error, msg:
            self.compiled = None
            self.statusdisplay.config(
                    text="re.error: %s" % str(msg),
                    background="red")
        self.reevaluate()

The except block catches various re errors, but it is possible to type a
pattern that will generate a TypeError. It is simple to simply catch the
TypeError with an additional except, but I'm wondering whether this should
actually be caught in the re module and turned into an re.error like other
invalid patterns?

Here's an example chopped down to just the compile and the resulting
TypeError exception.

>>> re.compile('([a-')
Traceback (most recent call last):
  File "<input>", line 1, in ?
  File "C:\Python22\Lib\sre.py", line 178, in compile
    return _compile(pattern, flags)
  File "C:\Python22\Lib\sre.py", line 226, in _compile
    p = sre_compile.compile(pattern, flags)
  File "C:\Python22\Lib\sre_compile.py", line 430, in compile
    p = sre_parse.parse(p, flags)
  File "C:\Python22\Lib\sre_parse.py", line 623, in parse
    p = _parse_sub(source, pattern, 0)
  File "C:\Python22\Lib\sre_parse.py", line 318, in _parse_sub
    items.append(_parse(source, state))
  File "C:\Python22\Lib\sre_parse.py", line 583, in _parse
    p = _parse_sub(source, state)
  File "C:\Python22\Lib\sre_parse.py", line 318, in _parse_sub
    items.append(_parse(source, state))
  File "C:\Python22\Lib\sre_parse.py", line 424, in _parse
    if this[0] == "\\":
TypeError: unsubscriptable object
>>>

I'm using Python 2.2.1 on Windows 2000, so this might be fixed in the 2.3
alpha builds, I haven't tried them yet.

ka
---
Kevin Altis
altis at semi-retired.com





More information about the Python-list mailing list