[pypy-svn] r65493 - in pypy/branch/io-lang/pypy/lang/io: . test

david at codespeak.net david at codespeak.net
Fri May 29 12:26:19 CEST 2009


Author: david
Date: Fri May 29 12:26:17 2009
New Revision: 65493

Added:
   pypy/branch/io-lang/pypy/lang/io/map.py
   pypy/branch/io-lang/pypy/lang/io/test/test_map.py
Modified:
   pypy/branch/io-lang/pypy/lang/io/model.py
   pypy/branch/io-lang/pypy/lang/io/objspace.py
Log:
Added map type for key value collections and two methods (clone, atPut)

Added: pypy/branch/io-lang/pypy/lang/io/map.py
==============================================================================
--- (empty file)
+++ pypy/branch/io-lang/pypy/lang/io/map.py	Fri May 29 12:26:17 2009
@@ -0,0 +1,6 @@
+from pypy.lang.io.register import register_method
+
+ at register_method('Map', 'atPut', unwrap_spec=[object, object, object])
+def map_at_put(space, target, key, value):
+    target.items[key] = value
+    return target
\ No newline at end of file

Modified: pypy/branch/io-lang/pypy/lang/io/model.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/model.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/model.py	Fri May 29 12:26:17 2009
@@ -73,6 +73,15 @@
         l.items += items
         return l
         
+class W_Map(W_Object):
+    """A key/value dictionary appropriate for holding large key/value collections."""
+    def __init__(self, space, protos = [], items = {}):
+        W_Object.__init__(self, space, protos)
+        self.items = items
+        
+    def clone(self):
+        return W_Map(self.space, [self], dict(self.items))
+        
 class W_ImmutableSequence(W_Object):
     def __init__(self, space, string):
         self.value = string

Modified: pypy/branch/io-lang/pypy/lang/io/objspace.py
==============================================================================
--- pypy/branch/io-lang/pypy/lang/io/objspace.py	(original)
+++ pypy/branch/io-lang/pypy/lang/io/objspace.py	Fri May 29 12:26:17 2009
@@ -1,5 +1,5 @@
 from pypy.rlib.objectmodel import instantiate
-from pypy.lang.io.model import W_Number, W_Object, W_CFunction, W_Block, W_Message, W_List
+from pypy.lang.io.model import W_Number, W_Object, W_CFunction, W_Block, W_Message, W_List, W_Map
 from pypy.lang.io.register import cfunction_definitions
 
 import pypy.lang.io.number
@@ -8,6 +8,8 @@
 import pypy.lang.io.list
 import pypy.lang.io.call
 import pypy.lang.io.message
+import pypy.lang.io.map
+
 class ObjSpace(object):
     """docstring for ObjSpace"""
     def __init__(self):
@@ -22,7 +24,7 @@
         self.w_nil = W_Object(self, [self.w_object])
         self.w_list = W_List(self, [self.w_object])
         self.w_call = W_Object(self)
-        
+        self.w_map = W_Map(self, [self.w_object])
         
         self.init_w_object()        
         
@@ -44,6 +46,12 @@
         
         self.init_w_call()
          
+        self.init_w_map()
+        
+    def init_w_map(self):
+        for key, function in cfunction_definitions['Map'].items():
+            self.w_map.slots[key] = W_CFunction(self, function)
+            
     def init_w_call(self):
         for key, function in cfunction_definitions['Call'].items():
             self.w_call.slots[key] = W_CFunction(self, function)
@@ -70,6 +78,7 @@
         self.w_core.slots['nil'] = self.w_nil
         self.w_core.slots['List'] = self.w_list
         self.w_core.slots['Call'] = self.w_call
+        self.w_core.slots['Map'] = self.w_map
 
     def init_w_number(self):
         self.w_number = instantiate(W_Number)

Added: pypy/branch/io-lang/pypy/lang/io/test/test_map.py
==============================================================================
--- (empty file)
+++ pypy/branch/io-lang/pypy/lang/io/test/test_map.py	Fri May 29 12:26:17 2009
@@ -0,0 +1,24 @@
+from pypy.lang.io.parserhack import parse, interpret
+from pypy.lang.io.model import W_Map, W_Number, W_ImmutableSequence
+import py.test
+
+def test_map_proto():
+    inp = 'Lobby Core Map'
+    res, space = interpret(inp)
+    assert res == space.w_map
+    assert isinstance(res, W_Map)
+    
+def test_map_clone():
+    inp = 'Map clone'
+    res, space = interpret(inp)
+    assert isinstance(res, W_Map)
+    assert res.protos == [space.w_map]
+    assert space.w_map.protos == [space.w_object]
+    
+def test_at_put():
+    inp = 'Map clone atPut("foo", "bar")'
+    res, space = interpret(inp)
+    keys = [(key.value) for key in res.items.keys()]
+    assert keys == ['foo']
+    values = [(val.value) for val in res.items.values()]
+    assert values == ['bar']
\ No newline at end of file



More information about the Pypy-commit mailing list