This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Default return value in ConfigParser
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: duplicate
Dependencies: Superseder: configparser.ConfigParser's getint, getboolean and getfloat don't accept `vars`
View: 9421
Assigned To: Nosy List: BreamoreBoy, amaury.forgeotdarc, eric.araujo, jjdominguezm, lukasz.langa
Priority: normal Keywords: patch

Created on 2009-08-21 08:57 by jjdominguezm, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
configparser.py.diff jjdominguezm, 2010-07-22 11:12
test_cfgparser.py.diff jjdominguezm, 2010-07-22 11:12
Messages (7)
msg91808 - (view) Author: Juan Javier (jjdominguezm) Date: 2009-08-21 08:57
I think it is useful, at least for me, to add an argument, default, to
[Safe,Raw]ConfigParser.get that, if present, will be returned if the
methid fails to return the value.

That is, instead of rasing an exception, return default, if present.

It could be done overriding the get method in SafeConfigParser,
something like this:

class SafeConfigParser(ConfigParser):
    def get(self, section, option, raw=False, vars=None, **kwds):
        try:
            return super().get(section, option, raw, vars)
        except Exception as exc:
            if "default" in kwds:
                return kwds["default"]
            raise exc
msg109862 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-07-10 12:26
A short inline patch is proposed, would something like this be acceptable?
msg111067 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2010-07-21 14:32
The idea is fine (dict.get also has a 'default' parameter) but this patch is very incomplete:
- the enhancement should be applied to the three Parsers
- it needs unit tests.
msg111166 - (view) Author: Juan Javier (jjdominguezm) Date: 2010-07-22 11:12
I've applied the enhancement to the three parsers, actually I've made the change to RawconfigParser with a small change to ConfigParser.

I've also created some unit tests.
msg112572 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-08-03 10:57
Why is it needed to add varargs and kwargs instead of only a new “default” argument?
msg113159 - (view) Author: Juan Javier (jjdominguezm) Date: 2010-08-07 10:06
I would like the method to have the exact same behavior as before if the "default" argument is not present, and return the given default value when "deafult" argument is present.

If you simply add a "default" keyword, it will always be present and you wouldn't know if the user wants the exception thrown or the default value returned.

Do you know how to program this using a "default" keyword argument?
msg113473 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2010-08-09 21:10
This issue is superseded by #9421.
History
Date User Action Args
2022-04-11 14:56:52adminsetgithub: 51000
2010-08-09 21:10:00lukasz.langasetstatus: open -> closed
resolution: duplicate
superseder: configparser.ConfigParser's getint, getboolean and getfloat don't accept `vars`
messages: + msg113473
2010-08-09 13:12:05lukasz.langalinkissue8666 superseder
2010-08-07 10:06:55jjdominguezmsetmessages: + msg113159
2010-08-03 10:57:18eric.araujosetmessages: + msg112572
2010-08-03 10:55:54eric.araujosetnosy: + eric.araujo, lukasz.langa
2010-07-22 11:12:27jjdominguezmsetfiles: + test_cfgparser.py.diff
2010-07-22 11:12:14jjdominguezmsetfiles: + configparser.py.diff
keywords: + patch
messages: + msg111166
2010-07-21 14:32:32amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg111067
2010-07-10 12:26:43BreamoreBoysetnosy: + BreamoreBoy

messages: + msg109862
stage: patch review
2009-08-21 08:57:56jjdominguezmcreate