mirror of
https://github.com/django/django.git
synced 2025-04-01 12:06:43 +00:00
Refs #34305 -- Added SimpleTestCase.enterClassContext() on Python < 3.11.
This commit is contained in:
parent
b0ad41198b
commit
ed4f83782d
@ -51,6 +51,7 @@ from django.test.utils import (
|
|||||||
override_settings,
|
override_settings,
|
||||||
)
|
)
|
||||||
from django.utils.functional import classproperty
|
from django.utils.functional import classproperty
|
||||||
|
from django.utils.version import PY311
|
||||||
from django.views.static import serve
|
from django.views.static import serve
|
||||||
|
|
||||||
logger = logging.getLogger("django.test")
|
logger = logging.getLogger("django.test")
|
||||||
@ -64,6 +65,24 @@ __all__ = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if not PY311:
|
||||||
|
# Backport of unittest.case._enter_context() from Python 3.11.
|
||||||
|
def _enter_context(cm, addcleanup):
|
||||||
|
# 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 not support the "
|
||||||
|
f"context manager protocol"
|
||||||
|
) from None
|
||||||
|
result = enter(cm)
|
||||||
|
addcleanup(exit, cm, None, None, None)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def to_list(value):
|
def to_list(value):
|
||||||
"""Put value into a list if it's not already one."""
|
"""Put value into a list if it's not already one."""
|
||||||
if not isinstance(value, list):
|
if not isinstance(value, list):
|
||||||
@ -312,6 +331,12 @@ class SimpleTestCase(unittest.TestCase):
|
|||||||
"""Perform post-test things."""
|
"""Perform post-test things."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
if not PY311:
|
||||||
|
# Backport of unittest.TestCase.enterClassContext() from Python 3.11.
|
||||||
|
@classmethod
|
||||||
|
def enterClassContext(cls, cm):
|
||||||
|
return _enter_context(cm, cls.addClassCleanup)
|
||||||
|
|
||||||
def settings(self, **kwargs):
|
def settings(self, **kwargs):
|
||||||
"""
|
"""
|
||||||
A context manager that temporarily sets a setting and reverts to the
|
A context manager that temporarily sets a setting and reverts to the
|
||||||
|
Loading…
x
Reference in New Issue
Block a user