Skip to content

Commit

Permalink
bpo-31307: Make ConfigParser.read() accept bytes objects (pythonGH-3420)
Browse files Browse the repository at this point in the history
  • Loading branch information
vxgmichel authored and berkerpeksag committed Nov 2, 2017
1 parent a64ce97 commit e314853
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Doc/library/configparser.rst
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,8 @@ ConfigParser Objects
Attempt to read and parse a list of filenames, returning a list of
filenames which were successfully parsed.

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

.. versionadded:: 3.7
The *filenames* parameter accepts a :class:`bytes` object.


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

Expand Down
2 changes: 1 addition & 1 deletion Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ def read(self, filenames, encoding=None):
Return list of successfully read files.
"""
if isinstance(filenames, (str, os.PathLike)):
if isinstance(filenames, (str, bytes, os.PathLike)):
filenames = [filenames]
read_ok = []
for filename in filenames:
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,23 @@ def test_read_returns_file_list(self):
parsed_files = cf.read([])
self.assertEqual(parsed_files, [])

def test_read_returns_file_list_with_bytestring_path(self):
if self.delimiters[0] != '=':
self.skipTest('incompatible format')
file1_bytestring = support.findfile("cfgparser.1").encode()
# check when passing an existing bytestring path
cf = self.newconfig()
parsed_files = cf.read(file1_bytestring)
self.assertEqual(parsed_files, [file1_bytestring])
# check when passing an non-existing bytestring path
cf = self.newconfig()
parsed_files = cf.read(b'nonexistent-file')
self.assertEqual(parsed_files, [])
# check when passing both an existing and non-existing bytestring path
cf = self.newconfig()
parsed_files = cf.read([file1_bytestring, b'nonexistent-file'])
self.assertEqual(parsed_files, [file1_bytestring])

# shared by subclasses
def get_interpolation_config(self):
return self.fromstring(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow use of bytes objects for arguments to
:meth:`configparser.ConfigParser.read`. Patch by Vincent Michel.

0 comments on commit e314853

Please sign in to comment.