diff --git a/src/polymorphic/query_translate.py b/src/polymorphic/query_translate.py index 43b7391f..10a78191 100644 --- a/src/polymorphic/query_translate.py +++ b/src/polymorphic/query_translate.py @@ -2,7 +2,6 @@ PolymorphicQuerySet support functions """ -import copy from collections import deque from django.apps import apps @@ -80,6 +79,35 @@ def tree_node_correct_field_specs(my_model, node): return potential_q_object +def _deepcopy_q_object(q): + """ + Make a deepcopy of a Q-object. + """ + + def _copy_child(child): + if isinstance(child, tuple): + return child # tuples are immutable, no need to make a copy. + elif isinstance(child, Q): + return _deepcopy_q_object(child) + else: + raise RuntimeError("Unknown child type: %s", type(child)) + + children = [_copy_child(c) for c in q.children] + + if hasattr(q, "copy"): # Django 4.2+ + obj = q.copy() + # This assignment of obj.children from children (created above) + # is required for test_query_filter_exclude_is_immutable to pass + # because somehow the capitalization changed for q_to_reuse? + # + # assert q_to_reuse.children == untouched_q_object.children + # AssertionError: assert [('model2b__f... 'something')] == [('Model2B___... 'something')] + obj.children = children + else: + obj = Q(*children, _connector=q.connector, _negated=q.negated) + return obj + + def translate_polymorphic_filter_definitions_in_args(queryset_model, args, using=DEFAULT_DB_ALIAS): """ Translate the non-keyword argument list for PolymorphicQuerySet.filter() @@ -92,7 +120,8 @@ def translate_polymorphic_filter_definitions_in_args(queryset_model, args, using Returns: modified Q objects """ return [ - translate_polymorphic_Q_object(queryset_model, copy.deepcopy(q), using=using) for q in args + translate_polymorphic_Q_object(queryset_model, _deepcopy_q_object(q), using=using) + for q in args ] diff --git a/src/polymorphic/tests/test_query_translate.py b/src/polymorphic/tests/test_query_translate.py index e69de29b..841d0a3c 100644 --- a/src/polymorphic/tests/test_query_translate.py +++ b/src/polymorphic/tests/test_query_translate.py @@ -0,0 +1,52 @@ +import copy +import tempfile +import pickle +import threading + +from django.db.models import Q +from django.test import TestCase + +from polymorphic.tests.models import Bottom, Middle, Top +from polymorphic.query_translate import translate_polymorphic_filter_definitions_in_args + + +class QueryTranslateTests(TestCase): + def test_translate_with_not_pickleable_query(self): + """ + In some cases, Django may attacha _thread object to the query and we + will get the following when we try to deepcopy inside of + translate_polymorphic_filter_definitions_in_args: + + TypeError: cannot pickle '_thread.lock' object + + + For this to trigger, we need to somehoe go down this path: + + File "/perfdash/.venv/lib64/python3.12/site-packages/polymorphic/query_translate.py", line 95, in translate_polymorphic_filter_definitions_in_args + translate_polymorphic_Q_object(queryset_model, copy.deepcopy(q), using=using) for q in args + ^^^^^^^^^^^^^^^^ + File "/usr/lib64/python3.12/copy.py", line 143, in deepcopy + y = copier(memo) + ^^^^^^^^^^^^ + File "/perfdash/.venv/lib64/python3.12/site-packages/django/utils/tree.py", line 53, in __deepcopy__ + obj.children = copy.deepcopy(self.children, memodict) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + File "/usr/lib64/python3.12/copy.py", line 136, in deepcopy + y = copier(x, memo) + ^^^^^^^^^^^^^^^ + + Internals in Django, somehow we must trigger this tree.py code in django via + the deepcopy in order to trigger this. + + """ + + with tempfile.TemporaryFile() as fd: + # verify this is definitely not pickleable + with self.assertRaises(TypeError): + pickle.dumps(threading.Lock()) + + # I know this doesn't make sense to pass as a Q(), but + # I haven't found another way to trigger the copy.deepcopy failing. + q = Q(blog__info="blog info") | Q(blog__info=threading.Lock()) + + translate_polymorphic_filter_definitions_in_args(Bottom, args=[q])