summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVincent Michel <vxgmichel@gmail.com>2017-11-02 12:47:04 (GMT)
committerBerker Peksag <berker.peksag@gmail.com>2017-11-02 12:47:04 (GMT)
commite314853d57450b2b9523157eebd405289a795a0e (patch)
treec410df1fbf11b9ec522e915819c58f5f53cbd27a
parenta64ce973a3ad90e4f4a93c402e946c132f647a63 (diff)
downloadcpython-e314853d57450b2b9523157eebd405289a795a0e.zip
cpython-e314853d57450b2b9523157eebd405289a795a0e.tar.gz
cpython-e314853d57450b2b9523157eebd405289a795a0e.tar.bz2
bpo-31307: Make ConfigParser.read() accept bytes objects (GH-3420)
-rw-r--r--Doc/library/configparser.rst6
-rw-r--r--Lib/configparser.py2
-rw-r--r--Lib/test/test_configparser.py17
-rw-r--r--Misc/NEWS.d/next/Library/2017-09-07-12-50-28.bpo-31307.AVBiNY.rst2
4 files changed, 25 insertions, 2 deletions
diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst
index acca647..dd82577 100644
--- a/Doc/library/configparser.rst
+++ b/Doc/library/configparser.rst
@@ -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
@@ -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)
diff --git a/Lib/configparser.py b/Lib/configparser.py
index e172ac8..33dc9b9 100644
--- a/Lib/configparser.py
+++ b/Lib/configparser.py
@@ -687,7 +687,7 @@ class RawConfigParser(MutableMapping):
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:
diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py
index d8969ef..4d07203 100644
--- a/Lib/test/test_configparser.py
+++ b/Lib/test/test_configparser.py
@@ -740,6 +740,23 @@ boolean {0[0]} NO
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(
diff --git a/Misc/NEWS.d/next/Library/2017-09-07-12-50-28.bpo-31307.AVBiNY.rst b/Misc/NEWS.d/next/Library/2017-09-07-12-50-28.bpo-31307.AVBiNY.rst
new file mode 100644
index 0000000..7e649aa
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-09-07-12-50-28.bpo-31307.AVBiNY.rst
@@ -0,0 +1,2 @@
+Allow use of bytes objects for arguments to
+:meth:`configparser.ConfigParser.read`. Patch by Vincent Michel.