Author: Armin Rigo <[email protected]>
Branch:
Changeset: r72316:4d1d1c2d78ae
Date: 2014-07-02 17:37 +0200
http://bitbucket.org/pypy/pypy/changeset/4d1d1c2d78ae/
Log: Issue #1790: implement numpy.empty() differently than numpy.zeros().
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
@@ -12,7 +12,7 @@
'scalar' : 'ctors.build_scalar',
'array': 'ctors.array',
'zeros': 'ctors.zeros',
- 'empty': 'ctors.zeros',
+ 'empty': 'ctors.empty',
'empty_like': 'ctors.empty_like',
'fromstring': 'ctors.fromstring',
'frombuffer': 'ctors.frombuffer',
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,12 +28,12 @@
self.implementation = implementation
@staticmethod
- def from_shape(space, shape, dtype, order='C', w_instance=None):
+ def from_shape(space, shape, dtype, order='C', w_instance=None, zero=True):
from pypy.module.micronumpy import concrete
from pypy.module.micronumpy.strides import calc_strides
strides, backstrides = calc_strides(shape, dtype.base, order)
impl = concrete.ConcreteArray(shape, dtype.base, order, strides,
- backstrides)
+ backstrides, zero=zero)
if w_instance:
return wrap_impl(space, space.type(w_instance), w_instance, impl)
return W_NDimArray(impl)
diff --git a/pypy/module/micronumpy/concrete.py
b/pypy/module/micronumpy/concrete.py
--- a/pypy/module/micronumpy/concrete.py
+++ b/pypy/module/micronumpy/concrete.py
@@ -369,9 +369,11 @@
class ConcreteArray(ConcreteArrayNotOwning):
- def __init__(self, shape, dtype, order, strides, backstrides,
storage=lltype.nullptr(RAW_STORAGE)):
+ def __init__(self, shape, dtype, order, strides, backstrides,
+ storage=lltype.nullptr(RAW_STORAGE), zero=True):
if storage == lltype.nullptr(RAW_STORAGE):
- storage = dtype.itemtype.malloc(support.product(shape) *
dtype.elsize)
+ storage = dtype.itemtype.malloc(support.product(shape) *
+ dtype.elsize, zero=zero)
ConcreteArrayNotOwning.__init__(self, shape, dtype, order, strides,
backstrides,
storage)
diff --git a/pypy/module/micronumpy/ctors.py b/pypy/module/micronumpy/ctors.py
--- a/pypy/module/micronumpy/ctors.py
+++ b/pypy/module/micronumpy/ctors.py
@@ -91,13 +91,19 @@
return w_arr
-def zeros(space, w_shape, w_dtype=None, w_order=None):
+def _zeros_or_empty(space, w_shape, w_dtype, w_order, zero):
dtype = space.interp_w(descriptor.W_Dtype,
space.call_function(space.gettypefor(descriptor.W_Dtype), w_dtype))
if dtype.is_str_or_unicode() and dtype.elsize < 1:
dtype = descriptor.variable_dtype(space, dtype.char + '1')
shape = shape_converter(space, w_shape, dtype)
- return W_NDimArray.from_shape(space, shape, dtype=dtype)
+ return W_NDimArray.from_shape(space, shape, dtype=dtype, zero=zero)
+
+def empty(space, w_shape, w_dtype=None, w_order=None):
+ return _zeros_or_empty(space, w_shape, w_dtype, w_order, zero=False)
+
+def zeros(space, w_shape, w_dtype=None, w_order=None):
+ return _zeros_or_empty(space, w_shape, w_dtype, w_order, zero=True)
@unwrap_spec(subok=bool)
@@ -111,7 +117,8 @@
if dtype.is_str_or_unicode() and dtype.elsize < 1:
dtype = descriptor.variable_dtype(space, dtype.char + '1')
return W_NDimArray.from_shape(space, w_a.get_shape(), dtype=dtype,
- w_instance=w_a if subok else None)
+ w_instance=w_a if subok else None,
+ zero=False)
def _fromstring_text(space, s, count, sep, length, dtype):
diff --git a/pypy/module/micronumpy/test/test_arrayops.py
b/pypy/module/micronumpy/test/test_arrayops.py
--- a/pypy/module/micronumpy/test/test_arrayops.py
+++ b/pypy/module/micronumpy/test/test_arrayops.py
@@ -2,6 +2,20 @@
class AppTestNumSupport(BaseNumpyAppTest):
+ def test_zeros(self):
+ from numpypy import zeros, empty
+ a = zeros(3)
+ assert len(a) == 3
+ assert a[0] == a[1] == a[2] == 0
+ a = empty(1000)
+ assert len(a) == 1000
+ for i in range(1000):
+ if a[i] != 0:
+ break
+ else:
+ raise AssertionError(
+ "empty() returned a zeroed out array of length 1000
(unlikely)")
+
def test_where(self):
from numpypy import where, ones, zeros, array
a = [1, 2, 3, 0, -3]
diff --git a/pypy/module/micronumpy/test/test_ndarray.py
b/pypy/module/micronumpy/test/test_ndarray.py
--- a/pypy/module/micronumpy/test/test_ndarray.py
+++ b/pypy/module/micronumpy/test/test_ndarray.py
@@ -11,7 +11,7 @@
class MockDtype(object):
class itemtype(object):
@staticmethod
- def malloc(size):
+ def malloc(size, zero=True):
return None
def __init__(self):
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -117,8 +117,11 @@
def __repr__(self):
return self.__class__.__name__
- def malloc(self, size):
- return alloc_raw_storage(size, track_allocation=False, zero=True)
+ def malloc(self, size, zero=True):
+ if zero:
+ return alloc_raw_storage(size, track_allocation=False, zero=True)
+ else:
+ return alloc_raw_storage(size, track_allocation=False, zero=False)
class Primitive(object):
_mixin_ = True
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit