]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
ConfigParser:
authorFred Drake <fdrake@acm.org>
Tue, 18 May 2004 03:29:52 +0000 (03:29 +0000)
committerFred Drake <fdrake@acm.org>
Tue, 18 May 2004 03:29:52 +0000 (03:29 +0000)
- don't allow setting options to non-string values; raise TypeError
  when the value is set, instead of raising an arbitrary exception
  later (such as when string interpolation is performed)
- add tests, documentation
(closes SF bug #810843)

Doc/lib/libcfgparser.tex
Lib/ConfigParser.py
Lib/test/test_cfgparser.py

index a64ae5300723e1f3984c9b6fa626fab9341b9e97..c67c52dd8c506eae6e0e533c17a4e1d99826d9df 100644 (file)
@@ -238,7 +238,9 @@ option in the given \var{section}.
 
 \begin{methoddesc}{set}{section, option, value}
 If the given section exists, set the given option to the specified value;
-otherwise raise \exception{NoSectionError}.
+otherwise raise \exception{NoSectionError}.  \var{value} must be a
+string (\class{str} or \class{unicode}); if not, \exception{TypeError}
+is raised.
 \versionadded{1.6}
 \end{methoddesc}
 
index 73acdd15ad0b61ba1698757caa559d34f8cc5b8b..fccfcdfea644f58930c6615edd11b4c39eab2694 100644 (file)
@@ -343,6 +343,8 @@ class RawConfigParser:
 
     def set(self, section, option, value):
         """Set an option."""
+        if not isinstance(value, basestring):
+            raise TypeError("option values must be strings")
         if not section or section == DEFAULTSECT:
             sectdict = self._defaults
         else:
index 28063b55893feca0b267d90b1a13d41e8df00984..b40cedf4ab17b50556f9f4ea73895d33be33e44e 100644 (file)
@@ -211,6 +211,37 @@ class TestCaseBase(unittest.TestCase):
             "\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_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())
+
     # shared by subclasses
     def get_interpolation_config(self):
         return self.fromstring(