mirror of
https://github.com/django/django.git
synced 2025-11-07 07:15:35 +00:00
Fixed #32114 -- Fixed parallel test crash on non-picklable objects in subtests.
This commit is contained in:
committed by
Mariusz Felisiak
parent
a269d8d1d8
commit
c09e8f5fd8
@@ -1,6 +1,7 @@
|
||||
import difflib
|
||||
import json
|
||||
import logging
|
||||
import pickle
|
||||
import posixpath
|
||||
import sys
|
||||
import threading
|
||||
@@ -92,6 +93,18 @@ def to_list(value):
|
||||
return value
|
||||
|
||||
|
||||
def is_pickable(obj):
|
||||
"""
|
||||
Returns true if the object can be dumped and loaded through the pickle
|
||||
module.
|
||||
"""
|
||||
try:
|
||||
pickle.loads(pickle.dumps(obj))
|
||||
except (AttributeError, TypeError):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def assert_and_parse_html(self, html, user_msg, msg):
|
||||
try:
|
||||
dom = parse_html(html)
|
||||
@@ -303,6 +316,23 @@ class SimpleTestCase(unittest.TestCase):
|
||||
"""
|
||||
self._setup_and_call(result)
|
||||
|
||||
def __getstate__(self):
|
||||
"""
|
||||
Make SimpleTestCase picklable for parallel tests using subtests.
|
||||
"""
|
||||
state = super().__dict__
|
||||
# _outcome and _subtest cannot be tested on picklability, since they
|
||||
# contain the TestCase itself, leading to an infinite recursion.
|
||||
if state["_outcome"]:
|
||||
pickable_state = {"_outcome": None, "_subtest": None}
|
||||
for key, value in state.items():
|
||||
if key in pickable_state or not is_pickable(value):
|
||||
continue
|
||||
pickable_state[key] = value
|
||||
return pickable_state
|
||||
|
||||
return state
|
||||
|
||||
def debug(self):
|
||||
"""Perform the same as __call__(), without catching the exception."""
|
||||
debug_result = _DebugResult()
|
||||
|
||||
Reference in New Issue
Block a user