嵌入Python 3

编程入门 行业动态 更新时间:2024-10-07 06:38:30
本文介绍了嵌入Python 3-没有内置函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

脱发很多之后,我正在寻求帮助.

After much hair loss, I'm looking for help.

我将Python 3.3嵌入到一个简单的应用程序中.一个不寻常的方面是Python不在路上,但似乎一切正常.但是由于某种原因,什么也无法执行.

I'm embedding Python 3.3 into a simple app. One unusual aspect is Python isn't on the path, but it all seems to load OK. But for some reason, nothing can be executed.

下面是一个显示错误的小示例程序:

Below is a small example program that shows the error:

编辑:我知道引用计数太可怕了-这只是一个简单的例子.

EDIT: I know the reference counting is horrible -- this is just a simple example.

SetDllDirectory(L"D:\\dev\\python\\python33"); //so Python33.dll can be delay-loaded since not in the path Py_Initialize(); PyObject* pGlobals = PyDict_New(); if (PyDict_GetItemString(pGlobals, "__builtins__") == NULL) { PyObject* pMod = PyImport_ImportModule("builtins"); <-- always succeeds if(NULL != pMod) PyDict_SetItemString(pGlobals, "__builtins__", pMod); } PyObject* pResult = PyRun_String("import sys", 0, pGlobals, pGlobals); <-- always fails if (PyErr_Occurred()) { //PyErr_Fetch returns: //<class 'SyntaxError'> //('invalid syntax', ('<string>', 1, 6, 'import sys')) }

我尝试了多种导入和定义内置插件的方法,包括如下所示的各种尝试:

I have tried a variety of ways to import and define builtins, including various attempts shown below:

PyObject* pMod = PyImport_ImportModule("builtins"); PyDict_SetItemString(pGlobals, "builtins", pMod); PyDict_SetItemString(pGlobals, "__builtins__", pMod); PyDict_SetItemString(pGlobals, "__builtins__", PyEval_GetBuiltins()); PyDict_SetItemString(pGlobals, "builtins", PyEval_GetBuiltins());

它们都不起作用-每个错误都相同.

None of them work -- the error is identical with each.

我做错了什么?与路径相关吗?关于内建函数的事情我做错了吗?

What am I doing wrong? Is it path related? Something about builtins that I'm doing wrong?

推荐答案

尝试将开始符号设置为 Py_file_input 并使用 PyEval_GetBuiltins .

Try it with the start symbol set to Py_file_input and use PyEval_GetBuiltins.

要设置的正确dict键为"__builtins__".

the correct dict key to set is "__builtins__".

更多推荐

嵌入Python 3

本文发布于:2023-11-30 15:31:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650518.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!