pitrou commented on code in PR #50409:
URL: https://github.com/apache/arrow/pull/50409#discussion_r3628275941


##########
python/pyarrow/src/arrow/python/helpers.cc:
##########
@@ -89,15 +89,16 @@ Result<uint16_t> PyFloat_AsHalf(PyObject* obj) {
     return PyArrayScalar_VAL(obj, Half);
   } else {
     return Status::TypeError("conversion to float16 expects a `float` or ",
-                             "`np.float16` object, got ", 
Py_TYPE(obj)->tp_name);
+                             "`np.float16` object, got ",
+                             internal::PyObject_StdStringTypeName(obj));
   }
 }
 
 namespace internal {
 
 std::string PyBytes_AsStdString(PyObject* obj) {
   ARROW_DCHECK(PyBytes_Check(obj));
-  return std::string(PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj));
+  return std::string(PyBytes_AsString(obj), PyBytes_Size(obj));

Review Comment:
   Can use `PyBytes_AsStringAndSize` as below.



##########
python/pyarrow/src/arrow/python/helpers.cc:
##########
@@ -121,12 +122,27 @@ std::string PyObject_StdStringRepr(PyObject* obj) {
   if (!bytes_ref) {
     PyErr_Clear();
     std::stringstream ss;
-    ss << "<object of type '" << Py_TYPE(obj)->tp_name << "' repr() failed>";
+    ss << "<object of type '" << PyObject_StdStringTypeName(obj) << "' repr() 
failed>";
     return ss.str();
   }
   return PyBytes_AsStdString(bytes_ref.obj());
 }
 
+std::string PyObject_StdStringTypeName(PyObject* obj) {
+  OwnedRef name_ref(PyType_GetName(Py_TYPE(obj)));
+  if (!name_ref) {
+    PyErr_Clear();
+    return "?";
+  }
+  Py_ssize_t size;
+  const char* data = PyUnicode_AsUTF8AndSize(name_ref.obj(), &size);

Review Comment:
   Why not use `PyUnicode_AsStdString`?



##########
python/pyarrow/src/arrow/python/benchmark.cc:
##########


Review Comment:
   It's not yet removed from the PR apparently :)



##########
python/pyarrow/src/arrow/python/numpy_to_arrow.cc:
##########
@@ -690,9 +690,9 @@ 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()));
+  const int32_t length = static_cast<int32_t>(PyBytes_Size(utf8_obj.obj()));

Review Comment:
   As an alternative, we can add `PyBytes_AsStdStringView` that would return a 
`std::string_view` of a PyBytes object, and use it here. 



-- 
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