Skip to content

Commit 85b8d01

Browse files
DavidCEllisberkerpeksag
authored andcommitted
bpo-29623: Make PathLike objects work with ConfigParser.read() (#242)
1 parent 677ab99 commit 85b8d01

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

Doc/library/configparser.rst

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -988,13 +988,16 @@ ConfigParser Objects
988988
.. method:: read(filenames, encoding=None)
989989

990990
Attempt to read and parse a list of filenames, returning a list of
991-
filenames which were successfully parsed. If *filenames* is a string, it
992-
is treated as a single filename. If a file named in *filenames* cannot
993-
be opened, that file will be ignored. This is designed so that you can
994-
specify a list of potential configuration file locations (for example,
995-
the current directory, the user's home directory, and some system-wide
996-
directory), and all existing configuration files in the list will be
997-
read. If none of the named files exist, the :class:`ConfigParser`
991+
filenames which were successfully parsed.
992+
993+
If *filenames* is a string or :term:`path-like object`, it is treated as
994+
a single filename. If a file named in *filenames* cannot be opened, that
995+
file will be ignored. This is designed so that you can specify a list of
996+
potential configuration file locations (for example, the current
997+
directory, the user's home directory, and some system-wide directory),
998+
and all existing configuration files in the list will be read.
999+
1000+
If none of the named files exist, the :class:`ConfigParser`
9981001
instance will contain an empty dataset. An application which requires
9991002
initial values to be loaded from a file should load the required file or
10001003
files using :meth:`read_file` before calling :meth:`read` for any
@@ -1011,6 +1014,9 @@ ConfigParser Objects
10111014
The *encoding* parameter. Previously, all files were read using the
10121015
default encoding for :func:`open`.
10131016

1017+
.. versionadded:: 3.6.1
1018+
The *filenames* parameter accepts a :term:`path-like object`.
1019+
10141020

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

Lib/configparser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
import functools
144144
import io
145145
import itertools
146+
import os
146147
import re
147148
import sys
148149
import warnings
@@ -687,7 +688,7 @@ def read(self, filenames, encoding=None):
687688
688689
Return list of successfully read files.
689690
"""
690-
if isinstance(filenames, str):
691+
if isinstance(filenames, (str, os.PathLike)):
691692
filenames = [filenames]
692693
read_ok = []
693694
for filename in filenames:
@@ -696,6 +697,8 @@ def read(self, filenames, encoding=None):
696697
self._read(fp, filename)
697698
except OSError:
698699
continue
700+
if isinstance(filename, os.PathLike):
701+
filename = os.fspath(filename)
699702
read_ok.append(filename)
700703
return read_ok
701704

Lib/test/test_configparser.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import configparser
33
import io
44
import os
5+
import pathlib
56
import textwrap
67
import unittest
78
import warnings
@@ -720,6 +721,16 @@ def test_read_returns_file_list(self):
720721
parsed_files = cf.read(file1)
721722
self.assertEqual(parsed_files, [file1])
722723
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
724+
# check when we pass only a Path object:
725+
cf = self.newconfig()
726+
parsed_files = cf.read(pathlib.Path(file1))
727+
self.assertEqual(parsed_files, [file1])
728+
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
729+
# check when we passed both a filename and a Path object:
730+
cf = self.newconfig()
731+
parsed_files = cf.read([pathlib.Path(file1), file1])
732+
self.assertEqual(parsed_files, [file1, file1])
733+
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar")
723734
# check when we pass only missing files:
724735
cf = self.newconfig()
725736
parsed_files = cf.read(["nonexistent-file"])

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ Extension Modules
259259
Library
260260
-------
261261

262+
- bpo-29623: Allow use of path-like object as a single argument in
263+
ConfigParser.read(). Patch by David Ellis.
264+
262265
- bpo-9303: Migrate sqlite3 module to _v2 API. Patch by Aviv Palivoda.
263266

264267
- bpo-28963: Fix out of bound iteration in asyncio.Future.remove_done_callback

0 commit comments

Comments
 (0)