Skip to content

Commit 59d06b9

Browse files
bharelilevkivskyi
authored andcommitted
[3.7] bpo-38878: Fix os.PathLike __subclasshook__ (GH-17336) (GH-17685)
https://bugs.python.org/issue38878
1 parent 0ffc900 commit 59d06b9

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Lib/os.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import sys
2727
import stat as st
2828

29+
from _collections_abc import _check_methods
30+
2931
_names = sys.builtin_module_names
3032

3133
# Note: more names are added to __all__ later.
@@ -1076,4 +1078,6 @@ def __fspath__(self):
10761078

10771079
@classmethod
10781080
def __subclasshook__(cls, subclass):
1079-
return hasattr(subclass, '__fspath__')
1081+
if cls is PathLike:
1082+
return _check_methods(subclass, '__fspath__')
1083+
return NotImplemented

Lib/test/test_os.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3745,6 +3745,14 @@ def test_bad_pathlike(self):
37453745
self.assertRaises(ZeroDivisionError, self.fspath,
37463746
FakePath(ZeroDivisionError()))
37473747

3748+
def test_pathlike_subclasshook(self):
3749+
# bpo-38878: subclasshook causes subclass checks
3750+
# true on abstract implementation.
3751+
class A(os.PathLike):
3752+
pass
3753+
self.assertFalse(issubclass(FakePath, A))
3754+
self.assertTrue(issubclass(FakePath, os.PathLike))
3755+
37483756

37493757
class TimesTests(unittest.TestCase):
37503758
def test_times(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed __subclasshook__ of :class:`os.PathLike` to return a correct result
2+
upon inheritence. Patch by Bar Harel.

0 commit comments

Comments
 (0)