[Tutor] Can you see my error? [regex module]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 5 Aug 2001 01:42:03 -0700 (PDT)


On Fri, 3 Aug 2001, Danny Yoo wrote:

> On Fri, 3 Aug 2001 DavidCraig@pia.ca.gov wrote:
> 
> > The message I get is:
> > 
> > >>>
> > Traceback (most recent call last):
> >   File "C:/Python21/Practice/therapist1.py", line 273, in ?
> >     gKeys = map(lambda x:regex.compile(x[0]),gPats)
> >   File "C:/Python21/Practice/therapist1.py", line 273, in <lambda>
> >     gKeys = map(lambda x:regex.compile(x[0]),gPats)
> > error: Badly placed parenthesis

I'm exploring this problem a little bit more.  Take a look:

###
>>> import re
>>> re.compile(")")
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python1.5/re.py", line 79, in compile
    code=pcre_compile(pattern, flags, groupindex)
pcre.error: ('unmatched brackets', 0)
###

What sort of strings are you feeding into regex.compile()?  The exception
triggered here has the same tone as your error, although not exactly.  
Python normally yells out "SyntaxError" on a typical parentheses
mismatching:

###
>>> print ("I should break"))
  File "<stdin>", line 1
    print ("I should break"))
                            ^
SyntaxError: invalid syntax
###

so I don't think your error has to do with your program... at least, not
from the level of source code.  After looking through Python source, I've
found that the string "Badly placed parenthesis" is coming somewhere from
the regular expression module:

###
[dyoo@ns Python-2.1]$ grep -i -r 'Badly placed parenthesis' *
Modules/regexpr.c:       return "Badly placed parenthesis";
###

and this happens when the regular expression engine sees too many
right parenthesis:

###
## Deep down in the bowels of regexpr.c
                case Rclosepar:
                {
                        if (paren_depth <= 0)
                                goto parenthesis_error;
###


Hmmm....

###
>>> import regex
__main__:1: DeprecationWarning: the regex module is deprecated; please use
the re module
>>> # yes, yes, I know... but let me try this...
>>> regex.compile('\)')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
regex.error: Badly placed parenthesis
###

There's the little critter!  


Check up and see what strings you're sending to regex.compile().  Anyway,
tell us if you get the bug fixed; the uncertainty of not knowing is
driving me nuts.  *grin*


Hope this helps!