Index: Lib/configparser.py =================================================================== --- Lib/configparser.py (revision 83048) +++ Lib/configparser.py (working copy) @@ -52,12 +52,13 @@ The filename defaults to fp.name; it is only used in error messages (if fp has no `name' attribute, the string `' is used). - get(section, option, raw=False, vars=None) + get(section, option, raw=False, vars=None, *, default) return a string value for the named option. All % interpolations are expanded in the return values, based on the defaults passed into the constructor and the DEFAULT section. Additional substitutions may be provided using the `vars' argument, which must be a dictionary whose - contents override any pre-existing defaults. + contents override any pre-existing defaults. If an exception is raised + and the default keyword argument is present, return the given value. getint(section, options) like get(), but convert value to an integer @@ -315,7 +316,15 @@ filename = '' self._read(fp, filename) - def get(self, section, option): + def get(self, section, option, *args, **kwds): + try: + return self._getit(section, option, *args, **kwds) + except Exception as exc: + if "default" in kwds: + return kwds["default"] + raise exc + + def _getit(self, section, option, **kwds): opt = self.optionxform(option) if section not in self._sections: if section != DEFAULTSECT: @@ -344,20 +353,20 @@ del d["__name__"] return d.items() - def _get(self, section, conv, option): - return conv(self.get(section, option)) + def _get(self, section, conv, option, **kwds): + return conv(self.get(section, option, **kwds)) - def getint(self, section, option): - return self._get(section, int, option) + def getint(self, section, option, **kwds): + return self._get(section, int, option, **kwds) - def getfloat(self, section, option): - return self._get(section, float, option) + def getfloat(self, section, option, **kwds): + return self._get(section, float, option, **kwds) _boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True, '0': False, 'no': False, 'false': False, 'off': False} - def getboolean(self, section, option): - v = self.get(section, option) + def getboolean(self, section, option, **kwds): + v = self.get(section, option, **kwds) if v.lower() not in self._boolean_states: raise ValueError('Not a boolean: %s' % v) return self._boolean_states[v.lower()] @@ -538,7 +547,7 @@ class ConfigParser(RawConfigParser): - def get(self, section, option, raw=False, vars=None): + def get(self, section, option, raw=False, vars=None, **kwds): """Get an option value for a given section. All % interpolations are expanded in the return values, based on the @@ -549,6 +558,9 @@ The section DEFAULT is special. """ + return super().get(section, option, raw, vars, **kwds) + + def _getit(self, section, option, raw=False, vars=None, **kwds): d = self._defaults.copy() try: d.update(self._sections[section]) @@ -699,3 +711,4 @@ raise ValueError("invalid interpolation syntax in %r at " "position %d" % (value, percent_index)) ConfigParser.set(self, section, option, value) +