Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pcmoritz committed Aug 18, 2017
1 parent f25f3f3 commit 4cc45cd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
9 changes: 8 additions & 1 deletion cpp/src/arrow/python/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ class ARROW_EXPORT OwnedRef {
PyObject* obj_;
};

// This assumes that the GIL is held by the caller
// This is different from OwnedRef in that it assumes that
// the GIL is held by the caller and doesn't decrement the
// reference count when release is called.
class ARROW_EXPORT ScopedRef {
public:
ScopedRef() : obj_(nullptr) {}
Expand All @@ -102,6 +104,11 @@ class ARROW_EXPORT ScopedRef {
Py_XDECREF(obj_);
}

void reset(PyObject* obj) {
Py_XDECREF(obj_);
obj_ = obj;
}

PyObject* release() {
PyObject* result = obj_;
obj_ = nullptr;
Expand Down
15 changes: 4 additions & 11 deletions cpp/src/arrow/python/python_to_arrow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,12 @@ Status SerializeSequences(std::vector<PyObject*> sequences, int32_t recursion_de
SequenceBuilder builder(nullptr);
std::vector<PyObject *> sublists, subtuples, subdicts;
for (const auto& sequence : sequences) {
PyObject* item;
PyObject* iterator = PyObject_GetIter(sequence);
ScopedRef iterator(PyObject_GetIter(sequence));
RETURN_IF_PYERROR();
while ((item = PyIter_Next(iterator))) {
Status s = Append(item, &builder, &sublists, &subtuples, &subdicts, tensors_out);
Py_DECREF(item);
// if an error occurs, we need to decrement the reference counts before returning
if (!s.ok()) {
Py_DECREF(iterator);
return s;
}
ScopedRef item;
while (item.reset(PyIter_Next(iterator.get())), item.get()) {
RETURN_NOT_OK(Append(item.get(), &builder, &sublists, &subtuples, &subdicts, tensors_out));
}
Py_DECREF(iterator);
}
std::shared_ptr<Array> list;
if (sublists.size() > 0) {
Expand Down

0 comments on commit 4cc45cd

Please sign in to comment.