forked from fanchy/ffpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffpython.h
3925 lines (3693 loc) · 141 KB
/
ffpython.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef __FFPYTHON_H_
#define __FFPYTHON_H_
#include <Python.h>
#include <structmember.h>
#include <string>
#include <sstream>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <stdexcept>
using namespace std;
#ifdef _WIN32
#define SAFE_SPRINTF _snprintf_s
#else
#define SAFE_SPRINTF snprintf
#endif
//! 获取python异常信息
struct pyops_t
{
static int traceback(string& ret_)
{
PyObject* err = PyErr_Occurred();
if (err != NULL) {
PyObject *ptype = NULL, *pvalue = NULL, *ptraceback = NULL;
PyObject *pyth_module = NULL, *pyth_func = NULL;
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
if (pvalue)
{
if (true == PyList_Check(pvalue))
{
int n = PyList_Size(pvalue);
for (int i = 0; i < n; ++i)
{
PyObject *pystr = PyObject_Str(PyList_GetItem(pvalue, i));
ret_ += PyString_AsString(pystr);
ret_ += "\n";
Py_DECREF(pystr);
}
}
if (true == PyTuple_Check(pvalue))
{
int n = PyTuple_Size(pvalue);
for (int i = 0; i < n; ++i)
{
PyObject* tmp_str = PyTuple_GetItem(pvalue, i);
if (true == PyTuple_Check(tmp_str))
{
int m = PyTuple_Size(tmp_str);
for (int j = 0; j < m; ++j)
{
PyObject *pystr = PyObject_Str(PyTuple_GetItem(tmp_str, j));
ret_ += PyString_AsString(pystr);
ret_ += ",";
Py_DECREF(pystr);
}
}
else
{
PyObject *pystr = PyObject_Str(tmp_str);
ret_ += PyString_AsString(pystr);
Py_DECREF(pystr);
}
ret_ += "\n";
}
}
else
{
PyObject *pystr = PyObject_Str(pvalue);
if (pystr)
{
ret_ += PyString_AsString(pystr);
ret_ += "\n";
Py_DECREF(pystr);
}
}
}
/* See if we can get a full traceback */
PyObject *module_name = PyString_FromString("traceback");
pyth_module = PyImport_Import(module_name);
Py_DECREF(module_name);
if (pyth_module && ptype && pvalue && ptraceback)
{
pyth_func = PyObject_GetAttrString(pyth_module, "format_exception");
if (pyth_func && PyCallable_Check(pyth_func)) {
PyObject *pyth_val = PyObject_CallFunctionObjArgs(pyth_func, ptype, pvalue, ptraceback, NULL);
if (pyth_val && true == PyList_Check(pyth_val))
{
int n = PyList_Size(pyth_val);
for (int i = 0; i < n; ++i)
{
PyObject* tmp_str = PyList_GetItem(pyth_val, i);
PyObject *pystr = PyObject_Str(tmp_str);
if (pystr)
{
ret_ += PyString_AsString(pystr);
Py_DECREF(pystr);
}
ret_ += "\n";
}
}
Py_XDECREF(pyth_val);
}
}
Py_XDECREF(pyth_func);
Py_XDECREF(pyth_module);
Py_XDECREF(ptype);
Py_XDECREF(pvalue);
Py_XDECREF(ptraceback);
PyErr_Clear();
return 0;
}
return -1;
}
};
struct cpp_void_t{};
//! 用于抽取类型、类型对应的引用
template<typename T>
struct type_ref_traits_t;
//! 用于python 可选参数
template<typename T>
struct pyoption_t
{
typedef typename type_ref_traits_t<T>::value_t value_t;
pyoption_t():m_set_flag(false){}
bool is_set() const { return m_set_flag;}
void set() { m_set_flag = true;}
value_t& value() { return m_value;}
const value_t& value() const{ return m_value;}
const value_t& value(const value_t& default_)
{
if (is_set())
return m_value;
else
return default_;
}
bool m_set_flag;
value_t m_value;
};
//! 用于判断是否是可选参数
template<typename T>
struct pyoption_traits_t;
//! pytype_traits_t 封装 PyLong_FromLong 相关的操作,用于为调用python生成参数
template<typename T>
struct pytype_traits_t
{
static PyObject* pyobj_from_cppobj(const char* const val_)
{
return PyString_FromString(val_);
}
/*
static int pyobj_to_cppobj(PyObject *pvalue_, char*& m_ret)
{
if (true == PyString_Check(pvalue_))
{
m_ret = PyString_AsString(pvalue_);
return 0;
}
return -1;
}
*/
static const char* get_typename() { return "string";}
};
//! 用于调用python函数,生成tuple类型的python函数参数的工具类
struct pycall_arg_t
{
pycall_arg_t(int arg_num):
arg_index(0),
pargs_tuple(PyTuple_New(arg_num))
{}
~pycall_arg_t()
{
release();
}
PyObject * get_args() const
{
return pargs_tuple;
}
template<typename T>
pycall_arg_t& add(const T& val_)
{
if (arg_index < PyTuple_Size(pargs_tuple))
{
PyObject* tmp_arg = pytype_traits_t<T>::pyobj_from_cppobj(val_);
PyTuple_SetItem(pargs_tuple, arg_index, tmp_arg);
++arg_index;
}
return *this;
}
void release()
{
if (pargs_tuple)
{
Py_DECREF(pargs_tuple);
pargs_tuple = NULL;
}
}
int arg_index;
PyObject * pargs_tuple;
};
//! 用于调用python函数,获取返回值的工具类
class pytype_tool_t
{
public:
virtual ~pytype_tool_t(){};
virtual int parse_value(PyObject *pvalue_) = 0;
virtual const char* return_type() {return "";}
virtual bool need_release() { return true; }
};
//! 用于调用python函数,获取返回值的工具泛型类
template<typename T>
class pytype_tool_impl_t;
//! 封装调用python函数的C API
struct pycall_t
{
static int call_func(PyObject *pModule, const string& mod_name_, const string& func_name_,
pycall_arg_t& pyarg_, pytype_tool_t& pyret_, string& err_)
{
PyObject *pFunc = PyObject_GetAttrString(pModule, func_name_.c_str());
if (pFunc && PyCallable_Check(pFunc)) {
PyObject *pArgs = pyarg_.get_args();
PyObject *pValue = PyObject_CallObject(pFunc, pArgs);
pyarg_.release();//! 等价于Py_DECREF(pArgs);
if (pValue != NULL) {
if (pyret_.parse_value(pValue))
{
err_ += "value returned is not ";
err_ += pyret_.return_type();
err_ += string(" ") + func_name_ + " in " + mod_name_;
}
if (pyret_.need_release()){
Py_DECREF(pValue);
}
}
}
else
{
err_ += "Cannot find function ";
err_ += func_name_ + " in " + mod_name_ + ",";
}
Py_XDECREF(pFunc);
if (PyErr_Occurred())
{
pyops_t::traceback(err_);
return 0;
}
return 0;
}
static int call_func_obj(PyObject *pFunc, pycall_arg_t& pyarg_, pytype_tool_t& pyret_, string& err_)
{
if (pFunc && PyCallable_Check(pFunc)) {
PyObject *pArgs = pyarg_.get_args();
PyObject *pValue = PyObject_CallObject(pFunc, pArgs);
pyarg_.release();//! 等价于Py_DECREF(pArgs);
if (pValue != NULL) {
if (pyret_.parse_value(pValue))
{
err_ += "value returned is not ";
err_ += pyret_.return_type();
}
if (pyret_.need_release()){
Py_DECREF(pValue);
}
}
}
else
{
err_ += "invalid function";
}
if (PyErr_Occurred())
{
pyops_t::traceback(err_);
return 0;
}
return 0;
}
template<typename T>
static const T& call(const string& mod_name_, const string& func_name_, pycall_arg_t& pyarg_, pytype_tool_impl_t<T>& pyret);
template<typename T>
static const T& call_obj_method(PyObject *pObj, const string& func_name_, pycall_arg_t& pyarg_, pytype_tool_impl_t<T>& pyret);
template<typename T>
static const T& call_lambda(PyObject *pFunc, pycall_arg_t& pyarg_, pytype_tool_impl_t<T>& pyret);
};
//! 用于扩展python的工具类,用来解析参数
struct pyext_tool_t;
template<typename T>
struct pyext_return_tool_t;
//! 用于扩展python,生成pyobject类型的返回值给python
template <typename T>
struct pyext_func_traits_t;
//! 用于扩展python,traits出注册给python的函数接口
#ifndef PYCTOR
#define PYCTOR int (*)
#endif
//! 表示void类型,由于void类型不能return,用void_ignore_t适配
template<typename T>
struct void_ignore_t;
template<typename T>
struct void_ignore_t
{
typedef T value_t;
};
template<>
struct void_ignore_t<void>
{
typedef cpp_void_t value_t;
};
#define RET_V typename void_ignore_t<RET>::value_t
//! 记录各个基类和子类的相互关系
struct cpp_to_pyclass_reg_info_t
{
struct inherit_info_t
{
inherit_info_t():pytype_def(NULL){}
PyTypeObject* pytype_def;
string inherit_name;
set<PyTypeObject*> all_child;
};
typedef map<string, inherit_info_t> inherit_info_map_t;
static inherit_info_map_t& get_all_info()
{
static inherit_info_map_t inherit_info;
return inherit_info;
}
static void add(const string& child_, const string& base_, PyTypeObject* def_)
{
inherit_info_t tmp;
tmp.inherit_name = base_;
tmp.pytype_def = def_;
get_all_info()[child_] = tmp;
get_all_info()[base_].all_child.insert(def_);
}
static bool is_instance(PyObject* pysrc, const string& class_)
{
inherit_info_map_t& inherit_info = get_all_info();
inherit_info_t& tmp = inherit_info[class_];
if (tmp.pytype_def && PyObject_TypeCheck(pysrc, tmp.pytype_def))
{
return true;
}
for (set<PyTypeObject*>::iterator it = tmp.all_child.begin(); it != tmp.all_child.end(); ++it)
{
if (*it && PyObject_TypeCheck(pysrc, *it))
{
return true;
}
}
return false;
}
};
//! 记录C++ class 对应到python中的名称、参数信息等,全局
struct static_pytype_info_t
{
string class_name;
string mod_name;
int total_args_num;
PyTypeObject* pytype_def;
};
//! 工具类,用于生成分配python class的接口,包括分配、释放
template<typename T>
struct pyclass_base_info_t
{
struct obj_data_t
{
obj_data_t():obj(NULL){}
PyObject_HEAD;
T* obj;
bool forbid_release;
void disable_auto_release(){ forbid_release = true; }
void release()
{
if (obj)
{
delete obj;
obj = NULL;
}
}
};
static void free_obj(obj_data_t* self)
{
if (false == self->forbid_release && self->obj)
{
self->release();
}
self->ob_type->tp_free((PyObject*)self);
}
static PyObject *alloc_obj(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
obj_data_t *self = (obj_data_t *)type->tp_alloc(type, 0);
return (PyObject *)self;
}
static PyObject *release(PyTypeObject *type, PyObject *args)
{
obj_data_t *self = (obj_data_t *)type;
self->release();
return Py_BuildValue("i", 1);
}
static static_pytype_info_t pytype_info;
};
template<typename T>
static_pytype_info_t pyclass_base_info_t<T>::pytype_info;
//! 方便生成pyclass 初始化函数
template <typename CLASS_TYPE, typename CTOR>
struct pyclass_ctor_tool_t;
//! used to gen method of py class
template<typename T>
struct pyclass_method_gen_t;
//! 防止出现指针为NULL调用出错
#define NULL_PTR_GUARD(X) if (NULL == X) {PyErr_SetString(PyExc_TypeError, "obj data ptr NULL");return NULL;}
//! 用于生成python 的getter和setter接口,适配于c++ class的成员变量
template <typename CLASS_TYPE, typename RET>
struct pyclass_member_func_gen_t
{
typedef typename pyclass_base_info_t<CLASS_TYPE>::obj_data_t obj_data_t;
typedef RET CLASS_TYPE::* property_ptr_t;
static PyObject *getter_func(obj_data_t *self, void *closure)
{
property_ptr_t property_ptr;
::memcpy((void*)&property_ptr, (const void*)&closure, sizeof(closure));
NULL_PTR_GUARD(self->obj);
CLASS_TYPE* p = self->obj;
return pytype_traits_t<RET>::pyobj_from_cppobj(p->*property_ptr);
}
static int setter_func(obj_data_t *self, PyObject *value, void *closure)
{
property_ptr_t property_ptr;
::memcpy((void*)&property_ptr, (const void*)&closure, sizeof(closure));
CLASS_TYPE* p = self->obj;
return pytype_traits_t<RET>::pyobj_to_cppobj(value, p->*property_ptr);
}
};
//! 用于C++ 注册class的工具类,会记录class对应的名称、成员方法、成员变量
class pyclass_regigster_tool_t
{
public:
struct method_info_t
{
PyCFunction func;
string func_name;
string func_real_name;
string doc;
string doc_real;
int args_num;
int option_args_num;
long func_addr;
};
struct property_info_t
{
void* ptr;
string property_name;
string doc;
getter getter_func;
setter setter_func;
};
virtual ~pyclass_regigster_tool_t(){}
typedef PyObject *(*pyobj_alloc_t)(PyTypeObject*, PyObject*, PyObject*);
string class_name;
string class_real_name;
string class_name_with_mod;
string class_reel_name_with_mod;
string inherit_name;
int type_size;
string doc;
int args_num;
int option_args_num;
destructor dector;
initproc init;
pyobj_alloc_t ctor;
//! member functions
PyCFunction delete_func;
vector<method_info_t> methods_info;
//! property
vector<property_info_t> propertys_info;
//! for init module
PyTypeObject pytype_def;
//! method
vector<PyMethodDef> pymethod_def;
//! property
vector<PyGetSetDef> pyproperty_def;
//! 静态类型需要全局记录该类型被注册成神马python 类型
static_pytype_info_t* static_pytype_info;
template<typename FUNC>
pyclass_regigster_tool_t& reg(FUNC f_, const string& func_name_, string doc_ = "")
{
method_info_t tmp;
tmp.func_name = func_name_;
tmp.func_real_name = func_name_ + "_internal_";
tmp.doc = doc_;
tmp.doc_real = "internal use";
tmp.func = (PyCFunction)pyclass_method_gen_t<FUNC>::pymethod;
tmp.args_num = pyclass_method_gen_t<FUNC>::args_num();
tmp.option_args_num = pyclass_method_gen_t<FUNC>::option_args_num();
::memcpy((void*)&tmp.func_addr, (const void*)&f_, sizeof(void*));
methods_info.push_back(tmp);
return *this;
}
template<typename CLASS_TYPE,typename RET>
pyclass_regigster_tool_t& reg_property(RET CLASS_TYPE::* member_, const string& member_name_, string doc_ = "")
{
property_info_t tmp;
::memcpy((void*)&tmp.ptr, (const void*)&member_, sizeof(member_));
tmp.property_name = member_name_;
tmp.doc = doc_;
tmp.getter_func = (getter)pyclass_member_func_gen_t<CLASS_TYPE, RET>::getter_func;
tmp.setter_func = (setter)pyclass_member_func_gen_t<CLASS_TYPE, RET>::setter_func;
propertys_info.push_back(tmp);
return *this;
}
};
class ffpython_t
{
struct reg_info_t
{
reg_info_t():args_num(0),option_args_num(0),func_addr(0){}
int args_num;
int option_args_num;
long func_addr;
PyCFunction func;
string func_name;
string func_impl_name;
string doc;
string doc_impl;
};
public:
ffpython_t()
{
if (!Py_IsInitialized())
Py_Initialize();
}
~ffpython_t()
{
clear_cache_pyobject();
}
static int init_py()
{
Py_Initialize();
return 0;
}
static int final_py()
{
Py_Finalize();
return 0;
}
static int add_path(const string& path_)
{
char buff[1024];
SAFE_SPRINTF(buff, sizeof(buff), "import sys\nif '%s' not in sys.path:\n\tsys.path.append('%s')\n", path_.c_str(), path_.c_str());
PyRun_SimpleString(buff);
return 0;
}
static int run_string(const string& py_)
{
PyRun_SimpleString(py_.c_str());
return 0;
}
static int reload(const string& py_name_)
{
PyObject *pName = NULL, *pModule = NULL;
string err_msg;
pName = PyString_FromString(py_name_.c_str());
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (NULL == pModule)
{
pyops_t::traceback(err_msg);
throw runtime_error(err_msg.c_str());
return -1;
}
PyObject *pNewMod = PyImport_ReloadModule(pModule);
Py_DECREF(pModule);
if (NULL == pNewMod)
{
pyops_t::traceback(err_msg);
throw runtime_error(err_msg.c_str());
return -1;
}
Py_DECREF(pNewMod);
return 0;
}
static int load(const string& py_name_)
{
PyObject *pName = NULL, *pModule = NULL;
string err_msg;
pName = PyString_FromString(py_name_.c_str());
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (NULL == pModule)
{
pyops_t::traceback(err_msg);
throw runtime_error(err_msg.c_str());
return -1;
}
Py_DECREF(pModule);
return 0;
}
//! 注册static function,
template<typename T>
ffpython_t& reg(T func_, const string& func_name_, string doc_ = "")
{
reg_info_t tmp;
tmp.args_num = pyext_func_traits_t<T>::args_num();
tmp.option_args_num = pyext_func_traits_t<T>::option_args_num();
tmp.func = (PyCFunction)pyext_func_traits_t<T>::pyfunc;
tmp.func_name= func_name_;
tmp.func_impl_name = func_name_ + "_internal_";
tmp.doc = doc_;
tmp.doc_impl = string("internal use, please call ") + func_name_;
tmp.func_addr= (long)func_;
m_func_info.push_back(tmp);
return *this;
}
//! 注册c++ class
template<typename T, typename CTOR>
pyclass_regigster_tool_t& reg_class(const string& class_name_, string doc_ = "", string inherit_name_ = "")
{
if (pyclass_base_info_t<T>::pytype_info.class_name.empty() == false)
throw runtime_error("this type has been registed");
pyclass_base_info_t<T>::pytype_info.class_name = class_name_;
//pyclass_base_info_t<T>::pytype_info.mod_name = m_mod_name;
pyclass_base_info_t<T>::pytype_info.total_args_num = pyext_func_traits_t<CTOR>::args_num() +
pyext_func_traits_t<CTOR>::option_args_num();
pyclass_regigster_tool_t tmp;
tmp.class_name = class_name_;
tmp.class_real_name = class_name_ + "_internal_";
tmp.inherit_name = inherit_name_;
tmp.doc = doc_;
tmp.dector = (destructor)pyclass_base_info_t<T>::free_obj;
tmp.init = (initproc)pyclass_ctor_tool_t<T, CTOR>::init_obj;
tmp.ctor = pyclass_base_info_t<T>::alloc_obj;
tmp.type_size = sizeof(typename pyclass_base_info_t<T>::obj_data_t);
tmp.args_num = pyext_func_traits_t<CTOR>::args_num();
tmp.option_args_num = pyext_func_traits_t<CTOR>::option_args_num();
tmp.static_pytype_info = &(pyclass_base_info_t<T>::pytype_info);
//! 注册析构函数,python若不调用析构函数,当对象被gc时自动调用
tmp.delete_func = (PyCFunction)pyclass_base_info_t<T>::release;
m_all_pyclass.push_back(tmp);
return m_all_pyclass.back();
}
//! 将需要注册的函数、类型注册到python虚拟机
int init(const string& mod_name_, string doc_ = "")
{
m_mod_name = mod_name_;
m_mod_doc = doc_;
PyObject* m = init_method();
init_pyclass(m);
return 0;
}
//! 调用python函数,最多支持9个参数
template<typename RET>
RET_V call(const string& mod_name_, const string& func_)
{
pycall_arg_t args(0);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call<RET_V>(mod_name_, func_, args, pyret);
}
template<typename RET, typename ARG1>
RET_V call(const string& mod_name_, const string& func_, const ARG1& a1)
{
pycall_arg_t args(1);
args.add(a1);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call<RET_V>(mod_name_, func_, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2>
RET_V call(const string& mod_name_, const string& func_, const ARG1& a1, const ARG2& a2)
{
pycall_arg_t args(2);
args.add(a1).add(a2);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call<RET_V>(mod_name_, func_, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3>
RET_V call(const string& mod_name_, const string& func_, const ARG1& a1, const ARG2& a2, const ARG3& a3)
{
pycall_arg_t args(3);
args.add(a1).add(a2).add(a3);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call<RET_V>(mod_name_, func_, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4>
RET_V call(const string& mod_name_, const string& func_, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4)
{
pycall_arg_t args(4);
args.add(a1).add(a2).add(a3).add(a4);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call<RET_V>(mod_name_, func_, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5>
RET_V call(const string& mod_name_, const string& func_, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4,
const ARG5& a5)
{
pycall_arg_t args(5);
args.add(a1).add(a2).add(a3).add(a4).add(a5);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call<RET_V>(mod_name_, func_, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6>
RET_V call(const string& mod_name_, const string& func_, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4,
const ARG5& a5,const ARG6& a6)
{
pycall_arg_t args(6);
args.add(a1).add(a2).add(a3).add(a4).add(a5).add(a6);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call<RET_V>(mod_name_, func_, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6, typename ARG7>
RET_V call(const string& mod_name_, const string& func_, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4,
const ARG5& a5,const ARG6& a6,const ARG7& a7)
{
pycall_arg_t args(7);
args.add(a1).add(a2).add(a3).add(a4).add(a5).add(a6).add(a7);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call<RET_V>(mod_name_, func_, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6, typename ARG7,
typename ARG8>
RET_V call(const string& mod_name_, const string& func_, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4,
const ARG5& a5, const ARG6& a6, const ARG7& a7, const ARG8& a8)
{
pycall_arg_t args(8);
args.add(a1).add(a2).add(a3).add(a4).add(a5).add(a6).add(a7).add(a8);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call<RET_V>(mod_name_, func_, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6, typename ARG7,
typename ARG8, typename ARG9>
RET_V call(const string& mod_name_, const string& func_, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4,
const ARG5& a5, const ARG6& a6, const ARG7& a7, const ARG8& a8, const ARG9& a9)
{
pycall_arg_t args(9);
args.add(a1).add(a2).add(a3).add(a4).add(a5).add(a6).add(a7).add(a8).add(a9);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call<RET_V>(mod_name_, func_, args, pyret);
}
//!call python class method begin******************************************************************
template<typename RET>
RET_V obj_call(PyObject* pobj, const string& func_)
{
pycall_arg_t args(0);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_obj_method<RET_V>(pobj, func_, args, pyret);
}
template<typename RET, typename ARG1>
RET_V obj_call(PyObject* pobj, const string& func_, const ARG1& a1)
{
pycall_arg_t args(1);
args.add(a1);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_obj_method<RET_V>(pobj, func_, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2>
RET_V obj_call(PyObject* pobj, const string& func_, const ARG1& a1, const ARG2& a2)
{
pycall_arg_t args(2);
args.add(a1).add(a2);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_obj_method<RET_V>(pobj, func_, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3>
RET_V obj_call(PyObject* pobj, const string& func_, const ARG1& a1, const ARG2& a2, const ARG3& a3)
{
pycall_arg_t args(3);
args.add(a1).add(a2).add(a3);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_obj_method<RET_V>(pobj, func_, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4>
RET_V obj_call(PyObject* pobj, const string& func_, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4)
{
pycall_arg_t args(4);
args.add(a1).add(a2).add(a3).add(a4);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_obj_method<RET_V>(pobj, func_, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5>
RET_V obj_call(PyObject* pobj, const string& func_, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4,
const ARG5& a5)
{
pycall_arg_t args(5);
args.add(a1).add(a2).add(a3).add(a4).add(a5);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_obj_method<RET_V>(pobj, func_, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6>
RET_V obj_call(PyObject* pobj, const string& func_, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4,
const ARG5& a5,const ARG6& a6)
{
pycall_arg_t args(6);
args.add(a1).add(a2).add(a3).add(a4).add(a5).add(a6);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_obj_method<RET_V>(pobj, func_, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6, typename ARG7>
RET_V obj_call(PyObject* pobj, const string& func_, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4,
const ARG5& a5,const ARG6& a6,const ARG7& a7)
{
pycall_arg_t args(7);
args.add(a1).add(a2).add(a3).add(a4).add(a5).add(a6).add(a7);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_obj_method<RET_V>(pobj, func_, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6, typename ARG7,
typename ARG8>
RET_V obj_call(PyObject* pobj, const string& func_, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4,
const ARG5& a5, const ARG6& a6, const ARG7& a7, const ARG8& a8)
{
pycall_arg_t args(8);
args.add(a1).add(a2).add(a3).add(a4).add(a5).add(a6).add(a7).add(a8);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_obj_method<RET_V>(pobj, func_, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6, typename ARG7,
typename ARG8, typename ARG9>
RET_V obj_call(PyObject* pobj, const string& func_, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4,
const ARG5& a5, const ARG6& a6, const ARG7& a7, const ARG8& a8, const ARG9& a9)
{
pycall_arg_t args(9);
args.add(a1).add(a2).add(a3).add(a4).add(a5).add(a6).add(a7).add(a8).add(a9);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_obj_method<RET_V>(pobj, func_, args, pyret);
}
//!call python class method end******************************************************************
//!call python lambad function begin ############################################################
template<typename RET>
RET_V call_lambda(PyObject* pobj)
{
pycall_arg_t args(0);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_lambda<RET_V>(pobj, args, pyret);
}
template<typename RET, typename ARG1>
RET_V call_lambda(PyObject* pobj, const ARG1& a1)
{
pycall_arg_t args(1);
args.add(a1);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_lambda<RET_V>(pobj, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2>
RET_V call_lambda(PyObject* pobj, const ARG1& a1, const ARG2& a2)
{
pycall_arg_t args(2);
args.add(a1).add(a2);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_lambda<RET_V>(pobj, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3>
RET_V call_lambda(PyObject* pobj, const ARG1& a1, const ARG2& a2, const ARG3& a3)
{
pycall_arg_t args(3);
args.add(a1).add(a2).add(a3);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_lambda<RET_V>(pobj, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4>
RET_V call_lambda(PyObject* pobj, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4)
{
pycall_arg_t args(4);
args.add(a1).add(a2).add(a3).add(a4);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_lambda<RET_V>(pobj, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5>
RET_V call_lambda(PyObject* pobj, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4,
const ARG5& a5)
{
pycall_arg_t args(5);
args.add(a1).add(a2).add(a3).add(a4).add(a5);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_lambda<RET_V>(pobj, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6>
RET_V call_lambda(PyObject* pobj, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4,
const ARG5& a5,const ARG6& a6)
{
pycall_arg_t args(6);
args.add(a1).add(a2).add(a3).add(a4).add(a5).add(a6);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_lambda<RET_V>(pobj, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6, typename ARG7>
RET_V call_lambda(PyObject* pobj, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4,
const ARG5& a5,const ARG6& a6,const ARG7& a7)
{
pycall_arg_t args(7);
args.add(a1).add(a2).add(a3).add(a4).add(a5).add(a6).add(a7);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_lambda<RET_V>(pobj, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6, typename ARG7,
typename ARG8>
RET_V call_lambda(PyObject* pobj, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4,
const ARG5& a5, const ARG6& a6, const ARG7& a7, const ARG8& a8)
{
pycall_arg_t args(8);
args.add(a1).add(a2).add(a3).add(a4).add(a5).add(a6).add(a7).add(a8);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_lambda<RET_V>(pobj, args, pyret);
}
template<typename RET, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6, typename ARG7,
typename ARG8, typename ARG9>
RET_V call_lambda(PyObject* pobj, const string& func_, const ARG1& a1, const ARG2& a2, const ARG3& a3, const ARG4& a4,
const ARG5& a5, const ARG6& a6, const ARG7& a7, const ARG8& a8, const ARG9& a9)
{
pycall_arg_t args(9);
args.add(a1).add(a2).add(a3).add(a4).add(a5).add(a6).add(a7).add(a8).add(a9);
pytype_tool_impl_t<RET_V> pyret;
return pycall_t::call_lambda<RET_V>(pobj, args, pyret);
}
//!call python lambad function ennd ############################################################
template<typename RET>
RET_V get_global_var(const string& mod_name_, const string& var_name_)
{
PyObject *pName = NULL, *pModule = NULL;
string err_msg;
pName = PyString_FromString(mod_name_.c_str());
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (NULL == pModule)
{
pyops_t::traceback(err_msg);
throw runtime_error(err_msg.c_str());
}
pytype_tool_impl_t<RET_V> pyret;
PyObject *pvalue = PyObject_GetAttrString(pModule, var_name_.c_str());
Py_DECREF(pModule);