[Python-Dev] ConfigParser case sensitive and strings vs objects returned

Gordon Williams g_will at cyberus.ca
Tue Oct 7 14:11:36 EDT 2003


Hi Fred,

A couple of other things about the ConfigParser module that I find a bit
strange and I'm not sure that is intended behaivior.


1. Option gets converted to lower case and therefore is not case sensitive,
but section is case sensitive.  I would have thought that both would be or
neither would be case sensitive.  (My preference would be that neither would
be case sensitive.)

example if I have a config.txt file with:
[File 1]
databaseADF adsfa:octago DASFDAS
user:Me
password:blank

then this gets written out it is (were databaseADF is now databaseadf):
[File 1]
databaseadf adsfa = octago DASFDAS
password = blank
user = Me

Using "file 1' instead of  "File 1":
>>> Config.config
<ConfigParser.SafeConfigParser instance at 0x010EEE40>
>>> Config.config.options('file 1')
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 240, in options
    raise NoSectionError(section)
NoSectionError: No section: 'file 1'

But using 'dataBASEadf adsfa' instead of  'databaseADF adsfa' or
'databaseadf adsfa ' is OK and returns the correct value:
>>> Config.config.get('File 1', 'dataBASEadf adsfa')
'octago DASFDAS'

The differences in handling the option and section are annoying and should
at least be described in the docs if they cant be changed.

2. SafeConfigParser is the recommended ConfigParser in the docs.  I'm not
sure what is meant be safe.  When values are read in from a file they are
first converted to strings.  This is not true for values set within the
code.

If I set an option with anything other than a string then this occurs:

>>> Config.config.set('File 1', 'test', 2)
>>> Config.config.get('File 1', 'test')
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 518, in get
    return self._interpolate(section, option, value, d)
  File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 576, in _interpolate
    self._interpolate_some(option, L, rawval, section, vars, 1)
  File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 585, in
_interpolate_some
    p = rest.find("%")
AttributeError: 'int' object has no attribute 'find'

Likely the value assigned to the object should be first converted to a
string before it is stored.  The same thing happens if a dict or float is
passed in as a default:

>>> c= Config.SafeConfigParser({'test':{'1':"One",'2':"Two"}, 'foo':2.3})

This looks OK:
>>> c.write(sys.stdout)
[DEFAULT]
test = {'1': 'One', '2': 'Two'}
foo = 2.3

Problem with get:
>>> c.get('DEFAULT', 'test')
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 518, in get
    return self._interpolate(section, option, value, d)
  File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 576, in _interpolate
    self._interpolate_some(option, L, rawval, section, vars, 1)
  File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 585, in
_interpolate_some
    p = rest.find("%")
AttributeError: 'dict' object has no attribute 'find'

>>> c.get('DEFAULT', 'foo')
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 518, in get
    return self._interpolate(section, option, value, d)
  File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 576, in _interpolate
    self._interpolate_some(option, L, rawval, section, vars, 1)
  File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 585, in
_interpolate_some
    p = rest.find("%")
AttributeError: 'float' object has no attribute 'find'
>>>

If we set raw= True, then we get back an object (<type 'float'>) and not a
string:
>>> c.get('DEFAULT', 'foo', raw= True)
2.2999999999999998

If we use vars= {} an exception is also thrown:
>>> c.get('DEFAULT', 'junk', vars= {'junk': 99})
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 518, in get
    return self._interpolate(section, option, value, d)
  File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 576, in _interpolate
    self._interpolate_some(option, L, rawval, section, vars, 1)
  File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 585, in
_interpolate_some
    p = rest.find("%")
AttributeError: 'int' object has no attribute 'find'


One last comment is that 'interpolation' is a bit confusing in the docs.
Maybe 'substitution' would be a better word.

Thanks,

Gordon Williams




More information about the Python-Dev mailing list