Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
daab1c80
Kaydet (Commit)
daab1c80
authored
Nis 27, 2011
tarafından
Łukasz Langa
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Closes #11670: configparser read_file now iterates over f.
üst
944d16c6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
9 deletions
+82
-9
configparser.rst
Doc/library/configparser.rst
+24
-5
configparser.py
Lib/configparser.py
+4
-4
test_cfgparser.py
Lib/test/test_cfgparser.py
+54
-0
No files found.
Doc/library/configparser.rst
Dosyayı görüntüle @
daab1c80
...
...
@@ -974,18 +974,37 @@ ConfigParser Objects
.. method:: read_file(f, source=None)
Read and parse configuration data from the file or file-like object in
*f* (only the :meth:`readline` method is used). The file-like object
must operate in text mode. Specifically, it must return strings from
:meth:`readline`.
Read and parse configuration data from *f* which must be an iterable
yielding Unicode strings (for example any file object).
Optional argument *source* specifies the name of the file being read. If
not given and *f* has a :attr:`name` attribute, that is used for
*source*; the default is ``'<???>'``.
.. versionadded:: 3.2
Replaces :meth:`readfp`.
Replaces :meth:`readfp`.
.. note::
Prior to Python 3.2, :meth:`readfp` consumed lines from the file-like
argument by calling its :meth:`~file.readline` method. For existing code
calling :meth:`readfp` with arguments which don't support iteration,
the following generator may be used as a wrapper around the file-like
object::
def readline_generator(f):
line = f.readline()
while line != '':
yield line
line = f.readline()
Before::
parser.readfp(f)
After::
parser.read_file(readline_generator(f))
.. method:: read_string(string, source='<string>')
...
...
Lib/configparser.py
Dosyayı görüntüle @
daab1c80
...
...
@@ -694,10 +694,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
:
...
...
Lib/test/test_cfgparser.py
Dosyayı görüntüle @
daab1c80
...
...
@@ -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
,
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment