简体   繁体   English

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

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

i have a problem with my code.我的代码有问题。 i have a python file for the capturing of mavlink messages(i'm using pymavlink library) and i need to create a library for interfacing python results with c/c++ code.我有一个 python 文件用于捕获 mavlink 消息(我正在使用 pymavlink 库),我需要创建一个库来将 python 结果与 c/c++ 代码连接起来。 this is my python code from.py file这是我的 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)

i need to recover the attitude object as PyObject, the result of the last print is:我需要将态度 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}

i have a streaming of messages, so i need to call the connection and the to evaluate the result in a loop.我有消息流,所以我需要调用连接并在循环中评估结果。 so i tried to call the simple python commands as string, to open the connection and then access to the data.所以我尝试将简单的 python 命令调用为字符串,以打开连接然后访问数据。 My C code is:我的 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();

the result of this code is:这段代码的结果是:

<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

i've tried using a return of the object, but it didn't work我已经尝试使用 object 的返回,但它没有用

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

i'm not expert of C/C++, is there a way to obtain the result in the right way?i'm not interested in a string format, i only need a way to use the result as c object我不是 C/C++ 专家,有没有办法以正确的方式获得结果?我对字符串格式不感兴趣,我只需要一种方法将结果用作 c object

i'm using python 3.9, on a raspberry pi, gcc version is 10.2.1.我正在使用 python 3.9,在树莓派上,gcc 版本是 10.2.1。 thank you谢谢你

You want你要

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

Py_eval_input treats it like the Python builtin eval (so what you're running must be an expression rather than a statement, which it is...). Py_eval_input将其视为 Python 内置eval (因此您运行的必须是表达式而不是语句,它是...)。

In contrast, Py_single_input evaluates a single statement, but just returns None because a statement doesn't necessary returns anything.相反, Py_single_input评估单个语句,但只返回None因为语句不需要返回任何东西。 (In Python all expressions are statements, but not all statements are expressions). (Python中所有的表达式都是语句,但不是所有的语句都是表达式)。 It's more akin to exec (but only deals with a single line).它更类似于exec (但只处理一行)。

Using "return(the_connection.messages['ATTITUDE'])" doesn't work because return is specifically designed to appear in a Python function.使用"return(the_connection.messages['ATTITUDE'])"不起作用,因为return专门设计为出现在 Python function 中。

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

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