Using percent signs with SafeConfigParser

Discussion in 'Python' started by Márcio Faustino, Apr 11, 2009.

  1. 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
    Márcio Faustino, Apr 11, 2009
    #1
    1. Advertising

  2. Márcio Faustino

    Peter Otten Guest

    Márcio Faustino wrote:

    > 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 "%".


    I think you are right. Please file a bug report .

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


    IMO this doesn't fix the problem because

    (1) it allows "%%%" which is also incorrect
    (2) _interpvar_re has already butchered values like "%%(alpha)s" to "%"

    Peter
    Peter Otten, Apr 11, 2009
    #2
    1. Advertising

  3. On 11 Abr, 20:06, Peter Otten <> 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 "(?<!%)%(?:%%)*(?!%)"?

    > (2) _interpvar_re has already butchered values like "%%(alpha)s" to "%"


    Isn't that expected? I mean, one wouldn't write "%%(alpha)s", but
    instead "%%%(alpha)s" or "%(alpha)s" right?
    Márcio Faustino, Apr 11, 2009
    #3
  4. Márcio Faustino

    MRAB Guest

    Márcio Faustino wrote:
    > On 11 Abr, 20:06, Peter Otten <> 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:

    >> (2) _interpvar_re has already butchered values like "%%(alpha)s" to "%"

    >
    > Isn't that expected? I mean, one wouldn't write "%%(alpha)s", but
    > instead "%%%(alpha)s" or "%(alpha)s" right?
    >
    MRAB, Apr 12, 2009
    #4
  5. Márcio Faustino

    Peter Otten Guest

    Márcio Faustino wrote:

    > On 11 Abr, 20:06, Peter Otten <> wrote:


    >> (2) _interpvar_re has already butchered values like "%%(alpha)s" to "%"

    >
    > Isn't that expected? I mean, one wouldn't write "%%(alpha)s", but
    > instead "%%%(alpha)s" or "%(alpha)s" right?


    "%%(alpha)s" would mean that you want the value to be "%(alpha)s" (unlikely,
    but possible).

    Peter
    Peter Otten, Apr 12, 2009
    #5
  6. Márcio Faustino

    Peter Otten Guest

    MRAB wrote:

    > Márcio Faustino wrote:
    > > On 11 Abr, 20:06, Peter Otten <> 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
    Peter Otten, Apr 12, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cGhpbGxpcA==?=

    Regexp and percent signs

    =?Utf-8?B?cGhpbGxpcA==?=, Sep 24, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    610
    =?Utf-8?B?cGhpbGxpcA==?=
    Sep 24, 2004
  2. =?Utf-8?B?Y2hhcmxpZXdlc3Q=?=

    How to: Convert DOUBLE to PERCENT?

    =?Utf-8?B?Y2hhcmxpZXdlc3Q=?=, Jul 12, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    32,927
    =?Utf-8?B?Y2hhcmxpZXdlc3Q=?=
    Jul 12, 2005
  3. Dmitri

    Percent JFormattedField

    Dmitri, Nov 22, 2004, in forum: Java
    Replies:
    2
    Views:
    1,894
    Dmitri
    Nov 23, 2004
  4. Hamish Moffatt

    SafeConfigParser can set unsafe values

    Hamish Moffatt, Jul 10, 2007, in forum: Python
    Replies:
    7
    Views:
    474
    Hamish Moffatt
    Jul 11, 2007
  5. Josh English

    Help with Singleton SafeConfigParser

    Josh English, Dec 8, 2012, in forum: Python
    Replies:
    4
    Views:
    139
    Mark Lawrence
    Dec 8, 2012
Loading...

Share This Page