2000-09-28 06:45:25 +08:00
|
|
|
import ConfigParser
|
|
|
|
import StringIO
|
|
|
|
|
2002-07-31 07:27:12 +08:00
|
|
|
from test.test_support import TestFailed, verify
|
2000-12-05 00:30:40 +08:00
|
|
|
|
|
|
|
|
2000-09-28 06:45:25 +08:00
|
|
|
def basic(src):
|
|
|
|
print "Testing basic accessors..."
|
|
|
|
cf = ConfigParser.ConfigParser()
|
|
|
|
sio = StringIO.StringIO(src)
|
|
|
|
cf.readfp(sio)
|
|
|
|
L = cf.sections()
|
|
|
|
L.sort()
|
2001-02-14 23:30:31 +08:00
|
|
|
verify(L == [r'Commented Bar',
|
|
|
|
r'Foo Bar',
|
|
|
|
r'Internationalized Stuff',
|
2002-03-09 02:10:12 +08:00
|
|
|
r'Long Line',
|
2001-02-14 23:30:31 +08:00
|
|
|
r'Section\with$weird%characters[' '\t',
|
2002-09-28 00:21:18 +08:00
|
|
|
r'Spaces',
|
2001-02-14 23:30:31 +08:00
|
|
|
r'Spacey Bar',
|
|
|
|
],
|
2001-02-13 01:23:20 +08:00
|
|
|
"unexpected list of section names")
|
2000-09-28 06:45:25 +08:00
|
|
|
|
|
|
|
# The use of spaces in the section names serves as a regression test for
|
|
|
|
# SourceForge bug #115357.
|
|
|
|
# http://sourceforge.net/bugs/?func=detailbug&group_id=5470&bug_id=115357
|
2001-02-13 01:23:20 +08:00
|
|
|
verify(cf.get('Foo Bar', 'foo', raw=1) == 'bar')
|
|
|
|
verify(cf.get('Spacey Bar', 'foo', raw=1) == 'bar')
|
|
|
|
verify(cf.get('Commented Bar', 'foo', raw=1) == 'bar')
|
2002-09-28 00:21:18 +08:00
|
|
|
verify(cf.get('Spaces', 'key with spaces', raw=1) == 'value')
|
|
|
|
verify(cf.get('Spaces', 'another with spaces', raw=1) == 'splat!')
|
2000-09-28 06:45:25 +08:00
|
|
|
|
2001-02-13 01:23:20 +08:00
|
|
|
verify('__name__' not in cf.options("Foo Bar"),
|
|
|
|
'__name__ "option" should not be exposed by the API!')
|
2000-09-28 06:45:25 +08:00
|
|
|
|
2000-12-05 00:30:40 +08:00
|
|
|
# Make sure the right things happen for remove_option();
|
|
|
|
# added to include check for SourceForge bug #123324:
|
2001-02-13 01:23:20 +08:00
|
|
|
verify(cf.remove_option('Foo Bar', 'foo'),
|
|
|
|
"remove_option() failed to report existance of option")
|
|
|
|
verify(not cf.has_option('Foo Bar', 'foo'),
|
|
|
|
"remove_option() failed to remove option")
|
|
|
|
verify(not cf.remove_option('Foo Bar', 'foo'),
|
|
|
|
"remove_option() failed to report non-existance of option"
|
|
|
|
" that was removed")
|
2000-12-05 00:30:40 +08:00
|
|
|
try:
|
|
|
|
cf.remove_option('No Such Section', 'foo')
|
|
|
|
except ConfigParser.NoSectionError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise TestFailed(
|
|
|
|
"remove_option() failed to report non-existance of option"
|
|
|
|
" that never existed")
|
|
|
|
|
2002-03-09 02:10:12 +08:00
|
|
|
verify(cf.get('Long Line', 'foo', raw=1) ==
|
|
|
|
'this line is much, much longer than my editor\nlikes it.')
|
2000-12-05 00:30:40 +08:00
|
|
|
|
2002-03-09 02:10:12 +08:00
|
|
|
|
2001-02-27 05:55:34 +08:00
|
|
|
def case_sensitivity():
|
|
|
|
print "Testing case sensitivity..."
|
|
|
|
cf = ConfigParser.ConfigParser()
|
|
|
|
cf.add_section("A")
|
|
|
|
cf.add_section("a")
|
|
|
|
L = cf.sections()
|
|
|
|
L.sort()
|
|
|
|
verify(L == ["A", "a"])
|
|
|
|
cf.set("a", "B", "value")
|
|
|
|
verify(cf.options("a") == ["b"])
|
|
|
|
verify(cf.get("a", "b", raw=1) == "value",
|
|
|
|
"could not locate option, expecting case-insensitive option names")
|
|
|
|
verify(cf.has_option("a", "b"))
|
|
|
|
cf.set("A", "A-B", "A-B value")
|
|
|
|
for opt in ("a-b", "A-b", "a-B", "A-B"):
|
|
|
|
verify(cf.has_option("A", opt),
|
|
|
|
"has_option() returned false for option which should exist")
|
|
|
|
verify(cf.options("A") == ["a-b"])
|
|
|
|
verify(cf.options("a") == ["b"])
|
|
|
|
cf.remove_option("a", "B")
|
|
|
|
verify(cf.options("a") == [])
|
|
|
|
|
2001-07-07 01:22:48 +08:00
|
|
|
# SF bug #432369:
|
|
|
|
cf = ConfigParser.ConfigParser()
|
|
|
|
sio = StringIO.StringIO("[MySection]\nOption: first line\n\tsecond line\n")
|
|
|
|
cf.readfp(sio)
|
|
|
|
verify(cf.options("MySection") == ["option"])
|
|
|
|
verify(cf.get("MySection", "Option") == "first line\nsecond line")
|
|
|
|
|
2002-09-27 23:35:23 +08:00
|
|
|
# SF bug #561822:
|
|
|
|
cf = ConfigParser.ConfigParser(defaults={"key":"value"})
|
|
|
|
cf.readfp(StringIO.StringIO("[section]\nnekey=nevalue\n"))
|
|
|
|
verify(cf.has_option("section", "Key"))
|
|
|
|
|
2001-02-27 05:55:34 +08:00
|
|
|
|
2001-10-09 01:13:12 +08:00
|
|
|
def boolean(src):
|
|
|
|
print "Testing interpretation of boolean Values..."
|
|
|
|
cf = ConfigParser.ConfigParser()
|
|
|
|
sio = StringIO.StringIO(src)
|
|
|
|
cf.readfp(sio)
|
|
|
|
for x in range(1, 5):
|
|
|
|
verify(cf.getboolean('BOOLTEST', 't%d' % (x)) == 1)
|
|
|
|
for x in range(1, 5):
|
|
|
|
verify(cf.getboolean('BOOLTEST', 'f%d' % (x)) == 0)
|
|
|
|
for x in range(1, 5):
|
|
|
|
try:
|
|
|
|
cf.getboolean('BOOLTEST', 'e%d' % (x))
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise TestFailed(
|
|
|
|
"getboolean() failed to report a non boolean value")
|
|
|
|
|
|
|
|
|
2000-09-28 06:45:25 +08:00
|
|
|
def interpolation(src):
|
|
|
|
print "Testing value interpolation..."
|
|
|
|
cf = ConfigParser.ConfigParser({"getname": "%(__name__)s"})
|
|
|
|
sio = StringIO.StringIO(src)
|
|
|
|
cf.readfp(sio)
|
2001-02-13 01:23:20 +08:00
|
|
|
verify(cf.get("Foo", "getname") == "Foo")
|
|
|
|
verify(cf.get("Foo", "bar") == "something with interpolation (1 step)")
|
|
|
|
verify(cf.get("Foo", "bar9")
|
|
|
|
== "something with lots of interpolation (9 steps)")
|
|
|
|
verify(cf.get("Foo", "bar10")
|
|
|
|
== "something with lots of interpolation (10 steps)")
|
2000-09-28 06:45:25 +08:00
|
|
|
expect_get_error(cf, ConfigParser.InterpolationDepthError, "Foo", "bar11")
|
|
|
|
|
2002-10-26 02:09:24 +08:00
|
|
|
# Now make sure we don't interpolate if we use RawConfigParser:
|
|
|
|
cf = ConfigParser.RawConfigParser({"getname": "%(__name__)s"})
|
|
|
|
sio = StringIO.StringIO(src)
|
|
|
|
cf.readfp(sio)
|
|
|
|
verify(cf.get("Foo", "getname") == "%(__name__)s")
|
|
|
|
verify(cf.get("Foo", "bar")
|
|
|
|
== "something %(with1)s interpolation (1 step)")
|
|
|
|
verify(cf.get("Foo", "bar9")
|
|
|
|
== "something %(with9)s lots of interpolation (9 steps)")
|
|
|
|
verify(cf.get("Foo", "bar10")
|
|
|
|
== "something %(with10)s lots of interpolation (10 steps)")
|
|
|
|
verify(cf.get("Foo", "bar11")
|
|
|
|
== "something %(with11)s lots of interpolation (11 steps)")
|
|
|
|
|
2001-02-13 01:23:20 +08:00
|
|
|
|
2000-09-28 06:45:25 +08:00
|
|
|
def parse_errors():
|
2001-02-13 01:23:20 +08:00
|
|
|
print "Testing parse errors..."
|
2000-09-28 06:45:25 +08:00
|
|
|
expect_parse_error(ConfigParser.ParsingError,
|
|
|
|
"""[Foo]\n extra-spaces: splat\n""")
|
|
|
|
expect_parse_error(ConfigParser.ParsingError,
|
|
|
|
"""[Foo]\n extra-spaces= splat\n""")
|
|
|
|
expect_parse_error(ConfigParser.ParsingError,
|
|
|
|
"""[Foo]\noption-without-value\n""")
|
|
|
|
expect_parse_error(ConfigParser.ParsingError,
|
|
|
|
"""[Foo]\n:value-without-option-name\n""")
|
|
|
|
expect_parse_error(ConfigParser.ParsingError,
|
|
|
|
"""[Foo]\n=value-without-option-name\n""")
|
|
|
|
expect_parse_error(ConfigParser.MissingSectionHeaderError,
|
|
|
|
"""No Section!\n""")
|
|
|
|
|
2001-02-13 01:23:20 +08:00
|
|
|
|
2000-09-28 06:45:25 +08:00
|
|
|
def query_errors():
|
|
|
|
print "Testing query interface..."
|
|
|
|
cf = ConfigParser.ConfigParser()
|
2001-02-13 01:23:20 +08:00
|
|
|
verify(cf.sections() == [],
|
|
|
|
"new ConfigParser should have no defined sections")
|
|
|
|
verify(not cf.has_section("Foo"),
|
|
|
|
"new ConfigParser should have no acknowledged sections")
|
2000-09-28 06:45:25 +08:00
|
|
|
try:
|
|
|
|
cf.options("Foo")
|
|
|
|
except ConfigParser.NoSectionError, e:
|
2001-02-13 01:23:20 +08:00
|
|
|
pass
|
2000-09-28 06:45:25 +08:00
|
|
|
else:
|
2001-02-13 01:23:20 +08:00
|
|
|
raise TestFailed(
|
|
|
|
"Failed to catch expected NoSectionError from options()")
|
2000-09-28 06:45:25 +08:00
|
|
|
try:
|
|
|
|
cf.set("foo", "bar", "value")
|
|
|
|
except ConfigParser.NoSectionError, e:
|
2001-02-13 01:23:20 +08:00
|
|
|
pass
|
2000-09-28 06:45:25 +08:00
|
|
|
else:
|
2001-02-13 01:23:20 +08:00
|
|
|
raise TestFailed("Failed to catch expected NoSectionError from set()")
|
2000-09-28 06:45:25 +08:00
|
|
|
expect_get_error(cf, ConfigParser.NoSectionError, "foo", "bar")
|
|
|
|
cf.add_section("foo")
|
|
|
|
expect_get_error(cf, ConfigParser.NoOptionError, "foo", "bar")
|
|
|
|
|
2001-02-13 01:23:20 +08:00
|
|
|
|
2000-09-28 06:45:25 +08:00
|
|
|
def weird_errors():
|
|
|
|
print "Testing miscellaneous error conditions..."
|
|
|
|
cf = ConfigParser.ConfigParser()
|
|
|
|
cf.add_section("Foo")
|
|
|
|
try:
|
|
|
|
cf.add_section("Foo")
|
|
|
|
except ConfigParser.DuplicateSectionError, e:
|
2001-02-13 01:23:20 +08:00
|
|
|
pass
|
2000-09-28 06:45:25 +08:00
|
|
|
else:
|
2001-02-13 01:23:20 +08:00
|
|
|
raise TestFailed("Failed to catch expected DuplicateSectionError")
|
|
|
|
|
2000-09-28 06:45:25 +08:00
|
|
|
|
|
|
|
def expect_get_error(cf, exctype, section, option, raw=0):
|
|
|
|
try:
|
|
|
|
cf.get(section, option, raw=raw)
|
|
|
|
except exctype, e:
|
2001-02-13 01:23:20 +08:00
|
|
|
pass
|
2000-09-28 06:45:25 +08:00
|
|
|
else:
|
2001-02-13 01:23:20 +08:00
|
|
|
raise TestFailed("Failed to catch expected " + exctype.__name__)
|
|
|
|
|
2000-09-28 06:45:25 +08:00
|
|
|
|
|
|
|
def expect_parse_error(exctype, src):
|
|
|
|
cf = ConfigParser.ConfigParser()
|
|
|
|
sio = StringIO.StringIO(src)
|
|
|
|
try:
|
|
|
|
cf.readfp(sio)
|
|
|
|
except exctype, e:
|
2001-02-13 01:23:20 +08:00
|
|
|
pass
|
2000-09-28 06:45:25 +08:00
|
|
|
else:
|
2001-02-13 01:23:20 +08:00
|
|
|
raise TestFailed("Failed to catch expected " + exctype.__name__)
|
|
|
|
|
2000-09-28 06:45:25 +08:00
|
|
|
|
2002-10-26 02:09:24 +08:00
|
|
|
# The long string literals present in the rest of the file screw up
|
|
|
|
# font-lock in Emacs/XEmacs, so this stuff needs to stay near the end
|
|
|
|
# of this file.
|
|
|
|
|
|
|
|
def write(src):
|
|
|
|
print "Testing writing of files..."
|
|
|
|
cf = ConfigParser.ConfigParser()
|
|
|
|
sio = StringIO.StringIO(src)
|
|
|
|
cf.readfp(sio)
|
|
|
|
output = StringIO.StringIO()
|
|
|
|
cf.write(output)
|
|
|
|
verify(output, """[DEFAULT]
|
|
|
|
foo = another very
|
|
|
|
long line
|
|
|
|
|
|
|
|
[Long Line]
|
|
|
|
foo = this line is much, much longer than my editor
|
|
|
|
likes it.
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
2000-09-28 06:45:25 +08:00
|
|
|
basic(r"""
|
|
|
|
[Foo Bar]
|
|
|
|
foo=bar
|
|
|
|
[Spacey Bar]
|
2000-10-24 01:22:08 +08:00
|
|
|
foo = bar
|
2000-09-28 06:45:25 +08:00
|
|
|
[Commented Bar]
|
|
|
|
foo: bar ; comment
|
2002-03-09 02:10:12 +08:00
|
|
|
[Long Line]
|
|
|
|
foo: this line is much, much longer than my editor
|
|
|
|
likes it.
|
2001-02-14 23:30:31 +08:00
|
|
|
[Section\with$weird%characters[""" '\t' r"""]
|
2001-02-13 01:23:20 +08:00
|
|
|
[Internationalized Stuff]
|
|
|
|
foo[bg]: Bulgarian
|
|
|
|
foo=Default
|
|
|
|
foo[en]=English
|
|
|
|
foo[de]=Deutsch
|
2002-09-28 00:21:18 +08:00
|
|
|
[Spaces]
|
|
|
|
key with spaces : value
|
|
|
|
another with spaces = splat!
|
2000-09-28 06:45:25 +08:00
|
|
|
""")
|
2002-03-09 02:10:12 +08:00
|
|
|
write("""[Long Line]
|
|
|
|
foo: this line is much, much longer than my editor
|
|
|
|
likes it.
|
|
|
|
[DEFAULT]
|
|
|
|
foo: another very
|
|
|
|
long line""")
|
2001-02-27 05:55:34 +08:00
|
|
|
case_sensitivity()
|
2001-10-09 01:13:12 +08:00
|
|
|
boolean(r"""
|
|
|
|
[BOOLTEST]
|
|
|
|
T1=1
|
|
|
|
T2=TRUE
|
|
|
|
T3=True
|
|
|
|
T4=oN
|
|
|
|
T5=yes
|
|
|
|
F1=0
|
|
|
|
F2=FALSE
|
|
|
|
F3=False
|
|
|
|
F4=oFF
|
|
|
|
F5=nO
|
|
|
|
E1=2
|
|
|
|
E2=foo
|
|
|
|
E3=-1
|
|
|
|
E4=0.1
|
|
|
|
E5=FALSE AND MORE
|
|
|
|
""")
|
2000-09-28 06:45:25 +08:00
|
|
|
interpolation(r"""
|
|
|
|
[Foo]
|
|
|
|
bar=something %(with1)s interpolation (1 step)
|
|
|
|
bar9=something %(with9)s lots of interpolation (9 steps)
|
|
|
|
bar10=something %(with10)s lots of interpolation (10 steps)
|
|
|
|
bar11=something %(with11)s lots of interpolation (11 steps)
|
|
|
|
with11=%(with10)s
|
|
|
|
with10=%(with9)s
|
|
|
|
with9=%(with8)s
|
|
|
|
with8=%(with7)s
|
|
|
|
with7=%(with6)s
|
|
|
|
with6=%(with5)s
|
|
|
|
with5=%(with4)s
|
|
|
|
with4=%(with3)s
|
|
|
|
with3=%(with2)s
|
|
|
|
with2=%(with1)s
|
|
|
|
with1=with
|
|
|
|
|
|
|
|
[Mutual Recursion]
|
|
|
|
foo=%(bar)s
|
|
|
|
bar=%(foo)s
|
|
|
|
""")
|
|
|
|
parse_errors()
|
|
|
|
query_errors()
|
|
|
|
weird_errors()
|