[pypy-commit] pypy fast_cffi_list_init: add space.unpackiterable_{int, float}: they are similar to listview_*, with the difference that you are free to modify the returned list.
antocuni
noreply at buildbot.pypy.org
Thu Oct 10 16:26:40 CEST 2013
Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: fast_cffi_list_init
Changeset: r67292:4af5ccf25009
Date: 2013-10-10 15:16 +0200
http://bitbucket.org/pypy/pypy/changeset/4af5ccf25009/
Log: add space.unpackiterable_{int,float}: they are similar to
listview_*, with the difference that you are free to modify the
returned list.
diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -239,6 +239,18 @@
# _____ this code is here to support testing only _____
return self
+ def unpackiterable_int(self, space):
+ lst = space.listview_int(self)
+ if lst:
+ return lst[:]
+ return None
+
+ def unpackiterable_float(self, space):
+ lst = space.listview_float(self)
+ if lst:
+ return lst[:]
+ return None
+
class W_InterpIterable(W_Root):
def __init__(self, space, w_iterable):
@@ -838,6 +850,22 @@
return self._unpackiterable_known_length_jitlook(w_iterator,
expected_length)
+
+ def unpackiterable_int(self, w_obj):
+ """
+ Return a RPython list of unwrapped ints out of w_obj. The list is
+ guaranteed to be acopy of the actual data contained in w_obj, so you
+ can freely modify it. It might return None if not supported.
+ """
+ return w_obj.unpackiterable_int(self)
+
+ def unpackiterable_float(self, w_obj):
+ """
+ Same as unpackiterable_int, but for floats.
+ """
+ return w_obj.unpackiterable_float(self)
+
+
def length_hint(self, w_obj, default):
"""Return the length of an object, consulting its __length_hint__
method if necessary.
diff --git a/pypy/objspace/std/test/test_liststrategies.py b/pypy/objspace/std/test/test_liststrategies.py
--- a/pypy/objspace/std/test/test_liststrategies.py
+++ b/pypy/objspace/std/test/test_liststrategies.py
@@ -650,6 +650,15 @@
w_l = W_ListObject(space, [space.wrap(1.1), space.wrap(2.2), space.wrap(3.3)])
assert self.space.listview_float(w_l) == [1.1, 2.2, 3.3]
+ def test_unpackiterable_int_list(self):
+ space = self.space
+ w_l = W_ListObject(space, [space.wrap(1), space.wrap(2), space.wrap(3)])
+ list_orig = self.space.listview_int(w_l)
+ list_copy = self.space.unpackiterable_int(w_l)
+ assert list_orig == list_copy == [1, 2, 3]
+ list_copy[0] = 42
+ assert list_orig == [1, 2, 3]
+
class TestW_ListStrategiesDisabled:
spaceconfig = {"objspace.std.withliststrategies": False}
More information about the pypy-commit
mailing list