Commits

Samuele Pedroni committed b5fde29

added delitem support for dicts.

Comments (0)

Files changed (2)

pypy/objspace/std/dictobject.py

     #print 'dict append %s:' % w_newkey, data
 
 StdObjSpace.setitem.register(setitem_dict_any_any, W_DictObject, W_ANY, W_ANY)
+
+def delitem_dict_any(space, w_dict, w_lookup):
+    data = w_dict.data
+    for i in range(len(data)):
+        w_key, w_value = data[i]
+        if space.is_true(space.eq(w_lookup, w_key)):
+            del data[i]
+            return
+    raise OperationError(space.w_KeyError, w_lookup)
+    
+StdObjSpace.delitem.register(delitem_dict_any, W_DictObject, W_ANY)

pypy/objspace/std/test/test_dictobject.py

         wback = space.getitem(d,wk1)
         self.assertEqual_w(wback,wone)
 
+    def test_delitem(self):
+        space = self.space
+        wk1 = space.wrap('key')
+        d = dobj.W_DictObject([(space.wrap('zero'),space.wrap(0)),
+                               (space.wrap('one'),space.wrap(1)),
+                               (space.wrap('two'),space.wrap(2))])
+        space.delitem(d,space.wrap('one'))
+        self.assertEqual_w(space.getitem(d,space.wrap('zero')),space.wrap(0))
+        self.assertEqual_w(space.getitem(d,space.wrap('two')),space.wrap(2))
+        self.failUnlessRaises(OperationError,space.getitem,d,space.wrap('one'))
+
+
 if __name__ == '__main__':
     unittest.main()