cpython: 5ecf650ede7c (original) (raw)
Mercurial > cpython
changeset 74545:5ecf650ede7c 2.7
Fixes #13760: picklability of ConfigParser exceptions [#13760]
Ćukasz Langa lukasz@langa.pl | |
---|---|
date | Fri, 20 Jan 2012 17:02:08 +0100 |
parents | b60c789c4ccd |
children | f6008e936fbc |
files | Lib/ConfigParser.py Lib/test/test_cfgparser.py |
diffstat | 2 files changed, 150 insertions(+), 1 deletions(-)[+] [-] Lib/ConfigParser.py 34 Lib/test/test_cfgparser.py 117 |
line wrap: on
line diff
--- a/Lib/ConfigParser.py +++ b/Lib/ConfigParser.py @@ -134,6 +134,9 @@ class Error(Exception): def repr(self): return self.message
+ str = repr class NoSectionError(Error): @@ -143,6 +146,9 @@ class NoSectionError(Error): Error.init(self, 'No section: %r' % (section,)) self.section = section
+ class DuplicateSectionError(Error): """Raised when a section is multiply-created.""" @@ -150,6 +156,9 @@ class DuplicateSectionError(Error): Error.init(self, "Section %r already exists" % section) self.section = section
+ class NoOptionError(Error): """A requested option was not found.""" @@ -159,6 +168,9 @@ class NoOptionError(Error): self.option = option self.section = section
+ class InterpolationError(Error): """Base class for interpolation-related exceptions.""" @@ -167,6 +179,9 @@ class InterpolationError(Error): self.option = option self.section = section
+ class InterpolationMissingOptionError(InterpolationError): """A string substitution required a setting which was not available.""" @@ -179,6 +194,11 @@ class InterpolationMissingOptionError(In % (section, option, reference, rawval)) InterpolationError.init(self, option, section, msg) self.reference = reference
self._rawval = rawval[](#l1.57)
- def reduce(self):
return self.__class__, (self.option, self.section, self._rawval,[](#l1.60)
self.reference)[](#l1.61)
class InterpolationSyntaxError(InterpolationError): """Raised when the source text into which substitutions are made @@ -194,19 +214,28 @@ class InterpolationDepthError(Interpolat "\trawval : %s\n" % (section, option, rawval)) InterpolationError.init(self, option, section, msg)
self._rawval = rawval[](#l1.69)
class ParsingError(Error): """Raised when a configuration file does not follow legal syntax."""
- def init(self, filename, _errors=[]): Error.init(self, 'File contains parsing errors: %s' % filename) self.filename = filename self.errors = []
for lineno, line in _errors:[](#l1.82)
self.append(lineno, line)[](#l1.83)
def append(self, lineno, line): self.errors.append((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.""" @@ -219,6 +248,9 @@ class MissingSectionHeaderError(ParsingE self.lineno = lineno self.line = line
+ class RawConfigParser: def init(self, defaults=None, dict_type=_default_dict,
--- a/Lib/test/test_cfgparser.py +++ b/Lib/test/test_cfgparser.py @@ -604,6 +604,122 @@ class SortedTestCase(RawConfigParserTest "o4 = 1\n\n") +class ExceptionPicklingTestCase(unittest.TestCase):
- def test_error(self):
import pickle[](#l2.11)
e1 = ConfigParser.Error('value')[](#l2.12)
pickled = pickle.dumps(e1)[](#l2.13)
e2 = pickle.loads(pickled)[](#l2.14)
self.assertEqual(e1.message, e2.message)[](#l2.15)
self.assertEqual(repr(e1), repr(e2))[](#l2.16)
- def test_nosectionerror(self):
import pickle[](#l2.19)
e1 = ConfigParser.NoSectionError('section')[](#l2.20)
pickled = pickle.dumps(e1)[](#l2.21)
e2 = pickle.loads(pickled)[](#l2.22)
self.assertEqual(e1.message, e2.message)[](#l2.23)
self.assertEqual(e1.args, e2.args)[](#l2.24)
self.assertEqual(e1.section, e2.section)[](#l2.25)
self.assertEqual(repr(e1), repr(e2))[](#l2.26)
- def test_nooptionerror(self):
import pickle[](#l2.29)
e1 = ConfigParser.NoOptionError('option', 'section')[](#l2.30)
pickled = pickle.dumps(e1)[](#l2.31)
e2 = pickle.loads(pickled)[](#l2.32)
self.assertEqual(e1.message, e2.message)[](#l2.33)
self.assertEqual(e1.args, e2.args)[](#l2.34)
self.assertEqual(e1.section, e2.section)[](#l2.35)
self.assertEqual(e1.option, e2.option)[](#l2.36)
self.assertEqual(repr(e1), repr(e2))[](#l2.37)
- def test_duplicatesectionerror(self):
import pickle[](#l2.40)
e1 = ConfigParser.DuplicateSectionError('section')[](#l2.41)
pickled = pickle.dumps(e1)[](#l2.42)
e2 = pickle.loads(pickled)[](#l2.43)
self.assertEqual(e1.message, e2.message)[](#l2.44)
self.assertEqual(e1.args, e2.args)[](#l2.45)
self.assertEqual(e1.section, e2.section)[](#l2.46)
self.assertEqual(repr(e1), repr(e2))[](#l2.47)
- def test_interpolationerror(self):
import pickle[](#l2.50)
e1 = ConfigParser.InterpolationError('option', 'section', 'msg')[](#l2.51)
pickled = pickle.dumps(e1)[](#l2.52)
e2 = pickle.loads(pickled)[](#l2.53)
self.assertEqual(e1.message, e2.message)[](#l2.54)
self.assertEqual(e1.args, e2.args)[](#l2.55)
self.assertEqual(e1.section, e2.section)[](#l2.56)
self.assertEqual(e1.option, e2.option)[](#l2.57)
self.assertEqual(repr(e1), repr(e2))[](#l2.58)
- def test_interpolationmissingoptionerror(self):
import pickle[](#l2.61)
e1 = ConfigParser.InterpolationMissingOptionError('option', 'section',[](#l2.62)
'rawval', 'reference')[](#l2.63)
pickled = pickle.dumps(e1)[](#l2.64)
e2 = pickle.loads(pickled)[](#l2.65)
self.assertEqual(e1.message, e2.message)[](#l2.66)
self.assertEqual(e1.args, e2.args)[](#l2.67)
self.assertEqual(e1.section, e2.section)[](#l2.68)
self.assertEqual(e1.option, e2.option)[](#l2.69)
self.assertEqual(e1.reference, e2.reference)[](#l2.70)
self.assertEqual(repr(e1), repr(e2))[](#l2.71)
- def test_interpolationsyntaxerror(self):
import pickle[](#l2.74)
e1 = ConfigParser.InterpolationSyntaxError('option', 'section', 'msg')[](#l2.75)
pickled = pickle.dumps(e1)[](#l2.76)
e2 = pickle.loads(pickled)[](#l2.77)
self.assertEqual(e1.message, e2.message)[](#l2.78)
self.assertEqual(e1.args, e2.args)[](#l2.79)
self.assertEqual(e1.section, e2.section)[](#l2.80)
self.assertEqual(e1.option, e2.option)[](#l2.81)
self.assertEqual(repr(e1), repr(e2))[](#l2.82)
- def test_interpolationdeptherror(self):
import pickle[](#l2.85)
e1 = ConfigParser.InterpolationDepthError('option', 'section',[](#l2.86)
'rawval')[](#l2.87)
pickled = pickle.dumps(e1)[](#l2.88)
e2 = pickle.loads(pickled)[](#l2.89)
self.assertEqual(e1.message, e2.message)[](#l2.90)
self.assertEqual(e1.args, e2.args)[](#l2.91)
self.assertEqual(e1.section, e2.section)[](#l2.92)
self.assertEqual(e1.option, e2.option)[](#l2.93)
self.assertEqual(repr(e1), repr(e2))[](#l2.94)
- def test_parsingerror(self):
import pickle[](#l2.97)
e1 = ConfigParser.ParsingError('source')[](#l2.98)
e1.append(1, 'line1')[](#l2.99)
e1.append(2, 'line2')[](#l2.100)
e1.append(3, 'line3')[](#l2.101)
pickled = pickle.dumps(e1)[](#l2.102)
e2 = pickle.loads(pickled)[](#l2.103)
self.assertEqual(e1.message, e2.message)[](#l2.104)
self.assertEqual(e1.args, e2.args)[](#l2.105)
self.assertEqual(e1.filename, e2.filename)[](#l2.106)
self.assertEqual(e1.errors, e2.errors)[](#l2.107)
self.assertEqual(repr(e1), repr(e2))[](#l2.108)
- def test_missingsectionheadererror(self):
import pickle[](#l2.111)
e1 = ConfigParser.MissingSectionHeaderError('filename', 123, 'line')[](#l2.112)
pickled = pickle.dumps(e1)[](#l2.113)
e2 = pickle.loads(pickled)[](#l2.114)
self.assertEqual(e1.message, e2.message)[](#l2.115)
self.assertEqual(e1.args, e2.args)[](#l2.116)
self.assertEqual(e1.line, e2.line)[](#l2.117)
self.assertEqual(e1.filename, e2.filename)[](#l2.118)
self.assertEqual(e1.lineno, e2.lineno)[](#l2.119)
self.assertEqual(repr(e1), repr(e2))[](#l2.120)
+ + def test_main(): test_support.run_unittest( ConfigParserTestCase, @@ -614,6 +730,7 @@ def test_main(): SortedTestCase, Issue7005TestCase, TestChainMap,
ExceptionPicklingTestCase,[](#l2.130) )[](#l2.131)