diff options
author | Fred Drake <fdrake@acm.org> | 2002-12-31 17:23:27 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2002-12-31 17:23:27 (GMT) |
commit | e2c649126ed1bd609710c10776d148750987e947 (patch) | |
tree | 2e46b83419cecd761716f901caf1460319bacc3a /Lib/ConfigParser.py | |
parent | 6c5bc3457c2cb9a293e9a402f1af3b60978cbe18 (diff) | |
download | cpython-e2c649126ed1bd609710c10776d148750987e947.zip cpython-e2c649126ed1bd609710c10776d148750987e947.tar.gz cpython-e2c649126ed1bd609710c10776d148750987e947.tar.bz2 |
Further cleanup of exceptions. All interpolation-related exceptions
now derive from InterpolationError, which is not raised directly (only
subclasses get raised). This matches what the docs already said.
Diffstat (limited to 'Lib/ConfigParser.py')
-rw-r--r-- | Lib/ConfigParser.py | 67 |
1 files changed, 37 insertions, 30 deletions
diff --git a/Lib/ConfigParser.py b/Lib/ConfigParser.py index 7c2eba5..1fb9ec5 100644 --- a/Lib/ConfigParser.py +++ b/Lib/ConfigParser.py @@ -106,11 +106,11 @@ class Error(Exception): """Base class for ConfigParser exceptions.""" def __init__(self, msg=''): - self._msg = msg + self.message = msg Exception.__init__(self, msg) def __repr__(self): - return self._msg + return self.message __str__ = __repr__ @@ -118,56 +118,60 @@ class NoSectionError(Error): """Raised when no section matches a requested option.""" def __init__(self, section): - Error.__init__(self, 'No section: %s' % section) + Error.__init__(self, 'No section: ' + `section`) self.section = section class DuplicateSectionError(Error): """Raised when a section is multiply-created.""" def __init__(self, section): - Error.__init__(self, "Section %s already exists" % section) + Error.__init__(self, "Section %r already exists" % section) self.section = section class NoOptionError(Error): """A requested option was not found.""" def __init__(self, option, section): - Error.__init__(self, "No option `%s' in section: %s" % + Error.__init__(self, "No option %r in section: %r" % (option, section)) self.option = option self.section = section class InterpolationError(Error): - """A string substitution required a setting which was not available.""" + """Base class for interpolation-related exceptions.""" - def __init__(self, reference, option, section, rawval): - Error.__init__(self, - "Bad value substitution:\n" - "\tsection: [%s]\n" - "\toption : %s\n" - "\tkey : %s\n" - "\trawval : %s\n" - % (section, option, reference, rawval)) - self.reference = reference + def __init__(self, option, section, msg): + Error.__init__(self, msg) self.option = option self.section = section -class InterpolationSyntaxError(Error): +class InterpolationMissingOptionError(InterpolationError): + """A string substitution required a setting which was not available.""" + + def __init__(self, option, section, rawval, reference): + msg = ("Bad value substitution:\n" + "\tsection: [%s]\n" + "\toption : %s\n" + "\tkey : %s\n" + "\trawval : %s\n" + % (section, option, reference, rawval)) + InterpolationError.__init__(self, option, section, msg) + self.reference = reference + +class InterpolationSyntaxError(InterpolationError): """Raised when the source text into which substitutions are made does not conform to the required syntax.""" -class InterpolationDepthError(Error): +class InterpolationDepthError(InterpolationError): """Raised when substitutions are nested too deeply.""" def __init__(self, option, section, rawval): - Error.__init__(self, - "Value interpolation too deeply recursive:\n" - "\tsection: [%s]\n" - "\toption : %s\n" - "\trawval : %s\n" - % (section, option, rawval)) - self.option = option - self.section = section + msg = ("Value interpolation too deeply recursive:\n" + "\tsection: [%s]\n" + "\toption : %s\n" + "\trawval : %s\n" + % (section, option, rawval)) + InterpolationError.__init__(self, option, section, msg) class ParsingError(Error): """Raised when a configuration file does not follow legal syntax.""" @@ -179,7 +183,7 @@ class ParsingError(Error): def append(self, lineno, line): self.errors.append((lineno, line)) - self._msg = self._msg + '\n\t[line %2d]: %s' % (lineno, line) + self.message += '\n\t[line %2d]: %s' % (lineno, line) class MissingSectionHeaderError(ParsingError): """Raised when a key-value pair is found before any section header.""" @@ -555,7 +559,8 @@ class ConfigParser(RawConfigParser): try: value = value % vars except KeyError, e: - raise InterpolationError(e[0], option, section, rawval) + raise InterpolationMissingOptionError( + option, section, rawval, e[0]) else: break if value.find("%(") != -1: @@ -593,13 +598,14 @@ class SafeConfigParser(ConfigParser): m = self._interpvar_match(rest) if m is None: raise InterpolationSyntaxError( - "bad interpolation variable syntax at: %r" % rest) + "bad interpolation variable reference", rest) var = m.group(1) rest = rest[m.end():] try: v = map[var] except KeyError: - raise InterpolationError(var, option, section, rest) + raise InterpolationMissingOptionError( + option, section, rest, var) if "%" in v: self._interpolate_some(option, accum, v, section, map, depth + 1) @@ -607,4 +613,5 @@ class SafeConfigParser(ConfigParser): accum.append(v) else: raise InterpolationSyntaxError( - "'%' must be followed by '%' or '('") + option, section, rest, + "'%' must be followed by '%' or '(', found: " + `rest`) |