Permalink
Browse files

Fix test failures on Python 3.5.

  • Loading branch information...
jaraco committed Jan 24, 2019
1 parent c6ccb5d commit 5ce1d1a7cb5781b94f405bfed8c77a4ac2cec8b5
Showing with 76 additions and 4 deletions.
  1. +5 −4 src/backports/configparser/__init__.py
  2. +67 −0 src/backports/configparser/helpers.py
  3. +4 −0 src/test_configparser.py
@@ -139,6 +139,7 @@
from backports.configparser.helpers import OrderedDict as _default_dict
from backports.configparser.helpers import ChainMap as _ChainMap
from backports.configparser.helpers import from_none, open, str, PY2
from backports.configparser.helpers import PathLike, fspath

__all__ = ["NoSectionError", "DuplicateOptionError", "DuplicateSectionError",
"NoOptionError", "InterpolationError", "InterpolationDepthError",
@@ -695,18 +696,18 @@ def read(self, filenames, encoding=None):
DeprecationWarning,
stacklevel=2,
)
filenames = [filenames]
elif isinstance(filenames, (str, bytes, os.PathLike)):

if isinstance(filenames, (str, bytes, PathLike)):
filenames = [filenames]
read_ok = []
for filename in filenames:
if isinstance(filename, PathLike):
filename = fspath(filename)
try:
with open(filename, encoding=encoding) as fp:
self._read(fp, filename)
except IOError:
continue
if isinstance(filename, os.PathLike):
filename = os.fspath(filename)
read_ok.append(filename)
return read_ok

@@ -6,6 +6,9 @@
from __future__ import print_function
from __future__ import unicode_literals

import abc
import os

from collections import MutableMapping
try:
from collections import UserDict
@@ -17,6 +20,11 @@
except ImportError:
from ordereddict import OrderedDict

try:
import pathlib
except ImportError:
pathlib = None

from io import open
import sys
try:
@@ -169,3 +177,62 @@ def clear(self):
from collections import ChainMap
except ImportError:
ChainMap = _ChainMap


class _PathLike(abc.ABC):

"""Abstract base class for implementing the file system path protocol."""

@abc.abstractmethod
def __fspath__(self):
"""Return the file system path representation of the object."""
raise NotImplementedError

@classmethod
def __subclasshook__(cls, subclass):
return (
hasattr(subclass, '__fspath__')
# workaround for Python 3.5
or issubclass(subclass, pathlib.Path)
)


PathLike = getattr(os, 'PathLike', _PathLike)


def _fspath(path):
"""Return the path representation of a path-like object.
If str or bytes is passed in, it is returned unchanged. Otherwise the
os.PathLike interface is used to get the path representation. If the
path representation is not str or bytes, TypeError is raised. If the
provided path is not str, bytes, or os.PathLike, TypeError is raised.
"""
if isinstance(path, (str, bytes)):
return path

if not hasattr(path, '__fspath__') and isinstance(path, pathlib.Path):
# workaround for Python 3.5
return str(path)

# Work from the object's type to match method resolution of other magic
# methods.
path_type = type(path)
try:
path_repr = path_type.__fspath__(path)
except AttributeError:

if hasattr(path_type, '__fspath__'):
raise
else:
raise TypeError("expected str, bytes or os.PathLike object, "
"not " + path_type.__name__)
if isinstance(path_repr, (str, bytes)):
return path_repr
else:
raise TypeError("expected {}.__fspath__() to return str or bytes, "
"not {}".format(path_type.__name__,
type(path_repr).__name__))


fspath = getattr(os, 'fspath', _fspath)
@@ -2147,6 +2147,10 @@ def test_instance_assignment(self):


class MiscTestCase(unittest.TestCase):
@unittest.skipIf(
not hasattr(support, 'check__all__'),
"check__all__ not available",
)
def test__all__(self):
blacklist = {"Error", "PY2"}
support.check__all__(self, configparser, blacklist=blacklist)

0 comments on commit 5ce1d1a

Please sign in to comment.