[Python-Dev] Remove str.find in 3.0?

Guido van Rossum gvanrossum at gmail.com
Sat Aug 27 16:36:46 CEST 2005


On 8/27/05, Raymond Hettinger <raymond.hettinger at verizon.net> wrote:

> --- From ConfigParser.py ---------------
> 
> optname, vi, optval = mo.group('option', 'vi', 'value')
> if vi in ('=', ':') and ';' in optval:
>     # ';' is a comment delimiter only if it follows
>     # a spacing character
>     pos = optval.find(';')
>     if pos != -1 and optval[pos-1].isspace():
>         optval = optval[:pos]
> optval = optval.strip()
> . . .
> 
> 
> optname, vi, optval = mo.group('option', 'vi', 'value')
> if vi in ('=', ':') and ';' in optval:
>     # ';' is a comment delimiter only if it follows
>     # a spacing character
>     try:
>         pos = optval.index(';')
>     except ValueError():

I'm sure you meant "except ValueError:"

>         pass
>     else:
>         if optval[pos-1].isspace():
>             optval = optval[:pos]
> optval = optval.strip()
> . . .

That code is buggy before and after the transformation -- consider
what happens if optval *starts* with a semicolon. Also, the code is
searching optval for ';' twice. Suggestion:

if vi in ('=',':'):
  try: pos = optval.index(';')
  except ValueError: pass
  else:
    if pos > 0 and optval[pos-1].isspace():
      optval = optval[:pos]

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list