脱发很多之后,我正在寻求帮助.
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
发布评论