[pypy-svn] r39911 - pypy/branch/pypy-2.5/pypy/objspace/std

xoraxax at codespeak.net xoraxax at codespeak.net
Sun Mar 4 18:46:40 CET 2007


Author: xoraxax
Date: Sun Mar  4 18:46:38 2007
New Revision: 39911

Modified:
   pypy/branch/pypy-2.5/pypy/objspace/std/dictmultiobject.py
   pypy/branch/pypy-2.5/pypy/objspace/std/dictobject.py
Log:
Implement the __missing__ method of Python 2.5.

Modified: pypy/branch/pypy-2.5/pypy/objspace/std/dictmultiobject.py
==============================================================================
--- pypy/branch/pypy-2.5/pypy/objspace/std/dictmultiobject.py	(original)
+++ pypy/branch/pypy-2.5/pypy/objspace/std/dictmultiobject.py	Sun Mar  4 18:46:38 2007
@@ -138,6 +138,7 @@
 
     def get(self, w_lookup):
         return None
+
     def setitem(self, w_key, w_value):
         if _is_str(self.space, w_key):
             return StrDictImplementation(self.space).setitem_str(w_key, w_value)
@@ -1010,6 +1011,15 @@
             result[key] = val
         return result
 
+    def missing_method(w_dict, space, w_key):
+        if not space.is_w(space.type(w_dict), space.w_dict):
+            w_missing = space.lookup(w_dict, "__missing__")
+            if w_missing is None:
+                return None
+            return space.call_function(w_missing, w_dict, w_key)
+        else:
+            return None
+
     def len(w_self):
         return w_self.implementation.length()
 
@@ -1054,6 +1064,11 @@
     w_value = w_dict.implementation.get(w_lookup)
     if w_value is not None:
         return w_value
+
+    w_missing_item = w_dict.missing_method(space, w_lookup)
+    if w_missing_item is not None:
+        return w_missing_item
+
     raise OperationError(space.w_KeyError, w_lookup)
 
 def setitem__DictMulti_ANY_ANY(space, w_dict, w_newkey, w_newvalue):

Modified: pypy/branch/pypy-2.5/pypy/objspace/std/dictobject.py
==============================================================================
--- pypy/branch/pypy-2.5/pypy/objspace/std/dictobject.py	(original)
+++ pypy/branch/pypy-2.5/pypy/objspace/std/dictobject.py	Sun Mar  4 18:46:38 2007
@@ -41,6 +41,15 @@
     def get(w_dict, w_lookup, w_default):
         return w_dict.content.get(w_lookup, w_default)
 
+    def missing_method(w_dict, space, w_key):
+        if not space.is_w(space.type(w_dict), space.w_dict):
+            w_missing = space.lookup(w_dict, "__missing__")
+            if w_missing is None:
+                return None
+            return space.call_function(w_missing, w_dict, w_key)
+        else:
+            return None
+
     def set_str_keyed_item(w_dict, w_key, w_value, shadows_type=True):
         w_dict.content[w_key] = w_value
 
@@ -73,7 +82,11 @@
     try:
         return w_dict.content[w_lookup]
     except KeyError:
-        raise OperationError(space.w_KeyError, w_lookup)
+        w_missing_item = w_dict.missing_method(space, w_lookup)
+        if w_missing_item is None:
+            raise OperationError(space.w_KeyError, w_lookup)
+        else:
+            return w_missing_item
 
 def setitem__Dict_ANY_ANY(space, w_dict, w_newkey, w_newvalue):
     w_dict.content[w_newkey] = w_newvalue



More information about the Pypy-commit mailing list