|
| 1 | +import copy |
| 2 | +import tempfile |
| 3 | +import pickle |
| 4 | +import threading |
| 5 | + |
| 6 | +from django.db.models import Q |
| 7 | +from django.test import TestCase |
| 8 | + |
| 9 | +from polymorphic.tests.models import Bottom, Middle, Top |
| 10 | +from polymorphic.query_translate import translate_polymorphic_filter_definitions_in_args |
| 11 | + |
| 12 | + |
| 13 | +class QueryTranslateTests(TestCase): |
| 14 | + |
| 15 | + def test_translate_with_not_pickleable_query(self): |
| 16 | + """ |
| 17 | + In some cases, Django may attacha _thread object to the query and we |
| 18 | + will get the following when we try to deepcopy inside of |
| 19 | + translate_polymorphic_filter_definitions_in_args: |
| 20 | +
|
| 21 | + TypeError: cannot pickle '_thread.lock' object |
| 22 | +
|
| 23 | +
|
| 24 | + For this to trigger, we need to somehoe go down this path: |
| 25 | +
|
| 26 | + File "/perfdash/.venv/lib64/python3.12/site-packages/polymorphic/query_translate.py", line 95, in translate_polymorphic_filter_definitions_in_args |
| 27 | + translate_polymorphic_Q_object(queryset_model, copy.deepcopy(q), using=using) for q in args |
| 28 | + ^^^^^^^^^^^^^^^^ |
| 29 | + File "/usr/lib64/python3.12/copy.py", line 143, in deepcopy |
| 30 | + y = copier(memo) |
| 31 | + ^^^^^^^^^^^^ |
| 32 | + File "/perfdash/.venv/lib64/python3.12/site-packages/django/utils/tree.py", line 53, in __deepcopy__ |
| 33 | + obj.children = copy.deepcopy(self.children, memodict) |
| 34 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 35 | + File "/usr/lib64/python3.12/copy.py", line 136, in deepcopy |
| 36 | + y = copier(x, memo) |
| 37 | + ^^^^^^^^^^^^^^^ |
| 38 | +
|
| 39 | + Internals in Django, somehow we must trigger this tree.py code in django via |
| 40 | + the deepcopy in order to trigger this. |
| 41 | +
|
| 42 | + """ |
| 43 | + |
| 44 | + with tempfile.TemporaryFile() as fd: |
| 45 | + # verify this is definitely not pickleable |
| 46 | + with self.assertRaises(TypeError): |
| 47 | + pickle.dumps(threading.Lock()) |
| 48 | + |
| 49 | + # I know this doesn't make sense to pass as a Q(), but |
| 50 | + # I haven't found another way to trigger the copy.deepcopy failing. |
| 51 | + q = Q(blog__info='blog info') | Q(blog__info=threading.Lock()) |
| 52 | + |
| 53 | + translate_polymorphic_filter_definitions_in_args(Bottom, args=[q]) |
0 commit comments