WillAyd commented on issue #597:
URL: 
https://github.com/apache/arrow-nanoarrow/issues/597#issuecomment-2313017661

   In your mind are you thinking of creating these in plain C just to increase 
their use outside of nanoarrow? The challenge I think with plain C extensions 
is getting reference counting correct, especially with branches that may return 
early. I think an equivalent of the above code in C would look like:
   
   ```c
   static int AppendBytes(const CArrayBuilder *builder, PyObject *obj) {
       ArrowErrorCode code;
       ArrowBufferView item
   
       if (!PyIter_Check(obj)) {
         PyErr_SetString(PyExc_TypeError, "Object is not iterable");
         return -1;
       }
   
       PyObject *iterator = PyObject_GetIter(obj);
       if (iterator == NULL)
         return NULL;
   
       PyObject *item;
       while ((item = PyIter_Next(iterator))) {
         if (item == Py_None) {
           code = ArrowArrayAppendNull(builder->_ptr, 1);
         } else {
           Py_buffer buffer;
           if (PyObject_GetBuffer(py_item, &buffer, PyBUF_ANY_CONTIGUOUS | 
PyBUF_FORMAT)) {
             Py_DECREF(item);
             Py_DECREF(iterator);
             return NULL;
           }
   
           if (buffer.ndim != 1) {
             PyErr_SetString(PyExc_ValueError, "Can't append buffer with 
dimensions != 1 to binary array");
             // TODO: current implementation might be missing a release?
             // PyBuffer_Release(&buffer);  
             Py_DECREF(item);
             Py_DECREF(iterator);
             return NULL;
           }
   
           if (buffer.itemsize != 1) {
             PyErr_SetString(PyExc_ValueError, "Can't append buffer with 
itemsize != 1 to binary array");
             PyBuffer_Release(&buffer);
             Py_DECREF(item);
             Py_DECREF(iterator);
             return NULL;          
           }
           
           item.data.data = buffer.buf;
           item.size_bytes = buffer.len;
           code = ArrowArrayAppendBytes(ptr_, item);
           PyBuffer_Release(&buffer);
         }
   
         if (code != NANOARROW_OK) {
           // TODO: might be a format specifier that doesn't require 
PyObject_Str
           PyObject *str = PyObject_Str(item);
           // TODO: might need to create a custom exception        
           PyErr_Format(PyExc_ValueError, "append bytes item %s failed %d", 
str, code);
           Py_DECREF(str);
           Py_DECREF(item);
           Py_DECREF(iterator);
           return -1;
         }
         Py_DECREF(item);
       }
   
       Py_DECREF(iterator);
       return 0;
   }
   ```
   
   of course code use goto cleverly to better simulate an RAII cleanup of 
resources, but again wanted to keep this as equivalent to current code as 
possible


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to