Using percent signs with SafeConfigParser

Peter Otten __peter__ at web.de
Sun Apr 12 09:57:48 CEST 2009


MRAB wrote:

> Márcio Faustino wrote:
>  > On 11 Abr, 20:06, Peter Otten <__pete... at web.de> wrote:
>  >> I think you are right. Please file a bug report .
>  >
>  > I will.
>  >
>  >> IMO this doesn't fix the problem because
>  >>
>  >> (1) it allows "%%%" which is also incorrect
>  >
>  > You're right, how about this one "(?<!%)%(?:%%)*(?!%)"?
>  >
> Instead of checking for a 'bad' value, why not check for a 'good' one?
> 
> Change:
> 
>      _badpercent_re = re.compile(r"%[^%]|%$")
> 
> to:
> 
>      _goodpercent_re = re.compile(r"(?:%%|[^%])*$")
> 
> and then:
> 
>          m = self._badpercent_re.search(tmp_value)
>          if m:
> 
> to:
> 
>          m = self._goodpercent_re.match(tmp_value)
>          if not m:

Configparser also knows a construct "%(whatever)s" which works like Python's
string formatting and takes "whatever" from the "DEFAULT" section of the
config file. 

I like your approach, but I think the regex should be

_goodpercent_re = re.compile(r"^(?:%%|%\([^)]+\)s|[^%])*$"

or similar -- what is currently allowed within the parens seems to be an
implementation accident...

Peter



More information about the Python-list mailing list