]> granicus.if.org Git - python/commitdiff
bpo-30266: support "= None" pattern in AbstractContextManager (GH-1448) (GH-2054)
authorMariatta <Mariatta@users.noreply.github.com>
Sat, 10 Jun 2017 03:36:28 +0000 (20:36 -0700)
committerGitHub <noreply@github.com>
Sat, 10 Jun 2017 03:36:28 +0000 (20:36 -0700)
contextlib.AbstractContextManager now supports anti-registration
by setting __enter__ = None or __exit__ = None, following the pattern
introduced in bpo-25958..
(cherry picked from commit 57161aac5eb9bcb0b43e551a1937ff0a84c1ec52)

Lib/contextlib.py
Lib/test/test_contextlib.py
Misc/NEWS

index 5e47054954ba5a27d08a017d311d5ba1d338ae94..6fcba9c7d28083885a6811bda92bf42712ffcd7c 100644 (file)
@@ -1,6 +1,7 @@
 """Utilities for with-statement contexts.  See PEP 343."""
 import abc
 import sys
+import _collections_abc
 from collections import deque
 from functools import wraps
 
@@ -25,9 +26,7 @@ class AbstractContextManager(abc.ABC):
     @classmethod
     def __subclasshook__(cls, C):
         if cls is AbstractContextManager:
-            if (any("__enter__" in B.__dict__ for B in C.__mro__) and
-                any("__exit__" in B.__dict__ for B in C.__mro__)):
-                return True
+            return _collections_abc._check_methods(C, "__enter__", "__exit__")
         return NotImplemented
 
 
index b1a467d952da0fbb511c2d596dcd7d946f5a1e20..2301f759d80858e791c6f10d682cbaa45239dee7 100644 (file)
@@ -44,6 +44,16 @@ class TestAbstractContextManager(unittest.TestCase):
 
         self.assertTrue(issubclass(DefaultEnter, AbstractContextManager))
 
+        class NoEnter(ManagerFromScratch):
+            __enter__ = None
+
+        self.assertFalse(issubclass(NoEnter, AbstractContextManager))
+
+        class NoExit(ManagerFromScratch):
+            __exit__ = None
+
+        self.assertFalse(issubclass(NoExit, AbstractContextManager))
+
 
 class ContextManagerTestCase(unittest.TestCase):
 
index 213a6e9c2f64422a3d31fb75e13bda428735c14a..e6517695a62c20d43acecaeff01bb88da93f2a2f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -96,6 +96,10 @@ Library
 - bpo-30048: Fixed ``Task.cancel()`` can be ignored when the task is
   running coroutine and the coroutine returned without any more ``await``.
 
+- bpo-30266: contextlib.AbstractContextManager now supports anti-registration
+  by setting __enter__ = None or __exit__ = None, following the pattern
+  introduced in bpo-25958. Patch by Jelle Zijlstra.
+
 - bpo-30298: Weaken the condition of deprecation warnings for inline modifiers.
   Now allowed several subsequential inline modifiers at the start of the
   pattern (e.g. ``'(?i)(?s)...'``).  In verbose mode whitespaces and comments