Commits

Maciej Fijalkowski  committed 2ff5e3c

Make a newlist_hint an official interface both from ObjSpace and from
__pypy__

  • Participants
  • Parent commits 30d9243
  • Branches length-hint

Comments (0)

Files changed (5)

File pypy/interpreter/baseobjspace.py

     def newlist_str(self, list_s):
         return self.newlist([self.wrap(s) for s in list_s])
 
+    def newlist_hint(self, sizehint):
+        from pypy.objspace.std.listobject import make_empty_list_with_size
+        return make_empty_list_with_size(self, sizehint)
+
     @jit.unroll_safe
     def exception_match(self, w_exc_type, w_check_class):
         """Checks if the given exception type matches 'w_check_class'."""

File pypy/module/__pypy__/__init__.py

         'list_strategy'             : 'interp_magic.list_strategy',
         'validate_fd'               : 'interp_magic.validate_fd',
         'resizelist_hint'           : 'interp_magic.resizelist_hint',
+        'newlist_hint'              : 'interp_magic.newlist_hint',
         'newdict'                   : 'interp_dict.newdict',
         'dictstrategy'              : 'interp_dict.dictstrategy',
     }

File pypy/module/__pypy__/interp_magic.py

 from pypy.interpreter.baseobjspace import ObjSpace, W_Root
 from pypy.interpreter.error import OperationError, wrap_oserror
 from pypy.interpreter.gateway import unwrap_spec
-from pypy.rlib.objectmodel import resizelist_hint, we_are_translated
+from pypy.rlib.objectmodel import we_are_translated
 from pypy.objspace.std.listobject import W_ListObject
 from pypy.objspace.std.typeobject import MethodCache
 from pypy.objspace.std.mapdict import IndexCache
         raise OperationError(space.w_TypeError,
                              space.wrap("arg 1 must be a 'list'"))
     w_iterable._resize_hint(sizehint)
+
+@unwrap_spec(sizehint=int)
+def newlist_hint(space, sizehint):
+    return space.newlist_hint(sizehint)

File pypy/objspace/std/listobject.py

     storage = strategy.erase(None)
     return W_ListObject.from_storage_and_strategy(space, storage, strategy)
 
+def make_empty_list_with_size(space, hint):
+    strategy = SizeListStrategy(space, hint)
+    storage = strategy.erase(None)
+    return W_ListObject.from_storage_and_strategy(space, storage, strategy)
+
 @jit.look_inside_iff(lambda space, list_w, sizehint:
                          jit.isconstant(len(list_w)) and len(list_w) < UNROLL_CUTOFF)
 def get_strategy_from_list_objects(space, list_w, sizehint):

File pypy/objspace/std/test/test_listobject.py

         space.call_method(w_l, 'append', space.w_None)
         assert isinstance(w_l.strategy, ObjectListStrategy)
 
+    def test_newlist_hint(self):
+        space = self.space
+        w_lst = space.newlist_hint(13)
+        assert isinstance(w_lst.strategy, SizeListStrategy)
+        assert w_lst.strategy.sizehint == 13
 
 class AppTestW_ListObject(object):
     def setup_class(cls):