mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
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.
This commit is contained in:
parent
6c5bc3457c
commit
e2c649126e
2 changed files with 49 additions and 33 deletions
|
@ -90,17 +90,26 @@ section.
|
||||||
\end{excdesc}
|
\end{excdesc}
|
||||||
|
|
||||||
\begin{excdesc}{InterpolationError}
|
\begin{excdesc}{InterpolationError}
|
||||||
Exception raised when problems occur performing string interpolation.
|
Base class for exceptions raised when problems occur performing string
|
||||||
|
interpolation.
|
||||||
\end{excdesc}
|
\end{excdesc}
|
||||||
|
|
||||||
\begin{excdesc}{InterpolationDepthError}
|
\begin{excdesc}{InterpolationDepthError}
|
||||||
Exception raised when string interpolation cannot be completed because
|
Exception raised when string interpolation cannot be completed because
|
||||||
the number of iterations exceeds \constant{MAX_INTERPOLATION_DEPTH}.
|
the number of iterations exceeds \constant{MAX_INTERPOLATION_DEPTH}.
|
||||||
|
Subclass of \exception{InterpolationError}.
|
||||||
|
\end{excdesc}
|
||||||
|
|
||||||
|
\begin{excdesc}{InterpolationMissingOptionError}
|
||||||
|
Exception raised when an option referenced from a value does not exist.
|
||||||
|
Subclass of \exception{InterpolationError}.
|
||||||
|
\versionadded{2.3}
|
||||||
\end{excdesc}
|
\end{excdesc}
|
||||||
|
|
||||||
\begin{excdesc}{InterpolationSyntaxError}
|
\begin{excdesc}{InterpolationSyntaxError}
|
||||||
Exception raised when the source text into which substitutions are
|
Exception raised when the source text into which substitutions are
|
||||||
made does not conform to the required syntax.
|
made does not conform to the required syntax.
|
||||||
|
Subclass of \exception{InterpolationError}.
|
||||||
\versionadded{2.3}
|
\versionadded{2.3}
|
||||||
\end{excdesc}
|
\end{excdesc}
|
||||||
|
|
||||||
|
@ -122,8 +131,8 @@ the \var{raw} parameter is false. This is relevant only for the
|
||||||
|
|
||||||
\begin{seealso}
|
\begin{seealso}
|
||||||
\seemodule{shlex}{Support for a creating \UNIX{} shell-like
|
\seemodule{shlex}{Support for a creating \UNIX{} shell-like
|
||||||
minilanguages which can be used as an alternate format
|
mini-languages which can be used as an alternate
|
||||||
for application configuration files.}
|
format for application configuration files.}
|
||||||
\end{seealso}
|
\end{seealso}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -106,11 +106,11 @@ class Error(Exception):
|
||||||
"""Base class for ConfigParser exceptions."""
|
"""Base class for ConfigParser exceptions."""
|
||||||
|
|
||||||
def __init__(self, msg=''):
|
def __init__(self, msg=''):
|
||||||
self._msg = msg
|
self.message = msg
|
||||||
Exception.__init__(self, msg)
|
Exception.__init__(self, msg)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return self._msg
|
return self.message
|
||||||
|
|
||||||
__str__ = __repr__
|
__str__ = __repr__
|
||||||
|
|
||||||
|
@ -118,56 +118,60 @@ class NoSectionError(Error):
|
||||||
"""Raised when no section matches a requested option."""
|
"""Raised when no section matches a requested option."""
|
||||||
|
|
||||||
def __init__(self, section):
|
def __init__(self, section):
|
||||||
Error.__init__(self, 'No section: %s' % section)
|
Error.__init__(self, 'No section: ' + `section`)
|
||||||
self.section = section
|
self.section = section
|
||||||
|
|
||||||
class DuplicateSectionError(Error):
|
class DuplicateSectionError(Error):
|
||||||
"""Raised when a section is multiply-created."""
|
"""Raised when a section is multiply-created."""
|
||||||
|
|
||||||
def __init__(self, section):
|
def __init__(self, section):
|
||||||
Error.__init__(self, "Section %s already exists" % section)
|
Error.__init__(self, "Section %r already exists" % section)
|
||||||
self.section = section
|
self.section = section
|
||||||
|
|
||||||
class NoOptionError(Error):
|
class NoOptionError(Error):
|
||||||
"""A requested option was not found."""
|
"""A requested option was not found."""
|
||||||
|
|
||||||
def __init__(self, option, section):
|
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))
|
(option, section))
|
||||||
self.option = option
|
self.option = option
|
||||||
self.section = section
|
self.section = section
|
||||||
|
|
||||||
class InterpolationError(Error):
|
class InterpolationError(Error):
|
||||||
|
"""Base class for interpolation-related exceptions."""
|
||||||
|
|
||||||
|
def __init__(self, option, section, msg):
|
||||||
|
Error.__init__(self, msg)
|
||||||
|
self.option = option
|
||||||
|
self.section = section
|
||||||
|
|
||||||
|
class InterpolationMissingOptionError(InterpolationError):
|
||||||
"""A string substitution required a setting which was not available."""
|
"""A string substitution required a setting which was not available."""
|
||||||
|
|
||||||
def __init__(self, reference, option, section, rawval):
|
def __init__(self, option, section, rawval, reference):
|
||||||
Error.__init__(self,
|
msg = ("Bad value substitution:\n"
|
||||||
"Bad value substitution:\n"
|
|
||||||
"\tsection: [%s]\n"
|
"\tsection: [%s]\n"
|
||||||
"\toption : %s\n"
|
"\toption : %s\n"
|
||||||
"\tkey : %s\n"
|
"\tkey : %s\n"
|
||||||
"\trawval : %s\n"
|
"\trawval : %s\n"
|
||||||
% (section, option, reference, rawval))
|
% (section, option, reference, rawval))
|
||||||
|
InterpolationError.__init__(self, option, section, msg)
|
||||||
self.reference = reference
|
self.reference = reference
|
||||||
self.option = option
|
|
||||||
self.section = section
|
|
||||||
|
|
||||||
class InterpolationSyntaxError(Error):
|
class InterpolationSyntaxError(InterpolationError):
|
||||||
"""Raised when the source text into which substitutions are made
|
"""Raised when the source text into which substitutions are made
|
||||||
does not conform to the required syntax."""
|
does not conform to the required syntax."""
|
||||||
|
|
||||||
class InterpolationDepthError(Error):
|
class InterpolationDepthError(InterpolationError):
|
||||||
"""Raised when substitutions are nested too deeply."""
|
"""Raised when substitutions are nested too deeply."""
|
||||||
|
|
||||||
def __init__(self, option, section, rawval):
|
def __init__(self, option, section, rawval):
|
||||||
Error.__init__(self,
|
msg = ("Value interpolation too deeply recursive:\n"
|
||||||
"Value interpolation too deeply recursive:\n"
|
|
||||||
"\tsection: [%s]\n"
|
"\tsection: [%s]\n"
|
||||||
"\toption : %s\n"
|
"\toption : %s\n"
|
||||||
"\trawval : %s\n"
|
"\trawval : %s\n"
|
||||||
% (section, option, rawval))
|
% (section, option, rawval))
|
||||||
self.option = option
|
InterpolationError.__init__(self, option, section, msg)
|
||||||
self.section = section
|
|
||||||
|
|
||||||
class ParsingError(Error):
|
class ParsingError(Error):
|
||||||
"""Raised when a configuration file does not follow legal syntax."""
|
"""Raised when a configuration file does not follow legal syntax."""
|
||||||
|
@ -179,7 +183,7 @@ class ParsingError(Error):
|
||||||
|
|
||||||
def append(self, lineno, line):
|
def append(self, lineno, line):
|
||||||
self.errors.append((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):
|
class MissingSectionHeaderError(ParsingError):
|
||||||
"""Raised when a key-value pair is found before any section header."""
|
"""Raised when a key-value pair is found before any section header."""
|
||||||
|
@ -555,7 +559,8 @@ class ConfigParser(RawConfigParser):
|
||||||
try:
|
try:
|
||||||
value = value % vars
|
value = value % vars
|
||||||
except KeyError, e:
|
except KeyError, e:
|
||||||
raise InterpolationError(e[0], option, section, rawval)
|
raise InterpolationMissingOptionError(
|
||||||
|
option, section, rawval, e[0])
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
if value.find("%(") != -1:
|
if value.find("%(") != -1:
|
||||||
|
@ -593,13 +598,14 @@ class SafeConfigParser(ConfigParser):
|
||||||
m = self._interpvar_match(rest)
|
m = self._interpvar_match(rest)
|
||||||
if m is None:
|
if m is None:
|
||||||
raise InterpolationSyntaxError(
|
raise InterpolationSyntaxError(
|
||||||
"bad interpolation variable syntax at: %r" % rest)
|
"bad interpolation variable reference", rest)
|
||||||
var = m.group(1)
|
var = m.group(1)
|
||||||
rest = rest[m.end():]
|
rest = rest[m.end():]
|
||||||
try:
|
try:
|
||||||
v = map[var]
|
v = map[var]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise InterpolationError(var, option, section, rest)
|
raise InterpolationMissingOptionError(
|
||||||
|
option, section, rest, var)
|
||||||
if "%" in v:
|
if "%" in v:
|
||||||
self._interpolate_some(option, accum, v,
|
self._interpolate_some(option, accum, v,
|
||||||
section, map, depth + 1)
|
section, map, depth + 1)
|
||||||
|
@ -607,4 +613,5 @@ class SafeConfigParser(ConfigParser):
|
||||||
accum.append(v)
|
accum.append(v)
|
||||||
else:
|
else:
|
||||||
raise InterpolationSyntaxError(
|
raise InterpolationSyntaxError(
|
||||||
"'%' must be followed by '%' or '('")
|
option, section, rest,
|
||||||
|
"'%' must be followed by '%' or '(', found: " + `rest`)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue