[pypy-commit] pypy pytest: merge from default

RonnyPfannschmidt noreply at buildbot.pypy.org
Fri Jan 18 15:45:49 CET 2013


Author: Ronny Pfannschmidt <Ronny.Pfannschmidt at gmx.de>
Branch: pytest
Changeset: r60174:1d88326d9eda
Date: 2013-01-18 10:10 +0100
http://bitbucket.org/pypy/pypy/changeset/1d88326d9eda/

Log:	merge from default

diff --git a/pypy/jit/metainterp/compile.py b/pypy/jit/metainterp/compile.py
--- a/pypy/jit/metainterp/compile.py
+++ b/pypy/jit/metainterp/compile.py
@@ -5,7 +5,7 @@
 from pypy.rlib.objectmodel import we_are_translated
 from pypy.rlib.debug import debug_start, debug_stop, debug_print
 from pypy.rlib import rstack
-from pypy.rlib.jit import JitDebugInfo, Counters
+from pypy.rlib.jit import JitDebugInfo, Counters, dont_look_inside
 from pypy.conftest import option
 from pypy.tool.sourcetools import func_with_new_name
 
@@ -685,6 +685,7 @@
         assert 0, "unreachable"
 
     @staticmethod
+    @dont_look_inside
     def force_now(cpu, token):
         # Called during a residual call from the assembler, if the code
         # actually needs to force one of the virtualrefs or the virtualizable.
diff --git a/pypy/jit/metainterp/virtualref.py b/pypy/jit/metainterp/virtualref.py
--- a/pypy/jit/metainterp/virtualref.py
+++ b/pypy/jit/metainterp/virtualref.py
@@ -169,4 +169,3 @@
             # token == TOKEN_NONE and the vref was not forced: it's invalid
             raise InvalidVirtualRef
         return vref.forced
-    force_virtual._dont_inline_ = True
diff --git a/pypy/module/micronumpy/arrayimpl/concrete.py b/pypy/module/micronumpy/arrayimpl/concrete.py
--- a/pypy/module/micronumpy/arrayimpl/concrete.py
+++ b/pypy/module/micronumpy/arrayimpl/concrete.py
@@ -9,7 +9,7 @@
 from pypy.interpreter.error import OperationError, operationerrfmt
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.rlib import jit
-from pypy.rlib.rawstorage import free_raw_storage
+from pypy.rlib.rawstorage import free_raw_storage, RAW_STORAGE
 from pypy.rlib.debug import make_sure_not_resized
 
 class ConcreteArrayIterator(base.BaseArrayIterator):
@@ -427,18 +427,18 @@
     def get_storage_as_int(self, space):
         return rffi.cast(lltype.Signed, self.storage)
 
-class ConcreteArray(BaseConcreteArray):
-    def __init__(self, shape, dtype, order, strides, backstrides):
+class ConcreteArrayNotOwning(BaseConcreteArray):
+    def __init__(self, shape, dtype, order, strides, backstrides, storage):
         make_sure_not_resized(shape)
         make_sure_not_resized(strides)
         make_sure_not_resized(backstrides)
         self.shape = shape
         self.size = support.product(shape) * dtype.get_size()
-        self.storage = dtype.itemtype.malloc(self.size)
         self.order = order
         self.dtype = dtype
         self.strides = strides
         self.backstrides = backstrides
+        self.storage = storage
 
     def create_iter(self, shape=None):
         if shape is None or shape == self.get_shape():
@@ -451,14 +451,24 @@
     def fill(self, box):
         self.dtype.fill(self.storage, box, 0, self.size)
 
-    def __del__(self):
-        free_raw_storage(self.storage, track_allocation=False)
-
     def set_shape(self, space, new_shape):
         strides, backstrides = support.calc_strides(new_shape, self.dtype,
                                                     self.order)
         return SliceArray(0, strides, backstrides, new_shape, self)
 
+class ConcreteArray(ConcreteArrayNotOwning):
+    def __init__(self, shape, dtype, order, strides, backstrides):
+        # we allocate the actual storage later because we need to compute
+        # self.size first
+        null_storage = lltype.nullptr(RAW_STORAGE)
+        ConcreteArrayNotOwning.__init__(self, shape, dtype, order, strides, backstrides,
+                                        null_storage)
+        self.storage = dtype.itemtype.malloc(self.size)
+
+    def __del__(self):
+        free_raw_storage(self.storage, track_allocation=False)
+
+
 class NonWritableArray(ConcreteArray):
     def descr_setitem(self, space, w_index, w_value):
         raise OperationError(space.w_RuntimeError, space.wrap(
diff --git a/pypy/module/micronumpy/base.py b/pypy/module/micronumpy/base.py
--- a/pypy/module/micronumpy/base.py
+++ b/pypy/module/micronumpy/base.py
@@ -28,6 +28,16 @@
         return W_NDimArray(impl)
 
     @staticmethod
+    def from_shape_and_storage(shape, storage, dtype, order='C'):
+        from pypy.module.micronumpy.arrayimpl import concrete
+        assert shape
+        strides, backstrides = calc_strides(shape, dtype, order)
+        impl = concrete.ConcreteArrayNotOwning(shape, dtype, order, strides,
+                                               backstrides, storage)
+        return W_NDimArray(impl)
+
+
+    @staticmethod
     def new_slice(offset, strides, backstrides, shape, parent, dtype=None):
         from pypy.module.micronumpy.arrayimpl import concrete
 
diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -397,7 +397,6 @@
                                                        space.w_False]))
         return w_d
 
-
     # --------------------- operations ----------------------------
 
     def _unaryop_impl(ufunc_name):
@@ -565,6 +564,20 @@
         return W_NDimArray.new_scalar(space, dtype)
     return W_NDimArray.from_shape(shape, dtype)
 
+ at unwrap_spec(addr=int)
+def descr__from_shape_and_storage(space, w_cls, w_shape, addr, w_dtype):
+    """
+    Create an array from an existing buffer, given its address as int.
+    PyPy-only implementation detail.
+    """
+    from pypy.rpython.lltypesystem import rffi
+    from pypy.rlib.rawstorage import RAW_STORAGE_PTR
+    shape = _find_shape(space, w_shape)
+    storage = rffi.cast(RAW_STORAGE_PTR, addr)
+    dtype = space.interp_w(interp_dtype.W_Dtype,
+                           space.call_function(space.gettypefor(interp_dtype.W_Dtype), w_dtype))
+    return W_NDimArray.from_shape_and_storage(shape, storage, dtype)
+
 W_NDimArray.typedef = TypeDef(
     "ndarray",
     __new__ = interp2app(descr_new_array),
@@ -661,6 +674,8 @@
     imag = GetSetProperty(W_NDimArray.descr_get_imag,
                           W_NDimArray.descr_set_imag),
     __array_interface__ = GetSetProperty(W_NDimArray.descr_array_iface),
+   _from_shape_and_storage = interp2app(descr__from_shape_and_storage,
+                                        as_classmethod=True)
 )
 
 @unwrap_spec(ndmin=int, copy=bool, subok=bool)
diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -189,7 +189,23 @@
         assert shape == [2]
         assert space.str_w(elems[0]) == "a"
         assert space.str_w(elems[1]) == "b"
-        
+
+    def test_from_shape_and_storage(self):
+        from pypy.rlib.rawstorage import alloc_raw_storage, raw_storage_setitem
+        from pypy.rpython.lltypesystem import rffi
+        from pypy.module.micronumpy.interp_dtype import get_dtype_cache
+        storage = alloc_raw_storage(4, track_allocation=False, zero=True)
+        for i in range(4):
+            raw_storage_setitem(storage, i, rffi.cast(rffi.UCHAR, i))
+        #
+        dtypes = get_dtype_cache(self.space)
+        w_array = W_NDimArray.from_shape_and_storage([2, 2], storage, dtypes.w_int8dtype)
+        def get(i, j):
+            return w_array.getitem(self.space, [i, j]).value
+        assert get(0, 0) == 0
+        assert get(0, 1) == 1
+        assert get(1, 0) == 2
+        assert get(1, 1) == 3
 
 class AppTestNumArray(BaseNumpyAppTest):
     def w_CustomIndexObject(self, index):
@@ -2340,7 +2356,7 @@
         s = repr(a)
         assert s.replace('\n', '') == \
                       "array(['abc', 'defg', 'ab'],       dtype='|S4')"
-         
+        
        
 class AppTestPyPy(BaseNumpyAppTest):
     def setup_class(cls):
@@ -2360,3 +2376,12 @@
         assert a[0][1] == 2
         a = _numpypy.array(([[[1, 2], [3, 4], [5, 6]]]))
         assert (a[0, 1] == [3, 4]).all()
+
+    def test_from_shape_and_storage(self):
+        from _numpypy import array, ndarray
+        x = array([1, 2, 3, 4])
+        addr, _ = x.__array_interface__['data']
+        y = ndarray._from_shape_and_storage([2, 2], addr, x.dtype)
+        assert y[0, 1] == 2
+        y[0, 1] = 42
+        assert x[1] == 42
diff --git a/pypy/rlib/runicode.py b/pypy/rlib/runicode.py
--- a/pypy/rlib/runicode.py
+++ b/pypy/rlib/runicode.py
@@ -460,6 +460,10 @@
                                  errorhandler=None,
                                  byteorder='little'):
     if size == 0:
+        if byteorder == 'native':
+            result = StringBuilder(2)
+            _STORECHAR(result, 0xFEFF, BYTEORDER)
+            return result.build()
         return ""
 
     result = StringBuilder(size * 2 + 2)
@@ -621,6 +625,10 @@
                                  errorhandler=None,
                                  byteorder='little'):
     if size == 0:
+        if byteorder == 'native':
+            result = StringBuilder(4)
+            _STORECHAR32(result, 0xFEFF, BYTEORDER)
+            return result.build()
         return ""
 
     result = StringBuilder(size * 4 + 4)
diff --git a/pypy/rlib/test/test_runicode.py b/pypy/rlib/test/test_runicode.py
--- a/pypy/rlib/test/test_runicode.py
+++ b/pypy/rlib/test/test_runicode.py
@@ -679,6 +679,11 @@
                          "utf-32 utf-32-be utf-32-le").split():
             self.checkencode(uni, encoding)
 
+    def test_empty(self):
+        for encoding in ("utf-8 utf-16 utf-16-be utf-16-le "
+                         "utf-32 utf-32-be utf-32-le").split():
+            self.checkencode(u'', encoding)
+
     def test_single_chars_utf8(self):
         # check every number of bytes per char
         for s in ["\xd7\x90", "\xd6\x96", "\xeb\x96\x95", "\xf0\x90\x91\x93"]:


More information about the pypy-commit mailing list