[pypy-commit] pypy default: added interp_magic method to get the strategy of a list on the application level

l.diekmann noreply at buildbot.pypy.org
Thu Dec 1 11:50:28 CET 2011


Author: Lukas Diekmann <lukas.diekmann at uni-duesseldorf.de>
Branch: 
Changeset: r50032:497907d5d515
Date: 2011-12-01 11:50 +0100
http://bitbucket.org/pypy/pypy/changeset/497907d5d515/

Log:	added interp_magic method to get the strategy of a list on the
	application level

diff --git a/pypy/module/__pypy__/__init__.py b/pypy/module/__pypy__/__init__.py
--- a/pypy/module/__pypy__/__init__.py
+++ b/pypy/module/__pypy__/__init__.py
@@ -27,6 +27,7 @@
         'builtinify'                : 'interp_magic.builtinify',
         'lookup_special'            : 'interp_magic.lookup_special',
         'do_what_I_mean'            : 'interp_magic.do_what_I_mean',
+        'list_strategy'             : 'interp_magic.list_strategy',
     }
 
     submodules = {
diff --git a/pypy/module/__pypy__/interp_magic.py b/pypy/module/__pypy__/interp_magic.py
--- a/pypy/module/__pypy__/interp_magic.py
+++ b/pypy/module/__pypy__/interp_magic.py
@@ -4,7 +4,7 @@
 from pypy.rlib.objectmodel import we_are_translated
 from pypy.objspace.std.typeobject import MethodCache
 from pypy.objspace.std.mapdict import IndexCache
-
+from pypy.objspace.std.listobject import W_ListObject, IntegerListStrategy, StringListStrategy, FloatListStrategy, RangeListStrategy, EmptyListStrategy, ObjectListStrategy
 
 def internal_repr(space, w_object):
     return space.wrap('%r' % (w_object,))
@@ -73,3 +73,9 @@
 
 def do_what_I_mean(space):
     return space.wrap(42)
+
+def list_strategy(space, w_list):
+    str_type = None
+    if isinstance(w_list, W_ListObject):
+        str_type = w_list.strategy._type
+    return space.wrap(str_type)
diff --git a/pypy/module/__pypy__/test/test_special.py b/pypy/module/__pypy__/test/test_special.py
--- a/pypy/module/__pypy__/test/test_special.py
+++ b/pypy/module/__pypy__/test/test_special.py
@@ -5,7 +5,7 @@
     def setup_class(cls):
         if option.runappdirect:
             py.test.skip("does not make sense on pypy-c")
-        cls.space = gettestobjspace(**{"objspace.usemodules.select": False})
+        cls.space = gettestobjspace(**{"objspace.usemodules.select": False, "objspace.std.withrangelist": True})
 
     def test__isfake(self):
         from __pypy__ import isfake
@@ -54,3 +54,20 @@
         from __pypy__ import do_what_I_mean
         x = do_what_I_mean()
         assert x == 42
+
+    def test_list_strategy(self):
+        from __pypy__ import list_strategy
+        l = [1,2,3]
+        assert list_strategy(l) == "int"
+        l = ["a","b","c"]
+        assert list_strategy(l) == "str"
+        l = [1.1,2.2,3.3]
+        assert list_strategy(l) == "float"
+        l = range(3)
+        assert list_strategy(l) == "range"
+        l = [1,"b",3]
+        assert list_strategy(l) == "object"
+        l = []
+        assert list_strategy(l) == "empty"
+        o = 5
+        assert list_strategy(o) == None
diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py
--- a/pypy/objspace/std/listobject.py
+++ b/pypy/objspace/std/listobject.py
@@ -326,6 +326,8 @@
     to the added item.
     W_Lists do not switch back to EmptyListStrategy when becoming empty again."""
 
+    _type = "empty"
+
     def __init__(self, space):
         ListStrategy.__init__(self, space)
         # cache an empty list that is used whenever getitems is called (i.e. sorting)
@@ -426,6 +428,8 @@
     On any operation destroying the range (inserting, appending non-ints)
     the strategy is switched to IntegerListStrategy."""
 
+    _type = "range"
+
     def switch_to_integer_strategy(self, w_list):
         items = self._getitems_range(w_list, False)
         strategy = w_list.strategy = self.space.fromcache(IntegerListStrategy)
@@ -864,6 +868,7 @@
 
 class ObjectListStrategy(AbstractUnwrappedStrategy, ListStrategy):
     _none_value = None
+    _type = "object"
 
     def unwrap(self, w_obj):
         return w_obj
@@ -892,6 +897,7 @@
 
 class IntegerListStrategy(AbstractUnwrappedStrategy, ListStrategy):
     _none_value = 0
+    _type = "int"
 
     def wrap(self, intval):
         return self.space.wrap(intval)
@@ -918,6 +924,7 @@
 
 class FloatListStrategy(AbstractUnwrappedStrategy, ListStrategy):
     _none_value = 0.0
+    _type = "float"
 
     def wrap(self, floatval):
         return self.space.wrap(floatval)
@@ -944,6 +951,7 @@
 
 class StringListStrategy(AbstractUnwrappedStrategy, ListStrategy):
     _none_value = None
+    _type = "str"
 
     def wrap(self, stringval):
         return self.space.wrap(stringval)


More information about the pypy-commit mailing list