[ 
https://issues.apache.org/jira/browse/ARROW-2033?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16344146#comment-16344146
 ] 

ASF GitHub Bot commented on ARROW-2033:
---------------------------------------

wesm commented on a change in pull request #1513: ARROW-2033: [Python] Fix 
pa.array() with iterator input
URL: https://github.com/apache/arrow/pull/1513#discussion_r164588591
 
 

 ##########
 File path: cpp/src/arrow/python/builtin_convert.cc
 ##########
 @@ -285,25 +273,44 @@ class SeqVisitor {
   }
 };
 
-Status InferArrowSize(PyObject* obj, int64_t* size) {
+// Convert *obj* to a sequence if necessary
+// Fill *size* to its length.  If >= 0 on entry, *size* is an upper size
+// bound that may lead to truncation.
+Status ConvertToSequenceAndInferSize(PyObject* obj, PyObject** seq, int64_t* 
size) {
   if (PySequence_Check(obj)) {
-    *size = static_cast<int64_t>(PySequence_Size(obj));
-  } else if (PyObject_HasAttrString(obj, "__iter__")) {
+    // obj is already a sequence
+    int64_t real_size = static_cast<int64_t>(PySequence_Size(obj));
+    if (*size < 0)
+      *size = real_size;
+    else
+      *size = std::min(real_size, *size);
+    Py_INCREF(obj);
+    *seq = obj;
+  } else if (*size < 0) {
+    // unknown size, exhaust iterator
+    *seq = PySequence_List(obj);
+    RETURN_IF_PYERROR();
+    *size = static_cast<int64_t>(PyList_GET_SIZE(*seq));
+  } else {
+    // size is known but iterator could be infinite
+    Py_ssize_t i, n = *size;
     PyObject* iter = PyObject_GetIter(obj);
+    RETURN_IF_PYERROR();
     OwnedRef iter_ref(iter);
-    *size = 0;
-    PyObject* item;
-    while ((item = PyIter_Next(iter))) {
-      OwnedRef item_ref(item);
-      *size += 1;
+    PyObject* lst = PyList_New(n);
+    RETURN_IF_PYERROR();
+    for (i = 0; i < n; i++) {
+      PyObject* item = PyIter_Next(iter);
+      if (!item) break;
+      PyList_SET_ITEM(lst, i, item);
     }
-  } else {
-    return Status::TypeError("Object is not a sequence or iterable");
-  }
-  if (PyErr_Occurred()) {
-    // Not a sequence
-    PyErr_Clear();
-    return Status::TypeError("Object is not a sequence or iterable");
+    // Shrink list if len(iterator) < size
+    if (i < n && PyList_SetSlice(lst, i, n, NULL)) {
+      Py_DECREF(lst);
+      return Status::UnknownError("failed to resize list");
+    }
+    *seq = lst;
+    *size = std::min(static_cast<int64_t>(i), *size);
 
 Review comment:
   NB. you can also indicate what `std::min` you want, e.g. `std::min<int64_t>`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> pa.array() doesn't work with iterators
> --------------------------------------
>
>                 Key: ARROW-2033
>                 URL: https://issues.apache.org/jira/browse/ARROW-2033
>             Project: Apache Arrow
>          Issue Type: Bug
>          Components: Python
>    Affects Versions: 0.8.0
>            Reporter: Antoine Pitrou
>            Assignee: Antoine Pitrou
>            Priority: Minor
>              Labels: pull-request-available
>             Fix For: 0.9.0
>
>
> pa.array handles iterables fine, but not iterators if size isn't passed:
> {code:java}
> >>> arr = pa.array(range(5))
> >>> arr
> <pyarrow.lib.Int64Array object at 0x7f4652a05318>
> [
>   0,
>   1,
>   2,
>   3,
>   4
> ]
> >>> arr = pa.array(iter(range(5)))
> >>> arr
> <pyarrow.lib.NullArray object at 0x7f4633c1d638>
> [
>   NA,
>   NA,
>   NA,
>   NA,
>   NA
> ]
> {code}
> This is because InferArrowSize() first exhausts the iterator.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to