Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_configparser.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 56c1227f21f5 |
| children |
| rev | line source |
|---|---|
|
63220
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1 import collections |
|
47012
35a8899256c2
Renamed ConfigParser to configparser.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45446
diff
changeset
|
2 import configparser |
|
42501
f6069f9dd52d
SF patch# 1770008 by Christian Heimes (plus some extras).
Guido van Rossum <guido@python.org>
parents:
42050
diff
changeset
|
3 import io |
|
63220
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
4 import os |
|
66707
f1be349234c3
support for checking test coverage added.
Łukasz Langa <lukasz@langa.pl>
parents:
66702
diff
changeset
|
5 import sys |
|
f1be349234c3
support for checking test coverage added.
Łukasz Langa <lukasz@langa.pl>
parents:
66702
diff
changeset
|
6 import textwrap |
| 25910 | 7 import unittest |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
8 import warnings |
|
14900
f932c128279b
Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
9 |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
47012
diff
changeset
|
10 from test import support |
|
15480
3c33f2c357d3
Add test cases for ConfigParser.remove_option() behavior. This includes
Fred Drake <fdrake@acm.org>
parents:
15305
diff
changeset
|
11 |
|
45185
8ef3c7c00470
Migrate remaining tests from UserDict.UserDict to collections.UserDict.
Raymond Hettinger <python@rcn.com>
parents:
42501
diff
changeset
|
12 class SortedDict(collections.UserDict): |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
13 |
|
40418
3ee9d1a36d46
Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents:
33665
diff
changeset
|
14 def items(self): |
|
40809
f340cb045bf9
- PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents:
40742
diff
changeset
|
15 return sorted(self.data.items()) |
|
40418
3ee9d1a36d46
Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents:
33665
diff
changeset
|
16 |
|
3ee9d1a36d46
Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents:
33665
diff
changeset
|
17 def keys(self): |
|
40809
f340cb045bf9
- PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents:
40742
diff
changeset
|
18 return sorted(self.data.keys()) |
|
40742
f1701ba2452b
Merged revisions 53538-53622 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40587
diff
changeset
|
19 |
|
40418
3ee9d1a36d46
Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents:
33665
diff
changeset
|
20 def values(self): |
|
40809
f340cb045bf9
- PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents:
40742
diff
changeset
|
21 return [i[1] for i in self.items()] |
|
40418
3ee9d1a36d46
Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents:
33665
diff
changeset
|
22 |
|
69657
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
23 def iteritems(self): |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
24 return iter(self.items()) |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
25 |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
26 def iterkeys(self): |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
27 return iter(self.keys()) |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
28 |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
29 def itervalues(self): |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
30 return iter(self.values()) |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
31 |
|
40418
3ee9d1a36d46
Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents:
33665
diff
changeset
|
32 __iter__ = iterkeys |
|
22296
9a091d72136a
backport akuchling's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20321
diff
changeset
|
33 |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
34 |
|
81385
7dbdd7204d0a
#16925: test_configparser now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81211
diff
changeset
|
35 class CfgParserTestCaseClass: |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
36 allow_no_value = False |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
37 delimiters = ('=', ':') |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
38 comment_prefixes = (';', '#') |
|
66981
074c21714a02
configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents:
66953
diff
changeset
|
39 inline_comment_prefixes = (';', '#') |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
40 empty_lines_in_values = True |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
41 dict_type = configparser._default_dict |
| 63875 | 42 strict = False |
|
66287
51e1cbdd8fd2
configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents:
66143
diff
changeset
|
43 default_section = configparser.DEFAULTSECT |
|
66647
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
44 interpolation = configparser._UNSET |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
45 |
| 25910 | 46 def newconfig(self, defaults=None): |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
47 arguments = dict( |
| 63875 | 48 defaults=defaults, |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
49 allow_no_value=self.allow_no_value, |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
50 delimiters=self.delimiters, |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
51 comment_prefixes=self.comment_prefixes, |
|
66981
074c21714a02
configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents:
66953
diff
changeset
|
52 inline_comment_prefixes=self.inline_comment_prefixes, |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
53 empty_lines_in_values=self.empty_lines_in_values, |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
54 dict_type=self.dict_type, |
| 63875 | 55 strict=self.strict, |
|
66287
51e1cbdd8fd2
configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents:
66143
diff
changeset
|
56 default_section=self.default_section, |
|
66647
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
57 interpolation=self.interpolation, |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
58 ) |
|
66953
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
59 instance = self.config_class(**arguments) |
|
66647
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
60 return instance |
| 25910 | 61 |
| 62 def fromstring(self, string, defaults=None): | |
| 63 cf = self.newconfig(defaults) | |
| 63875 | 64 cf.read_string(string) |
| 25910 | 65 return cf |
|
16771
b235160f1346
Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents:
16595
diff
changeset
|
66 |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
67 class BasicTestCase(CfgParserTestCaseClass): |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
68 |
| 63875 | 69 def basic_test(self, cf): |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
70 E = ['Commented Bar', |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
71 'Foo Bar', |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
72 'Internationalized Stuff', |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
73 'Long Line', |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
74 'Section\\with$weird%characters[\t', |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
75 'Spaces', |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
76 'Spacey Bar', |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
77 'Spacey Bar From The Beginning', |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
78 'Types', |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
79 ] |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
80 |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
81 if self.allow_no_value: |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
82 E.append('NoValue') |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
83 E.sort() |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
84 F = [('baz', 'qwe'), ('foo', 'bar3')] |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
85 |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
86 # API access |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
87 L = cf.sections() |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
88 L.sort() |
| 25910 | 89 eq = self.assertEqual |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
90 eq(L, E) |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
91 L = cf.items('Spacey Bar From The Beginning') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
92 L.sort() |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
93 eq(L, F) |
|
16771
b235160f1346
Make sure ConfigParser uses .optionxform() consistently; this affects
Fred Drake <fdrake@acm.org>
parents:
16595
diff
changeset
|
94 |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
95 # mapping access |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
96 L = [section for section in cf] |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
97 L.sort() |
|
66287
51e1cbdd8fd2
configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents:
66143
diff
changeset
|
98 E.append(self.default_section) |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
99 E.sort() |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
100 eq(L, E) |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
101 L = cf['Spacey Bar From The Beginning'].items() |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
102 L = sorted(list(L)) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
103 eq(L, F) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
104 L = cf.items() |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
105 L = sorted(list(L)) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
106 self.assertEqual(len(L), len(E)) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
107 for name, section in L: |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
108 eq(name, section.name) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
109 eq(cf.defaults(), cf[self.default_section]) |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
110 |
| 25910 | 111 # The use of spaces in the section names serves as a |
| 112 # regression test for SourceForge bug #583248: | |
| 113 # http://www.python.org/sf/583248 | |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
114 |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
115 # API access |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
116 eq(cf.get('Foo Bar', 'foo'), 'bar1') |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
117 eq(cf.get('Spacey Bar', 'foo'), 'bar2') |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
118 eq(cf.get('Spacey Bar From The Beginning', 'foo'), 'bar3') |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
119 eq(cf.get('Spacey Bar From The Beginning', 'baz'), 'qwe') |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
120 eq(cf.get('Commented Bar', 'foo'), 'bar4') |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
121 eq(cf.get('Commented Bar', 'baz'), 'qwe') |
| 25910 | 122 eq(cf.get('Spaces', 'key with spaces'), 'value') |
| 123 eq(cf.get('Spaces', 'another with spaces'), 'splat!') | |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
124 eq(cf.getint('Types', 'int'), 42) |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
125 eq(cf.get('Types', 'int'), "42") |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
126 self.assertAlmostEqual(cf.getfloat('Types', 'float'), 0.44) |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
127 eq(cf.get('Types', 'float'), "0.44") |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
128 eq(cf.getboolean('Types', 'boolean'), False) |
|
66702
ddc6c47bf825
configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents:
66647
diff
changeset
|
129 eq(cf.get('Types', '123'), 'strange but acceptable') |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
130 if self.allow_no_value: |
|
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
131 eq(cf.get('NoValue', 'option-without-value'), None) |
| 25910 | 132 |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
133 # test vars= and fallback= |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
134 eq(cf.get('Foo Bar', 'foo', fallback='baz'), 'bar1') |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
135 eq(cf.get('Foo Bar', 'foo', vars={'foo': 'baz'}), 'baz') |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
136 with self.assertRaises(configparser.NoSectionError): |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
137 cf.get('No Such Foo Bar', 'foo') |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
138 with self.assertRaises(configparser.NoOptionError): |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
139 cf.get('Foo Bar', 'no-such-foo') |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
140 eq(cf.get('No Such Foo Bar', 'foo', fallback='baz'), 'baz') |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
141 eq(cf.get('Foo Bar', 'no-such-foo', fallback='baz'), 'baz') |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
142 eq(cf.get('Spacey Bar', 'foo', fallback=None), 'bar2') |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
143 eq(cf.get('No Such Spacey Bar', 'foo', fallback=None), None) |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
144 eq(cf.getint('Types', 'int', fallback=18), 42) |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
145 eq(cf.getint('Types', 'no-such-int', fallback=18), 18) |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
146 eq(cf.getint('Types', 'no-such-int', fallback="18"), "18") # sic! |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
147 with self.assertRaises(configparser.NoOptionError): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
148 cf.getint('Types', 'no-such-int') |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
149 self.assertAlmostEqual(cf.getfloat('Types', 'float', |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
150 fallback=0.0), 0.44) |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
151 self.assertAlmostEqual(cf.getfloat('Types', 'no-such-float', |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
152 fallback=0.0), 0.0) |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
153 eq(cf.getfloat('Types', 'no-such-float', fallback="0.0"), "0.0") # sic! |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
154 with self.assertRaises(configparser.NoOptionError): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
155 cf.getfloat('Types', 'no-such-float') |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
156 eq(cf.getboolean('Types', 'boolean', fallback=True), False) |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
157 eq(cf.getboolean('Types', 'no-such-boolean', fallback="yes"), |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
158 "yes") # sic! |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
159 eq(cf.getboolean('Types', 'no-such-boolean', fallback=True), True) |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
160 with self.assertRaises(configparser.NoOptionError): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
161 cf.getboolean('Types', 'no-such-boolean') |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
162 eq(cf.getboolean('No Such Types', 'boolean', fallback=True), True) |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
163 if self.allow_no_value: |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
164 eq(cf.get('NoValue', 'option-without-value', fallback=False), None) |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
165 eq(cf.get('NoValue', 'no-such-option-without-value', |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
166 fallback=False), False) |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
167 |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
168 # mapping access |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
169 eq(cf['Foo Bar']['foo'], 'bar1') |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
170 eq(cf['Spacey Bar']['foo'], 'bar2') |
|
66350
f8ceb6afd7a9
configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents:
66349
diff
changeset
|
171 section = cf['Spacey Bar From The Beginning'] |
|
f8ceb6afd7a9
configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents:
66349
diff
changeset
|
172 eq(section.name, 'Spacey Bar From The Beginning') |
|
f8ceb6afd7a9
configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents:
66349
diff
changeset
|
173 self.assertIs(section.parser, cf) |
|
f8ceb6afd7a9
configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents:
66349
diff
changeset
|
174 with self.assertRaises(AttributeError): |
|
f8ceb6afd7a9
configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents:
66349
diff
changeset
|
175 section.name = 'Name is read-only' |
|
f8ceb6afd7a9
configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents:
66349
diff
changeset
|
176 with self.assertRaises(AttributeError): |
|
f8ceb6afd7a9
configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents:
66349
diff
changeset
|
177 section.parser = 'Parser is read-only' |
|
f8ceb6afd7a9
configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents:
66349
diff
changeset
|
178 eq(section['foo'], 'bar3') |
|
f8ceb6afd7a9
configparser: read-only attributes to get the section name and parser from a SectionProxy instance
Łukasz Langa <lukasz@langa.pl>
parents:
66349
diff
changeset
|
179 eq(section['baz'], 'qwe') |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
180 eq(cf['Commented Bar']['foo'], 'bar4') |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
181 eq(cf['Commented Bar']['baz'], 'qwe') |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
182 eq(cf['Spaces']['key with spaces'], 'value') |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
183 eq(cf['Spaces']['another with spaces'], 'splat!') |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
184 eq(cf['Long Line']['foo'], |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
185 'this line is much, much longer than my editor\nlikes it.') |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
186 if self.allow_no_value: |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
187 eq(cf['NoValue']['option-without-value'], None) |
|
66722
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
188 # test vars= and fallback= |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
189 eq(cf['Foo Bar'].get('foo', 'baz'), 'bar1') |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
190 eq(cf['Foo Bar'].get('foo', fallback='baz'), 'bar1') |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
191 eq(cf['Foo Bar'].get('foo', vars={'foo': 'baz'}), 'baz') |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
192 with self.assertRaises(KeyError): |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
193 cf['No Such Foo Bar']['foo'] |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
194 with self.assertRaises(KeyError): |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
195 cf['Foo Bar']['no-such-foo'] |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
196 with self.assertRaises(KeyError): |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
197 cf['No Such Foo Bar'].get('foo', fallback='baz') |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
198 eq(cf['Foo Bar'].get('no-such-foo', 'baz'), 'baz') |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
199 eq(cf['Foo Bar'].get('no-such-foo', fallback='baz'), 'baz') |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
200 eq(cf['Foo Bar'].get('no-such-foo'), None) |
|
66722
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
201 eq(cf['Spacey Bar'].get('foo', None), 'bar2') |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
202 eq(cf['Spacey Bar'].get('foo', fallback=None), 'bar2') |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
203 with self.assertRaises(KeyError): |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
204 cf['No Such Spacey Bar'].get('foo', None) |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
205 eq(cf['Types'].getint('int', 18), 42) |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
206 eq(cf['Types'].getint('int', fallback=18), 42) |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
207 eq(cf['Types'].getint('no-such-int', 18), 18) |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
208 eq(cf['Types'].getint('no-such-int', fallback=18), 18) |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
209 eq(cf['Types'].getint('no-such-int', "18"), "18") # sic! |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
210 eq(cf['Types'].getint('no-such-int', fallback="18"), "18") # sic! |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
211 eq(cf['Types'].getint('no-such-int'), None) |
|
66722
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
212 self.assertAlmostEqual(cf['Types'].getfloat('float', 0.0), 0.44) |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
213 self.assertAlmostEqual(cf['Types'].getfloat('float', |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
214 fallback=0.0), 0.44) |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
215 self.assertAlmostEqual(cf['Types'].getfloat('no-such-float', 0.0), 0.0) |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
216 self.assertAlmostEqual(cf['Types'].getfloat('no-such-float', |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
217 fallback=0.0), 0.0) |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
218 eq(cf['Types'].getfloat('no-such-float', "0.0"), "0.0") # sic! |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
219 eq(cf['Types'].getfloat('no-such-float', fallback="0.0"), "0.0") # sic! |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
220 eq(cf['Types'].getfloat('no-such-float'), None) |
|
66722
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
221 eq(cf['Types'].getboolean('boolean', True), False) |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
222 eq(cf['Types'].getboolean('boolean', fallback=True), False) |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
223 eq(cf['Types'].getboolean('no-such-boolean', "yes"), "yes") # sic! |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
224 eq(cf['Types'].getboolean('no-such-boolean', fallback="yes"), |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
225 "yes") # sic! |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
226 eq(cf['Types'].getboolean('no-such-boolean', True), True) |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
227 eq(cf['Types'].getboolean('no-such-boolean', fallback=True), True) |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
228 eq(cf['Types'].getboolean('no-such-boolean'), None) |
|
66722
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
229 if self.allow_no_value: |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
230 eq(cf['NoValue'].get('option-without-value', False), None) |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
231 eq(cf['NoValue'].get('option-without-value', fallback=False), None) |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
232 eq(cf['NoValue'].get('no-such-option-without-value', False), False) |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
233 eq(cf['NoValue'].get('no-such-option-without-value', |
|
3fee88e6442a
configparser: mapping protocol access get() handles configparser-specific arguments as well
Łukasz Langa <lukasz@langa.pl>
parents:
66707
diff
changeset
|
234 fallback=False), False) |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
235 |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
236 # Make sure the right things happen for remove_section() and |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
237 # remove_option(); added to include check for SourceForge bug #123324. |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
238 |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
239 cf[self.default_section]['this_value'] = '1' |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
240 cf[self.default_section]['that_value'] = '2' |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
241 |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
242 # API access |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
243 self.assertTrue(cf.remove_section('Spaces')) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
244 self.assertFalse(cf.has_option('Spaces', 'key with spaces')) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
245 self.assertFalse(cf.remove_section('Spaces')) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
246 self.assertFalse(cf.remove_section(self.default_section)) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53524
diff
changeset
|
247 self.assertTrue(cf.remove_option('Foo Bar', 'foo'), |
|
51776
d06a3ebf306f
Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47201
diff
changeset
|
248 "remove_option() failed to report existence of option") |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53524
diff
changeset
|
249 self.assertFalse(cf.has_option('Foo Bar', 'foo'), |
| 25910 | 250 "remove_option() failed to remove option") |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53524
diff
changeset
|
251 self.assertFalse(cf.remove_option('Foo Bar', 'foo'), |
|
51776
d06a3ebf306f
Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47201
diff
changeset
|
252 "remove_option() failed to report non-existence of option" |
| 25910 | 253 " that was removed") |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
254 self.assertTrue(cf.has_option('Foo Bar', 'this_value')) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
255 self.assertFalse(cf.remove_option('Foo Bar', 'this_value')) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
256 self.assertTrue(cf.remove_option(self.default_section, 'this_value')) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
257 self.assertFalse(cf.has_option('Foo Bar', 'this_value')) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
258 self.assertFalse(cf.remove_option(self.default_section, 'this_value')) |
| 25910 | 259 |
|
63216
9ecbdefc9849
Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
58949
diff
changeset
|
260 with self.assertRaises(configparser.NoSectionError) as cm: |
|
9ecbdefc9849
Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
58949
diff
changeset
|
261 cf.remove_option('No Such Section', 'foo') |
|
9ecbdefc9849
Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
58949
diff
changeset
|
262 self.assertEqual(cm.exception.args, ('No Such Section',)) |
| 25910 | 263 |
| 264 eq(cf.get('Long Line', 'foo'), | |
| 265 '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
|
266 |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
267 # mapping access |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
268 del cf['Types'] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
269 self.assertFalse('Types' in cf) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
270 with self.assertRaises(KeyError): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
271 del cf['Types'] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
272 with self.assertRaises(ValueError): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
273 del cf[self.default_section] |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
274 del cf['Spacey Bar']['foo'] |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
275 self.assertFalse('foo' in cf['Spacey Bar']) |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
276 with self.assertRaises(KeyError): |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
277 del cf['Spacey Bar']['foo'] |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
278 self.assertTrue('that_value' in cf['Spacey Bar']) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
279 with self.assertRaises(KeyError): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
280 del cf['Spacey Bar']['that_value'] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
281 del cf[self.default_section]['that_value'] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
282 self.assertFalse('that_value' in cf['Spacey Bar']) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
283 with self.assertRaises(KeyError): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
284 del cf[self.default_section]['that_value'] |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
285 with self.assertRaises(KeyError): |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
286 del cf['No Such Section']['foo'] |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
287 |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
288 # Don't add new asserts below in this method as most of the options |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
289 # and sections are now removed. |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
290 |
| 63875 | 291 def test_basic(self): |
| 292 config_string = """\ | |
| 293 [Foo Bar] | |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
294 foo{0[0]}bar1 |
| 63875 | 295 [Spacey Bar] |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
296 foo {0[0]} bar2 |
| 63875 | 297 [Spacey Bar From The Beginning] |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
298 foo {0[0]} bar3 |
| 63875 | 299 baz {0[0]} qwe |
| 300 [Commented Bar] | |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
301 foo{0[1]} bar4 {1[1]} comment |
| 63875 | 302 baz{0[0]}qwe {1[0]}another one |
| 303 [Long Line] | |
| 304 foo{0[1]} this line is much, much longer than my editor | |
| 305 likes it. | |
| 306 [Section\\with$weird%characters[\t] | |
| 307 [Internationalized Stuff] | |
| 308 foo[bg]{0[1]} Bulgarian | |
| 309 foo{0[0]}Default | |
| 310 foo[en]{0[0]}English | |
| 311 foo[de]{0[0]}Deutsch | |
| 312 [Spaces] | |
| 313 key with spaces {0[1]} value | |
| 314 another with spaces {0[0]} splat! | |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
315 [Types] |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
316 int {0[1]} 42 |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
317 float {0[0]} 0.44 |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
318 boolean {0[0]} NO |
|
66702
ddc6c47bf825
configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents:
66647
diff
changeset
|
319 123 {0[1]} strange but acceptable |
| 63875 | 320 """.format(self.delimiters, self.comment_prefixes) |
| 321 if self.allow_no_value: | |
| 322 config_string += ( | |
| 323 "[NoValue]\n" | |
| 324 "option-without-value\n" | |
| 325 ) | |
| 326 cf = self.fromstring(config_string) | |
| 327 self.basic_test(cf) | |
| 328 if self.strict: | |
| 329 with self.assertRaises(configparser.DuplicateOptionError): | |
| 330 cf.read_string(textwrap.dedent("""\ | |
| 331 [Duplicate Options Here] | |
| 332 option {0[0]} with a value | |
| 333 option {0[1]} with another value | |
| 334 """.format(self.delimiters))) | |
| 335 with self.assertRaises(configparser.DuplicateSectionError): | |
| 336 cf.read_string(textwrap.dedent("""\ | |
| 337 [And Now For Something] | |
| 338 completely different {0[0]} True | |
| 339 [And Now For Something] | |
| 340 the larch {0[1]} 1 | |
| 341 """.format(self.delimiters))) | |
| 342 else: | |
| 343 cf.read_string(textwrap.dedent("""\ | |
| 344 [Duplicate Options Here] | |
| 345 option {0[0]} with a value | |
| 346 option {0[1]} with another value | |
| 347 """.format(self.delimiters))) | |
| 348 | |
| 349 cf.read_string(textwrap.dedent("""\ | |
| 350 [And Now For Something] | |
| 351 completely different {0[0]} True | |
| 352 [And Now For Something] | |
| 353 the larch {0[1]} 1 | |
| 354 """.format(self.delimiters))) | |
| 355 | |
| 356 def test_basic_from_dict(self): | |
| 357 config = { | |
| 358 "Foo Bar": { | |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
359 "foo": "bar1", |
| 63875 | 360 }, |
| 361 "Spacey Bar": { | |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
362 "foo": "bar2", |
| 63875 | 363 }, |
| 364 "Spacey Bar From The Beginning": { | |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
365 "foo": "bar3", |
| 63875 | 366 "baz": "qwe", |
| 367 }, | |
| 368 "Commented Bar": { | |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
369 "foo": "bar4", |
| 63875 | 370 "baz": "qwe", |
| 371 }, | |
| 372 "Long Line": { | |
| 373 "foo": "this line is much, much longer than my editor\nlikes " | |
| 374 "it.", | |
| 375 }, | |
| 376 "Section\\with$weird%characters[\t": { | |
| 377 }, | |
| 378 "Internationalized Stuff": { | |
| 379 "foo[bg]": "Bulgarian", | |
| 380 "foo": "Default", | |
| 381 "foo[en]": "English", | |
| 382 "foo[de]": "Deutsch", | |
| 383 }, | |
| 384 "Spaces": { | |
| 385 "key with spaces": "value", | |
| 386 "another with spaces": "splat!", | |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
387 }, |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
388 "Types": { |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
389 "int": 42, |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
390 "float": 0.44, |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
391 "boolean": False, |
|
66702
ddc6c47bf825
configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents:
66647
diff
changeset
|
392 123: "strange but acceptable", |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
393 }, |
| 63875 | 394 } |
| 395 if self.allow_no_value: | |
| 396 config.update({ | |
| 397 "NoValue": { | |
| 398 "option-without-value": None, | |
| 399 } | |
| 400 }) | |
| 401 cf = self.newconfig() | |
| 402 cf.read_dict(config) | |
| 403 self.basic_test(cf) | |
| 404 if self.strict: | |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
405 with self.assertRaises(configparser.DuplicateSectionError): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
406 cf.read_dict({ |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
407 '1': {'key': 'value'}, |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
408 1: {'key2': 'value2'}, |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
409 }) |
| 63875 | 410 with self.assertRaises(configparser.DuplicateOptionError): |
| 411 cf.read_dict({ | |
| 412 "Duplicate Options Here": { | |
| 413 'option': 'with a value', | |
| 414 'OPTION': 'with another value', | |
| 415 }, | |
| 416 }) | |
| 417 else: | |
| 418 cf.read_dict({ | |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
419 'section': {'key': 'value'}, |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
420 'SECTION': {'key2': 'value2'}, |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
421 }) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
422 cf.read_dict({ |
| 63875 | 423 "Duplicate Options Here": { |
| 424 'option': 'with a value', | |
| 425 'OPTION': 'with another value', | |
| 426 }, | |
| 427 }) | |
| 428 | |
| 25910 | 429 def test_case_sensitivity(self): |
| 430 cf = self.newconfig() | |
| 431 cf.add_section("A") | |
| 432 cf.add_section("a") | |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
433 cf.add_section("B") |
| 25910 | 434 L = cf.sections() |
| 435 L.sort() | |
| 436 eq = self.assertEqual | |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
437 eq(L, ["A", "B", "a"]) |
| 25910 | 438 cf.set("a", "B", "value") |
| 439 eq(cf.options("a"), ["b"]) | |
| 440 eq(cf.get("a", "b"), "value", | |
| 441 "could not locate option, expecting case-insensitive option names") | |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
442 with self.assertRaises(configparser.NoSectionError): |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
443 # section names are case-sensitive |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
444 cf.set("b", "A", "value") |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53524
diff
changeset
|
445 self.assertTrue(cf.has_option("a", "b")) |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
446 self.assertFalse(cf.has_option("b", "b")) |
| 25910 | 447 cf.set("A", "A-B", "A-B value") |
| 448 for opt in ("a-b", "A-b", "a-B", "A-B"): | |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53524
diff
changeset
|
449 self.assertTrue( |
| 25910 | 450 cf.has_option("A", opt), |
| 451 "has_option() returned false for option which should exist") | |
| 452 eq(cf.options("A"), ["a-b"]) | |
| 453 eq(cf.options("a"), ["b"]) | |
| 454 cf.remove_option("a", "B") | |
| 455 eq(cf.options("a"), []) | |
|
14900
f932c128279b
Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
456 |
| 25910 | 457 # SF bug #432369: |
| 458 cf = self.fromstring( | |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
459 "[MySection]\nOption{} first line \n\tsecond line \n".format( |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
460 self.delimiters[0])) |
| 25910 | 461 eq(cf.options("MySection"), ["option"]) |
| 462 eq(cf.get("MySection", "Option"), "first line\nsecond line") | |
| 463 | |
| 464 # SF bug #561822: | |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
465 cf = self.fromstring("[section]\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
466 "nekey{}nevalue\n".format(self.delimiters[0]), |
| 25910 | 467 defaults={"key":"value"}) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53524
diff
changeset
|
468 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
|
469 |
|
16576
f654468b9ae8
Added options that use square brackets in their names; this ensures that
Fred Drake <fdrake@acm.org>
parents:
15480
diff
changeset
|
470 |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
471 def test_case_sensitivity_mapping_access(self): |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
472 cf = self.newconfig() |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
473 cf["A"] = {} |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
474 cf["a"] = {"B": "value"} |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
475 cf["B"] = {} |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
476 L = [section for section in cf] |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
477 L.sort() |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
478 eq = self.assertEqual |
|
66530
93cd4de8ee0b
Use assertCountEqual instead of assertItemsEqual
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66350
diff
changeset
|
479 elem_eq = self.assertCountEqual |
|
66287
51e1cbdd8fd2
configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents:
66143
diff
changeset
|
480 eq(L, sorted(["A", "B", self.default_section, "a"])) |
|
66143
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
481 eq(cf["a"].keys(), {"b"}) |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
482 eq(cf["a"]["b"], "value", |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
483 "could not locate option, expecting case-insensitive option names") |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
484 with self.assertRaises(KeyError): |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
485 # section names are case-sensitive |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
486 cf["b"]["A"] = "value" |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
487 self.assertTrue("b" in cf["a"]) |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
488 cf["A"]["A-B"] = "A-B value" |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
489 for opt in ("a-b", "A-b", "a-B", "A-B"): |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
490 self.assertTrue( |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
491 opt in cf["A"], |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
492 "has_option() returned false for option which should exist") |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
493 eq(cf["A"].keys(), {"a-b"}) |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
494 eq(cf["a"].keys(), {"b"}) |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
495 del cf["a"]["B"] |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
496 elem_eq(cf["a"].keys(), {}) |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
497 |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
498 # SF bug #432369: |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
499 cf = self.fromstring( |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
500 "[MySection]\nOption{} first line \n\tsecond line \n".format( |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
501 self.delimiters[0])) |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
502 eq(cf["MySection"].keys(), {"option"}) |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
503 eq(cf["MySection"]["Option"], "first line\nsecond line") |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
504 |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
505 # SF bug #561822: |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
506 cf = self.fromstring("[section]\n" |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
507 "nekey{}nevalue\n".format(self.delimiters[0]), |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
508 defaults={"key":"value"}) |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
509 self.assertTrue("Key" in cf["section"]) |
|
e546f7605cf1
Issue #5412: extend configparser to support mapping access
Łukasz Langa <lukasz@langa.pl>
parents:
64898
diff
changeset
|
510 |
|
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
|
511 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
|
512 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
|
513 self.assertEqual( |
|
66287
51e1cbdd8fd2
configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents:
66143
diff
changeset
|
514 cf.get(self.default_section, "Foo"), "Bar", |
|
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
|
515 "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
|
516 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
|
517 self.assertEqual( |
|
66287
51e1cbdd8fd2
configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents:
66143
diff
changeset
|
518 cf.get(self.default_section, "Foo"), "Bar", |
|
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
|
519 "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
|
520 |
| 25910 | 521 def test_parse_errors(self): |
| 63875 | 522 cf = self.newconfig() |
| 523 self.parse_error(cf, configparser.ParsingError, | |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
524 "[Foo]\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
525 "{}val-without-opt-name\n".format(self.delimiters[0])) |
| 63875 | 526 self.parse_error(cf, configparser.ParsingError, |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
527 "[Foo]\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
528 "{}val-without-opt-name\n".format(self.delimiters[1])) |
| 63875 | 529 e = self.parse_error(cf, configparser.MissingSectionHeaderError, |
|
63220
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
530 "No Section!\n") |
|
63216
9ecbdefc9849
Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
58949
diff
changeset
|
531 self.assertEqual(e.args, ('<???>', 1, "No Section!\n")) |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
532 if not self.allow_no_value: |
| 63875 | 533 e = self.parse_error(cf, configparser.ParsingError, |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
534 "[Foo]\n wrong-indent\n") |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
535 self.assertEqual(e.args, ('<???>',)) |
|
64897
d5001d6c7736
Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
64424
diff
changeset
|
536 # read_file on a real file |
|
d5001d6c7736
Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
64424
diff
changeset
|
537 tricky = support.findfile("cfgparser.3") |
|
d5001d6c7736
Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
64424
diff
changeset
|
538 if self.delimiters[0] == '=': |
|
d5001d6c7736
Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
64424
diff
changeset
|
539 error = configparser.ParsingError |
|
d5001d6c7736
Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
64424
diff
changeset
|
540 expected = (tricky,) |
|
d5001d6c7736
Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
64424
diff
changeset
|
541 else: |
|
d5001d6c7736
Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
64424
diff
changeset
|
542 error = configparser.MissingSectionHeaderError |
|
d5001d6c7736
Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
64424
diff
changeset
|
543 expected = (tricky, 1, |
|
d5001d6c7736
Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
64424
diff
changeset
|
544 ' # INI with as many tricky parts as possible\n') |
|
64898
f703a643e6ce
Fix encoding for test case added in r84967.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
64897
diff
changeset
|
545 with open(tricky, encoding='utf-8') as f: |
|
64897
d5001d6c7736
Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
64424
diff
changeset
|
546 e = self.parse_error(cf, error, f) |
|
d5001d6c7736
Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
64424
diff
changeset
|
547 self.assertEqual(e.args, expected) |
|
14900
f932c128279b
Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
548 |
| 63875 | 549 def parse_error(self, cf, exc, src): |
|
64897
d5001d6c7736
Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
64424
diff
changeset
|
550 if hasattr(src, 'readline'): |
|
d5001d6c7736
Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
64424
diff
changeset
|
551 sio = src |
|
d5001d6c7736
Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
64424
diff
changeset
|
552 else: |
|
d5001d6c7736
Fix typo and add test case.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
64424
diff
changeset
|
553 sio = io.StringIO(src) |
|
63216
9ecbdefc9849
Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
58949
diff
changeset
|
554 with self.assertRaises(exc) as cm: |
| 63875 | 555 cf.read_file(sio) |
|
63216
9ecbdefc9849
Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
58949
diff
changeset
|
556 return cm.exception |
| 25910 | 557 |
| 558 def test_query_errors(self): | |
| 559 cf = self.newconfig() | |
| 560 self.assertEqual(cf.sections(), [], | |
| 561 "new ConfigParser should have no defined sections") | |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53524
diff
changeset
|
562 self.assertFalse(cf.has_section("Foo"), |
|
63220
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
563 "new ConfigParser should have no acknowledged " |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
564 "sections") |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
565 with self.assertRaises(configparser.NoSectionError): |
|
63216
9ecbdefc9849
Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
58949
diff
changeset
|
566 cf.options("Foo") |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
567 with self.assertRaises(configparser.NoSectionError): |
|
63216
9ecbdefc9849
Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
58949
diff
changeset
|
568 cf.set("foo", "bar", "value") |
| 63875 | 569 e = self.get_error(cf, configparser.NoSectionError, "foo", "bar") |
|
63216
9ecbdefc9849
Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
58949
diff
changeset
|
570 self.assertEqual(e.args, ("foo",)) |
| 25910 | 571 cf.add_section("foo") |
| 63875 | 572 e = self.get_error(cf, configparser.NoOptionError, "foo", "bar") |
|
63216
9ecbdefc9849
Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
58949
diff
changeset
|
573 self.assertEqual(e.args, ("bar", "foo")) |
| 25910 | 574 |
| 63875 | 575 def get_error(self, cf, exc, section, option): |
|
26928
24cb62225040
Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents:
25918
diff
changeset
|
576 try: |
| 63875 | 577 cf.get(section, option) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
40418
diff
changeset
|
578 except exc as e: |
|
26928
24cb62225040
Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents:
25918
diff
changeset
|
579 return e |
|
24cb62225040
Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents:
25918
diff
changeset
|
580 else: |
|
24cb62225040
Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents:
25918
diff
changeset
|
581 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
|
582 % (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
|
583 |
| 25910 | 584 def test_boolean(self): |
| 585 cf = self.fromstring( | |
| 586 "[BOOLTEST]\n" | |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
587 "T1{equals}1\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
588 "T2{equals}TRUE\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
589 "T3{equals}True\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
590 "T4{equals}oN\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
591 "T5{equals}yes\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
592 "F1{equals}0\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
593 "F2{equals}FALSE\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
594 "F3{equals}False\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
595 "F4{equals}oFF\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
596 "F5{equals}nO\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
597 "E1{equals}2\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
598 "E2{equals}foo\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
599 "E3{equals}-1\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
600 "E4{equals}0.1\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
601 "E5{equals}FALSE AND MORE".format(equals=self.delimiters[0]) |
| 25910 | 602 ) |
| 603 for x in range(1, 5): | |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53524
diff
changeset
|
604 self.assertTrue(cf.getboolean('BOOLTEST', 't%d' % x)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53524
diff
changeset
|
605 self.assertFalse(cf.getboolean('BOOLTEST', 'f%d' % x)) |
| 25910 | 606 self.assertRaises(ValueError, |
| 607 cf.getboolean, 'BOOLTEST', 'e%d' % x) | |
|
14900
f932c128279b
Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
608 |
| 25910 | 609 def test_weird_errors(self): |
| 610 cf = self.newconfig() | |
| 611 cf.add_section("Foo") | |
|
63216
9ecbdefc9849
Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
58949
diff
changeset
|
612 with self.assertRaises(configparser.DuplicateSectionError) as cm: |
|
9ecbdefc9849
Issue #4686 - add .args to exceptions in the configparsermodule
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
58949
diff
changeset
|
613 cf.add_section("Foo") |
| 63875 | 614 e = cm.exception |
| 615 self.assertEqual(str(e), "Section 'Foo' already exists") | |
| 616 self.assertEqual(e.args, ("Foo", None, None)) | |
| 617 | |
| 618 if self.strict: | |
| 619 with self.assertRaises(configparser.DuplicateSectionError) as cm: | |
| 620 cf.read_string(textwrap.dedent("""\ | |
| 621 [Foo] | |
| 622 will this be added{equals}True | |
| 623 [Bar] | |
| 624 what about this{equals}True | |
| 625 [Foo] | |
| 626 oops{equals}this won't | |
| 627 """.format(equals=self.delimiters[0])), source='<foo-bar>') | |
| 628 e = cm.exception | |
|
84279
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
629 self.assertEqual(str(e), "While reading from '<foo-bar>' " |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
630 "[line 5]: section 'Foo' already exists") |
| 63875 | 631 self.assertEqual(e.args, ("Foo", '<foo-bar>', 5)) |
| 632 | |
| 633 with self.assertRaises(configparser.DuplicateOptionError) as cm: | |
| 634 cf.read_dict({'Bar': {'opt': 'val', 'OPT': 'is really `opt`'}}) | |
| 635 e = cm.exception | |
|
84279
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
636 self.assertEqual(str(e), "While reading from '<dict>': option " |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
637 "'opt' in section 'Bar' already exists") |
| 63875 | 638 self.assertEqual(e.args, ("Bar", "opt", "<dict>", None)) |
|
16576
f654468b9ae8
Added options that use square brackets in their names; this ensures that
Fred Drake <fdrake@acm.org>
parents:
15480
diff
changeset
|
639 |
| 25910 | 640 def test_write(self): |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
641 config_string = ( |
| 25910 | 642 "[Long Line]\n" |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
643 "foo{0[0]} this line is much, much longer than my editor\n" |
| 25910 | 644 " likes it.\n" |
|
66287
51e1cbdd8fd2
configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents:
66143
diff
changeset
|
645 "[{default_section}]\n" |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
646 "foo{0[1]} another very\n" |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
647 " long line\n" |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
648 "[Long Line - With Comments!]\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
649 "test {0[1]} we {comment} can\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
650 " also {comment} place\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
651 " comments {comment} in\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
652 " multiline {comment} values" |
|
66287
51e1cbdd8fd2
configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents:
66143
diff
changeset
|
653 "\n".format(self.delimiters, comment=self.comment_prefixes[0], |
|
51e1cbdd8fd2
configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents:
66143
diff
changeset
|
654 default_section=self.default_section) |
| 25910 | 655 ) |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
656 if self.allow_no_value: |
|
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
657 config_string += ( |
|
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
658 "[Valueless]\n" |
|
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
659 "option-without-value\n" |
|
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
660 ) |
|
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
661 |
|
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
662 cf = self.fromstring(config_string) |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
663 for space_around_delimiters in (True, False): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
664 output = io.StringIO() |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
665 cf.write(output, space_around_delimiters=space_around_delimiters) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
666 delimiter = self.delimiters[0] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
667 if space_around_delimiters: |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
668 delimiter = " {} ".format(delimiter) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
669 expect_string = ( |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
670 "[{default_section}]\n" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
671 "foo{equals}another very\n" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
672 "\tlong line\n" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
673 "\n" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
674 "[Long Line]\n" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
675 "foo{equals}this line is much, much longer than my editor\n" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
676 "\tlikes it.\n" |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
677 "\n" |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
678 "[Long Line - With Comments!]\n" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
679 "test{equals}we\n" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
680 "\talso\n" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
681 "\tcomments\n" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
682 "\tmultiline\n" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
683 "\n".format(equals=delimiter, |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
684 default_section=self.default_section) |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
685 ) |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
686 if self.allow_no_value: |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
687 expect_string += ( |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
688 "[Valueless]\n" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
689 "option-without-value\n" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
690 "\n" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
691 ) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
692 self.assertEqual(output.getvalue(), expect_string) |
|
14900
f932c128279b
Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
693 |
| 32115 | 694 def test_set_string_types(self): |
| 695 cf = self.fromstring("[sect]\n" | |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
696 "option1{eq}foo\n".format(eq=self.delimiters[0])) |
| 32115 | 697 # Check that we don't get an exception when setting values in |
| 698 # an existing section using strings: | |
| 699 class mystr(str): | |
| 700 pass | |
| 701 cf.set("sect", "option1", "splat") | |
| 702 cf.set("sect", "option1", mystr("splat")) | |
| 703 cf.set("sect", "option2", "splat") | |
| 704 cf.set("sect", "option2", mystr("splat")) | |
|
42050
ca693f4fd05d
Simplify various spots where: str() is called on something
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
705 cf.set("sect", "option1", "splat") |
|
ca693f4fd05d
Simplify various spots where: str() is called on something
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
706 cf.set("sect", "option2", "splat") |
| 32115 | 707 |
| 32117 | 708 def test_read_returns_file_list(self): |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
709 if self.delimiters[0] != '=': |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
710 # skip reading the file if we're using an incompatible format |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
711 return |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
47012
diff
changeset
|
712 file1 = support.findfile("cfgparser.1") |
| 32117 | 713 # check when we pass a mix of readable and non-readable files: |
| 714 cf = self.newconfig() | |
|
51776
d06a3ebf306f
Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47201
diff
changeset
|
715 parsed_files = cf.read([file1, "nonexistent-file"]) |
| 32117 | 716 self.assertEqual(parsed_files, [file1]) |
| 717 self.assertEqual(cf.get("Foo Bar", "foo"), "newbar") | |
| 718 # check when we pass only a filename: | |
| 719 cf = self.newconfig() | |
| 720 parsed_files = cf.read(file1) | |
| 721 self.assertEqual(parsed_files, [file1]) | |
| 722 self.assertEqual(cf.get("Foo Bar", "foo"), "newbar") | |
| 723 # check when we pass only missing files: | |
| 724 cf = self.newconfig() | |
|
51776
d06a3ebf306f
Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
47201
diff
changeset
|
725 parsed_files = cf.read(["nonexistent-file"]) |
| 32117 | 726 self.assertEqual(parsed_files, []) |
| 727 # check when we pass no files: | |
| 728 cf = self.newconfig() | |
| 729 parsed_files = cf.read([]) | |
| 730 self.assertEqual(parsed_files, []) | |
| 731 | |
| 25910 | 732 # shared by subclasses |
| 733 def get_interpolation_config(self): | |
| 734 return self.fromstring( | |
| 735 "[Foo]\n" | |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
736 "bar{equals}something %(with1)s interpolation (1 step)\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
737 "bar9{equals}something %(with9)s lots of interpolation (9 steps)\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
738 "bar10{equals}something %(with10)s lots of interpolation (10 steps)\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
739 "bar11{equals}something %(with11)s lots of interpolation (11 steps)\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
740 "with11{equals}%(with10)s\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
741 "with10{equals}%(with9)s\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
742 "with9{equals}%(with8)s\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
743 "with8{equals}%(With7)s\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
744 "with7{equals}%(WITH6)s\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
745 "with6{equals}%(with5)s\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
746 "With5{equals}%(with4)s\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
747 "WITH4{equals}%(with3)s\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
748 "with3{equals}%(with2)s\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
749 "with2{equals}%(with1)s\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
750 "with1{equals}with\n" |
| 25910 | 751 "\n" |
| 752 "[Mutual Recursion]\n" | |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
753 "foo{equals}%(bar)s\n" |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
754 "bar{equals}%(foo)s\n" |
|
26928
24cb62225040
Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents:
25918
diff
changeset
|
755 "\n" |
|
24cb62225040
Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents:
25918
diff
changeset
|
756 "[Interpolation Error]\n" |
|
24cb62225040
Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents:
25918
diff
changeset
|
757 # no definition for 'reference' |
|
66349
c13f8980ca7f
Issue #10489: removed broken `__name__` support from configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66310
diff
changeset
|
758 "name{equals}%(reference)s\n".format(equals=self.delimiters[0])) |
|
16576
f654468b9ae8
Added options that use square brackets in their names; this ensures that
Fred Drake <fdrake@acm.org>
parents:
15480
diff
changeset
|
759 |
|
25917
5b6d55b45287
Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents:
25910
diff
changeset
|
760 def check_items_config(self, expected): |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
761 cf = self.fromstring(""" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
762 [section] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
763 name {0[0]} %(value)s |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
764 key{0[1]} |%(name)s| |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
765 getdefault{0[1]} |%(default)s| |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
766 """.format(self.delimiters), defaults={"default": "<default>"}) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
767 L = list(cf.items("section", vars={'value': 'value'})) |
|
25917
5b6d55b45287
Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents:
25910
diff
changeset
|
768 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
|
769 self.assertEqual(L, expected) |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
770 with self.assertRaises(configparser.NoSectionError): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
771 cf.items("no such section") |
|
25917
5b6d55b45287
Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents:
25910
diff
changeset
|
772 |
|
81164
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
773 def test_popitem(self): |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
774 cf = self.fromstring(""" |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
775 [section1] |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
776 name1 {0[0]} value1 |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
777 [section2] |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
778 name2 {0[0]} value2 |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
779 [section3] |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
780 name3 {0[0]} value3 |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
781 """.format(self.delimiters), defaults={"default": "<default>"}) |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
782 self.assertEqual(cf.popitem()[0], 'section1') |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
783 self.assertEqual(cf.popitem()[0], 'section2') |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
784 self.assertEqual(cf.popitem()[0], 'section3') |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
785 with self.assertRaises(KeyError): |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
786 cf.popitem() |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
787 |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
788 def test_clear(self): |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
789 cf = self.newconfig({"foo": "Bar"}) |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
790 self.assertEqual( |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
791 cf.get(self.default_section, "Foo"), "Bar", |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
792 "could not locate option, expecting case-insensitive option names") |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
793 cf['zing'] = {'option1': 'value1', 'option2': 'value2'} |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
794 self.assertEqual(cf.sections(), ['zing']) |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
795 self.assertEqual(set(cf['zing'].keys()), {'option1', 'option2', 'foo'}) |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
796 cf.clear() |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
797 self.assertEqual(set(cf.sections()), set()) |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
798 self.assertEqual(set(cf[self.default_section].keys()), {'foo'}) |
|
dc5adc08f1a8
Fixes `parser.clean()` reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
74542
diff
changeset
|
799 |
|
81170
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
800 def test_setitem(self): |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
801 cf = self.fromstring(""" |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
802 [section1] |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
803 name1 {0[0]} value1 |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
804 [section2] |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
805 name2 {0[0]} value2 |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
806 [section3] |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
807 name3 {0[0]} value3 |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
808 """.format(self.delimiters), defaults={"nameD": "valueD"}) |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
809 self.assertEqual(set(cf['section1'].keys()), {'name1', 'named'}) |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
810 self.assertEqual(set(cf['section2'].keys()), {'name2', 'named'}) |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
811 self.assertEqual(set(cf['section3'].keys()), {'name3', 'named'}) |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
812 self.assertEqual(cf['section1']['name1'], 'value1') |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
813 self.assertEqual(cf['section2']['name2'], 'value2') |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
814 self.assertEqual(cf['section3']['name3'], 'value3') |
|
81205
f580342b63d8
configparser: preserve section order when using `__setitem__` (issue #16820)
Łukasz Langa <lukasz@langa.pl>
parents:
81170
diff
changeset
|
815 self.assertEqual(cf.sections(), ['section1', 'section2', 'section3']) |
|
81170
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
816 cf['section2'] = {'name22': 'value22'} |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
817 self.assertEqual(set(cf['section2'].keys()), {'name22', 'named'}) |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
818 self.assertEqual(cf['section2']['name22'], 'value22') |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
819 self.assertNotIn('name2', cf['section2']) |
|
81205
f580342b63d8
configparser: preserve section order when using `__setitem__` (issue #16820)
Łukasz Langa <lukasz@langa.pl>
parents:
81170
diff
changeset
|
820 self.assertEqual(cf.sections(), ['section1', 'section2', 'section3']) |
|
81170
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
821 cf['section3'] = {} |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
822 self.assertEqual(set(cf['section3'].keys()), {'named'}) |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
823 self.assertNotIn('name3', cf['section3']) |
|
81205
f580342b63d8
configparser: preserve section order when using `__setitem__` (issue #16820)
Łukasz Langa <lukasz@langa.pl>
parents:
81170
diff
changeset
|
824 self.assertEqual(cf.sections(), ['section1', 'section2', 'section3']) |
|
81170
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
825 cf[self.default_section] = {} |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
826 self.assertEqual(set(cf[self.default_section].keys()), set()) |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
827 self.assertEqual(set(cf['section1'].keys()), {'name1'}) |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
828 self.assertEqual(set(cf['section2'].keys()), {'name22'}) |
|
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
829 self.assertEqual(set(cf['section3'].keys()), set()) |
|
81205
f580342b63d8
configparser: preserve section order when using `__setitem__` (issue #16820)
Łukasz Langa <lukasz@langa.pl>
parents:
81170
diff
changeset
|
830 self.assertEqual(cf.sections(), ['section1', 'section2', 'section3']) |
|
81170
459a23083b66
Fixes `__setitem__` on parser['DEFAULT'] reported in issue #16820.
Łukasz Langa <lukasz@langa.pl>
parents:
81166
diff
changeset
|
831 |
|
14900
f932c128279b
Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
832 |
|
81385
7dbdd7204d0a
#16925: test_configparser now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81211
diff
changeset
|
833 class StrictTestCase(BasicTestCase, unittest.TestCase): |
| 63875 | 834 config_class = configparser.RawConfigParser |
| 835 strict = True | |
| 836 | |
| 837 | |
|
81385
7dbdd7204d0a
#16925: test_configparser now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81211
diff
changeset
|
838 class ConfigParserTestCase(BasicTestCase, unittest.TestCase): |
|
47012
35a8899256c2
Renamed ConfigParser to configparser.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45446
diff
changeset
|
839 config_class = configparser.ConfigParser |
|
25909
0bac66e1b356
Added (very) minimal tests of the RawConfigParser class.
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
840 |
| 25910 | 841 def test_interpolation(self): |
| 842 cf = self.get_interpolation_config() | |
| 843 eq = self.assertEqual | |
| 844 eq(cf.get("Foo", "bar"), "something with interpolation (1 step)") | |
| 845 eq(cf.get("Foo", "bar9"), | |
| 846 "something with lots of interpolation (9 steps)") | |
| 847 eq(cf.get("Foo", "bar10"), | |
| 848 "something with lots of interpolation (10 steps)") | |
| 63875 | 849 e = self.get_error(cf, configparser.InterpolationDepthError, "Foo", "bar11") |
|
66953
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
850 if self.interpolation == configparser._UNSET: |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
851 self.assertEqual(e.args, ("bar11", "Foo", "%(with1)s")) |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
852 elif isinstance(self.interpolation, configparser.LegacyInterpolation): |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
853 self.assertEqual(e.args, ("bar11", "Foo", |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
854 "something %(with11)s lots of interpolation (11 steps)")) |
|
25909
0bac66e1b356
Added (very) minimal tests of the RawConfigParser class.
Fred Drake <fdrake@acm.org>
parents:
25462
diff
changeset
|
855 |
|
26928
24cb62225040
Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents:
25918
diff
changeset
|
856 def test_interpolation_missing_value(self): |
| 63875 | 857 cf = self.get_interpolation_config() |
| 858 e = self.get_error(cf, configparser.InterpolationMissingOptionError, | |
|
26928
24cb62225040
Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents:
25918
diff
changeset
|
859 "Interpolation Error", "name") |
|
24cb62225040
Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents:
25918
diff
changeset
|
860 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
|
861 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
|
862 self.assertEqual(e.option, "name") |
|
66953
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
863 if self.interpolation == configparser._UNSET: |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
864 self.assertEqual(e.args, ('name', 'Interpolation Error', |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
865 '', 'reference')) |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
866 elif isinstance(self.interpolation, configparser.LegacyInterpolation): |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
867 self.assertEqual(e.args, ('name', 'Interpolation Error', |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
868 '%(reference)s', 'reference')) |
|
26928
24cb62225040
Add a test that InterpolationError is constructed properly and raised
Fred Drake <fdrake@acm.org>
parents:
25918
diff
changeset
|
869 |
|
25917
5b6d55b45287
Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents:
25910
diff
changeset
|
870 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
|
871 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
|
872 ('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
|
873 ('key', '|value|'), |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
874 ('name', 'value'), |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
875 ('value', 'value')]) |
|
25917
5b6d55b45287
Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents:
25910
diff
changeset
|
876 |
|
66953
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
877 def test_safe_interpolation(self): |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
878 # See http://www.python.org/sf/511737 |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
879 cf = self.fromstring("[section]\n" |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
880 "option1{eq}xxx\n" |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
881 "option2{eq}%(option1)s/xxx\n" |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
882 "ok{eq}%(option1)s/%%s\n" |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
883 "not_ok{eq}%(option2)s/%%s".format( |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
884 eq=self.delimiters[0])) |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
885 self.assertEqual(cf.get("section", "ok"), "xxx/%s") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
886 if self.interpolation == configparser._UNSET: |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
887 self.assertEqual(cf.get("section", "not_ok"), "xxx/xxx/%s") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
888 elif isinstance(self.interpolation, configparser.LegacyInterpolation): |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
889 with self.assertRaises(TypeError): |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
890 cf.get("section", "not_ok") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
891 |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
892 def test_set_malformatted_interpolation(self): |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
893 cf = self.fromstring("[sect]\n" |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
894 "option1{eq}foo\n".format(eq=self.delimiters[0])) |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
895 |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
896 self.assertEqual(cf.get('sect', "option1"), "foo") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
897 |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
898 self.assertRaises(ValueError, cf.set, "sect", "option1", "%foo") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
899 self.assertRaises(ValueError, cf.set, "sect", "option1", "foo%") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
900 self.assertRaises(ValueError, cf.set, "sect", "option1", "f%oo") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
901 |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
902 self.assertEqual(cf.get('sect', "option1"), "foo") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
903 |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
904 # bug #5741: double percents are *not* malformed |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
905 cf.set("sect", "option2", "foo%%bar") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
906 self.assertEqual(cf.get("sect", "option2"), "foo%bar") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
907 |
|
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
|
908 def test_set_nonstring_types(self): |
|
66953
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
909 cf = self.fromstring("[sect]\n" |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
910 "option1{eq}foo\n".format(eq=self.delimiters[0])) |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
911 # Check that we get a TypeError when setting non-string values |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
912 # in an existing section: |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
913 self.assertRaises(TypeError, cf.set, "sect", "option1", 1) |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
914 self.assertRaises(TypeError, cf.set, "sect", "option1", 1.0) |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
915 self.assertRaises(TypeError, cf.set, "sect", "option1", object()) |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
916 self.assertRaises(TypeError, cf.set, "sect", "option2", 1) |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
917 self.assertRaises(TypeError, cf.set, "sect", "option2", 1.0) |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
918 self.assertRaises(TypeError, cf.set, "sect", "option2", object()) |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
919 self.assertRaises(TypeError, cf.set, "sect", 123, "invalid opt name!") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
920 self.assertRaises(TypeError, cf.add_section, 123) |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
921 |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
922 def test_add_section_default(self): |
|
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
|
923 cf = self.newconfig() |
|
66953
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
924 self.assertRaises(ValueError, cf.add_section, self.default_section) |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
925 |
|
69668
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
926 |
|
81385
7dbdd7204d0a
#16925: test_configparser now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81211
diff
changeset
|
927 class ConfigParserTestCaseNoInterpolation(BasicTestCase, unittest.TestCase): |
|
69668
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
928 config_class = configparser.ConfigParser |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
929 interpolation = None |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
930 ini = textwrap.dedent(""" |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
931 [numbers] |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
932 one = 1 |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
933 two = %(one)s * 2 |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
934 three = ${common:one} * 3 |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
935 |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
936 [hexen] |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
937 sixteen = ${numbers:two} * 8 |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
938 """).strip() |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
939 |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
940 def assertMatchesIni(self, cf): |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
941 self.assertEqual(cf['numbers']['one'], '1') |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
942 self.assertEqual(cf['numbers']['two'], '%(one)s * 2') |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
943 self.assertEqual(cf['numbers']['three'], '${common:one} * 3') |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
944 self.assertEqual(cf['hexen']['sixteen'], '${numbers:two} * 8') |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
945 |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
946 def test_no_interpolation(self): |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
947 cf = self.fromstring(self.ini) |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
948 self.assertMatchesIni(cf) |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
949 |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
950 def test_empty_case(self): |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
951 cf = self.newconfig() |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
952 self.assertIsNone(cf.read_string("")) |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
953 |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
954 def test_none_as_default_interpolation(self): |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
955 class CustomConfigParser(configparser.ConfigParser): |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
956 _DEFAULT_INTERPOLATION = None |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
957 |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
958 cf = CustomConfigParser() |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
959 cf.read_string(self.ini) |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
960 self.assertMatchesIni(cf) |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
961 |
|
32031e33d793
Closes #11324: ConfigParser(interpolation=None) doesn't work.
Łukasz Langa <lukasz@langa.pl>
parents:
69663
diff
changeset
|
962 |
|
66953
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
963 class ConfigParserTestCaseLegacyInterpolation(ConfigParserTestCase): |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
964 config_class = configparser.ConfigParser |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
965 interpolation = configparser.LegacyInterpolation() |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
966 |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
967 def test_set_malformatted_interpolation(self): |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
968 cf = self.fromstring("[sect]\n" |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
969 "option1{eq}foo\n".format(eq=self.delimiters[0])) |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
970 |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
971 self.assertEqual(cf.get('sect', "option1"), "foo") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
972 |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
973 cf.set("sect", "option1", "%foo") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
974 self.assertEqual(cf.get('sect', "option1"), "%foo") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
975 cf.set("sect", "option1", "foo%") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
976 self.assertEqual(cf.get('sect', "option1"), "foo%") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
977 cf.set("sect", "option1", "f%oo") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
978 self.assertEqual(cf.get('sect', "option1"), "f%oo") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
979 |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
980 # bug #5741: double percents are *not* malformed |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
981 cf.set("sect", "option2", "foo%%bar") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
982 self.assertEqual(cf.get("sect", "option2"), "foo%%bar") |
|
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
|
983 |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
984 class ConfigParserTestCaseNonStandardDelimiters(ConfigParserTestCase): |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
985 delimiters = (':=', '$') |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
986 comment_prefixes = ('//', '"') |
|
66981
074c21714a02
configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents:
66953
diff
changeset
|
987 inline_comment_prefixes = ('//', '"') |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
988 |
|
66287
51e1cbdd8fd2
configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents:
66143
diff
changeset
|
989 class ConfigParserTestCaseNonStandardDefaultSection(ConfigParserTestCase): |
|
51e1cbdd8fd2
configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents:
66143
diff
changeset
|
990 default_section = 'general' |
|
51e1cbdd8fd2
configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents:
66143
diff
changeset
|
991 |
|
81385
7dbdd7204d0a
#16925: test_configparser now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81211
diff
changeset
|
992 class MultilineValuesTestCase(BasicTestCase, unittest.TestCase): |
|
63220
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
993 config_class = configparser.ConfigParser |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
994 wonderful_spam = ("I'm having spam spam spam spam " |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
995 "spam spam spam beaked beans spam " |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
996 "spam spam and spam!").replace(' ', '\t\n') |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
997 |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
998 def setUp(self): |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
999 cf = self.newconfig() |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1000 for i in range(100): |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1001 s = 'section{}'.format(i) |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1002 cf.add_section(s) |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1003 for j in range(10): |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1004 cf.set(s, 'lovely_spam{}'.format(j), self.wonderful_spam) |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1005 with open(support.TESTFN, 'w') as f: |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1006 cf.write(f) |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1007 |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1008 def tearDown(self): |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1009 os.unlink(support.TESTFN) |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1010 |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1011 def test_dominating_multiline_values(self): |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1012 # We're reading from file because this is where the code changed |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1013 # during performance updates in Python 3.2 |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1014 cf_from_file = self.newconfig() |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1015 with open(support.TESTFN) as f: |
| 63875 | 1016 cf_from_file.read_file(f) |
|
63220
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1017 self.assertEqual(cf_from_file.get('section8', 'lovely_spam4'), |
|
fdb71d6c1822
Fix #7113. Patch by Łukasz Langa.
Brian Curtin <brian.curtin@gmail.com>
parents:
63216
diff
changeset
|
1018 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
|
1019 |
|
81385
7dbdd7204d0a
#16925: test_configparser now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81211
diff
changeset
|
1020 class RawConfigParserTestCase(BasicTestCase, unittest.TestCase): |
|
47012
35a8899256c2
Renamed ConfigParser to configparser.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45446
diff
changeset
|
1021 config_class = configparser.RawConfigParser |
|
14900
f932c128279b
Regression test for ConfigParser module.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
1022 |
| 25910 | 1023 def test_interpolation(self): |
| 1024 cf = self.get_interpolation_config() | |
| 1025 eq = self.assertEqual | |
| 1026 eq(cf.get("Foo", "bar"), | |
| 1027 "something %(with1)s interpolation (1 step)") | |
| 1028 eq(cf.get("Foo", "bar9"), | |
| 1029 "something %(with9)s lots of interpolation (9 steps)") | |
| 1030 eq(cf.get("Foo", "bar10"), | |
| 1031 "something %(with10)s lots of interpolation (10 steps)") | |
| 1032 eq(cf.get("Foo", "bar11"), | |
| 1033 "something %(with11)s lots of interpolation (11 steps)") | |
| 1034 | |
|
25917
5b6d55b45287
Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents:
25910
diff
changeset
|
1035 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
|
1036 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
|
1037 ('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
|
1038 ('key', '|%(name)s|'), |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1039 ('name', '%(value)s'), |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1040 ('value', 'value')]) |
|
25917
5b6d55b45287
Add tests for both raw and non-raw versions of the items() methods.
Fred Drake <fdrake@acm.org>
parents:
25910
diff
changeset
|
1041 |
|
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
|
1042 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
|
1043 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
|
1044 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
|
1045 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
|
1046 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
|
1047 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
|
1048 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
|
1049 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
|
1050 [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
|
1051 self.assertEqual(cf.get('non-string', 'dict'), {'pi': 3.14159}) |
|
66702
ddc6c47bf825
configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents:
66647
diff
changeset
|
1052 cf.add_section(123) |
|
ddc6c47bf825
configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents:
66647
diff
changeset
|
1053 cf.set(123, 'this is sick', True) |
|
ddc6c47bf825
configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents:
66647
diff
changeset
|
1054 self.assertEqual(cf.get(123, 'this is sick'), True) |
|
69686
b7309b24528d
__class__ of a __class__ check worked only by chance.
Łukasz Langa <lukasz@langa.pl>
parents:
69668
diff
changeset
|
1055 if cf._dict is configparser._default_dict: |
|
66702
ddc6c47bf825
configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents:
66647
diff
changeset
|
1056 # would not work for SortedDict; only checking for the most common |
|
ddc6c47bf825
configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents:
66647
diff
changeset
|
1057 # default dictionary (OrderedDict) |
|
ddc6c47bf825
configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents:
66647
diff
changeset
|
1058 cf.optionxform = lambda x: x |
|
ddc6c47bf825
configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents:
66647
diff
changeset
|
1059 cf.set('non-string', 1, 1) |
|
ddc6c47bf825
configparser: fixed inconsistency where in SafeConfigParser option values
Łukasz Langa <lukasz@langa.pl>
parents:
66647
diff
changeset
|
1060 self.assertEqual(cf.get('non-string', 1), 1) |
|
33665
5def66f2c9ab
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33663
diff
changeset
|
1061 |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1062 class RawConfigParserTestCaseNonStandardDelimiters(RawConfigParserTestCase): |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1063 delimiters = (':=', '$') |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1064 comment_prefixes = ('//', '"') |
|
66981
074c21714a02
configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents:
66953
diff
changeset
|
1065 inline_comment_prefixes = ('//', '"') |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1066 |
|
81385
7dbdd7204d0a
#16925: test_configparser now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81211
diff
changeset
|
1067 class RawConfigParserTestSambaConf(CfgParserTestCaseClass, unittest.TestCase): |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1068 config_class = configparser.RawConfigParser |
|
66981
074c21714a02
configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents:
66953
diff
changeset
|
1069 comment_prefixes = ('#', ';', '----') |
|
074c21714a02
configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents:
66953
diff
changeset
|
1070 inline_comment_prefixes = ('//',) |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1071 empty_lines_in_values = False |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1072 |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1073 def test_reading(self): |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1074 smbconf = support.findfile("cfgparser.2") |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1075 # check when we pass a mix of readable and non-readable files: |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1076 cf = self.newconfig() |
|
63281
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1077 parsed_files = cf.read([smbconf, "nonexistent-file"], encoding='utf-8') |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1078 self.assertEqual(parsed_files, [smbconf]) |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1079 sections = ['global', 'homes', 'printers', |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1080 'print$', 'pdf-generator', 'tmp', 'Agustin'] |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1081 self.assertEqual(cf.sections(), sections) |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1082 self.assertEqual(cf.get("global", "workgroup"), "MDKGROUP") |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1083 self.assertEqual(cf.getint("global", "max log size"), 50) |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1084 self.assertEqual(cf.get("global", "hosts allow"), "127.") |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1085 self.assertEqual(cf.get("tmp", "echo command"), "cat %s; rm %s") |
| 25910 | 1086 |
|
81385
7dbdd7204d0a
#16925: test_configparser now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81211
diff
changeset
|
1087 class ConfigParserTestCaseExtendedInterpolation(BasicTestCase, unittest.TestCase): |
|
66953
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
1088 config_class = configparser.ConfigParser |
|
66647
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1089 interpolation = configparser.ExtendedInterpolation() |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1090 default_section = 'common' |
|
69657
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1091 strict = True |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1092 |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1093 def fromstring(self, string, defaults=None, optionxform=None): |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1094 cf = self.newconfig(defaults) |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1095 if optionxform: |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1096 cf.optionxform = optionxform |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1097 cf.read_string(string) |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1098 return cf |
|
66647
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1099 |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1100 def test_extended_interpolation(self): |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1101 cf = self.fromstring(textwrap.dedent(""" |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1102 [common] |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1103 favourite Beatle = Paul |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1104 favourite color = green |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1105 |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1106 [tom] |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1107 favourite band = ${favourite color} day |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1108 favourite pope = John ${favourite Beatle} II |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1109 sequel = ${favourite pope}I |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1110 |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1111 [ambv] |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1112 favourite Beatle = George |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1113 son of Edward VII = ${favourite Beatle} V |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1114 son of George V = ${son of Edward VII}I |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1115 |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1116 [stanley] |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1117 favourite Beatle = ${ambv:favourite Beatle} |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1118 favourite pope = ${tom:favourite pope} |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1119 favourite color = black |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1120 favourite state of mind = paranoid |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1121 favourite movie = soylent ${common:favourite color} |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1122 favourite song = ${favourite color} sabbath - ${favourite state of mind} |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1123 """).strip()) |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1124 |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1125 eq = self.assertEqual |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1126 eq(cf['common']['favourite Beatle'], 'Paul') |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1127 eq(cf['common']['favourite color'], 'green') |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1128 eq(cf['tom']['favourite Beatle'], 'Paul') |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1129 eq(cf['tom']['favourite color'], 'green') |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1130 eq(cf['tom']['favourite band'], 'green day') |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1131 eq(cf['tom']['favourite pope'], 'John Paul II') |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1132 eq(cf['tom']['sequel'], 'John Paul III') |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1133 eq(cf['ambv']['favourite Beatle'], 'George') |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1134 eq(cf['ambv']['favourite color'], 'green') |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1135 eq(cf['ambv']['son of Edward VII'], 'George V') |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1136 eq(cf['ambv']['son of George V'], 'George VI') |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1137 eq(cf['stanley']['favourite Beatle'], 'George') |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1138 eq(cf['stanley']['favourite color'], 'black') |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1139 eq(cf['stanley']['favourite state of mind'], 'paranoid') |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1140 eq(cf['stanley']['favourite movie'], 'soylent green') |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1141 eq(cf['stanley']['favourite pope'], 'John Paul II') |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1142 eq(cf['stanley']['favourite song'], |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1143 'black sabbath - paranoid') |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1144 |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1145 def test_endless_loop(self): |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1146 cf = self.fromstring(textwrap.dedent(""" |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1147 [one for you] |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1148 ping = ${one for me:pong} |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1149 |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1150 [one for me] |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1151 pong = ${one for you:ping} |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1152 |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1153 [selfish] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1154 me = ${me} |
|
66647
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1155 """).strip()) |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1156 |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1157 with self.assertRaises(configparser.InterpolationDepthError): |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1158 cf['one for you']['ping'] |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1159 with self.assertRaises(configparser.InterpolationDepthError): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1160 cf['selfish']['me'] |
|
66647
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1161 |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1162 def test_strange_options(self): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1163 cf = self.fromstring(""" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1164 [dollars] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1165 $var = $$value |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1166 $var2 = ${$var} |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1167 ${sick} = cannot interpolate me |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1168 |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1169 [interpolated] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1170 $other = ${dollars:$var} |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1171 $trying = ${dollars:${sick}} |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1172 """) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1173 |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1174 self.assertEqual(cf['dollars']['$var'], '$value') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1175 self.assertEqual(cf['interpolated']['$other'], '$value') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1176 self.assertEqual(cf['dollars']['${sick}'], 'cannot interpolate me') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1177 exception_class = configparser.InterpolationMissingOptionError |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1178 with self.assertRaises(exception_class) as cm: |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1179 cf['interpolated']['$trying'] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1180 self.assertEqual(cm.exception.reference, 'dollars:${sick') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1181 self.assertEqual(cm.exception.args[2], '}') #rawval |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1182 |
|
69657
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1183 def test_case_sensitivity_basic(self): |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1184 ini = textwrap.dedent(""" |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1185 [common] |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1186 optionlower = value |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1187 OptionUpper = Value |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1188 |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1189 [Common] |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1190 optionlower = a better ${common:optionlower} |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1191 OptionUpper = A Better ${common:OptionUpper} |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1192 |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1193 [random] |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1194 foolower = ${common:optionlower} redefined |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1195 FooUpper = ${Common:OptionUpper} Redefined |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1196 """).strip() |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1197 |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1198 cf = self.fromstring(ini) |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1199 eq = self.assertEqual |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1200 eq(cf['common']['optionlower'], 'value') |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1201 eq(cf['common']['OptionUpper'], 'Value') |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1202 eq(cf['Common']['optionlower'], 'a better value') |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1203 eq(cf['Common']['OptionUpper'], 'A Better Value') |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1204 eq(cf['random']['foolower'], 'value redefined') |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1205 eq(cf['random']['FooUpper'], 'A Better Value Redefined') |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1206 |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1207 def test_case_sensitivity_conflicts(self): |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1208 ini = textwrap.dedent(""" |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1209 [common] |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1210 option = value |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1211 Option = Value |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1212 |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1213 [Common] |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1214 option = a better ${common:option} |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1215 Option = A Better ${common:Option} |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1216 |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1217 [random] |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1218 foo = ${common:option} redefined |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1219 Foo = ${Common:Option} Redefined |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1220 """).strip() |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1221 with self.assertRaises(configparser.DuplicateOptionError): |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1222 cf = self.fromstring(ini) |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1223 |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1224 # raw options |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1225 cf = self.fromstring(ini, optionxform=lambda opt: opt) |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1226 eq = self.assertEqual |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1227 eq(cf['common']['option'], 'value') |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1228 eq(cf['common']['Option'], 'Value') |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1229 eq(cf['Common']['option'], 'a better value') |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1230 eq(cf['Common']['Option'], 'A Better Value') |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1231 eq(cf['random']['foo'], 'value redefined') |
|
57c076ab4bbd
Closes #11858: configparser.ExtendedInterpolation and section case.
Łukasz Langa <lukasz@langa.pl>
parents:
69631
diff
changeset
|
1232 eq(cf['random']['Foo'], 'A Better Value Redefined') |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1233 |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1234 def test_other_errors(self): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1235 cf = self.fromstring(""" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1236 [interpolation fail] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1237 case1 = ${where's the brace |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1238 case2 = ${does_not_exist} |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1239 case3 = ${wrong_section:wrong_value} |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1240 case4 = ${i:like:colon:characters} |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1241 case5 = $100 for Fail No 5! |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1242 """) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1243 |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1244 with self.assertRaises(configparser.InterpolationSyntaxError): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1245 cf['interpolation fail']['case1'] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1246 with self.assertRaises(configparser.InterpolationMissingOptionError): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1247 cf['interpolation fail']['case2'] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1248 with self.assertRaises(configparser.InterpolationMissingOptionError): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1249 cf['interpolation fail']['case3'] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1250 with self.assertRaises(configparser.InterpolationSyntaxError): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1251 cf['interpolation fail']['case4'] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1252 with self.assertRaises(configparser.InterpolationSyntaxError): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1253 cf['interpolation fail']['case5'] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1254 with self.assertRaises(ValueError): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1255 cf['interpolation fail']['case6'] = "BLACK $ABBATH" |
|
66647
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1256 |
|
5fda519834df
Issue 10499: Modular interpolation in configparser
Łukasz Langa <lukasz@langa.pl>
parents:
66530
diff
changeset
|
1257 |
|
66953
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
1258 class ConfigParserTestCaseNoValue(ConfigParserTestCase): |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
1259 allow_no_value = True |
|
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
1260 |
|
81385
7dbdd7204d0a
#16925: test_configparser now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81211
diff
changeset
|
1261 class ConfigParserTestCaseTrickyFile(CfgParserTestCaseClass, unittest.TestCase): |
|
66953
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
1262 config_class = configparser.ConfigParser |
|
63281
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1263 delimiters = {'='} |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1264 comment_prefixes = {'#'} |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1265 allow_no_value = True |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1266 |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1267 def test_cfgparser_dot_3(self): |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1268 tricky = support.findfile("cfgparser.3") |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1269 cf = self.newconfig() |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1270 self.assertEqual(len(cf.read(tricky, encoding='utf-8')), 1) |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1271 self.assertEqual(cf.sections(), ['strange', |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1272 'corruption', |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1273 'yeah, sections can be ' |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1274 'indented as well', |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1275 'another one!', |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1276 'no values here', |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1277 'tricky interpolation', |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1278 'more interpolation']) |
|
66287
51e1cbdd8fd2
configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents:
66143
diff
changeset
|
1279 self.assertEqual(cf.getint(self.default_section, 'go', |
|
64424
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
1280 vars={'interpolate': '-1'}), -1) |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
1281 with self.assertRaises(ValueError): |
|
b2b262c5daa8
add consistent support for the vars and default arguments on all
Fred Drake <fdrake@acm.org>
parents:
64386
diff
changeset
|
1282 # no interpolation will happen |
|
66287
51e1cbdd8fd2
configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents:
66143
diff
changeset
|
1283 cf.getint(self.default_section, 'go', raw=True, |
|
51e1cbdd8fd2
configparser: the name of the DEFAULT section is now customizable
Łukasz Langa <lukasz@langa.pl>
parents:
66143
diff
changeset
|
1284 vars={'interpolate': '-1'}) |
|
63281
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1285 self.assertEqual(len(cf.get('strange', 'other').split('\n')), 4) |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1286 self.assertEqual(len(cf.get('corruption', 'value').split('\n')), 10) |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1287 longname = 'yeah, sections can be indented as well' |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1288 self.assertFalse(cf.getboolean(longname, 'are they subsections')) |
|
66310
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66287
diff
changeset
|
1289 self.assertEqual(cf.get(longname, 'lets use some Unicode'), '片仮名') |
|
63281
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1290 self.assertEqual(len(cf.items('another one!')), 5) # 4 in section and |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1291 # `go` from DEFAULT |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1292 with self.assertRaises(configparser.InterpolationMissingOptionError): |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1293 cf.items('no values here') |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1294 self.assertEqual(cf.get('tricky interpolation', 'lets'), 'do this') |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1295 self.assertEqual(cf.get('tricky interpolation', 'lets'), |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1296 cf.get('tricky interpolation', 'go')) |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1297 self.assertEqual(cf.get('more interpolation', 'lets'), 'go shopping') |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1298 |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1299 def test_unicode_failure(self): |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1300 tricky = support.findfile("cfgparser.3") |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1301 cf = self.newconfig() |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1302 with self.assertRaises(UnicodeDecodeError): |
|
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1303 cf.read(tricky, encoding='ascii') |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
1304 |
|
64386
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1305 |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1306 class Issue7005TestCase(unittest.TestCase): |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1307 """Test output when None is set() as a value and allow_no_value == False. |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1308 |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1309 http://bugs.python.org/issue7005 |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1310 |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1311 """ |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1312 |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1313 expected_output = "[section]\noption = None\n\n" |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1314 |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1315 def prepare(self, config_class): |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1316 # This is the default, but that's the point. |
|
66953
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
1317 cp = config_class(allow_no_value=False) |
|
64386
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1318 cp.add_section("section") |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1319 cp.set("section", "option", None) |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1320 sio = io.StringIO() |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1321 cp.write(sio) |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1322 return sio.getvalue() |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1323 |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1324 def test_none_as_value_stringified(self): |
|
66953
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
1325 cp = configparser.ConfigParser(allow_no_value=False) |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
1326 cp.add_section("section") |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
1327 with self.assertRaises(TypeError): |
|
3de6cd902624
Broken ConfigParser removed, SafeConfigParser renamed to ConfigParser.
Łukasz Langa <lukasz@langa.pl>
parents:
66722
diff
changeset
|
1328 cp.set("section", "option", None) |
|
64386
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1329 |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1330 def test_none_as_value_stringified_raw(self): |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1331 output = self.prepare(configparser.RawConfigParser) |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1332 self.assertEqual(output, self.expected_output) |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1333 |
|
16d8a4e94dd0
fix output from RawConfigParser.write and ConfigParser.write for None
Fred Drake <fdrake@acm.org>
parents:
63875
diff
changeset
|
1334 |
|
40418
3ee9d1a36d46
Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents:
33665
diff
changeset
|
1335 class SortedTestCase(RawConfigParserTestCase): |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1336 dict_type = SortedDict |
|
40418
3ee9d1a36d46
Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents:
33665
diff
changeset
|
1337 |
|
3ee9d1a36d46
Patch #1371075: Make ConfigParser accept optional dict type
Martin v. Löwis <martin@v.loewis.de>
parents:
33665
diff
changeset
|
1338 def test_sorted(self): |
| 63875 | 1339 cf = self.fromstring("[b]\n" |
| 1340 "o4=1\n" | |
| 1341 "o3=2\n" | |
| 1342 "o2=3\n" | |
| 1343 "o1=4\n" | |
| 1344 "[a]\n" | |
| 1345 "k=v\n") | |
|
42501
f6069f9dd52d
SF patch# 1770008 by Christian Heimes (plus some extras).
Guido van Rossum <guido@python.org>
parents:
42050
diff
changeset
|
1346 output = io.StringIO() |
| 63875 | 1347 cf.write(output) |
|
66310
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66287
diff
changeset
|
1348 self.assertEqual(output.getvalue(), |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66287
diff
changeset
|
1349 "[a]\n" |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66287
diff
changeset
|
1350 "k = v\n\n" |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66287
diff
changeset
|
1351 "[b]\n" |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66287
diff
changeset
|
1352 "o1 = 4\n" |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66287
diff
changeset
|
1353 "o2 = 3\n" |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66287
diff
changeset
|
1354 "o3 = 2\n" |
|
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66287
diff
changeset
|
1355 "o4 = 1\n\n") |
|
25918
9901fda7c366
Implement a safer and more predictable interpolation approach.
Fred Drake <fdrake@acm.org>
parents:
25917
diff
changeset
|
1356 |
|
58949
4fc8b6fe4af8
Merged revisions 78232 via svnmerge from
Fred Drake <fdrake@acm.org>
parents:
58456
diff
changeset
|
1357 |
|
81385
7dbdd7204d0a
#16925: test_configparser now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81211
diff
changeset
|
1358 class CompatibleTestCase(CfgParserTestCaseClass, unittest.TestCase): |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1359 config_class = configparser.RawConfigParser |
|
66981
074c21714a02
configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents:
66953
diff
changeset
|
1360 comment_prefixes = '#;' |
|
074c21714a02
configparser API cleanup: default values now sensible, slightly incompatible.
Łukasz Langa <lukasz@langa.pl>
parents:
66953
diff
changeset
|
1361 inline_comment_prefixes = ';' |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1362 |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1363 def test_comment_handling(self): |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1364 config_string = textwrap.dedent("""\ |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1365 [Commented Bar] |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1366 baz=qwe ; a comment |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1367 foo: bar # not a comment! |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1368 # but this is a comment |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1369 ; another comment |
|
63281
e5a2f50fee64
#9411: allow selecting an encoding for configparser files. Also adds a new test config file to test special cases.
Georg Brandl <georg@python.org>
parents:
63264
diff
changeset
|
1370 quirk: this;is not a comment |
|
63380
9eb4ba45a6b2
Clarify comment in comments test case explaining comment semantics.
Georg Brandl <georg@python.org>
parents:
63281
diff
changeset
|
1371 ; a space must precede an inline comment |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1372 """) |
|
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1373 cf = self.fromstring(config_string) |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1374 self.assertEqual(cf.get('Commented Bar', 'foo'), |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1375 'bar # not a comment!') |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1376 self.assertEqual(cf.get('Commented Bar', 'baz'), 'qwe') |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1377 self.assertEqual(cf.get('Commented Bar', 'quirk'), |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1378 'this;is not a comment') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1379 |
|
81385
7dbdd7204d0a
#16925: test_configparser now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81211
diff
changeset
|
1380 class CopyTestCase(BasicTestCase, unittest.TestCase): |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1381 config_class = configparser.ConfigParser |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1382 |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1383 def fromstring(self, string, defaults=None): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1384 cf = self.newconfig(defaults) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1385 cf.read_string(string) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1386 cf_copy = self.newconfig() |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1387 cf_copy.read_dict(cf) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1388 # we have to clean up option duplicates that appeared because of |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1389 # the magic DEFAULTSECT behaviour. |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1390 for section in cf_copy.values(): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1391 if section.name == self.default_section: |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1392 continue |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1393 for default, value in cf[self.default_section].items(): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1394 if section[default] == value: |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1395 del section[default] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1396 return cf_copy |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1397 |
|
69631
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1398 |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1399 class FakeFile: |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1400 def __init__(self): |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1401 file_path = support.findfile("cfgparser.1") |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1402 with open(file_path) as f: |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1403 self.lines = f.readlines() |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1404 self.lines.reverse() |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1405 |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1406 def readline(self): |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1407 if len(self.lines): |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1408 return self.lines.pop() |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1409 return '' |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1410 |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1411 |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1412 def readline_generator(f): |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1413 """As advised in Doc/library/configparser.rst.""" |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1414 line = f.readline() |
|
69663
1bbda00bbe78
Style updates for the #11670 solution after post-commit review by Ezio Melotti:
Łukasz Langa <lukasz@langa.pl>
parents:
69657
diff
changeset
|
1415 while line: |
|
69631
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1416 yield line |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1417 line = f.readline() |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1418 |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1419 |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1420 class ReadFileTestCase(unittest.TestCase): |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1421 def test_file(self): |
|
84279
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1422 file_paths = [support.findfile("cfgparser.1")] |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1423 try: |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1424 file_paths.append(file_paths[0].encode('utf8')) |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1425 except UnicodeEncodeError: |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1426 pass # unfortunately we can't test bytes on this path |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1427 for file_path in file_paths: |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1428 parser = configparser.ConfigParser() |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1429 with open(file_path) as f: |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1430 parser.read_file(f) |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1431 self.assertIn("Foo Bar", parser) |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1432 self.assertIn("foo", parser["Foo Bar"]) |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1433 self.assertEqual(parser["Foo Bar"]["foo"], "newbar") |
|
69631
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1434 |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1435 def test_iterable(self): |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1436 lines = textwrap.dedent(""" |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1437 [Foo Bar] |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1438 foo=newbar""").strip().split('\n') |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1439 parser = configparser.ConfigParser() |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1440 parser.read_file(lines) |
|
69663
1bbda00bbe78
Style updates for the #11670 solution after post-commit review by Ezio Melotti:
Łukasz Langa <lukasz@langa.pl>
parents:
69657
diff
changeset
|
1441 self.assertIn("Foo Bar", parser) |
|
1bbda00bbe78
Style updates for the #11670 solution after post-commit review by Ezio Melotti:
Łukasz Langa <lukasz@langa.pl>
parents:
69657
diff
changeset
|
1442 self.assertIn("foo", parser["Foo Bar"]) |
|
69631
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1443 self.assertEqual(parser["Foo Bar"]["foo"], "newbar") |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1444 |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1445 def test_readline_generator(self): |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1446 """Issue #11670.""" |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1447 parser = configparser.ConfigParser() |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1448 with self.assertRaises(TypeError): |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1449 parser.read_file(FakeFile()) |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1450 parser.read_file(readline_generator(FakeFile())) |
|
69663
1bbda00bbe78
Style updates for the #11670 solution after post-commit review by Ezio Melotti:
Łukasz Langa <lukasz@langa.pl>
parents:
69657
diff
changeset
|
1451 self.assertIn("Foo Bar", parser) |
|
1bbda00bbe78
Style updates for the #11670 solution after post-commit review by Ezio Melotti:
Łukasz Langa <lukasz@langa.pl>
parents:
69657
diff
changeset
|
1452 self.assertIn("foo", parser["Foo Bar"]) |
|
69631
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1453 self.assertEqual(parser["Foo Bar"]["foo"], "newbar") |
|
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1454 |
|
84279
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1455 def test_source_as_bytes(self): |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1456 """Issue #18260.""" |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1457 lines = textwrap.dedent(""" |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1458 [badbad] |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1459 [badbad]""").strip().split('\n') |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1460 parser = configparser.ConfigParser() |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1461 with self.assertRaises(configparser.DuplicateSectionError) as dse: |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1462 parser.read_file(lines, source=b"badbad") |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1463 self.assertEqual( |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1464 str(dse.exception), |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1465 "While reading from b'badbad' [line 2]: section 'badbad' " |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1466 "already exists" |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1467 ) |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1468 lines = textwrap.dedent(""" |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1469 [badbad] |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1470 bad = bad |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1471 bad = bad""").strip().split('\n') |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1472 parser = configparser.ConfigParser() |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1473 with self.assertRaises(configparser.DuplicateOptionError) as dse: |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1474 parser.read_file(lines, source=b"badbad") |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1475 self.assertEqual( |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1476 str(dse.exception), |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1477 "While reading from b'badbad' [line 3]: option 'bad' in section " |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1478 "'badbad' already exists" |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1479 ) |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1480 lines = textwrap.dedent(""" |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1481 [badbad] |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1482 = bad""").strip().split('\n') |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1483 parser = configparser.ConfigParser() |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1484 with self.assertRaises(configparser.ParsingError) as dse: |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1485 parser.read_file(lines, source=b"badbad") |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1486 self.assertEqual( |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1487 str(dse.exception), |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1488 "Source contains parsing errors: b'badbad'\n\t[line 2]: '= bad'" |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1489 ) |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1490 lines = textwrap.dedent(""" |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1491 [badbad |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1492 bad = bad""").strip().split('\n') |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1493 parser = configparser.ConfigParser() |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1494 with self.assertRaises(configparser.MissingSectionHeaderError) as dse: |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1495 parser.read_file(lines, source=b"badbad") |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1496 self.assertEqual( |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1497 str(dse.exception), |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1498 "File contains no section headers.\nfile: b'badbad', line: 1\n" |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1499 "'[badbad'" |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1500 ) |
|
56c1227f21f5
Fixed issue #18260: configparser TypeError on source name specified as bytes
Łukasz Langa <lukasz@langa.pl>
parents:
81385
diff
changeset
|
1501 |
|
69631
6f937d6369b6
Closes #11670: configparser read_file now iterates over f.
Łukasz Langa <lukasz@langa.pl>
parents:
68257
diff
changeset
|
1502 |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1503 class CoverageOneHundredTestCase(unittest.TestCase): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1504 """Covers edge cases in the codebase.""" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1505 |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1506 def test_duplicate_option_error(self): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1507 error = configparser.DuplicateOptionError('section', 'option') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1508 self.assertEqual(error.section, 'section') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1509 self.assertEqual(error.option, 'option') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1510 self.assertEqual(error.source, None) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1511 self.assertEqual(error.lineno, None) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1512 self.assertEqual(error.args, ('section', 'option', None, None)) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1513 self.assertEqual(str(error), "Option 'option' in section 'section' " |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1514 "already exists") |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1515 |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1516 def test_interpolation_depth_error(self): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1517 error = configparser.InterpolationDepthError('option', 'section', |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1518 'rawval') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1519 self.assertEqual(error.args, ('option', 'section', 'rawval')) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1520 self.assertEqual(error.option, 'option') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1521 self.assertEqual(error.section, 'section') |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1522 |
|
66997
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1523 def test_parsing_error(self): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1524 with self.assertRaises(ValueError) as cm: |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1525 configparser.ParsingError() |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1526 self.assertEqual(str(cm.exception), "Required argument `source' not " |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1527 "given.") |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1528 with self.assertRaises(ValueError) as cm: |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1529 configparser.ParsingError(source='source', filename='filename') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1530 self.assertEqual(str(cm.exception), "Cannot specify both `filename' " |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1531 "and `source'. Use `source'.") |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1532 error = configparser.ParsingError(filename='source') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1533 self.assertEqual(error.source, 'source') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1534 with warnings.catch_warnings(record=True) as w: |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1535 warnings.simplefilter("always", DeprecationWarning) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1536 self.assertEqual(error.filename, 'source') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1537 error.filename = 'filename' |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1538 self.assertEqual(error.source, 'filename') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1539 for warning in w: |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1540 self.assertTrue(warning.category is DeprecationWarning) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1541 |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1542 def test_interpolation_validation(self): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1543 parser = configparser.ConfigParser() |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1544 parser.read_string(""" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1545 [section] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1546 invalid_percent = % |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1547 invalid_reference = %(() |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1548 invalid_variable = %(does_not_exist)s |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1549 """) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1550 with self.assertRaises(configparser.InterpolationSyntaxError) as cm: |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1551 parser['section']['invalid_percent'] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1552 self.assertEqual(str(cm.exception), "'%' must be followed by '%' or " |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1553 "'(', found: '%'") |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1554 with self.assertRaises(configparser.InterpolationSyntaxError) as cm: |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1555 parser['section']['invalid_reference'] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1556 self.assertEqual(str(cm.exception), "bad interpolation variable " |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1557 "reference '%(()'") |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1558 |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1559 def test_readfp_deprecation(self): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1560 sio = io.StringIO(""" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1561 [section] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1562 option = value |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1563 """) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1564 parser = configparser.ConfigParser() |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1565 with warnings.catch_warnings(record=True) as w: |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1566 warnings.simplefilter("always", DeprecationWarning) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1567 parser.readfp(sio, filename='StringIO') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1568 for warning in w: |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1569 self.assertTrue(warning.category is DeprecationWarning) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1570 self.assertEqual(len(parser), 2) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1571 self.assertEqual(parser['section']['option'], 'value') |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1572 |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1573 def test_safeconfigparser_deprecation(self): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1574 with warnings.catch_warnings(record=True) as w: |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1575 warnings.simplefilter("always", DeprecationWarning) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1576 parser = configparser.SafeConfigParser() |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1577 for warning in w: |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1578 self.assertTrue(warning.category is DeprecationWarning) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1579 |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1580 def test_sectionproxy_repr(self): |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1581 parser = configparser.ConfigParser() |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1582 parser.read_string(""" |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1583 [section] |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1584 key = value |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1585 """) |
|
892236137db9
100% test coverage, better mapping protocol compatibility, some minor bugfixes
Łukasz Langa <lukasz@langa.pl>
parents:
66981
diff
changeset
|
1586 self.assertEqual(repr(parser['section']), '<Section: section>') |
|
63264
bb9eef8d1ac6
#1682942: add some ConfigParser features: alternate delimiters, alternate comments, empty lines in values. Also enhance the docs with more examples and mention SafeConfigParser before ConfigParser. Patch by Lukas Langa, review by myself, Eric and Ezio.
Georg Brandl <georg@python.org>
parents:
63220
diff
changeset
|
1587 |
|
74541
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1588 |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1589 class ExceptionPicklingTestCase(unittest.TestCase): |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1590 """Tests for issue #13760: ConfigParser exceptions are not picklable.""" |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1591 |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1592 def test_error(self): |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1593 import pickle |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1594 e1 = configparser.Error('value') |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1595 pickled = pickle.dumps(e1) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1596 e2 = pickle.loads(pickled) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1597 self.assertEqual(e1.message, e2.message) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1598 self.assertEqual(repr(e1), repr(e2)) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1599 |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1600 def test_nosectionerror(self): |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1601 import pickle |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1602 e1 = configparser.NoSectionError('section') |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1603 pickled = pickle.dumps(e1) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1604 e2 = pickle.loads(pickled) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1605 self.assertEqual(e1.message, e2.message) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1606 self.assertEqual(e1.args, e2.args) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1607 self.assertEqual(e1.section, e2.section) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1608 self.assertEqual(repr(e1), repr(e2)) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1609 |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1610 def test_nooptionerror(self): |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1611 import pickle |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1612 e1 = configparser.NoOptionError('option', 'section') |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1613 pickled = pickle.dumps(e1) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1614 e2 = pickle.loads(pickled) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1615 self.assertEqual(e1.message, e2.message) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1616 self.assertEqual(e1.args, e2.args) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1617 self.assertEqual(e1.section, e2.section) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1618 self.assertEqual(e1.option, e2.option) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1619 self.assertEqual(repr(e1), repr(e2)) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1620 |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1621 def test_duplicatesectionerror(self): |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1622 import pickle |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1623 e1 = configparser.DuplicateSectionError('section', 'source', 123) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1624 pickled = pickle.dumps(e1) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1625 e2 = pickle.loads(pickled) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1626 self.assertEqual(e1.message, e2.message) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1627 self.assertEqual(e1.args, e2.args) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1628 self.assertEqual(e1.section, e2.section) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1629 self.assertEqual(e1.source, e2.source) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1630 self.assertEqual(e1.lineno, e2.lineno) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1631 self.assertEqual(repr(e1), repr(e2)) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1632 |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1633 def test_duplicateoptionerror(self): |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1634 import pickle |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1635 e1 = configparser.DuplicateOptionError('section', 'option', 'source', |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1636 123) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1637 pickled = pickle.dumps(e1) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1638 e2 = pickle.loads(pickled) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1639 self.assertEqual(e1.message, e2.message) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1640 self.assertEqual(e1.args, e2.args) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1641 self.assertEqual(e1.section, e2.section) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1642 self.assertEqual(e1.option, e2.option) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1643 self.assertEqual(e1.source, e2.source) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1644 self.assertEqual(e1.lineno, e2.lineno) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1645 self.assertEqual(repr(e1), repr(e2)) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1646 |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1647 def test_interpolationerror(self): |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1648 import pickle |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1649 e1 = configparser.InterpolationError('option', 'section', 'msg') |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1650 pickled = pickle.dumps(e1) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1651 e2 = pickle.loads(pickled) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1652 self.assertEqual(e1.message, e2.message) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1653 self.assertEqual(e1.args, e2.args) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1654 self.assertEqual(e1.section, e2.section) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1655 self.assertEqual(e1.option, e2.option) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1656 self.assertEqual(repr(e1), repr(e2)) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1657 |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1658 def test_interpolationmissingoptionerror(self): |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1659 import pickle |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1660 e1 = configparser.InterpolationMissingOptionError('option', 'section', |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1661 'rawval', 'reference') |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1662 pickled = pickle.dumps(e1) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1663 e2 = pickle.loads(pickled) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1664 self.assertEqual(e1.message, e2.message) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1665 self.assertEqual(e1.args, e2.args) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1666 self.assertEqual(e1.section, e2.section) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1667 self.assertEqual(e1.option, e2.option) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1668 self.assertEqual(e1.reference, e2.reference) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1669 self.assertEqual(repr(e1), repr(e2)) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1670 |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1671 def test_interpolationsyntaxerror(self): |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1672 import pickle |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1673 e1 = configparser.InterpolationSyntaxError('option', 'section', 'msg') |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1674 pickled = pickle.dumps(e1) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1675 e2 = pickle.loads(pickled) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1676 self.assertEqual(e1.message, e2.message) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1677 self.assertEqual(e1.args, e2.args) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1678 self.assertEqual(e1.section, e2.section) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1679 self.assertEqual(e1.option, e2.option) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1680 self.assertEqual(repr(e1), repr(e2)) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1681 |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1682 def test_interpolationdeptherror(self): |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1683 import pickle |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1684 e1 = configparser.InterpolationDepthError('option', 'section', |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1685 'rawval') |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1686 pickled = pickle.dumps(e1) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1687 e2 = pickle.loads(pickled) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1688 self.assertEqual(e1.message, e2.message) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1689 self.assertEqual(e1.args, e2.args) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1690 self.assertEqual(e1.section, e2.section) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1691 self.assertEqual(e1.option, e2.option) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1692 self.assertEqual(repr(e1), repr(e2)) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1693 |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1694 def test_parsingerror(self): |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1695 import pickle |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1696 e1 = configparser.ParsingError('source') |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1697 e1.append(1, 'line1') |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1698 e1.append(2, 'line2') |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1699 e1.append(3, 'line3') |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1700 pickled = pickle.dumps(e1) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1701 e2 = pickle.loads(pickled) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1702 self.assertEqual(e1.message, e2.message) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1703 self.assertEqual(e1.args, e2.args) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1704 self.assertEqual(e1.source, e2.source) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1705 self.assertEqual(e1.errors, e2.errors) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1706 self.assertEqual(repr(e1), repr(e2)) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1707 e1 = configparser.ParsingError(filename='filename') |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1708 e1.append(1, 'line1') |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1709 e1.append(2, 'line2') |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1710 e1.append(3, 'line3') |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1711 pickled = pickle.dumps(e1) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1712 e2 = pickle.loads(pickled) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1713 self.assertEqual(e1.message, e2.message) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1714 self.assertEqual(e1.args, e2.args) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1715 self.assertEqual(e1.source, e2.source) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1716 self.assertEqual(e1.errors, e2.errors) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1717 self.assertEqual(repr(e1), repr(e2)) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1718 |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1719 def test_missingsectionheadererror(self): |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1720 import pickle |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1721 e1 = configparser.MissingSectionHeaderError('filename', 123, 'line') |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1722 pickled = pickle.dumps(e1) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1723 e2 = pickle.loads(pickled) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1724 self.assertEqual(e1.message, e2.message) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1725 self.assertEqual(e1.args, e2.args) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1726 self.assertEqual(e1.line, e2.line) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1727 self.assertEqual(e1.source, e2.source) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1728 self.assertEqual(e1.lineno, e2.lineno) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1729 self.assertEqual(repr(e1), repr(e2)) |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1730 |
|
e63e2471f46f
#13760: picklability tests for configparser exceptions
Łukasz Langa <lukasz@langa.pl>
parents:
69688
diff
changeset
|
1731 |
|
77980
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1732 class InlineCommentStrippingTestCase(unittest.TestCase): |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1733 """Tests for issue #14590: ConfigParser doesn't strip inline comment when |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1734 delimiter occurs earlier without preceding space..""" |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1735 |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1736 def test_stripping(self): |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1737 cfg = configparser.ConfigParser(inline_comment_prefixes=(';', '#', |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1738 '//')) |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1739 cfg.read_string(""" |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1740 [section] |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1741 k1 = v1;still v1 |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1742 k2 = v2 ;a comment |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1743 k3 = v3 ; also a comment |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1744 k4 = v4;still v4 ;a comment |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1745 k5 = v5;still v5 ; also a comment |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1746 k6 = v6;still v6; and still v6 ;a comment |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1747 k7 = v7;still v7; and still v7 ; also a comment |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1748 |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1749 [multiprefix] |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1750 k1 = v1;still v1 #a comment ; yeah, pretty much |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1751 k2 = v2 // this already is a comment ; continued |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1752 k3 = v3;#//still v3# and still v3 ; a comment |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1753 """) |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1754 s = cfg['section'] |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1755 self.assertEqual(s['k1'], 'v1;still v1') |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1756 self.assertEqual(s['k2'], 'v2') |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1757 self.assertEqual(s['k3'], 'v3') |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1758 self.assertEqual(s['k4'], 'v4;still v4') |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1759 self.assertEqual(s['k5'], 'v5;still v5') |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1760 self.assertEqual(s['k6'], 'v6;still v6; and still v6') |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1761 self.assertEqual(s['k7'], 'v7;still v7; and still v7') |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1762 s = cfg['multiprefix'] |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1763 self.assertEqual(s['k1'], 'v1;still v1') |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1764 self.assertEqual(s['k2'], 'v2') |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1765 self.assertEqual(s['k3'], 'v3;#//still v3# and still v3') |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1766 |
|
5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
Łukasz Langa <lukasz@langa.pl>
parents:
74543
diff
changeset
|
1767 |
|
81385
7dbdd7204d0a
#16925: test_configparser now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81211
diff
changeset
|
1768 if __name__ == '__main__': |
|
7dbdd7204d0a
#16925: test_configparser now works with unittest test discovery. Patch by Zachary Ware.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81211
diff
changeset
|
1769 unittest.main() |
