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 c09e8f5fd8.
This commit is contained in:
David Wobrock 2024-02-26 17:18:48 +01:00 committed by GitHub
parent 18d79033b9
commit ef2434f850
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 2 deletions

View File

@ -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

View File

@ -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")