Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r67549:33365ef75d96
Date: 2013-10-24 09:19 +0200
http://bitbucket.org/pypy/pypy/changeset/33365ef75d96/

Log:    Move code around

diff --git a/pypy/module/_cffi_backend/cdataobj.py 
b/pypy/module/_cffi_backend/cdataobj.py
--- a/pypy/module/_cffi_backend/cdataobj.py
+++ b/pypy/module/_cffi_backend/cdataobj.py
@@ -283,10 +283,18 @@
         return self.ctype.iter(self)
 
     def unpackiterable_int(self, space):
-        return self.ctype.aslist_int(self)
+        from pypy.module._cffi_backend import ctypearray
+        ctype = self.ctype
+        if isinstance(ctype, ctypearray.W_CTypeArray):
+            return ctype.ctitem.unpack_list_of_int_items(self)
+        return None
 
     def unpackiterable_float(self, space):
-        return self.ctype.aslist_float(self)
+        from pypy.module._cffi_backend import ctypearray
+        ctype = self.ctype
+        if isinstance(ctype, ctypearray.W_CTypeArray):
+            return ctype.ctitem.unpack_list_of_float_items(self)
+        return None
 
     @specialize.argtype(1)
     def write_raw_signed_data(self, source):
diff --git a/pypy/module/_cffi_backend/ctypearray.py 
b/pypy/module/_cffi_backend/ctypearray.py
--- a/pypy/module/_cffi_backend/ctypearray.py
+++ b/pypy/module/_cffi_backend/ctypearray.py
@@ -105,26 +105,6 @@
     def iter(self, cdata):
         return W_CDataIter(self.space, self.ctitem, cdata)
 
-    def aslist_int(self, cdata):
-        from rpython.rlib.rarray import populate_list_from_raw_array
-        if self.ctitem.is_long():
-            res = []
-            buf = rffi.cast(rffi.LONGP, cdata._cdata)
-            length = cdata.get_array_length()
-            populate_list_from_raw_array(res, buf, length)
-            return res
-        return None
-
-    def aslist_float(self, cdata):
-        from rpython.rlib.rarray import populate_list_from_raw_array
-        if self.ctitem.is_double():
-            res = []
-            buf = rffi.cast(rffi.DOUBLEP, cdata._cdata)
-            length = cdata.get_array_length()
-            populate_list_from_raw_array(res, buf, length)
-            return res
-        return None
-
     def get_vararg_type(self):
         return self.ctptr
 
diff --git a/pypy/module/_cffi_backend/ctypeobj.py 
b/pypy/module/_cffi_backend/ctypeobj.py
--- a/pypy/module/_cffi_backend/ctypeobj.py
+++ b/pypy/module/_cffi_backend/ctypeobj.py
@@ -43,18 +43,15 @@
     def is_unichar_ptr_or_array(self):
         return False
 
-    def is_long(self):
-        return False
-
-    def is_double(self):
-        return False
-
-    def aslist_int(self, w_ob):
+    def unpack_list_of_int_items(self, cdata):
         return None
 
-    def aslist_float(self, w_ob):
+    def unpack_list_of_float_items(self, cdata):
         return None
 
+    def pack_list_of_items(self, cdata, w_ob):
+        return False
+
     def newp(self, w_init):
         space = self.space
         raise operationerrfmt(space.w_TypeError,
diff --git a/pypy/module/_cffi_backend/ctypeprim.py 
b/pypy/module/_cffi_backend/ctypeprim.py
--- a/pypy/module/_cffi_backend/ctypeprim.py
+++ b/pypy/module/_cffi_backend/ctypeprim.py
@@ -170,9 +170,6 @@
             self.vmin = r_uint(-1) << (sh - 1)
             self.vrangemax = (r_uint(1) << sh) - 1
 
-    def is_long(self):
-        return self.size == rffi.sizeof(lltype.Signed)
-
     def cast_to_int(self, cdata):
         return self.convert_to_object(cdata)
 
@@ -204,6 +201,26 @@
     def write_raw_integer_data(self, w_cdata, value):
         w_cdata.write_raw_signed_data(value)
 
+    def unpack_list_of_int_items(self, w_cdata):
+        if self.size == rffi.sizeof(rffi.LONG): # XXX
+            from rpython.rlib.rarray import populate_list_from_raw_array
+            res = []
+            buf = rffi.cast(rffi.LONGP, w_cdata._cdata)
+            length = w_cdata.get_array_length()
+            populate_list_from_raw_array(res, buf, length)
+            return res
+        return None
+
+    def pack_list_of_items(self, cdata, w_ob):
+        if self.size == rffi.sizeof(rffi.LONG): # XXX
+            int_list = self.space.listview_int(w_ob)
+            if int_list is not None:
+                from rpython.rlib.rarray import copy_list_to_raw_array
+                cdata = rffi.cast(rffi.LONGP, cdata)
+                copy_list_to_raw_array(int_list, cdata)
+                return True
+        return False
+
 
 class W_CTypePrimitiveUnsigned(W_CTypePrimitive):
     _attrs_            = ['value_fits_long', 'value_fits_ulong', 'vrangemax']
@@ -276,9 +293,6 @@
 class W_CTypePrimitiveFloat(W_CTypePrimitive):
     _attrs_ = []
 
-    def is_double(self):
-        return self.size == rffi.sizeof(lltype.Float)
-
     def cast(self, w_ob):
         space = self.space
         if isinstance(w_ob, cdataobj.W_CData):
@@ -318,6 +332,26 @@
         value = space.float_w(space.float(w_ob))
         misc.write_raw_float_data(cdata, value, self.size)
 
+    def unpack_list_of_float_items(self, w_cdata):
+        if self.size == rffi.sizeof(rffi.DOUBLE): # XXX
+            from rpython.rlib.rarray import populate_list_from_raw_array
+            res = []
+            buf = rffi.cast(rffi.DOUBLEP, w_cdata._cdata)
+            length = w_cdata.get_array_length()
+            populate_list_from_raw_array(res, buf, length)
+            return res
+        return None
+
+    def pack_list_of_items(self, cdata, w_ob):
+        if self.size == rffi.sizeof(rffi.DOUBLE): # XXX
+            float_list = self.space.listview_float(w_ob)
+            if float_list is not None:
+                from rpython.rlib.rarray import copy_list_to_raw_array
+                cdata = rffi.cast(rffi.DOUBLEP, cdata)
+                copy_list_to_raw_array(float_list, cdata)
+                return True
+        return False
+
 
 class W_CTypePrimitiveLongDouble(W_CTypePrimitiveFloat):
     _attrs_ = []
diff --git a/pypy/module/_cffi_backend/ctypeptr.py 
b/pypy/module/_cffi_backend/ctypeptr.py
--- a/pypy/module/_cffi_backend/ctypeptr.py
+++ b/pypy/module/_cffi_backend/ctypeptr.py
@@ -42,12 +42,6 @@
     def is_char_or_unichar_ptr_or_array(self):
         return isinstance(self.ctitem, ctypeprim.W_CTypePrimitiveCharOrUniChar)
 
-    def aslist_int(self, cdata):
-        return None
-
-    def aslist_float(self, cdata):
-        return None
-
     def cast(self, w_ob):
         # cast to a pointer, to a funcptr, or to an array.
         # Note that casting to an array is an extension to the C language,
@@ -64,24 +58,10 @@
             value = rffi.cast(rffi.CCHARP, value)
         return cdataobj.W_CData(space, value, self)
 
-    def _convert_array_from_list_strategy_maybe(self, cdata, w_ob):
-        from rpython.rlib.rarray import copy_list_to_raw_array
-        int_list = self.space.listview_int(w_ob)
-        float_list = self.space.listview_float(w_ob)
+    def _convert_array_from_listview(self, cdata, w_ob):
+        if self.ctitem.pack_list_of_items(cdata, w_ob):   # fast path
+            return
         #
-        if self.ctitem.is_long() and int_list is not None:
-            cdata = rffi.cast(rffi.LONGP, cdata)
-            copy_list_to_raw_array(int_list, cdata)
-            return True
-        #
-        if self.ctitem.is_double() and float_list is not None:
-            cdata = rffi.cast(rffi.DOUBLEP, cdata)
-            copy_list_to_raw_array(float_list, cdata)
-            return True
-        #
-        return False
-
-    def _convert_array_from_listview(self, cdata, w_ob):
         space = self.space
         lst_w = space.listview(w_ob)
         if self.length >= 0 and len(lst_w) > self.length:
@@ -97,11 +77,7 @@
         space = self.space
         if (space.isinstance_w(w_ob, space.w_list) or
             space.isinstance_w(w_ob, space.w_tuple)):
-            #
-            if not self._convert_array_from_list_strategy_maybe(cdata, w_ob):
-                # continue with the slow path
-                self._convert_array_from_listview(cdata, w_ob)
-            #
+            self._convert_array_from_listview(cdata, w_ob)
         elif (self.can_cast_anything or
               (self.ctitem.is_primitive_integer and
                self.ctitem.size == rffi.sizeof(lltype.Char))):
diff --git a/pypy/module/_cffi_backend/test/test_fastpath.py 
b/pypy/module/_cffi_backend/test/test_fastpath.py
--- a/pypy/module/_cffi_backend/test/test_fastpath.py
+++ b/pypy/module/_cffi_backend/test/test_fastpath.py
@@ -1,18 +1,18 @@
-# side-effect: FORMAT_LONGDOUBLE must be built before test_checkmodule()
+# side-effect: FORMAT_LONGDOUBLE must be built before the first test
 from pypy.module._cffi_backend import misc
-from pypy.module._cffi_backend.ctypeptr import W_CTypePtrOrArray
+
 
 class AppTest_fast_path_from_list(object):
     spaceconfig = dict(usemodules=('_cffi_backend', 'cStringIO'))
 
     def setup_method(self, meth):
-        def forbidden(self, *args):
+        def forbidden(*args):
             assert False, 'The slow path is forbidden'
-        self._original = W_CTypePtrOrArray._convert_array_from_listview.im_func
-        W_CTypePtrOrArray._convert_array_from_listview = forbidden
+        self._original = self.space.listview
+        self.space.listview = forbidden
 
     def teardown_method(self, meth):
-        W_CTypePtrOrArray._convert_array_from_listview = self._original
+        self.space.listview = self._original
 
     def test_fast_init_from_list(self):
         import _cffi_backend
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to