mirror of
https://github.com/python/cpython.git
synced 2025-07-31 15:14:22 +00:00

svn+ssh://pythondev@svn.python.org/python/trunk ........ r53545 | andrew.kuchling | 2007-01-24 21:06:41 +0100 (Wed, 24 Jan 2007) | 1 line Strengthen warning about using lock() ........ r53556 | thomas.heller | 2007-01-25 19:34:14 +0100 (Thu, 25 Jan 2007) | 3 lines Fix for #1643874: When calling SysAllocString, create a PyCObject which will eventually call SysFreeString to free the BSTR resource. ........ r53563 | andrew.kuchling | 2007-01-25 21:02:13 +0100 (Thu, 25 Jan 2007) | 1 line Add item ........ r53564 | brett.cannon | 2007-01-25 21:22:02 +0100 (Thu, 25 Jan 2007) | 8 lines Fix time.strptime's %U support. Basically rewrote the algorithm to be more generic so that one only has to shift certain values based on whether the week was specified to start on Monday or Sunday. Cut out a lot of edge case code compared to the previous version. Also broke algorithm out into its own function (that is private to the module). Fixes bug #1643943 (thanks Biran Nahas for the report). ........ r53570 | brett.cannon | 2007-01-26 00:30:39 +0100 (Fri, 26 Jan 2007) | 4 lines Remove specific mention of my name and email address from modules. Not really needed and all bug reports should go to the bug tracker, not directly to me. Plus I am not the only person to have edited these files at this point. ........ r53573 | fred.drake | 2007-01-26 17:28:44 +0100 (Fri, 26 Jan 2007) | 1 line fix typo (extraneous ")") ........ r53575 | georg.brandl | 2007-01-27 18:43:02 +0100 (Sat, 27 Jan 2007) | 4 lines Patch #1638243: the compiler package is now able to correctly compile a with statement; previously, executing code containing a with statement compiled by the compiler package crashed the interpreter. ........ r53578 | georg.brandl | 2007-01-27 18:59:42 +0100 (Sat, 27 Jan 2007) | 3 lines Patch #1634778: add missing encoding aliases for iso8859_15 and iso8859_16. ........ r53579 | georg.brandl | 2007-01-27 20:38:50 +0100 (Sat, 27 Jan 2007) | 2 lines Bug #1645944: os.access now returns bool but docstring is not updated ........ r53590 | brett.cannon | 2007-01-28 21:58:00 +0100 (Sun, 28 Jan 2007) | 2 lines Use the thread lock's context manager instead of a try/finally statement. ........ r53591 | brett.cannon | 2007-01-29 05:41:44 +0100 (Mon, 29 Jan 2007) | 2 lines Add a test for slicing an exception. ........ r53594 | andrew.kuchling | 2007-01-29 21:21:43 +0100 (Mon, 29 Jan 2007) | 1 line Minor edits to the curses HOWTO ........ r53596 | andrew.kuchling | 2007-01-29 21:55:40 +0100 (Mon, 29 Jan 2007) | 1 line Various minor edits ........ r53597 | andrew.kuchling | 2007-01-29 22:28:48 +0100 (Mon, 29 Jan 2007) | 1 line More edits ........ r53601 | tim.peters | 2007-01-30 04:03:46 +0100 (Tue, 30 Jan 2007) | 2 lines Whitespace normalization. ........ r53603 | georg.brandl | 2007-01-30 21:21:30 +0100 (Tue, 30 Jan 2007) | 2 lines Bug #1648191: typo in docs. ........ r53605 | brett.cannon | 2007-01-30 22:34:36 +0100 (Tue, 30 Jan 2007) | 8 lines No more raising of string exceptions! The next step of PEP 352 (for 2.6) causes raising a string exception to trigger a TypeError. Trying to catch a string exception raises a DeprecationWarning. References to string exceptions has been removed from the docs since they are now just an error. ........ r53618 | raymond.hettinger | 2007-02-01 22:02:59 +0100 (Thu, 01 Feb 2007) | 1 line Bug #1648179: set.update() not recognizing __iter__ overrides in dict subclasses. ........
470 lines
17 KiB
Python
470 lines
17 KiB
Python
import ConfigParser
|
|
import StringIO
|
|
import unittest
|
|
import UserDict
|
|
|
|
from test import test_support
|
|
|
|
class SortedDict(UserDict.UserDict):
|
|
def items(self):
|
|
result = self.data.items()
|
|
result.sort()
|
|
return result
|
|
|
|
def keys(self):
|
|
result = self.data.keys()
|
|
result.sort()
|
|
return result
|
|
|
|
def values(self):
|
|
result = self.items()
|
|
return [i[1] for i in values]
|
|
|
|
def iteritems(self): return iter(self.items())
|
|
def iterkeys(self): return iter(self.keys())
|
|
__iter__ = iterkeys
|
|
def itervalues(self): return iter(self.values())
|
|
|
|
class TestCaseBase(unittest.TestCase):
|
|
def newconfig(self, defaults=None):
|
|
if defaults is None:
|
|
self.cf = self.config_class()
|
|
else:
|
|
self.cf = self.config_class(defaults)
|
|
return self.cf
|
|
|
|
def fromstring(self, string, defaults=None):
|
|
cf = self.newconfig(defaults)
|
|
sio = StringIO.StringIO(string)
|
|
cf.readfp(sio)
|
|
return cf
|
|
|
|
def test_basic(self):
|
|
cf = self.fromstring(
|
|
"[Foo Bar]\n"
|
|
"foo=bar\n"
|
|
"[Spacey Bar]\n"
|
|
"foo = bar\n"
|
|
"[Commented Bar]\n"
|
|
"foo: bar ; comment\n"
|
|
"[Long Line]\n"
|
|
"foo: this line is much, much longer than my editor\n"
|
|
" likes it.\n"
|
|
"[Section\\with$weird%characters[\t]\n"
|
|
"[Internationalized Stuff]\n"
|
|
"foo[bg]: Bulgarian\n"
|
|
"foo=Default\n"
|
|
"foo[en]=English\n"
|
|
"foo[de]=Deutsch\n"
|
|
"[Spaces]\n"
|
|
"key with spaces : value\n"
|
|
"another with spaces = splat!\n"
|
|
)
|
|
L = cf.sections()
|
|
L.sort()
|
|
eq = self.assertEqual
|
|
eq(L, [r'Commented Bar',
|
|
r'Foo Bar',
|
|
r'Internationalized Stuff',
|
|
r'Long Line',
|
|
r'Section\with$weird%characters[' '\t',
|
|
r'Spaces',
|
|
r'Spacey Bar',
|
|
])
|
|
|
|
# The use of spaces in the section names serves as a
|
|
# regression test for SourceForge bug #583248:
|
|
# http://www.python.org/sf/583248
|
|
eq(cf.get('Foo Bar', 'foo'), 'bar')
|
|
eq(cf.get('Spacey Bar', 'foo'), 'bar')
|
|
eq(cf.get('Commented Bar', 'foo'), 'bar')
|
|
eq(cf.get('Spaces', 'key with spaces'), 'value')
|
|
eq(cf.get('Spaces', 'another with spaces'), 'splat!')
|
|
|
|
self.failIf('__name__' in cf.options("Foo Bar"),
|
|
'__name__ "option" should not be exposed by the API!')
|
|
|
|
# Make sure the right things happen for remove_option();
|
|
# added to include check for SourceForge bug #123324:
|
|
self.failUnless(cf.remove_option('Foo Bar', 'foo'),
|
|
"remove_option() failed to report existance of option")
|
|
self.failIf(cf.has_option('Foo Bar', 'foo'),
|
|
"remove_option() failed to remove option")
|
|
self.failIf(cf.remove_option('Foo Bar', 'foo'),
|
|
"remove_option() failed to report non-existance of option"
|
|
" that was removed")
|
|
|
|
self.assertRaises(ConfigParser.NoSectionError,
|
|
cf.remove_option, 'No Such Section', 'foo')
|
|
|
|
eq(cf.get('Long Line', 'foo'),
|
|
'this line is much, much longer than my editor\nlikes it.')
|
|
|
|
def test_case_sensitivity(self):
|
|
cf = self.newconfig()
|
|
cf.add_section("A")
|
|
cf.add_section("a")
|
|
L = cf.sections()
|
|
L.sort()
|
|
eq = self.assertEqual
|
|
eq(L, ["A", "a"])
|
|
cf.set("a", "B", "value")
|
|
eq(cf.options("a"), ["b"])
|
|
eq(cf.get("a", "b"), "value",
|
|
"could not locate option, expecting case-insensitive option names")
|
|
self.failUnless(cf.has_option("a", "b"))
|
|
cf.set("A", "A-B", "A-B value")
|
|
for opt in ("a-b", "A-b", "a-B", "A-B"):
|
|
self.failUnless(
|
|
cf.has_option("A", opt),
|
|
"has_option() returned false for option which should exist")
|
|
eq(cf.options("A"), ["a-b"])
|
|
eq(cf.options("a"), ["b"])
|
|
cf.remove_option("a", "B")
|
|
eq(cf.options("a"), [])
|
|
|
|
# SF bug #432369:
|
|
cf = self.fromstring(
|
|
"[MySection]\nOption: first line\n\tsecond line\n")
|
|
eq(cf.options("MySection"), ["option"])
|
|
eq(cf.get("MySection", "Option"), "first line\nsecond line")
|
|
|
|
# SF bug #561822:
|
|
cf = self.fromstring("[section]\nnekey=nevalue\n",
|
|
defaults={"key":"value"})
|
|
self.failUnless(cf.has_option("section", "Key"))
|
|
|
|
|
|
def test_default_case_sensitivity(self):
|
|
cf = self.newconfig({"foo": "Bar"})
|
|
self.assertEqual(
|
|
cf.get("DEFAULT", "Foo"), "Bar",
|
|
"could not locate option, expecting case-insensitive option names")
|
|
cf = self.newconfig({"Foo": "Bar"})
|
|
self.assertEqual(
|
|
cf.get("DEFAULT", "Foo"), "Bar",
|
|
"could not locate option, expecting case-insensitive defaults")
|
|
|
|
def test_parse_errors(self):
|
|
self.newconfig()
|
|
self.parse_error(ConfigParser.ParsingError,
|
|
"[Foo]\n extra-spaces: splat\n")
|
|
self.parse_error(ConfigParser.ParsingError,
|
|
"[Foo]\n extra-spaces= splat\n")
|
|
self.parse_error(ConfigParser.ParsingError,
|
|
"[Foo]\noption-without-value\n")
|
|
self.parse_error(ConfigParser.ParsingError,
|
|
"[Foo]\n:value-without-option-name\n")
|
|
self.parse_error(ConfigParser.ParsingError,
|
|
"[Foo]\n=value-without-option-name\n")
|
|
self.parse_error(ConfigParser.MissingSectionHeaderError,
|
|
"No Section!\n")
|
|
|
|
def parse_error(self, exc, src):
|
|
sio = StringIO.StringIO(src)
|
|
self.assertRaises(exc, self.cf.readfp, sio)
|
|
|
|
def test_query_errors(self):
|
|
cf = self.newconfig()
|
|
self.assertEqual(cf.sections(), [],
|
|
"new ConfigParser should have no defined sections")
|
|
self.failIf(cf.has_section("Foo"),
|
|
"new ConfigParser should have no acknowledged sections")
|
|
self.assertRaises(ConfigParser.NoSectionError,
|
|
cf.options, "Foo")
|
|
self.assertRaises(ConfigParser.NoSectionError,
|
|
cf.set, "foo", "bar", "value")
|
|
self.get_error(ConfigParser.NoSectionError, "foo", "bar")
|
|
cf.add_section("foo")
|
|
self.get_error(ConfigParser.NoOptionError, "foo", "bar")
|
|
|
|
def get_error(self, exc, section, option):
|
|
try:
|
|
self.cf.get(section, option)
|
|
except exc as e:
|
|
return e
|
|
else:
|
|
self.fail("expected exception type %s.%s"
|
|
% (exc.__module__, exc.__name__))
|
|
|
|
def test_boolean(self):
|
|
cf = self.fromstring(
|
|
"[BOOLTEST]\n"
|
|
"T1=1\n"
|
|
"T2=TRUE\n"
|
|
"T3=True\n"
|
|
"T4=oN\n"
|
|
"T5=yes\n"
|
|
"F1=0\n"
|
|
"F2=FALSE\n"
|
|
"F3=False\n"
|
|
"F4=oFF\n"
|
|
"F5=nO\n"
|
|
"E1=2\n"
|
|
"E2=foo\n"
|
|
"E3=-1\n"
|
|
"E4=0.1\n"
|
|
"E5=FALSE AND MORE"
|
|
)
|
|
for x in range(1, 5):
|
|
self.failUnless(cf.getboolean('BOOLTEST', 't%d' % x))
|
|
self.failIf(cf.getboolean('BOOLTEST', 'f%d' % x))
|
|
self.assertRaises(ValueError,
|
|
cf.getboolean, 'BOOLTEST', 'e%d' % x)
|
|
|
|
def test_weird_errors(self):
|
|
cf = self.newconfig()
|
|
cf.add_section("Foo")
|
|
self.assertRaises(ConfigParser.DuplicateSectionError,
|
|
cf.add_section, "Foo")
|
|
|
|
def test_write(self):
|
|
cf = self.fromstring(
|
|
"[Long Line]\n"
|
|
"foo: this line is much, much longer than my editor\n"
|
|
" likes it.\n"
|
|
"[DEFAULT]\n"
|
|
"foo: another very\n"
|
|
" long line"
|
|
)
|
|
output = StringIO.StringIO()
|
|
cf.write(output)
|
|
self.assertEqual(
|
|
output.getvalue(),
|
|
"[DEFAULT]\n"
|
|
"foo = another very\n"
|
|
"\tlong line\n"
|
|
"\n"
|
|
"[Long Line]\n"
|
|
"foo = this line is much, much longer than my editor\n"
|
|
"\tlikes it.\n"
|
|
"\n"
|
|
)
|
|
|
|
def test_set_string_types(self):
|
|
cf = self.fromstring("[sect]\n"
|
|
"option1=foo\n")
|
|
# Check that we don't get an exception when setting values in
|
|
# an existing section using strings:
|
|
class mystr(str):
|
|
pass
|
|
cf.set("sect", "option1", "splat")
|
|
cf.set("sect", "option1", mystr("splat"))
|
|
cf.set("sect", "option2", "splat")
|
|
cf.set("sect", "option2", mystr("splat"))
|
|
try:
|
|
unicode
|
|
except NameError:
|
|
pass
|
|
else:
|
|
cf.set("sect", "option1", unicode("splat"))
|
|
cf.set("sect", "option2", unicode("splat"))
|
|
|
|
def test_read_returns_file_list(self):
|
|
file1 = test_support.findfile("cfgparser.1")
|
|
# check when we pass a mix of readable and non-readable files:
|
|
cf = self.newconfig()
|
|
parsed_files = cf.read([file1, "nonexistant-file"])
|
|
self.assertEqual(parsed_files, [file1])
|
|
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
|
|
# check when we pass only a filename:
|
|
cf = self.newconfig()
|
|
parsed_files = cf.read(file1)
|
|
self.assertEqual(parsed_files, [file1])
|
|
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
|
|
# check when we pass only missing files:
|
|
cf = self.newconfig()
|
|
parsed_files = cf.read(["nonexistant-file"])
|
|
self.assertEqual(parsed_files, [])
|
|
# check when we pass no files:
|
|
cf = self.newconfig()
|
|
parsed_files = cf.read([])
|
|
self.assertEqual(parsed_files, [])
|
|
|
|
# shared by subclasses
|
|
def get_interpolation_config(self):
|
|
return self.fromstring(
|
|
"[Foo]\n"
|
|
"bar=something %(with1)s interpolation (1 step)\n"
|
|
"bar9=something %(with9)s lots of interpolation (9 steps)\n"
|
|
"bar10=something %(with10)s lots of interpolation (10 steps)\n"
|
|
"bar11=something %(with11)s lots of interpolation (11 steps)\n"
|
|
"with11=%(with10)s\n"
|
|
"with10=%(with9)s\n"
|
|
"with9=%(with8)s\n"
|
|
"with8=%(With7)s\n"
|
|
"with7=%(WITH6)s\n"
|
|
"with6=%(with5)s\n"
|
|
"With5=%(with4)s\n"
|
|
"WITH4=%(with3)s\n"
|
|
"with3=%(with2)s\n"
|
|
"with2=%(with1)s\n"
|
|
"with1=with\n"
|
|
"\n"
|
|
"[Mutual Recursion]\n"
|
|
"foo=%(bar)s\n"
|
|
"bar=%(foo)s\n"
|
|
"\n"
|
|
"[Interpolation Error]\n"
|
|
"name=%(reference)s\n",
|
|
# no definition for 'reference'
|
|
defaults={"getname": "%(__name__)s"})
|
|
|
|
def check_items_config(self, expected):
|
|
cf = self.fromstring(
|
|
"[section]\n"
|
|
"name = value\n"
|
|
"key: |%(name)s| \n"
|
|
"getdefault: |%(default)s|\n"
|
|
"getname: |%(__name__)s|",
|
|
defaults={"default": "<default>"})
|
|
L = list(cf.items("section"))
|
|
L.sort()
|
|
self.assertEqual(L, expected)
|
|
|
|
|
|
class ConfigParserTestCase(TestCaseBase):
|
|
config_class = ConfigParser.ConfigParser
|
|
|
|
def test_interpolation(self):
|
|
cf = self.get_interpolation_config()
|
|
eq = self.assertEqual
|
|
eq(cf.get("Foo", "getname"), "Foo")
|
|
eq(cf.get("Foo", "bar"), "something with interpolation (1 step)")
|
|
eq(cf.get("Foo", "bar9"),
|
|
"something with lots of interpolation (9 steps)")
|
|
eq(cf.get("Foo", "bar10"),
|
|
"something with lots of interpolation (10 steps)")
|
|
self.get_error(ConfigParser.InterpolationDepthError, "Foo", "bar11")
|
|
|
|
def test_interpolation_missing_value(self):
|
|
cf = self.get_interpolation_config()
|
|
e = self.get_error(ConfigParser.InterpolationError,
|
|
"Interpolation Error", "name")
|
|
self.assertEqual(e.reference, "reference")
|
|
self.assertEqual(e.section, "Interpolation Error")
|
|
self.assertEqual(e.option, "name")
|
|
|
|
def test_items(self):
|
|
self.check_items_config([('default', '<default>'),
|
|
('getdefault', '|<default>|'),
|
|
('getname', '|section|'),
|
|
('key', '|value|'),
|
|
('name', 'value')])
|
|
|
|
def test_set_nonstring_types(self):
|
|
cf = self.newconfig()
|
|
cf.add_section('non-string')
|
|
cf.set('non-string', 'int', 1)
|
|
cf.set('non-string', 'list', [0, 1, 1, 2, 3, 5, 8, 13, '%('])
|
|
cf.set('non-string', 'dict', {'pi': 3.14159, '%(': 1,
|
|
'%(list)': '%(list)'})
|
|
cf.set('non-string', 'string_with_interpolation', '%(list)s')
|
|
self.assertEqual(cf.get('non-string', 'int', raw=True), 1)
|
|
self.assertRaises(TypeError, cf.get, 'non-string', 'int')
|
|
self.assertEqual(cf.get('non-string', 'list', raw=True),
|
|
[0, 1, 1, 2, 3, 5, 8, 13, '%('])
|
|
self.assertRaises(TypeError, cf.get, 'non-string', 'list')
|
|
self.assertEqual(cf.get('non-string', 'dict', raw=True),
|
|
{'pi': 3.14159, '%(': 1, '%(list)': '%(list)'})
|
|
self.assertRaises(TypeError, cf.get, 'non-string', 'dict')
|
|
self.assertEqual(cf.get('non-string', 'string_with_interpolation',
|
|
raw=True), '%(list)s')
|
|
self.assertRaises(ValueError, cf.get, 'non-string',
|
|
'string_with_interpolation', raw=False)
|
|
|
|
|
|
class RawConfigParserTestCase(TestCaseBase):
|
|
config_class = ConfigParser.RawConfigParser
|
|
|
|
def test_interpolation(self):
|
|
cf = self.get_interpolation_config()
|
|
eq = self.assertEqual
|
|
eq(cf.get("Foo", "getname"), "%(__name__)s")
|
|
eq(cf.get("Foo", "bar"),
|
|
"something %(with1)s interpolation (1 step)")
|
|
eq(cf.get("Foo", "bar9"),
|
|
"something %(with9)s lots of interpolation (9 steps)")
|
|
eq(cf.get("Foo", "bar10"),
|
|
"something %(with10)s lots of interpolation (10 steps)")
|
|
eq(cf.get("Foo", "bar11"),
|
|
"something %(with11)s lots of interpolation (11 steps)")
|
|
|
|
def test_items(self):
|
|
self.check_items_config([('default', '<default>'),
|
|
('getdefault', '|%(default)s|'),
|
|
('getname', '|%(__name__)s|'),
|
|
('key', '|%(name)s|'),
|
|
('name', 'value')])
|
|
|
|
def test_set_nonstring_types(self):
|
|
cf = self.newconfig()
|
|
cf.add_section('non-string')
|
|
cf.set('non-string', 'int', 1)
|
|
cf.set('non-string', 'list', [0, 1, 1, 2, 3, 5, 8, 13])
|
|
cf.set('non-string', 'dict', {'pi': 3.14159})
|
|
self.assertEqual(cf.get('non-string', 'int'), 1)
|
|
self.assertEqual(cf.get('non-string', 'list'),
|
|
[0, 1, 1, 2, 3, 5, 8, 13])
|
|
self.assertEqual(cf.get('non-string', 'dict'), {'pi': 3.14159})
|
|
|
|
|
|
class SafeConfigParserTestCase(ConfigParserTestCase):
|
|
config_class = ConfigParser.SafeConfigParser
|
|
|
|
def test_safe_interpolation(self):
|
|
# See http://www.python.org/sf/511737
|
|
cf = self.fromstring("[section]\n"
|
|
"option1=xxx\n"
|
|
"option2=%(option1)s/xxx\n"
|
|
"ok=%(option1)s/%%s\n"
|
|
"not_ok=%(option2)s/%%s")
|
|
self.assertEqual(cf.get("section", "ok"), "xxx/%s")
|
|
self.assertEqual(cf.get("section", "not_ok"), "xxx/xxx/%s")
|
|
|
|
def test_set_nonstring_types(self):
|
|
cf = self.fromstring("[sect]\n"
|
|
"option1=foo\n")
|
|
# Check that we get a TypeError when setting non-string values
|
|
# in an existing section:
|
|
self.assertRaises(TypeError, cf.set, "sect", "option1", 1)
|
|
self.assertRaises(TypeError, cf.set, "sect", "option1", 1.0)
|
|
self.assertRaises(TypeError, cf.set, "sect", "option1", object())
|
|
self.assertRaises(TypeError, cf.set, "sect", "option2", 1)
|
|
self.assertRaises(TypeError, cf.set, "sect", "option2", 1.0)
|
|
self.assertRaises(TypeError, cf.set, "sect", "option2", object())
|
|
|
|
class SortedTestCase(RawConfigParserTestCase):
|
|
def newconfig(self, defaults=None):
|
|
self.cf = self.config_class(defaults=defaults, dict_type=SortedDict)
|
|
return self.cf
|
|
|
|
def test_sorted(self):
|
|
self.fromstring("[b]\n"
|
|
"o4=1\n"
|
|
"o3=2\n"
|
|
"o2=3\n"
|
|
"o1=4\n"
|
|
"[a]\n"
|
|
"k=v\n")
|
|
output = StringIO.StringIO()
|
|
self.cf.write(output)
|
|
self.assertEquals(output.getvalue(),
|
|
"[a]\n"
|
|
"k = v\n\n"
|
|
"[b]\n"
|
|
"o1 = 4\n"
|
|
"o2 = 3\n"
|
|
"o3 = 2\n"
|
|
"o4 = 1\n\n")
|
|
|
|
def test_main():
|
|
test_support.run_unittest(
|
|
ConfigParserTestCase,
|
|
RawConfigParserTestCase,
|
|
SafeConfigParserTestCase,
|
|
SortedTestCase
|
|
)
|
|
|
|
if __name__ == "__main__":
|
|
test_main()
|