10a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport ConfigParser
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport StringIO
30a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport os
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport unittest
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport UserDict
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom test import test_support
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass SortedDict(UserDict.UserDict):
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def items(self):
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        result = self.data.items()
130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        result.sort()
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return result
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def keys(self):
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        result = self.data.keys()
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        result.sort()
190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return result
200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def values(self):
220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # XXX never used?
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        result = self.items()
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return [i[1] for i in result]
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def iteritems(self): return iter(self.items())
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def iterkeys(self): return iter(self.keys())
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    __iter__ = iterkeys
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def itervalues(self): return iter(self.values())
300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestCaseBase(unittest.TestCase):
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    allow_no_value = False
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def newconfig(self, defaults=None):
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if defaults is None:
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cf = self.config_class(allow_no_value=self.allow_no_value)
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cf = self.config_class(defaults,
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                        allow_no_value=self.allow_no_value)
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.cf
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def fromstring(self, string, defaults=None):
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.newconfig(defaults)
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sio = StringIO.StringIO(string)
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.readfp(sio)
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return cf
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_basic(self):
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        config_string = (
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[Foo Bar]\n"
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "foo=bar\n"
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[Spacey Bar]\n"
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "foo = bar\n"
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[Commented Bar]\n"
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "foo: bar ; comment\n"
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[Long Line]\n"
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "foo: this line is much, much longer than my editor\n"
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "   likes it.\n"
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[Section\\with$weird%characters[\t]\n"
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[Internationalized Stuff]\n"
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "foo[bg]: Bulgarian\n"
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "foo=Default\n"
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "foo[en]=English\n"
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "foo[de]=Deutsch\n"
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[Spaces]\n"
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "key with spaces : value\n"
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "another with spaces = splat!\n"
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            )
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.allow_no_value:
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            config_string += (
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "[NoValue]\n"
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "option-without-value\n"
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                )
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.fromstring(config_string)
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        L = cf.sections()
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        L.sort()
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        E = [r'Commented Bar',
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao             r'Foo Bar',
810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao             r'Internationalized Stuff',
820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao             r'Long Line',
830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao             r'Section\with$weird%characters[' '\t',
840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao             r'Spaces',
850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao             r'Spacey Bar',
860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao             ]
870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.allow_no_value:
880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            E.append(r'NoValue')
890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        E.sort()
900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq = self.assertEqual
910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(L, E)
920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # The use of spaces in the section names serves as a
940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # regression test for SourceForge bug #583248:
950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # http://www.python.org/sf/583248
960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.get('Foo Bar', 'foo'), 'bar')
970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.get('Spacey Bar', 'foo'), 'bar')
980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.get('Commented Bar', 'foo'), 'bar')
990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.get('Spaces', 'key with spaces'), 'value')
1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.get('Spaces', 'another with spaces'), 'splat!')
1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.allow_no_value:
1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            eq(cf.get('NoValue', 'option-without-value'), None)
1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertNotIn('__name__', cf.options("Foo Bar"),
1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         '__name__ "option" should not be exposed by the API!')
1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Make sure the right things happen for remove_option();
1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # added to include check for SourceForge bug #123324:
1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(cf.remove_option('Foo Bar', 'foo'),
1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        "remove_option() failed to report existence of option")
1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertFalse(cf.has_option('Foo Bar', 'foo'),
1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    "remove_option() failed to remove option")
1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertFalse(cf.remove_option('Foo Bar', 'foo'),
1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    "remove_option() failed to report non-existence of option"
1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    " that was removed")
1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ConfigParser.NoSectionError,
1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                          cf.remove_option, 'No Such Section', 'foo')
1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.get('Long Line', 'foo'),
1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           'this line is much, much longer than my editor\nlikes it.')
1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_case_sensitivity(self):
1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.newconfig()
1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.add_section("A")
1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.add_section("a")
1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        L = cf.sections()
1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        L.sort()
1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq = self.assertEqual
1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(L, ["A", "a"])
1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.set("a", "B", "value")
1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.options("a"), ["b"])
1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.get("a", "b"), "value",
1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "could not locate option, expecting case-insensitive option names")
1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(cf.has_option("a", "b"))
1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.set("A", "A-B", "A-B value")
1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for opt in ("a-b", "A-b", "a-B", "A-B"):
1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(
1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                cf.has_option("A", opt),
1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "has_option() returned false for option which should exist")
1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.options("A"), ["a-b"])
1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.options("a"), ["b"])
1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.remove_option("a", "B")
1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.options("a"), [])
1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # SF bug #432369:
1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.fromstring(
1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[MySection]\nOption: first line\n\tsecond line\n")
1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.options("MySection"), ["option"])
1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.get("MySection", "Option"), "first line\nsecond line")
1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # SF bug #561822:
1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.fromstring("[section]\nnekey=nevalue\n",
1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             defaults={"key":"value"})
1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(cf.has_option("section", "Key"))
1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_default_case_sensitivity(self):
1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.newconfig({"foo": "Bar"})
1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(
1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            cf.get("DEFAULT", "Foo"), "Bar",
1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "could not locate option, expecting case-insensitive option names")
1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.newconfig({"Foo": "Bar"})
1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(
1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            cf.get("DEFAULT", "Foo"), "Bar",
1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "could not locate option, expecting case-insensitive defaults")
1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_parse_errors(self):
1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.newconfig()
1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.parse_error(ConfigParser.ParsingError,
1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         "[Foo]\n  extra-spaces: splat\n")
1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.parse_error(ConfigParser.ParsingError,
1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         "[Foo]\n  extra-spaces= splat\n")
1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.parse_error(ConfigParser.ParsingError,
1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         "[Foo]\n:value-without-option-name\n")
1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.parse_error(ConfigParser.ParsingError,
1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         "[Foo]\n=value-without-option-name\n")
1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.parse_error(ConfigParser.MissingSectionHeaderError,
1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         "No Section!\n")
1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def parse_error(self, exc, src):
1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sio = StringIO.StringIO(src)
1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(exc, self.cf.readfp, sio)
1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_query_errors(self):
1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.newconfig()
1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cf.sections(), [],
1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         "new ConfigParser should have no defined sections")
1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertFalse(cf.has_section("Foo"),
1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         "new ConfigParser should have no acknowledged "
1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         "sections")
1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ConfigParser.NoSectionError,
1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                          cf.options, "Foo")
1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ConfigParser.NoSectionError,
1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                          cf.set, "foo", "bar", "value")
1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.get_error(ConfigParser.NoSectionError, "foo", "bar")
1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.add_section("foo")
1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.get_error(ConfigParser.NoOptionError, "foo", "bar")
1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def get_error(self, exc, section, option):
2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cf.get(section, option)
2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except exc, e:
2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return e
2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("expected exception type %s.%s"
2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                      % (exc.__module__, exc.__name__))
2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_boolean(self):
2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.fromstring(
2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[BOOLTEST]\n"
2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "T1=1\n"
2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "T2=TRUE\n"
2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "T3=True\n"
2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "T4=oN\n"
2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "T5=yes\n"
2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "F1=0\n"
2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "F2=FALSE\n"
2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "F3=False\n"
2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "F4=oFF\n"
2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "F5=nO\n"
2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "E1=2\n"
2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "E2=foo\n"
2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "E3=-1\n"
2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "E4=0.1\n"
2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "E5=FALSE AND MORE"
2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            )
2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for x in range(1, 5):
2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(cf.getboolean('BOOLTEST', 't%d' % x))
2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertFalse(cf.getboolean('BOOLTEST', 'f%d' % x))
2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertRaises(ValueError,
2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              cf.getboolean, 'BOOLTEST', 'e%d' % x)
2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_weird_errors(self):
2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.newconfig()
2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.add_section("Foo")
2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ConfigParser.DuplicateSectionError,
2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                          cf.add_section, "Foo")
2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_write(self):
2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        config_string = (
2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[Long Line]\n"
2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "foo: this line is much, much longer than my editor\n"
2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "   likes it.\n"
2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[DEFAULT]\n"
2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "foo: another very\n"
2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            " long line\n"
2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            )
2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.allow_no_value:
2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            config_string += (
2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[Valueless]\n"
2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "option-without-value\n"
2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            )
2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.fromstring(config_string)
2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        output = StringIO.StringIO()
2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.write(output)
2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        expect_string = (
2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[DEFAULT]\n"
2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "foo = another very\n"
2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "\tlong line\n"
2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "\n"
2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[Long Line]\n"
2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "foo = this line is much, much longer than my editor\n"
2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "\tlikes it.\n"
2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "\n"
2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            )
2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.allow_no_value:
2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            expect_string += (
2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "[Valueless]\n"
2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "option-without-value\n"
2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "\n"
2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                )
2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(output.getvalue(), expect_string)
2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_set_string_types(self):
2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.fromstring("[sect]\n"
2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             "option1=foo\n")
2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Check that we don't get an exception when setting values in
2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # an existing section using strings:
2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class mystr(str):
2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.set("sect", "option1", "splat")
2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.set("sect", "option1", mystr("splat"))
2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.set("sect", "option2", "splat")
2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.set("sect", "option2", mystr("splat"))
2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            unicode
2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except NameError:
2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            cf.set("sect", "option1", unicode("splat"))
2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            cf.set("sect", "option2", unicode("splat"))
2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_read_returns_file_list(self):
2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        file1 = test_support.findfile("cfgparser.1")
2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # check when we pass a mix of readable and non-readable files:
2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.newconfig()
2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        parsed_files = cf.read([file1, "nonexistent-file"])
3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(parsed_files, [file1])
3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # check when we pass only a filename:
3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.newconfig()
3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        parsed_files = cf.read(file1)
3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(parsed_files, [file1])
3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # check when we pass only missing files:
3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.newconfig()
3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        parsed_files = cf.read(["nonexistent-file"])
3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(parsed_files, [])
3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # check when we pass no files:
3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.newconfig()
3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        parsed_files = cf.read([])
3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(parsed_files, [])
3150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # shared by subclasses
3170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def get_interpolation_config(self):
3180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.fromstring(
3190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[Foo]\n"
3200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "bar=something %(with1)s interpolation (1 step)\n"
3210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "bar9=something %(with9)s lots of interpolation (9 steps)\n"
3220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "bar10=something %(with10)s lots of interpolation (10 steps)\n"
3230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "bar11=something %(with11)s lots of interpolation (11 steps)\n"
3240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "with11=%(with10)s\n"
3250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "with10=%(with9)s\n"
3260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "with9=%(with8)s\n"
3270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "with8=%(With7)s\n"
3280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "with7=%(WITH6)s\n"
3290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "with6=%(with5)s\n"
3300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "With5=%(with4)s\n"
3310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "WITH4=%(with3)s\n"
3320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "with3=%(with2)s\n"
3330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "with2=%(with1)s\n"
3340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "with1=with\n"
3350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "\n"
3360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[Mutual Recursion]\n"
3370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "foo=%(bar)s\n"
3380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "bar=%(foo)s\n"
3390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "\n"
3400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[Interpolation Error]\n"
3410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "name=%(reference)s\n",
3420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # no definition for 'reference'
3430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            defaults={"getname": "%(__name__)s"})
3440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def check_items_config(self, expected):
3460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.fromstring(
3470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "[section]\n"
3480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "name = value\n"
3490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "key: |%(name)s| \n"
3500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "getdefault: |%(default)s|\n"
3510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "getname: |%(__name__)s|",
3520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            defaults={"default": "<default>"})
3530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        L = list(cf.items("section"))
3540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        L.sort()
3550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(L, expected)
3560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3580a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass ConfigParserTestCase(TestCaseBase):
3590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    config_class = ConfigParser.ConfigParser
3600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    allow_no_value = True
3610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_interpolation(self):
3630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        rawval = {
3640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            ConfigParser.ConfigParser: ("something %(with11)s "
3650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                        "lots of interpolation (11 steps)"),
3660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            ConfigParser.SafeConfigParser: "%(with1)s",
3670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        }
3680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.get_interpolation_config()
3690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq = self.assertEqual
3700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.get("Foo", "getname"), "Foo")
3710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.get("Foo", "bar"), "something with interpolation (1 step)")
3720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.get("Foo", "bar9"),
3730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "something with lots of interpolation (9 steps)")
3740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.get("Foo", "bar10"),
3750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "something with lots of interpolation (10 steps)")
3760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.get_error(ConfigParser.InterpolationDepthError, "Foo", "bar11")
3770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_interpolation_missing_value(self):
3790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.get_interpolation_config()
3800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e = self.get_error(ConfigParser.InterpolationError,
3810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                           "Interpolation Error", "name")
3820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e.reference, "reference")
3830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e.section, "Interpolation Error")
3840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e.option, "name")
3850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_items(self):
3870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.check_items_config([('default', '<default>'),
3880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                 ('getdefault', '|<default>|'),
3890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                 ('getname', '|section|'),
3900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                 ('key', '|value|'),
3910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                 ('name', 'value')])
3920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_set_nonstring_types(self):
3940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.newconfig()
3950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.add_section('non-string')
3960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.set('non-string', 'int', 1)
3970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.set('non-string', 'list', [0, 1, 1, 2, 3, 5, 8, 13, '%('])
3980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.set('non-string', 'dict', {'pi': 3.14159, '%(': 1,
3990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                      '%(list)': '%(list)'})
4000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.set('non-string', 'string_with_interpolation', '%(list)s')
4010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.set('non-string', 'no-value')
4020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cf.get('non-string', 'int', raw=True), 1)
4030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, cf.get, 'non-string', 'int')
4040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cf.get('non-string', 'list', raw=True),
4050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         [0, 1, 1, 2, 3, 5, 8, 13, '%('])
4060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, cf.get, 'non-string', 'list')
4070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cf.get('non-string', 'dict', raw=True),
4080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         {'pi': 3.14159, '%(': 1, '%(list)': '%(list)'})
4090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, cf.get, 'non-string', 'dict')
4100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cf.get('non-string', 'string_with_interpolation',
4110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                raw=True), '%(list)s')
4120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, cf.get, 'non-string',
4130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                          'string_with_interpolation', raw=False)
4140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cf.get('non-string', 'no-value'), None)
4150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4160a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass MultilineValuesTestCase(TestCaseBase):
4170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    config_class = ConfigParser.ConfigParser
4180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    wonderful_spam = ("I'm having spam spam spam spam "
4190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                      "spam spam spam beaked beans spam "
4200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                      "spam spam and spam!").replace(' ', '\t\n')
4210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def setUp(self):
4230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.newconfig()
4240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for i in range(100):
4250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            s = 'section{}'.format(i)
4260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            cf.add_section(s)
4270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for j in range(10):
4280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                cf.set(s, 'lovely_spam{}'.format(j), self.wonderful_spam)
4290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with open(test_support.TESTFN, 'w') as f:
4300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            cf.write(f)
4310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def tearDown(self):
4330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        os.unlink(test_support.TESTFN)
4340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_dominating_multiline_values(self):
4360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # we're reading from file because this is where the code changed
4370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # during performance updates in Python 3.2
4380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf_from_file = self.newconfig()
4390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with open(test_support.TESTFN) as f:
4400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            cf_from_file.readfp(f)
4410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cf_from_file.get('section8', 'lovely_spam4'),
4420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         self.wonderful_spam.replace('\t\n', '\n'))
4430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4440a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass RawConfigParserTestCase(TestCaseBase):
4450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    config_class = ConfigParser.RawConfigParser
4460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_interpolation(self):
4480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.get_interpolation_config()
4490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq = self.assertEqual
4500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.get("Foo", "getname"), "%(__name__)s")
4510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.get("Foo", "bar"),
4520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "something %(with1)s interpolation (1 step)")
4530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.get("Foo", "bar9"),
4540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "something %(with9)s lots of interpolation (9 steps)")
4550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.get("Foo", "bar10"),
4560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "something %(with10)s lots of interpolation (10 steps)")
4570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(cf.get("Foo", "bar11"),
4580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "something %(with11)s lots of interpolation (11 steps)")
4590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_items(self):
4610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.check_items_config([('default', '<default>'),
4620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                 ('getdefault', '|%(default)s|'),
4630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                 ('getname', '|%(__name__)s|'),
4640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                 ('key', '|%(name)s|'),
4650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                 ('name', 'value')])
4660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_set_nonstring_types(self):
4680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.newconfig()
4690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.add_section('non-string')
4700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.set('non-string', 'int', 1)
4710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.set('non-string', 'list', [0, 1, 1, 2, 3, 5, 8, 13])
4720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.set('non-string', 'dict', {'pi': 3.14159})
4730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cf.get('non-string', 'int'), 1)
4740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cf.get('non-string', 'list'),
4750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         [0, 1, 1, 2, 3, 5, 8, 13])
4760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cf.get('non-string', 'dict'), {'pi': 3.14159})
4770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4790a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass SafeConfigParserTestCase(ConfigParserTestCase):
4800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    config_class = ConfigParser.SafeConfigParser
4810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_safe_interpolation(self):
4830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # See http://www.python.org/sf/511737
4840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.fromstring("[section]\n"
4850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             "option1=xxx\n"
4860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             "option2=%(option1)s/xxx\n"
4870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             "ok=%(option1)s/%%s\n"
4880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             "not_ok=%(option2)s/%%s")
4890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cf.get("section", "ok"), "xxx/%s")
4900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cf.get("section", "not_ok"), "xxx/xxx/%s")
4910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_set_malformatted_interpolation(self):
4930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.fromstring("[sect]\n"
4940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             "option1=foo\n")
4950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cf.get('sect', "option1"), "foo")
4970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, cf.set, "sect", "option1", "%foo")
4990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, cf.set, "sect", "option1", "foo%")
5000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, cf.set, "sect", "option1", "f%oo")
5010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cf.get('sect', "option1"), "foo")
5030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # bug #5741: double percents are *not* malformed
5050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf.set("sect", "option2", "foo%%bar")
5060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cf.get("sect", "option2"), "foo%bar")
5070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_set_nonstring_types(self):
5090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.fromstring("[sect]\n"
5100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             "option1=foo\n")
5110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Check that we get a TypeError when setting non-string values
5120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # in an existing section:
5130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, cf.set, "sect", "option1", 1)
5140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, cf.set, "sect", "option1", 1.0)
5150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, cf.set, "sect", "option1", object())
5160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, cf.set, "sect", "option2", 1)
5170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, cf.set, "sect", "option2", 1.0)
5180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, cf.set, "sect", "option2", object())
5190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_add_section_default_1(self):
5210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.newconfig()
5220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, cf.add_section, "default")
5230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_add_section_default_2(self):
5250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cf = self.newconfig()
5260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, cf.add_section, "DEFAULT")
5270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5290a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass SafeConfigParserTestCaseNoValue(SafeConfigParserTestCase):
5300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    allow_no_value = True
5310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5320a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestChainMap(unittest.TestCase):
5330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_issue_12717(self):
5340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        d1 = dict(red=1, green=2)
5350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        d2 = dict(green=3, blue=4)
5360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        dcomb = d2.copy()
5370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        dcomb.update(d1)
5380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cm = ConfigParser._Chainmap(d1, d2)
5390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertIsInstance(cm.keys(), list)
5400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(set(cm.keys()), set(dcomb.keys()))      # keys()
5410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(set(cm.values()), set(dcomb.values()))  # values()
5420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(set(cm.items()), set(dcomb.items()))    # items()
5430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(set(cm), set(dcomb))                    # __iter__ ()
5440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(cm, dcomb)                              # __eq__()
5450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual([cm[k] for k in dcomb], dcomb.values()) # __getitem__()
5460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        klist = 'red green blue black brown'.split()
5470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual([cm.get(k, 10) for k in klist],
5480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         [dcomb.get(k, 10) for k in klist])      # get()
5490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual([k in cm for k in klist],
5500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         [k in dcomb for k in klist])            # __contains__()
5510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with test_support.check_py3k_warnings():
5520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual([cm.has_key(k) for k in klist],
5530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             [dcomb.has_key(k) for k in klist])  # has_key()
5540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5550a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Issue7005TestCase(unittest.TestCase):
5560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Test output when None is set() as a value and allow_no_value == False.
5570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    http://bugs.python.org/issue7005
5590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
5610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    expected_output = "[section]\noption = None\n\n"
5630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def prepare(self, config_class):
5650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # This is the default, but that's the point.
5660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cp = config_class(allow_no_value=False)
5670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cp.add_section("section")
5680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cp.set("section", "option", None)
5690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sio = StringIO.StringIO()
5700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cp.write(sio)
5710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return sio.getvalue()
5720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_none_as_value_stringified(self):
5740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        output = self.prepare(ConfigParser.ConfigParser)
5750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(output, self.expected_output)
5760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_none_as_value_stringified_raw(self):
5780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        output = self.prepare(ConfigParser.RawConfigParser)
5790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(output, self.expected_output)
5800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5820a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass SortedTestCase(RawConfigParserTestCase):
5830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def newconfig(self, defaults=None):
5840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cf = self.config_class(defaults=defaults, dict_type=SortedDict)
5850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.cf
5860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_sorted(self):
5880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.fromstring("[b]\n"
5890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        "o4=1\n"
5900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        "o3=2\n"
5910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        "o2=3\n"
5920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        "o1=4\n"
5930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        "[a]\n"
5940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        "k=v\n")
5950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        output = StringIO.StringIO()
5960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cf.write(output)
5970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(output.getvalue(),
5980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         "[a]\n"
5990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         "k = v\n\n"
6000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         "[b]\n"
6010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         "o1 = 4\n"
6020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         "o2 = 3\n"
6030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         "o3 = 2\n"
6040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         "o4 = 1\n\n")
6050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6070a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass ExceptionPicklingTestCase(unittest.TestCase):
6080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Tests for issue #13760: ConfigParser exceptions are not picklable."""
6090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_error(self):
6110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        import pickle
6120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e1 = ConfigParser.Error('value')
6130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pickled = pickle.dumps(e1)
6140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e2 = pickle.loads(pickled)
6150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.message, e2.message)
6160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(repr(e1), repr(e2))
6170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_nosectionerror(self):
6190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        import pickle
6200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e1 = ConfigParser.NoSectionError('section')
6210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pickled = pickle.dumps(e1)
6220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e2 = pickle.loads(pickled)
6230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.message, e2.message)
6240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.args, e2.args)
6250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.section, e2.section)
6260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(repr(e1), repr(e2))
6270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_nooptionerror(self):
6290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        import pickle
6300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e1 = ConfigParser.NoOptionError('option', 'section')
6310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pickled = pickle.dumps(e1)
6320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e2 = pickle.loads(pickled)
6330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.message, e2.message)
6340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.args, e2.args)
6350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.section, e2.section)
6360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.option, e2.option)
6370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(repr(e1), repr(e2))
6380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_duplicatesectionerror(self):
6400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        import pickle
6410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e1 = ConfigParser.DuplicateSectionError('section')
6420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pickled = pickle.dumps(e1)
6430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e2 = pickle.loads(pickled)
6440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.message, e2.message)
6450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.args, e2.args)
6460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.section, e2.section)
6470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(repr(e1), repr(e2))
6480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_interpolationerror(self):
6500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        import pickle
6510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e1 = ConfigParser.InterpolationError('option', 'section', 'msg')
6520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pickled = pickle.dumps(e1)
6530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e2 = pickle.loads(pickled)
6540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.message, e2.message)
6550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.args, e2.args)
6560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.section, e2.section)
6570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.option, e2.option)
6580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(repr(e1), repr(e2))
6590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_interpolationmissingoptionerror(self):
6610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        import pickle
6620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e1 = ConfigParser.InterpolationMissingOptionError('option', 'section',
6630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            'rawval', 'reference')
6640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pickled = pickle.dumps(e1)
6650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e2 = pickle.loads(pickled)
6660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.message, e2.message)
6670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.args, e2.args)
6680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.section, e2.section)
6690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.option, e2.option)
6700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.reference, e2.reference)
6710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(repr(e1), repr(e2))
6720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_interpolationsyntaxerror(self):
6740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        import pickle
6750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e1 = ConfigParser.InterpolationSyntaxError('option', 'section', 'msg')
6760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pickled = pickle.dumps(e1)
6770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e2 = pickle.loads(pickled)
6780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.message, e2.message)
6790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.args, e2.args)
6800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.section, e2.section)
6810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.option, e2.option)
6820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(repr(e1), repr(e2))
6830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_interpolationdeptherror(self):
6850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        import pickle
6860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e1 = ConfigParser.InterpolationDepthError('option', 'section',
6870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            'rawval')
6880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pickled = pickle.dumps(e1)
6890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e2 = pickle.loads(pickled)
6900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.message, e2.message)
6910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.args, e2.args)
6920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.section, e2.section)
6930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.option, e2.option)
6940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(repr(e1), repr(e2))
6950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_parsingerror(self):
6970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        import pickle
6980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e1 = ConfigParser.ParsingError('source')
6990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e1.append(1, 'line1')
7000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e1.append(2, 'line2')
7010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e1.append(3, 'line3')
7020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pickled = pickle.dumps(e1)
7030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e2 = pickle.loads(pickled)
7040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.message, e2.message)
7050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.args, e2.args)
7060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.filename, e2.filename)
7070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.errors, e2.errors)
7080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(repr(e1), repr(e2))
7090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_missingsectionheadererror(self):
7110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        import pickle
7120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e1 = ConfigParser.MissingSectionHeaderError('filename', 123, 'line')
7130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pickled = pickle.dumps(e1)
7140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e2 = pickle.loads(pickled)
7150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.message, e2.message)
7160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.args, e2.args)
7170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.line, e2.line)
7180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.filename, e2.filename)
7190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(e1.lineno, e2.lineno)
7200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(repr(e1), repr(e2))
7210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7230a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_main():
7240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    test_support.run_unittest(
7250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ConfigParserTestCase,
7260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        MultilineValuesTestCase,
7270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        RawConfigParserTestCase,
7280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        SafeConfigParserTestCase,
7290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        SafeConfigParserTestCaseNoValue,
7300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        SortedTestCase,
7310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Issue7005TestCase,
7320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        TestChainMap,
7330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ExceptionPicklingTestCase,
7340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        )
7350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7370a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == "__main__":
7380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    test_main()
739