6 print "Testing basic accessors..."
7 cf
= ConfigParser
.ConfigParser()
8 sio
= StringIO
.StringIO(src
)
14 print "%s: %s" % (s
, cf
.options(s
))
16 # The use of spaces in the section names serves as a regression test for
17 # SourceForge bug #115357.
18 # http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357
19 print `cf
.get('Foo Bar', 'foo', raw
=1)`
20 print `cf
.get('Spacey Bar', 'foo', raw
=1)`
21 print `cf
.get('Commented Bar', 'foo', raw
=1)`
23 if '__name__' in cf
.options("Foo Bar"):
24 print '__name__ "option" should not be exposed by the API!'
26 print '__name__ "option" properly hidden by the API.'
28 def interpolation(src
):
30 print "Testing value interpolation..."
31 cf
= ConfigParser
.ConfigParser({"getname": "%(__name__)s"})
32 sio
= StringIO
.StringIO(src
)
34 print `cf
.get("Foo", "getname")`
35 print `cf
.get("Foo", "bar")`
36 print `cf
.get("Foo", "bar9")`
37 print `cf
.get("Foo", "bar10")`
38 expect_get_error(cf
, ConfigParser
.InterpolationDepthError
, "Foo", "bar11")
42 print "Testing for parsing errors..."
43 expect_parse_error(ConfigParser
.ParsingError
,
44 """[Foo]\n extra-spaces: splat\n""")
45 expect_parse_error(ConfigParser
.ParsingError
,
46 """[Foo]\n extra-spaces= splat\n""")
47 expect_parse_error(ConfigParser
.ParsingError
,
48 """[Foo]\noption-without-value\n""")
49 expect_parse_error(ConfigParser
.ParsingError
,
50 """[Foo]\n:value-without-option-name\n""")
51 expect_parse_error(ConfigParser
.ParsingError
,
52 """[Foo]\n=value-without-option-name\n""")
53 expect_parse_error(ConfigParser
.MissingSectionHeaderError
,
58 print "Testing query interface..."
59 cf
= ConfigParser
.ConfigParser()
61 print "Has section 'Foo'?", cf
.has_section("Foo")
64 except ConfigParser
.NoSectionError
, e
:
65 print "Caught expected NoSectionError:", e
67 print "Failed to catch expected NoSectionError from options()"
69 cf
.set("foo", "bar", "value")
70 except ConfigParser
.NoSectionError
, e
:
71 print "Caught expected NoSectionError:", e
73 print "Failed to catch expected NoSectionError from set()"
74 expect_get_error(cf
, ConfigParser
.NoSectionError
, "foo", "bar")
76 expect_get_error(cf
, ConfigParser
.NoOptionError
, "foo", "bar")
80 print "Testing miscellaneous error conditions..."
81 cf
= ConfigParser
.ConfigParser()
85 except ConfigParser
.DuplicateSectionError
, e
:
86 print "Caught expected DuplicateSectionError:", e
88 print "Failed to catch expected DuplicateSectionError"
90 def expect_get_error(cf
, exctype
, section
, option
, raw
=0):
92 cf
.get(section
, option
, raw
=raw
)
94 print "Caught expected", exctype
.__name
__, ":"
97 print "Failed to catch expected", exctype
.__name
__
99 def expect_parse_error(exctype
, src
):
100 cf
= ConfigParser
.ConfigParser()
101 sio
= StringIO
.StringIO(src
)
105 print "Caught expected exception:", e
107 print "Failed to catch expected", exctype
.__name
__
119 bar=something %(with1)s interpolation (1 step)
120 bar9=something %(with9)s lots of interpolation (9 steps)
121 bar10=something %(with10)s lots of interpolation (10 steps)
122 bar11=something %(with11)s lots of interpolation (11 steps)