annotate Lib/test/test_cfgparser.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 5ecf650ede7c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
1 import ConfigParser
14900
f932c128279b Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2 import StringIO
63221
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
3 import os
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
4 import unittest
40418
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
5 import UserDict
14900
f932c128279b Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
7 from test import test_support
15480
3c33f2c357d3 Add test cases for ConfigParser.remove_option() behavior. This includes
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
8
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
9
40418
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
10 class SortedDict(UserDict.UserDict):
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
11 def items(self):
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
12 result = self.data.items()
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
13 result.sort()
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
14 return result
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
15
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
16 def keys(self):
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
17 result = self.data.keys()
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
18 result.sort()
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
19 return result
40732
5dae0d7f683a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40418
diff changeset
20
40418
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
21 def values(self):
58795
ca67c37cd26d Merged revisions 78035,78040,78043,78049-78050,78052-78054 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55049
diff changeset
22 # XXX never used?
40418
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
23 result = self.items()
58795
ca67c37cd26d Merged revisions 78035,78040,78043,78049-78050,78052-78054 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55049
diff changeset
24 return [i[1] for i in result]
40418
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
25
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
26 def iteritems(self): return iter(self.items())
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
27 def iterkeys(self): return iter(self.keys())
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
28 __iter__ = iterkeys
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
29 def itervalues(self): return iter(self.values())
22296
9a091d72136a backport akuchling's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20321
diff changeset
30
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
31
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
32 class TestCaseBase(unittest.TestCase):
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
33 allow_no_value = False
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
34
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
35 def newconfig(self, defaults=None):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
36 if defaults is None:
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
37 self.cf = self.config_class(allow_no_value=self.allow_no_value)
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
38 else:
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
39 self.cf = self.config_class(defaults,
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
40 allow_no_value=self.allow_no_value)
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
41 return self.cf
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
42
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
43 def fromstring(self, string, defaults=None):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
44 cf = self.newconfig(defaults)
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
45 sio = StringIO.StringIO(string)
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
46 cf.readfp(sio)
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
47 return cf
16771
b235160f1346 Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents: 16595
diff changeset
48
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
49 def test_basic(self):
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
50 config_string = (
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
51 "[Foo Bar]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
52 "foo=bar\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
53 "[Spacey Bar]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
54 "foo = bar\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
55 "[Commented Bar]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
56 "foo: bar ; comment\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
57 "[Long Line]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
58 "foo: this line is much, much longer than my editor\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
59 " likes it.\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
60 "[Section\\with$weird%characters[\t]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
61 "[Internationalized Stuff]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
62 "foo[bg]: Bulgarian\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
63 "foo=Default\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
64 "foo[en]=English\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
65 "foo[de]=Deutsch\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
66 "[Spaces]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
67 "key with spaces : value\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
68 "another with spaces = splat!\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
69 )
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
70 if self.allow_no_value:
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
71 config_string += (
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
72 "[NoValue]\n"
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
73 "option-without-value\n"
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
74 )
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
75
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
76 cf = self.fromstring(config_string)
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
77 L = cf.sections()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
78 L.sort()
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
79 E = [r'Commented Bar',
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
80 r'Foo Bar',
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
81 r'Internationalized Stuff',
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
82 r'Long Line',
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
83 r'Section\with$weird%characters[' '\t',
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
84 r'Spaces',
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
85 r'Spacey Bar',
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
86 ]
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
87 if self.allow_no_value:
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
88 E.append(r'NoValue')
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
89 E.sort()
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
90 eq = self.assertEqual
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
91 eq(L, E)
16771
b235160f1346 Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents: 16595
diff changeset
92
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
93 # The use of spaces in the section names serves as a
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
94 # regression test for SourceForge bug #583248:
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
95 # http://www.python.org/sf/583248
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
96 eq(cf.get('Foo Bar', 'foo'), 'bar')
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
97 eq(cf.get('Spacey Bar', 'foo'), 'bar')
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
98 eq(cf.get('Commented Bar', 'foo'), 'bar')
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
99 eq(cf.get('Spaces', 'key with spaces'), 'value')
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
100 eq(cf.get('Spaces', 'another with spaces'), 'splat!')
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
101 if self.allow_no_value:
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
102 eq(cf.get('NoValue', 'option-without-value'), None)
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
103
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
104 self.assertNotIn('__name__', cf.options("Foo Bar"),
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
105 '__name__ "option" should not be exposed by the API!')
20321
267c6fe0abc2 Added tests that check getboolean() with the newly allowed values from
Fred Drake <fdrake@acm.org>
parents: 18407
diff changeset
106
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
107 # Make sure the right things happen for remove_option();
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
108 # added to include check for SourceForge bug #123324:
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53108
diff changeset
109 self.assertTrue(cf.remove_option('Foo Bar', 'foo'),
55049
74c0a78114ae Merged revisions 69846 via svnmerge from
Jesus Cea <jcea@jcea.es>
parents: 53109
diff changeset
110 "remove_option() failed to report existence of option")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53108
diff changeset
111 self.assertFalse(cf.has_option('Foo Bar', 'foo'),
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
112 "remove_option() failed to remove option")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53108
diff changeset
113 self.assertFalse(cf.remove_option('Foo Bar', 'foo'),
55049
74c0a78114ae Merged revisions 69846 via svnmerge from
Jesus Cea <jcea@jcea.es>
parents: 53109
diff changeset
114 "remove_option() failed to report non-existence of option"
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
115 " that was removed")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
116
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
117 self.assertRaises(ConfigParser.NoSectionError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
118 cf.remove_option, 'No Such Section', 'foo')
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
119
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
120 eq(cf.get('Long Line', 'foo'),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
121 'this line is much, much longer than my editor\nlikes it.')
20321
267c6fe0abc2 Added tests that check getboolean() with the newly allowed values from
Fred Drake <fdrake@acm.org>
parents: 18407
diff changeset
122
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
123 def test_case_sensitivity(self):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
124 cf = self.newconfig()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
125 cf.add_section("A")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
126 cf.add_section("a")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
127 L = cf.sections()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
128 L.sort()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
129 eq = self.assertEqual
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
130 eq(L, ["A", "a"])
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
131 cf.set("a", "B", "value")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
132 eq(cf.options("a"), ["b"])
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
133 eq(cf.get("a", "b"), "value",
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
134 "could not locate option, expecting case-insensitive option names")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53108
diff changeset
135 self.assertTrue(cf.has_option("a", "b"))
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
136 cf.set("A", "A-B", "A-B value")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
137 for opt in ("a-b", "A-b", "a-B", "A-B"):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53108
diff changeset
138 self.assertTrue(
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
139 cf.has_option("A", opt),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
140 "has_option() returned false for option which should exist")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
141 eq(cf.options("A"), ["a-b"])
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
142 eq(cf.options("a"), ["b"])
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
143 cf.remove_option("a", "B")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
144 eq(cf.options("a"), [])
14900
f932c128279b Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
145
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
146 # SF bug #432369:
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
147 cf = self.fromstring(
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
148 "[MySection]\nOption: first line\n\tsecond line\n")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
149 eq(cf.options("MySection"), ["option"])
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
150 eq(cf.get("MySection", "Option"), "first line\nsecond line")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
151
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
152 # SF bug #561822:
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
153 cf = self.fromstring("[section]\nnekey=nevalue\n",
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
154 defaults={"key":"value"})
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53108
diff changeset
155 self.assertTrue(cf.has_option("section", "Key"))
25909
0bac66e1b356 Added (very) minimal tests of the RawConfigParser class.
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
156
16576
f654468b9ae8 Added options that use square brackets in their names; this ensures that
Fred Drake <fdrake@acm.org>
parents: 15480
diff changeset
157
33662
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
158 def test_default_case_sensitivity(self):
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
159 cf = self.newconfig({"foo": "Bar"})
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
160 self.assertEqual(
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
161 cf.get("DEFAULT", "Foo"), "Bar",
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
162 "could not locate option, expecting case-insensitive option names")
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
163 cf = self.newconfig({"Foo": "Bar"})
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
164 self.assertEqual(
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
165 cf.get("DEFAULT", "Foo"), "Bar",
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
166 "could not locate option, expecting case-insensitive defaults")
cc4684051c46 SF bug #1017864: ConfigParser now correctly handles default keys, processing them with ``ConfigParser.optionxform`` when supplied, consistent with the handling of config file entries and runtime-set options.
David Goodger <goodger@python.org>
parents: 32117
diff changeset
167
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
168 def test_parse_errors(self):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
169 self.newconfig()
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
170 self.parse_error(ConfigParser.ParsingError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
171 "[Foo]\n extra-spaces: splat\n")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
172 self.parse_error(ConfigParser.ParsingError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
173 "[Foo]\n extra-spaces= splat\n")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
174 self.parse_error(ConfigParser.ParsingError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
175 "[Foo]\n:value-without-option-name\n")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
176 self.parse_error(ConfigParser.ParsingError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
177 "[Foo]\n=value-without-option-name\n")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
178 self.parse_error(ConfigParser.MissingSectionHeaderError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
179 "No Section!\n")
14900
f932c128279b Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
180
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
181 def parse_error(self, exc, src):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
182 sio = StringIO.StringIO(src)
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
183 self.assertRaises(exc, self.cf.readfp, sio)
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
184
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
185 def test_query_errors(self):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
186 cf = self.newconfig()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
187 self.assertEqual(cf.sections(), [],
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
188 "new ConfigParser should have no defined sections")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53108
diff changeset
189 self.assertFalse(cf.has_section("Foo"),
63221
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
190 "new ConfigParser should have no acknowledged "
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
191 "sections")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
192 self.assertRaises(ConfigParser.NoSectionError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
193 cf.options, "Foo")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
194 self.assertRaises(ConfigParser.NoSectionError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
195 cf.set, "foo", "bar", "value")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
196 self.get_error(ConfigParser.NoSectionError, "foo", "bar")
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
197 cf.add_section("foo")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
198 self.get_error(ConfigParser.NoOptionError, "foo", "bar")
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
199
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
200 def get_error(self, exc, section, option):
26928
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
201 try:
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
202 self.cf.get(section, option)
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
203 except exc, e:
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
204 return e
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
205 else:
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
206 self.fail("expected exception type %s.%s"
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
207 % (exc.__module__, exc.__name__))
16576
f654468b9ae8 Added options that use square brackets in their names; this ensures that
Fred Drake <fdrake@acm.org>
parents: 15480
diff changeset
208
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
209 def test_boolean(self):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
210 cf = self.fromstring(
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
211 "[BOOLTEST]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
212 "T1=1\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
213 "T2=TRUE\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
214 "T3=True\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
215 "T4=oN\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
216 "T5=yes\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
217 "F1=0\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
218 "F2=FALSE\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
219 "F3=False\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
220 "F4=oFF\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
221 "F5=nO\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
222 "E1=2\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
223 "E2=foo\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
224 "E3=-1\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
225 "E4=0.1\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
226 "E5=FALSE AND MORE"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
227 )
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
228 for x in range(1, 5):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53108
diff changeset
229 self.assertTrue(cf.getboolean('BOOLTEST', 't%d' % x))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 53108
diff changeset
230 self.assertFalse(cf.getboolean('BOOLTEST', 'f%d' % x))
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
231 self.assertRaises(ValueError,
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
232 cf.getboolean, 'BOOLTEST', 'e%d' % x)
14900
f932c128279b Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
233
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
234 def test_weird_errors(self):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
235 cf = self.newconfig()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
236 cf.add_section("Foo")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
237 self.assertRaises(ConfigParser.DuplicateSectionError,
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
238 cf.add_section, "Foo")
16576
f654468b9ae8 Added options that use square brackets in their names; this ensures that
Fred Drake <fdrake@acm.org>
parents: 15480
diff changeset
239
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
240 def test_write(self):
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
241 config_string = (
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
242 "[Long Line]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
243 "foo: this line is much, much longer than my editor\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
244 " likes it.\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
245 "[DEFAULT]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
246 "foo: another very\n"
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
247 " long line\n"
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
248 )
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
249 if self.allow_no_value:
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
250 config_string += (
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
251 "[Valueless]\n"
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
252 "option-without-value\n"
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
253 )
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
254
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
255 cf = self.fromstring(config_string)
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
256 output = StringIO.StringIO()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
257 cf.write(output)
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
258 expect_string = (
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
259 "[DEFAULT]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
260 "foo = another very\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
261 "\tlong line\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
262 "\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
263 "[Long Line]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
264 "foo = this line is much, much longer than my editor\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
265 "\tlikes it.\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
266 "\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
267 )
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
268 if self.allow_no_value:
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
269 expect_string += (
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
270 "[Valueless]\n"
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
271 "option-without-value\n"
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
272 "\n"
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
273 )
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
274 self.assertEqual(output.getvalue(), expect_string)
14900
f932c128279b Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
275
32115
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
276 def test_set_string_types(self):
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
277 cf = self.fromstring("[sect]\n"
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
278 "option1=foo\n")
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
279 # Check that we don't get an exception when setting values in
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
280 # an existing section using strings:
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
281 class mystr(str):
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
282 pass
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
283 cf.set("sect", "option1", "splat")
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
284 cf.set("sect", "option1", mystr("splat"))
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
285 cf.set("sect", "option2", "splat")
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
286 cf.set("sect", "option2", mystr("splat"))
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
287 try:
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
288 unicode
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
289 except NameError:
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
290 pass
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
291 else:
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
292 cf.set("sect", "option1", unicode("splat"))
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
293 cf.set("sect", "option2", unicode("splat"))
84dfa9a67da1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32114
diff changeset
294
32117
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
295 def test_read_returns_file_list(self):
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
296 file1 = test_support.findfile("cfgparser.1")
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
297 # check when we pass a mix of readable and non-readable files:
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
298 cf = self.newconfig()
55049
74c0a78114ae Merged revisions 69846 via svnmerge from
Jesus Cea <jcea@jcea.es>
parents: 53109
diff changeset
299 parsed_files = cf.read([file1, "nonexistent-file"])
32117
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
300 self.assertEqual(parsed_files, [file1])
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
301 self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
302 # check when we pass only a filename:
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
303 cf = self.newconfig()
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
304 parsed_files = cf.read(file1)
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
305 self.assertEqual(parsed_files, [file1])
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
306 self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
307 # check when we pass only missing files:
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
308 cf = self.newconfig()
55049
74c0a78114ae Merged revisions 69846 via svnmerge from
Jesus Cea <jcea@jcea.es>
parents: 53109
diff changeset
309 parsed_files = cf.read(["nonexistent-file"])
32117
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
310 self.assertEqual(parsed_files, [])
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
311 # check when we pass no files:
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
312 cf = self.newconfig()
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
313 parsed_files = cf.read([])
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
314 self.assertEqual(parsed_files, [])
29a59f662dd1 ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 32115
diff changeset
315
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
316 # shared by subclasses
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
317 def get_interpolation_config(self):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
318 return self.fromstring(
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
319 "[Foo]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
320 "bar=something %(with1)s interpolation (1 step)\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
321 "bar9=something %(with9)s lots of interpolation (9 steps)\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
322 "bar10=something %(with10)s lots of interpolation (10 steps)\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
323 "bar11=something %(with11)s lots of interpolation (11 steps)\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
324 "with11=%(with10)s\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
325 "with10=%(with9)s\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
326 "with9=%(with8)s\n"
32114
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 28917
diff changeset
327 "with8=%(With7)s\n"
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 28917
diff changeset
328 "with7=%(WITH6)s\n"
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
329 "with6=%(with5)s\n"
32114
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 28917
diff changeset
330 "With5=%(with4)s\n"
70023af2085a ConfigParser:
Fred Drake <fdrake@acm.org>
parents: 28917
diff changeset
331 "WITH4=%(with3)s\n"
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
332 "with3=%(with2)s\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
333 "with2=%(with1)s\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
334 "with1=with\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
335 "\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
336 "[Mutual Recursion]\n"
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
337 "foo=%(bar)s\n"
26928
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
338 "bar=%(foo)s\n"
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
339 "\n"
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
340 "[Interpolation Error]\n"
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
341 "name=%(reference)s\n",
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
342 # no definition for 'reference'
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
343 defaults={"getname": "%(__name__)s"})
16576
f654468b9ae8 Added options that use square brackets in their names; this ensures that
Fred Drake <fdrake@acm.org>
parents: 15480
diff changeset
344
25917
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
345 def check_items_config(self, expected):
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
346 cf = self.fromstring(
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
347 "[section]\n"
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
348 "name = value\n"
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
349 "key: |%(name)s| \n"
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
350 "getdefault: |%(default)s|\n"
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
351 "getname: |%(__name__)s|",
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
352 defaults={"default": "<default>"})
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
353 L = list(cf.items("section"))
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
354 L.sort()
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
355 self.assertEqual(L, expected)
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
356
14900
f932c128279b Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
357
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
358 class ConfigParserTestCase(TestCaseBase):
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
359 config_class = ConfigParser.ConfigParser
63910
4f76ce69aed0 Issue #9551: Do not raise TypeError when setting the value to None for
Fred Drake <fdrake@acm.org>
parents: 63221
diff changeset
360 allow_no_value = True
25909
0bac66e1b356 Added (very) minimal tests of the RawConfigParser class.
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
361
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
362 def test_interpolation(self):
63221
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
363 rawval = {
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
364 ConfigParser.ConfigParser: ("something %(with11)s "
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
365 "lots of interpolation (11 steps)"),
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
366 ConfigParser.SafeConfigParser: "%(with1)s",
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
367 }
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
368 cf = self.get_interpolation_config()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
369 eq = self.assertEqual
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
370 eq(cf.get("Foo", "getname"), "Foo")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
371 eq(cf.get("Foo", "bar"), "something with interpolation (1 step)")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
372 eq(cf.get("Foo", "bar9"),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
373 "something with lots of interpolation (9 steps)")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
374 eq(cf.get("Foo", "bar10"),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
375 "something with lots of interpolation (10 steps)")
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
376 self.get_error(ConfigParser.InterpolationDepthError, "Foo", "bar11")
25909
0bac66e1b356 Added (very) minimal tests of the RawConfigParser class.
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
377
26928
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
378 def test_interpolation_missing_value(self):
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
379 self.get_interpolation_config()
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
380 e = self.get_error(ConfigParser.InterpolationError,
26928
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
381 "Interpolation Error", "name")
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
382 self.assertEqual(e.reference, "reference")
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
383 self.assertEqual(e.section, "Interpolation Error")
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
384 self.assertEqual(e.option, "name")
24cb62225040 Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents: 25918
diff changeset
385
25917
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
386 def test_items(self):
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
387 self.check_items_config([('default', '<default>'),
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
388 ('getdefault', '|<default>|'),
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
389 ('getname', '|section|'),
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
390 ('key', '|value|'),
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
391 ('name', 'value')])
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
392
33663
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
393 def test_set_nonstring_types(self):
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
394 cf = self.newconfig()
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
395 cf.add_section('non-string')
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
396 cf.set('non-string', 'int', 1)
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
397 cf.set('non-string', 'list', [0, 1, 1, 2, 3, 5, 8, 13, '%('])
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
398 cf.set('non-string', 'dict', {'pi': 3.14159, '%(': 1,
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
399 '%(list)': '%(list)'})
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
400 cf.set('non-string', 'string_with_interpolation', '%(list)s')
63910
4f76ce69aed0 Issue #9551: Do not raise TypeError when setting the value to None for
Fred Drake <fdrake@acm.org>
parents: 63221
diff changeset
401 cf.set('non-string', 'no-value')
33663
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
402 self.assertEqual(cf.get('non-string', 'int', raw=True), 1)
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
403 self.assertRaises(TypeError, cf.get, 'non-string', 'int')
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
404 self.assertEqual(cf.get('non-string', 'list', raw=True),
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
405 [0, 1, 1, 2, 3, 5, 8, 13, '%('])
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
406 self.assertRaises(TypeError, cf.get, 'non-string', 'list')
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
407 self.assertEqual(cf.get('non-string', 'dict', raw=True),
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
408 {'pi': 3.14159, '%(': 1, '%(list)': '%(list)'})
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
409 self.assertRaises(TypeError, cf.get, 'non-string', 'dict')
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
410 self.assertEqual(cf.get('non-string', 'string_with_interpolation',
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
411 raw=True), '%(list)s')
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
412 self.assertRaises(ValueError, cf.get, 'non-string',
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
413 'string_with_interpolation', raw=False)
63910
4f76ce69aed0 Issue #9551: Do not raise TypeError when setting the value to None for
Fred Drake <fdrake@acm.org>
parents: 63221
diff changeset
414 self.assertEqual(cf.get('non-string', 'no-value'), None)
33663
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
415
63221
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
416 class MultilineValuesTestCase(TestCaseBase):
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
417 config_class = ConfigParser.ConfigParser
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
418 wonderful_spam = ("I'm having spam spam spam spam "
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
419 "spam spam spam beaked beans spam "
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
420 "spam spam and spam!").replace(' ', '\t\n')
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
421
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
422 def setUp(self):
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
423 cf = self.newconfig()
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
424 for i in range(100):
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
425 s = 'section{}'.format(i)
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
426 cf.add_section(s)
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
427 for j in range(10):
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
428 cf.set(s, 'lovely_spam{}'.format(j), self.wonderful_spam)
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
429 with open(test_support.TESTFN, 'w') as f:
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
430 cf.write(f)
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
431
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
432 def tearDown(self):
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
433 os.unlink(test_support.TESTFN)
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
434
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
435 def test_dominating_multiline_values(self):
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
436 # we're reading from file because this is where the code changed
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
437 # during performance updates in Python 3.2
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
438 cf_from_file = self.newconfig()
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
439 with open(test_support.TESTFN) as f:
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
440 cf_from_file.readfp(f)
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
441 self.assertEqual(cf_from_file.get('section8', 'lovely_spam4'),
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
442 self.wonderful_spam.replace('\t\n', '\n'))
25909
0bac66e1b356 Added (very) minimal tests of the RawConfigParser class.
Fred Drake <fdrake@acm.org>
parents: 25462
diff changeset
443
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
444 class RawConfigParserTestCase(TestCaseBase):
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
445 config_class = ConfigParser.RawConfigParser
14900
f932c128279b Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
446
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
447 def test_interpolation(self):
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
448 cf = self.get_interpolation_config()
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
449 eq = self.assertEqual
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
450 eq(cf.get("Foo", "getname"), "%(__name__)s")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
451 eq(cf.get("Foo", "bar"),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
452 "something %(with1)s interpolation (1 step)")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
453 eq(cf.get("Foo", "bar9"),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
454 "something %(with9)s lots of interpolation (9 steps)")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
455 eq(cf.get("Foo", "bar10"),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
456 "something %(with10)s lots of interpolation (10 steps)")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
457 eq(cf.get("Foo", "bar11"),
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
458 "something %(with11)s lots of interpolation (11 steps)")
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
459
25917
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
460 def test_items(self):
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
461 self.check_items_config([('default', '<default>'),
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
462 ('getdefault', '|%(default)s|'),
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
463 ('getname', '|%(__name__)s|'),
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
464 ('key', '|%(name)s|'),
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
465 ('name', 'value')])
5b6d55b45287 Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents: 25910
diff changeset
466
33663
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
467 def test_set_nonstring_types(self):
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
468 cf = self.newconfig()
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
469 cf.add_section('non-string')
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
470 cf.set('non-string', 'int', 1)
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
471 cf.set('non-string', 'list', [0, 1, 1, 2, 3, 5, 8, 13])
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
472 cf.set('non-string', 'dict', {'pi': 3.14159})
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
473 self.assertEqual(cf.get('non-string', 'int'), 1)
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
474 self.assertEqual(cf.get('non-string', 'list'),
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
475 [0, 1, 1, 2, 3, 5, 8, 13])
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
476 self.assertEqual(cf.get('non-string', 'dict'), {'pi': 3.14159})
33665
5def66f2c9ab Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33663
diff changeset
477
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
478
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
479 class SafeConfigParserTestCase(ConfigParserTestCase):
47252
e534ee0a4cd0 ConfigParser renaming reversal part 3: move module into place and adapt imports.
Georg Brandl <georg@python.org>
parents: 47009
diff changeset
480 config_class = ConfigParser.SafeConfigParser
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
481
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
482 def test_safe_interpolation(self):
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
483 # See http://www.python.org/sf/511737
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
484 cf = self.fromstring("[section]\n"
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
485 "option1=xxx\n"
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
486 "option2=%(option1)s/xxx\n"
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
487 "ok=%(option1)s/%%s\n"
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
488 "not_ok=%(option2)s/%%s")
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
489 self.assertEqual(cf.get("section", "ok"), "xxx/%s")
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
490 self.assertEqual(cf.get("section", "not_ok"), "xxx/xxx/%s")
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
491
41157
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
492 def test_set_malformatted_interpolation(self):
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
493 cf = self.fromstring("[sect]\n"
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
494 "option1=foo\n")
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
495
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
496 self.assertEqual(cf.get('sect', "option1"), "foo")
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
497
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
498 self.assertRaises(ValueError, cf.set, "sect", "option1", "%foo")
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
499 self.assertRaises(ValueError, cf.set, "sect", "option1", "foo%")
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
500 self.assertRaises(ValueError, cf.set, "sect", "option1", "f%oo")
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
501
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
502 self.assertEqual(cf.get('sect', "option1"), "foo")
53ee2795287b Patch #1603688: ConfigParser.SafeConfigParser now checks values that
Georg Brandl <georg@python.org>
parents: 40732
diff changeset
503
53109
83228bbe5906 Merged revisions 71537 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47252
diff changeset
504 # bug #5741: double percents are *not* malformed
83228bbe5906 Merged revisions 71537 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47252
diff changeset
505 cf.set("sect", "option2", "foo%%bar")
83228bbe5906 Merged revisions 71537 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47252
diff changeset
506 self.assertEqual(cf.get("sect", "option2"), "foo%bar")
83228bbe5906 Merged revisions 71537 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47252
diff changeset
507
33663
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
508 def test_set_nonstring_types(self):
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
509 cf = self.fromstring("[sect]\n"
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
510 "option1=foo\n")
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
511 # Check that we get a TypeError when setting non-string values
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
512 # in an existing section:
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
513 self.assertRaises(TypeError, cf.set, "sect", "option1", 1)
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
514 self.assertRaises(TypeError, cf.set, "sect", "option1", 1.0)
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
515 self.assertRaises(TypeError, cf.set, "sect", "option1", object())
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
516 self.assertRaises(TypeError, cf.set, "sect", "option2", 1)
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
517 self.assertRaises(TypeError, cf.set, "sect", "option2", 1.0)
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
518 self.assertRaises(TypeError, cf.set, "sect", "option2", object())
5412096d0f95 SF bug #997050: Document, test, & check for non-string values in ConfigParser. Moved the new string-only restriction added in rev. 1.65 to the SafeConfigParser class, leaving existing ConfigParser & RawConfigParser behavior alone, and documented the conditions under which non-string values work.
David Goodger <goodger@python.org>
parents: 33662
diff changeset
519
45445
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 41157
diff changeset
520 def test_add_section_default_1(self):
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 41157
diff changeset
521 cf = self.newconfig()
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 41157
diff changeset
522 self.assertRaises(ValueError, cf.add_section, "default")
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 41157
diff changeset
523
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 41157
diff changeset
524 def test_add_section_default_2(self):
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 41157
diff changeset
525 cf = self.newconfig()
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 41157
diff changeset
526 self.assertRaises(ValueError, cf.add_section, "DEFAULT")
dda92a097e8c Issue 1781. Now ConfigParser.add_section does not let you add a
Facundo Batista <facundobatista@gmail.com>
parents: 41157
diff changeset
527
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
528
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
529 class SafeConfigParserTestCaseNoValue(SafeConfigParserTestCase):
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
530 allow_no_value = True
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
531
71789
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
532 class TestChainMap(unittest.TestCase):
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
533 def test_issue_12717(self):
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
534 d1 = dict(red=1, green=2)
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
535 d2 = dict(green=3, blue=4)
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
536 dcomb = d2.copy()
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
537 dcomb.update(d1)
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
538 cm = ConfigParser._Chainmap(d1, d2)
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
539 self.assertIsInstance(cm.keys(), list)
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
540 self.assertEqual(set(cm.keys()), set(dcomb.keys())) # keys()
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
541 self.assertEqual(set(cm.values()), set(dcomb.values())) # values()
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
542 self.assertEqual(set(cm.items()), set(dcomb.items())) # items()
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
543 self.assertEqual(set(cm), set(dcomb)) # __iter__ ()
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
544 self.assertEqual(cm, dcomb) # __eq__()
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
545 self.assertEqual([cm[k] for k in dcomb], dcomb.values()) # __getitem__()
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
546 klist = 'red green blue black brown'.split()
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
547 self.assertEqual([cm.get(k, 10) for k in klist],
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
548 [dcomb.get(k, 10) for k in klist]) # get()
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
549 self.assertEqual([k in cm for k in klist],
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
550 [k in dcomb for k in klist]) # __contains__()
73406
a6d70861bf6d Silence a couple of warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71789
diff changeset
551 with test_support.check_py3k_warnings():
a6d70861bf6d Silence a couple of warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71789
diff changeset
552 self.assertEqual([cm.has_key(k) for k in klist],
a6d70861bf6d Silence a couple of warnings.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 71789
diff changeset
553 [dcomb.has_key(k) for k in klist]) # has_key()
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
554
64385
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
555 class Issue7005TestCase(unittest.TestCase):
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
556 """Test output when None is set() as a value and allow_no_value == False.
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
557
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
558 http://bugs.python.org/issue7005
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
559
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
560 """
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
561
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
562 expected_output = "[section]\noption = None\n\n"
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
563
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
564 def prepare(self, config_class):
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
565 # This is the default, but that's the point.
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
566 cp = config_class(allow_no_value=False)
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
567 cp.add_section("section")
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
568 cp.set("section", "option", None)
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
569 sio = StringIO.StringIO()
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
570 cp.write(sio)
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
571 return sio.getvalue()
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
572
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
573 def test_none_as_value_stringified(self):
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
574 output = self.prepare(ConfigParser.ConfigParser)
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
575 self.assertEqual(output, self.expected_output)
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
576
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
577 def test_none_as_value_stringified_raw(self):
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
578 output = self.prepare(ConfigParser.RawConfigParser)
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
579 self.assertEqual(output, self.expected_output)
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
580
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
581
40418
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
582 class SortedTestCase(RawConfigParserTestCase):
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
583 def newconfig(self, defaults=None):
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
584 self.cf = self.config_class(defaults=defaults, dict_type=SortedDict)
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
585 return self.cf
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
586
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
587 def test_sorted(self):
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
588 self.fromstring("[b]\n"
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
589 "o4=1\n"
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
590 "o3=2\n"
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
591 "o2=3\n"
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
592 "o1=4\n"
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
593 "[a]\n"
40732
5dae0d7f683a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40418
diff changeset
594 "k=v\n")
40418
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
595 output = StringIO.StringIO()
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
596 self.cf.write(output)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64385
diff changeset
597 self.assertEqual(output.getvalue(),
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64385
diff changeset
598 "[a]\n"
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64385
diff changeset
599 "k = v\n\n"
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64385
diff changeset
600 "[b]\n"
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64385
diff changeset
601 "o1 = 4\n"
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64385
diff changeset
602 "o2 = 3\n"
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64385
diff changeset
603 "o3 = 2\n"
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 64385
diff changeset
604 "o4 = 1\n\n")
25918
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
605
9901fda7c366 Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents: 25917
diff changeset
606
74545
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
607 class ExceptionPicklingTestCase(unittest.TestCase):
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
608 """Tests for issue #13760: ConfigParser exceptions are not picklable."""
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
609
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
610 def test_error(self):
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
611 import pickle
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
612 e1 = ConfigParser.Error('value')
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
613 pickled = pickle.dumps(e1)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
614 e2 = pickle.loads(pickled)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
615 self.assertEqual(e1.message, e2.message)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
616 self.assertEqual(repr(e1), repr(e2))
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
617
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
618 def test_nosectionerror(self):
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
619 import pickle
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
620 e1 = ConfigParser.NoSectionError('section')
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
621 pickled = pickle.dumps(e1)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
622 e2 = pickle.loads(pickled)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
623 self.assertEqual(e1.message, e2.message)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
624 self.assertEqual(e1.args, e2.args)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
625 self.assertEqual(e1.section, e2.section)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
626 self.assertEqual(repr(e1), repr(e2))
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
627
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
628 def test_nooptionerror(self):
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
629 import pickle
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
630 e1 = ConfigParser.NoOptionError('option', 'section')
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
631 pickled = pickle.dumps(e1)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
632 e2 = pickle.loads(pickled)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
633 self.assertEqual(e1.message, e2.message)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
634 self.assertEqual(e1.args, e2.args)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
635 self.assertEqual(e1.section, e2.section)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
636 self.assertEqual(e1.option, e2.option)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
637 self.assertEqual(repr(e1), repr(e2))
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
638
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
639 def test_duplicatesectionerror(self):
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
640 import pickle
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
641 e1 = ConfigParser.DuplicateSectionError('section')
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
642 pickled = pickle.dumps(e1)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
643 e2 = pickle.loads(pickled)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
644 self.assertEqual(e1.message, e2.message)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
645 self.assertEqual(e1.args, e2.args)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
646 self.assertEqual(e1.section, e2.section)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
647 self.assertEqual(repr(e1), repr(e2))
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
648
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
649 def test_interpolationerror(self):
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
650 import pickle
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
651 e1 = ConfigParser.InterpolationError('option', 'section', 'msg')
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
652 pickled = pickle.dumps(e1)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
653 e2 = pickle.loads(pickled)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
654 self.assertEqual(e1.message, e2.message)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
655 self.assertEqual(e1.args, e2.args)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
656 self.assertEqual(e1.section, e2.section)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
657 self.assertEqual(e1.option, e2.option)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
658 self.assertEqual(repr(e1), repr(e2))
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
659
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
660 def test_interpolationmissingoptionerror(self):
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
661 import pickle
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
662 e1 = ConfigParser.InterpolationMissingOptionError('option', 'section',
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
663 'rawval', 'reference')
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
664 pickled = pickle.dumps(e1)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
665 e2 = pickle.loads(pickled)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
666 self.assertEqual(e1.message, e2.message)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
667 self.assertEqual(e1.args, e2.args)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
668 self.assertEqual(e1.section, e2.section)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
669 self.assertEqual(e1.option, e2.option)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
670 self.assertEqual(e1.reference, e2.reference)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
671 self.assertEqual(repr(e1), repr(e2))
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
672
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
673 def test_interpolationsyntaxerror(self):
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
674 import pickle
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
675 e1 = ConfigParser.InterpolationSyntaxError('option', 'section', 'msg')
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
676 pickled = pickle.dumps(e1)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
677 e2 = pickle.loads(pickled)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
678 self.assertEqual(e1.message, e2.message)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
679 self.assertEqual(e1.args, e2.args)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
680 self.assertEqual(e1.section, e2.section)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
681 self.assertEqual(e1.option, e2.option)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
682 self.assertEqual(repr(e1), repr(e2))
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
683
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
684 def test_interpolationdeptherror(self):
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
685 import pickle
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
686 e1 = ConfigParser.InterpolationDepthError('option', 'section',
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
687 'rawval')
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
688 pickled = pickle.dumps(e1)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
689 e2 = pickle.loads(pickled)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
690 self.assertEqual(e1.message, e2.message)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
691 self.assertEqual(e1.args, e2.args)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
692 self.assertEqual(e1.section, e2.section)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
693 self.assertEqual(e1.option, e2.option)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
694 self.assertEqual(repr(e1), repr(e2))
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
695
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
696 def test_parsingerror(self):
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
697 import pickle
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
698 e1 = ConfigParser.ParsingError('source')
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
699 e1.append(1, 'line1')
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
700 e1.append(2, 'line2')
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
701 e1.append(3, 'line3')
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
702 pickled = pickle.dumps(e1)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
703 e2 = pickle.loads(pickled)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
704 self.assertEqual(e1.message, e2.message)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
705 self.assertEqual(e1.args, e2.args)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
706 self.assertEqual(e1.filename, e2.filename)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
707 self.assertEqual(e1.errors, e2.errors)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
708 self.assertEqual(repr(e1), repr(e2))
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
709
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
710 def test_missingsectionheadererror(self):
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
711 import pickle
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
712 e1 = ConfigParser.MissingSectionHeaderError('filename', 123, 'line')
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
713 pickled = pickle.dumps(e1)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
714 e2 = pickle.loads(pickled)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
715 self.assertEqual(e1.message, e2.message)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
716 self.assertEqual(e1.args, e2.args)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
717 self.assertEqual(e1.line, e2.line)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
718 self.assertEqual(e1.filename, e2.filename)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
719 self.assertEqual(e1.lineno, e2.lineno)
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
720 self.assertEqual(repr(e1), repr(e2))
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
721
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
722
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
723 def test_main():
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 26928
diff changeset
724 test_support.run_unittest(
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 26928
diff changeset
725 ConfigParserTestCase,
63221
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
726 MultilineValuesTestCase,
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 26928
diff changeset
727 RawConfigParserTestCase,
40418
3ee9d1a36d46 Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents: 33665
diff changeset
728 SafeConfigParserTestCase,
63221
eecc018aac3e Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents: 58948
diff changeset
729 SafeConfigParserTestCaseNoValue,
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
730 SortedTestCase,
64385
16cefdfed682 fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents: 63910
diff changeset
731 Issue7005TestCase,
71789
7d5a37ce42d5 Issue 12717: Fix-up an earlier backport in ConfigParser.
Raymond Hettinger <python@rcn.com>
parents: 68256
diff changeset
732 TestChainMap,
74545
5ecf650ede7c Fixes #13760: picklability of ConfigParser exceptions
Łukasz Langa <lukasz@langa.pl>
parents: 73406
diff changeset
733 ExceptionPicklingTestCase,
58948
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
734 )
6e74d3e51d33 - apply patch from issue 7005
Fred Drake <fdrake@acm.org>
parents: 58774
diff changeset
735
25910
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
736
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
737 if __name__ == "__main__":
accd2f54e184 Convert to PyUnit.
Fred Drake <fdrake@acm.org>
parents: 25909
diff changeset
738 test_main()