-
Notifications
You must be signed in to change notification settings - Fork 4.2k
GH-50398 [Python] Use more CPython Limited APIs in python/pyarrow #50409
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
base: main
Are you sure you want to change the base?
Changes from all commits
d9cbc05
a8c248b
f308f1c
9921f67
bab582f
b4db884
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| #include <utility> | ||
|
|
||
| #include "arrow/buffer.h" | ||
| #include "arrow/python/helpers.h" | ||
| #include "arrow/python/pyarrow.h" | ||
| #include "arrow/python/visibility.h" | ||
| #include "arrow/result.h" | ||
|
|
@@ -398,12 +399,16 @@ struct PyBytesView { | |
| // View the given Python object as binary-like, i.e. bytes | ||
| Status ParseBinary(PyObject* obj) { | ||
| if (PyBytes_Check(obj)) { | ||
| bytes = PyBytes_AS_STRING(obj); | ||
| size = PyBytes_GET_SIZE(obj); | ||
| bytes = PyBytes_AsString(obj); | ||
| RETURN_IF_PYERROR(); | ||
| size = PyBytes_Size(obj); | ||
|
Comment on lines
+402
to
+404
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to check for errors here, at least as a debug assertion (since those functions shouldn't fail on a bytes object).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For all these error checking, an agent picked up on using |
||
| RETURN_IF_PYERROR(); | ||
| is_utf8 = false; | ||
| } else if (PyByteArray_Check(obj)) { | ||
| bytes = PyByteArray_AS_STRING(obj); | ||
| size = PyByteArray_GET_SIZE(obj); | ||
| bytes = PyByteArray_AsString(obj); | ||
| RETURN_IF_PYERROR(); | ||
| size = PyByteArray_Size(obj); | ||
| RETURN_IF_PYERROR(); | ||
| is_utf8 = false; | ||
| } else if (PyMemoryView_Check(obj)) { | ||
| PyObject* ref = PyMemoryView_GetContiguous(obj, PyBUF_READ, 'C'); | ||
|
|
@@ -413,8 +418,8 @@ struct PyBytesView { | |
| size = buffer->len; | ||
| is_utf8 = false; | ||
| } else { | ||
| return Status::TypeError("Expected bytes, got a '", Py_TYPE(obj)->tp_name, | ||
| "' object"); | ||
| return Status::TypeError("Expected bytes, got a '", | ||
| internal::PyObject_StdStringTypeName(obj), "' object"); | ||
| } | ||
| return Status::OK(); | ||
| } | ||
|
|
@@ -425,10 +430,13 @@ struct PyBytesView { | |
| RETURN_IF_PYERROR(); | ||
| if (!PyBytes_Check(ref.obj())) { | ||
| return Status::TypeError("Expected uuid.UUID.bytes to return bytes, got '", | ||
| Py_TYPE(ref.obj())->tp_name, "' object"); | ||
| internal::PyObject_StdStringTypeName(ref.obj()), | ||
| "' object"); | ||
| } | ||
| bytes = PyBytes_AS_STRING(ref.obj()); | ||
| size = PyBytes_GET_SIZE(ref.obj()); | ||
| bytes = PyBytes_AsString(ref.obj()); | ||
| RETURN_IF_PYERROR(); | ||
| size = PyBytes_Size(ref.obj()); | ||
| RETURN_IF_PYERROR(); | ||
| is_utf8 = false; | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -690,9 +690,11 @@ Status AppendUTF32(const char* data, int64_t itemsize, int byteorder, T* builder | |
| return Status::Invalid("failed converting UTF32 to UTF8"); | ||
| } | ||
|
|
||
| const int32_t length = static_cast<int32_t>(PyBytes_GET_SIZE(utf8_obj.obj())); | ||
| return builder->Append( | ||
| reinterpret_cast<const uint8_t*>(PyBytes_AS_STRING(utf8_obj.obj())), length); | ||
| const int32_t length = static_cast<int32_t>(PyBytes_Size(utf8_obj.obj())); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to check for errors. |
||
| RETURN_IF_PYERROR(); | ||
| const char* utf8_data = PyBytes_AsString(utf8_obj.obj()); | ||
| RETURN_IF_PYERROR(); | ||
| return builder->Append(reinterpret_cast<const uint8_t*>(utf8_data), length); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
@@ -836,9 +838,12 @@ Status NumPyConverter::Visit(const StructType& type) { | |
| return Status::Invalid("Missing field '", field->name(), "' in struct array"); | ||
| } | ||
| PyArray_Descr* sub_dtype = | ||
| reinterpret_cast<PyArray_Descr*>(PyTuple_GET_ITEM(tup, 0)); | ||
| reinterpret_cast<PyArray_Descr*>(PyTuple_GetItem(tup, 0)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to check for errors. |
||
| RETURN_IF_PYERROR(); | ||
| ARROW_DCHECK(PyObject_TypeCheck(sub_dtype, &PyArrayDescr_Type)); | ||
| int offset = static_cast<int>(PyLong_AsLong(PyTuple_GET_ITEM(tup, 1))); | ||
| PyObject* offset_obj = PyTuple_GetItem(tup, 1); | ||
| RETURN_IF_PYERROR(); | ||
| int offset = static_cast<int>(PyLong_AsLong(offset_obj)); | ||
| RETURN_IF_PYERROR(); | ||
| Py_INCREF(sub_dtype); /* PyArray_GetField() steals ref */ | ||
| PyObject* sub_array = PyArray_GetField(arr_, sub_dtype, offset); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -255,7 +255,7 @@ class PyValue { | |
| static Result<double> Convert(const DoubleType*, const O&, I obj) { | ||
| double value; | ||
| if (PyFloat_Check(obj)) { | ||
| value = PyFloat_AS_DOUBLE(obj); | ||
| value = PyFloat_AsDouble(obj); | ||
| } else if (internal::PyFloatScalar_Check(obj)) { | ||
| // Other kinds of float-y things | ||
| value = PyFloat_AsDouble(obj); | ||
|
|
@@ -445,12 +445,11 @@ class PyValue { | |
| return output; | ||
| } | ||
| if (PyTuple_Check(obj) && PyTuple_Size(obj) == 3) { | ||
| RETURN_NOT_OK(internal::CIntFromPython(PyTuple_GET_ITEM(obj, 0), &output.months, | ||
| RETURN_NOT_OK(internal::CIntFromPython(PyTuple_GetItem(obj, 0), &output.months, | ||
| "Months (tuple item #0) too large")); | ||
| RETURN_NOT_OK(internal::CIntFromPython(PyTuple_GET_ITEM(obj, 1), &output.days, | ||
| RETURN_NOT_OK(internal::CIntFromPython(PyTuple_GetItem(obj, 1), &output.days, | ||
| "Days (tuple item #1) too large")); | ||
| RETURN_NOT_OK(internal::CIntFromPython(PyTuple_GET_ITEM(obj, 2), | ||
| &output.nanoseconds, | ||
| RETURN_NOT_OK(internal::CIntFromPython(PyTuple_GetItem(obj, 2), &output.nanoseconds, | ||
| "Nanoseconds (tuple item #2) too large")); | ||
| return output; | ||
| } | ||
|
|
@@ -1050,8 +1049,8 @@ class PyStructConverter : public StructConverter<PyConverter, PyConverterTrait> | |
| PyObject* unicode = | ||
| PyUnicode_FromStringAndSize(field_name.c_str(), field_name.size()); | ||
| RETURN_IF_PYERROR(); | ||
| PyList_SET_ITEM(bytes_field_names_.obj(), i, bytes); | ||
| PyList_SET_ITEM(unicode_field_names_.obj(), i, unicode); | ||
| PyList_SetItem(bytes_field_names_.obj(), i, bytes); | ||
| PyList_SetItem(unicode_field_names_.obj(), i, unicode); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to assert for errors.
Comment on lines
+1052
to
+1053
|
||
| } | ||
| return Status::OK(); | ||
| } | ||
|
|
@@ -1111,7 +1110,7 @@ class PyStructConverter : public StructConverter<PyConverter, PyConverterTrait> | |
| return Status::Invalid("Tuple size must be equal to number of struct fields"); | ||
| } | ||
| for (int i = 0; i < num_fields_; i++) { | ||
| PyObject* value = PyTuple_GET_ITEM(tuple, i); | ||
| PyObject* value = PyTuple_GetItem(tuple, i); | ||
| RETURN_NOT_OK(this->children_[i]->Append(value)); | ||
| } | ||
| return Status::OK(); | ||
|
|
@@ -1266,7 +1265,7 @@ Status ConvertToSequenceAndInferSize(PyObject* obj, PyObject** seq, int64_t* siz | |
| RETURN_IF_PYERROR(); | ||
| break; | ||
| } | ||
| PyList_SET_ITEM(lst, i, item); | ||
| PyList_SetItem(lst, i, item); | ||
|
|
||
| } | ||
| // Shrink list if len(iterator) < size | ||
| if (i < n && PyList_SetSlice(lst, i, n, NULL)) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably remove this file instead. Nobody ever runs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. Removed.