From 4c6a6d5ac7aa348d5b4b9a4d6bb7cc18af49f72a Mon Sep 17 00:00:00 2001 From: Jonathan Davis Date: Thu, 29 Jul 2021 06:04:56 +0100 Subject: [PATCH] Fixed #23895 -- Prevented pickling of ResolverMatch. Pickling a ResolverMatch did not work correctly in many cases, especially with CBVs and URLResolvers in the list of tried URL paths. --- AUTHORS | 1 + django/urls/resolvers.py | 4 ++++ tests/urlpatterns_reverse/tests.py | 7 +++++++ 3 files changed, 12 insertions(+) diff --git a/AUTHORS b/AUTHORS index 0b7dd6ad72..3a8df03ca3 100644 --- a/AUTHORS +++ b/AUTHORS @@ -478,6 +478,7 @@ answer newbie questions, and generally made Django that much better: Jökull Sólberg Auðunsson Jon Dufresne Jonas Haag + Jonathan Davis Jonatas C. D. Jonathan Buchanan Jonathan Daugherty (cygnus) diff --git a/django/urls/resolvers.py b/django/urls/resolvers.py index 3a53bce1de..6ea18dd22f 100644 --- a/django/urls/resolvers.py +++ b/django/urls/resolvers.py @@ -10,6 +10,7 @@ import inspect import re import string from importlib import import_module +from pickle import PicklingError from urllib.parse import quote from asgiref.local import Local @@ -71,6 +72,9 @@ class ResolverMatch: ) ) + def __reduce_ex__(self, protocol): + raise PicklingError(f'Cannot pickle {self.__class__.__qualname__}.') + def get_resolver(urlconf=None): if urlconf is None: diff --git a/tests/urlpatterns_reverse/tests.py b/tests/urlpatterns_reverse/tests.py index f2102c5304..332bc19e76 100644 --- a/tests/urlpatterns_reverse/tests.py +++ b/tests/urlpatterns_reverse/tests.py @@ -1,6 +1,7 @@ """ Unit tests for reverse URL lookups. """ +import pickle import sys import threading @@ -1167,6 +1168,12 @@ class ResolverMatchTests(SimpleTestCase): f"route='{name}/')", ) + @override_settings(ROOT_URLCONF='urlpatterns.path_urls') + def test_pickling(self): + msg = 'Cannot pickle ResolverMatch.' + with self.assertRaisesMessage(pickle.PicklingError, msg): + pickle.dumps(resolve('/users/')) + @override_settings(ROOT_URLCONF='urlpatterns_reverse.erroneous_urls') class ErroneousViewTests(SimpleTestCase):