]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix typo and add test case.
authorFlorent Xicluna <florent.xicluna@gmail.com>
Wed, 22 Sep 2010 22:35:38 +0000 (22:35 +0000)
committerFlorent Xicluna <florent.xicluna@gmail.com>
Wed, 22 Sep 2010 22:35:38 +0000 (22:35 +0000)
Lib/configparser.py
Lib/test/test_cfgparser.py

index 7f1514f902077cdbb1abe83ed730a05d2527641a..03d6713097cada1fa11cf8ddc2515e79aab77a87 100644 (file)
@@ -493,11 +493,10 @@ class RawConfigParser:
         read_ok = []
         for filename in filenames:
             try:
-                fp = open(filename, encoding=encoding)
+                with open(filename, encoding=encoding) as fp:
+                    self._read(fp, filename)
             except IOError:
                 continue
-            self._read(fp, filename)
-            fp.close()
             read_ok.append(filename)
         return read_ok
 
@@ -511,7 +510,7 @@ class RawConfigParser:
         """
         if source is None:
             try:
-                srouce = f.name
+                source = f.name
             except AttributeError:
                 source = '<???>'
         self._read(f, source)
index 3079cfb80ce6bbdd74b2a5d46752d4b84ae72106..ad9b62a2bc623b97acab4fbd8feebc2ae45ef830 100644 (file)
@@ -328,9 +328,24 @@ boolean {0[0]} NO
             e = self.parse_error(cf, configparser.ParsingError,
                                 "[Foo]\n  wrong-indent\n")
             self.assertEqual(e.args, ('<???>',))
+            # read_file on a real file
+            tricky = support.findfile("cfgparser.3")
+            if self.delimiters[0] == '=':
+                error = configparser.ParsingError
+                expected = (tricky,)
+            else:
+                error = configparser.MissingSectionHeaderError
+                expected = (tricky, 1,
+                            '  # INI with as many tricky parts as possible\n')
+            with open(tricky) as f:
+                e = self.parse_error(cf, error, f)
+            self.assertEqual(e.args, expected)
 
     def parse_error(self, cf, exc, src):
-        sio = io.StringIO(src)
+        if hasattr(src, 'readline'):
+            sio = src
+        else:
+            sio = io.StringIO(src)
         with self.assertRaises(exc) as cm:
             cf.read_file(sio)
         return cm.exception