-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_callpython.cpp
More file actions
103 lines (73 loc) · 2.12 KB
/
cpp_callpython.cpp
File metadata and controls
103 lines (73 loc) · 2.12 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
/*
* =====================================================================================
*
* Filename: python.cpp
*
* Description:
*
* Version: 1.0
* Created: 2017年08月11日 14时46分32秒
* Last Modified: 2017年08月11日 14时46分32秒
* Revision: none
* Compiler: gcc
*
* Author: zt (),
* Organization:
*
* =====================================================================================
*/
//g++ python.cpp -o a -g -lpython2.7
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <python2.7/Python.h>
void Test_Add ( int a, int b )
{
Py_Initialize();
PyRun_SimpleString ( "import sys" );
/* */
PyRun_SimpleString ( "sys.path.append('./')" );
/* 模块不能用test之类常见的名字,python内置test模块优先级最高 */
PyObject* pModule = PyImport_ImportModule ( "test2" );
if ( !pModule )
std::cout << "pModule error\n";
PyObject* pv = PyObject_GetAttrString ( pModule, "Add" );
if ( !pv )
std::cout << "pv error\n";
PyObject* pArg = Py_BuildValue ( "(i,i)", a, b );
if ( !pArg )
std::cout << "pArg error\n";
PyEval_CallObject ( pv, pArg );
Py_Finalize();
}
void Test_Class ( std::string name )
{
Py_Initialize();
PyRun_SimpleString ( "import sys" );
PyRun_SimpleString ( "sys.path.append('./')" );
PyObject* pModule = PyImport_ImportModule ( "test2" );
if ( !pModule )
std::cout << "pModule !\n";
PyObject* pClass = PyObject_GetAttrString ( pModule, "TestClass" );
if ( !pClass )
std::cout << "pClass!\n";
PyObject* pArg = PyTuple_New ( 1 );
PyTuple_SetItem ( pArg, 0, Py_BuildValue ( "s", name.c_str() ) );
if ( !pArg )
std::cout << "pArg!\n";
PyObject* pClassObj = PyEval_CallObject ( pClass, pArg );
if ( !pClassObj )
std::cout << "pClassObj!\n";
PyObject* pFunc = PyObject_GetAttrString ( pClassObj, "printName" );
if ( !pFunc )
std::cout << "pFunc!\n";
PyEval_CallObject ( pFunc, NULL );
Py_Finalize();
}
int main ( int argc, char* argv[] )
{
//Test_Add ( 10, 22 );
Test_Class ( "test class" );
return 0;
}