summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorŁukasz Langa <lukasz@langa.pl>2011-04-27 18:11:50 +0200
committerŁukasz Langa <lukasz@langa.pl>2011-04-27 18:11:50 +0200
commit557b3d3d0b36970b9dbf4bc85a742d8f5e44275c (patch)
tree6f21d77c0e74d816b400f0eed9bce5dfb8e64861 /Lib
parent591bd46b5ac995decce97480dd8f05d840adb967 (diff)
parent809900da23d3e77b7adaf2fb046eaf92e0403a68 (diff)
downloadcpython-557b3d3d0b36970b9dbf4bc85a742d8f5e44275c.tar.gz
Merged #11670 from 3.2
Diffstat (limited to 'Lib')
-rw-r--r--Lib/configparser.py8
-rw-r--r--Lib/test/test_configparser.py54
2 files changed, 58 insertions, 4 deletions
diff --git a/Lib/configparser.py b/Lib/configparser.py
index 15fc26677e..fe28b1318c 100644
--- a/Lib/configparser.py
+++ b/Lib/configparser.py
@@ -695,10 +695,10 @@ class RawConfigParser(MutableMapping):
def read_file(self, f, source=None):
"""Like read() but the argument must be a file-like object.
- The `f' argument must have a `readline' method. Optional second
- argument is the `source' specifying the name of the file being read. If
- not given, it is taken from f.name. If `f' has no `name' attribute,
- `<???>' is used.
+ The `f' argument must be iterable, returning one line at a time.
+ Optional second argument is the `source' specifying the name of the
+ file being read. If not given, it is taken from f.name. If `f' has no
+ `name' attribute, `<???>' is used.
"""
if source is None:
try:
diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py
index f7d9a26e89..a29da9388c 100644
--- a/Lib/test/test_configparser.py
+++ b/Lib/test/test_configparser.py
@@ -1235,6 +1235,59 @@ class CopyTestCase(BasicTestCase):
del section[default]
return cf_copy
+
+class FakeFile:
+ def __init__(self):
+ file_path = support.findfile("cfgparser.1")
+ with open(file_path) as f:
+ self.lines = f.readlines()
+ self.lines.reverse()
+
+ def readline(self):
+ if len(self.lines):
+ return self.lines.pop()
+ return ''
+
+
+def readline_generator(f):
+ """As advised in Doc/library/configparser.rst."""
+ line = f.readline()
+ while line != '':
+ yield line
+ line = f.readline()
+
+
+class ReadFileTestCase(unittest.TestCase):
+ def test_file(self):
+ file_path = support.findfile("cfgparser.1")
+ parser = configparser.ConfigParser()
+ with open(file_path) as f:
+ parser.read_file(f)
+ self.assertTrue("Foo Bar" in parser)
+ self.assertTrue("foo" in parser["Foo Bar"])
+ self.assertEqual(parser["Foo Bar"]["foo"], "newbar")
+
+ def test_iterable(self):
+ lines = textwrap.dedent("""
+ [Foo Bar]
+ foo=newbar""").strip().split('\n')
+ parser = configparser.ConfigParser()
+ parser.read_file(lines)
+ self.assertTrue("Foo Bar" in parser)
+ self.assertTrue("foo" in parser["Foo Bar"])
+ self.assertEqual(parser["Foo Bar"]["foo"], "newbar")
+
+ def test_readline_generator(self):
+ """Issue #11670."""
+ parser = configparser.ConfigParser()
+ with self.assertRaises(TypeError):
+ parser.read_file(FakeFile())
+ parser.read_file(readline_generator(FakeFile()))
+ self.assertTrue("Foo Bar" in parser)
+ self.assertTrue("foo" in parser["Foo Bar"])
+ self.assertEqual(parser["Foo Bar"]["foo"], "newbar")
+
+
class CoverageOneHundredTestCase(unittest.TestCase):
"""Covers edge cases in the codebase."""
@@ -1338,5 +1391,6 @@ def test_main():
CompatibleTestCase,
CopyTestCase,
ConfigParserTestCaseNonStandardDefaultSection,
+ ReadFileTestCase,
CoverageOneHundredTestCase,
)