forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.cc
More file actions
329 lines (270 loc) · 9.42 KB
/
Copy pathio.cc
File metadata and controls
329 lines (270 loc) · 9.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "arrow/python/io.h"
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <mutex>
#include <string>
#include "arrow/io/memory.h"
#include "arrow/memory_pool.h"
#include "arrow/status.h"
#include "arrow/util/logging.h"
#include "arrow/python/common.h"
#include "arrow/python/pyarrow.h"
namespace arrow {
namespace py {
// ----------------------------------------------------------------------
// Python file
// A common interface to a Python file-like object. Must acquire GIL before
// calling any methods
class PythonFile {
public:
explicit PythonFile(PyObject* file) : file_(file) { Py_INCREF(file); }
Status CheckClosed() const {
if (!file_) {
return Status::Invalid("operation on closed Python file");
}
return Status::OK();
}
Status Close() {
if (file_) {
PyObject* result = cpp_PyObject_CallMethod(file_.obj(), "close", "()");
Py_XDECREF(result);
file_.reset();
PY_RETURN_IF_ERROR(StatusCode::IOError);
}
return Status::OK();
}
Status Abort() {
file_.reset();
return Status::OK();
}
bool closed() const {
if (!file_) {
return true;
}
PyObject* result = PyObject_GetAttrString(file_.obj(), "closed");
if (result == NULL) {
// Can't propagate the error, so write it out and return an arbitrary value
PyErr_WriteUnraisable(NULL);
return true;
}
int ret = PyObject_IsTrue(result);
Py_XDECREF(result);
if (ret < 0) {
PyErr_WriteUnraisable(NULL);
return true;
}
return ret != 0;
}
Status Seek(int64_t position, int whence) {
RETURN_NOT_OK(CheckClosed());
// whence: 0 for relative to start of file, 2 for end of file
PyObject* result = cpp_PyObject_CallMethod(file_.obj(), "seek", "(ni)",
static_cast<Py_ssize_t>(position), whence);
Py_XDECREF(result);
PY_RETURN_IF_ERROR(StatusCode::IOError);
return Status::OK();
}
Status Read(int64_t nbytes, PyObject** out) {
RETURN_NOT_OK(CheckClosed());
PyObject* result = cpp_PyObject_CallMethod(file_.obj(), "read", "(n)",
static_cast<Py_ssize_t>(nbytes));
PY_RETURN_IF_ERROR(StatusCode::IOError);
*out = result;
return Status::OK();
}
Status Write(const void* data, int64_t nbytes) {
RETURN_NOT_OK(CheckClosed());
// Since the data isn't owned, we have to make a copy
PyObject* py_data =
PyBytes_FromStringAndSize(reinterpret_cast<const char*>(data), nbytes);
PY_RETURN_IF_ERROR(StatusCode::IOError);
PyObject* result = cpp_PyObject_CallMethod(file_.obj(), "write", "(O)", py_data);
Py_XDECREF(py_data);
Py_XDECREF(result);
PY_RETURN_IF_ERROR(StatusCode::IOError);
return Status::OK();
}
Status Write(const std::shared_ptr<Buffer>& buffer) {
RETURN_NOT_OK(CheckClosed());
#if PY_MAJOR_VERSION < 3
// On Python 2, a write() method can typically call str() on its argument
// to get its bytes payload (this is the case with socket.makefile()).
// Unfortunately, on non-bytes buffer-like objects this will give out
// the repr() of the object rather than its data. So fall back on
// copying the data to a bytes object.
return Write(buffer->data(), buffer->size());
#else
PyObject* py_data = wrap_buffer(buffer);
PY_RETURN_IF_ERROR(StatusCode::IOError);
PyObject* result = cpp_PyObject_CallMethod(file_.obj(), "write", "(O)", py_data);
Py_XDECREF(py_data);
Py_XDECREF(result);
PY_RETURN_IF_ERROR(StatusCode::IOError);
return Status::OK();
#endif
}
Status Tell(int64_t* position) {
RETURN_NOT_OK(CheckClosed());
PyObject* result = cpp_PyObject_CallMethod(file_.obj(), "tell", "()");
PY_RETURN_IF_ERROR(StatusCode::IOError);
*position = PyLong_AsLongLong(result);
Py_DECREF(result);
// PyLong_AsLongLong can raise OverflowError
PY_RETURN_IF_ERROR(StatusCode::IOError);
return Status::OK();
}
std::mutex& lock() { return lock_; }
private:
std::mutex lock_;
OwnedRefNoGIL file_;
};
// ----------------------------------------------------------------------
// Seekable input stream
PyReadableFile::PyReadableFile(PyObject* file) { file_.reset(new PythonFile(file)); }
// The destructor does not close the underlying Python file object, as
// there may be multiple references to it. Instead let the Python
// destructor do its job.
PyReadableFile::~PyReadableFile() {}
Status PyReadableFile::Abort() {
return SafeCallIntoPython([this]() { return file_->Abort(); });
}
Status PyReadableFile::Close() {
return SafeCallIntoPython([this]() { return file_->Close(); });
}
bool PyReadableFile::closed() const {
bool res;
Status st = SafeCallIntoPython([this, &res]() {
res = file_->closed();
return Status::OK();
});
return res;
}
Status PyReadableFile::Seek(int64_t position) {
return SafeCallIntoPython([=] { return file_->Seek(position, 0); });
}
Status PyReadableFile::Tell(int64_t* position) const {
return SafeCallIntoPython([=]() { return file_->Tell(position); });
}
Status PyReadableFile::Read(int64_t nbytes, int64_t* bytes_read, void* out) {
return SafeCallIntoPython([=]() {
OwnedRef bytes;
RETURN_NOT_OK(file_->Read(nbytes, bytes.ref()));
PyObject* bytes_obj = bytes.obj();
DCHECK(bytes_obj != NULL);
if (!PyBytes_Check(bytes_obj)) {
return Status::TypeError(
"Python file read() should have returned a bytes object, got '",
Py_TYPE(bytes_obj)->tp_name, "' (did you open the file in binary mode?)");
}
*bytes_read = PyBytes_GET_SIZE(bytes_obj);
std::memcpy(out, PyBytes_AS_STRING(bytes_obj), *bytes_read);
return Status::OK();
});
}
Status PyReadableFile::Read(int64_t nbytes, std::shared_ptr<Buffer>* out) {
return SafeCallIntoPython([=]() {
OwnedRef bytes_obj;
RETURN_NOT_OK(file_->Read(nbytes, bytes_obj.ref()));
DCHECK(bytes_obj.obj() != NULL);
return PyBuffer::FromPyObject(bytes_obj.obj(), out);
});
}
Status PyReadableFile::ReadAt(int64_t position, int64_t nbytes, int64_t* bytes_read,
void* out) {
std::lock_guard<std::mutex> guard(file_->lock());
return SafeCallIntoPython([=]() {
RETURN_NOT_OK(Seek(position));
return Read(nbytes, bytes_read, out);
});
}
Status PyReadableFile::ReadAt(int64_t position, int64_t nbytes,
std::shared_ptr<Buffer>* out) {
std::lock_guard<std::mutex> guard(file_->lock());
return SafeCallIntoPython([=]() {
RETURN_NOT_OK(Seek(position));
return Read(nbytes, out);
});
}
Status PyReadableFile::GetSize(int64_t* size) {
return SafeCallIntoPython([=]() {
int64_t current_position = -1;
RETURN_NOT_OK(file_->Tell(¤t_position));
RETURN_NOT_OK(file_->Seek(0, 2));
int64_t file_size = -1;
RETURN_NOT_OK(file_->Tell(&file_size));
// Restore previous file position
RETURN_NOT_OK(file_->Seek(current_position, 0));
*size = file_size;
return Status::OK();
});
}
// ----------------------------------------------------------------------
// Output stream
PyOutputStream::PyOutputStream(PyObject* file) : position_(0) {
file_.reset(new PythonFile(file));
}
// The destructor does not close the underlying Python file object, as
// there may be multiple references to it. Instead let the Python
// destructor do its job.
PyOutputStream::~PyOutputStream() {}
Status PyOutputStream::Abort() {
return SafeCallIntoPython([=]() { return file_->Abort(); });
}
Status PyOutputStream::Close() {
return SafeCallIntoPython([=]() { return file_->Close(); });
}
bool PyOutputStream::closed() const {
bool res;
Status st = SafeCallIntoPython([this, &res]() {
res = file_->closed();
return Status::OK();
});
return res;
}
Status PyOutputStream::Tell(int64_t* position) const {
*position = position_;
return Status::OK();
}
Status PyOutputStream::Write(const void* data, int64_t nbytes) {
return SafeCallIntoPython([=]() {
position_ += nbytes;
return file_->Write(data, nbytes);
});
}
Status PyOutputStream::Write(const std::shared_ptr<Buffer>& buffer) {
return SafeCallIntoPython([=]() {
position_ += buffer->size();
return file_->Write(buffer);
});
}
// ----------------------------------------------------------------------
// Foreign buffer
Status PyForeignBuffer::Make(const uint8_t* data, int64_t size, PyObject* base,
std::shared_ptr<Buffer>* out) {
PyForeignBuffer* buf = new PyForeignBuffer(data, size, base);
if (buf == NULL) {
return Status::OutOfMemory("could not allocate foreign buffer object");
} else {
*out = std::shared_ptr<Buffer>(buf);
return Status::OK();
}
}
} // namespace py
} // namespace arrow