@@ -1235,6 +1235,59 @@ def fromstring(self, string, defaults=None):
12351235 del section [default ]
12361236 return cf_copy
12371237
1238+
1239+ class FakeFile :
1240+ def __init__ (self ):
1241+ file_path = support .findfile ("cfgparser.1" )
1242+ with open (file_path ) as f :
1243+ self .lines = f .readlines ()
1244+ self .lines .reverse ()
1245+
1246+ def readline (self ):
1247+ if len (self .lines ):
1248+ return self .lines .pop ()
1249+ return ''
1250+
1251+
1252+ def readline_generator (f ):
1253+ """As advised in Doc/library/configparser.rst."""
1254+ line = f .readline ()
1255+ while line != '' :
1256+ yield line
1257+ line = f .readline ()
1258+
1259+
1260+ class ReadFileTestCase (unittest .TestCase ):
1261+ def test_file (self ):
1262+ file_path = support .findfile ("cfgparser.1" )
1263+ parser = configparser .ConfigParser ()
1264+ with open (file_path ) as f :
1265+ parser .read_file (f )
1266+ self .assertTrue ("Foo Bar" in parser )
1267+ self .assertTrue ("foo" in parser ["Foo Bar" ])
1268+ self .assertEqual (parser ["Foo Bar" ]["foo" ], "newbar" )
1269+
1270+ def test_iterable (self ):
1271+ lines = textwrap .dedent ("""
1272+ [Foo Bar]
1273+ foo=newbar""" ).strip ().split ('\n ' )
1274+ parser = configparser .ConfigParser ()
1275+ parser .read_file (lines )
1276+ self .assertTrue ("Foo Bar" in parser )
1277+ self .assertTrue ("foo" in parser ["Foo Bar" ])
1278+ self .assertEqual (parser ["Foo Bar" ]["foo" ], "newbar" )
1279+
1280+ def test_readline_generator (self ):
1281+ """Issue #11670."""
1282+ parser = configparser .ConfigParser ()
1283+ with self .assertRaises (TypeError ):
1284+ parser .read_file (FakeFile ())
1285+ parser .read_file (readline_generator (FakeFile ()))
1286+ self .assertTrue ("Foo Bar" in parser )
1287+ self .assertTrue ("foo" in parser ["Foo Bar" ])
1288+ self .assertEqual (parser ["Foo Bar" ]["foo" ], "newbar" )
1289+
1290+
12381291class CoverageOneHundredTestCase (unittest .TestCase ):
12391292 """Covers edge cases in the codebase."""
12401293
@@ -1338,5 +1391,6 @@ def test_main():
13381391 CompatibleTestCase ,
13391392 CopyTestCase ,
13401393 ConfigParserTestCaseNonStandardDefaultSection ,
1394+ ReadFileTestCase ,
13411395 CoverageOneHundredTestCase ,
13421396 )
0 commit comments