[3.8] bpo-38878: Fix os.PathLike __subclasshook__ (GH-17336) (GH-17684)

https://bugs.python.org/issue38878
This commit is contained in:
Bar Harel 2019-12-23 20:31:00 +02:00 committed by Ivan Levkivskyi
parent 7eb8c6d2c8
commit 0846e5d460
3 changed files with 15 additions and 1 deletions

View File

@ -26,6 +26,8 @@ import abc
import sys
import stat as st
from _collections_abc import _check_methods
_names = sys.builtin_module_names
# Note: more names are added to __all__ later.
@ -1070,7 +1072,9 @@ class PathLike(abc.ABC):
@classmethod
def __subclasshook__(cls, subclass):
return hasattr(subclass, '__fspath__')
if cls is PathLike:
return _check_methods(subclass, '__fspath__')
return NotImplemented
if name == 'nt':

View File

@ -4017,6 +4017,14 @@ class TestPEP519(unittest.TestCase):
self.assertRaises(ZeroDivisionError, self.fspath,
FakePath(ZeroDivisionError()))
def test_pathlike_subclasshook(self):
# bpo-38878: subclasshook causes subclass checks
# true on abstract implementation.
class A(os.PathLike):
pass
self.assertFalse(issubclass(FakePath, A))
self.assertTrue(issubclass(FakePath, os.PathLike))
class TimesTests(unittest.TestCase):
def test_times(self):

View File

@ -0,0 +1,2 @@
Fixed __subclasshook__ of :class:`os.PathLike` to return a correct result
upon inheritence. Patch by Bar Harel.