Skip to content

Commit 2414995

Browse files
committed
Avoid _blocking_manager mutable global
Scope it to the pytest config at least.
1 parent d93631f commit 2414995

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

pytest_django/plugin.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def find_django_path(args) -> pathlib.Path | None:
220220
return PROJECT_NOT_FOUND
221221

222222

223-
def _setup_django() -> None:
223+
def _setup_django(config: pytest.Config) -> None:
224224
if "django" not in sys.modules:
225225
return
226226

@@ -235,7 +235,8 @@ def _setup_django() -> None:
235235
if not django.apps.apps.ready:
236236
django.setup()
237237

238-
_blocking_manager.block()
238+
blocking_manager = config.stash[blocking_manager_key]
239+
blocking_manager.block()
239240

240241

241242
def _get_boolean_value(
@@ -354,14 +355,16 @@ def _get_option_with_source(
354355
with _handle_import_error(_django_project_scan_outcome):
355356
dj_settings.DATABASES # noqa: B018
356357

357-
_setup_django()
358+
early_config.stash[blocking_manager_key] = DjangoDbBlocker(_ispytest=True)
359+
360+
_setup_django(early_config)
358361

359362

360363
@pytest.hookimpl(trylast=True)
361-
def pytest_configure() -> None:
364+
def pytest_configure(config: pytest.Config) -> None:
362365
# Allow Django settings to be configured in a user pytest_configure call,
363366
# but make sure we call django.setup()
364-
_setup_django()
367+
_setup_django(config)
365368

366369

367370
@pytest.hookimpl()
@@ -478,7 +481,7 @@ def django_test_environment(request: pytest.FixtureRequest) -> Generator[None, N
478481
we need to follow this model.
479482
"""
480483
if django_settings_is_configured():
481-
_setup_django()
484+
_setup_django(request.config)
482485
from django.test.utils import setup_test_environment, teardown_test_environment
483486

484487
debug_ini = request.config.getini("django_debug_mode")
@@ -496,7 +499,7 @@ def django_test_environment(request: pytest.FixtureRequest) -> Generator[None, N
496499

497500

498501
@pytest.fixture(scope="session")
499-
def django_db_blocker() -> DjangoDbBlocker | None:
502+
def django_db_blocker(request: pytest.FixtureRequest) -> DjangoDbBlocker | None:
500503
"""Wrapper around Django's database access.
501504
502505
This object can be used to re-enable database access. This fixture is used
@@ -512,7 +515,8 @@ def django_db_blocker() -> DjangoDbBlocker | None:
512515
if not django_settings_is_configured():
513516
return None
514517

515-
return _blocking_manager
518+
blocking_manager = request.config.stash[blocking_manager_key]
519+
return blocking_manager
516520

517521

518522
@pytest.fixture(autouse=True)
@@ -813,7 +817,8 @@ def restore(self) -> None:
813817
self._dj_db_wrapper.ensure_connection = self._history.pop()
814818

815819

816-
_blocking_manager = DjangoDbBlocker(_ispytest=True)
820+
# On Config.stash.
821+
blocking_manager_key = pytest.StashKey[DjangoDbBlocker]()
817822

818823

819824
def validate_urls(marker) -> list[str]:

0 commit comments

Comments
 (0)