From 57161aac5eb9bcb0b43e551a1937ff0a84c1ec52 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Fri, 9 Jun 2017 08:21:47 -0700 Subject: [PATCH] bpo-30266: support "= None" pattern in AbstractContextManager (#1448) contextlib.AbstractContextManager now supports anti-registration by setting __enter__ = None or __exit__ = None, following the pattern introduced in bpo-25958. --- Lib/contextlib.py | 5 ++--- Lib/test/test_contextlib.py | 10 ++++++++++ Misc/NEWS | 4 ++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Lib/contextlib.py b/Lib/contextlib.py index c53b35e8d5..962cedab49 100644 --- a/Lib/contextlib.py +++ b/Lib/contextlib.py @@ -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 diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index b1a467d952..2301f759d8 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -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): diff --git a/Misc/NEWS b/Misc/NEWS index 7d31839e67..f126dd4e64 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -410,6 +410,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-30340: Enhanced regular expressions optimization. This increased the performance of matching some patterns up to 25 times. -- 2.40.0