-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f01538
commit e9dbdd9
Showing
2 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from __future__ import annotations | ||
|
||
import sys | ||
from contextlib import AbstractContextManager | ||
from typing import Any | ||
from typing import Callable | ||
from typing import TypeVar | ||
|
||
from django import test | ||
|
||
_T = TypeVar("_T") | ||
|
||
if sys.version_info < (3, 11): | ||
|
||
def _enter_context(cm: Any, addcleanup: Callable[..., None]) -> Any: | ||
# We look up the special methods on the type to match the with | ||
# statement. | ||
cls = type(cm) | ||
try: | ||
enter = cls.__enter__ | ||
exit = cls.__exit__ | ||
except AttributeError: | ||
raise TypeError( | ||
f"'{cls.__module__}.{cls.__qualname__}' object does " | ||
f"not support the context manager protocol" | ||
) from None | ||
result = enter(cm) | ||
addcleanup(exit, cm, None, None, None) | ||
return result | ||
|
||
|
||
class SimpleTestCase(test.SimpleTestCase): | ||
if sys.version_info < (3, 11): | ||
|
||
def enterContext(self, cm: AbstractContextManager[_T]) -> _T: | ||
result: _T = _enter_context(cm, self.addCleanup) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters