Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

magic-removal: Fixed #1539: Fixed multithreading problem in QueryDict…

…. Thanks, Eugene and Alex Brown

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2577 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 15b5f4ded23217983d937442ebe0ec197d09846f 1 parent ff3810c
@adrianholovaty adrianholovaty authored
Showing with 31 additions and 19 deletions.
  1. +18 −11 django/http/__init__.py
  2. +13 −8 django/utils/datastructures.py
View
29 django/http/__init__.py
@@ -69,21 +69,34 @@ def parse_file_upload(header_dict, post_data):
class QueryDict(MultiValueDict):
"""A specialized MultiValueDict that takes a query string when initialized.
This is immutable unless you create a copy of it."""
- def __init__(self, query_string):
+ def __init__(self, query_string, mutable=False):
MultiValueDict.__init__(self)
self._mutable = True
for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True
self.appendlist(key, value)
- self._mutable = False
+ self._mutable = mutable
def _assert_mutable(self):
if not self._mutable:
raise AttributeError, "This QueryDict instance is immutable"
- def _setitem_if_mutable(self, key, value):
+ def __setitem__(self, key, value):
self._assert_mutable()
MultiValueDict.__setitem__(self, key, value)
- __setitem__ = _setitem_if_mutable
+
+ def __copy__(self):
+ result = self.__class__('', mutable=True)
+ for key, value in dict.items(self):
+ dict.__setitem__(result, key, value)
+ return result
+
+ def __deepcopy__(self, memo={}):
+ import copy
+ result = self.__class__('', mutable=True)
+ memo[id(self)] = result
+ for key, value in dict.items(self):
+ dict.__setitem__(result, copy.deepcopy(key, memo), copy.deepcopy(value, memo))
+ return result
def setlist(self, key, list_):
self._assert_mutable()
@@ -115,13 +128,7 @@ def setdefault(self, *args):
def copy(self):
"Returns a mutable copy of this object."
- import copy
- # Our custom __setitem__ must be disabled for copying machinery.
- QueryDict.__setitem__ = dict.__setitem__
- cp = copy.deepcopy(self)
- QueryDict.__setitem__ = QueryDict._setitem_if_mutable
- cp._mutable = True
- return cp
+ return self.__deepcopy__()
def urlencode(self):
output = []
View
21 django/utils/datastructures.py
@@ -117,9 +117,19 @@ def __getitem__(self, key):
except IndexError:
return []
- def _setitem_list(self, key, value):
+ def __setitem__(self, key, value):
dict.__setitem__(self, key, [value])
- __setitem__ = _setitem_list
+
+ def __copy__(self):
+ return self.__class__(dict.items(self))
+
+ def __deepcopy__(self, memo={}):
+ import copy
+ result = self.__class__()
+ memo[id(self)] = result
+ for key, value in dict.items(self):
+ dict.__setitem__(result, copy.deepcopy(key, memo), copy.deepcopy(value, memo))
+ return result
def get(self, key, default=None):
"Returns the default value if the requested data doesn't exist"
@@ -173,12 +183,7 @@ def values(self):
def copy(self):
"Returns a copy of this object."
- import copy
- # Our custom __setitem__ must be disabled for copying machinery.
- MultiValueDict.__setitem__ = dict.__setitem__
- cp = copy.deepcopy(self)
- MultiValueDict.__setitem__ = MultiValueDict._setitem_list
- return cp
+ return self.__deepcopy__()
def update(self, other_dict):
"update() extends rather than replaces existing key lists."
Please sign in to comment.
Something went wrong with that request. Please try again.