Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 95b96d3

Browse files
committed
Added options that use square brackets in their names; this ensures that
GNOME-style internationalized options can be parsed using ConfigParser (SF bug #131635). Converted the tests to use test_support.verify() instead of output comparison to work.
1 parent d83bbbf commit 95b96d3

2 files changed

Lines changed: 50 additions & 89 deletions

File tree

Lib/test/output/test_cfgparser

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,6 @@
11
test_cfgparser
2-
32
Testing basic accessors...
4-
['Commented Bar', 'Foo Bar', 'Spacey Bar']
5-
Commented Bar: ['foo']
6-
Foo Bar: ['foo']
7-
Spacey Bar: ['foo']
8-
'bar'
9-
'bar'
10-
'bar'
11-
__name__ "option" properly hidden by the API.
12-
133
Testing value interpolation...
14-
'Foo'
15-
'something with interpolation (1 step)'
16-
'something with lots of interpolation (9 steps)'
17-
'something with lots of interpolation (10 steps)'
18-
Caught expected InterpolationDepthError :
19-
Value interpolation too deeply recursive:
20-
section: [Foo]
21-
option : bar11
22-
rawval : something %(with11)s lots of interpolation (11 steps)
23-
24-
25-
Testing for parsing errors...
26-
Caught expected exception: File contains parsing errors: <???>
27-
[line 2]: ' extra-spaces: splat\n'
28-
Caught expected exception: File contains parsing errors: <???>
29-
[line 2]: ' extra-spaces= splat\n'
30-
Caught expected exception: File contains parsing errors: <???>
31-
[line 2]: 'option-without-value\n'
32-
Caught expected exception: File contains parsing errors: <???>
33-
[line 2]: ':value-without-option-name\n'
34-
Caught expected exception: File contains parsing errors: <???>
35-
[line 2]: '=value-without-option-name\n'
36-
Caught expected exception: File contains no section headers.
37-
file: <???>, line: 1
38-
'No Section!\n'
39-
4+
Testing parse errors...
405
Testing query interface...
41-
[]
42-
Has section 'Foo'? 0
43-
Caught expected NoSectionError: No section: Foo
44-
Caught expected NoSectionError: No section: foo
45-
Caught expected NoSectionError :
46-
No section: foo
47-
Caught expected NoOptionError :
48-
No option `bar' in section: foo
49-
506
Testing miscellaneous error conditions...
51-
Caught expected DuplicateSectionError: Section Foo already exists

Lib/test/test_cfgparser.py

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,39 @@
11
import ConfigParser
22
import StringIO
33

4-
from test_support import TestFailed
4+
from test_support import TestFailed, verify
55

66

77
def basic(src):
8-
print
98
print "Testing basic accessors..."
109
cf = ConfigParser.ConfigParser()
1110
sio = StringIO.StringIO(src)
1211
cf.readfp(sio)
1312
L = cf.sections()
1413
L.sort()
15-
print L
16-
for s in L:
17-
print "%s: %s" % (s, cf.options(s))
14+
verify(L == ['Commented Bar', 'Foo Bar',
15+
'Internationalized Stuff', 'Spacey Bar'],
16+
"unexpected list of section names")
1817

1918
# The use of spaces in the section names serves as a regression test for
2019
# SourceForge bug #115357.
2120
# http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357
22-
print `cf.get('Foo Bar', 'foo', raw=1)`
23-
print `cf.get('Spacey Bar', 'foo', raw=1)`
24-
print `cf.get('Commented Bar', 'foo', raw=1)`
21+
verify(cf.get('Foo Bar', 'foo', raw=1) == 'bar')
22+
verify(cf.get('Spacey Bar', 'foo', raw=1) == 'bar')
23+
verify(cf.get('Commented Bar', 'foo', raw=1) == 'bar')
2524

26-
if '__name__' in cf.options("Foo Bar"):
27-
print '__name__ "option" should not be exposed by the API!'
28-
else:
29-
print '__name__ "option" properly hidden by the API.'
25+
verify('__name__' not in cf.options("Foo Bar"),
26+
'__name__ "option" should not be exposed by the API!')
3027

3128
# Make sure the right things happen for remove_option();
3229
# added to include check for SourceForge bug #123324:
33-
if not cf.remove_option('Foo Bar', 'foo'):
34-
raise TestFailed(
35-
"remove_option() failed to report existance of option")
36-
if cf.has_option('Foo Bar', 'foo'):
37-
raise TestFailed("remove_option() failed to remove option")
38-
if cf.remove_option('Foo Bar', 'foo'):
39-
raise TestFailed(
40-
"remove_option() failed to report non-existance of option"
41-
" that was removed")
30+
verify(cf.remove_option('Foo Bar', 'foo'),
31+
"remove_option() failed to report existance of option")
32+
verify(not cf.has_option('Foo Bar', 'foo'),
33+
"remove_option() failed to remove option")
34+
verify(not cf.remove_option('Foo Bar', 'foo'),
35+
"remove_option() failed to report non-existance of option"
36+
" that was removed")
4237
try:
4338
cf.remove_option('No Such Section', 'foo')
4439
except ConfigParser.NoSectionError:
@@ -50,20 +45,21 @@ def basic(src):
5045

5146

5247
def interpolation(src):
53-
print
5448
print "Testing value interpolation..."
5549
cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"})
5650
sio = StringIO.StringIO(src)
5751
cf.readfp(sio)
58-
print `cf.get("Foo", "getname")`
59-
print `cf.get("Foo", "bar")`
60-
print `cf.get("Foo", "bar9")`
61-
print `cf.get("Foo", "bar10")`
52+
verify(cf.get("Foo", "getname") == "Foo")
53+
verify(cf.get("Foo", "bar") == "something with interpolation (1 step)")
54+
verify(cf.get("Foo", "bar9")
55+
== "something with lots of interpolation (9 steps)")
56+
verify(cf.get("Foo", "bar10")
57+
== "something with lots of interpolation (10 steps)")
6258
expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11")
6359

60+
6461
def parse_errors():
65-
print
66-
print "Testing for parsing errors..."
62+
print "Testing parse errors..."
6763
expect_parse_error(ConfigParser.ParsingError,
6864
"""[Foo]\n extra-spaces: splat\n""")
6965
expect_parse_error(ConfigParser.ParsingError,
@@ -77,58 +73,63 @@ def parse_errors():
7773
expect_parse_error(ConfigParser.MissingSectionHeaderError,
7874
"""No Section!\n""")
7975

76+
8077
def query_errors():
81-
print
8278
print "Testing query interface..."
8379
cf = ConfigParser.ConfigParser()
84-
print cf.sections()
85-
print "Has section 'Foo'?", cf.has_section("Foo")
80+
verify(cf.sections() == [],
81+
"new ConfigParser should have no defined sections")
82+
verify(not cf.has_section("Foo"),
83+
"new ConfigParser should have no acknowledged sections")
8684
try:
8785
cf.options("Foo")
8886
except ConfigParser.NoSectionError, e:
89-
print "Caught expected NoSectionError:", e
87+
pass
9088
else:
91-
print "Failed to catch expected NoSectionError from options()"
89+
raise TestFailed(
90+
"Failed to catch expected NoSectionError from options()")
9291
try:
9392
cf.set("foo", "bar", "value")
9493
except ConfigParser.NoSectionError, e:
95-
print "Caught expected NoSectionError:", e
94+
pass
9695
else:
97-
print "Failed to catch expected NoSectionError from set()"
96+
raise TestFailed("Failed to catch expected NoSectionError from set()")
9897
expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar")
9998
cf.add_section("foo")
10099
expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar")
101100

101+
102102
def weird_errors():
103-
print
104103
print "Testing miscellaneous error conditions..."
105104
cf = ConfigParser.ConfigParser()
106105
cf.add_section("Foo")
107106
try:
108107
cf.add_section("Foo")
109108
except ConfigParser.DuplicateSectionError, e:
110-
print "Caught expected DuplicateSectionError:", e
109+
pass
111110
else:
112-
print "Failed to catch expected DuplicateSectionError"
111+
raise TestFailed("Failed to catch expected DuplicateSectionError")
112+
113113

114114
def expect_get_error(cf, exctype, section, option, raw=0):
115115
try:
116116
cf.get(section, option, raw=raw)
117117
except exctype, e:
118-
print "Caught expected", exctype.__name__, ":"
119-
print e
118+
pass
120119
else:
121-
print "Failed to catch expected", exctype.__name__
120+
raise TestFailed("Failed to catch expected " + exctype.__name__)
121+
122122

123123
def expect_parse_error(exctype, src):
124124
cf = ConfigParser.ConfigParser()
125125
sio = StringIO.StringIO(src)
126126
try:
127127
cf.readfp(sio)
128128
except exctype, e:
129-
print "Caught expected exception:", e
129+
pass
130130
else:
131-
print "Failed to catch expected", exctype.__name__
131+
raise TestFailed("Failed to catch expected " + exctype.__name__)
132+
132133

133134
basic(r"""
134135
[Foo Bar]
@@ -137,6 +138,11 @@ def expect_parse_error(exctype, src):
137138
foo = bar
138139
[Commented Bar]
139140
foo: bar ; comment
141+
[Internationalized Stuff]
142+
foo[bg]: Bulgarian
143+
foo=Default
144+
foo[en]=English
145+
foo[de]=Deutsch
140146
""")
141147
interpolation(r"""
142148
[Foo]

0 commit comments

Comments
 (0)