bpo-29623: Make PathLike objects work with ConfigParser.read() (#242) · python/cpython@85b8d01 · GitHub
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

4 files changed

Lines changed: 31 additions & 8 deletions

File tree

Doc/library/configparser.rst

Lines changed: 13 additions & 7 deletions

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

0 commit comments

Comments
 (0)