summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_cfgparser.py
diff options
context:
space:
mode:
authorŁukasz Langa <lukasz@langa.pl>2011-04-27 16:10:05 (GMT)
committerŁukasz Langa <lukasz@langa.pl>2011-04-27 16:10:05 (GMT)
commitdaab1c80928108a3b2ddf19c0245fe15af1b1fd3 (patch)
treea45cada2685b2b8067b975d31649e8d294d60509 /Lib/test/test_cfgparser.py
parent944d16c6c490f5e3933f8a729d810f60d3bab1d3 (diff)
downloadcpython-daab1c80928108a3b2ddf19c0245fe15af1b1fd3.zip
cpython-daab1c80928108a3b2ddf19c0245fe15af1b1fd3.tar.gz
cpython-daab1c80928108a3b2ddf19c0245fe15af1b1fd3.tar.bz2
Closes #11670: configparser read_file now iterates over f.
Diffstat (limited to 'Lib/test/test_cfgparser.py')
-rw-r--r--Lib/test/test_cfgparser.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/Lib/test/test_cfgparser.py b/Lib/test/test_cfgparser.py
index f7d9a26..a29da93 100644
--- a/Lib/test/test_cfgparser.py
+++ b/Lib/test/test_cfgparser.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,
)