classification
Title: ConfigParser non-string defaults broken with .getboolean()
Type: enhancement Stage: test needed
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: ajaksu2, anthonybaxter, draghuram, melicertes, rhettinger
Priority: low Keywords: easy, patch

Created on 2004-06-16 16:36 by melicertes, last changed 2009-04-22 17:12 by draghuram. This issue is now closed.

Files
File name Uploaded Description Edit
ConfigParser.patch melicertes, 2004-06-16 16:36 Patch to fix ConfigParser.getboolean()
Messages (5)
msg54180 - (view) Author: Charles (melicertes) Date: 2004-06-16 16:36
ConfigParser.getboolean() fails if it falls back to a
default value, and the value passed in was a boolean
object (instead of a string) because it unconditionally
does v.lower().
This should be fixed; there's something un-Pythonic
about expecting a boolean value but not being able to
actually provide a boolean as the default.

I've attached a patch (against 2.3.4c1; should apply to
2.3.4, I think) which makes the v.lower() conditional
on v being a string, and adds bool(True), bool(False),
int(1), and int(0) to _boolean_states.

Alternative resolution:  change the documentation to
specify that /only/ strings should be passed in the
defaults dictionary.  Less Pythonic.

msg54181 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-06-17 12:37
Logged In: YES 
user_id=80475

The method is functioning exactly as documented.

Changing this to a feature request.

FWIW, it seems reasonable to allow a boolean to be passed in
a default; however, calling it unpythonic is a bit extreme
since the whole API is designed to work with text arguments.

My solution would be to just add two lines after the self.get:

    if isinstance(v, bool):
        return v
msg54182 - (view) Author: Anthony Baxter (anthonybaxter) (Python triager) Date: 2004-08-09 09:58
Logged In: YES 
user_id=29957

I don't see any reason to not allow a boolean as a default.
Raymond's solution seems like a good one to me. Anyone object?
msg61964 - (view) Author: Raghuram Devarakonda (draghuram) (Python triager) Date: 2008-02-01 16:32
With the latest python, get() itself fails with boolean value default. I
tried with this script:

-------------
from ConfigParser import ConfigParser
cfg = ConfigParser({'var':True})
cfg.add_section('test_section')
print cfg.getboolean('test_section', 'var')
-------------

and it results in

-------------
Traceback (most recent call last):
  File "t.py", line 4, in <module>
    print cfg.getboolean('test_section', 'var')
  File "/localhome/raghu/localwork/cpython/trunk/Lib/ConfigParser.py",
line 349, in getboolean
    v = self.get(section, option)
  File "/localhome/raghu/localwork/cpython/trunk/Lib/ConfigParser.py",
line 545, in get
    return self._interpolate(section, option, value, d)
  File "/localhome/raghu/localwork/cpython/trunk/Lib/ConfigParser.py",
line 585, in _interpolate
    if "%(" in value:
TypeError: argument of type 'bool' is not iterable
-------------

I doubt if it is worth fixing the OP's issue considering that
_interpolate is assuming the value to be string. Can I close this issue?
msg86300 - (view) Author: Daniel Diniz (ajaksu2) (Python triager) Date: 2009-04-22 15:21
+1 for closing.
History
Date User Action Args
2009-04-22 17:12:56draghuramsetstatus: open -> closed
resolution: wont fix
2009-04-22 15:21:52ajaksu2setpriority: normal -> low
nosy: + ajaksu2
messages: + msg86300

2009-02-14 13:57:53ajaksu2setkeywords: + patch, easy
stage: test needed
versions: + Python 2.7, - Python 2.6
2008-02-01 16:32:08draghuramsetmessages: + msg61964
components: + Library (Lib), - None
versions: + Python 2.6
2008-02-01 15:00:38draghuramsetnosy: + draghuram
2004-06-16 16:36:16melicertescreate