summaryrefslogtreecommitdiff
path: root/django/utils/datastructures.py
diff options
context:
space:
mode:
authorNick Pope <nick.pope@flightdataservices.com>2020-10-05 15:57:47 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-10-30 10:44:44 +0100
commit966b5b49b6521483f1c90b4499c4c80e80136de3 (patch)
tree3cdbeda0dae479a13839db9d001cbb35745772b3 /django/utils/datastructures.py
parent1a8ad8a5c6f3344959e81531177164d1c4c4e52a (diff)
Updated MultiValueDict.update() to mirror dict.update() behavior.
Changes in behavior include: - Accepting iteration over empty sequences, updating nothing. - Accepting iterable of 2-tuples providing key-value pairs. - Failing with the same or comparable exceptions for invalid input. Notably this replaces the previous attempt to catch TypeError which was unreachable as the call to .items() resulted in AttributeError on non-dict objects.
Diffstat (limited to 'django/utils/datastructures.py')
-rw-r--r--django/utils/datastructures.py15
1 files changed, 7 insertions, 8 deletions
diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py
index 740ad5a86d..871b016715 100644
--- a/django/utils/datastructures.py
+++ b/django/utils/datastructures.py
@@ -194,16 +194,15 @@ class MultiValueDict(dict):
if len(args) > 1:
raise TypeError("update expected at most 1 argument, got %d" % len(args))
if args:
- other_dict = args[0]
- if isinstance(other_dict, MultiValueDict):
- for key, value_list in other_dict.lists():
+ arg = args[0]
+ if isinstance(arg, MultiValueDict):
+ for key, value_list in arg.lists():
self.setlistdefault(key).extend(value_list)
else:
- try:
- for key, value in other_dict.items():
- self.setlistdefault(key).append(value)
- except TypeError:
- raise ValueError("MultiValueDict.update() takes either a MultiValueDict or dictionary")
+ if isinstance(arg, Mapping):
+ arg = arg.items()
+ for key, value in arg:
+ self.setlistdefault(key).append(value)
for key, value in kwargs.items():
self.setlistdefault(key).append(value)