将 Python 嵌入 C/C++
Embedding Python in C/C++
为了我的计算机科学高级项目,我正在制作一款益智视频游戏,教人们如何在 Python 中编码。设计的最大部分涉及创建用户可以分类的引人入胜的谜题。然而,拼图设计不是我目前遇到的问题。
虽然我最初的想法是让每个 "problem" 都有一系列预期答案来检查用户的答案,但我学校的 CS 系主任(运行 是高级研讨会)建议我改用嵌入式 Python,他建议既要挑战我,又要使数据更容易嵌入一个简单的解释器,该解释器将根据预期输出检查用户代码输出。
快进四个星期,我学到了很多关于 Python 实际工作原理的知识。我什至让解释器将 简单的 C 字符串 作为 python 代码,比如 print("hello") 或 print("hello/tworld"),只要C字符串中没有白色space。
该代码看起来像
#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();
}
我决定的下一步是获取像 print("hello world") white space 这样的字符串,可读为 Python 代码。
我已经尝试将执行代码函数更改为看起来像
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();
}
当函数像这样更改时编译正常,但是当我输入任何字符串时,简单的或白色 space,程序退出并显示 return 值为 0 并且不打印任何内容python 代码。
我很好奇为什么会这样,我是否在将用户输入转换为足够的 C 字符串以作为 Python 代码读取时遇到问题,或者它不理解 [=14 的声明=] 或 PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)
, I have stared at this documentation, taken notes, diagrammed, and I still get the same exit condition.
我也在想:
- 如果我为 Python 字符串命令声明字典以正确读取代码,或者如果它是
- 如果在简化版本中将我的 Python 文件标志设置为
NULL
而不是 Py_CompileStrngFlags(...)
或 Py_RunStringFlags(...)
,那么这就是导致 return 的原因我遇到的 NULL 值问题。
- 如果我什至使用正确的函数 运行 嵌入 Python 用户输入。
你的程序中不需要C标签。您的问题仅与 C++ 代码有关。首先,
string input;
cin >> input;
读取一个space分隔的单词,与C中的scanf("%s", input);
没有什么不同。您需要使用
getline(cin, input);
下一个问题是input.data()
need not be zero-terminated。您 必须改用 input.c_str()
。
最后:您必须始终检查每个的return值PythonAPI 你调用的函数。没有允许的快捷方式。
为了我的计算机科学高级项目,我正在制作一款益智视频游戏,教人们如何在 Python 中编码。设计的最大部分涉及创建用户可以分类的引人入胜的谜题。然而,拼图设计不是我目前遇到的问题。
虽然我最初的想法是让每个 "problem" 都有一系列预期答案来检查用户的答案,但我学校的 CS 系主任(运行 是高级研讨会)建议我改用嵌入式 Python,他建议既要挑战我,又要使数据更容易嵌入一个简单的解释器,该解释器将根据预期输出检查用户代码输出。
快进四个星期,我学到了很多关于 Python 实际工作原理的知识。我什至让解释器将 简单的 C 字符串 作为 python 代码,比如 print("hello") 或 print("hello/tworld"),只要C字符串中没有白色space。
该代码看起来像
#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();
}
我决定的下一步是获取像 print("hello world") white space 这样的字符串,可读为 Python 代码。
我已经尝试将执行代码函数更改为看起来像
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();
}
当函数像这样更改时编译正常,但是当我输入任何字符串时,简单的或白色 space,程序退出并显示 return 值为 0 并且不打印任何内容python 代码。
我很好奇为什么会这样,我是否在将用户输入转换为足够的 C 字符串以作为 Python 代码读取时遇到问题,或者它不理解 [=14 的声明=] 或 PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)
, I have stared at this documentation, taken notes, diagrammed, and I still get the same exit condition.
我也在想:
- 如果我为 Python 字符串命令声明字典以正确读取代码,或者如果它是
- 如果在简化版本中将我的 Python 文件标志设置为
NULL
而不是Py_CompileStrngFlags(...)
或Py_RunStringFlags(...)
,那么这就是导致 return 的原因我遇到的 NULL 值问题。 - 如果我什至使用正确的函数 运行 嵌入 Python 用户输入。
你的程序中不需要C标签。您的问题仅与 C++ 代码有关。首先,
string input;
cin >> input;
读取一个space分隔的单词,与C中的scanf("%s", input);
没有什么不同。您需要使用
getline(cin, input);
下一个问题是input.data()
need not be zero-terminated。您 必须改用 input.c_str()
。
最后:您必须始终检查每个的return值PythonAPI 你调用的函数。没有允许的快捷方式。