Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
common.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_COMMON_H
19 #define ARROW_PYTHON_COMMON_H
20 
21 #include <string>
22 
23 #include "arrow/python/config.h"
24 
25 #include "arrow/buffer.h"
26 #include "arrow/util/macros.h"
27 #include "arrow/util/visibility.h"
28 
29 namespace arrow {
30 
31 class MemoryPool;
32 
33 namespace py {
34 
35 class ARROW_EXPORT PyAcquireGIL {
36  public:
37  PyAcquireGIL() : acquired_gil_(false) { acquire(); }
38 
39  ~PyAcquireGIL() { release(); }
40 
41  void acquire() {
42  if (!acquired_gil_) {
43  state_ = PyGILState_Ensure();
44  acquired_gil_ = true;
45  }
46  }
47 
48  // idempotent
49  void release() {
50  if (acquired_gil_) {
51  PyGILState_Release(state_);
52  acquired_gil_ = false;
53  }
54  }
55 
56  private:
57  bool acquired_gil_;
58  PyGILState_STATE state_;
60 };
61 
62 #define PYARROW_IS_PY2 PY_MAJOR_VERSION <= 2
63 
64 class ARROW_EXPORT OwnedRef {
65  public:
66  OwnedRef() : obj_(NULLPTR) {}
67 
68  explicit OwnedRef(PyObject* obj) : obj_(obj) {}
69 
71  PyAcquireGIL lock;
72  release();
73  }
74 
75  void reset(PyObject* obj) {
79  Py_XDECREF(obj_);
80  obj_ = obj;
81  }
82 
83  void release() {
84  Py_XDECREF(obj_);
85  obj_ = NULLPTR;
86  }
87 
88  PyObject* obj() const { return obj_; }
89 
90  private:
91  PyObject* obj_;
92 };
93 
94 // This is different from OwnedRef in that it assumes that
95 // the GIL is held by the caller and doesn't decrement the
96 // reference count when release is called.
97 class ARROW_EXPORT ScopedRef {
98  public:
99  ScopedRef() : obj_(NULLPTR) {}
100 
101  explicit ScopedRef(PyObject* obj) : obj_(obj) {}
102 
103  ~ScopedRef() { Py_XDECREF(obj_); }
104 
105  void reset(PyObject* obj) {
106  Py_XDECREF(obj_);
107  obj_ = obj;
108  }
109 
110  PyObject* release() {
111  PyObject* result = obj_;
112  obj_ = NULLPTR;
113  return result;
114  }
115 
116  PyObject* get() const { return obj_; }
117 
118  PyObject** ref() { return &obj_; }
119 
120  private:
121  PyObject* obj_;
122 };
123 
124 struct ARROW_EXPORT PyObjectStringify {
126  const char* bytes;
127  Py_ssize_t size;
128 
129  explicit PyObjectStringify(PyObject* obj) {
130  PyObject* bytes_obj;
131  if (PyUnicode_Check(obj)) {
132  bytes_obj = PyUnicode_AsUTF8String(obj);
133  tmp_obj.reset(bytes_obj);
134  bytes = PyBytes_AsString(bytes_obj);
135  size = PyBytes_GET_SIZE(bytes_obj);
136  } else if (PyBytes_Check(obj)) {
137  bytes = PyBytes_AsString(obj);
138  size = PyBytes_GET_SIZE(obj);
139  } else {
140  bytes = NULLPTR;
141  size = -1;
142  }
143  }
144 };
145 
147 
149 
150 // TODO(wesm): We can just let errors pass through. To be explored later
151 #define RETURN_IF_PYERROR() RETURN_NOT_OK(CheckPyError());
152 
153 #define PY_RETURN_IF_ERROR(CODE) RETURN_NOT_OK(CheckPyError(CODE));
154 
155 // Return the common PyArrow memory pool
156 ARROW_EXPORT void set_default_memory_pool(MemoryPool* pool);
157 ARROW_EXPORT MemoryPool* get_memory_pool();
158 
159 class ARROW_EXPORT PyBuffer : public Buffer {
160  public:
165  explicit PyBuffer(PyObject* obj);
166  ~PyBuffer();
167 
168  private:
169  PyObject* obj_;
170 };
171 
172 } // namespace py
173 } // namespace arrow
174 
175 #endif // ARROW_PYTHON_COMMON_H
Py_ssize_t size
Definition: common.h:127
Definition: common.h:124
OwnedRef()
Definition: common.h:66
#define NULLPTR
Definition: macros.h:69
StatusCode
Definition: status.h:85
~OwnedRef()
Definition: common.h:70
PyObject * obj() const
Definition: common.h:88
PyObjectStringify(PyObject *obj)
Definition: common.h:129
Definition: status.h:106
void release()
Definition: common.h:83
void acquire()
Definition: common.h:41
void release()
Definition: common.h:49
~ScopedRef()
Definition: common.h:103
Status CheckPyError(StatusCode code=StatusCode::UnknownError)
MemoryPool * get_memory_pool()
Definition: common.h:159
void reset(PyObject *obj)
Definition: common.h:75
Top-level namespace for Apache Arrow C++ API.
Definition: allocator.h:29
OwnedRef(PyObject *obj)
Definition: common.h:68
void set_default_memory_pool(MemoryPool *pool)
OwnedRef tmp_obj
Definition: common.h:125
PyAcquireGIL()
Definition: common.h:37
PyObject * release()
Definition: common.h:110
Definition: common.h:35
const char * bytes
Definition: common.h:126
ScopedRef(PyObject *obj)
Definition: common.h:101
~PyAcquireGIL()
Definition: common.h:39
ScopedRef()
Definition: common.h:99
Immutable API for a chunk of bytes which may or may not be owned by the class instance.
Definition: buffer.h:48
#define ARROW_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:23
PyObject ** ref()
Definition: common.h:118
Definition: common.h:64
void reset(PyObject *obj)
Definition: common.h:105
Definition: common.h:97
Status PassPyError()