8000 ENH: Add support for dict view objects. by hypercubestart · Pull Request #14539 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add support for dict view objects. #14539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion numpy/core/src/multiarray/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ PyArray_DTypeFromObjectHelper(PyObject *obj, int maxdims,
* be treated as objects, and they expect numpy to treat it as an object if
* __len__ is not defined.
*/
if (maxdims == 0 || !PySequence_Check(obj) || PySequence_Size(obj) < 0) {
if (maxdims == 0 || !PyArray_SequenceOrMappingViewCheck(obj) || PySequence_Size(obj) < 0) {
/* clear any PySequence_Size error which corrupts further calls */
PyErr_Clear();

Expand Down
29 changes: 29 additions & 0 deletions numpy/core/src/multiarray/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,35 @@ check_and_adjust_index(npy_intp *index, npy_intp max_item, int axis,
return 0;
}

/*
* Returns 1 if *obj is array-like object, otherwise returns 0
*/
static NPY_INLINE int
PyArray_SequenceOrMappingViewCheck(PyObject *obj) {
/* Check is *obj is sequence*/
if (PySequence_Check(obj)) {
return 1;
}

/* Check is *obj inherits collections.abc.MappingView */
static PyObject *mappingview = NULL;

if (mappingview == NULL) {
PyObject *mod = PyImport_ImportModule("collections.abc");

if (mod != NULL) {
mappingview = PyObject_GetAttrString(mod, "MappingView");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to do this, we should use Collection

Py_DECREF(mod);
}
}

if (PyObject_IsInstance(obj, mappingview) == 1) {
return 1;
}

return 0;
}

/*
* Returns -1 and sets an exception if *axis is an invalid axis for
* an array of dimension ndim, otherwise adjusts it in place to be
Expand Down
4 changes: 2 additions & 2 deletions numpy/core/src/multiarray/compiled_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ astype_anyint(PyObject *obj) {
return NULL;
}
if (dtype_guess == NULL) {
if (PySequence_Check(obj) && PySequence_Size(obj) == 0) {
if (PyArray_SequenceOrMappingViewCheck(obj) && PySequence_Size(obj) == 0) {
PyErr_SetString(PyExc_TypeError, EMPTY_SEQUENCE_ERR_MSG);
}
return NULL;
Expand Down Expand Up @@ -901,7 +901,7 @@ static int int_sequence_to_arrays(PyObject *seq,
{
int i;

if (!PySequence_Check(seq) || PySequence_Size(seq) != count) {
if (!PyArray_SequenceOrMappingViewCheck(seq) || PySequence_Size(seq) != count) {
PyErr_Format(PyExc_ValueError,
"parameter %s must be a sequence of length %d",
paramname, count);
Expand Down
6 changes: 3 additions & 3 deletions numpy/core/src/multiarray/ctors.c
Original file line number Diff line number Diff line change
8000 Expand Up @@ -624,7 +624,7 @@ setArrayFromSequence(PyArrayObject *a, PyObject *s,
NPY_NO_EXPORT int
PyArray_AssignFromSequence(PyArrayObject *self, PyObject *v)
{
if (!PySequence_Check(v)) {
if (!PyArray_SequenceOrMappingViewCheck(v)) {
PyErr_SetString(PyExc_ValueError,
"assignment from non-sequence");
return -1;
Expand Down Expand Up @@ -752,7 +752,7 @@ discover_dimensions(PyObject *obj, int *maxndim, npy_intp *d, int check_it,
}

/* obj is not a Sequence */
if (!PySequence_Check(obj) ||
if (!PyArray_SequenceOrMappingViewCheck(obj) ||
PySequence_Length(obj) < 0) {
*maxndim = 0;
PyErr_Clear();
Expand Down Expand Up @@ -1801,7 +1801,7 @@ PyArray_GetArrayParamsFromObject(PyObject *op,
}

/* Try to treat op as a list of lists or array-like objects. */
if (!writeable && PySequence_Check(op)) {
if (!writeable && PyArray_SequenceOrMappingViewCheck(op)) {
int check_it, stop_at_string, stop_at_tuple, is_object;
int type_num, type;

Expand Down
16 changes: 16 additions & 0 deletions numpy/core/tests/test_multiarray.py
9463
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,22 @@ def test_jagged_shape_object(self):
assert_equal(a.shape, (3,))
assert_equal(a.dtype, object)

def test_mapping_view(self):
# Test correct array creation from dict subtypes
d = {0: 1, 2: 3, 4: 5}

a = np.array(d.items())
assert_equal(a.shape, (3,2))
# dtype inferred differently on Windows/Linux/Mac
assert_equal((a.dtype == np.int32 or a.dtype == np.int64), True)

a = np.array(d.keys())
assert_equal(a.shape, (3,))
assert_equal((a.dtype == np.int32 or a.dtype == np.int64), True)

a = np.array(d.values())
assert_equal(a.shape, (3,))
assert_equal((a.dtype == np.int32 or a.dtype == np.int64), True)

class TestStructured(object):
def test_subarray_field_access(self):
Expand Down
0