summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Wobrock <david.wobrock@gmail.com>2024-02-26 17:18:48 +0100
committerGitHub <noreply@github.com>2024-02-26 17:18:48 +0100
commitef2434f8508551fee183079ab471b1dc325c7acb (patch)
tree503a6bd98f9c547b363b8149a6cce7e910ebce73
parent18d79033b90902a6d6b615b42051191fd1b37892 (diff)
Refs #32114 -- Fixed test crash on non-picklable objects in subtests when PickleError is raised.
Related to the https://github.com/python/cpython/issues/73373. Follow up to c09e8f5fd8f977bf16e9ec5d11b370151fc81ea8.
-rw-r--r--django/test/testcases.py2
-rw-r--r--tests/test_utils/test_testcase.py16
2 files changed, 16 insertions, 2 deletions
diff --git a/django/test/testcases.py b/django/test/testcases.py
index 911a2d50ac..0a802c887b 100644
--- a/django/test/testcases.py
+++ b/django/test/testcases.py
@@ -100,7 +100,7 @@ def is_pickable(obj):
"""
try:
pickle.loads(pickle.dumps(obj))
- except (AttributeError, TypeError):
+ except (AttributeError, TypeError, pickle.PickleError):
return False
return True
diff --git a/tests/test_utils/test_testcase.py b/tests/test_utils/test_testcase.py
index 0f41f29a23..efca01e29e 100644
--- a/tests/test_utils/test_testcase.py
+++ b/tests/test_utils/test_testcase.py
@@ -3,17 +3,31 @@ from functools import wraps
from django.db import IntegrityError, connections, transaction
from django.test import TestCase, skipUnlessDBFeature
-from django.test.testcases import DatabaseOperationForbidden, SimpleTestCase, TestData
+from django.test.testcases import (
+ DatabaseOperationForbidden,
+ SimpleTestCase,
+ TestData,
+ is_pickable,
+)
from .models import Car, Person, PossessedCar
+class UnpicklableObject:
+ def __getstate__(self):
+ raise pickle.PickleError("cannot be pickled for testing reasons")
+
+
class TestSimpleTestCase(SimpleTestCase):
def test_is_picklable_with_non_picklable_properties(self):
"""ParallelTestSuite requires that all TestCases are picklable."""
self.non_picklable = lambda: 0
self.assertEqual(self, pickle.loads(pickle.dumps(self)))
+ def test_is_picklable_with_non_picklable_object(self):
+ unpicklable_obj = UnpicklableObject()
+ self.assertEqual(is_pickable(unpicklable_obj), False)
+
class TestTestCase(TestCase):
@skipUnlessDBFeature("can_defer_constraint_checks")