From 8cfcf9a30edd3ec0447af6a493b16d21e298e4e0 Mon Sep 17 00:00:00 2001 From: Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> Date: Fri, 28 Jun 2024 13:35:58 +0200 Subject: [PATCH] Reverted "Fixed #35564 -- Improved readability of subclass identification." This reverts commit f0d05a747f7a099e6c6bc58c42a787546d2212e7 due to a performance regression. --- django/contrib/admin/checks.py | 16 ++++++++++------ django/contrib/auth/checks.py | 12 +++++++----- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/django/contrib/admin/checks.py b/django/contrib/admin/checks.py index 9937273c28..94e700cf68 100644 --- a/django/contrib/admin/checks.py +++ b/django/contrib/admin/checks.py @@ -1,5 +1,4 @@ import collections -import contextlib from itertools import chain from django.apps import apps @@ -22,9 +21,10 @@ def _issubclass(cls, classinfo): issubclass() variant that doesn't raise an exception if cls isn't a class. """ - with contextlib.suppress(TypeError): + try: return issubclass(cls, classinfo) - return False + except TypeError: + return False def _contains_subclass(class_path, candidate_paths): @@ -34,9 +34,13 @@ def _contains_subclass(class_path, candidate_paths): """ cls = import_string(class_path) for path in candidate_paths: - with contextlib.suppress(ImportError, TypeError): - if issubclass(import_string(path), cls): - return True + try: + candidate_cls = import_string(path) + except ImportError: + # ImportErrors are raised elsewhere. + continue + if _issubclass(candidate_cls, cls): + return True return False diff --git a/django/contrib/auth/checks.py b/django/contrib/auth/checks.py index 61da4f293a..f2f9a74a6c 100644 --- a/django/contrib/auth/checks.py +++ b/django/contrib/auth/checks.py @@ -1,4 +1,3 @@ -import contextlib from itertools import chain from types import MethodType @@ -16,10 +15,13 @@ def _subclass_index(class_path, candidate_paths): list of candidate paths. If it does not exist, return -1. """ cls = import_string(class_path) - for i, path in enumerate(candidate_paths): - with contextlib.suppress(ImportError, TypeError): - if issubclass(import_string(path), cls): - return i + for index, path in enumerate(candidate_paths): + try: + candidate_cls = import_string(path) + if issubclass(candidate_cls, cls): + return index + except (ImportError, TypeError): + continue return -1