bpo-30266: support "= None" pattern in AbstractContextManager (GH-1448) (GH-2054)

contextlib.AbstractContextManager now supports anti-registration
by setting __enter__ = None or __exit__ = None, following the pattern
introduced in bpo-25958..
(cherry picked from commit 57161aac5e)
This commit is contained in:
Mariatta 2017-06-09 20:36:28 -07:00 committed by GitHub
parent e380c19b7e
commit 753422f6e3
3 changed files with 16 additions and 3 deletions

View 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