-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathPythonFileWrapper.cpp
More file actions
286 lines (270 loc) · 11.4 KB
/
Copy pathPythonFileWrapper.cpp
File metadata and controls
286 lines (270 loc) · 11.4 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
//
// Created by Paul Ross on 08/07/2021.
//
#include "PythonFileWrapper.h"
#include <sstream>
/**
* Macro that gets the given method and checks that it is callable.
* If not an ExceptionPythonFileObjectWrapper is thrown.
*/
#define EXTRACT_METHOD_AND_CHECK(name) \
m_python_##name##_method = PyObject_GetAttrString(python_file_object, #name); /* New ref. */\
if (!m_python_##name##_method) { \
std::ostringstream oss; \
oss << "PythonFileObjectWrapper: can not get method: " << #name << std::endl; \
Py_XDECREF(python_file_object); \
Py_XDECREF(m_python_read_method); \
Py_XDECREF(m_python_write_method); \
Py_XDECREF(m_python_seek_method); \
Py_XDECREF(m_python_tell_method); \
throw ExceptionPythonFileObjectWrapper(oss.str()); \
} \
if (!PyCallable_Check(m_python_##name##_method)) { \
std::ostringstream oss; \
oss << "PythonFileObjectWrapper: method: " << #name << " is not callable" << std::endl; \
Py_XDECREF(m_python_file_object); \
Py_XDECREF(m_python_read_method); \
Py_XDECREF(m_python_write_method); \
Py_XDECREF(m_python_seek_method); \
Py_XDECREF(m_python_tell_method); \
throw ExceptionPythonFileObjectWrapper(oss.str()); \
}
PythonFileObjectWrapper::PythonFileObjectWrapper(PyObject *python_file_object) : m_python_file_object(
python_file_object),
m_python_read_method(NULL),
m_python_write_method(NULL),
m_python_seek_method(NULL),
m_python_tell_method(NULL) {
assert(python_file_object);
Py_INCREF(m_python_file_object);
/* Get the read and write methods of the passed object */
EXTRACT_METHOD_AND_CHECK(read);
EXTRACT_METHOD_AND_CHECK(write);
EXTRACT_METHOD_AND_CHECK(seek);
EXTRACT_METHOD_AND_CHECK(tell);
}
int PythonFileObjectWrapper::read_py_write_cpp(Py_ssize_t number_of_bytes, std::iostream &ios) {
assert(!PyErr_Occurred());
assert(m_python_file_object);
assert(m_python_read_method);
assert(m_python_write_method);
int ret = 0;
#if DEBUG_PYEXT_COMMON
fprintf(stdout, "%s(): %s#%d number_of_bytes=%ld\n", __FUNCTION__, __FILE__, __LINE__, number_of_bytes);
#endif
PyObject * read_args = Py_BuildValue("(i)", number_of_bytes);
PyObject * read_value = PyObject_Call(m_python_read_method, read_args, NULL);
if (read_value == NULL) {
ret = -1;
goto except;
} else {
/* Check for EOF */
if (number_of_bytes >= 0 && PySequence_Length(read_value) != number_of_bytes) {
ret = -2; /* Signal EOF. */
goto except;
}
if (PyBytes_Check(read_value)) {
ios.write(PyBytes_AsString(read_value), PyBytes_Size(read_value));
} else if (PyUnicode_Check(read_value)) {
Py_ssize_t size;
const char *buffer = PyUnicode_AsUTF8AndSize(read_value, &size);
ios.write(buffer, size);
} else {
ret = -3;
goto except;
}
}
goto finally;
except:
/* Handle every abnormal condition and clean up. */
assert(ret);
finally:
/* Clean up under normal conditions and return an appropriate value. */
Py_XDECREF(read_args);
Py_XDECREF(read_value);
#if DEBUG_PYEXT_COMMON
fprintf(stdout, "%s(): %s#%d ret=%d\n", __FUNCTION__, __FILE__, __LINE__, ret);
#endif
return ret;
}
int PythonFileObjectWrapper::read_cpp_write_py(std::iostream &ios, Py_ssize_t number_of_bytes) {
assert(!PyErr_Occurred());
assert(m_python_file_object);
assert(m_python_read_method);
assert(m_python_write_method);
int ret = 0;
PyObject *py_bytes = NULL;
PyObject *write_args = NULL;
PyObject *write_result = NULL;
#if DEBUG_PYEXT_COMMON
fprintf(stdout, "%s(): %s#%d number_of_bytes=%ld\n", __FUNCTION__, __FILE__, __LINE__, number_of_bytes);
#endif
if (!ios.good()) {
PyErr_SetString(PyExc_ValueError, "C++ stream not capable of being read.");
goto except;
}
// Read from ios, write to Python file.
// Create a Python bytes object, read into it.
py_bytes = PyBytes_FromStringAndSize(NULL, number_of_bytes);
ios.read(PyBytes_AsString(py_bytes), number_of_bytes);
if (!ios.good()) {
PyErr_SetString(PyExc_ValueError, "Can not read from C++ stream.");
goto except;
}
write_args = Py_BuildValue("(O)", py_bytes);
write_result = PyObject_Call(m_python_write_method, write_args, NULL);
if (write_result == NULL) {
ret = -1;
goto except;
}
if (PyLong_AsLong(write_result) != number_of_bytes) {
ret = -2;
goto except;
}
goto finally;
except:
/* Handle every abnormal condition and clean up. */
assert(ret);
finally:
/* Clean up under normal conditions and return an appropriate value. */
Py_XDECREF(py_bytes);
Py_XDECREF(write_args);
Py_XDECREF(write_result);
#if DEBUG_PYEXT_COMMON
fprintf(stdout, "%s(): %s#%d ret=%d\n", __FUNCTION__, __FILE__, __LINE__, ret);
#endif
return ret;
}
int PythonFileObjectWrapper::read(Py_ssize_t number_of_bytes, std::vector<char> &result) {
assert(!PyErr_Occurred());
assert(m_python_file_object);
assert(m_python_read_method);
assert(m_python_write_method);
int ret = 0;
#if DEBUG_PYEXT_COMMON
fprintf(stdout, "%s(): %s#%d number_of_bytes=%ld\n", __FUNCTION__, __FILE__, __LINE__, number_of_bytes);
#endif
result.clear();
PyObject * read_args = Py_BuildValue("(i)", number_of_bytes);
PyObject * read_value = PyObject_Call(m_python_read_method, read_args, NULL);
if (read_value == NULL) {
ret = -1;
goto except;
} else {
/* Check for EOF */
if (number_of_bytes >= 0 && PySequence_Length(read_value) != number_of_bytes) {
ret = -2; /* Signal EOF. */
goto except;
}
const char *buffer;
Py_ssize_t size;
if (PyBytes_Check(read_value)) {
buffer = PyBytes_AsString(read_value);
size =PyBytes_Size(read_value);
} else if (PyUnicode_Check(read_value)) {
buffer = PyUnicode_AsUTF8AndSize(read_value, &size);
} else {
ret = -3;
goto except;
}
for (Py_ssize_t i = 0; i < size; ++i) {
result.push_back(buffer[i]);
}
}
goto finally;
except:
/* Handle every abnormal condition and clean up. */
assert(ret);
finally:
/* Clean up under normal conditions and return an appropriate value. */
Py_XDECREF(read_args);
Py_XDECREF(read_value);
#if DEBUG_PYEXT_COMMON
fprintf(stdout, "%s(): %s#%d ret=%d\n", __FUNCTION__, __FILE__, __LINE__, ret);
#endif
return ret;
}
int PythonFileObjectWrapper::write(const char *buffer, Py_ssize_t number_of_bytes) {
assert(!PyErr_Occurred());
assert(m_python_file_object);
assert(m_python_read_method);
assert(m_python_write_method);
int ret = 0;
PyObject * py_bytes = NULL;
PyObject * write_args = NULL;
PyObject * write_result = NULL;
#if DEBUG_PYEXT_COMMON
fprintf(stdout, "%s(): %s#%d number_of_bytes=%ld\n", __FUNCTION__, __FILE__, __LINE__, number_of_bytes);
#endif
// Create a Python bytes object, read into it.
py_bytes = PyBytes_FromStringAndSize(buffer, number_of_bytes);
write_args = Py_BuildValue("(O)", py_bytes);
write_result = PyObject_Call(m_python_write_method, write_args, NULL);
if (write_result == NULL) {
ret = -1;
goto except;
}
if (PyLong_AsLong(write_result) != number_of_bytes) {
ret = -2;
goto except;
}
goto finally;
except:
/* Handle every abnormal condition and clean up. */
assert(ret);
finally:
/* Clean up under normal conditions and return an appropriate value. */
Py_XDECREF(py_bytes);
Py_XDECREF(write_args);
Py_XDECREF(write_result);
#if DEBUG_PYEXT_COMMON
fprintf(stdout, "%s(): %s#%d ret=%d\n", __FUNCTION__, __FILE__, __LINE__, ret);
#endif
return ret;
}
long PythonFileObjectWrapper::seek(Py_ssize_t pos, int whence) {
assert(!PyErr_Occurred());
assert(m_python_file_object);
assert(m_python_seek_method);
PyObject * arguments = Py_BuildValue("ni", pos, whence);
PyObject * result = PyObject_Call(m_python_seek_method, arguments, NULL);
return PyLong_AsLong(result);
}
long PythonFileObjectWrapper::tell() {
assert(!PyErr_Occurred());
assert(m_python_file_object);
assert(m_python_tell_method);
PyObject * result = PyObject_CallNoArgs(m_python_tell_method);
return PyLong_AsLong(result);
}
std::string PythonFileObjectWrapper::str_pointers() const {
std::ostringstream oss;
oss << "PythonFileObjectWrapper:" << std::endl;
oss << "m_python_file_object " << std::hex << m_python_file_object << " type: "
<< Py_TYPE(m_python_file_object)->tp_name << " ref count=" << std::dec << m_python_file_object->ob_refcnt
<< std::endl;
oss << "m_python_read_method " << std::hex << m_python_read_method << " type: "
<< Py_TYPE(m_python_read_method)->tp_name << " ref count=" << std::dec << m_python_read_method->ob_refcnt
<< std::endl;
oss << "m_python_write_method " << std::hex << m_python_write_method << " type: "
<< Py_TYPE(m_python_write_method)->tp_name << " ref count=" << std::dec << m_python_write_method->ob_refcnt
<< std::endl;
oss << "m_python_seek_method " << std::hex << m_python_seek_method << " type: "
<< Py_TYPE(m_python_seek_method)->tp_name << " ref count=" << std::dec << m_python_seek_method->ob_refcnt
<< std::endl;
oss << "m_python_tell_method " << std::hex << m_python_tell_method << " type: "
<< Py_TYPE(m_python_tell_method)->tp_name << " ref count=" << std::dec << m_python_tell_method->ob_refcnt
<< std::endl;
return {oss.str()};
}
PyObject *PythonFileObjectWrapper::py_str_pointers() const {
std::string str_result = str_pointers();
return PyBytes_FromStringAndSize(str_result.c_str(), (Py_ssize_t) str_result.size());
}
PythonFileObjectWrapper::~PythonFileObjectWrapper() {
Py_XDECREF(m_python_read_method);
Py_XDECREF(m_python_write_method);
Py_XDECREF(m_python_seek_method);
Py_XDECREF(m_python_tell_method);
Py_XDECREF(m_python_file_object);
}