Skip to content

Commit 57161aa

Browse files
JelleZijlstragvanrossum
authored andcommitted
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.
1 parent 3b5cf85 commit 57161aa

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

Lib/contextlib.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Utilities for with-statement contexts. See PEP 343."""
22
import abc
33
import sys
4+
import _collections_abc
45
from collections import deque
56
from functools import wraps
67

@@ -25,9 +26,7 @@ def __exit__(self, exc_type, exc_value, traceback):
2526
@classmethod
2627
def __subclasshook__(cls, C):
2728
if cls is AbstractContextManager:
28-
if (any("__enter__" in B.__dict__ for B in C.__mro__) and
29-
any("__exit__" in B.__dict__ for B in C.__mro__)):
30-
return True
29+
return _collections_abc._check_methods(C, "__enter__", "__exit__")
3130
return NotImplemented
3231

3332

Lib/test/test_contextlib.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ def __exit__(self, *args):
4444

4545
self.assertTrue(issubclass(DefaultEnter, AbstractContextManager))
4646

47+
class NoEnter(ManagerFromScratch):
48+
__enter__ = None
49+
50+
self.assertFalse(issubclass(NoEnter, AbstractContextManager))
51+
52+
class NoExit(ManagerFromScratch):
53+
__exit__ = None
54+
55+
self.assertFalse(issubclass(NoExit, AbstractContextManager))
56+
4757

4858
class ContextManagerTestCase(unittest.TestCase):
4959

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ Library
410410
- bpo-30048: Fixed ``Task.cancel()`` can be ignored when the task is
411411
running coroutine and the coroutine returned without any more ``await``.
412412

413+
- bpo-30266: contextlib.AbstractContextManager now supports anti-registration
414+
by setting __enter__ = None or __exit__ = None, following the pattern
415+
introduced in bpo-25958. Patch by Jelle Zijlstra.
416+
413417
- bpo-30340: Enhanced regular expressions optimization. This increased
414418
the performance of matching some patterns up to 25 times.
415419

0 commit comments

Comments
 (0)