]> granicus.if.org Git - python/commitdiff
Issue #9137: Fix issue in MutableMapping.update, which incorrectly
authorMark Dickinson <dickinsm@gmail.com>
Sun, 11 Jul 2010 18:53:06 +0000 (18:53 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Sun, 11 Jul 2010 18:53:06 +0000 (18:53 +0000)
treated keyword arguments called 'self' or 'other' specially.

Lib/_abcoll.py
Lib/test/test_collections.py
Misc/NEWS

index e9f06a5ed370bf867b3fcbed0e60ab14ce9fc34c..cc00fd9b5d5163ccf4a1d675342cff2f3812fd16 100644 (file)
@@ -480,7 +480,15 @@ class MutableMapping(Mapping):
         except KeyError:
             pass
 
-    def update(self, other=(), **kwds):
+    def update(*args, **kwds):
+        if len(args) > 2:
+            raise TypeError("update() takes at most 2 positional "
+                            "arguments ({} given)".format(len(args)))
+        elif not args:
+            raise TypeError("update() takes at least 1 argument (0 given)")
+        self = args[0]
+        other = args[1] if len(args) >= 2 else ()
+
         if isinstance(other, Mapping):
             for key in other:
                 self[key] = other[key]
index e595b754275848faa95b3b0895524215410b0d47..69c4a9f173efe6f72fd2f01a1b7bc3f850ee02d2 100644 (file)
@@ -758,6 +758,19 @@ class TestOrderedDict(unittest.TestCase):
         od.update([('a', 1), ('b', 2), ('c', 9), ('d', 4)], c=3, e=5)
         self.assertEqual(list(od.items()), pairs)                                   # mixed input
 
+        # Issue 9137: Named argument called 'other' or 'self'
+        # shouldn't be treated specially.
+        od = OrderedDict()
+        od.update(self=23)
+        self.assertEqual(list(od.items()), [('self', 23)])
+        od = OrderedDict()
+        od.update(other={})
+        self.assertEqual(list(od.items()), [('other', {})])
+        od = OrderedDict()
+        od.update(red=5, blue=6, other=7, self=8)
+        self.assertEqual(sorted(list(od.items())),
+                         [('blue', 6), ('other', 7), ('red', 5), ('self', 8)])
+
         # Make sure that direct calls to update do not clear previous contents
         # add that updates items are not moved to the end
         d = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 44), ('e', 55)])
index 474ae024ddb7ffd63b34919467a00fc01b774822..1b0c3d18f3d8d2ed533581fc9c1216dc03fdd060 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -473,6 +473,9 @@ C-API
 Library
 -------
 
+- Issue #9137: Fix issue in MutableMapping.update, which incorrectly
+  treated keyword arguments called 'self' or 'other' specially.
+
 - ``ast.literal_eval()`` now allows set literals.
 
 - Issue #9164: Ensure that sysconfig handles duplicate -arch flags in CFLAGS.