forked from qtnc/6pad2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython34.h
392 lines (355 loc) · 16 KB
/
python34.h
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#ifndef ___PYTHON___HPP___9
#define ___PYTHON___HPP___9
#include<python/python.h>
#include "strings.hpp"
#include "variant.h"
#include<tuple>
#include<functional>
#include<string>
#define GIL_PROTECT RAII_GIL ___RAII_GIL_VAR_##__LINE__;
typedef PyObject*(*PyCFunc)(PyObject*,PyObject*);
struct RAII_GIL {
PyGILState_STATE gil;
RAII_GIL(): gil(PyGILState_Ensure()) { }
~RAII_GIL(){ PyGILState_Release(gil); }
};
struct PyObjectWithDic {
PyObject_HEAD
PyObject* dic;
};
void PyStart (void);
///Automatic wrappers!
template<class T> struct PyTypeSpec { };
template<> struct PyTypeSpec<int> {
typedef int type;
static constexpr const char c = 'i';
static inline int convert (int i) { return i; }
static inline int convert2 (int i) { return i; }
static inline int convert3 (PyObject* o) {
if (!PyLong_Check(o)) { PyErr_SetString(PyExc_TypeError, "int expected"); return 0; }
return PyLong_AsLong(o);
}
};
template<> struct PyTypeSpec<DWORD> {
typedef DWORD type;
static constexpr const char c = 'I';
static inline DWORD convert (DWORD i) { return i; }
static inline DWORD convert2 (DWORD i) { return i; }
static inline DWORD convert3 (PyObject* o) {
if (!PyLong_Check(o)) { PyErr_SetString(PyExc_TypeError, "int expected"); return 0; }
return PyLong_AsLongLong(o);
}
};
template<> struct PyTypeSpec<size_t> {
typedef size_t type;
static constexpr const char c = 'n';
static inline size_t convert (size_t i) { return i; }
static inline size_t convert2 (size_t i) { return i; }
static inline int convert3 (PyObject* o) {
if (!PyLong_Check(o)) { PyErr_SetString(PyExc_TypeError, "int expected"); return 0; }
return PyLong_AsLongLong(o);
}
};
template<> struct PyTypeSpec<double> {
typedef double type;
static constexpr const char c = 'd';
static inline double convert (double i) { return i; }
static inline double convert2 (double i) { return i; }
static inline double convert3 (PyObject* o) {
if (!PyFloat_Check(o)) { PyErr_SetString(PyExc_TypeError, "float expected"); return 0; }
return PyFloat_AsDouble(o);
}
};
template<> struct PyTypeSpec<bool> {
typedef bool type;
static constexpr const char c = 'p';
static inline bool convert (bool i) { return i; }
static inline bool convert2 (bool i) { return i; }
static inline bool convert3 (PyObject* o) {
if (o==Py_True) return true;
else if (o==Py_False) return false;
else PyErr_SetString(PyExc_TypeError, "bool expected");
return false;
}
};
template<> struct PyTypeSpec<std::string> {
typedef char* type;
static constexpr const char c = 's';
static inline std::string convert (const char* s) { return s; }
static inline const char* convert2 (const std::string& s) { return s.c_str(); }
static inline string convert3 (PyObject* o) {
if (!PyUnicode_Check(o)) { PyErr_SetString(PyExc_TypeError, "str expected"); return ""; }
return toString(PyUnicode_AsUnicode(o));
}
};
template<> struct PyTypeSpec<const std::string&> {
typedef char* type;
static constexpr const char c = 's';
static inline std::string convert (const char* s) { return s; }
static inline const char* convert2 (const std::string& s) { return s.c_str(); }
static inline string convert3 (PyObject* o) {
if (!PyUnicode_Check(o)) { PyErr_SetString(PyExc_TypeError, "str expected"); return ""; }
return toString(PyUnicode_AsUnicode(o));
}
};
template<> struct PyTypeSpec<std::wstring> {
typedef wchar_t* type;
static constexpr const char c = 'u';
static inline std::wstring convert (const wchar_t* s) { return s; }
static inline const wchar_t* convert2 (const std::wstring& s) { return s.c_str(); }
static inline wstring convert3 (PyObject* o) {
if (!PyUnicode_Check(o)) { PyErr_SetString(PyExc_TypeError, "str expected"); return L""; }
return toWString(PyUnicode_AsUnicode(o));
}
};
template<> struct PyTypeSpec<const std::wstring&> {
typedef wchar_t* type;
static constexpr const char c = 'u';
static inline std::wstring convert (const wchar_t* s) { return s; }
static inline const wchar_t* convert2 (const std::wstring& s) { return s.c_str(); }
static inline wstring convert3 (PyObject* o) {
if (!PyUnicode_Check(o)) { PyErr_SetString(PyExc_TypeError, "str expected"); return L""; }
return toWString(PyUnicode_AsUnicode(o));
}
};
template<> struct PyTypeSpec<const char*> {
typedef const char* type;
static constexpr const char c = 's';
static inline const char* convert (const char* s) { return s; }
static inline const char* convert2 (const char* s) { return s; }
};
template<> struct PyTypeSpec<const wchar_t*> {
typedef const wchar_t* type;
static constexpr const char c = 'u';
static inline const wchar_t* convert (const wchar_t* s) { return s; }
static inline const wchar_t* convert2 (const wchar_t* s) { return s; }
};
template<> struct PyTypeSpec<var> {
static var convert3 (PyObject* o) {
if (o==Py_None) return var();
else if (o==Py_True) return true;
else if (o==Py_False) return false;
else if (PyLong_Check(o)) return (int)(PyLong_AsLong(o));
else if (PyUnicode_Check(o)) return toTString(PyUnicode_AsUnicode(o));
else PyErr_SetString(PyExc_TypeError, "none, bool, int or str expected");
return var();
}};
template<> struct PyTypeSpec<PyObject*> {
typedef PyObject* type;
static constexpr const char c = 'O';
static inline PyObject* convert (PyObject* i) { return i; }
static inline PyObject* convert2 (PyObject* i) { return i; }
static inline PyObject* convert3 (PyObject* o) { return o; }
};
template<class... Args> inline const char* PyTypeSpecs (void) {
static constexpr const int n = sizeof...(Args);
static constexpr const char cc[n+1] = { PyTypeSpec<Args>::c... ,0};
return cc;
};
template<class... Args> inline const char* PyTypeSpecsTuple (void) {
static constexpr const int n = sizeof...(Args);
static constexpr const char cc[n+3] = { '(', PyTypeSpec<Args>::c... , ')', 0};
return cc;
};
struct PySafeObject {
PyObject* o;
inline PySafeObject (): o(0) {}
inline PySafeObject (PyObject* x): o(0) { operator=(x); }
inline PySafeObject (const PySafeObject& x): PySafeObject(x.o) {}
inline PySafeObject& operator= (const PySafeObject& x) { return operator=(x.o); }
PySafeObject& operator= (PyObject* x) {
GIL_PROTECT
Py_XINCREF(x);
Py_XDECREF(o);
o=x;
return *this;
}
PySafeObject& operator= (PySafeObject&& x) {
if (this==&x) return *this;
GIL_PROTECT;
Py_XDECREF(o);
o = x.o;
x.o = 0;
return *this;
}
inline PySafeObject (PySafeObject&& x): o(x.o) { x.o=0; }
inline ~PySafeObject () { operator=(NULL); }
inline bool operator== (PyObject* x) { return x==o; }
inline bool operator== (const PySafeObject& x) { return x.o==o; }
inline PyObject* operator* () { return o; }
inline operator bool () { return !!o && o!=Py_None && o!=Py_False; }
};
struct PyCallback {
PyObject* func;
inline PyCallback (): func(0) {}
inline PyCallback (PyObject* x): func(0) { operator=(x); }
inline PyCallback (const PyCallback& x): PyCallback(x.func) {}
inline PyCallback& operator= (const PyCallback& x) { return operator=(x.func); }
PyCallback& operator= (PyObject* o) {
GIL_PROTECT
if (o && !PyCallable_Check(o)) o=NULL;
Py_XINCREF(o);
Py_XDECREF(func);
func=o;
return *this;
}
PyCallback& operator= (PyCallback&& x) {
if (this==&x) return *this;
GIL_PROTECT
Py_XDECREF(func);
func = x.func;
x.func = 0;
return *this;
}
inline PyCallback (PyCallback&& x): func(x.func) { x.func=0; }
inline ~PyCallback () { operator=(NULL); }
inline bool operator== (const PyCallback& pcb) { return func==pcb.func; }
inline operator bool () const { return !!func && func!=Py_None; }
template<class R, class... A> R operator() (A... args) const {
GIL_PROTECT
PyObject* argtuple = Py_BuildValue(PyTypeSpecsTuple<A...>(), PyTypeSpec<A>::convert2(args)...);
PyObject* pyResult = PyObject_CallObject(func, argtuple);
if (!pyResult) PyErr_Print();
Py_XDECREF(argtuple);
R cResult = PyTypeSpec<R>::convert3(pyResult);
Py_XDECREF(pyResult);
return cResult;
}
template<class... A> void operator() (A... args) const {
GIL_PROTECT
PyObject* argtuple = Py_BuildValue(PyTypeSpecsTuple<A...>(), PyTypeSpec<A>::convert2(args)...);
PyObject* pyResult = PyObject_CallObject(func, argtuple);
if (!pyResult) PyErr_Print();
Py_XDECREF(argtuple);
Py_XDECREF(pyResult);
}
};//PyCallback
template<> struct PyTypeSpec<PyCallback> {
typedef PyObject* type;
static constexpr const char c = 'O';
static inline PyCallback convert (PyObject* i) { return i; }
static inline PyObject* convert2 (const PyCallback& i) { return i.func; }
static inline PyCallback convert3 (PyObject* o) {
if (!PyCallable_Check(o)) { PyErr_SetString(PyExc_TypeError, "object must be callable"); return 0; }
return o;
}
};
template<> struct PyTypeSpec<const PyCallback&> {
typedef PyObject* type;
static constexpr const char c = 'O';
static inline PyCallback convert (PyObject* i) { return i; }
static inline PyObject* convert2 (const PyCallback& i) { return i.func; }
static inline PyCallback convert3 (PyObject* o) {
if (!PyCallable_Check(o)) { PyErr_SetString(PyExc_TypeError, "object must be callable"); return 0; }
return o;
}
};
template<> struct PyTypeSpec<PySafeObject> {
typedef PyObject* type;
static constexpr const char c = 'O';
static inline PySafeObject convert (PyObject* i) { return i; }
static inline PyObject* convert2 (const PySafeObject& i) { return i.o; }
static inline PySafeObject convert3 (PyObject* o) { return o; }
};
template<> struct PyTypeSpec<const PySafeObject&> {
typedef PyObject* type;
static constexpr const char c = 'O';
static inline PySafeObject convert (PyObject* i) { return i; }
static inline PyObject* convert2 (const PySafeObject& i) { return i.o; }
static inline PySafeObject convert3 (PyObject* o) { return o; }
};
template<int... S> struct TemplateSequence {};
template<int N, int... S> struct TemplateSequenceGenerator: TemplateSequenceGenerator<N -1, N -1, S...> {};
template<int... S> struct TemplateSequenceGenerator<0, S...> { typedef TemplateSequence<S...> sequence; };
template<class... A> struct PyParseTupleSpec {
template<int... S> static int PyArg_ParseTuple (TemplateSequence<S...> seq, PyObject* pyTuple, const char* pyArgSpec, std::tuple<typename PyTypeSpec<A>::type...>& args) { return ::PyArg_ParseTuple(pyTuple, pyArgSpec, &std::get<S>(args)...); }
};
template<class R, class... A> struct PyCTupleCallSpec {
template<int... S> static R call (TemplateSequence<S...> seq, R(*f)(A...), std::tuple<typename PyTypeSpec<A>::type...>& args) { return f( PyTypeSpec<typename std::tuple_element<S, std::tuple<A...>>::type>::convert(std::get<S>(args))...); }
template<int... S> static R call (TemplateSequence<S...> seq, R(__stdcall *f)(A...), std::tuple<typename PyTypeSpec<A>::type...>& args) { return f( PyTypeSpec<typename std::tuple_element<S, std::tuple<A...>>::type>::convert(std::get<S>(args))...); }
template<class C, int... S> static R callmeth (TemplateSequence<S...> seq, C& c, R(C::*f)(A...), std::tuple<typename PyTypeSpec<A>::type...>& args) { return (c.*f)( PyTypeSpec<typename std::tuple_element<S, std::tuple<A...>>::type>::convert(std::get<S>(args))...); }
};
template<class... A> struct PyCTupleCallSpec<void, A...> {
template<int... S> static void call (TemplateSequence<S...> seq, void(*f)(A...), std::tuple<typename PyTypeSpec<A>::type...>& args) { f( PyTypeSpec<typename std::tuple_element<S, std::tuple<A...>>::type>::convert(std::get<S>(args))...); }
template<int... S> static void call (TemplateSequence<S...> seq, void(__stdcall *f)(A...), std::tuple<typename PyTypeSpec<A>::type...>& args) { f( PyTypeSpec<typename std::tuple_element<S, std::tuple<A...>>::type>::convert(std::get<S>(args))...); }
template<class C, int... S> static void callmeth (TemplateSequence<S...> seq, C& c, void(C::*f)(A...), std::tuple<typename PyTypeSpec<A>::type...>& args) { (c.*f)( PyTypeSpec<typename std::tuple_element<S, std::tuple<A...>>::type>::convert(std::get<S>(args))...); }
};
template<class CFunc> struct PyFuncSpec {
template<class R, class... A> static inline PyObject* func2 (R(*cfunc)(A...), PyObject* pySelf, PyObject* pyArgs) {
typename TemplateSequenceGenerator<sizeof...(A)>::sequence seq;
std::tuple<typename PyTypeSpec<A>::type...> argtuple;
if (!PyParseTupleSpec<A...>::PyArg_ParseTuple(seq, pyArgs, PyTypeSpecs<A...>(), argtuple)) return NULL;
R result = PyCTupleCallSpec<R,A...>::call(seq, cfunc, argtuple);
return Py_BuildValue(PyTypeSpecs<R>(), PyTypeSpec<R>::convert2(result) );
}
template<class... A> static inline PyObject* func2 (void(*cfunc)(A...), PyObject* pySelf, PyObject* pyArgs) {
typename TemplateSequenceGenerator<sizeof...(A)>::sequence seq;
std::tuple<typename PyTypeSpec<A>::type...> argtuple;
if (!PyParseTupleSpec<A...>::PyArg_ParseTuple(seq, pyArgs, PyTypeSpecs<A...>(), argtuple)) return NULL;
PyCTupleCallSpec<void,A...>::call(seq, cfunc, argtuple);
Py_RETURN_NONE;
}
template<class R, class... A> static inline PyObject* func2 (R(__stdcall *cfunc)(A...), PyObject* pySelf, PyObject* pyArgs) {
typename TemplateSequenceGenerator<sizeof...(A)>::sequence seq;
std::tuple<typename PyTypeSpec<A>::type...> argtuple;
if (!PyParseTupleSpec<A...>::PyArg_ParseTuple(seq, pyArgs, PyTypeSpecs<A...>(), argtuple)) return NULL;
R result = PyCTupleCallSpec<R,A...>::call(seq, cfunc, argtuple);
return Py_BuildValue(PyTypeSpecs<R>(), PyTypeSpec<R>::convert2(result) );
}
template<class... A> static inline PyObject* func2 (void(*__stdcall cfunc)(A...), PyObject* pySelf, PyObject* pyArgs) {
typename TemplateSequenceGenerator<sizeof...(A)>::sequence seq;
std::tuple<typename PyTypeSpec<A>::type...> argtuple;
if (!PyParseTupleSpec<A...>::PyArg_ParseTuple(seq, pyArgs, PyTypeSpecs<A...>(), argtuple)) return NULL;
PyCTupleCallSpec<void,A...>::call(seq, cfunc, argtuple);
Py_RETURN_NONE;
}
template<class R, class O, class... A> static inline PyObject* func2 (R(O::*cfunc)(A...), PyObject* pySelf, PyObject* pyArgs) {
typename TemplateSequenceGenerator<sizeof...(A)>::sequence seq;
std::tuple<typename PyTypeSpec<A>::type...> argtuple;
if (!PyParseTupleSpec<A...>::PyArg_ParseTuple(seq, pyArgs, PyTypeSpecs<A...>(), argtuple)) return NULL;
R result = PyCTupleCallSpec<R,A...>::callmeth(seq, *(O*)(pySelf), cfunc, argtuple);
return Py_BuildValue(PyTypeSpecs<R>(), PyTypeSpec<R>::convert2(result) );
}
template<class O, class... A> static inline PyObject* func2 (void(O::*cfunc)(A...), PyObject* pySelf, PyObject* pyArgs) {
typename TemplateSequenceGenerator<sizeof...(A)>::sequence seq;
std::tuple<typename PyTypeSpec<A>::type...> argtuple;
if (!PyParseTupleSpec<A...>::PyArg_ParseTuple(seq, pyArgs, PyTypeSpecs<A...>(), argtuple)) return NULL;
PyCTupleCallSpec<void,A...>::callmeth(seq, *(O*)(pySelf), cfunc, argtuple);
Py_RETURN_NONE;
}
template<CFunc cfunc> static PyObject* func (PyObject* pySelf, PyObject* pyArgs) { return func2(cfunc, pySelf, pyArgs); }
};
template<class S> struct PySetterSpec {
template<class O, class A> static inline int set2 (void(O::*setf)(A), PyObject* self, PyObject* pyVal) {
A cVal = PyTypeSpec<A>::convert3(pyVal);
if (PyErr_Occurred()) return -1;
((*(O*)self).*setf)(cVal);
return 0;
}
template<class A> static inline int set2 (void(*setf)(A), PyObject* self, PyObject* pyVal) {
A cVal = PyTypeSpec<A>::convert3(pyVal);
if (PyErr_Occurred()) return -1;
setf(cVal);
return 0;
}
template<S setf> static inline int setter (PyObject* self, PyObject* val, void* unused) { return set2(setf, self, val); }
};
template<class G> struct PyGetterSpec {
template<class O, class A> static inline PyObject* get2 (A(O::*getf)(void), PyObject* self ) {
A result = ((*(O*)self).*getf)();
return Py_BuildValue(PyTypeSpecs<A>(), PyTypeSpec<A>::convert2(result) );
}
template<class A> static inline PyObject* get2 (A(*getf)(void), PyObject* self ) {
A result = getf();
return Py_BuildValue(PyTypeSpecs<A>(), PyTypeSpec<A>::convert2(result) );
}
template<G getf> static inline PyObject* getter (PyObject* self, void* unused) { return get2(getf, self); }
};
#define PyToCType(t,x) (PyTypeSpec<t>::convert3(x))
#define PyToPyType(x) (Py_BuildValue(PyTypeSpecs<decltype(x)>(), PyTypeSpec<decltype(x)>::convert2(x)))
#define PyDecl(n,f) {(n), (PyFuncSpec<decltype(f)>::func<f>), METH_VARARGS, NULL}
#define PyAccessor(n,g,s) {(n), (PyGetterSpec<decltype(g)>::getter<g>), (PySetterSpec<decltype(s)>::setter<s>), NULL, NULL}
#define PyWriteOnlyAccessor(n,s) {(n), NULL, (PySetterSpec<decltype(s)>::setter<s>), NULL, NULL}
#define PyReadOnlyAccessor(n,g) {(n), (PyGetterSpec<decltype(g)>::getter<g>), NULL, NULL, NULL}
#define PyDeclEnd {0, 0, 0, 0}
#endif