def frombuffer(space, w_buffer, w_dtype=None, count=-1, offset=0): dtype = space.interp_w( descriptor.W_Dtype, space.call_function(space.gettypefor(descriptor.W_Dtype), w_dtype)) if dtype.elsize == 0: raise oefmt(space.w_ValueError, "itemsize cannot be zero in type") try: buf = _getbuffer(space, w_buffer) except OperationError as e: if not e.match(space, space.w_TypeError): raise w_buffer = space.call_method(w_buffer, '__buffer__', space.newint(space.BUF_FULL_RO)) buf = _getbuffer(space, w_buffer) ts = buf.getlength() if offset < 0 or offset > ts: raise oefmt( space.w_ValueError, "offset must be non-negative and no greater than " "buffer length (%d)", ts) s = ts - offset if offset: buf = SubBuffer(buf, offset, s) n = count itemsize = dtype.elsize assert itemsize > 0 if n < 0: if s % itemsize != 0: raise oefmt(space.w_ValueError, "buffer size must be a multiple of element size") n = s / itemsize else: if s < n * itemsize: raise oefmt(space.w_ValueError, "buffer is smaller than requested size") try: storage = buf.get_raw_address() except ValueError: a = W_NDimArray.from_shape(space, [n], dtype=dtype) loop.fromstring_loop(space, a, dtype, itemsize, buf.as_str()) return a else: writable = not buf.readonly return W_NDimArray.from_shape_and_storage(space, [n], storage, storage_bytes=s, dtype=dtype, w_base=w_buffer, writable=writable)
def frombuffer(space, w_buffer, w_dtype=None, count=-1, offset=0): dtype = space.interp_w(descriptor.W_Dtype, space.call_function(space.gettypefor(descriptor.W_Dtype), w_dtype)) if dtype.elsize == 0: raise oefmt(space.w_ValueError, "itemsize cannot be zero in type") try: buf = _getbuffer(space, w_buffer) except OperationError as e: if not e.match(space, space.w_TypeError): raise w_buffer = space.call_method(w_buffer, '__buffer__', space.newint(space.BUF_FULL_RO)) buf = _getbuffer(space, w_buffer) ts = buf.getlength() if offset < 0 or offset > ts: raise oefmt(space.w_ValueError, "offset must be non-negative and no greater than " "buffer length (%d)", ts) s = ts - offset if offset: buf = SubBuffer(buf, offset, s) n = count itemsize = dtype.elsize assert itemsize > 0 if n < 0: if s % itemsize != 0: raise oefmt(space.w_ValueError, "buffer size must be a multiple of element size") n = s / itemsize else: if s < n * itemsize: raise oefmt(space.w_ValueError, "buffer is smaller than requested size") try: storage = buf.get_raw_address() except ValueError: a = W_NDimArray.from_shape(space, [n], dtype=dtype) loop.fromstring_loop(space, a, dtype, itemsize, buf.as_str()) return a else: writable = not buf.readonly return W_NDimArray.from_shape_and_storage(space, [n], storage, storage_bytes=s, dtype=dtype, w_base=w_buffer, writable=writable)
def _array(space, w_object, w_dtype=None, copy=True, w_order=None, subok=False): from pypy.module.micronumpy import strides # for anything that isn't already an array, try __array__ method first if not isinstance(w_object, W_NDimArray): w_array = try_array_method(space, w_object, w_dtype) if w_array is not None: # continue with w_array, but do further operations in place w_object = w_array copy = False if not isinstance(w_object, W_NDimArray): w_array = try_interface_method(space, w_object) if w_array is not None: w_object = w_array copy = False dtype = descriptor.decode_w_dtype(space, w_dtype) if space.is_none(w_order): order = 'C' else: order = space.str_w(w_order) if order == 'K': order = 'C' if order != 'C': # or order != 'F': raise oefmt(space.w_ValueError, "Unknown order: %s", order) if isinstance(w_object, W_NDimArray): if (dtype is None or w_object.get_dtype() is dtype): if copy and (subok or type(w_object) is W_NDimArray): return w_object.descr_copy(space, w_order) elif not copy and (subok or type(w_object) is W_NDimArray): return w_object if subok and not type(w_object) is W_NDimArray: raise oefmt(space.w_NotImplementedError, "array(..., subok=True) only partially implemented") # we have a ndarray, but need to copy or change dtype if dtype is None: dtype = w_object.get_dtype() if dtype != w_object.get_dtype(): # silently reject the copy value copy = True if copy: shape = w_object.get_shape() elems_w = [None] * w_object.get_size() elsize = w_object.get_dtype().elsize # TODO - use w_object.implementation without copying to a list # unfortunately that causes a union error in translation for i in range(w_object.get_size()): elems_w[i] = w_object.implementation.getitem(i * elsize) else: imp = w_object.implementation with imp as storage: sz = support.product(w_object.get_shape()) * dtype.elsize return W_NDimArray.from_shape_and_storage(space, w_object.get_shape(), storage, dtype, storage_bytes=sz, w_base=w_object, start=imp.start) else: # not an array shape, elems_w = strides.find_shape_and_elems(space, w_object, dtype) if dtype is None or (dtype.is_str_or_unicode() and dtype.elsize < 1): dtype = strides.find_dtype_for_seq(space, elems_w, dtype) if dtype is None: dtype = descriptor.get_dtype_cache(space).w_float64dtype elif dtype.is_str_or_unicode() and dtype.elsize < 1: # promote S0 -> S1, U0 -> U1 dtype = descriptor.variable_dtype(space, dtype.char + '1') w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order) if support.product(shape) == 1: w_arr.set_scalar_value(dtype.coerce(space, elems_w[0])) else: loop.assign(space, w_arr, elems_w) return w_arr
def _array(space, w_object, w_dtype=None, copy=True, w_order=None, subok=False): from pypy.module.micronumpy import strides # for anything that isn't already an array, try __array__ method first if not isinstance(w_object, W_NDimArray): w_array = try_array_method(space, w_object, w_dtype) if w_array is not None: # continue with w_array, but do further operations in place w_object = w_array copy = False if not isinstance(w_object, W_NDimArray): w_array = try_interface_method(space, w_object) if w_array is not None: w_object = w_array copy = False dtype = descriptor.decode_w_dtype(space, w_dtype) if space.is_none(w_order): order = 'C' else: order = space.str_w(w_order) if order == 'K': order = 'C' if order != 'C': # or order != 'F': raise oefmt(space.w_ValueError, "Unknown order: %s", order) if isinstance(w_object, W_NDimArray): if (dtype is None or w_object.get_dtype() is dtype): if copy and (subok or type(w_object) is W_NDimArray): return w_object.descr_copy(space, w_order) elif not copy and (subok or type(w_object) is W_NDimArray): return w_object if subok and not type(w_object) is W_NDimArray: raise oefmt(space.w_NotImplementedError, "array(..., subok=True) only partially implemented") # we have a ndarray, but need to copy or change dtype if dtype is None: dtype = w_object.get_dtype() if dtype != w_object.get_dtype(): # silently reject the copy value copy = True if copy: shape = w_object.get_shape() w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order) if support.product(shape) == 1: w_arr.set_scalar_value(dtype.coerce(space, w_object.implementation.getitem(0))) else: loop.setslice(space, shape, w_arr.implementation, w_object.implementation) return w_arr else: imp = w_object.implementation w_base = w_object if imp.base() is not None: w_base = imp.base() with imp as storage: sz = support.product(w_object.get_shape()) * dtype.elsize return W_NDimArray.from_shape_and_storage(space, w_object.get_shape(), storage, dtype, storage_bytes=sz, w_base=w_base, start=imp.start) else: # not an array shape, elems_w = strides.find_shape_and_elems(space, w_object, dtype) if dtype is None and space.isinstance_w(w_object, space.w_buffer): dtype = descriptor.get_dtype_cache(space).w_uint8dtype if dtype is None or (dtype.is_str_or_unicode() and dtype.elsize < 1): dtype = find_dtype_for_seq(space, elems_w, dtype) if dtype is None: dtype = descriptor.get_dtype_cache(space).w_float64dtype elif dtype.is_str_or_unicode() and dtype.elsize < 1: # promote S0 -> S1, U0 -> U1 dtype = descriptor.variable_dtype(space, dtype.char + '1') w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order) if support.product(shape) == 1: # safe from overflow since from_shape checks w_arr.set_scalar_value(dtype.coerce(space, elems_w[0])) else: loop.assign(space, w_arr, elems_w) return w_arr
def try_interface_method(space, w_object, copy): try: w_interface = space.getattr(w_object, space.newtext("__array_interface__")) if w_interface is None: return None, False version_w = space.finditem(w_interface, space.newtext("version")) if version_w is None: raise oefmt(space.w_ValueError, "__array_interface__ found without" " 'version' key") if not space.isinstance_w(version_w, space.w_int): raise oefmt( space.w_ValueError, "__array_interface__ found with" " non-int 'version' key") version = space.int_w(version_w) if version < 3: raise oefmt(space.w_ValueError, "__array_interface__ version %d not supported", version) # make a view into the data w_shape = space.finditem(w_interface, space.newtext('shape')) w_dtype = space.finditem(w_interface, space.newtext('typestr')) w_descr = space.finditem(w_interface, space.newtext('descr')) w_data = space.finditem(w_interface, space.newtext('data')) w_strides = space.finditem(w_interface, space.newtext('strides')) if w_shape is None or w_dtype is None: raise oefmt( space.w_ValueError, "__array_interface__ missing one or more required keys: shape, typestr" ) if w_descr is not None: raise oefmt(space.w_NotImplementedError, "__array_interface__ descr not supported yet") if w_strides is None or space.is_w(w_strides, space.w_None): strides = None else: strides = [space.int_w(i) for i in space.listview(w_strides)] shape = [space.int_w(i) for i in space.listview(w_shape)] dtype = descriptor.decode_w_dtype(space, w_dtype) if dtype is None: raise oefmt(space.w_ValueError, "__array_interface__ could not decode dtype %R", w_dtype) if w_data is not None and (space.isinstance_w(w_data, space.w_tuple) or space.isinstance_w(w_data, space.w_list)): data_w = space.listview(w_data) w_data = rffi.cast(RAW_STORAGE_PTR, space.int_w(data_w[0])) read_only = space.is_true(data_w[1]) or copy offset = 0 w_base = w_object if read_only: w_base = None return W_NDimArray.from_shape_and_storage(space, shape, w_data, dtype, w_base=w_base, strides=strides, start=offset), read_only if w_data is None: w_data = w_object w_offset = space.finditem(w_interface, space.newtext('offset')) if w_offset is None: offset = 0 else: offset = space.int_w(w_offset) #print 'create view from shape',shape,'dtype',dtype,'data',data if strides is not None: raise oefmt(space.w_NotImplementedError, "__array_interface__ strides not fully supported yet") arr = frombuffer(space, w_data, dtype, support.product(shape), offset) new_impl = arr.implementation.reshape(arr, shape) return W_NDimArray(new_impl), False except OperationError as e: if e.match(space, space.w_AttributeError): return None, False raise
def _array(space, w_object, w_dtype=None, copy=True, w_order=None, subok=False): from pypy.module.micronumpy.boxes import W_GenericBox # numpy testing calls array(type(array([]))) and expects a ValueError if space.isinstance_w(w_object, space.w_type): raise oefmt(space.w_ValueError, "cannot create ndarray from type instance") # for anything that isn't already an array, try __array__ method first dtype = descriptor.decode_w_dtype(space, w_dtype) if not isinstance(w_object, W_NDimArray): w_array = try_array_method(space, w_object, w_dtype) if w_array is None: if (not space.isinstance_w(w_object, space.w_bytes) and not space.isinstance_w(w_object, space.w_unicode) and not isinstance(w_object, W_GenericBox)): # use buffer interface w_object = _array_from_buffer_3118(space, w_object, dtype) else: # continue with w_array, but do further operations in place w_object = w_array copy = False dtype = w_object.get_dtype() if not isinstance(w_object, W_NDimArray): w_array, _copy = try_interface_method(space, w_object, copy) if w_array is not None: w_object = w_array copy = _copy dtype = w_object.get_dtype() if isinstance(w_object, W_NDimArray): npy_order = order_converter(space, w_order, NPY.ANYORDER) if (dtype is None or w_object.get_dtype() is dtype) and ( subok or type(w_object) is W_NDimArray): flags = w_object.get_flags() must_copy = copy must_copy |= (npy_order == NPY.CORDER and not flags & NPY.ARRAY_C_CONTIGUOUS) must_copy |= (npy_order == NPY.FORTRANORDER and not flags & NPY.ARRAY_F_CONTIGUOUS) if must_copy: return w_object.descr_copy(space, space.newint(npy_order)) else: return w_object if subok and not type(w_object) is W_NDimArray: raise oefmt(space.w_NotImplementedError, "array(..., subok=True) only partially implemented") # we have a ndarray, but need to copy or change dtype if dtype is None: dtype = w_object.get_dtype() if dtype != w_object.get_dtype(): # silently reject the copy value copy = True if copy: shape = w_object.get_shape() order = support.get_order_as_CF(w_object.get_order(), npy_order) w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order) if support.product(shape) == 1: w_arr.set_scalar_value( dtype.coerce(space, w_object.implementation.getitem(0))) else: loop.setslice(space, shape, w_arr.implementation, w_object.implementation) return w_arr else: imp = w_object.implementation w_base = w_object sz = w_base.get_size() * dtype.elsize if imp.base() is not None: w_base = imp.base() if type(w_base) is W_NDimArray: sz = w_base.get_size() * dtype.elsize else: # this must succeed (mmap, buffer, ...) sz = space.int_w(space.call_method(w_base, 'size')) with imp as storage: return W_NDimArray.from_shape_and_storage(space, w_object.get_shape(), storage, dtype, storage_bytes=sz, w_base=w_base, strides=imp.strides, start=imp.start) else: # not an array npy_order = order_converter(space, w_order, NPY.CORDER) shape, elems_w = find_shape_and_elems(space, w_object, dtype) if dtype is None and space.isinstance_w(w_object, space.w_buffer): dtype = descriptor.get_dtype_cache(space).w_uint8dtype if dtype is None or (dtype.is_str_or_unicode() and dtype.elsize < 1): dtype = find_dtype_for_seq(space, elems_w, dtype) w_arr = W_NDimArray.from_shape(space, shape, dtype, order=npy_order) if support.product( shape) == 1: # safe from overflow since from_shape checks w_arr.set_scalar_value(dtype.coerce(space, elems_w[0])) else: loop.assign(space, w_arr, elems_w) return w_arr
def _array_from_buffer_3118(space, w_object, dtype): try: w_buf = space.call_method(space.builtin, "memoryview", w_object) except OperationError as e: if e.match(space, space.w_TypeError): # object does not have buffer interface return w_object raise format = space.getattr(w_buf, space.newtext('format')) if format: descr = _descriptor_from_pep3118_format(space, space.text_w(format)) if not descr: return w_object if dtype and descr: raise oefmt( space.w_NotImplementedError, "creating an array from a memoryview while specifying dtype " "not supported") if descr.elsize != space.int_w( space.getattr(w_buf, space.newbytes('itemsize'))): msg = ("Item size computed from the PEP 3118 buffer format " "string does not match the actual item size.") space.warn(space.newtext(msg), space.w_RuntimeWarning) return w_object dtype = descr elif not dtype: dtype = descriptor.get_dtype_cache(space).w_stringdtype dtype.elsize = space.int_w( space.getattr(w_buf, space.newbytes('itemsize'))) nd = space.int_w(space.getattr(w_buf, space.newbytes('ndim'))) shape = [ space.int_w(d) for d in space.listview(space.getattr(w_buf, space.newbytes('shape'))) ] strides = [] buflen = space.len_w(w_buf) * dtype.elsize if shape: strides = [ space.int_w(d) for d in space.listview( space.getattr(w_buf, space.newbytes('strides'))) ] if not strides: d = buflen strides = [0] * nd for k in range(nd): if shape[k] > 0: d /= shape[k] strides[k] = d else: if nd == 1: shape = [ buflen / dtype.elsize, ] strides = [ dtype.elsize, ] elif nd > 1: msg = ("ndim computed from the PEP 3118 buffer format " "is greater than 1, but shape is NULL.") space.warn(space.newtext(msg), space.w_RuntimeWarning) return w_object try: w_data = rffi.cast( RAW_STORAGE_PTR, space.int_w(space.call_method(w_buf, '_pypy_raw_address'))) except OperationError as e: if e.match(space, space.w_ValueError): return w_object else: raise e writable = not space.bool_w( space.getattr(w_buf, space.newbytes('readonly'))) w_ret = W_NDimArray.from_shape_and_storage(space, shape, w_data, storage_bytes=buflen, dtype=dtype, w_base=w_object, writable=writable, strides=strides) if w_ret: return w_ret return w_object
def try_interface_method(space, w_object, copy): try: w_interface = space.getattr(w_object, space.wrap("__array_interface__")) if w_interface is None: return None, False version_w = space.finditem(w_interface, space.wrap("version")) if version_w is None: raise oefmt(space.w_ValueError, "__array_interface__ found without" " 'version' key") if not space.isinstance_w(version_w, space.w_int): raise oefmt(space.w_ValueError, "__array_interface__ found with" " non-int 'version' key") version = space.int_w(version_w) if version < 3: raise oefmt(space.w_ValueError, "__array_interface__ version %d not supported", version) # make a view into the data w_shape = space.finditem(w_interface, space.wrap('shape')) w_dtype = space.finditem(w_interface, space.wrap('typestr')) w_descr = space.finditem(w_interface, space.wrap('descr')) w_data = space.finditem(w_interface, space.wrap('data')) w_strides = space.finditem(w_interface, space.wrap('strides')) if w_shape is None or w_dtype is None: raise oefmt(space.w_ValueError, "__array_interface__ missing one or more required keys: shape, typestr" ) if w_descr is not None: raise oefmt(space.w_NotImplementedError, "__array_interface__ descr not supported yet") if w_strides is None or space.is_w(w_strides, space.w_None): strides = None else: strides = [space.int_w(i) for i in space.listview(w_strides)] shape = [space.int_w(i) for i in space.listview(w_shape)] dtype = descriptor.decode_w_dtype(space, w_dtype) if dtype is None: raise oefmt(space.w_ValueError, "__array_interface__ could not decode dtype %R", w_dtype ) if w_data is not None and (space.isinstance_w(w_data, space.w_tuple) or space.isinstance_w(w_data, space.w_list)): data_w = space.listview(w_data) w_data = rffi.cast(RAW_STORAGE_PTR, space.int_w(data_w[0])) read_only = space.is_true(data_w[1]) or copy offset = 0 w_base = w_object if read_only: w_base = None return W_NDimArray.from_shape_and_storage(space, shape, w_data, dtype, w_base=w_base, strides=strides, start=offset), read_only if w_data is None: w_data = w_object w_offset = space.finditem(w_interface, space.wrap('offset')) if w_offset is None: offset = 0 else: offset = space.int_w(w_offset) #print 'create view from shape',shape,'dtype',dtype,'data',data if strides is not None: raise oefmt(space.w_NotImplementedError, "__array_interface__ strides not fully supported yet") arr = frombuffer(space, w_data, dtype, support.product(shape), offset) new_impl = arr.implementation.reshape(arr, shape) return W_NDimArray(new_impl), False except OperationError as e: if e.match(space, space.w_AttributeError): return None, False raise
def _array(space, w_object, w_dtype=None, copy=True, w_order=None, subok=False): from pypy.module.micronumpy.boxes import W_GenericBox # numpy testing calls array(type(array([]))) and expects a ValueError if space.isinstance_w(w_object, space.w_type): raise oefmt(space.w_ValueError, "cannot create ndarray from type instance") # for anything that isn't already an array, try __array__ method first dtype = descriptor.decode_w_dtype(space, w_dtype) if not isinstance(w_object, W_NDimArray): w_array = try_array_method(space, w_object, w_dtype) if w_array is None: if ( not space.isinstance_w(w_object, space.w_str) and not space.isinstance_w(w_object, space.w_unicode) and not isinstance(w_object, W_GenericBox)): # use buffer interface w_object = _array_from_buffer_3118(space, w_object, dtype) else: # continue with w_array, but do further operations in place w_object = w_array copy = False dtype = w_object.get_dtype() if not isinstance(w_object, W_NDimArray): w_array, _copy = try_interface_method(space, w_object, copy) if w_array is not None: w_object = w_array copy = _copy dtype = w_object.get_dtype() if isinstance(w_object, W_NDimArray): npy_order = order_converter(space, w_order, NPY.ANYORDER) if (dtype is None or w_object.get_dtype() is dtype) and (subok or type(w_object) is W_NDimArray): flags = w_object.get_flags() must_copy = copy must_copy |= (npy_order == NPY.CORDER and not flags & NPY.ARRAY_C_CONTIGUOUS) must_copy |= (npy_order == NPY.FORTRANORDER and not flags & NPY.ARRAY_F_CONTIGUOUS) if must_copy: return w_object.descr_copy(space, space.wrap(npy_order)) else: return w_object if subok and not type(w_object) is W_NDimArray: raise oefmt(space.w_NotImplementedError, "array(..., subok=True) only partially implemented") # we have a ndarray, but need to copy or change dtype if dtype is None: dtype = w_object.get_dtype() if dtype != w_object.get_dtype(): # silently reject the copy value copy = True if copy: shape = w_object.get_shape() order = support.get_order_as_CF(w_object.get_order(), npy_order) w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order) if support.product(shape) == 1: w_arr.set_scalar_value(dtype.coerce(space, w_object.implementation.getitem(0))) else: loop.setslice(space, shape, w_arr.implementation, w_object.implementation) return w_arr else: imp = w_object.implementation w_base = w_object sz = w_base.get_size() * dtype.elsize if imp.base() is not None: w_base = imp.base() if type(w_base) is W_NDimArray: sz = w_base.get_size() * dtype.elsize else: # this must succeed (mmap, buffer, ...) sz = space.int_w(space.call_method(w_base, 'size')) with imp as storage: return W_NDimArray.from_shape_and_storage(space, w_object.get_shape(), storage, dtype, storage_bytes=sz, w_base=w_base, strides=imp.strides, start=imp.start) else: # not an array npy_order = order_converter(space, w_order, NPY.CORDER) shape, elems_w = find_shape_and_elems(space, w_object, dtype) if dtype is None and space.isinstance_w(w_object, space.w_buffer): dtype = descriptor.get_dtype_cache(space).w_uint8dtype if dtype is None or (dtype.is_str_or_unicode() and dtype.elsize < 1): dtype = find_dtype_for_seq(space, elems_w, dtype) w_arr = W_NDimArray.from_shape(space, shape, dtype, order=npy_order) if support.product(shape) == 1: # safe from overflow since from_shape checks w_arr.set_scalar_value(dtype.coerce(space, elems_w[0])) else: loop.assign(space, w_arr, elems_w) return w_arr
def _array_from_buffer_3118(space, w_object, dtype): try: w_buf = space.call_method(space.builtin, "memoryview", w_object) except OperationError as e: if e.match(space, space.w_TypeError): # object does not have buffer interface return w_object raise format = space.getattr(w_buf,space.newbytes('format')) if format: descr = _descriptor_from_pep3118_format(space, space.str_w(format)) if not descr: return w_object if dtype and descr: raise oefmt(space.w_NotImplementedError, "creating an array from a memoryview while specifying dtype " "not supported") if descr.elsize != space.int_w(space.getattr(w_buf, space.newbytes('itemsize'))): msg = ("Item size computed from the PEP 3118 buffer format " "string does not match the actual item size.") space.warn(space.wrap(msg), space.w_RuntimeWarning) return w_object dtype = descr elif not dtype: dtype = descriptor.get_dtype_cache(space).w_stringdtype dtype.elsize = space.int_w(space.getattr(w_buf, space.newbytes('itemsize'))) nd = space.int_w(space.getattr(w_buf, space.newbytes('ndim'))) shape = [space.int_w(d) for d in space.listview( space.getattr(w_buf, space.newbytes('shape')))] strides = [] buflen = space.len_w(w_buf) * dtype.elsize if shape: strides = [space.int_w(d) for d in space.listview( space.getattr(w_buf, space.newbytes('strides')))] if not strides: d = buflen strides = [0] * nd for k in range(nd): if shape[k] > 0: d /= shape[k] strides[k] = d else: if nd == 1: shape = [buflen / dtype.elsize, ] strides = [dtype.elsize, ] elif nd > 1: msg = ("ndim computed from the PEP 3118 buffer format " "is greater than 1, but shape is NULL.") space.warn(space.wrap(msg), space.w_RuntimeWarning) return w_object try: w_data = rffi.cast(RAW_STORAGE_PTR, space.int_w(space.call_method(w_buf, '_pypy_raw_address'))) except OperationError as e: if e.match(space, space.w_ValueError): return w_object else: raise e writable = not space.bool_w(space.getattr(w_buf, space.newbytes('readonly'))) w_ret = W_NDimArray.from_shape_and_storage(space, shape, w_data, storage_bytes=buflen, dtype=dtype, w_base=w_object, writable=writable, strides=strides) if w_ret: return w_ret return w_object
def _array(space, w_object, w_dtype=None, copy=True, w_order=None, subok=False): from pypy.module.micronumpy import strides # for anything that isn't already an array, try __array__ method first if not isinstance(w_object, W_NDimArray): w_array = try_array_method(space, w_object, w_dtype) if w_array is not None: # continue with w_array, but do further operations in place w_object = w_array copy = False dtype = descriptor.decode_w_dtype(space, w_dtype) if space.is_none(w_order): order = 'C' else: order = space.str_w(w_order) if order == 'K': order = 'C' if order != 'C': # or order != 'F': raise oefmt(space.w_ValueError, "Unknown order: %s", order) if isinstance(w_object, W_NDimArray): if (dtype is None or w_object.get_dtype() is dtype): if copy and (subok or type(w_object) is W_NDimArray): return w_object.descr_copy(space, w_order) elif not copy and (subok or type(w_object) is W_NDimArray): return w_object # we have a ndarray, but need to copy or change dtype or create W_NDimArray if dtype is None: dtype = w_object.get_dtype() if dtype != w_object.get_dtype(): # silently reject the copy value copy = True if copy: shape = w_object.get_shape() _elems_w = w_object.reshape(space, space.wrap(-1)) elems_w = [None] * w_object.get_size() for i in range(len(elems_w)): elems_w[i] = _elems_w.descr_getitem(space, space.wrap(i)) elif subok: raise oefmt(space.w_NotImplementedError, "array(...copy=False, subok=True) not implemented yet") else: sz = support.product(w_object.get_shape()) * dtype.elsize return W_NDimArray.from_shape_and_storage(space, w_object.get_shape(),w_object.implementation.storage, dtype, storage_bytes=sz, w_base=w_object) else: # not an array shape, elems_w = strides.find_shape_and_elems(space, w_object, dtype) if dtype is None or (dtype.is_str_or_unicode() and dtype.elsize < 1): dtype = strides.find_dtype_for_seq(space, elems_w, dtype) if dtype is None: dtype = descriptor.get_dtype_cache(space).w_float64dtype elif dtype.is_str_or_unicode() and dtype.elsize < 1: # promote S0 -> S1, U0 -> U1 dtype = descriptor.variable_dtype(space, dtype.char + '1') w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order) if len(elems_w) == 1: w_arr.set_scalar_value(dtype.coerce(space, elems_w[0])) else: loop.assign(space, w_arr, elems_w) return w_arr