{"id":289,"date":"2012-04-03T16:45:55","date_gmt":"2012-04-03T20:45:55","guid":{"rendered":"http:\/\/zhanxw.com\/blog\/?p=289"},"modified":"2012-04-05T21:44:01","modified_gmt":"2012-04-06T01:44:01","slug":"embedding-python-in-cc","status":"publish","type":"post","link":"https:\/\/zhanxw.com\/blog\/2012\/04\/embedding-python-in-cc\/","title":{"rendered":"Embedding Python in C\/C++"},"content":{"rendered":"<p>\u5c06Python\u5d4c\u5165C\/C++<\/p>\n<p>\u9996\u5148\u5d4c\u5165\u548c\u6269\u5c55\u662f\u76f8\u5173\u8054\u4f46\u4e0d\u540c\u7684\u4e24\u4e2a\u6982\u5ff5\uff1a\u5d4c\u5165\u662f\u6307\u5728C\u8bed\u8a00\u4e2d\u4f7f\u7528Python\uff0c\u800c\u6269\u5c55\u662f\u5728Python\u4e2d\u4f7f\u7528C\u8bed\u8a00\uff08\u4ee5library\u7684\u5f62\u5f0f\uff09\u3002<\/p>\n<p>\u5176\u6b21\u5d4c\u5165Python\u7684\u672c\u8d28\u662f\u5d4c\u5165Python \u89e3\u91ca\u5668(Interpretor)\u3002\u56e0\u6b64\u6211\u4eec\u9700\u8981\u8c03\u7528\u76f8\u5e94\u7684Initialize, Finalize\u51fd\u6570\u3002\u53e6\u5916\uff0c\u4e3a\u4e86\u8ba9C\/C++\u8bc6\u522bPython\u76f8\u5173\u7684\u51fd\u6570\uff0c\u6211\u4eec\u8fd8\u9700\u8981#include <Python.h>\u4ee5\u53ca\u76f8\u5e94\u7684linker options\uff1a-Xlinker -export-dynamic -ldl -lrt -lutil<\/p>\n<p>\u53e6\u5916\uff0cPython\u8bed\u53e5\u6709\u4e24\u79cd\uff1astatement \u548c expression\u3002\u6ce8\u610fstatement\u662f\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u3002\u56e0\u6b64Python\u8bed\u8a00\u91cc\u6709exec\u548ceval\u5206\u522b\u5bf9\u5e94\u8fd9\u4e24\u79cd\u60c5\u5f62\u3002\u6700\u672c\u8d28\u7684\u533a\u522b\u662fstatement\u6709\u526f\u4f5c\u7528\uff08side effect\uff09\uff0c\u6bd4\u5982\u4f1a\u628a\u503c\u7ed1\u5b9a\u5230\u4e00\u4e2a\u540d\u79f0\u4e0a\uff0c\u6bd4\u5982\uff1a a = 3 \u3002\u5f53\u6211\u4eec\u7528PyRun_SimpleString(&#8220;a=3&#8221;)\u65f6\uff0c\u8fd9\u79cd\u526f\u4f5c\u7528\u662f\u5728\u5f53\u524d\u7684environment\u4e0b\uff08\u5185\u90e8\u5b9e\u73b0\u662fdict\uff09\u591a\u51fa\u4e00\u4e2a\u53d8\u91cf\uff08dict\u7684key\uff09\uff0c\u540d\u79f0\u662fa\u3002<\/p>\n<p>\u6b64\u5916\u8981\u6ce8\u610fPython\u7684\u6267\u884c\u4ee3\u7801\u662f\u548cenvironment\u76f8\u5173\u7684\uff0c\u6bd4\u5982global\u548clocal\uff0c\u60f3\u7528\u7684\u51fd\u6570\u540d\u79f0\uff08\u4f8b\u5982\uff1adir\uff0cstr\uff0cprint\uff09\uff0c\u53d8\u91cf\u90fd\u662f\u4fdd\u5b58\u5728\u5404\u81ea\u7684environment\u91cc\u3002\u5e73\u65f6\u6211\u4eec\u5199\u7684<code> if name == \"__main__\" <\/code> \u5c31\u662f\u8bf4\u9ed8\u8ba4\u7684\u73af\u5883\u662f\u5728\u6a21\u5757__main__\u91cc\u9762\u3002\u6211\u4eec\u8981\u53d6\u51fa\u9ed8\u8ba4\u7684\u51fd\u6570\u6216\u8005\u53d8\u91cf\uff08Python\u5185\u90e8\u4e0d\u4e25\u683c\u533a\u5206\u8fd9\u4e24\u4e2a\u6982\u5ff5\uff0c\u77e5\u8bc6\u51fd\u6570\u53ef\u4ee5callable\uff09\uff0c\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u4ee3\u7801\uff08\u4ee5\u53d6\u51fadir\u51fd\u6570\u4e3a\u4f8b\uff09\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n    PyObject* main_module =\r\n        PyImport_AddModule(&quot;__main__&quot;);\r\n\r\n\/\/ Get the main module's dictionary                                                                                                                                                  \r\n\/\/ and make a copy of it.                                                                                                                                                            \r\n    PyObject* main_dict =\r\n        PyModule_GetDict(main_module);\r\n    \/\/  pFunc is also a borrowed reference \r\n    PyObject* pFunc = PyDict_GetItemString(pDict, &quot;dir&quot;);\r\n<\/pre>\n<p>\u5d4c\u5165Python\u8fd8\u5e94\u6ce8\u610f\u5185\u5b58\u7684\u4f7f\u7528\u3002\u56e0\u4e3aPython\u4e3b\u8981\u4f7f\u7528\uff08\u53e6\u4e00\u79cd\u662fPython\u7684malloc, free\uff09Reference count\u65b9\u5f0f\u6765\u7ba1\u7406\u65b0\u7684\u53d8\u91cf\uff0c\u6211\u4eec\u5e94\u8bb0\u4f4f\u5728\u83b7\u5f97\u4e00\u4e2aPyObject*\u7c7b\u578b\u7684\u6307\u9488\u4e4b\u540e\uff0c\u7528Py_DECREF\u6216\u8005Py_XDECREF\u6765\u51cf\u5c11reference count \uff08\u6ce8\u610f\uff0c\u7279\u6b8a\u60c5\u51b5\u4e0b\u53d6\u51fa\u7684\u7ed3\u679c\u662f\u4e0d\u80fd\u51cf\u5c11reference count\u7684\uff0c\u6bd4\u5982\u53d6\u51falist\u4e2d\u67d0\u4e2a\u5143\u7d20\uff09\u3002<\/p>\n<p>\u6700\u540e\u7ed9\u4e00\u4e2a\u4f8b\u5b50\u6765\u8bf4\u660e\u600e\u4e48\u5728Python\u91cc\u8ba1\u7b97\u4efb\u4f55\u8868\u8fbe\u5f0fexpression \uff08\u53c2\u8003\u4e86FAQ[1])<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;Python.h&gt;\r\n                                                                                                                                                                                     \r\ndouble checkExpression(const char* formular, double gq, double dp) {\r\n    Py_Initialize();\r\n\r\n    \/\/ Get a reference to the main module.                                                                                                                                               \r\n    PyObject* main_module =\r\n        PyImport_AddModule(&quot;__main__&quot;);\r\n\r\n    \/\/ Get the main module's dictionary                                                                                                                                                  \r\n    \/\/ and make a copy of it.                                                                                                                                                            \r\n    PyObject* main_dict =\r\n        PyModule_GetDict(main_module);\r\n\r\n    char s&#x5B;1024];\r\n    sprintf(s, &quot;GQ=%lf&quot;, gq); \/\/, dp, formular);                                                                                                                                     \r\n    if ( 0 != PyRun_SimpleString(s) ) { \/\/ something wrong happen!                                                                                                                   \r\n        fprintf(stderr, &quot;\\nSomething wrong in assigning GQ\\n&quot;);\r\n        return -1.;\r\n    }\r\n    sprintf(s, &quot;DP=%lf&quot;, dp);\r\n    if ( 0 != PyRun_SimpleString(s) ) { \/\/ something wrong happen!                                                                                                                   \r\n        fprintf(stderr, &quot;\\nSomething wrong in assigning DP\\n&quot;);\r\n        return -1.;\r\n    }\r\n\r\n    PyObject* ret = PyRun_String(formular, Py_eval_input, main_dict, main_dict);\r\n    if (ret == NULL) {\r\n        Py_XDECREF(ret);\r\n        PyErr_Clear();\r\n        return -1.;\r\n    };\r\n    double res;\r\n    if (PyInt_Check(ret)) {\r\n        res = PyLong_AsLong(ret);\r\n    } else if (PyFloat_Check(ret)) {\r\n        res = PyFloat_AS_DOUBLE(ret);\r\n    } else if (PyBool_Check(ret)) {\r\n        res = ret == Py_True;\r\n    }\r\n    Py_XDECREF(ret);\r\n    Py_Finalize();\r\n    return res;\r\n};\r\n\r\n<\/pre>\n<p>\u91cd\u8981\u53c2\u8003\u8d44\u6599<\/p>\n<p>\u30101\u3011 \u6269\u5c55\/\u5d4c\u5165Python\u7684FAQ <a href=\"http:\/\/docs.python.org\/faq\/extending.html\" title=\"Extending\/Embedding FAQ\" target=\"_blank\">Extending\/Embedding FAQ<\/a><\/p>\n<p>\u30102\u3011API \u624b\u518c <a href=\"http:\/\/docs.python.org\/c-api\" title=\"Python\/C API Reference Manual\" target=\"_blank\">Python\/C API Reference Manual<\/a><\/p>\n<p>\u30103\u3011\u5d4c\u5165Python\u7684\u6d41\u7a0b\u6027\u8bf4\u660e <a href=\"http:\/\/docs.python.org\/extending\/embedding.html\" title=\"Embedding Python in Another Application\" target=\"_blank\">Embedding Python in Another Application<\/a><\/p>\n<p>\u30104\u3011\u6269\u5c55Python\u7684\u6d41\u7a0b\u6027\u8bf4\u660e\uff0c\u8fd9\u91cc\u4ecb\u7ecd\u4e86Python\u5e95\u5c42\u7684\u77e5\u8bc6\uff0c\u8fd9\u4e9b\u77e5\u8bc6\u4e0d\u4f1a\u5728&#8221;\u5d4c\u5165Python\u7684\u6d41\u7a0b\u6027\u8bf4\u660e&#8221;\u4e2d\u91cd\u590d\u51fa\u73b0 <a href=\"http:\/\/docs.python.org\/extending\/extending.html\" title=\"Extending Python with C or C++\" target=\"_blank\">Extending Python with C or C++<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c06Python\u5d4c\u5165C\/C++ \u9996\u5148\u5d4c\u5165\u548c\u6269\u5c55\u662f\u76f8\u5173\u8054\u4f46\u4e0d\u540c\u7684\u4e24\u4e2a\u6982\u5ff5\uff1a\u5d4c\u5165\u662f\u6307\u5728C\u8bed\u8a00\u4e2d\u4f7f\u7528Python\uff0c\u800c\u6269\u5c55\u662f\u5728Python\u4e2d\u4f7f\u7528C\u8bed\u8a00\uff08\u4ee5library\u7684\u5f62\u5f0f\uff09\u3002 \u5176\u6b21\u5d4c\u5165Python\u7684\u672c\u8d28\u662f\u5d4c\u5165Python \u89e3\u91ca\u5668(Interpretor)\u3002\u56e0\u6b64\u6211\u4eec\u9700\u8981\u8c03\u7528\u76f8\u5e94\u7684Initialize, Finalize\u51fd\u6570\u3002\u53e6\u5916\uff0c\u4e3a\u4e86\u8ba9C\/C++\u8bc6\u522bPython\u76f8\u5173\u7684\u51fd\u6570\uff0c\u6211\u4eec\u8fd8\u9700\u8981#include \u4ee5\u53ca\u76f8\u5e94\u7684linker options\uff1a-Xlinker -export-dynamic -ldl -lrt -lutil \u53e6\u5916\uff0cPython\u8bed\u53e5\u6709\u4e24\u79cd\uff1astatement \u548c expression\u3002\u6ce8\u610fstatement\u662f\u6ca1\u6709\u8fd4\u56de\u503c\u7684\u3002\u56e0\u6b64Python\u8bed\u8a00\u91cc\u6709exec\u548ceval\u5206\u522b\u5bf9\u5e94\u8fd9\u4e24\u79cd\u60c5\u5f62\u3002\u6700\u672c\u8d28\u7684\u533a\u522b\u662fstatement\u6709\u526f\u4f5c\u7528\uff08side effect\uff09\uff0c\u6bd4\u5982\u4f1a\u628a\u503c\u7ed1\u5b9a\u5230\u4e00\u4e2a\u540d\u79f0\u4e0a\uff0c\u6bd4\u5982\uff1a a = 3 \u3002\u5f53\u6211\u4eec\u7528PyRun_SimpleString(&#8220;a=3&#8221;)\u65f6\uff0c\u8fd9\u79cd\u526f\u4f5c\u7528\u662f\u5728\u5f53\u524d\u7684environment\u4e0b\uff08\u5185\u90e8\u5b9e\u73b0\u662fdict\uff09\u591a\u51fa\u4e00\u4e2a\u53d8\u91cf\uff08dict\u7684key\uff09\uff0c\u540d\u79f0\u662fa\u3002 \u6b64\u5916\u8981\u6ce8\u610fPython\u7684\u6267\u884c\u4ee3\u7801\u662f\u548cenvironment\u76f8\u5173\u7684\uff0c\u6bd4\u5982global\u548clocal\uff0c\u60f3\u7528\u7684\u51fd\u6570\u540d\u79f0\uff08\u4f8b\u5982\uff1adir\uff0cstr\uff0cprint\uff09\uff0c\u53d8\u91cf\u90fd\u662f\u4fdd\u5b58\u5728\u5404\u81ea\u7684environment\u91cc\u3002\u5e73\u65f6\u6211\u4eec\u5199\u7684 if name == &#8220;__main__&#8221; \u5c31\u662f\u8bf4\u9ed8\u8ba4\u7684\u73af\u5883\u662f\u5728\u6a21\u5757__main__\u91cc\u9762\u3002\u6211\u4eec\u8981\u53d6\u51fa\u9ed8\u8ba4\u7684\u51fd\u6570\u6216\u8005\u53d8\u91cf\uff08Python\u5185\u90e8\u4e0d\u4e25\u683c\u533a\u5206\u8fd9\u4e24\u4e2a\u6982\u5ff5\uff0c\u77e5\u8bc6\u51fd\u6570\u53ef\u4ee5callable\uff09\uff0c\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u4ee3\u7801\uff08\u4ee5\u53d6\u51fadir\u51fd\u6570\u4e3a\u4f8b\uff09\uff1a PyObject* main_module = PyImport_AddModule(&quot;__main__&quot;); \/\/ Get the main module&#8217;s dictionary \/\/ and make a copy of it. PyObject* main_dict = PyModule_GetDict(main_module); \/\/ pFunc is also a borrowed reference PyObject* pFunc [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-289","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/comments?post=289"}],"version-history":[{"count":0,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/289\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/media?parent=289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/categories?post=289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/tags?post=289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}