mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Merged #11670 from 3.2
This commit is contained in:
commit
29050d7317
3 changed files with 82 additions and 9 deletions
|
@ -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,
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue