8000 ENH: adds support for array construction from iterators by behzadnouri · Pull Request #5863 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: adds support for array construction from iterators #5863

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 1 commit 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

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions numpy/core/src/multiarray/ctors.c
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,34 @@ PyArray_GetArrayParamsFromObject(PyObject *op,

/* Anything can be viewed as an object, unless it needs to be writeable */
if (!writeable) {
/*
* PyIter_Check returns false positive for python 2 old-style
* instances;
*/
#if defined(NPY_PY3K)
if (PyIter_Check(op)) {
#else
if (PyIter_Check(op) && !PyInstance_Check(op)) {
#endif
/* PyArray_FromIter needs type & does not like 'object' type */
if (requested_dtype != NULL &&
!PyDataType_REFCHK(requested_dtype)) {
Py_INCREF(requested_dtype);
*out_arr = (PyArrayObject *)
PyArray_FromIter(op, requested_dtype, -1);
}
else {
PyObject *list = PySequence_List(op);
if (list == NULL) {
return -1;
}
Py_XINCREF(requested_dtype);
*out_arr = (PyArrayObject *) PyArray_FromAny(list,
requested_dtype, 0, 0, NPY_ARRAY_DEFAULT, context);
Py_DECREF(list);
}
return (*out_arr) == NULL ? -1 : 0;
}
*out_dtype = PyArray_DescrFromType(NPY_OBJECT);
if (*out_dtype == NULL) {
return -1;
Expand Down
12 changes: 12 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import builtins
else:
import __builtin__ as builtins
range = xrange
from decimal import Decimal


Expand Down Expand Up @@ -304,6 +305,17 @@ def test_array_cont(self):
assert_(np.ascontiguousarray(d).flags.c_contiguous)
assert_(np.asfortranarray(d).flags.f_contiguous)

def test_array_iter(self):
assert_array_equal(np.array(x for x in range(3)), [0, 1, 2])

arr = np.array(map(np.sqrt, [0, 1, 4]), dtype='float')
assert_array_equal(arr, [0, 1, 2])

arr = np.array((x for x in [0, 'abc', 2.7]), dtype='object')
assert_array_equal(arr, np.array([0, 'abc', 2.7], dtype='object'))

arr = np.array(x for x in [range(2), range(2, 4)])
assert_array_equal(arr, [[0, 1], [2, 3]])

class TestAssignment(TestCase):
def test_assignment_broadcasting(self):
Expand Down
0