Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba52e16

Browse files
committed
allow assignment with multiple same indices as susbscripting. Limit to 1 ellipsis object for 0d array subscripting.
1 parent 04d7338 commit ba52e16

2 files changed

Lines changed: 37 additions & 35 deletions

File tree

THANKS.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ Travis Vaught and Joe Cooper for administration of numpy.org web site and SVN
1414
Eric Firing for bugfixes.
1515
Arnd Baecker for 64-bit testing
1616
David Cooke for many code improvements including the auto-generated C-API
17-
Alexander Belopolsky (sasha) for Masked array bug-fixes and tests...
17+
Alexander Belopolsky (sasha) for Masked array bug-fixes and tests and rank-0 array improvements

numpy/core/src/arrayobject.c

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,42 +1742,25 @@ PyArray_SetMap(PyArrayMapIterObject *mit, PyObject *op)
17421742
return 0;
17431743
}
17441744

1745-
/* Called when treating array object like a mapping -- called first from
1746-
Python when using a[object] unless object is a standard slice object
1747-
(not an extended one).
1748-
1749-
*/
1750-
1751-
/* There are two situations:
1752-
1753-
1 - the subscript is a standard view and a reference to the
1754-
array can be returned
1755-
1756-
2 - the subscript uses Boolean masks or integer indexing and
1757-
therefore a new array is created and returned.
1758-
1759-
*/
1760-
1761-
/* Always returns arrays */
1762-
1763-
static PyObject *iter_subscript(PyArrayIterObject *, PyObject *);
1764-
17651745
int
17661746
count_new_axes_0d(PyObject *tuple)
17671747
{
17681748
int i, argument_count;
17691749
int ellipsis_count = 0;
17701750
int newaxis_count = 0;
1751+
17711752
argument_count = PyTuple_GET_SIZE(tuple);
1753+
17721754
for (i = 0; i < argument_count; ++i) {
17731755
PyObject *arg = PyTuple_GET_ITEM(tuple, i);
1774-
ellipsis_count += (arg == Py_Ellipsis);
1775-
newaxis_count += (arg == Py_None);
1756+
if (arg == Py_Ellipsis && !ellipsis_count) ellipsis_count++;
1757+
else if (arg == Py_None) newaxis_count++;
1758+
else break;
17761759
}
1777-
if (newaxis_count + ellipsis_count != argument_count) {
1760+
if (i < argument_count) {
17781761
PyErr_SetString(PyExc_IndexError,
17791762
"0-d arrays can only use a single ()"
1780-
" or a list of ellipses and newaxes"
1763+
" or a list of newaxes (and a single ...)"
17811764
" as an index");
17821765
return -1;
17831766
}
@@ -1787,34 +1770,51 @@ count_new_axes_0d(PyObject *tuple)
17871770
return -1;
17881771
}
17891772
return newaxis_count;
1790-
17911773
}
1774+
17921775
static PyObject *
17931776
add_new_axes_0d(PyArrayObject *arr, int newaxis_count)
17941777
{
17951778
PyArrayObject *other;
1796-
intp dimensions[MAX_DIMS], strides[MAX_DIMS];
1779+
intp dimensions[MAX_DIMS];
17971780
int i;
17981781
for (i = 0; i < newaxis_count; ++i) {
1799-
dimensions[i] = strides[i] = 1;
1782+
dimensions[i] = 1;
18001783
}
18011784
Py_INCREF(arr->descr);
18021785
if ((other = (PyArrayObject *)
18031786
PyArray_NewFromDescr(arr->ob_type, arr->descr,
18041787
newaxis_count, dimensions,
1805-
strides, arr->data,
1788+
NULL, arr->data,
18061789
arr->flags,
18071790
(PyObject *)arr)) == NULL)
18081791
return NULL;
1809-
18101792
other->base = (PyObject *)arr;
18111793
Py_INCREF(arr);
1812-
1813-
other ->flags &= ~OWNDATA;
1814-
18151794
return (PyObject *)other;
18161795
}
18171796

1797+
/* Called when treating array object like a mapping -- called first from
1798+
Python when using a[object] unless object is a standard slice object
1799+
(not an extended one).
1800+
1801+
*/
1802+
1803+
/* There are two situations:
1804+
1805+
1 - the subscript is a standard view and a reference to the
1806+
array can be returned
1807+
1808+
2 - the subscript uses Boolean masks or integer indexing and
1809+
therefore a new array is created and returned.
1810+
1811+
*/
1812+
1813+
/* Always returns arrays */
1814+
1815+
static PyObject *iter_subscript(PyArrayIterObject *, PyObject *);
1816+
1817+
18181818
static PyObject *
18191819
array_subscript(PyArrayObject *self, PyObject *op)
18201820
{
@@ -1999,8 +1999,9 @@ array_ass_sub(PyArrayObject *self, PyObject *index, PyObject *op)
19991999
}
20002000

20012001
if (self->nd == 0) {
2002-
if (index == Py_Ellipsis || (PyTuple_Check(index) && \
2003-
0 == PyTuple_GET_SIZE(index)))
2002+
if (index == Py_Ellipsis || index == Py_None || \
2003+
(PyTuple_Check(index) && (0 == PyTuple_GET_SIZE(index) || \
2004+
count_new_axes_0d(index) > 0)))
20042005
return self->descr->f->setitem(op, self->data, self);
20052006
PyErr_SetString(PyExc_IndexError,
20062007
"0-d arrays can't be indexed.");
@@ -2049,6 +2050,7 @@ array_ass_sub(PyArrayObject *self, PyObject *index, PyObject *op)
20492050
return ret;
20502051
}
20512052

2053+
20522054
/* There are places that require that array_subscript return a PyArrayObject
20532055
and not possibly a scalar. Thus, this is the function exposed to
20542056
Python so that 0-dim arrays are passed as scalars

0 commit comments

Comments
 (0)