发布时间:2023-08-05 11:30
作者:虚坏叔叔
博客:https://xuhss.com
早餐店不会开到晚上,想吃的人早就来了!
python代码test.py中添加变量打印,这时变量a需要从c++端传递过来
# c++传递的变量(PyRun_SimpleString("a=888");)
print("c++ to python a = ", a)
c++添加变量的定义并传递
// 传递位置要在python代码调用之前
// 给python传递 变量、函数、类、模块(读取一个python模块传递给主模块)
// 传递变量(只能传给主模块__main__)
PyRun_SimpleString("a=888");
运行结果:
python代码test.py中添加变量打印,这时变量count需要从c++端传递过来
print("c++ to python count = ", count)
c++添加变量的定义并传递
// 支持传递其他非_main__模块,空间转给python管理
PyObject_SetAttrString(m, "count", PyLong_FromLong(777));
运行
#include
#include
#include
using namespace std;
int main(int argc, char*argv[])
{
cout << "C++ call Python" << endl;
// 设置Python的Home路径
Py_SetPythonHome(L"./");
// Python初始化解释器
Py_Initialize();
PyObject *m = NULL; // 主模块
try
{
int re = 0;
// 执行Python脚本
re = PyRun_SimpleString("print('Hello world!')");
re = PyRun_SimpleString("print(\"__name__ = \", __name__)");
// 获取主模块
PyObject *key = PyUnicode_FromString("__main__");
m = PyImport_GetModule(key); // 不清理参数,需要手动清理
Py_XDECREF(key);
// 传递位置要在python代码调用之前
// 给python传递 变量、函数、类、模块(读取一个python模块传递给主模块)
// 传递变量(只能传给主模块__main__)
PyRun_SimpleString("a=888");
// 支持传递其他非_main__模块,空间转给python管理
PyObject_SetAttrString(m, "count", PyLong_FromLong(777));
// 执行Python文件
char* filename = "test.py";
FILE * fp = fopen(filename, "r");
if (!fp)
{
throw exception("open file failed");
}
PyRun_AnyFile(fp, filename);
if (re != 0)
{
PyErr_Print();
throw exception("PyRun_AnyFile failed");
}
// 2-1 调用python的变量 python做配置文件
//con = {
// "width":1920,
// "heigth" : 1080,
// "title" : "C++ call Python"
//}
{
// 根据模块和名称获取对象(对象可以是变量、函数和类)
PyObject* conf = PyObject_GetAttrString(m, "conf");
if (!conf) {
throw exception("conf noe find!");
}
PyObject *key = PyUnicode_FromString("width");
int width = PyLong_AsLong(PyDict_GetItem(conf, key));
Py_XDECREF(key);
key = PyUnicode_FromString("height");
int height = PyLong_AsLong(PyDict_GetItem(conf, key));
Py_XDECREF(key);
key = PyUnicode_FromString("title");
wchar_t title[1024] = { 0 };
int size = PyUnicode_AsWideChar(PyDict_GetItem(conf, key), title, 1023);
Py_XDECREF(key);
printf("width=%d height=%d \n", width, height);
wprintf(L"title=%s\n", title);
Py_XDECREF(conf);
}
{
// 获取类
PyObject* TypePy = PyObject_GetAttrString(m, "TypePy");
if (!TypePy) {
throw exception("TypePy noe find!");
}
// 实例化对象 相当于调用构造函数__init__ 传递构造函数的参数NULL
PyObject *obj = PyObject_CallObject(TypePy, NULL);
if (!obj) {
throw exception("obj not Create!");
}
// 调用类成员函数 i(int) s(string)
PyObject *re = PyObject_CallMethod(obj, "test", "is", 2001, "c Para2");
cout << "PyObject_CallMethod return" << PyLong_AsLong(re) << endl;
Py_XDECREF(re);
// 访问成员变量
PyObject* var = PyObject_GetAttrString(obj, "id");
cout << "TypePy.id=" << PyLong_AsLong(var) << endl;
Py_XDECREF(var);
Py_XDECREF(obj);
Py_XDECREF(TypePy);
}
{
// C++调用Python函数
PyObject* Main = PyObject_GetAttrString(m, "Main");
if (Main && PyCallable_Check(Main)) {
// 函数对象和参数 返回对象
if (!PyObject_CallObject(Main, 0))
{
throw exception("PyObject_CallObject Failed");
}
}
Py_XDECREF(Main);
// c++调用Python的函数 list参数和list返回值
PyObject* TestFun = PyObject_GetAttrString(m, "TestFun");
if (TestFun && PyCallable_Check(TestFun)) {
// 参数准备 参数变量是tuple
PyObject *args = PyTuple_New(1); // 参数是元组 只有元组这个一个参数
// 传递的list对象
PyObject *lis = PyList_New(0);
for (int i = 0; i < 5; i++)
PyList_Append(lis, PyLong_FromLong(i + 100));
// 将list写入元组参数列表中
PyTuple_SetItem(args, 0, lis);
// 函数对象和参数的返回值
PyObject* re = PyObject_CallObject(TestFun, args);
Py_XDECREF(args); // lis也在args中销毁
// 处理返回值
if (re)
{
cout << "PyObject_CallObject return" << endl;
int size = PyList_Size(re);
for (int i = 0; i < size; i++)
{
PyObject *val = PyList_GetItem(re, i);
if (!val)
continue;
printf("[%d]", PyLong_AsLong(val));
}
Py_XDECREF(re);
}
}
Py_XDECREF(TestFun);
}
Py_XDECREF(m);
// 清理python
Py_Finalize();
}
catch (const std::exception&ex)
{
cout << ex.what() << endl;// 清理python
Py_XDECREF(m);
Py_Finalize();
}
getchar();
return 0;
}
点赞
收藏
转发
一波哦,博主也支持为铁粉丝制作专属动态壁纸哦~