Using percent signs with SafeConfigParser

Márcio Faustino m.faustino at gmail.com
Sat Apr 11 14:45:21 CEST 2009


Hi,

Does the SafeConfigParser class correctly detects lone percent signs?
For example, shouldn't the string "100%%" be accepted as a valid
value? Executing the code below should only print one error, instead
it prints 2. (I've tested this with version 2.6.1 on Windows XP.)

It seems the "_badpercent_re" regular expression (an attribute of
SafeConfigParser, with value "%[^%]|%$") is flawed:
- The first alternative "%[^%]" fails with the string "%%_", because
it matches "%_".
- The second alternative "%$" fails with the string "%%", because it
matches "%".

The correct regular expression is "(?<!%)%(?!%)", which passes the
previous tests.

Thanks,

--

from ConfigParser import *
import re

config = SafeConfigParser()

# Expected:
try:
    config.set('DEFAULT', 'test', '100%')
except ValueError as error:
    print 'Error:', error

# Unexpected:
try:
    config.set('DEFAULT', 'test', '100%%')
except ValueError as error:
    print 'Error:', error

try:
    config._badpercent_re = re.compile('(?<!%)%(?!%)')
    config.set('DEFAULT', 'test', '100%%')
except ValueError as error:
    print 'Error:', error



More information about the Python-list mailing list