Skip to content

Commit

Permalink
Fixed #23895 -- Prevented pickling of ResolverMatch.
Browse files Browse the repository at this point in the history
Pickling a ResolverMatch did not work correctly in many cases,
especially with CBVs and URLResolvers in the list of tried URL paths.
  • Loading branch information
Jonathan Davis authored Jul 29, 2021
1 parent 85d47a5 commit 4c6a6d5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ answer newbie questions, and generally made Django that much better:
Jökull Sólberg Auðunsson <[email protected]>
Jon Dufresne <[email protected]>
Jonas Haag <[email protected]>
Jonathan Davis <[email protected]>
Jonatas C. D. <[email protected]>
Jonathan Buchanan <[email protected]>
Jonathan Daugherty (cygnus) <http://www.cprogrammer.org/>
Expand Down
4 changes: 4 additions & 0 deletions django/urls/resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import re
import string
from importlib import import_module
from pickle import PicklingError
from urllib.parse import quote

from asgiref.local import Local
Expand Down Expand Up @@ -71,6 +72,9 @@ def __repr__(self):
)
)

def __reduce_ex__(self, protocol):
raise PicklingError(f'Cannot pickle {self.__class__.__qualname__}.')


def get_resolver(urlconf=None):
if urlconf is None:
Expand Down
7 changes: 7 additions & 0 deletions tests/urlpatterns_reverse/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Unit tests for reverse URL lookups.
"""
import pickle
import sys
import threading

Expand Down Expand Up @@ -1167,6 +1168,12 @@ def test_repr_functools_partial(self):
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):
Expand Down

0 comments on commit 4c6a6d5

Please sign in to comment.