Skip to content

Commit

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

// This assumes that the GIL is held by the caller
class ARROW_EXPORT ScopedRef {
public:
ScopedRef() : obj_(nullptr) {}

explicit ScopedRef(PyObject* obj) : obj_(obj) {}

~ScopedRef() {
PyAcquireGIL lock;
Py_XDECREF(obj_);
}

Expand Down
24 changes: 9 additions & 15 deletions cpp/src/arrow/python/python_to_arrow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,15 @@ Status CallCustomCallback(PyObject* callback, PyObject* elem, PyObject** result)
*result = NULL;
if (!callback) {
std::stringstream ss;
PyObject* repr = PyObject_Repr(elem);
ScopedRef repr(PyObject_Repr(elem));
RETURN_IF_PYERROR();
PyObject* ascii = PyUnicode_AsASCIIString(repr);
ss << "error while calling callback on " << PyBytes_AsString(ascii)
ScopedRef ascii(PyUnicode_AsASCIIString(repr.get()));
ss << "error while calling callback on " << PyBytes_AsString(ascii.get())
<< ": handler not registered";
Py_XDECREF(ascii);
Py_XDECREF(repr);
return Status::NotImplemented(ss.str());
} else {
PyObject* arglist = Py_BuildValue("(O)", elem);
*result = PyObject_CallObject(callback, arglist);
Py_XDECREF(arglist);
ScopedRef arglist(Py_BuildValue("(O)", elem));
*result = PyObject_CallObject(callback, arglist.get());
RETURN_IF_PYERROR();
}
return Status::OK();
Expand Down Expand Up @@ -211,15 +208,12 @@ Status Append(PyObject* elem, SequenceBuilder* builder, std::vector<PyObject*>*
Py_ssize_t size;
#if PY_MAJOR_VERSION >= 3
char* data = PyUnicode_AsUTF8AndSize(elem, &size);
Status s = builder->AppendString(data, size);
#else
PyObject* str = PyUnicode_AsUTF8String(elem);
char* data = PyString_AS_STRING(str);
size = PyString_GET_SIZE(str);
Status s = builder->AppendString(data, size);
Py_XDECREF(str);
ScopedRef str(PyUnicode_AsUTF8String(elem));
char* data = PyString_AS_STRING(str.get());
size = PyString_GET_SIZE(str.get());
#endif
RETURN_NOT_OK(s);
RETURN_NOT_OK(builder->AppendString(data, size));
} else if (PyList_Check(elem)) {
RETURN_NOT_OK(builder->AppendList(PyList_Size(elem)));
sublists->push_back(elem);
Expand Down

0 comments on commit f25f3f3

Please sign in to comment.