Commits

Armin Rigo  committed 3dec80f Merge

merge heads

  • Participants
  • Parent commits 8920909, 65c577d

Comments (0)

Files changed (6)

File pypy/module/__pypy__/__init__.py

         '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',
         'validate_fd'               : 'interp_magic.validate_fd',
         'resizelist_hint'           : 'interp_magic.resizelist_hint',
         'newlist_hint'              : 'interp_magic.newlist_hint',
         'add_memory_pressure'       : 'interp_magic.add_memory_pressure',
         'newdict'                   : 'interp_dict.newdict',
-        'dictstrategy'              : 'interp_dict.dictstrategy',
+        'strategy'                  : 'interp_magic.strategy',  # dict,set,list
         'set_debug'                 : 'interp_magic.set_debug',
         'locals_to_fast'            : 'interp_magic.locals_to_fast',
     }

File pypy/module/__pypy__/interp_dict.py

 
 from pypy.interpreter.error import OperationError, oefmt
 from pypy.interpreter.gateway import unwrap_spec
-from pypy.objspace.std.dictmultiobject import W_DictMultiObject
 
 @unwrap_spec(type=str)
 def newdict(space, type):
         return space.newdict(strdict=True)
     else:
         raise oefmt(space.w_TypeError, "unknown type of dict %s", type)
-
-def dictstrategy(space, w_obj):
-    """ dictstrategy(dict)
-
-    show the underlaying strategy used by a dict object
-    """
-    if not isinstance(w_obj, W_DictMultiObject):
-        raise OperationError(space.w_TypeError,
-                             space.wrap("expecting dict object"))
-    return space.wrap('%r' % (w_obj.strategy,))

File pypy/module/__pypy__/interp_magic.py

 from pypy.interpreter.gateway import unwrap_spec
 from pypy.interpreter.pyframe import PyFrame
 from rpython.rlib.objectmodel import we_are_translated
+from pypy.objspace.std.dictmultiobject import W_DictMultiObject
 from pypy.objspace.std.listobject import W_ListObject
+from pypy.objspace.std.setobject import W_BaseSetObject
 from pypy.objspace.std.typeobject import MethodCache
 from pypy.objspace.std.mapdict import MapAttrCache
 from rpython.rlib import rposix, rgc
 def do_what_I_mean(space):
     return space.wrap(42)
 
-def list_strategy(space, w_list):
-    if isinstance(w_list, W_ListObject):
-        return space.wrap(w_list.strategy._applevel_repr)
+
+def _nameof(cls):
+    return cls.__name__
+_nameof._annspecialcase_ = 'specialize:memo'
+
+def strategy(space, w_obj):
+    """ strategy(dict or list or set)
+
+    Return the underlying strategy currently used by a dict, list or set object
+    """
+    if isinstance(w_obj, W_DictMultiObject):
+        name = _nameof(w_obj.strategy.__class__)
+    elif isinstance(w_obj, W_ListObject):
+        name = _nameof(w_obj.strategy.__class__)
+    elif isinstance(w_obj, W_BaseSetObject):
+        name = _nameof(w_obj.strategy.__class__)
     else:
-        w_msg = space.wrap("Can only get the list strategy of a list")
-        raise OperationError(space.w_TypeError, w_msg)
+        raise OperationError(space.w_TypeError,
+                             space.wrap("expecting dict or list or set object"))
+    return space.wrap(name)
+
 
 @unwrap_spec(fd='c_int')
 def validate_fd(space, fd):

File pypy/module/__pypy__/test/test_special.py

         assert x == 42
 
     def test_list_strategy(self):
-        from __pypy__ import list_strategy
+        from __pypy__ import strategy
 
         l = [1, 2, 3]
-        assert list_strategy(l) == "int"
+        assert strategy(l) == "IntegerListStrategy"
         l = ["a", "b", "c"]
-        assert list_strategy(l) == "bytes"
+        assert strategy(l) == "BytesListStrategy"
         l = [u"a", u"b", u"c"]
-        assert list_strategy(l) == "unicode"
+        assert strategy(l) == "UnicodeListStrategy"
         l = [1.1, 2.2, 3.3]
-        assert list_strategy(l) == "float"
+        assert strategy(l) == "FloatListStrategy"
         l = range(3)
-        assert list_strategy(l) == "simple_range"
+        assert strategy(l) == "SimpleRangeListStrategy"
         l = range(1, 2)
-        assert list_strategy(l) == "range"
+        assert strategy(l) == "RangeListStrategy"
         l = [1, "b", 3]
-        assert list_strategy(l) == "object"
+        assert strategy(l) == "ObjectListStrategy"
         l = []
-        assert list_strategy(l) == "empty"
+        assert strategy(l) == "EmptyListStrategy"
         o = 5
-        raises(TypeError, list_strategy, 5)
+        raises(TypeError, strategy, 5)
+
+    def test_dict_strategy(self):
+        from __pypy__ import strategy
+
+        d = {}
+        assert strategy(d) == "EmptyDictStrategy"
+        d = {1: None, 5: None}
+        assert strategy(d) == "IntDictStrategy"
+
+    def test_set_strategy(self):
+        from __pypy__ import strategy
+
+        s = set()
+        assert strategy(s) == "EmptySetStrategy"
+        s = set([2, 3, 4])
+        assert strategy(s) == "IntegerSetStrategy"
 
 
 class AppTestJitFeatures(object):

File pypy/objspace/std/listobject.py

     W_Lists do not switch back to EmptyListStrategy when becoming empty again.
     """
 
-    _applevel_repr = "empty"
-
     def __init__(self, space):
         ListStrategy.__init__(self, space)
 
        method providing only positive length. The storage is a one element tuple
        with positive integer storing length."""
 
-    _applevel_repr = "simple_range"
-
     erase, unerase = rerased.new_erasing_pair("simple_range")
     erase = staticmethod(erase)
     unerase = staticmethod(unerase)
     destroying the range (inserting, appending non-ints) the strategy is
     switched to IntegerListStrategy."""
 
-    _applevel_repr = "range"
-
     erase, unerase = rerased.new_erasing_pair("range")
     erase = staticmethod(erase)
     unerase = staticmethod(unerase)
     import_from_mixin(AbstractUnwrappedStrategy)
 
     _none_value = None
-    _applevel_repr = "object"
 
     def unwrap(self, w_obj):
         return w_obj
     import_from_mixin(AbstractUnwrappedStrategy)
 
     _none_value = 0
-    _applevel_repr = "int"
 
     def wrap(self, intval):
         return self.space.wrap(intval)
     import_from_mixin(AbstractUnwrappedStrategy)
 
     _none_value = 0.0
-    _applevel_repr = "float"
 
     def wrap(self, floatval):
         return self.space.wrap(floatval)
     import_from_mixin(AbstractUnwrappedStrategy)
 
     _none_value = None
-    _applevel_repr = "bytes"
 
     def wrap(self, stringval):
         return self.space.wrap(stringval)
     import_from_mixin(AbstractUnwrappedStrategy)
 
     _none_value = None
-    _applevel_repr = "unicode"
 
     def wrap(self, stringval):
         return self.space.wrap(stringval)

File rpython/rtyper/test/test_rclass.py

         res = self.interpret(f, [3])
         assert res == ~0x0200 & 0x3ff
 
+    def test_class___name__(self):
+        class ACLS(object): pass
+        class Bcls(ACLS): pass
+        class CCls(ACLS): pass
+        def nameof(cls):
+            return cls.__name__
+        nameof._annspecialcase_ = "specialize:memo"
+        def f(i):
+            if i == 1: x = ACLS()
+            elif i == 2: x = Bcls()
+            else: x = CCls()
+            return nameof(x.__class__)
+        res = self.interpret(f, [1])
+        assert ''.join(res.chars) == 'ACLS'
+        res = self.interpret(f, [2])
+        assert ''.join(res.chars) == 'Bcls'
+        res = self.interpret(f, [3])
+        assert ''.join(res.chars) == 'CCls'
+
     def test_hash_preservation(self):
         from rpython.rlib.objectmodel import current_object_addr_as_int
         from rpython.rlib.objectmodel import compute_identity_hash