在 C/C++ 中嵌入 Python

Rya*_*ler 5 c++ python python-c-api

在我的计算机科学高级项目中,我正在制作一款益智视频游戏,该游戏将教人们如何使用 Python 进行编码。设计的最大部分涉及创建用户可以分类的引人入胜的谜题。然而,拼图设计不是我目前遇到的问题。

虽然我最初的想法是让每个“问题”都有一系列预期的答案来检查用户的答案,但我学校的 CS 部门负责人(主持高级研讨会)建议我改用嵌入式 Python,他建议两者都进行挑战me,并使数据更容易嵌入一个简单的解释器,该解释器将根据预期输出检查用户代码输出。

快进四个星期,我学到了很多关于 Python 实际工作原理的知识。我什至让解释器将简单的 C 字符串作为 python 代码,比如 print("hello") 或 print("hello/tworld"),只要 C 字符串中没有空格。

该代码看起来像

#include "pch.h"
#include <iostream>
#include <string>
#include <fstream>
#include <Python.h>

void exec_pycode(const char* code);

int main()
{
    using namespace std;
    Py_Initialize();

    string input;

    cin >> input;
     /*Though the variable gets passed to a const char* definition,
    the pointer is constant over the character, because the pointer ALWAYS points
    to the same function*/
    char const *PyCode = input.data();

    exec_pycode(PyCode);        

    Py_Finalize();
    return EXIT_SUCCESS;    
}        

//The execution of python code as a simple string in C and interpreting in python
void exec_pycode(const char* code)
{
    Py_Initialize();
    PyRun_SimpleString(code);
    Py_Finalize();
}
Run Code Online (Sandbox Code Playgroud)

我决定的下一步是获取包含 print("hello world") 空格之类的字符串,可作为 Python 代码读取。

我试图将 exec 代码函数更改为如下所示

void exec_pycode(const char* code)
{
    Py_Initialize();
    const char *inFile = "FileName";
    PyObject *dict = PyDict_New();
    Py_CompileString(code, inFile, Py_file_input);
    PyRun_String(code, Py_file_input, dict, dict);

    Py_Finalize();
}
Run Code Online (Sandbox Code Playgroud)

当函数像这样更改时,这编译得很好,但是当我输入任何字符串时,简单的或带有空格的,程序退出时返回值为 0 而不打印任何 python 代码。

我很好奇为什么会发生这种情况,我是否在将用户输入转换为足够的 C 字符串以作为 Python 代码读取时遇到问题,或者它不理解Py_CompileString(const char *str, const char *filename, int start)or的声明PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)我已经盯着这个文档,做了笔记,图解,我仍然得到相同的退出条件

我也想问:

  1. 如果我为 Python 字符串命令声明字典以正确读取代码,或者它是
  2. 如果有我的Python文件标志设置NULL的简化版本,而不是Py_CompileStrngFlags(...)Py_RunStringFlags(...)它的是什么导致我遇到NULL问题的返回值。
  3. 如果我什至使用正确的函数来运行带有用户输入的嵌入式 Python。

Ant*_*ala 3

您的程序中不需要 C 标记。您的问题仅与 C++ 代码有关。首先,

string input;

cin >> input;
Run Code Online (Sandbox Code Playgroud)

读取一个空格分隔的单词,与 C 中不同。scanf("%s", input); 您需要使用

getline(cin, input);
Run Code Online (Sandbox Code Playgroud)

下一个问题是不需要input.data()以零终止。您必须使用 input.c_str()它来代替。

最后:您必须始终检查您调用的每个Python API 函数的返回值。没有允许的捷径。

  • 自 C++11 起,`data()` 已以 null 终止。 (4认同)