]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.7] bpo-38878: Fix os.PathLike __subclasshook__ (GH-17336) (GH-17685)
authorBar Harel <bzvi7919@gmail.com>
Mon, 23 Dec 2019 18:31:15 +0000 (20:31 +0200)
committerIvan Levkivskyi <levkivskyi@gmail.com>
Mon, 23 Dec 2019 18:31:15 +0000 (18:31 +0000)
https://bugs.python.org/issue38878

Lib/os.py
Lib/test/test_os.py
Misc/NEWS.d/next/Library/2019-11-22-12-08-52.bpo-38878.EJ0cFf.rst [new file with mode: 0644]

index b93f95d98ede1cd8a57da3676ed4a77a8c9db6de..9853e37c61a2047d6c8fdce8afceb995fcb84e27 100644 (file)
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -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.
@@ -1076,4 +1078,6 @@ class PathLike(abc.ABC):
 
     @classmethod
     def __subclasshook__(cls, subclass):
-        return hasattr(subclass, '__fspath__')
+        if cls is PathLike:
+            return _check_methods(subclass, '__fspath__')
+        return NotImplemented
index df4bad7a8cf7d587523e75935289477aad1838f0..411e5aa507389eef7395b217de86ff18298fc925 100644 (file)
@@ -3745,6 +3745,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):
diff --git a/Misc/NEWS.d/next/Library/2019-11-22-12-08-52.bpo-38878.EJ0cFf.rst b/Misc/NEWS.d/next/Library/2019-11-22-12-08-52.bpo-38878.EJ0cFf.rst
new file mode 100644 (file)
index 0000000..9cbdf08
--- /dev/null
@@ -0,0 +1,2 @@
+Fixed __subclasshook__ of :class:`os.PathLike` to return a correct result
+upon inheritence. Patch by Bar Harel.