diff options
author | David Goodger <goodger@python.org> | 2004-10-03 15:55:09 (GMT) |
---|---|---|
committer | David Goodger <goodger@python.org> | 2004-10-03 15:55:09 (GMT) |
commit | 1cbf206d322ad1fac12b4bb5a64d501ca021a61d (patch) | |
tree | 31ad42e2d25206665e9e3b381220967b358b87dd /Lib/test | |
parent | 68a1abdadec2377fd3e8d2211cb3cc119de77a28 (diff) | |
download | cpython-1cbf206d322ad1fac12b4bb5a64d501ca021a61d.zip cpython-1cbf206d322ad1fac12b4bb5a64d501ca021a61d.tar.gz cpython-1cbf206d322ad1fac12b4bb5a64d501ca021a61d.tar.bz2 |
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.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_cfgparser.py | 56 |
1 files changed, 44 insertions, 12 deletions
diff --git a/Lib/test/test_cfgparser.py b/Lib/test/test_cfgparser.py index 6b3e68a..36640f1 100644 --- a/Lib/test/test_cfgparser.py +++ b/Lib/test/test_cfgparser.py @@ -240,18 +240,6 @@ class TestCaseBase(unittest.TestCase): cf.set("sect", "option1", unicode("splat")) cf.set("sect", "option2", unicode("splat")) - 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()) - 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: @@ -344,6 +332,27 @@ class ConfigParserTestCase(TestCaseBase): ('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 @@ -368,6 +377,17 @@ class RawConfigParserTestCase(TestCaseBase): ('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 @@ -382,6 +402,18 @@ class SafeConfigParserTestCase(ConfigParserTestCase): 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()) + def test_main(): test_support.run_unittest( |