diff options
author | David Ellis <ducksual@gmail.com> | 2017-03-03 17:14:27 (GMT) |
---|---|---|
committer | Berker Peksag <berker.peksag@gmail.com> | 2017-03-03 17:14:27 (GMT) |
commit | 85b8d01c916b482dac937b93ede1e53b1db0361c (patch) | |
tree | bfa366f638fdd8276bfca76ffaf252ee1e3fa5f7 /Lib | |
parent | 677ab995cede784f5dafa64d54c92e98c8b81dd7 (diff) | |
download | cpython-85b8d01c916b482dac937b93ede1e53b1db0361c.zip cpython-85b8d01c916b482dac937b93ede1e53b1db0361c.tar.gz cpython-85b8d01c916b482dac937b93ede1e53b1db0361c.tar.bz2 |
bpo-29623: Make PathLike objects work with ConfigParser.read() (#242)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/configparser.py | 5 | ||||
-rw-r--r-- | Lib/test/test_configparser.py | 11 |
2 files changed, 15 insertions, 1 deletions
diff --git a/Lib/configparser.py b/Lib/configparser.py index af5aca1..230ab2b 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -143,6 +143,7 @@ from collections import OrderedDict as _default_dict, ChainMap as _ChainMap import functools import io import itertools +import os import re import sys import warnings @@ -687,7 +688,7 @@ class RawConfigParser(MutableMapping): Return list of successfully read files. """ - if isinstance(filenames, str): + if isinstance(filenames, (str, os.PathLike)): filenames = [filenames] read_ok = [] for filename in filenames: @@ -696,6 +697,8 @@ class RawConfigParser(MutableMapping): self._read(fp, filename) except OSError: continue + if isinstance(filename, os.PathLike): + filename = os.fspath(filename) read_ok.append(filename) return read_ok diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py index 696f642..72c3f19 100644 --- a/Lib/test/test_configparser.py +++ b/Lib/test/test_configparser.py @@ -2,6 +2,7 @@ import collections import configparser import io import os +import pathlib import textwrap import unittest import warnings @@ -720,6 +721,16 @@ boolean {0[0]} NO parsed_files = cf.read(file1) self.assertEqual(parsed_files, [file1]) self.assertEqual(cf.get("Foo Bar", "foo"), "newbar") + # check when we pass only a Path object: + cf = self.newconfig() + parsed_files = cf.read(pathlib.Path(file1)) + self.assertEqual(parsed_files, [file1]) + self.assertEqual(cf.get("Foo Bar", "foo"), "newbar") + # check when we passed both a filename and a Path object: + cf = self.newconfig() + parsed_files = cf.read([pathlib.Path(file1), file1]) + self.assertEqual(parsed_files, [file1, file1]) + self.assertEqual(cf.get("Foo Bar", "foo"), "newbar") # check when we pass only missing files: cf = self.newconfig() parsed_files = cf.read(["nonexistent-file"]) |