Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
helpers.h
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 #ifndef ARROW_PYTHON_HELPERS_H
19 #define ARROW_PYTHON_HELPERS_H
20 
21 #include "arrow/python/platform.h"
22 
23 #include <limits>
24 #include <memory>
25 #include <string>
26 #include <utility>
27 
28 #include <numpy/halffloat.h>
29 
30 #include "arrow/type.h"
31 #include "arrow/util/macros.h"
32 #include "arrow/util/visibility.h"
33 
34 namespace arrow {
35 
36 namespace py {
37 
38 class OwnedRef;
39 
40 // \brief Get an arrow DataType instance from Arrow's Type::type enum
41 // \param[in] type One of the values of Arrow's Type::type enum
42 // \return A shared pointer to DataType
43 ARROW_EXPORT std::shared_ptr<DataType> GetPrimitiveType(Type::type type);
44 
45 // \brief Construct a np.float16 object from a npy_half value.
46 ARROW_EXPORT PyObject* PyHalf_FromHalf(npy_half value);
47 
48 // \brief Convert a Python object to a npy_half value.
49 ARROW_EXPORT Status PyFloat_AsHalf(PyObject* obj, npy_half* out);
50 
51 namespace internal {
52 
53 // \brief Import a Python module
54 // \param[in] module_name The name of the module
55 // \param[out] ref The OwnedRef containing the module PyObject*
56 Status ImportModule(const std::string& module_name, OwnedRef* ref);
57 
58 // \brief Import an object from a Python module
59 // \param[in] module A Python module
60 // \param[in] name The name of the object to import
61 // \param[out] ref The OwnedRef containing the \c name attribute of the Python module \c
62 // module
63 Status ImportFromModule(const OwnedRef& module, const std::string& name, OwnedRef* ref);
64 
65 // \brief Check whether obj is an integer, independent of Python versions.
66 inline bool IsPyInteger(PyObject* obj) {
67 #if PYARROW_IS_PY2
68  return PyLong_Check(obj) || PyInt_Check(obj);
69 #else
70  return PyLong_Check(obj);
71 #endif
72 }
73 
74 // \brief Use pandas missing value semantics to check if a value is null
75 bool PandasObjectIsNull(PyObject* obj);
76 
77 // \brief Check whether obj is nan
78 bool PyFloat_IsNaN(PyObject* obj);
79 
80 inline bool IsPyBinary(PyObject* obj) {
81  return PyBytes_Check(obj) || PyByteArray_Check(obj);
82 }
83 
84 // \brief Convert a Python integer into a C integer
85 // \param[in] obj A Python integer
86 // \param[out] out A pointer to a C integer to hold the result of the conversion
87 // \return The status of the operation
88 template <typename Int>
89 Status CIntFromPython(PyObject* obj, Int* out, const std::string& overflow_message = "");
90 
91 // \brief Convert a Python unicode string to a std::string
92 Status PyUnicode_AsStdString(PyObject* obj, std::string* out);
93 
94 // \brief Convert a Python bytes object to a std::string
95 std::string PyBytes_AsStdString(PyObject* obj);
96 
97 // \brief Call str() on the given object and return the result as a std::string
98 Status PyObject_StdStringStr(PyObject* obj, std::string* out);
99 
100 // \brief Return the repr() of the given object (always succeeds)
101 std::string PyObject_StdStringRepr(PyObject* obj);
102 
103 // \brief Cast the given size to int32_t, with error checking
104 inline Status CastSize(Py_ssize_t size, int32_t* out,
105  const char* error_msg = "Maximum size exceeded (2GB)") {
106  // size is assumed to be positive
107  if (size > std::numeric_limits<int32_t>::max()) {
108  return Status::Invalid(error_msg);
109  }
110  *out = static_cast<int32_t>(size);
111  return Status::OK();
112 }
113 
114 Status BuilderAppend(StringBuilder* builder, PyObject* obj, bool check_valid = false,
115  bool* is_full = nullptr);
116 Status BuilderAppend(BinaryBuilder* builder, PyObject* obj, bool* is_full = nullptr);
117 Status BuilderAppend(FixedSizeBinaryBuilder* builder, PyObject* obj,
118  bool* is_full = nullptr);
119 
120 } // namespace internal
121 } // namespace py
122 } // namespace arrow
123 
124 #endif // ARROW_PYTHON_HELPERS_H
type
Definition: type.h:46
PyObject * PyHalf_FromHalf(npy_half value)
std::shared_ptr< DataType > GetPrimitiveType(Type::type type)
static Status OK()
Definition: status.h:106
static Status Invalid(const std::string &msg)
Definition: status.h:129
Top-level namespace for Apache Arrow C++ API.
Definition: adapter.h:32
Status PyFloat_AsHalf(PyObject *obj, npy_half *out)