[pypy-commit] pypy numpy-dtype-refactor-complex: merged default
alex_gaynor
noreply at buildbot.pypy.org
Tue Dec 6 20:28:20 CET 2011
Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: numpy-dtype-refactor-complex
Changeset: r50220:e4002f73fb25
Date: 2011-12-06 14:12 -0500
http://bitbucket.org/pypy/pypy/changeset/e4002f73fb25/
Log: merged default
diff --git a/lib_pypy/_collections.py b/lib_pypy/_collections.py
--- a/lib_pypy/_collections.py
+++ b/lib_pypy/_collections.py
@@ -379,12 +379,14 @@
class defaultdict(dict):
def __init__(self, *args, **kwds):
- self.default_factory = None
- if 'default_factory' in kwds:
- self.default_factory = kwds.pop('default_factory')
- elif len(args) > 0 and (callable(args[0]) or args[0] is None):
- self.default_factory = args[0]
+ if len(args) > 0:
+ default_factory = args[0]
args = args[1:]
+ if not callable(default_factory) and default_factory is not None:
+ raise TypeError("first argument must be callable")
+ else:
+ default_factory = None
+ self.default_factory = default_factory
super(defaultdict, self).__init__(*args, **kwds)
def __missing__(self, key):
@@ -404,7 +406,7 @@
recurse.remove(id(self))
def copy(self):
- return type(self)(self, default_factory=self.default_factory)
+ return type(self)(self.default_factory, self)
def __copy__(self):
return self.copy()
diff --git a/pypy/jit/metainterp/test/test_math.py b/pypy/jit/metainterp/test/test_math.py
new file mode 100644
--- /dev/null
+++ b/pypy/jit/metainterp/test/test_math.py
@@ -0,0 +1,47 @@
+import math
+from pypy.jit.metainterp.test.support import LLJitMixin, OOJitMixin
+from pypy.rlib.rfloat import isinf, isnan, INFINITY, NAN
+
+class MathTests:
+
+ def test_math_sqrt(self):
+ def f(x):
+ try:
+ return math.sqrt(x)
+ except ValueError:
+ return -INFINITY
+
+ res = self.interp_operations(f, [0.0])
+ assert res == 0.0
+ self.check_operations_history(call_pure=1)
+ #
+ res = self.interp_operations(f, [25.0])
+ assert res == 5.0
+ self.check_operations_history(call_pure=1)
+ #
+ res = self.interp_operations(f, [-0.0])
+ assert str(res) == '-0.0'
+ self.check_operations_history(call_pure=1)
+ #
+ res = self.interp_operations(f, [1000000.0])
+ assert res == 1000.0
+ self.check_operations_history(call_pure=1)
+ #
+ res = self.interp_operations(f, [-1.0])
+ assert res == -INFINITY
+ self.check_operations_history(call_pure=0)
+ #
+ res = self.interp_operations(f, [INFINITY])
+ assert isinf(res) and not isnan(res) and res > 0.0
+ self.check_operations_history(call_pure=0)
+ #
+ res = self.interp_operations(f, [NAN])
+ assert isnan(res) and not isinf(res)
+ self.check_operations_history(call_pure=0)
+
+
+class TestOOtype(MathTests, OOJitMixin):
+ pass
+
+class TestLLtype(MathTests, LLJitMixin):
+ pass
diff --git a/pypy/module/_collections/app_defaultdict.py b/pypy/module/_collections/app_defaultdict.py
--- a/pypy/module/_collections/app_defaultdict.py
+++ b/pypy/module/_collections/app_defaultdict.py
@@ -13,12 +13,14 @@
class defaultdict(dict):
def __init__(self, *args, **kwds):
- self.default_factory = None
- if 'default_factory' in kwds:
- self.default_factory = kwds.pop('default_factory')
- elif len(args) > 0 and (callable(args[0]) or args[0] is None):
- self.default_factory = args[0]
+ if len(args) > 0:
+ default_factory = args[0]
args = args[1:]
+ if not callable(default_factory) and default_factory is not None:
+ raise TypeError("first argument must be callable")
+ else:
+ default_factory = None
+ self.default_factory = default_factory
super(defaultdict, self).__init__(*args, **kwds)
def __missing__(self, key):
@@ -36,7 +38,7 @@
recurse.remove(id(self))
def copy(self):
- return type(self)(self, default_factory=self.default_factory)
+ return type(self)(self.default_factory, self)
def __copy__(self):
return self.copy()
diff --git a/pypy/module/_collections/test/test_defaultdict.py b/pypy/module/_collections/test/test_defaultdict.py
--- a/pypy/module/_collections/test/test_defaultdict.py
+++ b/pypy/module/_collections/test/test_defaultdict.py
@@ -19,11 +19,41 @@
def test_keyerror_without_factory(self):
from _collections import defaultdict
- d1 = defaultdict()
- for key in ['foo', (1,)]:
- try:
- d1[key]
- except KeyError, err:
- assert err.args[0] == key
- else:
- assert 0, "expected KeyError"
+ for d1 in [defaultdict(), defaultdict(None)]:
+ for key in ['foo', (1,)]:
+ try:
+ d1[key]
+ except KeyError, err:
+ assert err.args[0] == key
+ else:
+ assert 0, "expected KeyError"
+
+ def test_noncallable(self):
+ from _collections import defaultdict
+ raises(TypeError, defaultdict, [('a', 5)])
+ d = defaultdict(None, [('a', 5)])
+ assert d.items() == [('a', 5)]
+
+ def test_kwds(self):
+ from _collections import defaultdict
+ d = defaultdict(default_factory=5)
+ assert d.keys() == ['default_factory']
+
+ def test_copy(self):
+ import _collections
+ def f():
+ return 42
+ d = _collections.defaultdict(f, {2: 3})
+ #
+ d1 = d.copy()
+ assert type(d1) is _collections.defaultdict
+ assert len(d1) == 1
+ assert d1[2] == 3
+ assert d1[3] == 42
+ #
+ import copy
+ d2 = copy.deepcopy(d)
+ assert type(d2) is _collections.defaultdict
+ assert len(d2) == 1
+ assert d2[2] == 3
+ assert d2[3] == 42
diff --git a/pypy/module/_socket/interp_func.py b/pypy/module/_socket/interp_func.py
--- a/pypy/module/_socket/interp_func.py
+++ b/pypy/module/_socket/interp_func.py
@@ -1,7 +1,7 @@
from pypy.interpreter.gateway import unwrap_spec
from pypy.module._socket.interp_socket import converted_error, W_RSocket
from pypy.rlib import rsocket
-from pypy.rlib.rsocket import SocketError
+from pypy.rlib.rsocket import SocketError, INVALID_SOCKET
from pypy.interpreter.error import OperationError
def gethostname(space):
@@ -284,7 +284,7 @@
space.wrap(socktype),
space.wrap(protocol),
space.wrap(canonname),
- addr.as_object(-1, space)]) # -1 as per cpython
+ addr.as_object(INVALID_SOCKET, space)]) # -1 as per cpython
for (family, socktype, protocol, canonname, addr) in lst]
return space.newlist(lst1)
diff --git a/pypy/module/micronumpy/__init__.py b/pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py
+++ b/pypy/module/micronumpy/__init__.py
@@ -32,6 +32,7 @@
'int_': 'interp_boxes.W_LongBox',
'inexact': 'interp_boxes.W_InexactBox',
'floating': 'interp_boxes.W_FloatingBox',
+ 'float32': 'interp_boxes.W_Float32Box',
'float64': 'interp_boxes.W_Float64Box',
'complexfloating': 'interp_boxes.W_ComplexFloatingBox',
'complex128': 'interp_boxes.W_Complex128Box',
@@ -78,4 +79,5 @@
'inf': 'app_numpy.inf',
'e': 'app_numpy.e',
'arange': 'app_numpy.arange',
+ 'reshape': 'app_numpy.reshape',
}
diff --git a/pypy/module/micronumpy/app_numpy.py b/pypy/module/micronumpy/app_numpy.py
--- a/pypy/module/micronumpy/app_numpy.py
+++ b/pypy/module/micronumpy/app_numpy.py
@@ -36,3 +36,40 @@
j += 1
i += step
return arr
+
+
+def reshape(a, shape):
+ '''reshape(a, newshape)
+ Gives a new shape to an array without changing its data.
+
+ Parameters
+ ----------
+ a : array_like
+ Array to be reshaped.
+ newshape : int or tuple of ints
+ The new shape should be compatible with the original shape. If
+ an integer, then the result will be a 1-D array of that length.
+ One shape dimension can be -1. In this case, the value is inferred
+ from the length of the array and remaining dimensions.
+
+ Returns
+ -------
+ reshaped_array : ndarray
+ This will be a new view object if possible; otherwise, it will
+ be a copy.
+
+
+ See Also
+ --------
+ ndarray.reshape : Equivalent method.
+
+ Notes
+ -----
+
+ It is not always possible to change the shape of an array without
+ copying the data. If you want an error to be raise if the data is copied,
+ you should assign the new shape to the shape attribute of the array
+'''
+ if not hasattr(a, 'reshape'):
+ a = numpypy.array(a)
+ return a.reshape(shape)
diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py
--- a/pypy/module/micronumpy/interp_boxes.py
+++ b/pypy/module/micronumpy/interp_boxes.py
@@ -285,6 +285,8 @@
W_Float32Box.typedef = TypeDef("float32", W_FloatingBox.typedef,
__module__ = "numpypy",
+
+ __new__ = interp2app(W_Float32Box.descr__new__.im_func),
)
W_Float64Box.typedef = TypeDef("float64", (W_FloatingBox.typedef, float_typedef),
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
@@ -98,6 +98,105 @@
endshape[i] = remainder[i]
return endshape
+def get_shape_from_iterable(space, old_size, w_iterable):
+ new_size = 0
+ new_shape = []
+ if space.isinstance_w(w_iterable, space.w_int):
+ new_size = space.int_w(w_iterable)
+ if new_size < 0:
+ new_size = old_size
+ new_shape = [new_size]
+ else:
+ neg_dim = -1
+ batch = space.listview(w_iterable)
+ new_size = 1
+ if len(batch) < 1:
+ if old_size == 1:
+ # Scalars can have an empty size.
+ new_size = 1
+ else:
+ new_size = 0
+ new_shape = []
+ i = 0
+ for elem in batch:
+ s = space.int_w(elem)
+ if s < 0:
+ if neg_dim >= 0:
+ raise OperationError(space.w_ValueError, space.wrap(
+ "can only specify one unknown dimension"))
+ s = 1
+ neg_dim = i
+ new_size *= s
+ new_shape.append(s)
+ i += 1
+ if neg_dim >= 0:
+ new_shape[neg_dim] = old_size / new_size
+ new_size *= new_shape[neg_dim]
+ if new_size != old_size:
+ raise OperationError(space.w_ValueError,
+ space.wrap("total size of new array must be unchanged"))
+ return new_shape
+
+# Recalculating strides. Find the steps that the iteration does for each
+# dimension, given the stride and shape. Then try to create a new stride that
+# fits the new shape, using those steps. If there is a shape/step mismatch
+# (meaning that the realignment of elements crosses from one step into another)
+# return None so that the caller can raise an exception.
+def calc_new_strides(new_shape, old_shape, old_strides):
+ # Return the proper strides for new_shape, or None if the mapping crosses
+ # stepping boundaries
+
+ # Assumes that prod(old_shape) == prod(new_shape), len(old_shape) > 1, and
+ # len(new_shape) > 0
+ steps = []
+ last_step = 1
+ oldI = 0
+ new_strides = []
+ if old_strides[0] < old_strides[-1]:
+ for i in range(len(old_shape)):
+ steps.append(old_strides[i] / last_step)
+ last_step *= old_shape[i]
+ cur_step = steps[0]
+ n_new_elems_used = 1
+ n_old_elems_to_use = old_shape[0]
+ for s in new_shape:
+ new_strides.append(cur_step * n_new_elems_used)
+ n_new_elems_used *= s
+ while n_new_elems_used > n_old_elems_to_use:
+ oldI += 1
+ if steps[oldI] != steps[oldI - 1]:
+ return None
+ n_old_elems_to_use *= old_shape[oldI]
+ if n_new_elems_used == n_old_elems_to_use:
+ oldI += 1
+ if oldI >= len(old_shape):
+ break
+ cur_step = steps[oldI]
+ n_old_elems_to_use *= old_shape[oldI]
+ else:
+ for i in range(len(old_shape) - 1, -1, -1):
+ steps.insert(0, old_strides[i] / last_step)
+ last_step *= old_shape[i]
+ cur_step = steps[-1]
+ n_new_elems_used = 1
+ oldI = -1
+ n_old_elems_to_use = old_shape[-1]
+ for i in range(len(new_shape) - 1, -1, -1):
+ s = new_shape[i]
+ new_strides.insert(0, cur_step * n_new_elems_used)
+ n_new_elems_used *= s
+ while n_new_elems_used > n_old_elems_to_use:
+ oldI -= 1
+ if steps[oldI] != steps[oldI + 1]:
+ return None
+ n_old_elems_to_use *= old_shape[oldI]
+ if n_new_elems_used == n_old_elems_to_use:
+ oldI -= 1
+ if oldI < -len(old_shape):
+ break
+ cur_step = steps[oldI]
+ n_old_elems_to_use *= old_shape[oldI]
+ return new_strides
# Iterators for arrays
# --------------------
@@ -444,6 +543,7 @@
return False
i = i.next(shapelen)
return True
+
def descr_all(self, space):
return space.wrap(self._all())
@@ -459,6 +559,7 @@
return True
i = i.next(shapelen)
return False
+
def descr_any(self, space):
return space.wrap(self._any())
@@ -483,6 +584,12 @@
def descr_get_shape(self, space):
return space.newtuple([space.wrap(i) for i in self.shape])
+ def descr_set_shape(self, space, w_iterable):
+ concrete = self.get_concrete()
+ new_shape = get_shape_from_iterable(space,
+ concrete.find_size(), w_iterable)
+ concrete.setshape(space, new_shape)
+
def descr_get_size(self, space):
return space.wrap(self.find_size())
@@ -607,11 +714,6 @@
def _index_of_single_item(self, space, w_idx):
if space.isinstance_w(w_idx, space.w_int):
idx = space.int_w(w_idx)
- if not self.shape:
- if idx != 0:
- raise OperationError(space.w_IndexError,
- space.wrap("index out of range"))
- return 0
if idx < 0:
idx = self.shape[0] + idx
if idx < 0 or idx >= self.shape[0]:
@@ -730,10 +832,49 @@
strides += self.strides[s:]
backstrides += self.backstrides[s:]
new_sig = signature.Signature.find_sig([
- NDimSlice.signature, self.signature,
+ W_NDimSlice.signature, self.signature,
])
- return NDimSlice(self, new_sig, start, strides[:], backstrides[:],
- shape[:])
+ return W_NDimSlice(self, new_sig, start, strides[:], backstrides[:],
+ shape[:])
+
+ def descr_reshape(self, space, args_w):
+ """reshape(...)
+ a.reshape(shape)
+
+ Returns an array containing the same data with a new shape.
+
+ Refer to `numpypy.reshape` for full documentation.
+
+ See Also
+ --------
+ numpypy.reshape : equivalent function
+"""
+ if len(args_w) == 1:
+ w_shape = args_w[0]
+ else:
+ w_shape = space.newlist(args_w)
+ concrete = self.get_concrete()
+ new_shape = get_shape_from_iterable(space,
+ concrete.find_size(), w_shape)
+ # Since we got to here, prod(new_shape) == self.size
+ new_strides = calc_new_strides(new_shape,
+ concrete.shape, concrete.strides)
+ if new_strides:
+ # We can create a view, strides somehow match up.
+ new_sig = signature.Signature.find_sig([
+ W_NDimSlice.signature, self.signature
+ ])
+ ndims = len(new_shape)
+ new_backstrides = [0] * ndims
+ for nd in range(ndims):
+ new_backstrides[nd] = (new_shape[nd] - 1) * new_strides[nd]
+ arr = W_NDimSlice(self, new_sig, self.start, new_strides,
+ new_backstrides, new_shape)
+ else:
+ # Create copy with contiguous data
+ arr = concrete.copy()
+ arr.setshape(space, new_shape)
+ return arr
def descr_mean(self, space):
return space.div(self.descr_sum(space), space.wrap(self.find_size()))
@@ -751,7 +892,7 @@
if len(concrete.shape) < 2:
return space.wrap(self)
new_sig = signature.Signature.find_sig([
- NDimSlice.signature, self.signature
+ W_NDimSlice.signature, self.signature
])
strides = []
backstrides = []
@@ -760,8 +901,8 @@
strides.append(concrete.strides[i])
backstrides.append(concrete.backstrides[i])
shape.append(concrete.shape[i])
- return space.wrap(NDimSlice(concrete, new_sig, self.start, strides[:],
- backstrides[:], shape[:]))
+ return space.wrap(W_NDimSlice(concrete, new_sig, self.start, strides[:],
+ backstrides[:], shape[:]))
def descr_get_flatiter(self, space):
return space.wrap(W_FlatIterator(self))
@@ -830,6 +971,11 @@
def debug_repr(self):
return 'Scalar'
+ def setshape(self, space, new_shape):
+ # In order to get here, we already checked that prod(new_shape) == 1,
+ # so in order to have a consistent API, let it go through.
+ pass
+
class VirtualArray(BaseArray):
"""
Class for representing virtual arrays, such as binary ops or ufuncs
@@ -1022,13 +1168,46 @@
return space.wrap(self.shape[0])
return space.wrap(1)
+ def setshape(self, space, new_shape):
+ if len(self.shape) < 1:
+ return
+ elif len(self.shape) < 2:
+ # TODO: this code could be refactored into calc_strides
+ # but then calc_strides would have to accept a stepping factor
+ strides = []
+ backstrides = []
+ s = self.strides[0]
+ if self.order == 'C':
+ new_shape.reverse()
+ for sh in new_shape:
+ strides.append(s)
+ backstrides.append(s * (sh - 1))
+ s *= sh
+ if self.order == 'C':
+ strides.reverse()
+ backstrides.reverse()
+ new_shape.reverse()
+ self.strides = strides[:]
+ self.backstrides = backstrides[:]
+ self.shape = new_shape[:]
+ return
+ new_strides = calc_new_strides(new_shape, self.shape, self.strides)
+ if new_strides is None:
+ raise OperationError(space.w_AttributeError, space.wrap(
+ "incompatible shape for a non-contiguous array"))
+ new_backstrides = [0] * len(new_shape)
+ for nd in range(len(new_shape)):
+ new_backstrides[nd] = (new_shape[nd] - 1) * new_strides[nd]
+ self.strides = new_strides[:]
+ self.backstrides = new_backstrides[:]
+ self.shape = new_shape[:]
-class NDimSlice(ViewArray):
+class W_NDimSlice(ViewArray):
signature = signature.BaseSignature()
def __init__(self, parent, signature, start, strides, backstrides,
shape):
- if isinstance(parent, NDimSlice):
+ if isinstance(parent, W_NDimSlice):
parent = parent.parent
ViewArray.__init__(self, parent, signature, strides, backstrides, shape)
self.start = start
@@ -1077,9 +1256,11 @@
def copy(self):
array = W_NDimArray(self.size, self.shape[:], self.find_dtype())
iter = self.start_iter()
+ a_iter = array.start_iter()
while not iter.done():
- array.setitem(iter.offset, self.getitem(iter.offset))
+ array.setitem(a_iter.offset, self.getitem(iter.offset))
iter = iter.next(len(self.shape))
+ a_iter = a_iter.next(len(array.shape))
return array
class W_NDimArray(BaseArray):
@@ -1137,6 +1318,10 @@
return ArrayIterator(self.size)
raise NotImplementedError # use ViewIterator simply, test it
+ def setshape(self, space, new_shape):
+ self.shape = new_shape
+ self.calc_strides(new_shape)
+
def debug_repr(self):
return 'Array'
@@ -1261,7 +1446,8 @@
__debug_repr__ = interp2app(BaseArray.descr_debug_repr),
dtype = GetSetProperty(BaseArray.descr_get_dtype),
- shape = GetSetProperty(BaseArray.descr_get_shape),
+ shape = GetSetProperty(BaseArray.descr_get_shape,
+ BaseArray.descr_set_shape),
size = GetSetProperty(BaseArray.descr_get_size),
T = GetSetProperty(BaseArray.descr_get_transpose),
@@ -1279,6 +1465,7 @@
dot = interp2app(BaseArray.descr_dot),
copy = interp2app(BaseArray.descr_copy),
+ reshape = interp2app(BaseArray.descr_reshape),
)
diff --git a/pypy/module/micronumpy/test/test_dtypes.py b/pypy/module/micronumpy/test/test_dtypes.py
--- a/pypy/module/micronumpy/test/test_dtypes.py
+++ b/pypy/module/micronumpy/test/test_dtypes.py
@@ -251,6 +251,13 @@
assert numpy.dtype(numpy.int64).type is numpy.int64
assert numpy.int64(3) == 3
+ def test_float32(self):
+ import numpypy as numpy
+
+ assert numpy.float32.mro() == [numpy.float32, numpy.floating, numpy.inexact, numpy.number, numpy.generic, object]
+
+ assert numpy.float32(12) == numpy.float64(12)
+
def test_float64(self):
import numpypy as numpy
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
@@ -158,6 +158,13 @@
assert shape_agreement(self.space,
[5, 2], [4, 3, 5, 2]) == [4, 3, 5, 2]
+ def test_calc_new_strides(self):
+ from pypy.module.micronumpy.interp_numarray import calc_new_strides
+ assert calc_new_strides([2, 4], [4, 2], [4, 2]) == [8, 2]
+ assert calc_new_strides([2, 4, 3], [8, 3], [1, 16]) == [1, 2, 16]
+ assert calc_new_strides([2, 3, 4], [8, 3], [1, 16]) is None
+ assert calc_new_strides([24], [2, 4, 3], [48, 6, 1]) is None
+ assert calc_new_strides([24], [2, 4, 3], [24, 6, 2]) == [2]
class AppTestNumArray(BaseNumpyAppTest):
def test_ndarray(self):
@@ -216,8 +223,8 @@
assert a[2] == 4
def test_copy(self):
- from numpypy import array
- a = array(range(5))
+ from numpypy import arange, array
+ a = arange(5)
b = a.copy()
for i in xrange(5):
assert b[i] == a[i]
@@ -227,6 +234,11 @@
a = array(1)
assert a.copy() == a
+ a = arange(8)
+ b = a[::2]
+ c = b.copy()
+ assert (c == b).all()
+
def test_iterator_init(self):
from numpypy import array
a = array(range(5))
@@ -318,8 +330,8 @@
def test_scalar(self):
from numpypy import array, dtype
a = array(3)
- #assert a[0] == 3
raises(IndexError, "a[0]")
+ raises(IndexError, "a[0] = 5")
assert a.size == 1
assert a.shape == ()
assert a.dtype is dtype(int)
@@ -339,6 +351,81 @@
c = a[:3]
assert c.shape == (3,)
+ def test_set_shape(self):
+ from numpypy import array, zeros
+ a = array([])
+ a.shape = []
+ a = array(range(12))
+ a.shape = (3, 4)
+ assert (a == [range(4), range(4, 8), range(8, 12)]).all()
+ a.shape = (3, 2, 2)
+ assert a[1, 1, 1] == 7
+ a.shape = (3, -1, 2)
+ assert a.shape == (3, 2, 2)
+ a.shape = 12
+ assert a.shape == (12, )
+ exc = raises(ValueError, "a.shape = 10")
+ assert str(exc.value) == "total size of new array must be unchanged"
+ a = array(3)
+ a.shape = ()
+ #numpy allows this
+ a.shape = (1,)
+
+ def test_reshape(self):
+ from numpypy import array, zeros
+ a = array(range(12))
+ exc = raises(ValueError, "b = a.reshape((3, 10))")
+ assert str(exc.value) == "total size of new array must be unchanged"
+ b = a.reshape((3, 4))
+ assert b.shape == (3, 4)
+ assert (b == [range(4), range(4, 8), range(8, 12)]).all()
+ b[:, 0] = 1000
+ assert (a == [1000, 1, 2, 3, 1000, 5, 6, 7, 1000, 9, 10, 11]).all()
+ a = zeros((4, 2, 3))
+ a.shape = (12, 2)
+
+ def test_slice_reshape(self):
+ from numpypy import zeros, arange
+ a = zeros((4, 2, 3))
+ b = a[::2, :, :]
+ b.shape = (2, 6)
+ exc = raises(AttributeError, "b.shape = 12")
+ assert str(exc.value) == \
+ "incompatible shape for a non-contiguous array"
+ b = a[::2, :, :].reshape((2, 6))
+ assert b.shape == (2, 6)
+ b = arange(20)[1:17:2]
+ b.shape = (4, 2)
+ assert (b == [[1, 3], [5, 7], [9, 11], [13, 15]]).all()
+ c = b.reshape((2, 4))
+ assert (c == [[1, 3, 5, 7], [9, 11, 13, 15]]).all()
+
+ z = arange(96).reshape((12, -1))
+ assert z.shape == (12, 8)
+ y = z.reshape((4, 3, 8))
+ v = y[:, ::2, :]
+ w = y.reshape(96)
+ u = v.reshape(64)
+ assert y[1, 2, 1] == z[5, 1]
+ y[1, 2, 1] = 1000
+ # z, y, w, v are views of eachother
+ assert z[5, 1] == 1000
+ assert v[1, 1, 1] == 1000
+ assert w[41] == 1000
+ # u is not a view, it is a copy!
+ assert u[25] == 41
+
+ a = zeros((5, 2))
+ assert a.reshape(-1).shape == (10,)
+
+ raises(ValueError, arange(10).reshape, (5, -1, -1))
+
+ def test_reshape_varargs(self):
+ from numpypy import arange
+ z = arange(96).reshape(12, -1)
+ y = z.reshape(4, 3, 8)
+ assert y.shape == (4, 3, 8)
+
def test_add(self):
from numpypy import array
a = array(range(5))
@@ -1168,3 +1255,14 @@
a = arange(0, 0.8, 0.1)
assert len(a) == 8
assert arange(False, True, True).dtype is dtype(int)
+
+
+class AppTestRanges(BaseNumpyAppTest):
+ def test_app_reshape(self):
+ from numpypy import arange, array, dtype, reshape
+ a = arange(12)
+ b = reshape(a, (3, 4))
+ assert b.shape == (3, 4)
+ a = range(12)
+ b = reshape(a, (3, 4))
+ assert b.shape == (3, 4)
diff --git a/pypy/module/micronumpy/test/test_zjit.py b/pypy/module/micronumpy/test/test_zjit.py
--- a/pypy/module/micronumpy/test/test_zjit.py
+++ b/pypy/module/micronumpy/test/test_zjit.py
@@ -8,13 +8,12 @@
from pypy.jit.metainterp import pyjitpl
from pypy.jit.metainterp.test.support import LLJitMixin
from pypy.jit.metainterp.warmspot import reset_stats
-from pypy.module.micronumpy import interp_boxes, interp_ufuncs, signature
-from pypy.module.micronumpy.compile import (numpy_compile, FakeSpace,
- FloatObject, IntObject, BoolObject, Parser, InterpreterState)
-from pypy.module.micronumpy.interp_numarray import (W_NDimArray, NDimSlice,
+from pypy.module.micronumpy import interp_boxes
+from pypy.module.micronumpy.compile import (FakeSpace,
+ IntObject, Parser, InterpreterState)
+from pypy.module.micronumpy.interp_numarray import (W_NDimArray,
BaseArray)
from pypy.rlib.nonconst import NonConstant
-from pypy.rpython.annlowlevel import llstr, hlstr
class TestNumpyJIt(LLJitMixin):
diff --git a/pypy/module/test_lib_pypy/test_collections.py b/pypy/module/test_lib_pypy/test_collections.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/test_collections.py
@@ -0,0 +1,27 @@
+
+"""
+Extra tests for the pure Python PyPy _collections module
+(not used in normal PyPy's)
+"""
+
+from pypy.conftest import gettestobjspace
+
+class AppTestcStringIO:
+ def test_copy(self):
+ import _collections
+ def f():
+ return 42
+ d = _collections.defaultdict(f, {2: 3})
+ #
+ d1 = d.copy()
+ assert type(d1) is _collections.defaultdict
+ assert len(d1) == 1
+ assert d1[2] == 3
+ assert d1[3] == 42
+ #
+ import copy
+ d2 = copy.deepcopy(d)
+ assert type(d2) is _collections.defaultdict
+ assert len(d2) == 1
+ assert d2[2] == 3
+ assert d2[3] == 42
diff --git a/pypy/rlib/_rsocket_rffi.py b/pypy/rlib/_rsocket_rffi.py
--- a/pypy/rlib/_rsocket_rffi.py
+++ b/pypy/rlib/_rsocket_rffi.py
@@ -418,7 +418,7 @@
if _MSVC:
def invalid_socket(fd):
return fd == INVALID_SOCKET
- INVALID_SOCKET = intmask(cConfig.INVALID_SOCKET)
+ INVALID_SOCKET = r_uint(cConfig.INVALID_SOCKET)
else:
def invalid_socket(fd):
return fd < 0
diff --git a/pypy/rlib/rsocket.py b/pypy/rlib/rsocket.py
--- a/pypy/rlib/rsocket.py
+++ b/pypy/rlib/rsocket.py
@@ -20,6 +20,7 @@
from pypy.rlib.rarithmetic import intmask, r_uint
from pypy.rpython.lltypesystem import lltype, rffi
from pypy.rpython.lltypesystem.rffi import sizeof, offsetof
+INVALID_SOCKET = _c.INVALID_SOCKET
def mallocbuf(buffersize):
return lltype.malloc(rffi.CCHARP.TO, buffersize, flavor='raw')
More information about the pypy-commit
mailing list