summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/_internal.py7
-rw-r--r--numpy/core/bscript2
-rw-r--r--numpy/core/src/multiarray/calculation.c26
-rw-r--r--numpy/core/src/multiarray/scalarapi.c2
-rw-r--r--numpy/core/tests/test_multiarray.py5
-rw-r--r--numpy/core/tests/test_regression.py37
-rw-r--r--numpy/lib/index_tricks.py45
-rw-r--r--numpy/lib/tests/test_index_tricks.py8
-rw-r--r--numpy/lib/utils.py18
9 files changed, 97 insertions, 53 deletions
diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py
index 103418fa2..686b773f2 100644
--- a/numpy/core/_internal.py
+++ b/numpy/core/_internal.py
@@ -285,7 +285,7 @@ def _newnames(datatype, order):
# Given an array with fields and a sequence of field names
# construct a new array with just those fields copied over
def _index_fields(ary, fields):
- from multiarray import empty, dtype
+ from multiarray import empty, dtype, array
dt = ary.dtype
names = [name for name in fields if name in dt.names]
@@ -295,7 +295,10 @@ def _index_fields(ary, fields):
view_dtype = {'names':names, 'formats':formats, 'offsets':offsets, 'itemsize':dt.itemsize}
view = ary.view(dtype=view_dtype)
- return view.copy()
+ # Return a copy for now until behavior is fully deprecated
+ # in favor of returning view
+ copy_dtype = {'names':view_dtype['names'], 'formats':view_dtype['formats']}
+ return array(view, dtype=copy_dtype, copy=True)
# Given a string containing a PEP 3118 format specifier,
# construct a Numpy dtype
diff --git a/numpy/core/bscript b/numpy/core/bscript
index fcbfcb3e0..1ded8eff3 100644
--- a/numpy/core/bscript
+++ b/numpy/core/bscript
@@ -501,7 +501,7 @@ def pre_build(context):
use="npymath")
context.register_builder("umath", build_ufunc)
- context.tweak_extension("scalarmath", use="npymath")
+ context.tweak_extension("scalarmath", use="npymath", includes=["src/private"])
context.tweak_extension("multiarray_tests", use="npymath", includes=["src/private"])
context.tweak_extension("umath_tests", use="npymath", includes=["src/private"])
diff --git a/numpy/core/src/multiarray/calculation.c b/numpy/core/src/multiarray/calculation.c
index 618a8714c..c083a00f0 100644
--- a/numpy/core/src/multiarray/calculation.c
+++ b/numpy/core/src/multiarray/calculation.c
@@ -895,7 +895,7 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
PyArrayObject *maxa = NULL;
PyArrayObject *mina = NULL;
PyArrayObject *newout = NULL, *newin = NULL;
- PyArray_Descr *indescr, *newdescr;
+ PyArray_Descr *indescr = NULL, *newdescr = NULL;
char *max_data, *min_data;
PyObject *zero;
@@ -922,23 +922,24 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
/* Use the fast scalar clip function */
/* First we need to figure out the correct type */
- indescr = NULL;
if (min != NULL) {
indescr = PyArray_DescrFromObject(min, NULL);
if (indescr == NULL) {
- return NULL;
+ goto fail;
}
}
if (max != NULL) {
newdescr = PyArray_DescrFromObject(max, indescr);
Py_XDECREF(indescr);
+ indescr = NULL;
if (newdescr == NULL) {
- return NULL;
+ goto fail;
}
}
else {
/* Steal the reference */
newdescr = indescr;
+ indescr = NULL;
}
@@ -950,8 +951,12 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
if (PyArray_ScalarKind(newdescr->type_num, NULL) >
PyArray_ScalarKind(PyArray_DESCR(self)->type_num, NULL)) {
indescr = PyArray_PromoteTypes(newdescr, PyArray_DESCR(self));
+ if (indescr == NULL) {
+ goto fail;
+ }
func = indescr->f->fastclip;
if (func == NULL) {
+ Py_DECREF(indescr);
return _slow_array_clip(self, min, max, out);
}
}
@@ -960,11 +965,13 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
Py_INCREF(indescr);
}
Py_DECREF(newdescr);
+ newdescr = NULL;
if (!PyDataType_ISNOTSWAPPED(indescr)) {
PyArray_Descr *descr2;
descr2 = PyArray_DescrNewByteorder(indescr, '=');
Py_DECREF(indescr);
+ indescr = NULL;
if (descr2 == NULL) {
goto fail;
}
@@ -973,16 +980,13 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
/* Convert max to an array */
if (max != NULL) {
+ Py_INCREF(indescr);
maxa = (PyArrayObject *)PyArray_FromAny(max, indescr, 0, 0,
NPY_ARRAY_DEFAULT, NULL);
if (maxa == NULL) {
- return NULL;
+ goto fail;
}
}
- else {
- /* Side-effect of PyArray_FromAny */
- Py_DECREF(indescr);
- }
/*
* If we are unsigned, then make sure min is not < 0
@@ -1147,6 +1151,8 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
func(PyArray_DATA(newin), PyArray_SIZE(newin), min_data, max_data, PyArray_DATA(newout));
/* Clean up temporary variables */
+ Py_XDECREF(indescr);
+ Py_XDECREF(newdescr);
Py_XDECREF(mina);
Py_XDECREF(maxa);
Py_DECREF(newin);
@@ -1155,6 +1161,8 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o
return (PyObject *)out;
fail:
+ Py_XDECREF(indescr);
+ Py_XDECREF(newdescr);
Py_XDECREF(maxa);
Py_XDECREF(mina);
Py_XDECREF(newin);
diff --git a/numpy/core/src/multiarray/scalarapi.c b/numpy/core/src/multiarray/scalarapi.c
index d9bc492be..cdb929699 100644
--- a/numpy/core/src/multiarray/scalarapi.c
+++ b/numpy/core/src/multiarray/scalarapi.c
@@ -334,7 +334,7 @@ finish:
if (outcode == NULL) {
return (PyObject *)r;
}
- if (outcode->type_num == typecode->type_num) {
+ if (PyArray_EquivTypes(outcode, typecode)) {
if (!PyTypeNum_ISEXTENDED(typecode->type_num)
|| (outcode->elsize == typecode->elsize)) {
Py_DECREF(outcode);
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index b9fd3ad86..118f221ae 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -1956,6 +1956,11 @@ class TestRecord(TestCase):
assert_equal(b[['f1','f2']][0].tolist(), (2, 3))
assert_equal(b[['f2','f1']][0].tolist(), (3, 2))
assert_equal(b[['f1','f3']][0].tolist(), (2, (1,)))
+ # view of subfield view/copy
+ assert_equal(b[['f1','f2']][0].view(('i4',2)).tolist(), (2, 3))
+ assert_equal(b[['f2','f1']][0].view(('i4',2)).tolist(), (3, 2))
+ view_dtype=[('f1', 'i4'),('f3', [('', 'i4')])]
+ assert_equal(b[['f1','f3']][0].view(view_dtype).tolist(), (2, (1,)))
# non-ascii unicode field indexing is well behaved
if not is_py3:
raise SkipTest('non ascii unicode field indexing skipped; '
diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py
index 492a4f64b..08431c6f0 100644
--- a/numpy/core/tests/test_regression.py
+++ b/numpy/core/tests/test_regression.py
@@ -169,6 +169,34 @@ class TestRegression(TestCase):
assert_(np.all(a[ya] > 0.5))
assert_(np.all(b[yb] > 0.5))
+ def test_endian_where(self,level=rlevel):
+ """GitHuB issue #369"""
+ net = np.zeros(3, dtype='>f4')
+ net[1] = 0.00458849
+ net[2] = 0.605202
+ max_net = net.max()
+ test = np.where(net <= 0., max_net, net)
+ correct = np.array([ 0.60520202, 0.00458849, 0.60520202])
+ assert_array_almost_equal(test, correct)
+
+ def test_endian_recarray(self,level=rlevel):
+ """Ticket #2185"""
+ dt = np.dtype([
+ ('head', '>u4'),
+ ('data', '>u4', 2),
+ ])
+ buf = np.recarray(1, dtype=dt)
+ buf[0]['head'] = 1
+ buf[0]['data'][:] = [1,1]
+
+ h = buf[0]['head']
+ d = buf[0]['data'][0]
+ buf[0]['head'] = h
+ buf[0]['data'][0] = d
+ print buf[0]['head']
+ assert_(buf[0]['head'] == 1)
+
+
def test_mem_dot(self,level=rlevel):
"""Ticket #106"""
x = np.random.randn(0,1)
@@ -1700,5 +1728,14 @@ class TestRegression(TestCase):
a = np.array(['abc'], dtype=np.unicode)[0]
del a
+ def test_refcount_error_in_clip(self):
+ # Ticket #1588
+ a = np.zeros((2,), dtype='>i2').clip(min=0)
+ x = a + a
+ # This used to segfault:
+ y = str(x)
+ # Check the final string:
+ assert_(y == "[0 0]")
+
if __name__ == "__main__":
run_module_suite()
diff --git a/numpy/lib/index_tricks.py b/numpy/lib/index_tricks.py
index 6f2aa1d02..b07fde27d 100644
--- a/numpy/lib/index_tricks.py
+++ b/numpy/lib/index_tricks.py
@@ -18,6 +18,7 @@ import function_base
import numpy.matrixlib as matrix
from function_base import diff
from numpy.lib._compiled_base import ravel_multi_index, unravel_index
+from numpy.lib.stride_tricks import as_strided
makemat = matrix.matrix
def ix_(*args):
@@ -531,37 +532,20 @@ class ndindex(object):
(2, 1, 0)
"""
+ def __init__(self, *shape):
+ x = as_strided(_nx.zeros(1), shape=shape, strides=_nx.zeros_like(shape))
+ self._it = _nx.nditer(x, flags=['multi_index'], order='C')
- def __init__(self, *args):
- if len(args) == 1 and isinstance(args[0], tuple):
- args = args[0]
- self.nd = len(args)
- self.ind = [0]*self.nd
- self.index = 0
- self.maxvals = args
- tot = 1
- for k in range(self.nd):
- tot *= args[k]
- self.total = tot
-
- def _incrementone(self, axis):
- if (axis < 0): # base case
- return
- if (self.ind[axis] < self.maxvals[axis]-1):
- self.ind[axis] += 1
- else:
- self.ind[axis] = 0
- self._incrementone(axis-1)
+ def __iter__(self):
+ return self
def ndincr(self):
"""
Increment the multi-dimensional index by one.
- `ndincr` takes care of the "wrapping around" of the axes.
- It is called by `ndindex.next` and not normally used directly.
-
+ This method is for backward compatibility only: do not use.
"""
- self._incrementone(self.nd-1)
+ self.next()
def next(self):
"""
@@ -573,17 +557,8 @@ class ndindex(object):
Returns a tuple containing the indices of the current iteration.
"""
- if (self.index >= self.total):
- raise StopIteration
- val = tuple(self.ind)
- self.index += 1
- self.ndincr()
- return val
-
- def __iter__(self):
- return self
-
-
+ self._it.next()
+ return self._it.multi_index
# You can do all this with slice() plus a few special objects,
diff --git a/numpy/lib/tests/test_index_tricks.py b/numpy/lib/tests/test_index_tricks.py
index beda2d146..0ede40d5a 100644
--- a/numpy/lib/tests/test_index_tricks.py
+++ b/numpy/lib/tests/test_index_tricks.py
@@ -2,7 +2,7 @@ from numpy.testing import *
import numpy as np
from numpy import ( array, ones, r_, mgrid, unravel_index, zeros, where,
ndenumerate, fill_diagonal, diag_indices,
- diag_indices_from, s_, index_exp )
+ diag_indices_from, s_, index_exp, ndindex )
class TestRavelUnravelIndex(TestCase):
def test_basic(self):
@@ -237,5 +237,11 @@ def test_diag_indices_from():
assert_array_equal(c, np.arange(4))
+def test_ndindex():
+ x = list(np.ndindex(1, 2, 3))
+ expected = [ix for ix, e in np.ndenumerate(np.zeros((1, 2, 3)))]
+ assert_array_equal(x, expected)
+
+
if __name__ == "__main__":
run_module_suite()
diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py
index 924289a6a..dc6c16767 100644
--- a/numpy/lib/utils.py
+++ b/numpy/lib/utils.py
@@ -1136,11 +1136,21 @@ def safe_eval(source):
SyntaxError: Unsupported source construct: compiler.ast.CallFunc
"""
- # Local import to speed up numpy's import time.
+ # Local imports to speed up numpy's import time.
+ import warnings
+ from numpy.testing.utils import WarningManager
+ warn_ctx = WarningManager()
+ warn_ctx.__enter__()
try:
- import compiler
- except ImportError:
- import ast as compiler
+ # compiler package is deprecated for 3.x, which is already solved here
+ warnings.simplefilter('ignore', DeprecationWarning)
+ try:
+ import compiler
+ except ImportError:
+ import ast as compiler
+ finally:
+ warn_ctx.__exit__()
+
walker = SafeEval()
try:
ast = compiler.parse(source, mode="eval")