Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba702da

Browse files
committed
Style updates for the #11670 solution after post-commit review by Ezio Melotti:
http://mail.python.org/pipermail/python-checkins/2011-April/104688.html Thanks!
1 parent c20566c commit ba702da

2 files changed

Lines changed: 24 additions & 30 deletions

File tree

Doc/library/configparser.rst

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ ConfigParser Objects
975975
.. method:: read_file(f, source=None)
976976

977977
Read and parse configuration data from *f* which must be an iterable
978-
yielding Unicode strings (for example any file object).
978+
yielding Unicode strings (for example files opened in text mode).
979979

980980
Optional argument *source* specifies the name of the file being read. If
981981
not given and *f* has a :attr:`name` attribute, that is used for
@@ -984,28 +984,6 @@ ConfigParser Objects
984984
.. versionadded:: 3.2
985985
Replaces :meth:`readfp`.
986986

987-
.. note::
988-
989-
Prior to Python 3.2, :meth:`readfp` consumed lines from the file-like
990-
argument by calling its :meth:`~file.readline` method. For existing code
991-
calling :meth:`readfp` with arguments which don't support iteration,
992-
the following generator may be used as a wrapper around the file-like
993-
object::
994-
995-
def readline_generator(f):
996-
line = f.readline()
997-
while line != '':
998-
yield line
999-
line = f.readline()
1000-
1001-
Before::
1002-
1003-
parser.readfp(f)
1004-
1005-
After::
1006-
1007-
parser.read_file(readline_generator(f))
1008-
1009987
.. method:: read_string(string, source='<string>')
1010988

1011989
Parse configuration data from a string.
@@ -1142,6 +1120,22 @@ ConfigParser Objects
11421120
.. deprecated:: 3.2
11431121
Use :meth:`read_file` instead.
11441122

1123+
.. versionchanged:: 3.2
1124+
:meth:`readfp` now iterates on *f* instead of calling ``f.readline()``.
1125+
1126+
For existing code calling :meth:`readfp` with arguments which don't
1127+
support iteration, the following generator may be used as a wrapper
1128+
around the file-like object::
1129+
1130+
def readline_generator(f):
1131+
line = f.readline()
1132+
while line:
1133+
yield line
1134+
line = f.readline()
1135+
1136+
Instead of ``parser.readfp(f)`` use
1137+
``parser.read_file(readline_generator(f))``.
1138+
11451139

11461140
.. data:: MAX_INTERPOLATION_DEPTH
11471141

Lib/test/test_cfgparser.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ def readline(self):
13161316
def readline_generator(f):
13171317
"""As advised in Doc/library/configparser.rst."""
13181318
line = f.readline()
1319-
while line != '':
1319+
while line:
13201320
yield line
13211321
line = f.readline()
13221322

@@ -1327,8 +1327,8 @@ def test_file(self):
13271327
parser = configparser.ConfigParser()
13281328
with open(file_path) as f:
13291329
parser.read_file(f)
1330-
self.assertTrue("Foo Bar" in parser)
1331-
self.assertTrue("foo" in parser["Foo Bar"])
1330+
self.assertIn("Foo Bar", parser)
1331+
self.assertIn("foo", parser["Foo Bar"])
13321332
self.assertEqual(parser["Foo Bar"]["foo"], "newbar")
13331333

13341334
def test_iterable(self):
@@ -1337,8 +1337,8 @@ def test_iterable(self):
13371337
foo=newbar""").strip().split('\n')
13381338
parser = configparser.ConfigParser()
13391339
parser.read_file(lines)
1340-
self.assertTrue("Foo Bar" in parser)
1341-
self.assertTrue("foo" in parser["Foo Bar"])
1340+
self.assertIn("Foo Bar", parser)
1341+
self.assertIn("foo", parser["Foo Bar"])
13421342
self.assertEqual(parser["Foo Bar"]["foo"], "newbar")
13431343

13441344
def test_readline_generator(self):
@@ -1347,8 +1347,8 @@ def test_readline_generator(self):
13471347
with self.assertRaises(TypeError):
13481348
parser.read_file(FakeFile())
13491349
parser.read_file(readline_generator(FakeFile()))
1350-
self.assertTrue("Foo Bar" in parser)
1351-
self.assertTrue("foo" in parser["Foo Bar"])
1350+
self.assertIn("Foo Bar", parser)
1351+
self.assertIn("foo", parser["Foo Bar"])
13521352
self.assertEqual(parser["Foo Bar"]["foo"], "newbar")
13531353

13541354

0 commit comments

Comments
 (0)