summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBar Harel <bzvi7919@gmail.com>2019-12-23 20:31:00 +0200
committerIvan Levkivskyi <levkivskyi@gmail.com>2019-12-23 18:31:00 +0000
commit0846e5d4603434c2bbf8a528677cf1ff3fe29b95 (patch)
treef44bd35fa8bc5c58ed22085a8d5a31c9172dacaf
parent7eb8c6d2c89a8a7ba3af4a99ab20456dff544881 (diff)
downloadcpython-git-0846e5d4603434c2bbf8a528677cf1ff3fe29b95.tar.gz
[3.8] bpo-38878: Fix os.PathLike __subclasshook__ (GH-17336) (GH-17684)
https://bugs.python.org/issue38878
-rw-r--r--Lib/os.py6
-rw-r--r--Lib/test/test_os.py8
-rw-r--r--Misc/NEWS.d/next/Library/2019-11-22-12-08-52.bpo-38878.EJ0cFf.rst2
3 files changed, 15 insertions, 1 deletions
diff --git a/Lib/os.py b/Lib/os.py
index 52d3f1d741..253cad1a59 100644
--- 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.
@@ -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':
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index 4a076e3bbf..11454b2e88 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -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):
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
index 0000000000..9cbdf08dd5
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-11-22-12-08-52.bpo-38878.EJ0cFf.rst
@@ -0,0 +1,2 @@
+Fixed __subclasshook__ of :class:`os.PathLike` to return a correct result
+upon inheritence. Patch by Bar Harel.