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 ARROW_EXPORT
57 Status ImportModule(const std::string& module_name, OwnedRef* ref);
58 
59 // \brief Import an object from a Python module
60 // \param[in] module A Python module
61 // \param[in] name The name of the object to import
62 // \param[out] ref The OwnedRef containing the \c name attribute of the Python module \c
63 // module
64 ARROW_EXPORT
65 Status ImportFromModule(const OwnedRef& module, const std::string& name, OwnedRef* ref);
66 
67 // \brief Check whether obj is an integer, independent of Python versions.
68 inline bool IsPyInteger(PyObject* obj) {
69 #if PYARROW_IS_PY2
70  return PyLong_Check(obj) || PyInt_Check(obj);
71 #else
72  return PyLong_Check(obj);
73 #endif
74 }
75 
76 // \brief Use pandas missing value semantics to check if a value is null
77 ARROW_EXPORT
78 bool PandasObjectIsNull(PyObject* obj);
79 
80 // \brief Check whether obj is a floating-point NaN
81 ARROW_EXPORT
82 bool PyFloat_IsNaN(PyObject* obj);
83 
84 inline bool IsPyBinary(PyObject* obj) {
85  return PyBytes_Check(obj) || PyByteArray_Check(obj);
86 }
87 
88 // \brief Convert a Python integer into a C integer
89 // \param[in] obj A Python integer
90 // \param[out] out A pointer to a C integer to hold the result of the conversion
91 // \return The status of the operation
92 template <typename Int>
93 Status CIntFromPython(PyObject* obj, Int* out, const std::string& overflow_message = "");
94 
95 // \brief Convert a Python unicode string to a std::string
96 ARROW_EXPORT
97 Status PyUnicode_AsStdString(PyObject* obj, std::string* out);
98 
99 // \brief Convert a Python bytes object to a std::string
100 ARROW_EXPORT
101 std::string PyBytes_AsStdString(PyObject* obj);
102 
103 // \brief Call str() on the given object and return the result as a std::string
104 ARROW_EXPORT
105 Status PyObject_StdStringStr(PyObject* obj, std::string* out);
106 
107 // \brief Return the repr() of the given object (always succeeds)
108 ARROW_EXPORT
109 std::string PyObject_StdStringRepr(PyObject* obj);
110 
111 // \brief Cast the given size to int32_t, with error checking
112 inline Status CastSize(Py_ssize_t size, int32_t* out,
113  const char* error_msg = "Maximum size exceeded (2GB)") {
114  // size is assumed to be positive
115  if (size > std::numeric_limits<int32_t>::max()) {
116  return Status::Invalid(error_msg);
117  }
118  *out = static_cast<int32_t>(size);
119  return Status::OK();
120 }
121 
122 // \brief Print the Python object's __str__ form along with the passed error
123 // message
124 ARROW_EXPORT
125 Status InvalidValue(PyObject* obj, const std::string& why);
126 
127 ARROW_EXPORT
128 Status IntegerScalarToDoubleSafe(PyObject* obj, double* result);
129 ARROW_EXPORT
130 Status IntegerScalarToFloat32Safe(PyObject* obj, float* result);
131 
132 } // namespace internal
133 } // namespace py
134 } // namespace arrow
135 
136 #endif // ARROW_PYTHON_HELPERS_H
type
Definition: type.h:48
PyObject * PyHalf_FromHalf(npy_half value)
std::shared_ptr< DataType > GetPrimitiveType(Type::type type)
static Status OK()
Definition: status.h:124
static Status Invalid(const std::string &msg)
Definition: status.h:150
Top-level namespace for Apache Arrow C++ API.
Definition: adapter.h:32
_object PyObject
Definition: serialize.h:30
Status PyFloat_AsHalf(PyObject *obj, npy_half *out)