diff options
author | Raymond Hettinger <python@rcn.com> | 2004-03-04 08:25:44 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2004-03-04 08:25:44 (GMT) |
commit | 31017aed36a5c5b0e4b16ca58bea09c9ce360134 (patch) | |
tree | 766d70bb4fbb6878a71c81fc3874515cfcbc8aa8 /Lib | |
parent | 6c79a518e70ea8e45e3287573d99c648ae3cb21b (diff) | |
download | cpython-31017aed36a5c5b0e4b16ca58bea09c9ce360134.zip cpython-31017aed36a5c5b0e4b16ca58bea09c9ce360134.tar.gz cpython-31017aed36a5c5b0e4b16ca58bea09c9ce360134.tar.bz2 |
SF #904720: dict.update should take a 2-tuple sequence like dict.__init_
(Championed by Bob Ippolito.)
The update() method for mappings now accepts all the same argument forms
as the dict() constructor. This includes item lists and/or keyword
arguments.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/UserDict.py | 28 | ||||
-rw-r--r-- | Lib/os.py | 6 | ||||
-rw-r--r-- | Lib/test/test_call.py | 18 | ||||
-rw-r--r-- | Lib/test/test_types.py | 2 | ||||
-rw-r--r-- | Lib/test/test_userdict.py | 6 | ||||
-rw-r--r-- | Lib/weakref.py | 22 |
6 files changed, 48 insertions, 34 deletions
diff --git a/Lib/UserDict.py b/Lib/UserDict.py index 8141e7f..87cc6a3 100644 --- a/Lib/UserDict.py +++ b/Lib/UserDict.py @@ -4,8 +4,6 @@ class UserDict: def __init__(self, dict=None, **kwargs): self.data = {} if dict is not None: - if not hasattr(dict,'keys'): - dict = type({})(dict) # make mapping from a sequence self.update(dict) if len(kwargs): self.update(kwargs) @@ -39,14 +37,18 @@ class UserDict: def itervalues(self): return self.data.itervalues() def values(self): return self.data.values() def has_key(self, key): return self.data.has_key(key) - def update(self, dict): - if isinstance(dict, UserDict): + def update(self, dict=None, **kwargs): + if dict is None: + pass + elif isinstance(dict, UserDict): self.data.update(dict.data) - elif isinstance(dict, type(self.data)): + elif isinstance(dict, type({})) or not hasattr(dict, 'items'): self.data.update(dict) else: for k, v in dict.items(): self[k] = v + if len(kwargs): + self.data.update(kwargs) def get(self, key, failobj=None): if not self.has_key(key): return failobj @@ -136,17 +138,21 @@ class DictMixin: raise KeyError, 'container is empty' del self[k] return (k, v) - def update(self, other): + def update(self, other=None, **kwargs): # Make progressively weaker assumptions about "other" - if hasattr(other, 'iteritems'): # iteritems saves memory and lookups + if other is None: + pass + elif hasattr(other, 'iteritems'): # iteritems saves memory and lookups for k, v in other.iteritems(): self[k] = v - elif hasattr(other, '__iter__'): # iter saves memory - for k in other: - self[k] = other[k] - else: + elif hasattr(other, 'keys'): for k in other.keys(): self[k] = other[k] + else: + for k, v in other: + self[k] = v + if kwargs: + self.update(kwargs) def get(self, key, default=None): try: return self[key] @@ -433,9 +433,6 @@ else: return key.upper() in self.data def get(self, key, failobj=None): return self.data.get(key.upper(), failobj) - def update(self, dict): - for k, v in dict.items(): - self[k] = v def copy(self): return dict(self) @@ -447,9 +444,6 @@ else: def __setitem__(self, key, item): putenv(key, item) self.data[key] = item - def update(self, dict): - for k, v in dict.items(): - self[k] = v try: unsetenv except NameError: diff --git a/Lib/test/test_call.py b/Lib/test/test_call.py index 99554c7..f3c7c8c 100644 --- a/Lib/test/test_call.py +++ b/Lib/test/test_call.py @@ -86,41 +86,41 @@ class CFunctionCalls(unittest.TestCase): self.assertRaises(TypeError, {}.keys, x=2, y=2) def test_oldargs1_0(self): - self.assertRaises(TypeError, {}.update) + self.assertRaises(TypeError, [].count) def test_oldargs1_1(self): - {}.update({}) + [].count(1) def test_oldargs1_2(self): - self.assertRaises(TypeError, {}.update, {}, 1) + self.assertRaises(TypeError, [].count, 1, 2) def test_oldargs1_0_ext(self): try: - {}.update(*()) + [].count(*()) except TypeError: pass else: raise RuntimeError def test_oldargs1_1_ext(self): - {}.update(*({},)) + [].count(*(1,)) def test_oldargs1_2_ext(self): try: - {}.update(*({}, 2)) + [].count(*(1, 2)) except TypeError: pass else: raise RuntimeError def test_oldargs1_0_kw(self): - self.assertRaises(TypeError, {}.update, x=2) + self.assertRaises(TypeError, [].count, x=2) def test_oldargs1_1_kw(self): - self.assertRaises(TypeError, {}.update, {}, x=2) + self.assertRaises(TypeError, [].count, {}, x=2) def test_oldargs1_2_kw(self): - self.assertRaises(TypeError, {}.update, x=2, y=2) + self.assertRaises(TypeError, [].count, x=2, y=2) def test_main(): diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index aa8f854..4b4e19c 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -253,7 +253,7 @@ d.update({1:1, 2:2, 3:3}) if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict update' d.clear() try: d.update(None) -except AttributeError: pass +except (TypeError, AttributeError): pass else: raise TestFailed, 'dict.update(None), AttributeError expected' class SimpleUserDict: def __init__(self): diff --git a/Lib/test/test_userdict.py b/Lib/test/test_userdict.py index 602fd90..0ed4369 100644 --- a/Lib/test/test_userdict.py +++ b/Lib/test/test_userdict.py @@ -93,8 +93,12 @@ class TestMappingProtocol(unittest.TestCase): #update p.update(self.reference) self.assertEqual(dict(p), self.reference) + items = p.items() + p = self._empty_mapping() + p.update(items) + self.assertEqual(dict(p), self.reference) d = self._full_mapping(self.reference) - #setdefaullt + #setdefault key, value = d.iteritems().next() knownkey, knownvalue = self.other.iteritems().next() self.assertEqual(d.setdefault(key, knownvalue), value) diff --git a/Lib/weakref.py b/Lib/weakref.py index 09bed65..5c66186 100644 --- a/Lib/weakref.py +++ b/Lib/weakref.py @@ -122,10 +122,15 @@ class WeakValueDictionary(UserDict.UserDict): else: return wr() - def update(self, dict): + def update(self, dict=None, **kwargs): d = self.data - for key, o in dict.items(): - d[key] = ref(o, self.__makeremove(key)) + if dict is not None: + if not hasattr(dict, "items"): + dict = type({})(dict) + for key, o in dict.items(): + d[key] = ref(o, self.__makeremove(key)) + if len(kwargs): + self.update(kwargs) def values(self): L = [] @@ -239,10 +244,15 @@ class WeakKeyDictionary(UserDict.UserDict): def setdefault(self, key, default): return self.data.setdefault(ref(key, self._remove),default) - def update(self, dict): + def update(self, dict=None, **kwargs): d = self.data - for key, value in dict.items(): - d[ref(key, self._remove)] = value + if dict is not None: + if not hasattr(dict, "items"): + dict = type({})(dict) + for key, value in dict.items(): + d[ref(key, self._remove)] = value + if len(kwargs): + self.update(kwargs) class BaseIter: |