繁体   English   中英

当 python 代码返回 object 时获取 PyRun_String 的结果

[英]Getting result of PyRun_String when python code returns an object

我的代码有问题。 我有一个 python 文件用于捕获 mavlink 消息(我正在使用 pymavlink 库),我需要创建一个库来将 python 结果与 c/c++ 代码连接起来。 这是我的 python 来自 .py 文件的代码

from pymavlink import mavutil

the_connection = mavutil.mavlink_connection('udpin:localhost:14550')

the_connection.wait_heartbeat()
print("Heartbeat from system (system %u component %u)" % (the_connection.target_system, the_connection.target_component))

while 1:
    
    attitude=the_connection.messages['ATTITUDE']
    print("attitude: ",attitude)

我需要将态度 object 恢复为 PyObject,最后打印的结果是:

attitude:  ATTITUDE {time_boot_ms : 1351674, roll : -0.006938610225915909, pitch : -0.009435104206204414, yaw : 1.8100472688674927, rollspeed : 0.0005244240164756775, pitchspeed : -0.0023000920191407204, yawspeed : 0.0002169199287891388}

我有消息流,所以我需要调用连接并在循环中评估结果。 所以我尝试将简单的 python 命令调用为字符串,以打开连接然后访问数据。 我的 C 代码是:

Py_Initialize();
                
PyRun_SimpleString("from pymavlink import mavutil\n"
                "the_connection = mavutil.mavlink_connection('udpin:localhost:14550')\n"
                "the_connection.wait_heartbeat()\n"
                "print(\"Heartbeat from system (system %u component %u)\" % (the_connection.target_system, the_connection.target_component), flush=True)" );
    
PyObject* main_module=PyImport_AddModule("__main__");
PyObject* pdict = PyModule_GetDict(main_module);
PyObject* pdict_new = PyDict_New();

    
while (1) {
        
    PyObject* pval = PyRun_String("the_connection.messages['ATTITUDE']", Py_single_input, pdict, pdict_new);
    PyObject* repr = PyObject_Str(pval);

    PyObject* str = PyUnicode_AsEncodedString(repr, "utf-8", "~E~");
    const char* bytes = PyBytes_AS_STRING(str);

    PyObject_Print(pval, stdout, 0);
    printf(" end\n");

    Py_XDECREF(repr);
}
    
Py_Finalize();

这段代码的结果是:

<pymavlink.dialects.v20.ardupilotmega.MAVLink_attitude_message object at 0x7fba218220>
None end
<pymavlink.dialects.v20.ardupilotmega.MAVLink_attitude_message object at 0x7fba218220>
None end
<pymavlink.dialects.v20.ardupilotmega.MAVLink_attitude_message object at 0x7fba218220>
None end
<pymavlink.dialects.v20.ardupilotmega.MAVLink_attitude_message object at 0x7fba218220>
None end

我已经尝试使用 object 的返回,但它没有用

PyObject* pval = PyRun_String("return(the_connection.messages['ATTITUDE'])", Py_single_input, pdict, pdict_new);

我不是 C/C++ 专家,有没有办法以正确的方式获得结果?我对字符串格式不感兴趣,我只需要一种方法将结果用作 c object

我正在使用 python 3.9,在树莓派上,gcc 版本是 10.2.1。 谢谢你

你要

PyRun_String("the_connection.messages['ATTITUDE']", Py_eval_input, pdict, pdict_new);

Py_eval_input将其视为 Python 内置eval (因此您运行的必须是表达式而不是语句,它是...)。

相反, Py_single_input评估单个语句,但只返回None因为语句不需要返回任何东西。 (Python中所有的表达式都是语句,但不是所有的语句都是表达式)。 它更类似于exec (但只处理一行)。

使用"return(the_connection.messages['ATTITUDE'])"不起作用,因为return专门设计为出现在 Python function 中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM