summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatti Picus <matti.picus@gmail.com>2020-03-31 23:23:06 +0300
committerGitHub <noreply@github.com>2020-03-31 23:23:06 +0300
commitf01b4e54a4b46a84a5a79b03bd2bd2f21b190f12 (patch)
tree29076bc7f22139c820caff12ff3a3155493eb1e5
parent642181c76b11bd1cc87ee7443d3ee215744df0fd (diff)
parent8235553ec3e5b6ec64c0e0146849498aed49bbaa (diff)
downloadnumpy-f01b4e54a4b46a84a5a79b03bd2bd2f21b190f12.tar.gz
Merge pull request #15881 from eric-wieser/empty_like-0d
BUG: Fix empty_like to respect shape=()
-rw-r--r--numpy/core/src/multiarray/ctors.c8
-rw-r--r--numpy/core/src/multiarray/multiarraymodule.c5
-rw-r--r--numpy/core/tests/test_numeric.py2
3 files changed, 8 insertions, 7 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index a23510c8b..12bf9eace 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -1211,8 +1211,8 @@ PyArray_NewFromDescrAndBase(
* NPY_ANYORDER - Fortran if prototype is Fortran, C otherwise.
* NPY_KEEPORDER - Keeps the axis ordering of prototype.
* dtype - If not NULL, overrides the data type of the result.
- * ndim - If not 0 and dims not NULL, overrides the shape of the result.
- * dims - If not NULL and ndim not 0, overrides the shape of the result.
+ * ndim - If not -1, overrides the shape of the result.
+ * dims - If ndim is not -1, overrides the shape of the result.
* subok - If 1, use the prototype's array subtype, otherwise
* always create a base-class array.
*
@@ -1225,7 +1225,7 @@ PyArray_NewLikeArrayWithShape(PyArrayObject *prototype, NPY_ORDER order,
{
PyObject *ret = NULL;
- if (dims == NULL) {
+ if (ndim == -1) {
ndim = PyArray_NDIM(prototype);
dims = PyArray_DIMS(prototype);
}
@@ -1322,7 +1322,7 @@ NPY_NO_EXPORT PyObject *
PyArray_NewLikeArray(PyArrayObject *prototype, NPY_ORDER order,
PyArray_Descr *dtype, int subok)
{
- return PyArray_NewLikeArrayWithShape(prototype, order, dtype, 0, NULL, subok);
+ return PyArray_NewLikeArrayWithShape(prototype, order, dtype, -1, NULL, subok);
}
/*NUMPY_API
diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c
index 2856a123f..9e8022abd 100644
--- a/numpy/core/src/multiarray/multiarraymodule.c
+++ b/numpy/core/src/multiarray/multiarraymodule.c
@@ -1857,14 +1857,15 @@ array_empty_like(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *kwds)
NPY_ORDER order = NPY_KEEPORDER;
PyArrayObject *ret = NULL;
int subok = 1;
- PyArray_Dims shape = {NULL, 0};
+ /* -1 is a special value meaning "not specified" */
+ PyArray_Dims shape = {NULL, -1};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|O&O&iO&:empty_like", kwlist,
&PyArray_Converter, &prototype,
&PyArray_DescrConverter2, &dtype,
&PyArray_OrderConverter, &order,
&subok,
- &PyArray_IntpConverter, &shape)) {
+ &PyArray_OptionalIntpConverter, &shape)) {
goto fail;
}
/* steals the reference to dtype if it's not NULL */
diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py
index 05f59d9dc..dfb2d81fe 100644
--- a/numpy/core/tests/test_numeric.py
+++ b/numpy/core/tests/test_numeric.py
@@ -2457,7 +2457,7 @@ class TestLikeFuncs:
(np.arange(24).reshape(2, 3, 4).swapaxes(0, 1), None),
(np.arange(24).reshape(4, 3, 2).swapaxes(0, 1), '?'),
]
- self.shapes = [(5,), (5,6,), (5,6,7,)]
+ self.shapes = [(), (5,), (5,6,), (5,6,7,)]
def compare_array_value(self, dz, value, fill_value):
if value is not None: