From fb9216382a387685e1082ac97caa3feaf3d5fe80 Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Mon, 4 Dec 2023 11:23:08 +0000 Subject: [PATCH] Refs #34986 -- Moved garbage_collect() helper to django.test.utils. --- django/test/utils.py | 10 ++++++++++ tests/dispatch/tests.py | 16 +--------------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/django/test/utils.py b/django/test/utils.py index 08f00f9eb4..ddb85127dc 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -1,4 +1,5 @@ import collections +import gc import logging import os import re @@ -27,6 +28,7 @@ from django.template import Template from django.test.signals import template_rendered from django.urls import get_script_prefix, set_script_prefix from django.utils.translation import deactivate +from django.utils.version import PYPY try: import jinja2 @@ -38,6 +40,7 @@ __all__ = ( "Approximate", "ContextList", "isolate_lru_cache", + "garbage_collect", "get_runner", "CaptureQueriesContext", "ignore_warnings", @@ -982,3 +985,10 @@ def register_lookup(field, *lookups, lookup_name=None): finally: for lookup in lookups: field._unregister_lookup(lookup, lookup_name) + + +def garbage_collect(): + gc.collect() + if PYPY: + # Collecting weakreferences can take two collections on PyPy. + gc.collect() diff --git a/tests/dispatch/tests.py b/tests/dispatch/tests.py index 8e72e626dd..18426d8dd1 100644 --- a/tests/dispatch/tests.py +++ b/tests/dispatch/tests.py @@ -1,23 +1,9 @@ -import gc import weakref from types import TracebackType from django.dispatch import Signal, receiver from django.test import SimpleTestCase -from django.test.utils import override_settings -from django.utils.version import PYPY - -if PYPY: - - def garbage_collect(): - # Collecting weakreferences can take two collections on PyPy. - gc.collect() - gc.collect() - -else: - - def garbage_collect(): - gc.collect() +from django.test.utils import garbage_collect, override_settings def receiver_1_arg(val, **kwargs):