arrow-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From w...@apache.org
Subject arrow git commit: ARROW-1125: Python: Add public C++ API to unwrap PyArrow object
Date Tue, 11 Jul 2017 02:28:01 GMT
Repository: arrow
Updated Branches:
  refs/heads/master 471a85fd7 -> ad57ea8ec


ARROW-1125: Python: Add public C++ API to unwrap PyArrow object

Author: Uwe L. Korn <uwelk@xhochy.com>

Closes #827 from xhochy/ARROW-1125 and squashes the following commits:

5738c893 [Uwe L. Korn] ninja format
cd80f880 [Uwe L. Korn] Explicitly cast to boolean
6abad750 [Uwe L. Korn] Add to arrow::py
83d55e9f [Uwe L. Korn] ARROW-1125: Python: Add public C++ API to unwrap PyArrow object


Project: http://git-wip-us.apache.org/repos/asf/arrow/repo
Commit: http://git-wip-us.apache.org/repos/asf/arrow/commit/ad57ea8e
Tree: http://git-wip-us.apache.org/repos/asf/arrow/tree/ad57ea8e
Diff: http://git-wip-us.apache.org/repos/asf/arrow/diff/ad57ea8e

Branch: refs/heads/master
Commit: ad57ea8ecabc38af0dc680780c33fb3cb130eb26
Parents: 471a85f
Author: Uwe L. Korn <uwelk@xhochy.com>
Authored: Mon Jul 10 22:27:56 2017 -0400
Committer: Wes McKinney <wes.mckinney@twosigma.com>
Committed: Mon Jul 10 22:27:56 2017 -0400

----------------------------------------------------------------------
 cpp/src/arrow/python/pyarrow.cc    | 117 +++++++++++++++++++++++++++++++
 cpp/src/arrow/python/pyarrow.h     |  29 ++++++++
 cpp/src/arrow/python/pyarrow_api.h |  54 ++++++++++++++
 python/pyarrow/includes/common.pxd |   1 +
 python/pyarrow/public-api.pxi      | 120 ++++++++++++++++++++++++++++++++
 5 files changed, 321 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/arrow/blob/ad57ea8e/cpp/src/arrow/python/pyarrow.cc
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/python/pyarrow.cc b/cpp/src/arrow/python/pyarrow.cc
index 56c0381..5d88051 100644
--- a/cpp/src/arrow/python/pyarrow.cc
+++ b/cpp/src/arrow/python/pyarrow.cc
@@ -35,38 +35,155 @@ int import_pyarrow() {
   return ::import_pyarrow__lib();
 }
 
+bool is_buffer(PyObject* buffer) {
+  return ::pyarrow_is_buffer(buffer) != 0;
+}
+
+Status unwrap_buffer(PyObject* buffer, std::shared_ptr<Buffer>* out) {
+  *out = ::pyarrow_unwrap_buffer(buffer);
+  if (*out) {
+    return Status::OK();
+  } else {
+    return Status::Invalid("Could not unwrap Buffer from the passed Python object.");
+  }
+}
+
 PyObject* wrap_buffer(const std::shared_ptr<Buffer>& buffer) {
   return ::pyarrow_wrap_buffer(buffer);
 }
 
+bool is_data_type(PyObject* data_type) {
+  return ::pyarrow_is_data_type(data_type) != 0;
+}
+
+Status unwrap_data_type(PyObject* object, std::shared_ptr<DataType>* out) {
+  *out = ::pyarrow_unwrap_data_type(object);
+  if (*out) {
+    return Status::OK();
+  } else {
+    return Status::Invalid("Could not unwrap DataType from the passed Python object.");
+  }
+}
+
 PyObject* wrap_data_type(const std::shared_ptr<DataType>& type) {
   return ::pyarrow_wrap_data_type(type);
 }
 
+bool is_field(PyObject* field) {
+  return ::pyarrow_is_field(field) != 0;
+}
+
+Status unwrap_field(PyObject* field, std::shared_ptr<Field>* out) {
+  *out = ::pyarrow_unwrap_field(field);
+  if (*out) {
+    return Status::OK();
+  } else {
+    return Status::Invalid("Could not unwrap Field from the passed Python object.");
+  }
+}
+
 PyObject* wrap_field(const std::shared_ptr<Field>& field) {
   return ::pyarrow_wrap_field(field);
 }
 
+bool is_schema(PyObject* schema) {
+  return ::pyarrow_is_schema(schema) != 0;
+}
+
+Status unwrap_schema(PyObject* schema, std::shared_ptr<Schema>* out) {
+  *out = ::pyarrow_unwrap_schema(schema);
+  if (*out) {
+    return Status::OK();
+  } else {
+    return Status::Invalid("Could not unwrap Schema from the passed Python object.");
+  }
+}
+
 PyObject* wrap_schema(const std::shared_ptr<Schema>& schema) {
   return ::pyarrow_wrap_schema(schema);
 }
 
+bool is_array(PyObject* array) {
+  return ::pyarrow_is_array(array) != 0;
+}
+
+Status unwrap_array(PyObject* array, std::shared_ptr<Array>* out) {
+  *out = ::pyarrow_unwrap_array(array);
+  if (*out) {
+    return Status::OK();
+  } else {
+    return Status::Invalid("Could not unwrap Array from the passed Python object.");
+  }
+}
+
 PyObject* wrap_array(const std::shared_ptr<Array>& array) {
   return ::pyarrow_wrap_array(array);
 }
 
+bool is_tensor(PyObject* tensor) {
+  return ::pyarrow_is_tensor(tensor) != 0;
+}
+
+Status unwrap_tensor(PyObject* tensor, std::shared_ptr<Tensor>* out) {
+  *out = ::pyarrow_unwrap_tensor(tensor);
+  if (*out) {
+    return Status::OK();
+  } else {
+    return Status::Invalid("Could not unwrap Tensor from the passed Python object.");
+  }
+}
+
 PyObject* wrap_tensor(const std::shared_ptr<Tensor>& tensor) {
   return ::pyarrow_wrap_tensor(tensor);
 }
 
+bool is_column(PyObject* column) {
+  return ::pyarrow_is_column(column) != 0;
+}
+
+Status unwrap_column(PyObject* column, std::shared_ptr<Column>* out) {
+  *out = ::pyarrow_unwrap_column(column);
+  if (*out) {
+    return Status::OK();
+  } else {
+    return Status::Invalid("Could not unwrap Column from the passed Python object.");
+  }
+}
+
 PyObject* wrap_column(const std::shared_ptr<Column>& column) {
   return ::pyarrow_wrap_column(column);
 }
 
+bool is_table(PyObject* table) {
+  return ::pyarrow_is_table(table) != 0;
+}
+
+Status unwrap_table(PyObject* table, std::shared_ptr<Table>* out) {
+  *out = ::pyarrow_unwrap_table(table);
+  if (*out) {
+    return Status::OK();
+  } else {
+    return Status::Invalid("Could not unwrap Table from the passed Python object.");
+  }
+}
+
 PyObject* wrap_table(const std::shared_ptr<Table>& table) {
   return ::pyarrow_wrap_table(table);
 }
 
+bool is_record_batch(PyObject* batch) {
+  return ::pyarrow_is_batch(batch) != 0;
+}
+
+Status unwrap_record_batch(PyObject* batch, std::shared_ptr<RecordBatch>* out) {
+  *out = ::pyarrow_unwrap_batch(batch);
+  if (*out) {
+    return Status::OK();
+  } else {
+    return Status::Invalid("Could not unwrap RecordBatch from the passed Python object.");
+  }
+}
+
 PyObject* wrap_record_batch(const std::shared_ptr<RecordBatch>& batch) {
   return ::pyarrow_wrap_batch(batch);
 }

http://git-wip-us.apache.org/repos/asf/arrow/blob/ad57ea8e/cpp/src/arrow/python/pyarrow.h
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/python/pyarrow.h b/cpp/src/arrow/python/pyarrow.h
index 7c618ce..7278d1c 100644
--- a/cpp/src/arrow/python/pyarrow.h
+++ b/cpp/src/arrow/python/pyarrow.h
@@ -33,20 +33,49 @@ class DataType;
 class Field;
 class RecordBatch;
 class Schema;
+class Status;
 class Table;
 class Tensor;
 
 namespace py {
 
 ARROW_EXPORT int import_pyarrow();
+
+ARROW_EXPORT bool is_buffer(PyObject* buffer);
+ARROW_EXPORT Status unwrap_buffer(PyObject* buffer, std::shared_ptr<Buffer>* out);
 ARROW_EXPORT PyObject* wrap_buffer(const std::shared_ptr<Buffer>& buffer);
+
+ARROW_EXPORT bool is_data_type(PyObject* data_type);
+ARROW_EXPORT Status unwrap_data_type(PyObject* data_type, std::shared_ptr<DataType>*
out);
 ARROW_EXPORT PyObject* wrap_data_type(const std::shared_ptr<DataType>& type);
+
+ARROW_EXPORT bool is_field(PyObject* field);
+ARROW_EXPORT Status unwrap_field(PyObject* field, std::shared_ptr<Field>* out);
 ARROW_EXPORT PyObject* wrap_field(const std::shared_ptr<Field>& field);
+
+ARROW_EXPORT bool is_schema(PyObject* schema);
+ARROW_EXPORT Status unwrap_schema(PyObject* schema, std::shared_ptr<Schema>* out);
 ARROW_EXPORT PyObject* wrap_schema(const std::shared_ptr<Schema>& schema);
+
+ARROW_EXPORT bool is_array(PyObject* array);
+ARROW_EXPORT Status unwrap_array(PyObject* array, std::shared_ptr<Array>* out);
 ARROW_EXPORT PyObject* wrap_array(const std::shared_ptr<Array>& array);
+
+ARROW_EXPORT bool is_tensor(PyObject* tensor);
+ARROW_EXPORT Status unwrap_tensor(PyObject* tensor, std::shared_ptr<Tensor>* out);
 ARROW_EXPORT PyObject* wrap_tensor(const std::shared_ptr<Tensor>& tensor);
+
+ARROW_EXPORT bool is_column(PyObject* column);
+ARROW_EXPORT Status unwrap_column(PyObject* column, std::shared_ptr<Column>* out);
 ARROW_EXPORT PyObject* wrap_column(const std::shared_ptr<Column>& column);
+
+ARROW_EXPORT bool is_table(PyObject* table);
+ARROW_EXPORT Status unwrap_table(PyObject* table, std::shared_ptr<Table>* out);
 ARROW_EXPORT PyObject* wrap_table(const std::shared_ptr<Table>& table);
+
+ARROW_EXPORT bool is_record_batch(PyObject* batch);
+ARROW_EXPORT Status unwrap_record_batch(
+    PyObject* batch, std::shared_ptr<RecordBatch>* out);
 ARROW_EXPORT PyObject* wrap_record_batch(const std::shared_ptr<RecordBatch>& batch);
 
 }  // namespace py

http://git-wip-us.apache.org/repos/asf/arrow/blob/ad57ea8e/cpp/src/arrow/python/pyarrow_api.h
----------------------------------------------------------------------
diff --git a/cpp/src/arrow/python/pyarrow_api.h b/cpp/src/arrow/python/pyarrow_api.h
index 7b70844..423595f 100644
--- a/cpp/src/arrow/python/pyarrow_api.h
+++ b/cpp/src/arrow/python/pyarrow_api.h
@@ -41,6 +41,42 @@ static PyObject *(*__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_table)(std::shared_ptr
 #define pyarrow_wrap_table __pyx_api_f_7pyarrow_3lib_pyarrow_wrap_table
 static PyObject *(*__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_batch)(std::shared_ptr< arrow::RecordBatch>
 const &) = 0;
 #define pyarrow_wrap_batch __pyx_api_f_7pyarrow_3lib_pyarrow_wrap_batch
+static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_is_buffer)(PyObject *) = 0;
+#define pyarrow_is_buffer __pyx_api_f_7pyarrow_3lib_pyarrow_is_buffer
+static std::shared_ptr< arrow::Buffer>  (*__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_buffer)(PyObject
*) = 0;
+#define pyarrow_unwrap_buffer __pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_buffer
+static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_is_data_type)(PyObject *) = 0;
+#define pyarrow_is_data_type __pyx_api_f_7pyarrow_3lib_pyarrow_is_data_type
+static std::shared_ptr< arrow::DataType>  (*__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_data_type)(PyObject
*) = 0;
+#define pyarrow_unwrap_data_type __pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_data_type
+static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_is_field)(PyObject *) = 0;
+#define pyarrow_is_field __pyx_api_f_7pyarrow_3lib_pyarrow_is_field
+static std::shared_ptr< arrow::Field>  (*__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_field)(PyObject
*) = 0;
+#define pyarrow_unwrap_field __pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_field
+static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_is_schema)(PyObject *) = 0;
+#define pyarrow_is_schema __pyx_api_f_7pyarrow_3lib_pyarrow_is_schema
+static std::shared_ptr< arrow::Schema>  (*__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_schema)(PyObject
*) = 0;
+#define pyarrow_unwrap_schema __pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_schema
+static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_is_array)(PyObject *) = 0;
+#define pyarrow_is_array __pyx_api_f_7pyarrow_3lib_pyarrow_is_array
+static std::shared_ptr< arrow::Array>  (*__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_array)(PyObject
*) = 0;
+#define pyarrow_unwrap_array __pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_array
+static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_is_tensor)(PyObject *) = 0;
+#define pyarrow_is_tensor __pyx_api_f_7pyarrow_3lib_pyarrow_is_tensor
+static std::shared_ptr< arrow::Tensor>  (*__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_tensor)(PyObject
*) = 0;
+#define pyarrow_unwrap_tensor __pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_tensor
+static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_is_column)(PyObject *) = 0;
+#define pyarrow_is_column __pyx_api_f_7pyarrow_3lib_pyarrow_is_column
+static std::shared_ptr< arrow::Column>  (*__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_column)(PyObject
*) = 0;
+#define pyarrow_unwrap_column __pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_column
+static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_is_table)(PyObject *) = 0;
+#define pyarrow_is_table __pyx_api_f_7pyarrow_3lib_pyarrow_is_table
+static std::shared_ptr< arrow::Table>  (*__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_table)(PyObject
*) = 0;
+#define pyarrow_unwrap_table __pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_table
+static int (*__pyx_api_f_7pyarrow_3lib_pyarrow_is_batch)(PyObject *) = 0;
+#define pyarrow_is_batch __pyx_api_f_7pyarrow_3lib_pyarrow_is_batch
+static std::shared_ptr< arrow::RecordBatch>  (*__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_batch)(PyObject
*) = 0;
+#define pyarrow_unwrap_batch __pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_batch
 #if !defined(__Pyx_PyIdentifier_FromString)
 #if PY_MAJOR_VERSION < 3
   #define __Pyx_PyIdentifier_FromString(s) PyString_FromString(s)
@@ -133,6 +169,24 @@ static int import_pyarrow__lib(void) {
   if (__Pyx_ImportFunction(module, "pyarrow_wrap_column", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_column,
"PyObject *(std::shared_ptr< arrow::Column>  const &)") < 0) goto bad;
   if (__Pyx_ImportFunction(module, "pyarrow_wrap_table", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_table,
"PyObject *(std::shared_ptr< arrow::Table>  const &)") < 0) goto bad;
   if (__Pyx_ImportFunction(module, "pyarrow_wrap_batch", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_wrap_batch,
"PyObject *(std::shared_ptr< arrow::RecordBatch>  const &)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_is_buffer", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_buffer,
"int (PyObject *)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_unwrap_buffer", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_buffer,
"std::shared_ptr< arrow::Buffer>  (PyObject *)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_is_data_type", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_data_type,
"int (PyObject *)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_unwrap_data_type", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_data_type,
"std::shared_ptr< arrow::DataType>  (PyObject *)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_is_field", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_field,
"int (PyObject *)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_unwrap_field", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_field,
"std::shared_ptr< arrow::Field>  (PyObject *)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_is_schema", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_schema,
"int (PyObject *)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_unwrap_schema", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_schema,
"std::shared_ptr< arrow::Schema>  (PyObject *)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_is_array", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_array,
"int (PyObject *)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_unwrap_array", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_array,
"std::shared_ptr< arrow::Array>  (PyObject *)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_is_tensor", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_tensor,
"int (PyObject *)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_unwrap_tensor", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_tensor,
"std::shared_ptr< arrow::Tensor>  (PyObject *)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_is_column", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_column,
"int (PyObject *)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_unwrap_column", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_column,
"std::shared_ptr< arrow::Column>  (PyObject *)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_is_table", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_table,
"int (PyObject *)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_unwrap_table", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_table,
"std::shared_ptr< arrow::Table>  (PyObject *)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_is_batch", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_is_batch,
"int (PyObject *)") < 0) goto bad;
+  if (__Pyx_ImportFunction(module, "pyarrow_unwrap_batch", (void (**)(void))&__pyx_api_f_7pyarrow_3lib_pyarrow_unwrap_batch,
"std::shared_ptr< arrow::RecordBatch>  (PyObject *)") < 0) goto bad;
   Py_DECREF(module); module = 0;
   return 0;
   bad:

http://git-wip-us.apache.org/repos/asf/arrow/blob/ad57ea8e/python/pyarrow/includes/common.pxd
----------------------------------------------------------------------
diff --git a/python/pyarrow/includes/common.pxd b/python/pyarrow/includes/common.pxd
index 73bfb4f..3487d48 100644
--- a/python/pyarrow/includes/common.pxd
+++ b/python/pyarrow/includes/common.pxd
@@ -36,6 +36,7 @@ cdef extern from "<Python.h>":
 cdef extern from "arrow/api.h" namespace "arrow" nogil:
     # We can later add more of the common status factory methods as needed
     cdef CStatus CStatus_OK "Status::OK"()
+    cdef CStatus CStatus_Invalid "Status::Invalid"()
 
     cdef cppclass CStatus "arrow::Status":
         CStatus()

http://git-wip-us.apache.org/repos/asf/arrow/blob/ad57ea8e/python/pyarrow/public-api.pxi
----------------------------------------------------------------------
diff --git a/python/pyarrow/public-api.pxi b/python/pyarrow/public-api.pxi
index 637b749..8c2083d 100644
--- a/python/pyarrow/public-api.pxi
+++ b/python/pyarrow/public-api.pxi
@@ -20,6 +20,22 @@ from pyarrow.includes.libarrow cimport (CArray, CColumn, CDataType, CField,
                                         CRecordBatch, CSchema,
                                         CTable, CTensor)
 
+# You cannot assign something to a dereferenced pointer in Cython thus these
+# methods don't use Status to indicate a successful operation.
+
+
+cdef public api bint pyarrow_is_buffer(object buffer):
+    return isinstance(buffer, Buffer)
+
+
+cdef public api shared_ptr[CBuffer] pyarrow_unwrap_buffer(object buffer):
+    cdef Buffer buf
+    if pyarrow_is_buffer(buffer):
+        buf = <Buffer>(buffer)
+        return buf.buffer
+
+    return shared_ptr[CBuffer]()
+
 
 cdef public api object pyarrow_wrap_buffer(const shared_ptr[CBuffer]& buf):
     cdef Buffer result = Buffer()
@@ -27,6 +43,19 @@ cdef public api object pyarrow_wrap_buffer(const shared_ptr[CBuffer]&
buf):
     return result
 
 
+cdef public api bint pyarrow_is_data_type(object type_):
+    return isinstance(type_, DataType)
+
+
+cdef public api shared_ptr[CDataType] pyarrow_unwrap_data_type(object data_type):
+    cdef DataType type_
+    if pyarrow_is_data_type(data_type):
+        type_ = <DataType>(data_type)
+        return type_.sp_type
+
+    return shared_ptr[CDataType]()
+
+
 cdef public api object pyarrow_wrap_data_type(
     const shared_ptr[CDataType]& type):
     cdef:
@@ -52,6 +81,19 @@ cdef public api object pyarrow_wrap_data_type(
     return out
 
 
+cdef public api bint pyarrow_is_field(object field):
+    return isinstance(field, Field)
+
+
+cdef public api shared_ptr[CField] pyarrow_unwrap_field(object field):
+    cdef Field field_
+    if pyarrow_is_field(field):
+        field_ = <Field>(field)
+        return field_.sp_field
+
+    return shared_ptr[CField]()
+
+
 cdef public api object pyarrow_wrap_field(const shared_ptr[CField]& field):
     if field.get() == NULL:
         return None
@@ -60,12 +102,38 @@ cdef public api object pyarrow_wrap_field(const shared_ptr[CField]&
field):
     return out
 
 
+cdef public api bint pyarrow_is_schema(object schema):
+    return isinstance(schema, Schema)
+
+
+cdef public api shared_ptr[CSchema] pyarrow_unwrap_schema(object schema):
+    cdef Schema sch
+    if pyarrow_is_schema(schema):
+        sch = <Schema>(schema)
+        return sch.sp_schema
+
+    return shared_ptr[CSchema]()
+
+
 cdef public api object pyarrow_wrap_schema(const shared_ptr[CSchema]& type):
     cdef Schema out = Schema()
     out.init_schema(type)
     return out
 
 
+cdef public api bint pyarrow_is_array(object array):
+    return isinstance(array, Array)
+
+
+cdef public api shared_ptr[CArray] pyarrow_unwrap_array(object array):
+    cdef Array arr
+    if pyarrow_is_array(array):
+        arr = <Array>(array)
+        return arr.sp_array
+
+    return shared_ptr[CArray]()
+
+
 cdef public api object pyarrow_wrap_array(const shared_ptr[CArray]& sp_array):
     if sp_array.get() == NULL:
         raise ValueError('Array was NULL')
@@ -80,6 +148,19 @@ cdef public api object pyarrow_wrap_array(const shared_ptr[CArray]&
sp_array):
     return arr
 
 
+cdef public api bint pyarrow_is_tensor(object tensor):
+    return isinstance(tensor, Tensor)
+
+
+cdef public api shared_ptr[CTensor] pyarrow_unwrap_tensor(object tensor):
+    cdef Tensor ten
+    if pyarrow_is_tensor(tensor):
+        ten = <Tensor>(tensor)
+        return ten.sp_tensor
+
+    return shared_ptr[CTensor]()
+
+
 cdef public api object pyarrow_wrap_tensor(
     const shared_ptr[CTensor]& sp_tensor):
     if sp_tensor.get() == NULL:
@@ -90,18 +171,57 @@ cdef public api object pyarrow_wrap_tensor(
     return tensor
 
 
+cdef public api bint pyarrow_is_column(object column):
+    return isinstance(column, Column)
+
+
+cdef public api shared_ptr[CColumn] pyarrow_unwrap_column(object column):
+    cdef Column col
+    if pyarrow_is_column(column):
+        col = <Column>(column)
+        return col.sp_column
+
+    return shared_ptr[CColumn]()
+
+
 cdef public api object pyarrow_wrap_column(const shared_ptr[CColumn]& ccolumn):
     cdef Column column = Column()
     column.init(ccolumn)
     return column
 
 
+cdef public api bint pyarrow_is_table(object table):
+    return isinstance(table, Table)
+
+
+cdef public api shared_ptr[CTable] pyarrow_unwrap_table(object table):
+    cdef Table tab
+    if pyarrow_is_table(table):
+        tab = <Table>(table)
+        return tab.sp_table
+
+    return shared_ptr[CTable]()
+
+
 cdef public api object pyarrow_wrap_table(const shared_ptr[CTable]& ctable):
     cdef Table table = Table()
     table.init(ctable)
     return table
 
 
+cdef public api bint pyarrow_is_batch(object batch):
+    return isinstance(batch, RecordBatch)
+
+
+cdef public api shared_ptr[CRecordBatch] pyarrow_unwrap_batch(object batch):
+    cdef RecordBatch bat
+    if pyarrow_is_batch(batch):
+        bat = <RecordBatch>(batch)
+        return bat.sp_batch
+
+    return shared_ptr[CRecordBatch]()
+
+
 cdef public api object pyarrow_wrap_batch(
     const shared_ptr[CRecordBatch]& cbatch):
     cdef RecordBatch batch = RecordBatch()


Mime
View raw message