Debian Codegraph @

Lib/test/test_configparser.py

raw
1 import collections
2 import configparser
3 import io
4 import os
5 import textwrap
6 import unittest
7
8 from test import support
9 from test.support import os_helper
10
11
12 class SortedDict(collections.UserDict):
13
14 def items(self):
15 return sorted(self.data.items())
16
17 def keys(self):
18 return sorted(self.data.keys())
19
20 def values(self):
21 return [i[1] for i in self.items()]
22
23 def iteritems(self):
24 return iter(self.items())
25
26 def iterkeys(self):
27 return iter(self.keys())
28
29 def itervalues(self):
30 return iter(self.values())
31
32 __iter__ = iterkeys
33
34
35 class CfgParserTestCaseClass:
36 allow_no_value = False
37 delimiters = ('=', ':')
38 comment_prefixes = (';', '#')
39 inline_comment_prefixes = (';', '#')
40 empty_lines_in_values = True
41 dict_type = configparser._default_dict
42 strict = False
43 default_section = configparser.DEFAULTSECT
44 interpolation = configparser._UNSET
45
46 def newconfig(self, defaults=None):
47 arguments = dict(
48 defaults=defaults,
49 allow_no_value=self.allow_no_value,
50 delimiters=self.delimiters,
51 comment_prefixes=self.comment_prefixes,
52 inline_comment_prefixes=self.inline_comment_prefixes,
53 empty_lines_in_values=self.empty_lines_in_values,
54 dict_type=self.dict_type,
55 strict=self.strict,
56 default_section=self.default_section,
57 interpolation=self.interpolation,
58 )
59 instance = self.config_class(**arguments)
60 return instance
61
62 def fromstring(self, string, defaults=None):
63 cf = self.newconfig(defaults)
64 cf.read_string(string)
65 return cf
66
67
68 class BasicTestCase(CfgParserTestCaseClass):
69
70 def basic_test(self, cf):
71 E = ['Commented Bar',
72 'Foo Bar',
73 'Internationalized Stuff',
74 'Long Line',
75 'Section\\with$weird%characters[\t',
76 'Spaces',
77 'Spacey Bar',
78 'Spacey Bar From The Beginning',
79 'Types',
80 'This One Has A ] In It',
81 ]
82
83 if self.allow_no_value:
84 E.append('NoValue')
85 E.sort()
86 F = [('baz', 'qwe'), ('foo', 'bar3')]
87
88 # API access
89 L = cf.sections()
90 L.sort()
91 eq = self.assertEqual
92 eq(L, E)
93 L = cf.items('Spacey Bar From The Beginning')
94 L.sort()
95 eq(L, F)
96
97 # mapping access
98 L = [section for section in cf]
99 L.sort()
100 E.append(self.default_section)
101 E.sort()
102 eq(L, E)
103 L = cf['Spacey Bar From The Beginning'].items()
104 L = sorted(list(L))
105 eq(L, F)
106 L = cf.items()
107 L = sorted(list(L))
108 self.assertEqual(len(L), len(E))
109 for name, section in L:
110 eq(name, section.name)
111 eq(cf.defaults(), cf[self.default_section])
112
113 # The use of spaces in the section names serves as a
114 # regression test for SourceForge bug #583248:
115 # https://bugs.python.org/issue583248
116
117 # API access
118 eq(cf.get('Foo Bar', 'foo'), 'bar1')
119 eq(cf.get('Spacey Bar', 'foo'), 'bar2')
120 eq(cf.get('Spacey Bar From The Beginning', 'foo'), 'bar3')
121 eq(cf.get('Spacey Bar From The Beginning', 'baz'), 'qwe')
122 eq(cf.get('Commented Bar', 'foo'), 'bar4')
123 eq(cf.get('Commented Bar', 'baz'), 'qwe')
124 eq(cf.get('Spaces', 'key with spaces'), 'value')
125 eq(cf.get('Spaces', 'another with spaces'), 'splat!')
126 eq(cf.getint('Types', 'int'), 42)
127 eq(cf.get('Types', 'int'), "42")
128 self.assertAlmostEqual(cf.getfloat('Types', 'float'), 0.44)
129 eq(cf.get('Types', 'float'), "0.44")
130 eq(cf.getboolean('Types', 'boolean'), False)
131 eq(cf.get('Types', '123'), 'strange but acceptable')
132 eq(cf.get('This One Has A ] In It', 'forks'), 'spoons')
133 if self.allow_no_value:
134 eq(cf.get('NoValue', 'option-without-value'), None)
135
136 # test vars= and fallback=
137 eq(cf.get('Foo Bar', 'foo', fallback='baz'), 'bar1')
138 eq(cf.get('Foo Bar', 'foo', vars={'foo': 'baz'}), 'baz')
139 with self.assertRaises(configparser.NoSectionError):
140 cf.get('No Such Foo Bar', 'foo')
141 with self.assertRaises(configparser.NoOptionError):
142 cf.get('Foo Bar', 'no-such-foo')
143 eq(cf.get('No Such Foo Bar', 'foo', fallback='baz'), 'baz')
144 eq(cf.get('Foo Bar', 'no-such-foo', fallback='baz'), 'baz')
145 eq(cf.get('Spacey Bar', 'foo', fallback=None), 'bar2')
146 eq(cf.get('No Such Spacey Bar', 'foo', fallback=None), None)
147 eq(cf.getint('Types', 'int', fallback=18), 42)
148 eq(cf.getint('Types', 'no-such-int', fallback=18), 18)
149 eq(cf.getint('Types', 'no-such-int', fallback="18"), "18") # sic!
150 with self.assertRaises(configparser.NoOptionError):
151 cf.getint('Types', 'no-such-int')
152 self.assertAlmostEqual(cf.getfloat('Types', 'float',
153 fallback=0.0), 0.44)
154 self.assertAlmostEqual(cf.getfloat('Types', 'no-such-float',
155 fallback=0.0), 0.0)
156 eq(cf.getfloat('Types', 'no-such-float', fallback="0.0"), "0.0") # sic!
157 with self.assertRaises(configparser.NoOptionError):
158 cf.getfloat('Types', 'no-such-float')
159 eq(cf.getboolean('Types', 'boolean', fallback=True), False)
160 eq(cf.getboolean('Types', 'no-such-boolean', fallback="yes"),
161 "yes") # sic!
162 eq(cf.getboolean('Types', 'no-such-boolean', fallback=True), True)
163 with self.assertRaises(configparser.NoOptionError):
164 cf.getboolean('Types', 'no-such-boolean')
165 eq(cf.getboolean('No Such Types', 'boolean', fallback=True), True)
166 if self.allow_no_value:
167 eq(cf.get('NoValue', 'option-without-value', fallback=False), None)
168 eq(cf.get('NoValue', 'no-such-option-without-value',
169 fallback=False), False)
170
171 # mapping access
172 eq(cf['Foo Bar']['foo'], 'bar1')
173 eq(cf['Spacey Bar']['foo'], 'bar2')
174 section = cf['Spacey Bar From The Beginning']
175 eq(section.name, 'Spacey Bar From The Beginning')
176 self.assertIs(section.parser, cf)
177 with self.assertRaises(AttributeError):
178 section.name = 'Name is read-only'
179 with self.assertRaises(AttributeError):
180 section.parser = 'Parser is read-only'
181 eq(section['foo'], 'bar3')
182 eq(section['baz'], 'qwe')
183 eq(cf['Commented Bar']['foo'], 'bar4')
184 eq(cf['Commented Bar']['baz'], 'qwe')
185 eq(cf['Spaces']['key with spaces'], 'value')
186 eq(cf['Spaces']['another with spaces'], 'splat!')
187 eq(cf['Long Line']['foo'],
188 'this line is much, much longer than my editor\nlikes it.')
189 if self.allow_no_value:
190 eq(cf['NoValue']['option-without-value'], None)
191 # test vars= and fallback=
192 eq(cf['Foo Bar'].get('foo', 'baz'), 'bar1')
193 eq(cf['Foo Bar'].get('foo', fallback='baz'), 'bar1')
194 eq(cf['Foo Bar'].get('foo', vars={'foo': 'baz'}), 'baz')
195 with self.assertRaises(KeyError):
196 cf['No Such Foo Bar']['foo']
197 with self.assertRaises(KeyError):
198 cf['Foo Bar']['no-such-foo']
199 with self.assertRaises(KeyError):
200 cf['No Such Foo Bar'].get('foo', fallback='baz')
201 eq(cf['Foo Bar'].get('no-such-foo', 'baz'), 'baz')
202 eq(cf['Foo Bar'].get('no-such-foo', fallback='baz'), 'baz')
203 eq(cf['Foo Bar'].get('no-such-foo'), None)
204 eq(cf['Spacey Bar'].get('foo', None), 'bar2')
205 eq(cf['Spacey Bar'].get('foo', fallback=None), 'bar2')
206 with self.assertRaises(KeyError):
207 cf['No Such Spacey Bar'].get('foo', None)
208 eq(cf['Types'].getint('int', 18), 42)
209 eq(cf['Types'].getint('int', fallback=18), 42)
210 eq(cf['Types'].getint('no-such-int', 18), 18)
211 eq(cf['Types'].getint('no-such-int', fallback=18), 18)
212 eq(cf['Types'].getint('no-such-int', "18"), "18") # sic!
213 eq(cf['Types'].getint('no-such-int', fallback="18"), "18") # sic!
214 eq(cf['Types'].getint('no-such-int'), None)
215 self.assertAlmostEqual(cf['Types'].getfloat('float', 0.0), 0.44)
216 self.assertAlmostEqual(cf['Types'].getfloat('float',
217 fallback=0.0), 0.44)
218 self.assertAlmostEqual(cf['Types'].getfloat('no-such-float', 0.0), 0.0)
219 self.assertAlmostEqual(cf['Types'].getfloat('no-such-float',
220 fallback=0.0), 0.0)
221 eq(cf['Types'].getfloat('no-such-float', "0.0"), "0.0") # sic!
222 eq(cf['Types'].getfloat('no-such-float', fallback="0.0"), "0.0") # sic!
223 eq(cf['Types'].getfloat('no-such-float'), None)
224 eq(cf['Types'].getboolean('boolean', True), False)
225 eq(cf['Types'].getboolean('boolean', fallback=True), False)
226 eq(cf['Types'].getboolean('no-such-boolean', "yes"), "yes") # sic!
227 eq(cf['Types'].getboolean('no-such-boolean', fallback="yes"),
228 "yes") # sic!
229 eq(cf['Types'].getboolean('no-such-boolean', True), True)
230 eq(cf['Types'].getboolean('no-such-boolean', fallback=True), True)
231 eq(cf['Types'].getboolean('no-such-boolean'), None)
232 if self.allow_no_value:
233 eq(cf['NoValue'].get('option-without-value', False), None)
234 eq(cf['NoValue'].get('option-without-value', fallback=False), None)
235 eq(cf['NoValue'].get('no-such-option-without-value', False), False)
236 eq(cf['NoValue'].get('no-such-option-without-value',
237 fallback=False), False)
238
239 # Make sure the right things happen for remove_section() and
240 # remove_option(); added to include check for SourceForge bug #123324.
241
242 cf[self.default_section]['this_value'] = '1'
243 cf[self.default_section]['that_value'] = '2'
244
245 # API access
246 self.assertTrue(cf.remove_section('Spaces'))
247 self.assertFalse(cf.has_option('Spaces', 'key with spaces'))
248 self.assertFalse(cf.remove_section('Spaces'))
249 self.assertFalse(cf.remove_section(self.default_section))
250 self.assertTrue(cf.remove_option('Foo Bar', 'foo'),
251 "remove_option() failed to report existence of option")
252 self.assertFalse(cf.has_option('Foo Bar', 'foo'),
253 "remove_option() failed to remove option")
254 self.assertFalse(cf.remove_option('Foo Bar', 'foo'),
255 "remove_option() failed to report non-existence of option"
256 " that was removed")
257 self.assertTrue(cf.has_option('Foo Bar', 'this_value'))
258 self.assertFalse(cf.remove_option('Foo Bar', 'this_value'))
259 self.assertTrue(cf.remove_option(self.default_section, 'this_value'))
260 self.assertFalse(cf.has_option('Foo Bar', 'this_value'))
261 self.assertFalse(cf.remove_option(self.default_section, 'this_value'))
262
263 with self.assertRaises(configparser.NoSectionError) as cm:
264 cf.remove_option('No Such Section', 'foo')
265 self.assertEqual(cm.exception.args, ('No Such Section',))
266
267 eq(cf.get('Long Line', 'foo'),
268 'this line is much, much longer than my editor\nlikes it.')
269
270 # mapping access
271 del cf['Types']
272 self.assertFalse('Types' in cf)
273 with self.assertRaises(KeyError):
274 del cf['Types']
275 with self.assertRaises(ValueError):
276 del cf[self.default_section]
277 del cf['Spacey Bar']['foo']
278 self.assertFalse('foo' in cf['Spacey Bar'])
279 with self.assertRaises(KeyError):
280 del cf['Spacey Bar']['foo']
281 self.assertTrue('that_value' in cf['Spacey Bar'])
282 with self.assertRaises(KeyError):
283 del cf['Spacey Bar']['that_value']
284 del cf[self.default_section]['that_value']
285 self.assertFalse('that_value' in cf['Spacey Bar'])
286 with self.assertRaises(KeyError):
287 del cf[self.default_section]['that_value']
288 with self.assertRaises(KeyError):
289 del cf['No Such Section']['foo']
290
291 # Don't add new asserts below in this method as most of the options
292 # and sections are now removed.
293
294 def test_basic(self):
295 config_string = """\
296 [Foo Bar]
297 foo{0[0]}bar1
298 [Spacey Bar]
299 foo {0[0]} bar2
300 [Spacey Bar From The Beginning]
301 foo {0[0]} bar3
302 baz {0[0]} qwe
303 [Commented Bar]
304 foo{0[1]} bar4 {1[1]} comment
305 baz{0[0]}qwe {1[0]}another one
306 [Long Line]
307 foo{0[1]} this line is much, much longer than my editor
308 likes it.
309 [Section\\with$weird%characters[\t]
310 [Internationalized Stuff]
311 foo[bg]{0[1]} Bulgarian
312 foo{0[0]}Default
313 foo[en]{0[0]}English
314 foo[de]{0[0]}Deutsch
315 [Spaces]
316 key with spaces {0[1]} value
317 another with spaces {0[0]} splat!
318 [Types]
319 int {0[1]} 42
320 float {0[0]} 0.44
321 boolean {0[0]} NO
322 123 {0[1]} strange but acceptable
323 [This One Has A ] In It]
324 forks {0[0]} spoons
325 """.format(self.delimiters, self.comment_prefixes)
326 if self.allow_no_value:
327 config_string += (
328 "[NoValue]\n"
329 "option-without-value\n"
330 )
331 cf = self.fromstring(config_string)
332 self.basic_test(cf)
333 if self.strict:
334 with self.assertRaises(configparser.DuplicateOptionError):
335 cf.read_string(textwrap.dedent("""\
336 [Duplicate Options Here]
337 option {0[0]} with a value
338 option {0[1]} with another value
339 """.format(self.delimiters)))
340 with self.assertRaises(configparser.DuplicateSectionError):
341 cf.read_string(textwrap.dedent("""\
342 [And Now For Something]
343 completely different {0[0]} True
344 [And Now For Something]
345 the larch {0[1]} 1
346 """.format(self.delimiters)))
347 else:
348 cf.read_string(textwrap.dedent("""\
349 [Duplicate Options Here]
350 option {0[0]} with a value
351 option {0[1]} with another value
352 """.format(self.delimiters)))
353
354 cf.read_string(textwrap.dedent("""\
355 [And Now For Something]
356 completely different {0[0]} True
357 [And Now For Something]
358 the larch {0[1]} 1
359 """.format(self.delimiters)))
360
361 def test_basic_from_dict(self):
362 config = {
363 "Foo Bar": {
364 "foo": "bar1",
365 },
366 "Spacey Bar": {
367 "foo": "bar2",
368 },
369 "Spacey Bar From The Beginning": {
370 "foo": "bar3",
371 "baz": "qwe",
372 },
373 "Commented Bar": {
374 "foo": "bar4",
375 "baz": "qwe",
376 },
377 "Long Line": {
378 "foo": "this line is much, much longer than my editor\nlikes "
379 "it.",
380 },
381 "Section\\with$weird%characters[\t": {
382 },
383 "Internationalized Stuff": {
384 "foo[bg]": "Bulgarian",
385 "foo": "Default",
386 "foo[en]": "English",
387 "foo[de]": "Deutsch",
388 },
389 "Spaces": {
390 "key with spaces": "value",
391 "another with spaces": "splat!",
392 },
393 "Types": {
394 "int": 42,
395 "float": 0.44,
396 "boolean": False,
397 123: "strange but acceptable",
398 },
399 "This One Has A ] In It": {
400 "forks": "spoons"
401 },
402 }
403 if self.allow_no_value:
404 config.update({
405 "NoValue": {
406 "option-without-value": None,
407 }
408 })
409 cf = self.newconfig()
410 cf.read_dict(config)
411 self.basic_test(cf)
412 if self.strict:
413 with self.assertRaises(configparser.DuplicateSectionError):
414 cf.read_dict({
415 '1': {'key': 'value'},
416 1: {'key2': 'value2'},
417 })
418 with self.assertRaises(configparser.DuplicateOptionError):
419 cf.read_dict({
420 "Duplicate Options Here": {
421 'option': 'with a value',
422 'OPTION': 'with another value',
423 },
424 })
425 else:
426 cf.read_dict({
427 'section': {'key': 'value'},
428 'SECTION': {'key2': 'value2'},
429 })
430 cf.read_dict({
431 "Duplicate Options Here": {
432 'option': 'with a value',
433 'OPTION': 'with another value',
434 },
435 })
436
437 def test_case_sensitivity(self):
438 cf = self.newconfig()
439 cf.add_section("A")
440 cf.add_section("a")
441 cf.add_section("B")
442 L = cf.sections()
443 L.sort()
444 eq = self.assertEqual
445 eq(L, ["A", "B", "a"])
446 cf.set("a", "B", "value")
447 eq(cf.options("a"), ["b"])
448 eq(cf.get("a", "b"), "value",
449 "could not locate option, expecting case-insensitive option names")
450 with self.assertRaises(configparser.NoSectionError):
451 # section names are case-sensitive
452 cf.set("b", "A", "value")
453 self.assertTrue(cf.has_option("a", "b"))
454 self.assertFalse(cf.has_option("b", "b"))
455 cf.set("A", "A-B", "A-B value")
456 for opt in ("a-b", "A-b", "a-B", "A-B"):
457 self.assertTrue(
458 cf.has_option("A", opt),
459 "has_option() returned false for option which should exist")
460 eq(cf.options("A"), ["a-b"])
461 eq(cf.options("a"), ["b"])
462 cf.remove_option("a", "B")
463 eq(cf.options("a"), [])
464
465 # SF bug #432369:
466 cf = self.fromstring(
467 "[MySection]\nOption{} first line \n\tsecond line \n".format(
468 self.delimiters[0]))
469 eq(cf.options("MySection"), ["option"])
470 eq(cf.get("MySection", "Option"), "first line\nsecond line")
471
472 # SF bug #561822:
473 cf = self.fromstring("[section]\n"
474 "nekey{}nevalue\n".format(self.delimiters[0]),
475 defaults={"key":"value"})
476 self.assertTrue(cf.has_option("section", "Key"))
477
478
479 def test_case_sensitivity_mapping_access(self):
480 cf = self.newconfig()
481 cf["A"] = {}
482 cf["a"] = {"B": "value"}
483 cf["B"] = {}
484 L = [section for section in cf]
485 L.sort()
486 eq = self.assertEqual
487 elem_eq = self.assertCountEqual
488 eq(L, sorted(["A", "B", self.default_section, "a"]))
489 eq(cf["a"].keys(), {"b"})
490 eq(cf["a"]["b"], "value",
491 "could not locate option, expecting case-insensitive option names")
492 with self.assertRaises(KeyError):
493 # section names are case-sensitive
494 cf["b"]["A"] = "value"
495 self.assertTrue("b" in cf["a"])
496 cf["A"]["A-B"] = "A-B value"
497 for opt in ("a-b", "A-b", "a-B", "A-B"):
498 self.assertTrue(
499 opt in cf["A"],
500 "has_option() returned false for option which should exist")
501 eq(cf["A"].keys(), {"a-b"})
502 eq(cf["a"].keys(), {"b"})
503 del cf["a"]["B"]
504 elem_eq(cf["a"].keys(), {})
505
506 # SF bug #432369:
507 cf = self.fromstring(
508 "[MySection]\nOption{} first line \n\tsecond line \n".format(
509 self.delimiters[0]))
510 eq(cf["MySection"].keys(), {"option"})
511 eq(cf["MySection"]["Option"], "first line\nsecond line")
512
513 # SF bug #561822:
514 cf = self.fromstring("[section]\n"
515 "nekey{}nevalue\n".format(self.delimiters[0]),
516 defaults={"key":"value"})
517 self.assertTrue("Key" in cf["section"])
518
519 def test_default_case_sensitivity(self):
520 cf = self.newconfig({"foo": "Bar"})
521 self.assertEqual(
522 cf.get(self.default_section, "Foo"), "Bar",
523 "could not locate option, expecting case-insensitive option names")
524 cf = self.newconfig({"Foo": "Bar"})
525 self.assertEqual(
526 cf.get(self.default_section, "Foo"), "Bar",
527 "could not locate option, expecting case-insensitive defaults")
528
529 def test_parse_errors(self):
530 cf = self.newconfig()
531 self.parse_error(cf, configparser.ParsingError,
532 "[Foo]\n"
533 "{}val-without-opt-name\n".format(self.delimiters[0]))
534 self.parse_error(cf, configparser.ParsingError,
535 "[Foo]\n"
536 "{}val-without-opt-name\n".format(self.delimiters[1]))
537 e = self.parse_error(cf, configparser.MissingSectionHeaderError,
538 "No Section!\n")
539 self.assertEqual(e.args, ('<???>', 1, "No Section!\n"))
540 if not self.allow_no_value:
541 e = self.parse_error(cf, configparser.ParsingError,
542 "[Foo]\n wrong-indent\n")
543 self.assertEqual(e.args, ('<???>',))
544 # read_file on a real file
545 tricky = support.findfile("cfgparser.3", subdir="configdata")
546 if self.delimiters[0] == '=':
547 error = configparser.ParsingError
548 expected = (tricky,)
549 else:
550 error = configparser.MissingSectionHeaderError
551 expected = (tricky, 1,
552 ' # INI with as many tricky parts as possible\n')
553 with open(tricky, encoding='utf-8') as f:
554 e = self.parse_error(cf, error, f)
555 self.assertEqual(e.args, expected)
556
557 def parse_error(self, cf, exc, src):
558 if hasattr(src, 'readline'):
559 sio = src
560 else:
561 sio = io.StringIO(src)
562 with self.assertRaises(exc) as cm:
563 cf.read_file(sio)
564 return cm.exception
565
566 def test_query_errors(self):
567 cf = self.newconfig()
568 self.assertEqual(cf.sections(), [],
569 "new ConfigParser should have no defined sections")
570 self.assertFalse(cf.has_section("Foo"),
571 "new ConfigParser should have no acknowledged "
572 "sections")
573 with self.assertRaises(configparser.NoSectionError):
574 cf.options("Foo")
575 with self.assertRaises(configparser.NoSectionError):
576 cf.set("foo", "bar", "value")
577 e = self.get_error(cf, configparser.NoSectionError, "foo", "bar")
578 self.assertEqual(e.args, ("foo",))
579 cf.add_section("foo")
580 e = self.get_error(cf, configparser.NoOptionError, "foo", "bar")
581 self.assertEqual(e.args, ("bar", "foo"))
582
583 def get_error(self, cf, exc, section, option):
584 try:
585 cf.get(section, option)
586 except exc as e:
587 return e
588 else:
589 self.fail("expected exception type %s.%s"
590 % (exc.__module__, exc.__qualname__))
591
592 def test_boolean(self):
593 cf = self.fromstring(
594 "[BOOLTEST]\n"
595 "T1{equals}1\n"
596 "T2{equals}TRUE\n"
597 "T3{equals}True\n"
598 "T4{equals}oN\n"
599 "T5{equals}yes\n"
600 "F1{equals}0\n"
601 "F2{equals}FALSE\n"
602 "F3{equals}False\n"
603 "F4{equals}oFF\n"
604 "F5{equals}nO\n"
605 "E1{equals}2\n"
606 "E2{equals}foo\n"
607 "E3{equals}-1\n"
608 "E4{equals}0.1\n"
609 "E5{equals}FALSE AND MORE".format(equals=self.delimiters[0])
610 )
611 for x in range(1, 5):
612 self.assertTrue(cf.getboolean('BOOLTEST', 't%d' % x))
613 self.assertFalse(cf.getboolean('BOOLTEST', 'f%d' % x))
614 self.assertRaises(ValueError,
615 cf.getboolean, 'BOOLTEST', 'e%d' % x)
616
617 def test_weird_errors(self):
618 cf = self.newconfig()
619 cf.add_section("Foo")
620 with self.assertRaises(configparser.DuplicateSectionError) as cm:
621 cf.add_section("Foo")
622 e = cm.exception
623 self.assertEqual(str(e), "Section 'Foo' already exists")
624 self.assertEqual(e.args, ("Foo", None, None))
625
626 if self.strict:
627 with self.assertRaises(configparser.DuplicateSectionError) as cm:
628 cf.read_string(textwrap.dedent("""\
629 [Foo]
630 will this be added{equals}True
631 [Bar]
632 what about this{equals}True
633 [Foo]
634 oops{equals}this won't
635 """.format(equals=self.delimiters[0])), source='<foo-bar>')
636 e = cm.exception
637 self.assertEqual(str(e), "While reading from '<foo-bar>' "
638 "[line 5]: section 'Foo' already exists")
639 self.assertEqual(e.args, ("Foo", '<foo-bar>', 5))
640
641 with self.assertRaises(configparser.DuplicateOptionError) as cm:
642 cf.read_dict({'Bar': {'opt': 'val', 'OPT': 'is really `opt`'}})
643 e = cm.exception
644 self.assertEqual(str(e), "While reading from '<dict>': option "
645 "'opt' in section 'Bar' already exists")
646 self.assertEqual(e.args, ("Bar", "opt", "<dict>", None))
647
648 def test_get_after_duplicate_option_error(self):
649 cf = self.newconfig()
650 ini = textwrap.dedent("""\
651 [Foo]
652 x{equals}1
653 y{equals}2
654 y{equals}3
655 """.format(equals=self.delimiters[0]))
656 if self.strict:
657 with self.assertRaises(configparser.DuplicateOptionError):
658 cf.read_string(ini)
659 else:
660 cf.read_string(ini)
661 self.assertEqual(cf.get('Foo', 'x'), '1')
662
663 def test_write(self):
664 config_string = (
665 "[Long Line]\n"
666 "foo{0[0]} this line is much, much longer than my editor\n"
667 " likes it.\n"
668 "[{default_section}]\n"
669 "foo{0[1]} another very\n"
670 " long line\n"
671 "[Long Line - With Comments!]\n"
672 "test {0[1]} we {comment} can\n"
673 " also {comment} place\n"
674 " comments {comment} in\n"
675 " multiline {comment} values"
676 "\n".format(self.delimiters, comment=self.comment_prefixes[0],
677 default_section=self.default_section)
678 )
679 if self.allow_no_value:
680 config_string += (
681 "[Valueless]\n"
682 "option-without-value\n"
683 )
684
685 cf = self.fromstring(config_string)
686 for space_around_delimiters in (True, False):
687 output = io.StringIO()
688 cf.write(output, space_around_delimiters=space_around_delimiters)
689 delimiter = self.delimiters[0]
690 if space_around_delimiters:
691 delimiter = " {} ".format(delimiter)
692 expect_string = (
693 "[{default_section}]\n"
694 "foo{equals}another very\n"
695 "\tlong line\n"
696 "\n"
697 "[Long Line]\n"
698 "foo{equals}this line is much, much longer than my editor\n"
699 "\tlikes it.\n"
700 "\n"
701 "[Long Line - With Comments!]\n"
702 "test{equals}we\n"
703 "\talso\n"
704 "\tcomments\n"
705 "\tmultiline\n"
706 "\n".format(equals=delimiter,
707 default_section=self.default_section)
708 )
709 if self.allow_no_value:
710 expect_string += (
711 "[Valueless]\n"
712 "option-without-value\n"
713 "\n"
714 )
715 self.assertEqual(output.getvalue(), expect_string)
716
717 def test_set_string_types(self):
718 cf = self.fromstring("[sect]\n"
719 "option1{eq}foo\n".format(eq=self.delimiters[0]))
720 # Check that we don't get an exception when setting values in
721 # an existing section using strings:
722 class mystr(str):
723 pass
724 cf.set("sect", "option1", "splat")
725 cf.set("sect", "option1", mystr("splat"))
726 cf.set("sect", "option2", "splat")
727 cf.set("sect", "option2", mystr("splat"))
728 cf.set("sect", "option1", "splat")
729 cf.set("sect", "option2", "splat")
730
731 def test_read_returns_file_list(self):
732 if self.delimiters[0] != '=':
733 self.skipTest('incompatible format')
734 file1 = support.findfile("cfgparser.1", subdir="configdata")
735 # check when we pass a mix of readable and non-readable files:
736 cf = self.newconfig()
737 parsed_files = cf.read([file1, "nonexistent-file"], encoding="utf-8")
738 self.assertEqual(parsed_files, [file1])
739 self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
740 # check when we pass only a filename:
741 cf = self.newconfig()
742 parsed_files = cf.read(file1, encoding="utf-8")
743 self.assertEqual(parsed_files, [file1])
744 self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
745 # check when we pass only a Path object:
746 cf = self.newconfig()
747 parsed_files = cf.read(os_helper.FakePath(file1), encoding="utf-8")
748 self.assertEqual(parsed_files, [file1])
749 self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
750 # check when we passed both a filename and a Path object:
751 cf = self.newconfig()
752 parsed_files = cf.read([os_helper.FakePath(file1), file1], encoding="utf-8")
753 self.assertEqual(parsed_files, [file1, file1])
754 self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
755 # check when we pass only missing files:
756 cf = self.newconfig()
757 parsed_files = cf.read(["nonexistent-file"], encoding="utf-8")
758 self.assertEqual(parsed_files, [])
759 # check when we pass no files:
760 cf = self.newconfig()
761 parsed_files = cf.read([], encoding="utf-8")
762 self.assertEqual(parsed_files, [])
763
764 def test_read_returns_file_list_with_bytestring_path(self):
765 if self.delimiters[0] != '=':
766 self.skipTest('incompatible format')
767 file1_bytestring = support.findfile("cfgparser.1", subdir="configdata").encode()
768 # check when passing an existing bytestring path
769 cf = self.newconfig()
770 parsed_files = cf.read(file1_bytestring, encoding="utf-8")
771 self.assertEqual(parsed_files, [file1_bytestring])
772 # check when passing an non-existing bytestring path
773 cf = self.newconfig()
774 parsed_files = cf.read(b'nonexistent-file', encoding="utf-8")
775 self.assertEqual(parsed_files, [])
776 # check when passing both an existing and non-existing bytestring path
777 cf = self.newconfig()
778 parsed_files = cf.read([file1_bytestring, b'nonexistent-file'], encoding="utf-8")
779 self.assertEqual(parsed_files, [file1_bytestring])
780
781 # shared by subclasses
782 def get_interpolation_config(self):
783 return self.fromstring(
784 "[Foo]\n"
785 "bar{equals}something %(with1)s interpolation (1 step)\n"
786 "bar9{equals}something %(with9)s lots of interpolation (9 steps)\n"
787 "bar10{equals}something %(with10)s lots of interpolation (10 steps)\n"
788 "bar11{equals}something %(with11)s lots of interpolation (11 steps)\n"
789 "with11{equals}%(with10)s\n"
790 "with10{equals}%(with9)s\n"
791 "with9{equals}%(with8)s\n"
792 "with8{equals}%(With7)s\n"
793 "with7{equals}%(WITH6)s\n"
794 "with6{equals}%(with5)s\n"
795 "With5{equals}%(with4)s\n"
796 "WITH4{equals}%(with3)s\n"
797 "with3{equals}%(with2)s\n"
798 "with2{equals}%(with1)s\n"
799 "with1{equals}with\n"
800 "\n"
801 "[Mutual Recursion]\n"
802 "foo{equals}%(bar)s\n"
803 "bar{equals}%(foo)s\n"
804 "\n"
805 "[Interpolation Error]\n"
806 # no definition for 'reference'
807 "name{equals}%(reference)s\n".format(equals=self.delimiters[0]))
808
809 def check_items_config(self, expected):
810 cf = self.fromstring("""
811 [section]
812 name {0[0]} %(value)s
813 key{0[1]} |%(name)s|
814 getdefault{0[1]} |%(default)s|
815 """.format(self.delimiters), defaults={"default": "<default>"})
816 L = list(cf.items("section", vars={'value': 'value'}))
817 L.sort()
818 self.assertEqual(L, expected)
819 with self.assertRaises(configparser.NoSectionError):
820 cf.items("no such section")
821
822 def test_popitem(self):
823 cf = self.fromstring("""
824 [section1]
825 name1 {0[0]} value1
826 [section2]
827 name2 {0[0]} value2
828 [section3]
829 name3 {0[0]} value3
830 """.format(self.delimiters), defaults={"default": "<default>"})
831 self.assertEqual(cf.popitem()[0], 'section1')
832 self.assertEqual(cf.popitem()[0], 'section2')
833 self.assertEqual(cf.popitem()[0], 'section3')
834 with self.assertRaises(KeyError):
835 cf.popitem()
836
837 def test_clear(self):
838 cf = self.newconfig({"foo": "Bar"})
839 self.assertEqual(
840 cf.get(self.default_section, "Foo"), "Bar",
841 "could not locate option, expecting case-insensitive option names")
842 cf['zing'] = {'option1': 'value1', 'option2': 'value2'}
843 self.assertEqual(cf.sections(), ['zing'])
844 self.assertEqual(set(cf['zing'].keys()), {'option1', 'option2', 'foo'})
845 cf.clear()
846 self.assertEqual(set(cf.sections()), set())
847 self.assertEqual(set(cf[self.default_section].keys()), {'foo'})
848
849 def test_setitem(self):
850 cf = self.fromstring("""
851 [section1]
852 name1 {0[0]} value1
853 [section2]
854 name2 {0[0]} value2
855 [section3]
856 name3 {0[0]} value3
857 """.format(self.delimiters), defaults={"nameD": "valueD"})
858 self.assertEqual(set(cf['section1'].keys()), {'name1', 'named'})
859 self.assertEqual(set(cf['section2'].keys()), {'name2', 'named'})
860 self.assertEqual(set(cf['section3'].keys()), {'name3', 'named'})
861 self.assertEqual(cf['section1']['name1'], 'value1')
862 self.assertEqual(cf['section2']['name2'], 'value2')
863 self.assertEqual(cf['section3']['name3'], 'value3')
864 self.assertEqual(cf.sections(), ['section1', 'section2', 'section3'])
865 cf['section2'] = {'name22': 'value22'}
866 self.assertEqual(set(cf['section2'].keys()), {'name22', 'named'})
867 self.assertEqual(cf['section2']['name22'], 'value22')
868 self.assertNotIn('name2', cf['section2'])
869 self.assertEqual(cf.sections(), ['section1', 'section2', 'section3'])
870 cf['section3'] = {}
871 self.assertEqual(set(cf['section3'].keys()), {'named'})
872 self.assertNotIn('name3', cf['section3'])
873 self.assertEqual(cf.sections(), ['section1', 'section2', 'section3'])
874 # For bpo-32108, assigning default_section to itself.
875 cf[self.default_section] = cf[self.default_section]
876 self.assertNotEqual(set(cf[self.default_section].keys()), set())
877 cf[self.default_section] = {}
878 self.assertEqual(set(cf[self.default_section].keys()), set())
879 self.assertEqual(set(cf['section1'].keys()), {'name1'})
880 self.assertEqual(set(cf['section2'].keys()), {'name22'})
881 self.assertEqual(set(cf['section3'].keys()), set())
882 self.assertEqual(cf.sections(), ['section1', 'section2', 'section3'])
883 # For bpo-32108, assigning section to itself.
884 cf['section2'] = cf['section2']
885 self.assertEqual(set(cf['section2'].keys()), {'name22'})
886
887 def test_invalid_multiline_value(self):
888 if self.allow_no_value:
889 self.skipTest('if no_value is allowed, ParsingError is not raised')
890
891 invalid = textwrap.dedent("""\
892 [DEFAULT]
893 test {0} test
894 invalid""".format(self.delimiters[0])
895 )
896 cf = self.newconfig()
897 with self.assertRaises(configparser.ParsingError):
898 cf.read_string(invalid)
899 self.assertEqual(cf.get('DEFAULT', 'test'), 'test')
900 self.assertEqual(cf['DEFAULT']['test'], 'test')
901
902
903 class StrictTestCase(BasicTestCase, unittest.TestCase):
904 config_class = configparser.RawConfigParser
905 strict = True
906
907
908 class ConfigParserTestCase(BasicTestCase, unittest.TestCase):
909 config_class = configparser.ConfigParser
910
911 def test_interpolation(self):
912 cf = self.get_interpolation_config()
913 eq = self.assertEqual
914 eq(cf.get("Foo", "bar"), "something with interpolation (1 step)")
915 eq(cf.get("Foo", "bar9"),
916 "something with lots of interpolation (9 steps)")
917 eq(cf.get("Foo", "bar10"),
918 "something with lots of interpolation (10 steps)")
919 e = self.get_error(cf, configparser.InterpolationDepthError, "Foo", "bar11")
920 if self.interpolation == configparser._UNSET:
921 self.assertEqual(e.args, ("bar11", "Foo",
922 "something %(with11)s lots of interpolation (11 steps)"))
923
924 def test_interpolation_missing_value(self):
925 cf = self.get_interpolation_config()
926 e = self.get_error(cf, configparser.InterpolationMissingOptionError,
927 "Interpolation Error", "name")
928 self.assertEqual(e.reference, "reference")
929 self.assertEqual(e.section, "Interpolation Error")
930 self.assertEqual(e.option, "name")
931 if self.interpolation == configparser._UNSET:
932 self.assertEqual(e.args, ('name', 'Interpolation Error',
933 '%(reference)s', 'reference'))
934
935 def test_items(self):
936 self.check_items_config([('default', '<default>'),
937 ('getdefault', '|<default>|'),
938 ('key', '|value|'),
939 ('name', 'value')])
940
941 def test_safe_interpolation(self):
942 # See https://bugs.python.org/issue511737
943 cf = self.fromstring("[section]\n"
944 "option1{eq}xxx\n"
945 "option2{eq}%(option1)s/xxx\n"
946 "ok{eq}%(option1)s/%%s\n"
947 "not_ok{eq}%(option2)s/%%s".format(
948 eq=self.delimiters[0]))
949 self.assertEqual(cf.get("section", "ok"), "xxx/%s")
950 if self.interpolation == configparser._UNSET:
951 self.assertEqual(cf.get("section", "not_ok"), "xxx/xxx/%s")
952
953 def test_set_malformatted_interpolation(self):
954 cf = self.fromstring("[sect]\n"
955 "option1{eq}foo\n".format(eq=self.delimiters[0]))
956
957 self.assertEqual(cf.get('sect', "option1"), "foo")
958
959 self.assertRaises(ValueError, cf.set, "sect", "option1", "%foo")
960 self.assertRaises(ValueError, cf.set, "sect", "option1", "foo%")
961 self.assertRaises(ValueError, cf.set, "sect", "option1", "f%oo")
962
963 self.assertEqual(cf.get('sect', "option1"), "foo")
964
965 # bug #5741: double percents are *not* malformed
966 cf.set("sect", "option2", "foo%%bar")
967 self.assertEqual(cf.get("sect", "option2"), "foo%bar")
968
969 def test_set_nonstring_types(self):
970 cf = self.fromstring("[sect]\n"
971 "option1{eq}foo\n".format(eq=self.delimiters[0]))
972 # Check that we get a TypeError when setting non-string values
973 # in an existing section:
974 self.assertRaises(TypeError, cf.set, "sect", "option1", 1)
975 self.assertRaises(TypeError, cf.set, "sect", "option1", 1.0)
976 self.assertRaises(TypeError, cf.set, "sect", "option1", object())
977 self.assertRaises(TypeError, cf.set, "sect", "option2", 1)
978 self.assertRaises(TypeError, cf.set, "sect", "option2", 1.0)
979 self.assertRaises(TypeError, cf.set, "sect", "option2", object())
980 self.assertRaises(TypeError, cf.set, "sect", 123, "invalid opt name!")
981 self.assertRaises(TypeError, cf.add_section, 123)
982
983 def test_add_section_default(self):
984 cf = self.newconfig()
985 self.assertRaises(ValueError, cf.add_section, self.default_section)
986
987 def test_defaults_keyword(self):
988 """bpo-23835 fix for ConfigParser"""
989 cf = self.newconfig(defaults={1: 2.5})
990 self.assertEqual(cf[self.default_section]['1'], '2.5')
991 self.assertAlmostEqual(cf[self.default_section].getfloat('1'), 2.5)
992 cf = self.newconfig(defaults={"A": 5.25})
993 self.assertEqual(cf[self.default_section]['a'], '5.25')
994 self.assertAlmostEqual(cf[self.default_section].getfloat('a'), 5.25)
995
996
997 class ConfigParserTestCaseNoInterpolation(BasicTestCase, unittest.TestCase):
998 config_class = configparser.ConfigParser
999 interpolation = None
1000 ini = textwrap.dedent("""
1001 [numbers]
1002 one = 1
1003 two = %(one)s * 2
1004 three = ${common:one} * 3
1005
1006 [hexen]
1007 sixteen = ${numbers:two} * 8
1008 """).strip()
1009
1010 def assertMatchesIni(self, cf):
1011 self.assertEqual(cf['numbers']['one'], '1')
1012 self.assertEqual(cf['numbers']['two'], '%(one)s * 2')
1013 self.assertEqual(cf['numbers']['three'], '${common:one} * 3')
1014 self.assertEqual(cf['hexen']['sixteen'], '${numbers:two} * 8')
1015
1016 def test_no_interpolation(self):
1017 cf = self.fromstring(self.ini)
1018 self.assertMatchesIni(cf)
1019
1020 def test_empty_case(self):
1021 cf = self.newconfig()
1022 self.assertIsNone(cf.read_string(""))
1023
1024 def test_none_as_default_interpolation(self):
1025 class CustomConfigParser(configparser.ConfigParser):
1026 _DEFAULT_INTERPOLATION = None
1027
1028 cf = CustomConfigParser()
1029 cf.read_string(self.ini)
1030 self.assertMatchesIni(cf)
1031
1032 class ConfigParserTestCaseInvalidInterpolationType(unittest.TestCase):
1033 def test_error_on_wrong_type_for_interpolation(self):
1034 for value in [configparser.ExtendedInterpolation, 42, "a string"]:
1035 with self.subTest(value=value):
1036 with self.assertRaises(TypeError):
1037 configparser.ConfigParser(interpolation=value)
1038
1039
1040 class ConfigParserTestCaseNonStandardDelimiters(ConfigParserTestCase):
1041 delimiters = (':=', '$')
1042 comment_prefixes = ('//', '"')
1043 inline_comment_prefixes = ('//', '"')
1044
1045
1046 class ConfigParserTestCaseNonStandardDefaultSection(ConfigParserTestCase):
1047 default_section = 'general'
1048
1049
1050 class MultilineValuesTestCase(BasicTestCase, unittest.TestCase):
1051 config_class = configparser.ConfigParser
1052 wonderful_spam = ("I'm having spam spam spam spam "
1053 "spam spam spam beaked beans spam "
1054 "spam spam and spam!").replace(' ', '\t\n')
1055
1056 def setUp(self):
1057 cf = self.newconfig()
1058 for i in range(100):
1059 s = 'section{}'.format(i)
1060 cf.add_section(s)
1061 for j in range(10):
1062 cf.set(s, 'lovely_spam{}'.format(j), self.wonderful_spam)
1063 with open(os_helper.TESTFN, 'w', encoding="utf-8") as f:
1064 cf.write(f)
1065
1066 def tearDown(self):
1067 os.unlink(os_helper.TESTFN)
1068
1069 def test_dominating_multiline_values(self):
1070 # We're reading from file because this is where the code changed
1071 # during performance updates in Python 3.2
1072 cf_from_file = self.newconfig()
1073 with open(os_helper.TESTFN, encoding="utf-8") as f:
1074 cf_from_file.read_file(f)
1075 self.assertEqual(cf_from_file.get('section8', 'lovely_spam4'),
1076 self.wonderful_spam.replace('\t\n', '\n'))
1077
1078
1079 class RawConfigParserTestCase(BasicTestCase, unittest.TestCase):
1080 config_class = configparser.RawConfigParser
1081
1082 def test_interpolation(self):
1083 cf = self.get_interpolation_config()
1084 eq = self.assertEqual
1085 eq(cf.get("Foo", "bar"),
1086 "something %(with1)s interpolation (1 step)")
1087 eq(cf.get("Foo", "bar9"),
1088 "something %(with9)s lots of interpolation (9 steps)")
1089 eq(cf.get("Foo", "bar10"),
1090 "something %(with10)s lots of interpolation (10 steps)")
1091 eq(cf.get("Foo", "bar11"),
1092 "something %(with11)s lots of interpolation (11 steps)")
1093
1094 def test_items(self):
1095 self.check_items_config([('default', '<default>'),
1096 ('getdefault', '|%(default)s|'),
1097 ('key', '|%(name)s|'),
1098 ('name', '%(value)s')])
1099
1100 def test_set_nonstring_types(self):
1101 cf = self.newconfig()
1102 cf.add_section('non-string')
1103 cf.set('non-string', 'int', 1)
1104 cf.set('non-string', 'list', [0, 1, 1, 2, 3, 5, 8, 13])
1105 cf.set('non-string', 'dict', {'pi': 3.14159})
1106 self.assertEqual(cf.get('non-string', 'int'), 1)
1107 self.assertEqual(cf.get('non-string', 'list'),
1108 [0, 1, 1, 2, 3, 5, 8, 13])
1109 self.assertEqual(cf.get('non-string', 'dict'), {'pi': 3.14159})
1110 cf.add_section(123)
1111 cf.set(123, 'this is sick', True)
1112 self.assertEqual(cf.get(123, 'this is sick'), True)
1113 if cf._dict is configparser._default_dict:
1114 # would not work for SortedDict; only checking for the most common
1115 # default dictionary (dict)
1116 cf.optionxform = lambda x: x
1117 cf.set('non-string', 1, 1)
1118 self.assertEqual(cf.get('non-string', 1), 1)
1119
1120 def test_defaults_keyword(self):
1121 """bpo-23835 legacy behavior for RawConfigParser"""
1122 with self.assertRaises(AttributeError) as ctx:
1123 self.newconfig(defaults={1: 2.4})
1124 err = ctx.exception
1125 self.assertEqual(str(err), "'int' object has no attribute 'lower'")
1126 cf = self.newconfig(defaults={"A": 5.2})
1127 self.assertAlmostEqual(cf[self.default_section]['a'], 5.2)
1128
1129
1130 class RawConfigParserTestCaseNonStandardDelimiters(RawConfigParserTestCase):
1131 delimiters = (':=', '$')
1132 comment_prefixes = ('//', '"')
1133 inline_comment_prefixes = ('//', '"')
1134
1135
1136 class RawConfigParserTestSambaConf(CfgParserTestCaseClass, unittest.TestCase):
1137 config_class = configparser.RawConfigParser
1138 comment_prefixes = ('#', ';', '----')
1139 inline_comment_prefixes = ('//',)
1140 empty_lines_in_values = False
1141
1142 def test_reading(self):
1143 smbconf = support.findfile("cfgparser.2", subdir="configdata")
1144 # check when we pass a mix of readable and non-readable files:
1145 cf = self.newconfig()
1146 parsed_files = cf.read([smbconf, "nonexistent-file"], encoding='utf-8')
1147 self.assertEqual(parsed_files, [smbconf])
1148 sections = ['global', 'homes', 'printers',
1149 'print$', 'pdf-generator', 'tmp', 'Agustin']
1150 self.assertEqual(cf.sections(), sections)
1151 self.assertEqual(cf.get("global", "workgroup"), "MDKGROUP")
1152 self.assertEqual(cf.getint("global", "max log size"), 50)
1153 self.assertEqual(cf.get("global", "hosts allow"), "127.")
1154 self.assertEqual(cf.get("tmp", "echo command"), "cat %s; rm %s")
1155
1156 class ConfigParserTestCaseExtendedInterpolation(BasicTestCase, unittest.TestCase):
1157 config_class = configparser.ConfigParser
1158 interpolation = configparser.ExtendedInterpolation()
1159 default_section = 'common'
1160 strict = True
1161
1162 def fromstring(self, string, defaults=None, optionxform=None):
1163 cf = self.newconfig(defaults)
1164 if optionxform:
1165 cf.optionxform = optionxform
1166 cf.read_string(string)
1167 return cf
1168
1169 def test_extended_interpolation(self):
1170 cf = self.fromstring(textwrap.dedent("""
1171 [common]
1172 favourite Beatle = Paul
1173 favourite color = green
1174
1175 [tom]
1176 favourite band = ${favourite color} day
1177 favourite pope = John ${favourite Beatle} II
1178 sequel = ${favourite pope}I
1179
1180 [ambv]
1181 favourite Beatle = George
1182 son of Edward VII = ${favourite Beatle} V
1183 son of George V = ${son of Edward VII}I
1184
1185 [stanley]
1186 favourite Beatle = ${ambv:favourite Beatle}
1187 favourite pope = ${tom:favourite pope}
1188 favourite color = black
1189 favourite state of mind = paranoid
1190 favourite movie = soylent ${common:favourite color}
1191 favourite song = ${favourite color} sabbath - ${favourite state of mind}
1192 """).strip())
1193
1194 eq = self.assertEqual
1195 eq(cf['common']['favourite Beatle'], 'Paul')
1196 eq(cf['common']['favourite color'], 'green')
1197 eq(cf['tom']['favourite Beatle'], 'Paul')
1198 eq(cf['tom']['favourite color'], 'green')
1199 eq(cf['tom']['favourite band'], 'green day')
1200 eq(cf['tom']['favourite pope'], 'John Paul II')
1201 eq(cf['tom']['sequel'], 'John Paul III')
1202 eq(cf['ambv']['favourite Beatle'], 'George')
1203 eq(cf['ambv']['favourite color'], 'green')
1204 eq(cf['ambv']['son of Edward VII'], 'George V')
1205 eq(cf['ambv']['son of George V'], 'George VI')
1206 eq(cf['stanley']['favourite Beatle'], 'George')
1207 eq(cf['stanley']['favourite color'], 'black')
1208 eq(cf['stanley']['favourite state of mind'], 'paranoid')
1209 eq(cf['stanley']['favourite movie'], 'soylent green')
1210 eq(cf['stanley']['favourite pope'], 'John Paul II')
1211 eq(cf['stanley']['favourite song'],
1212 'black sabbath - paranoid')
1213
1214 def test_endless_loop(self):
1215 cf = self.fromstring(textwrap.dedent("""
1216 [one for you]
1217 ping = ${one for me:pong}
1218
1219 [one for me]
1220 pong = ${one for you:ping}
1221
1222 [selfish]
1223 me = ${me}
1224 """).strip())
1225
1226 with self.assertRaises(configparser.InterpolationDepthError):
1227 cf['one for you']['ping']
1228 with self.assertRaises(configparser.InterpolationDepthError):
1229 cf['selfish']['me']
1230
1231 def test_strange_options(self):
1232 cf = self.fromstring("""
1233 [dollars]
1234 $var = $$value
1235 $var2 = ${$var}
1236 ${sick} = cannot interpolate me
1237
1238 [interpolated]
1239 $other = ${dollars:$var}
1240 $trying = ${dollars:${sick}}
1241 """)
1242
1243 self.assertEqual(cf['dollars']['$var'], '$value')
1244 self.assertEqual(cf['interpolated']['$other'], '$value')
1245 self.assertEqual(cf['dollars']['${sick}'], 'cannot interpolate me')
1246 exception_class = configparser.InterpolationMissingOptionError
1247 with self.assertRaises(exception_class) as cm:
1248 cf['interpolated']['$trying']
1249 self.assertEqual(cm.exception.reference, 'dollars:${sick')
1250 self.assertEqual(cm.exception.args[2], '${dollars:${sick}}') #rawval
1251
1252 def test_case_sensitivity_basic(self):
1253 ini = textwrap.dedent("""
1254 [common]
1255 optionlower = value
1256 OptionUpper = Value
1257
1258 [Common]
1259 optionlower = a better ${common:optionlower}
1260 OptionUpper = A Better ${common:OptionUpper}
1261
1262 [random]
1263 foolower = ${common:optionlower} redefined
1264 FooUpper = ${Common:OptionUpper} Redefined
1265 """).strip()
1266
1267 cf = self.fromstring(ini)
1268 eq = self.assertEqual
1269 eq(cf['common']['optionlower'], 'value')
1270 eq(cf['common']['OptionUpper'], 'Value')
1271 eq(cf['Common']['optionlower'], 'a better value')
1272 eq(cf['Common']['OptionUpper'], 'A Better Value')
1273 eq(cf['random']['foolower'], 'value redefined')
1274 eq(cf['random']['FooUpper'], 'A Better Value Redefined')
1275
1276 def test_case_sensitivity_conflicts(self):
1277 ini = textwrap.dedent("""
1278 [common]
1279 option = value
1280 Option = Value
1281
1282 [Common]
1283 option = a better ${common:option}
1284 Option = A Better ${common:Option}
1285
1286 [random]
1287 foo = ${common:option} redefined
1288 Foo = ${Common:Option} Redefined
1289 """).strip()
1290 with self.assertRaises(configparser.DuplicateOptionError):
1291 cf = self.fromstring(ini)
1292
1293 # raw options
1294 cf = self.fromstring(ini, optionxform=lambda opt: opt)
1295 eq = self.assertEqual
1296 eq(cf['common']['option'], 'value')
1297 eq(cf['common']['Option'], 'Value')
1298 eq(cf['Common']['option'], 'a better value')
1299 eq(cf['Common']['Option'], 'A Better Value')
1300 eq(cf['random']['foo'], 'value redefined')
1301 eq(cf['random']['Foo'], 'A Better Value Redefined')
1302
1303 def test_other_errors(self):
1304 cf = self.fromstring("""
1305 [interpolation fail]
1306 case1 = ${where's the brace
1307 case2 = ${does_not_exist}
1308 case3 = ${wrong_section:wrong_value}
1309 case4 = ${i:like:colon:characters}
1310 case5 = $100 for Fail No 5!
1311 """)
1312
1313 with self.assertRaises(configparser.InterpolationSyntaxError):
1314 cf['interpolation fail']['case1']
1315 with self.assertRaises(configparser.InterpolationMissingOptionError):
1316 cf['interpolation fail']['case2']
1317 with self.assertRaises(configparser.InterpolationMissingOptionError):
1318 cf['interpolation fail']['case3']
1319 with self.assertRaises(configparser.InterpolationSyntaxError):
1320 cf['interpolation fail']['case4']
1321 with self.assertRaises(configparser.InterpolationSyntaxError):
1322 cf['interpolation fail']['case5']
1323 with self.assertRaises(ValueError):
1324 cf['interpolation fail']['case6'] = "BLACK $ABBATH"
1325
1326
1327 class ConfigParserTestCaseNoValue(ConfigParserTestCase):
1328 allow_no_value = True
1329
1330
1331 class NoValueAndExtendedInterpolation(CfgParserTestCaseClass):
1332 interpolation = configparser.ExtendedInterpolation()
1333 allow_no_value = True
1334
1335 def test_interpolation_with_allow_no_value(self):
1336 config = textwrap.dedent("""
1337 [dummy]
1338 a
1339 b = ${a}
1340 """)
1341 cf = self.fromstring(config)
1342
1343 self.assertIs(cf["dummy"]["a"], None)
1344 self.assertEqual(cf["dummy"]["b"], "")
1345
1346 def test_explicit_none(self):
1347 config = textwrap.dedent("""
1348 [dummy]
1349 a = None
1350 b = ${a}
1351 """)
1352 cf = self.fromstring(config)
1353
1354 self.assertEqual(cf["dummy"]["a"], "None")
1355 self.assertEqual(cf["dummy"]["b"], "None")
1356
1357
1358 class ConfigParserNoValueAndExtendedInterpolationTest(
1359 NoValueAndExtendedInterpolation,
1360 unittest.TestCase,
1361 ):
1362 config_class = configparser.ConfigParser
1363
1364
1365 class RawConfigParserNoValueAndExtendedInterpolationTest(
1366 NoValueAndExtendedInterpolation,
1367 unittest.TestCase,
1368 ):
1369 config_class = configparser.RawConfigParser
1370
1371
1372 class ConfigParserTestCaseTrickyFile(CfgParserTestCaseClass, unittest.TestCase):
1373 config_class = configparser.ConfigParser
1374 delimiters = {'='}
1375 comment_prefixes = {'#'}
1376 allow_no_value = True
1377
1378 def test_cfgparser_dot_3(self):
1379 tricky = support.findfile("cfgparser.3", subdir="configdata")
1380 cf = self.newconfig()
1381 self.assertEqual(len(cf.read(tricky, encoding='utf-8')), 1)
1382 self.assertEqual(cf.sections(), ['strange',
1383 'corruption',
1384 'yeah, sections can be '
1385 'indented as well',
1386 'another one!',
1387 'no values here',
1388 'tricky interpolation',
1389 'more interpolation'])
1390 self.assertEqual(cf.getint(self.default_section, 'go',
1391 vars={'interpolate': '-1'}), -1)
1392 with self.assertRaises(ValueError):
1393 # no interpolation will happen
1394 cf.getint(self.default_section, 'go', raw=True,
1395 vars={'interpolate': '-1'})
1396 self.assertEqual(len(cf.get('strange', 'other').split('\n')), 4)
1397 self.assertEqual(len(cf.get('corruption', 'value').split('\n')), 10)
1398 longname = 'yeah, sections can be indented as well'
1399 self.assertFalse(cf.getboolean(longname, 'are they subsections'))
1400 self.assertEqual(cf.get(longname, 'lets use some Unicode'), '片仮名')
1401 self.assertEqual(len(cf.items('another one!')), 5) # 4 in section and
1402 # `go` from DEFAULT
1403 with self.assertRaises(configparser.InterpolationMissingOptionError):
1404 cf.items('no values here')
1405 self.assertEqual(cf.get('tricky interpolation', 'lets'), 'do this')
1406 self.assertEqual(cf.get('tricky interpolation', 'lets'),
1407 cf.get('tricky interpolation', 'go'))
1408 self.assertEqual(cf.get('more interpolation', 'lets'), 'go shopping')
1409
1410 def test_unicode_failure(self):
1411 tricky = support.findfile("cfgparser.3", subdir="configdata")
1412 cf = self.newconfig()
1413 with self.assertRaises(UnicodeDecodeError):
1414 cf.read(tricky, encoding='ascii')
1415
1416
1417 class Issue7005TestCase(unittest.TestCase):
1418 """Test output when None is set() as a value and allow_no_value == False.
1419
1420 http://bugs.python.org/issue7005
1421
1422 """
1423
1424 expected_output = "[section]\noption = None\n\n"
1425
1426 def prepare(self, config_class):
1427 # This is the default, but that's the point.
1428 cp = config_class(allow_no_value=False)
1429 cp.add_section("section")
1430 cp.set("section", "option", None)
1431 sio = io.StringIO()
1432 cp.write(sio)
1433 return sio.getvalue()
1434
1435 def test_none_as_value_stringified(self):
1436 cp = configparser.ConfigParser(allow_no_value=False)
1437 cp.add_section("section")
1438 with self.assertRaises(TypeError):
1439 cp.set("section", "option", None)
1440
1441 def test_none_as_value_stringified_raw(self):
1442 output = self.prepare(configparser.RawConfigParser)
1443 self.assertEqual(output, self.expected_output)
1444
1445
1446 class SortedTestCase(RawConfigParserTestCase):
1447 dict_type = SortedDict
1448
1449 def test_sorted(self):
1450 cf = self.fromstring("[b]\n"
1451 "o4=1\n"
1452 "o3=2\n"
1453 "o2=3\n"
1454 "o1=4\n"
1455 "[a]\n"
1456 "k=v\n")
1457 output = io.StringIO()
1458 cf.write(output)
1459 self.assertEqual(output.getvalue(),
1460 "[a]\n"
1461 "k = v\n\n"
1462 "[b]\n"
1463 "o1 = 4\n"
1464 "o2 = 3\n"
1465 "o3 = 2\n"
1466 "o4 = 1\n\n")
1467
1468
1469 class CompatibleTestCase(CfgParserTestCaseClass, unittest.TestCase):
1470 config_class = configparser.RawConfigParser
1471 comment_prefixes = '#;'
1472 inline_comment_prefixes = ';'
1473
1474 def test_comment_handling(self):
1475 config_string = textwrap.dedent("""\
1476 [Commented Bar]
1477 baz=qwe ; a comment
1478 foo: bar # not a comment!
1479 # but this is a comment
1480 ; another comment
1481 quirk: this;is not a comment
1482 ; a space must precede an inline comment
1483 """)
1484 cf = self.fromstring(config_string)
1485 self.assertEqual(cf.get('Commented Bar', 'foo'),
1486 'bar # not a comment!')
1487 self.assertEqual(cf.get('Commented Bar', 'baz'), 'qwe')
1488 self.assertEqual(cf.get('Commented Bar', 'quirk'),
1489 'this;is not a comment')
1490
1491 class CopyTestCase(BasicTestCase, unittest.TestCase):
1492 config_class = configparser.ConfigParser
1493
1494 def fromstring(self, string, defaults=None):
1495 cf = self.newconfig(defaults)
1496 cf.read_string(string)
1497 cf_copy = self.newconfig()
1498 cf_copy.read_dict(cf)
1499 # we have to clean up option duplicates that appeared because of
1500 # the magic DEFAULTSECT behaviour.
1501 for section in cf_copy.values():
1502 if section.name == self.default_section:
1503 continue
1504 for default, value in cf[self.default_section].items():
1505 if section[default] == value:
1506 del section[default]
1507 return cf_copy
1508
1509
1510 class FakeFile:
1511 def __init__(self):
1512 file_path = support.findfile("cfgparser.1", subdir="configdata")
1513 with open(file_path, encoding="utf-8") as f:
1514 self.lines = f.readlines()
1515 self.lines.reverse()
1516
1517 def readline(self):
1518 if len(self.lines):
1519 return self.lines.pop()
1520 return ''
1521
1522
1523 def readline_generator(f):
1524 """As advised in Doc/library/configparser.rst."""
1525 line = f.readline()
1526 while line:
1527 yield line
1528 line = f.readline()
1529
1530
1531 class ReadFileTestCase(unittest.TestCase):
1532 def test_file(self):
1533 file_paths = [support.findfile("cfgparser.1", subdir="configdata")]
1534 try:
1535 file_paths.append(file_paths[0].encode('utf8'))
1536 except UnicodeEncodeError:
1537 pass # unfortunately we can't test bytes on this path
1538 for file_path in file_paths:
1539 parser = configparser.ConfigParser()
1540 with open(file_path, encoding="utf-8") as f:
1541 parser.read_file(f)
1542 self.assertIn("Foo Bar", parser)
1543 self.assertIn("foo", parser["Foo Bar"])
1544 self.assertEqual(parser["Foo Bar"]["foo"], "newbar")
1545
1546 def test_iterable(self):
1547 lines = textwrap.dedent("""
1548 [Foo Bar]
1549 foo=newbar""").strip().split('\n')
1550 parser = configparser.ConfigParser()
1551 parser.read_file(lines)
1552 self.assertIn("Foo Bar", parser)
1553 self.assertIn("foo", parser["Foo Bar"])
1554 self.assertEqual(parser["Foo Bar"]["foo"], "newbar")
1555
1556 def test_readline_generator(self):
1557 """Issue #11670."""
1558 parser = configparser.ConfigParser()
1559 with self.assertRaises(TypeError):
1560 parser.read_file(FakeFile())
1561 parser.read_file(readline_generator(FakeFile()))
1562 self.assertIn("Foo Bar", parser)
1563 self.assertIn("foo", parser["Foo Bar"])
1564 self.assertEqual(parser["Foo Bar"]["foo"], "newbar")
1565
1566 def test_source_as_bytes(self):
1567 """Issue #18260."""
1568 lines = textwrap.dedent("""
1569 [badbad]
1570 [badbad]""").strip().split('\n')
1571 parser = configparser.ConfigParser()
1572 with self.assertRaises(configparser.DuplicateSectionError) as dse:
1573 parser.read_file(lines, source=b"badbad")
1574 self.assertEqual(
1575 str(dse.exception),
1576 "While reading from b'badbad' [line 2]: section 'badbad' "
1577 "already exists"
1578 )
1579 lines = textwrap.dedent("""
1580 [badbad]
1581 bad = bad
1582 bad = bad""").strip().split('\n')
1583 parser = configparser.ConfigParser()
1584 with self.assertRaises(configparser.DuplicateOptionError) as dse:
1585 parser.read_file(lines, source=b"badbad")
1586 self.assertEqual(
1587 str(dse.exception),
1588 "While reading from b'badbad' [line 3]: option 'bad' in section "
1589 "'badbad' already exists"
1590 )
1591 lines = textwrap.dedent("""
1592 [badbad]
1593 = bad""").strip().split('\n')
1594 parser = configparser.ConfigParser()
1595 with self.assertRaises(configparser.ParsingError) as dse:
1596 parser.read_file(lines, source=b"badbad")
1597 self.assertEqual(
1598 str(dse.exception),
1599 "Source contains parsing errors: b'badbad'\n\t[line 2]: '= bad'"
1600 )
1601 lines = textwrap.dedent("""
1602 [badbad
1603 bad = bad""").strip().split('\n')
1604 parser = configparser.ConfigParser()
1605 with self.assertRaises(configparser.MissingSectionHeaderError) as dse:
1606 parser.read_file(lines, source=b"badbad")
1607 self.assertEqual(
1608 str(dse.exception),
1609 "File contains no section headers.\nfile: b'badbad', line: 1\n"
1610 "'[badbad'"
1611 )
1612
1613 def test_keys_without_value_with_extra_whitespace(self):
1614 lines = [
1615 '[SECT]\n',
1616 'KEY1\n',
1617 ' KEY2 = VAL2\n', # note the Space before the key!
1618 ]
1619 parser = configparser.ConfigParser(
1620 comment_prefixes="",
1621 allow_no_value=True,
1622 strict=False,
1623 delimiters=('=',),
1624 interpolation=None,
1625 )
1626 with self.assertRaises(configparser.MultilineContinuationError) as dse:
1627 parser.read_file(lines)
1628 self.assertEqual(
1629 str(dse.exception),
1630 "Key without value continued with an indented line.\n"
1631 "file: '<???>', line: 3\n"
1632 "' KEY2 = VAL2\\n'"
1633 )
1634
1635
1636
1637
1638 class CoverageOneHundredTestCase(unittest.TestCase):
1639 """Covers edge cases in the codebase."""
1640
1641 def test_duplicate_option_error(self):
1642 error = configparser.DuplicateOptionError('section', 'option')
1643 self.assertEqual(error.section, 'section')
1644 self.assertEqual(error.option, 'option')
1645 self.assertEqual(error.source, None)
1646 self.assertEqual(error.lineno, None)
1647 self.assertEqual(error.args, ('section', 'option', None, None))
1648 self.assertEqual(str(error), "Option 'option' in section 'section' "
1649 "already exists")
1650
1651 def test_interpolation_depth_error(self):
1652 error = configparser.InterpolationDepthError('option', 'section',
1653 'rawval')
1654 self.assertEqual(error.args, ('option', 'section', 'rawval'))
1655 self.assertEqual(error.option, 'option')
1656 self.assertEqual(error.section, 'section')
1657
1658 def test_parsing_error(self):
1659 with self.assertRaises(TypeError) as cm:
1660 configparser.ParsingError()
1661 error = configparser.ParsingError(source='source')
1662 self.assertEqual(error.source, 'source')
1663 error = configparser.ParsingError('source')
1664 self.assertEqual(error.source, 'source')
1665
1666 def test_interpolation_validation(self):
1667 parser = configparser.ConfigParser()
1668 parser.read_string("""
1669 [section]
1670 invalid_percent = %
1671 invalid_reference = %(()
1672 invalid_variable = %(does_not_exist)s
1673 """)
1674 with self.assertRaises(configparser.InterpolationSyntaxError) as cm:
1675 parser['section']['invalid_percent']
1676 self.assertEqual(str(cm.exception), "'%' must be followed by '%' or "
1677 "'(', found: '%'")
1678 with self.assertRaises(configparser.InterpolationSyntaxError) as cm:
1679 parser['section']['invalid_reference']
1680 self.assertEqual(str(cm.exception), "bad interpolation variable "
1681 "reference '%(()'")
1682
1683 def test_sectionproxy_repr(self):
1684 parser = configparser.ConfigParser()
1685 parser.read_string("""
1686 [section]
1687 key = value
1688 """)
1689 self.assertEqual(repr(parser['section']), '<Section: section>')
1690
1691 def test_inconsistent_converters_state(self):
1692 parser = configparser.ConfigParser()
1693 import decimal
1694 parser.converters['decimal'] = decimal.Decimal
1695 parser.read_string("""
1696 [s1]
1697 one = 1
1698 [s2]
1699 two = 2
1700 """)
1701 self.assertIn('decimal', parser.converters)
1702 self.assertEqual(parser.getdecimal('s1', 'one'), 1)
1703 self.assertEqual(parser.getdecimal('s2', 'two'), 2)
1704 self.assertEqual(parser['s1'].getdecimal('one'), 1)
1705 self.assertEqual(parser['s2'].getdecimal('two'), 2)
1706 del parser.getdecimal
1707 with self.assertRaises(AttributeError):
1708 parser.getdecimal('s1', 'one')
1709 self.assertIn('decimal', parser.converters)
1710 del parser.converters['decimal']
1711 self.assertNotIn('decimal', parser.converters)
1712 with self.assertRaises(AttributeError):
1713 parser.getdecimal('s1', 'one')
1714 with self.assertRaises(AttributeError):
1715 parser['s1'].getdecimal('one')
1716 with self.assertRaises(AttributeError):
1717 parser['s2'].getdecimal('two')
1718
1719
1720 class ExceptionPicklingTestCase(unittest.TestCase):
1721 """Tests for issue #13760: ConfigParser exceptions are not picklable."""
1722
1723 def test_error(self):
1724 import pickle
1725 e1 = configparser.Error('value')
1726 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1727 pickled = pickle.dumps(e1, proto)
1728 e2 = pickle.loads(pickled)
1729 self.assertEqual(e1.message, e2.message)
1730 self.assertEqual(repr(e1), repr(e2))
1731
1732 def test_nosectionerror(self):
1733 import pickle
1734 e1 = configparser.NoSectionError('section')
1735 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1736 pickled = pickle.dumps(e1, proto)
1737 e2 = pickle.loads(pickled)
1738 self.assertEqual(e1.message, e2.message)
1739 self.assertEqual(e1.args, e2.args)
1740 self.assertEqual(e1.section, e2.section)
1741 self.assertEqual(repr(e1), repr(e2))
1742
1743 def test_nooptionerror(self):
1744 import pickle
1745 e1 = configparser.NoOptionError('option', 'section')
1746 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1747 pickled = pickle.dumps(e1, proto)
1748 e2 = pickle.loads(pickled)
1749 self.assertEqual(e1.message, e2.message)
1750 self.assertEqual(e1.args, e2.args)
1751 self.assertEqual(e1.section, e2.section)
1752 self.assertEqual(e1.option, e2.option)
1753 self.assertEqual(repr(e1), repr(e2))
1754
1755 def test_duplicatesectionerror(self):
1756 import pickle
1757 e1 = configparser.DuplicateSectionError('section', 'source', 123)
1758 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1759 pickled = pickle.dumps(e1, proto)
1760 e2 = pickle.loads(pickled)
1761 self.assertEqual(e1.message, e2.message)
1762 self.assertEqual(e1.args, e2.args)
1763 self.assertEqual(e1.section, e2.section)
1764 self.assertEqual(e1.source, e2.source)
1765 self.assertEqual(e1.lineno, e2.lineno)
1766 self.assertEqual(repr(e1), repr(e2))
1767
1768 def test_duplicateoptionerror(self):
1769 import pickle
1770 e1 = configparser.DuplicateOptionError('section', 'option', 'source',
1771 123)
1772 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1773 pickled = pickle.dumps(e1, proto)
1774 e2 = pickle.loads(pickled)
1775 self.assertEqual(e1.message, e2.message)
1776 self.assertEqual(e1.args, e2.args)
1777 self.assertEqual(e1.section, e2.section)
1778 self.assertEqual(e1.option, e2.option)
1779 self.assertEqual(e1.source, e2.source)
1780 self.assertEqual(e1.lineno, e2.lineno)
1781 self.assertEqual(repr(e1), repr(e2))
1782
1783 def test_interpolationerror(self):
1784 import pickle
1785 e1 = configparser.InterpolationError('option', 'section', 'msg')
1786 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1787 pickled = pickle.dumps(e1, proto)
1788 e2 = pickle.loads(pickled)
1789 self.assertEqual(e1.message, e2.message)
1790 self.assertEqual(e1.args, e2.args)
1791 self.assertEqual(e1.section, e2.section)
1792 self.assertEqual(e1.option, e2.option)
1793 self.assertEqual(repr(e1), repr(e2))
1794
1795 def test_interpolationmissingoptionerror(self):
1796 import pickle
1797 e1 = configparser.InterpolationMissingOptionError('option', 'section',
1798 'rawval', 'reference')
1799 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1800 pickled = pickle.dumps(e1, proto)
1801 e2 = pickle.loads(pickled)
1802 self.assertEqual(e1.message, e2.message)
1803 self.assertEqual(e1.args, e2.args)
1804 self.assertEqual(e1.section, e2.section)
1805 self.assertEqual(e1.option, e2.option)
1806 self.assertEqual(e1.reference, e2.reference)
1807 self.assertEqual(repr(e1), repr(e2))
1808
1809 def test_interpolationsyntaxerror(self):
1810 import pickle
1811 e1 = configparser.InterpolationSyntaxError('option', 'section', 'msg')
1812 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1813 pickled = pickle.dumps(e1, proto)
1814 e2 = pickle.loads(pickled)
1815 self.assertEqual(e1.message, e2.message)
1816 self.assertEqual(e1.args, e2.args)
1817 self.assertEqual(e1.section, e2.section)
1818 self.assertEqual(e1.option, e2.option)
1819 self.assertEqual(repr(e1), repr(e2))
1820
1821 def test_interpolationdeptherror(self):
1822 import pickle
1823 e1 = configparser.InterpolationDepthError('option', 'section',
1824 'rawval')
1825 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1826 pickled = pickle.dumps(e1, proto)
1827 e2 = pickle.loads(pickled)
1828 self.assertEqual(e1.message, e2.message)
1829 self.assertEqual(e1.args, e2.args)
1830 self.assertEqual(e1.section, e2.section)
1831 self.assertEqual(e1.option, e2.option)
1832 self.assertEqual(repr(e1), repr(e2))
1833
1834 def test_parsingerror(self):
1835 import pickle
1836 e1 = configparser.ParsingError('source')
1837 e1.append(1, 'line1')
1838 e1.append(2, 'line2')
1839 e1.append(3, 'line3')
1840 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1841 pickled = pickle.dumps(e1, proto)
1842 e2 = pickle.loads(pickled)
1843 self.assertEqual(e1.message, e2.message)
1844 self.assertEqual(e1.args, e2.args)
1845 self.assertEqual(e1.source, e2.source)
1846 self.assertEqual(e1.errors, e2.errors)
1847 self.assertEqual(repr(e1), repr(e2))
1848 e1 = configparser.ParsingError('filename')
1849 e1.append(1, 'line1')
1850 e1.append(2, 'line2')
1851 e1.append(3, 'line3')
1852 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1853 pickled = pickle.dumps(e1, proto)
1854 e2 = pickle.loads(pickled)
1855 self.assertEqual(e1.message, e2.message)
1856 self.assertEqual(e1.args, e2.args)
1857 self.assertEqual(e1.source, e2.source)
1858 self.assertEqual(e1.errors, e2.errors)
1859 self.assertEqual(repr(e1), repr(e2))
1860
1861 def test_missingsectionheadererror(self):
1862 import pickle
1863 e1 = configparser.MissingSectionHeaderError('filename', 123, 'line')
1864 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1865 pickled = pickle.dumps(e1, proto)
1866 e2 = pickle.loads(pickled)
1867 self.assertEqual(e1.message, e2.message)
1868 self.assertEqual(e1.args, e2.args)
1869 self.assertEqual(e1.line, e2.line)
1870 self.assertEqual(e1.source, e2.source)
1871 self.assertEqual(e1.lineno, e2.lineno)
1872 self.assertEqual(repr(e1), repr(e2))
1873
1874
1875 class InlineCommentStrippingTestCase(unittest.TestCase):
1876 """Tests for issue #14590: ConfigParser doesn't strip inline comment when
1877 delimiter occurs earlier without preceding space.."""
1878
1879 def test_stripping(self):
1880 cfg = configparser.ConfigParser(inline_comment_prefixes=(';', '#',
1881 '//'))
1882 cfg.read_string("""
1883 [section]
1884 k1 = v1;still v1
1885 k2 = v2 ;a comment
1886 k3 = v3 ; also a comment
1887 k4 = v4;still v4 ;a comment
1888 k5 = v5;still v5 ; also a comment
1889 k6 = v6;still v6; and still v6 ;a comment
1890 k7 = v7;still v7; and still v7 ; also a comment
1891
1892 [multiprefix]
1893 k1 = v1;still v1 #a comment ; yeah, pretty much
1894 k2 = v2 // this already is a comment ; continued
1895 k3 = v3;#//still v3# and still v3 ; a comment
1896 """)
1897 s = cfg['section']
1898 self.assertEqual(s['k1'], 'v1;still v1')
1899 self.assertEqual(s['k2'], 'v2')
1900 self.assertEqual(s['k3'], 'v3')
1901 self.assertEqual(s['k4'], 'v4;still v4')
1902 self.assertEqual(s['k5'], 'v5;still v5')
1903 self.assertEqual(s['k6'], 'v6;still v6; and still v6')
1904 self.assertEqual(s['k7'], 'v7;still v7; and still v7')
1905 s = cfg['multiprefix']
1906 self.assertEqual(s['k1'], 'v1;still v1')
1907 self.assertEqual(s['k2'], 'v2')
1908 self.assertEqual(s['k3'], 'v3;#//still v3# and still v3')
1909
1910
1911 class ExceptionContextTestCase(unittest.TestCase):
1912 """ Test that implementation details doesn't leak
1913 through raising exceptions. """
1914
1915 def test_get_basic_interpolation(self):
1916 parser = configparser.ConfigParser()
1917 parser.read_string("""
1918 [Paths]
1919 home_dir: /Users
1920 my_dir: %(home_dir1)s/lumberjack
1921 my_pictures: %(my_dir)s/Pictures
1922 """)
1923 cm = self.assertRaises(configparser.InterpolationMissingOptionError)
1924 with cm:
1925 parser.get('Paths', 'my_dir')
1926 self.assertIs(cm.exception.__suppress_context__, True)
1927
1928 def test_get_extended_interpolation(self):
1929 parser = configparser.ConfigParser(
1930 interpolation=configparser.ExtendedInterpolation())
1931 parser.read_string("""
1932 [Paths]
1933 home_dir: /Users
1934 my_dir: ${home_dir1}/lumberjack
1935 my_pictures: ${my_dir}/Pictures
1936 """)
1937 cm = self.assertRaises(configparser.InterpolationMissingOptionError)
1938 with cm:
1939 parser.get('Paths', 'my_dir')
1940 self.assertIs(cm.exception.__suppress_context__, True)
1941
1942 def test_missing_options(self):
1943 parser = configparser.ConfigParser()
1944 parser.read_string("""
1945 [Paths]
1946 home_dir: /Users
1947 """)
1948 with self.assertRaises(configparser.NoSectionError) as cm:
1949 parser.options('test')
1950 self.assertIs(cm.exception.__suppress_context__, True)
1951
1952 def test_missing_section(self):
1953 config = configparser.ConfigParser()
1954 with self.assertRaises(configparser.NoSectionError) as cm:
1955 config.set('Section1', 'an_int', '15')
1956 self.assertIs(cm.exception.__suppress_context__, True)
1957
1958 def test_remove_option(self):
1959 config = configparser.ConfigParser()
1960 with self.assertRaises(configparser.NoSectionError) as cm:
1961 config.remove_option('Section1', 'an_int')
1962 self.assertIs(cm.exception.__suppress_context__, True)
1963
1964
1965 class ConvertersTestCase(BasicTestCase, unittest.TestCase):
1966 """Introduced in 3.5, issue #18159."""
1967
1968 config_class = configparser.ConfigParser
1969
1970 def newconfig(self, defaults=None):
1971 instance = super().newconfig(defaults=defaults)
1972 instance.converters['list'] = lambda v: [e.strip() for e in v.split()
1973 if e.strip()]
1974 return instance
1975
1976 def test_converters(self):
1977 cfg = self.newconfig()
1978 self.assertIn('boolean', cfg.converters)
1979 self.assertIn('list', cfg.converters)
1980 self.assertIsNone(cfg.converters['int'])
1981 self.assertIsNone(cfg.converters['float'])
1982 self.assertIsNone(cfg.converters['boolean'])
1983 self.assertIsNotNone(cfg.converters['list'])
1984 self.assertEqual(len(cfg.converters), 4)
1985 with self.assertRaises(ValueError):
1986 cfg.converters[''] = lambda v: v
1987 with self.assertRaises(ValueError):
1988 cfg.converters[None] = lambda v: v
1989 cfg.read_string("""
1990 [s]
1991 str = string
1992 int = 1
1993 float = 0.5
1994 list = a b c d e f g
1995 bool = yes
1996 """)
1997 s = cfg['s']
1998 self.assertEqual(s['str'], 'string')
1999 self.assertEqual(s['int'], '1')
2000 self.assertEqual(s['float'], '0.5')
2001 self.assertEqual(s['list'], 'a b c d e f g')
2002 self.assertEqual(s['bool'], 'yes')
2003 self.assertEqual(cfg.get('s', 'str'), 'string')
2004 self.assertEqual(cfg.get('s', 'int'), '1')
2005 self.assertEqual(cfg.get('s', 'float'), '0.5')
2006 self.assertEqual(cfg.get('s', 'list'), 'a b c d e f g')
2007 self.assertEqual(cfg.get('s', 'bool'), 'yes')
2008 self.assertEqual(cfg.get('s', 'str'), 'string')
2009 self.assertEqual(cfg.getint('s', 'int'), 1)
2010 self.assertEqual(cfg.getfloat('s', 'float'), 0.5)
2011 self.assertEqual(cfg.getlist('s', 'list'), ['a', 'b', 'c', 'd',
2012 'e', 'f', 'g'])
2013 self.assertEqual(cfg.getboolean('s', 'bool'), True)
2014 self.assertEqual(s.get('str'), 'string')
2015 self.assertEqual(s.getint('int'), 1)
2016 self.assertEqual(s.getfloat('float'), 0.5)
2017 self.assertEqual(s.getlist('list'), ['a', 'b', 'c', 'd',
2018 'e', 'f', 'g'])
2019 self.assertEqual(s.getboolean('bool'), True)
2020 with self.assertRaises(AttributeError):
2021 cfg.getdecimal('s', 'float')
2022 with self.assertRaises(AttributeError):
2023 s.getdecimal('float')
2024 import decimal
2025 cfg.converters['decimal'] = decimal.Decimal
2026 self.assertIn('decimal', cfg.converters)
2027 self.assertIsNotNone(cfg.converters['decimal'])
2028 self.assertEqual(len(cfg.converters), 5)
2029 dec0_5 = decimal.Decimal('0.5')
2030 self.assertEqual(cfg.getdecimal('s', 'float'), dec0_5)
2031 self.assertEqual(s.getdecimal('float'), dec0_5)
2032 del cfg.converters['decimal']
2033 self.assertNotIn('decimal', cfg.converters)
2034 self.assertEqual(len(cfg.converters), 4)
2035 with self.assertRaises(AttributeError):
2036 cfg.getdecimal('s', 'float')
2037 with self.assertRaises(AttributeError):
2038 s.getdecimal('float')
2039 with self.assertRaises(KeyError):
2040 del cfg.converters['decimal']
2041 with self.assertRaises(KeyError):
2042 del cfg.converters['']
2043 with self.assertRaises(KeyError):
2044 del cfg.converters[None]
2045
2046
2047 class BlatantOverrideConvertersTestCase(unittest.TestCase):
2048 """What if somebody overrode a getboolean()? We want to make sure that in
2049 this case the automatic converters do not kick in."""
2050
2051 config = """
2052 [one]
2053 one = false
2054 two = false
2055 three = long story short
2056
2057 [two]
2058 one = false
2059 two = false
2060 three = four
2061 """
2062
2063 def test_converters_at_init(self):
2064 cfg = configparser.ConfigParser(converters={'len': len})
2065 cfg.read_string(self.config)
2066 self._test_len(cfg)
2067 self.assertIsNotNone(cfg.converters['len'])
2068
2069 def test_inheritance(self):
2070 class StrangeConfigParser(configparser.ConfigParser):
2071 gettysburg = 'a historic borough in south central Pennsylvania'
2072
2073 def getboolean(self, section, option, *, raw=False, vars=None,
2074 fallback=configparser._UNSET):
2075 if section == option:
2076 return True
2077 return super().getboolean(section, option, raw=raw, vars=vars,
2078 fallback=fallback)
2079 def getlen(self, section, option, *, raw=False, vars=None,
2080 fallback=configparser._UNSET):
2081 return self._get_conv(section, option, len, raw=raw, vars=vars,
2082 fallback=fallback)
2083
2084 cfg = StrangeConfigParser()
2085 cfg.read_string(self.config)
2086 self._test_len(cfg)
2087 self.assertIsNone(cfg.converters['len'])
2088 self.assertTrue(cfg.getboolean('one', 'one'))
2089 self.assertTrue(cfg.getboolean('two', 'two'))
2090 self.assertFalse(cfg.getboolean('one', 'two'))
2091 self.assertFalse(cfg.getboolean('two', 'one'))
2092 cfg.converters['boolean'] = cfg._convert_to_boolean
2093 self.assertFalse(cfg.getboolean('one', 'one'))
2094 self.assertFalse(cfg.getboolean('two', 'two'))
2095 self.assertFalse(cfg.getboolean('one', 'two'))
2096 self.assertFalse(cfg.getboolean('two', 'one'))
2097
2098 def _test_len(self, cfg):
2099 self.assertEqual(len(cfg.converters), 4)
2100 self.assertIn('boolean', cfg.converters)
2101 self.assertIn('len', cfg.converters)
2102 self.assertNotIn('tysburg', cfg.converters)
2103 self.assertIsNone(cfg.converters['int'])
2104 self.assertIsNone(cfg.converters['float'])
2105 self.assertIsNone(cfg.converters['boolean'])
2106 self.assertEqual(cfg.getlen('one', 'one'), 5)
2107 self.assertEqual(cfg.getlen('one', 'two'), 5)
2108 self.assertEqual(cfg.getlen('one', 'three'), 16)
2109 self.assertEqual(cfg.getlen('two', 'one'), 5)
2110 self.assertEqual(cfg.getlen('two', 'two'), 5)
2111 self.assertEqual(cfg.getlen('two', 'three'), 4)
2112 self.assertEqual(cfg.getlen('two', 'four', fallback=0), 0)
2113 with self.assertRaises(configparser.NoOptionError):
2114 cfg.getlen('two', 'four')
2115 self.assertEqual(cfg['one'].getlen('one'), 5)
2116 self.assertEqual(cfg['one'].getlen('two'), 5)
2117 self.assertEqual(cfg['one'].getlen('three'), 16)
2118 self.assertEqual(cfg['two'].getlen('one'), 5)
2119 self.assertEqual(cfg['two'].getlen('two'), 5)
2120 self.assertEqual(cfg['two'].getlen('three'), 4)
2121 self.assertEqual(cfg['two'].getlen('four', 0), 0)
2122 self.assertEqual(cfg['two'].getlen('four'), None)
2123
2124 def test_instance_assignment(self):
2125 cfg = configparser.ConfigParser()
2126 cfg.getboolean = lambda section, option: True
2127 cfg.getlen = lambda section, option: len(cfg[section][option])
2128 cfg.read_string(self.config)
2129 self.assertEqual(len(cfg.converters), 3)
2130 self.assertIn('boolean', cfg.converters)
2131 self.assertNotIn('len', cfg.converters)
2132 self.assertIsNone(cfg.converters['int'])
2133 self.assertIsNone(cfg.converters['float'])
2134 self.assertIsNone(cfg.converters['boolean'])
2135 self.assertTrue(cfg.getboolean('one', 'one'))
2136 self.assertTrue(cfg.getboolean('two', 'two'))
2137 self.assertTrue(cfg.getboolean('one', 'two'))
2138 self.assertTrue(cfg.getboolean('two', 'one'))
2139 cfg.converters['boolean'] = cfg._convert_to_boolean
2140 self.assertFalse(cfg.getboolean('one', 'one'))
2141 self.assertFalse(cfg.getboolean('two', 'two'))
2142 self.assertFalse(cfg.getboolean('one', 'two'))
2143 self.assertFalse(cfg.getboolean('two', 'one'))
2144 self.assertEqual(cfg.getlen('one', 'one'), 5)
2145 self.assertEqual(cfg.getlen('one', 'two'), 5)
2146 self.assertEqual(cfg.getlen('one', 'three'), 16)
2147 self.assertEqual(cfg.getlen('two', 'one'), 5)
2148 self.assertEqual(cfg.getlen('two', 'two'), 5)
2149 self.assertEqual(cfg.getlen('two', 'three'), 4)
2150 # If a getter impl is assigned straight to the instance, it won't
2151 # be available on the section proxies.
2152 with self.assertRaises(AttributeError):
2153 self.assertEqual(cfg['one'].getlen('one'), 5)
2154 with self.assertRaises(AttributeError):
2155 self.assertEqual(cfg['two'].getlen('one'), 5)
2156
2157
2158 class SectionlessTestCase(unittest.TestCase):
2159
2160 def fromstring(self, string):
2161 cfg = configparser.ConfigParser(allow_unnamed_section=True)
2162 cfg.read_string(string)
2163 return cfg
2164
2165 def test_no_first_section(self):
2166 cfg1 = self.fromstring("""
2167 a = 1
2168 b = 2
2169 [sect1]
2170 c = 3
2171 """)
2172
2173 self.assertEqual(set([configparser.UNNAMED_SECTION, 'sect1']), set(cfg1.sections()))
2174 self.assertEqual('1', cfg1[configparser.UNNAMED_SECTION]['a'])
2175 self.assertEqual('2', cfg1[configparser.UNNAMED_SECTION]['b'])
2176 self.assertEqual('3', cfg1['sect1']['c'])
2177
2178 output = io.StringIO()
2179 cfg1.write(output)
2180 cfg2 = self.fromstring(output.getvalue())
2181
2182 #self.assertEqual(set([configparser.UNNAMED_SECTION, 'sect1']), set(cfg2.sections()))
2183 self.assertEqual('1', cfg2[configparser.UNNAMED_SECTION]['a'])
2184 self.assertEqual('2', cfg2[configparser.UNNAMED_SECTION]['b'])
2185 self.assertEqual('3', cfg2['sect1']['c'])
2186
2187 def test_no_section(self):
2188 cfg1 = self.fromstring("""
2189 a = 1
2190 b = 2
2191 """)
2192
2193 self.assertEqual([configparser.UNNAMED_SECTION], cfg1.sections())
2194 self.assertEqual('1', cfg1[configparser.UNNAMED_SECTION]['a'])
2195 self.assertEqual('2', cfg1[configparser.UNNAMED_SECTION]['b'])
2196
2197 output = io.StringIO()
2198 cfg1.write(output)
2199 cfg2 = self.fromstring(output.getvalue())
2200
2201 self.assertEqual([configparser.UNNAMED_SECTION], cfg2.sections())
2202 self.assertEqual('1', cfg2[configparser.UNNAMED_SECTION]['a'])
2203 self.assertEqual('2', cfg2[configparser.UNNAMED_SECTION]['b'])
2204
2205 def test_multiple_configs(self):
2206 cfg = configparser.ConfigParser(allow_unnamed_section=True)
2207 cfg.read_string('a = 1')
2208 cfg.read_string('b = 2')
2209
2210 self.assertEqual([configparser.UNNAMED_SECTION], cfg.sections())
2211 self.assertEqual('1', cfg[configparser.UNNAMED_SECTION]['a'])
2212 self.assertEqual('2', cfg[configparser.UNNAMED_SECTION]['b'])
2213
2214
2215 class MiscTestCase(unittest.TestCase):
2216 def test__all__(self):
2217 support.check__all__(self, configparser, not_exported={"Error"})
2218
2219
2220 if __name__ == '__main__':
2221 unittest.main()