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

Skip to content

Commit e314853

Browse files
vxgmichelberkerpeksag
authored andcommitted
bpo-31307: Make ConfigParser.read() accept bytes objects (GH-3420)
1 parent a64ce97 commit e314853

4 files changed

Lines changed: 25 additions & 2 deletions

File tree

Doc/library/configparser.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,8 @@ ConfigParser Objects
995995
Attempt to read and parse a list of filenames, returning a list of
996996
filenames which were successfully parsed.
997997

998-
If *filenames* is a string or :term:`path-like object`, it is treated as
998+
If *filenames* is a string, a :class:`bytes` object or a
999+
:term:`path-like object`, it is treated as
9991000
a single filename. If a file named in *filenames* cannot be opened, that
10001001
file will be ignored. This is designed so that you can specify a list of
10011002
potential configuration file locations (for example, the current
@@ -1022,6 +1023,9 @@ ConfigParser Objects
10221023
.. versionadded:: 3.6.1
10231024
The *filenames* parameter accepts a :term:`path-like object`.
10241025

1026+
.. versionadded:: 3.7
1027+
The *filenames* parameter accepts a :class:`bytes` object.
1028+
10251029

10261030
.. method:: read_file(f, source=None)
10271031

Lib/configparser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ def read(self, filenames, encoding=None):
687687
688688
Return list of successfully read files.
689689
"""
690-
if isinstance(filenames, (str, os.PathLike)):
690+
if isinstance(filenames, (str, bytes, os.PathLike)):
691691
filenames = [filenames]
692692
read_ok = []
693693
for filename in filenames:

Lib/test/test_configparser.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,23 @@ def test_read_returns_file_list(self):
740740
parsed_files = cf.read([])
741741
self.assertEqual(parsed_files, [])
742742

743+
def test_read_returns_file_list_with_bytestring_path(self):
744+
if self.delimiters[0] != '=':
745+
self.skipTest('incompatible format')
746+
file1_bytestring = support.findfile("cfgparser.1").encode()
747+
# check when passing an existing bytestring path
748+
cf = self.newconfig()
749+
parsed_files = cf.read(file1_bytestring)
750+
self.assertEqual(parsed_files, [file1_bytestring])
751+
# check when passing an non-existing bytestring path
752+
cf = self.newconfig()
753+
parsed_files = cf.read(b'nonexistent-file')
754+
self.assertEqual(parsed_files, [])
755+
# check when passing both an existing and non-existing bytestring path
756+
cf = self.newconfig()
757+
parsed_files = cf.read([file1_bytestring, b'nonexistent-file'])
758+
self.assertEqual(parsed_files, [file1_bytestring])
759+
743760
# shared by subclasses
744761
def get_interpolation_config(self):
745762
return self.fromstring(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Allow use of bytes objects for arguments to
2+
:meth:`configparser.ConfigParser.read`. Patch by Vincent Michel.

0 commit comments

Comments
 (0)