{"id":559,"date":"2025-01-18T23:03:43","date_gmt":"2025-01-18T15:03:43","guid":{"rendered":"http:\/\/39.104.51.85\/?p=559"},"modified":"2025-01-18T23:35:57","modified_gmt":"2025-01-18T15:35:57","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8c%e9%87%8c%e8%b0%83%e7%94%a8python%e8%af%a6%e7%bb%86%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"http:\/\/39.104.51.85\/index.php\/2025\/01\/18\/%e5%a6%82%e4%bd%95%e5%9c%a8c%e9%87%8c%e8%b0%83%e7%94%a8python%e8%af%a6%e7%bb%86%e6%95%99%e7%a8%8b\/","title":{"rendered":"\u5982\u4f55\u5728C++\u91cc\u8c03\u7528python\u8be6\u7ec6\u6559\u7a0b"},"content":{"rendered":"\n<p>\u6700\u8fd1\u5728\u505a\u6709\u5173\u65e0\u4eba\u673a\u89c6\u89c9\u7684\u9879\u76ee\uff0c\u7531\u4e8esuperpoint\u7f51\u7edc\u548cros\u7cfb\u7edf\u7684\u8054\u52a8\u6bd4\u8f83\u56f0\u96be\u590d\u6742\uff0c\u6545\u6700\u540e\u51b3\u5b9a\u76f4\u63a5\u5c06superpoint\u7684\u4ee3\u7801\u96c6\u6210\u5230C++\u91cc\uff0c\u7528C++\u76f4\u63a5\u53bb\u8c03\u7528\uff0c\u7701\u53bb\u4e86\u8282\u70b9\u4e4b\u95f4\u901a\u4fe1\u7684\u7e41\u7410\uff0c\u6545\u5728\u6b64\u8bb0\u5f55\u4e00\u4e0b\u6211\u4e00\u70b9\u4e00\u70b9\u6478\u7d22\u7684\u5386\u7a0b\u3002<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_69_1 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >\u5185\u5bb9<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/39.104.51.85\/index.php\/2025\/01\/18\/%e5%a6%82%e4%bd%95%e5%9c%a8c%e9%87%8c%e8%b0%83%e7%94%a8python%e8%af%a6%e7%bb%86%e6%95%99%e7%a8%8b\/#%E4%BB%80%E4%B9%88%E6%98%AFPyObject%EF%BC%9F\" title=\"\u4ec0\u4e48\u662fPyObject\uff1f\">\u4ec0\u4e48\u662fPyObject\uff1f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/39.104.51.85\/index.php\/2025\/01\/18\/%e5%a6%82%e4%bd%95%e5%9c%a8c%e9%87%8c%e8%b0%83%e7%94%a8python%e8%af%a6%e7%bb%86%e6%95%99%e7%a8%8b\/#python%E5%88%9D%E5%A7%8B%E5%8C%96\" title=\"python\u521d\u59cb\u5316\">python\u521d\u59cb\u5316<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/39.104.51.85\/index.php\/2025\/01\/18\/%e5%a6%82%e4%bd%95%e5%9c%a8c%e9%87%8c%e8%b0%83%e7%94%a8python%e8%af%a6%e7%bb%86%e6%95%99%e7%a8%8b\/#python%E8%B0%83%E7%94%A8%E7%B1%BB%E5%8F%8A%E5%8F%82%E6%95%B0%E6%9E%84%E5%BB%BA\" title=\"python\u8c03\u7528\u7c7b\u53ca\u53c2\u6570\u6784\u5efa\">python\u8c03\u7528\u7c7b\u53ca\u53c2\u6570\u6784\u5efa<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/39.104.51.85\/index.php\/2025\/01\/18\/%e5%a6%82%e4%bd%95%e5%9c%a8c%e9%87%8c%e8%b0%83%e7%94%a8python%e8%af%a6%e7%bb%86%e6%95%99%e7%a8%8b\/#%E5%8F%98%E9%87%8F%E4%B8%8E%E8%99%9A%E6%8B%9F%E8%B5%84%E6%BA%90%E7%9A%84%E9%87%8A%E6%94%BE\" title=\"\u53d8\u91cf\u4e0e\u865a\u62df\u8d44\u6e90\u7684\u91ca\u653e\">\u53d8\u91cf\u4e0e\u865a\u62df\u8d44\u6e90\u7684\u91ca\u653e<\/a><\/li><\/ul><\/nav><\/div>\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E4%BB%80%E4%B9%88%E6%98%AFPyObject%EF%BC%9F\"><\/span>\u4ec0\u4e48\u662fPyObject\uff1f<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u5728\u4e00\u5207\u7684\u5f00\u59cb\uff0c\u4f60\u52a1\u5fc5\u8981\u77e5\u9053\u4ec0\u4e48\u662fPyObject\uff0c\u7b80\u5355\u6765\u8bf4\uff0c\u4e00\u5207python\u91cc\u7684\u4e1c\u897f\u90fd\u662fPyObject\uff0c\u65e0\u8bba\u4f60\u662f\u60f3\u8981\u4f20\u53c2\uff0c\u8fd8\u662f\u63a5\u53d7\u8fd4\u56de\u503c\uff0c\u8fd8\u662f\u8c03\u7528\u67d0\u4e2a\u51fd\u6570\uff0c\u67d0\u4e2a\u7c7b\u7b49\u7b49\uff0c\u4ed6\u4eec\u90fd\u662fPyObject\uff0c\u4f60\u4e5f\u5fc5\u987b\u7528\u4e00\u4e2aPyObject\u7c7b\u578b\u7684\u6307\u9488\u6765\u63a5\u53d7\u6216\u4f20\u9012\u4ed6\u4eec\uff0c\u4e0b\u9762\u7ed9\u51faPyObject\u7684\u5b9a\u4e49<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ object.h\n\/* Nothing is actually declared to be a PyObject, but every pointer to\n * a Python object can be cast to a PyObject*.  This is inheritance built\n * by hand.  Similarly every pointer to a variable-size Python object can,\n * in addition, be cast to PyVarObject*.\n *\/\nstruct _object {\n    _PyObject_HEAD_EXTRA\n    Py_ssize_t ob_refcnt;\n    PyTypeObject *ob_type;\n};\n\/\/ pytypedefs.h\ntypedef struct _object PyObject;\n\n\/\/ Include\/pytypedefs.h\ntypedef struct _typeobject PyTypeObject;\n\/\/ Include\/cpython\/object.h\nstruct _typeobject {\n    PyObject_VAR_HEAD\n    const char *tp_name; \n    Py_ssize_t tp_basicsize, tp_itemsize; \n    destructor tp_dealloc;\n    Py_ssize_t tp_vectorcall_offset;\n    getattrfunc tp_getattr;\n    setattrfunc tp_setattr;\n    PyAsyncMethods *tp_as_async; \n                                    \n    reprfunc tp_repr;\n    PyNumberMethods *tp_as_number;\n    PySequenceMethods *tp_as_sequence;\n    PyMappingMethods *tp_as_mapping;\n    hashfunc tp_hash;\n    ternaryfunc tp_call;\n    reprfunc tp_str;\n    getattrofunc tp_getattro;\n    setattrofunc tp_setattro;\n    PyBufferProcs *tp_as_buffer;\n    unsigned long tp_flags;\n    const char *tp_doc; \n    traverseproc tp_traverse;\n    inquiry tp_clear;\n    richcmpfunc tp_richcompare;\n    Py_ssize_t tp_weaklistoffset;\n    getiterfunc tp_iter;\n    iternextfunc tp_iternext;\n    PyMethodDef *tp_methods;\n    PyMemberDef *tp_members;\n    PyGetSetDef *tp_getset;\n    PyTypeObject *tp_base;\n    PyObject *tp_dict;\n    descrgetfunc tp_descr_get;\n    descrsetfunc tp_descr_set;\n    Py_ssize_t tp_dictoffset;\n    initproc tp_init;\n    allocfunc tp_alloc;\n    newfunc tp_new;\n    freefunc tp_free; \n    inquiry tp_is_gc; \n    PyObject *tp_bases;\n    PyObject *tp_mro; \n    PyObject *tp_cache; \n    void *tp_subclasses;  \n    PyObject *tp_weaklist; \n    destructor tp_del;\n    unsigned int tp_version_tag;\n    destructor tp_finalize;\n    vectorcallfunc tp_vectorcall;\n    unsigned char tp_watched;\n};<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d\u4f60\u9700\u8981\u91cd\u70b9\u5173\u6ce8\u7684\u662fob_type\u8fd9\u4e2a\u6307\u9488\uff0c\u6211\u4eec\u540e\u7eed\u5e38\u5e38\u7528cout&lt;&lt;p->ob_type->tp_name\u6765\u8f93\u51fa\u8fd9\u4e2a\u6210\u5458\u7c7b\u578b\u7684\u540d\u79f0\uff0c\u597d\u5e2e\u52a9\u6211\u4eec\u5224\u65ad\u63a5\u4e0b\u6765\u5e94\u8be5\u4f7f\u7528\u4ec0\u4e48\u7c7b\u578b\u5bf9\u5e94\u7684\u5904\u7406\u51fd\u6570\uff0c\u6bd4\u5982list\u548ctuple\u5c31\u662f\u5b8c\u5168\u4e24\u79cd\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\uff0c\u8981\u8c03\u7528\u4e24\u79cd\u6570\u636e\u7c7b\u578b\u6765\u5904\u7406<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"python%E5%88%9D%E5%A7%8B%E5%8C%96\"><\/span>python\u521d\u59cb\u5316<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>C++\u8c03\u7528python\u7684\u672c\u8d28\u539f\u7406\u5176\u5b9e\u662f\u542f\u52a8\u4e00\u4e2apython\u865a\u62df\u673a\uff0c\u6240\u4ee5\u5728\u8c03\u7528python\u4ee3\u7801\u4e4b\u524d\u9700\u8981\u5148\u5bf9\u8fd9\u4e2a\u865a\u62df\u673a\u8fdb\u884c\u4e00\u4e0b\u521d\u59cb\u5316<\/p>\n\n\n\n<p>\u9996\u5148\u8981\u5305\u542b\u4e00\u4e0bpython\u7684\u5934\u6587\u4ef6\uff0c\u8fd9\u4e2a\u5934\u6587\u4ef6\u53ea\u8981\u4f60\u4e0b\u8f7d\u4e86python\u5c31\u662f\u9ed8\u8ba4\u90fd\u6709\u7684\uff0c\u4ed6\u7684\u8def\u5f84\u662f\/usr\/include\/pythonx.x(\u4f60\u7684\u7248\u672c)\/Python.h\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\u53ea\u9700\u8981#include&lt;Python.h>\u5c31\u53ef\u4ee5\u4e86\uff0c\u4f46\u662f\u5982\u679c\u4f60\u6709\u5f88\u591a\u4e2apython\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u6df7\u6dc6\uff0c\u8fd9\u4e2a\u65f6\u5019\u5c31\u5efa\u8bae\u4f60\u7528\u4e00\u4e0b\u7edd\u5bf9\u8def\u5f84\u6765\u6307\u5b9apython\u7684\u5934\u6587\u4ef6<\/p>\n\n\n\n<p>\u4e00\u822c\u60c5\u51b5\u4e0b\u521d\u59cb\u5316\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include&lt;Python.h>\nint main()\n{\n    Py_initlize();\n    import_array();\/\/\u62a5\u9519\u7684\u8bdd\u5c31\u628aimport_array\u66ff\u6362\u6210_import_array\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u8bf4\u4e00\u4e0b\u8fd9\u4e2aimport_array\u7684\u4e8b\u3002\u4ed6\u662f\u5bf9array\u6570\u7ec4\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u4e0d\u7ba1\u4f60\u540e\u9762\u7528\u4e0d\u7528\u5230array\u90fd\u8981\u8c03\u7528\u4e00\u4e0b\u8fd9\u4e2a\u51fd\u6570\uff0c\u4f46\u662f\u6211\u5728\u5199\u4ee3\u7801\u7684\u65f6\u5019\u53d1\u73b0\u76f4\u63a5\u8c03\u7528import_array\u4f1a\u62a5\u9519\uff0c\u7f51\u4e0a\u67e5\u4e86\u4e00\u4e0b\u8bf4\u662fimport_array\u662f\u4e00\u4e2a\u5b8f\u5b9a\u4e49\uff0c\u6709\u4e00\u4e2a\u8fd4\u56de\u503c\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u8fd4\u56de\u503c\u5bfc\u81f4\u4e86\u95ee\u9898\uff0c\u800cimport_array\u662f\u8c03\u7528\u4e86_import_array\u4f5c\u4e3a\u6838\u5fc3\u5224\u65ad\u903b\u8f91\u7684\uff0c\u6240\u4ee5\u53ef\u4ee5\u7528_import_array\u8fdb\u884c\u4ee3\u66ff\uff0c\u4e5f\u53ef\u4ee5\u4fee\u6539\u4e00\u4e0bimport_array\u7684\u5b8f\u5b9a\u4e49\uff0c\u8ba9\u4ed6\u4e0d\u8981\u6709\u8fd4\u56de\u503c\uff0c\u5177\u4f53\u64cd\u4f5c\u7f51\u4e0a\u6709\u5f88\u591a\uff0c\u8bfb\u8005\u5982\u679c\u9047\u5230\u53ef\u4ee5\u81ea\u884c\u67e5\u9605\u4e00\u4e0b<\/p>\n\n\n\n<p>\u63a5\u7740\u9700\u8981\u6dfb\u52a0\u4e00\u4e0b\u6587\u4ef6\u8def\u5f84\uff0c\u8fd9\u4e2a\u8def\u5f84\u5c31\u662f\u4f60\u60f3\u8981\u8c03\u7528\u7684python\u7a0b\u5e8f\u7684\u7edd\u5bf9\u8def\u5f84<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PyRun_SimpleString(\"import sys\");\nPyRun_SimpleString(\"sys.path.append('your route\/')\");\/\/\u4f60\u8981\u8c03\u7528\u7684python\u7a0b\u5e8f\u7684\u8def\u5f84\nPyRun_SimpleString(\"print('route append successfully')\");<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u7684PyRun_SimpleString\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u5b57\u9762\u610f\u601d\u7406\u89e3\uff0c\u6211\u89c9\u5f97\u5e94\u8be5\u6ca1\u6709\u4ec0\u4e48\u5f88\u96be\u7406\u89e3\u7684\u5730\u65b9\uff0c\u503c\u5f97\u6ce8\u610f\u7684\u5c31\u662f\uff0cpython\u6b63\u5e38\u662f\u4e0d\u533a\u5206\u5355\u5f15\u53f7\u548c\u53cc\u5f15\u53f7\u7684\uff0c\u4f46\u5728C++\u91cc\u5982\u679c\u4f60\u60f3\u8c03\u7528python\u5c31\u53ea\u80fd\u5728\u53cc\u5f15\u53f7\u91cc\u4f7f\u7528\u5355\u5f15\u53f7\uff0c\u4e0d\u80fd\u6df7\u6dc6<\/p>\n\n\n\n<p>\u6700\u540e\u9009\u62e9\u4f60\u60f3\u6253\u5f00\u7684py\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u4e0d\u9700\u8981\u5e26py<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PyObject *pmodule=PyImport_ImportModule(\"demo\");\/\/\u6253\u5f00\u4f60\u60f3\u8981\u6267\u884c\u7684python\u7a0b\u5e8f<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"python%E8%B0%83%E7%94%A8%E7%B1%BB%E5%8F%8A%E5%8F%82%E6%95%B0%E6%9E%84%E5%BB%BA\"><\/span>python\u8c03\u7528\u7c7b\u53ca\u53c2\u6570\u6784\u5efa<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u7531\u4e8e\u672c\u9879\u76ee\u6ca1\u6709\u7528\u5230python\u51fd\u6570\u7684\u8c03\u7528\uff0c\u7528\u7684\u90fd\u662f\u7c7b\u6210\u5458\u548c\u65b9\u6cd5\u7684\u8c03\u7528\uff0c\u6545\u6709\u5173\u51fd\u6570\u7684\u8c03\u7528\u4e0d\u591a\u8d58\u8ff0\uff0c\u6709\u9700\u8981\u7684\u8bfb\u8005\u53ef\u4ee5\u4e0a\u7f51\u4e0a\u67e5\u9605\uff0c\u7f51\u4e0a\u6709\u5f88\u591a<\/p>\n\n\n\n<p>\u8981\u8c03\u7528\u4e00\u4e2a\u7c7b\uff0c\u9996\u5148\u9700\u8981\u627e\u5230\u8fd9\u4e2a\u7c7b\uff0c\u5e76\u5c06\u5176\u8fd4\u56de\u503c\u5b58\u5728\u4e00\u4e2aPyObject\u7c7b\u578b\u7684\u6307\u9488\u4e2d\uff0c\u6bd4\u5982\u6211\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PyObject *superpointfrontend=PyObject_GetAttrString(pmodule,\"SuperPointFrontend\");\/\/\u627e\u5230superpointfrontend\u8fd9\u4e2a\u7c7b\uff0c\u5e76\u4f20\u56de\u6307\u9488<\/code><\/pre>\n\n\n\n<p>pmodule\u5c31\u662f\u524d\u9762\u6307\u5411\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u6307\u9488<\/p>\n\n\n\n<p>\u627e\u5230\u8fd9\u4e2a\u7c7b\u7684\u8bdd\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u8981\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\u4e86\uff0c\u521d\u59cb\u5316\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u4e0d\u53ef\u907f\u514d\u7684\u8981\u4f20\u5165\u53c2\u6570\uff0c\u6240\u4ee5\u8fd9\u91cc\u8bf4\u4e00\u4e0b\u5982\u4f55\u6784\u5efa\u4f20\u5165\u53c2\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PyObject *argofsuperpoint=Py_BuildValue(\"(siddi)\",weights_path,nms_dist,conf_thresh,nn_thresh,cuda);<\/code><\/pre>\n\n\n\n<p>\u5229\u7528Py_BuildValue\u51fd\u6570\uff0c\u5c06\u8fd4\u56de\u503c\u4f20\u56de\u4e00\u4e2aPyObject\u91cc\uff0c\u5b83\u7684\u51fd\u6570\u539f\u578b\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PyObject* Py_BuildValue(const char *format, ...);<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2dformat\u662f\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u7528\u4e8e\u6307\u5b9a\u540e\u9762\u7684\u53c2\u6570\u7c7b\u578b\uff0c\u5bf9\u5e94\u5173\u7cfb\u5982\u4e0b<\/p>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<p><code>\"i\"<\/code>: \u6574\u6570<\/p>\n\n\n\n<p><code>\"s\"<\/code>: \u5b57\u7b26\u4e32\uff08<code>const char*<\/code>\uff09<\/p>\n\n\n\n<p><code>\"d\"<\/code>: \u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570<\/p>\n\n\n\n<p><code>\"f\"<\/code>: \u5355\u7cbe\u5ea6\u6d6e\u70b9\u6570<\/p>\n\n\n\n<p><code>\"O\"<\/code>: \u6307\u5411\u00a0<code>PyObject<\/code>\u00a0\u7684\u6307\u9488<\/p>\n\n\n\n<p><code>\"()\"<\/code>: \u5143\u7ec4<\/p>\n\n\n\n<p><code>\"{}\"<\/code>: \u5b57\u5178<\/p>\n\n\n\n<p><code>\"[]\"<\/code>: \u5217\u8868<\/p>\n<\/div>\n\n\n\n<p>\u540e\u9762\u662f\u53ef\u53d8\u53c2\u6570\uff0c\u53c2\u6570\u7684\u6570\u91cf\u6839\u636e\u4f60\u8f93\u5165\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u51b3\u5b9a\uff0c\u6240\u4ee5\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\uff08siddi\uff09\u5c31\u8868\u793a\u6211\u8981\u4f20\u5165\u4e00\u4e2a\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u5b57\u7b26\u4e32\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u662fint\uff0c\u7b2c\u4e09\u3001\u56db\u4e2a\u5143\u7d20\u90fd\u662fdouble\uff0c\u6700\u540e\u662f\u4e00\u4e2aint\u7684\u5143\u7ec4\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>\u6784\u5efa\u597d\u53c2\u6570\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u6109\u5feb\u7684\u8c03\u7528\u5e76\u521d\u59cb\u5316\u7c7b\u6210\u5458\u4e86\uff0c\u5229\u7528PyEval_CallObject\u51fd\u6570\u5373\u53ef<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PyObject *fe=PyEval_CallObject(superpointfrontend,argofsuperpoint);<\/code><\/pre>\n\n\n\n<p>\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6307\u5411\u8fd9\u4e2a\u7c7b\u7684\u6307\u9488\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u6307\u5411\u53c2\u6570\u7684\u6307\u9488\u3002\u7528\u4e00\u4e2aPyObject\u6765\u63a5\u53d7\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<p>\u63a5\u7740\u5c31\u8981\u8c03\u7528\u7c7b\u7684\u65b9\u6cd5\u4e86\uff0c\u5229\u7528\u7684\u662fPyObject_CallMethod\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u7684\u4f20\u53c2\u6709\u4e24\u79cd\u65b9\u5f0f\uff0c\u6211\u4f1a\u5728\u4e0b\u9762\u8bf4\u660e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PyObject *left=PyObject_CallMethod(fe,\"run\",\"(O)\",pyimg0);<\/code><\/pre>\n\n\n\n<p>\u6765\u770b\u4e00\u4e0b\u4ed6\u7684\u51fd\u6570\u539f\u578b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PyObject* PyObject_CallMethod(PyObject *o, const char *name, const char *format, ...);<\/code><\/pre>\n\n\n\n<p>\u7b2c\u4e00\u4e2a\u53c2\u6570\u5c31\u662f\u6307\u5411\u7c7b\u7684\u6307\u9488\uff0c\u8fd9\u4e2a\u6307\u9488\u662f\u4f60\u521d\u59cb\u5316\u5b8c\u4e4b\u540e\u7684\u6307\u9488\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4f60\u60f3\u8981\u8fd0\u884c\u7684\u65b9\u6cd5\u7684\u540d\u5b57\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u51b3\u5b9a\u4e86\u5982\u4f55\u4f20\u53c2\uff0c\u540e\u9762\u7684\u90fd\u662f\u53ef\u53d8\u53c2\u6570\uff0c\u540c\u4e0a\u9762\u6240\u8bf4\u4e00\u6837<\/p>\n\n\n\n<p>\u7b2c\u4e00\u79cd\u65b9\u5f0f\uff0c\u5c31\u662f\u5229\u7528\u4e0a\u9762\u6784\u9020\u53c2\u6570\u7684\u65b9\u6cd5\uff0c\u5c06\u53c2\u6570\u6784\u9020\u597d\u4f5c\u4e3a\u4e00\u4e2aPyObject\u4f20\u5165\uff0c\u4f8b\u5b50\u6240\u5c55\u793a\u7684\u65b9\u6cd5\u5c31\u662f\u8fd9\u79cd\u65b9\u6cd5\uff0c\u201cpyimg0\u201d\u8fd9\u4e2a\u53c2\u6570\u63d0\u524d\u88ab\u6211\u6784\u9020\u597d\u4e86<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u79cd\u65b9\u6cd5\uff0c\u5c31\u662f\u76f4\u63a5\u50cfPy_BuildValue\u51fd\u6570\u4e00\u6837\u4f20\u53c2\uff0c\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u76f4\u63a5\u6307\u5b9a\u8981\u4f20\u7684\u53c2\u6570\uff0c\u6bd4\u5982<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PyObject *show=PyObject_CallMethod(test,\"chuancan\",\"sid\",\"hahaha\",100,3.1415926);<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6837\u4e5f\u662fOK\u7684<\/p>\n\n\n\n<p>\u8c03\u7528\u5b8c\u6bd5\u4e4b\u540e\uff0c\u5c31\u8be5\u628a\u8fd4\u56de\u7684\u6570\u636e\u60f3\u529e\u6cd5\u8f6c\u6362\u6210C++\u7684\u683c\u5f0f\uff0c\u7b80\u5355\u8fd4\u56de\u4e00\u4e2a\u6570\u8fd8\u597d\u8bf4\uff0c\u5982\u679c\u662f\u8fd4\u56de\u4e00\u4e2alist\uff0c\u6216\u8005tuple\u5c31\u6bd4\u8f83\u9ebb\u70e6\u4e86\uff0c\u6240\u4ee5\u4e0b\u9762\u8bb2\u89e3\u4e00\u4e0b\u9762\u5bf9\u8fd9\u79cd\u60c5\u51b5\u8be5\u600e\u4e48\u529e<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eeccout&lt;&lt;p->ob_type->tp_name\u770b\u4e00\u4e0b\u8fd4\u56de\u7684\u6570\u636e\u7c7b\u578b\u662f\u4ec0\u4e48\uff0c\u8fd9\u91cc\u6211\u8f93\u51fa\u5b8c\u4e4b\u540e\u53d1\u73b0\u8fd4\u56de\u7684\u662f\u4e00\u4e2atuple\uff0c\u6839\u636epython\u4ee3\u7801\u6211\u77e5\u9053\u8fd4\u56de\u503c\u4e00\u5171\u6709\u4e09\u4e2a\uff0c\u6240\u4ee5\u8fd9\u4e2atuple\u91cc\u9762\u6709\u4e09\u4e2a\u5143\u7d20\u3002<\/p>\n\n\n\n<p>\u786e\u5b9a\u662ftuple\u4e86\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u9700\u8981\u7528\u5230PyTuple_GetItem\u4ece\u8fd4\u56de\u503c\u91cc\u9762\u63d0\u53d6\u6211\u60f3\u8981\u7684\u6570\u636e\u4e86\uff0c\u51fd\u6570\u539f\u578b\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PyObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos);<\/code><\/pre>\n\n\n\n<p>\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6211\u8c03\u7528\u5b8c\u65b9\u6cd5\u4e4b\u540e\u7684\u8fd4\u56de\u6307\u9488\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u6211\u60f3\u8981\u83b7\u53d6\u7684\u5143\u7d20\u7684\u4e0b\u6807\uff0c\u4ece0\u5f00\u59cb\u7b97<\/p>\n\n\n\n<p>\u6211\u4eec\u7528\u4e00\u4e2a\u5b9e\u4f8b\u6765\u8bb2\u89e3\u4e00\u4e0b<\/p>\n\n\n\n<p>PyObject <em>leftpts,<\/em>rightpts;<br>PyArg_Parse(PyTuple_GetItem(left,0),&#8221;O&#8221;,&amp;leftpts);\/\/&#8221;O&#8221;\u8868\u793a\u5bf9\u8c61\u662f\u4e00\u4e2apython\u2014\u2014object\uff0c\u8fd9\u4e2a\u662f\u628aleft\u8fd9\u4e2a\u5143\u7ec4\u7684\u7b2c0\u4e2a\u5143\u7d20\uff0c\u4e5f\u5c31\u662fpts\u8fd4\u56de\u7ed9leftpts<br>PyArg_Parse(PyTuple_GetItem(right,0),&#8221;O&#8221;,&amp;rightpts);\/\/&#8221;O&#8221;\u8868\u793a\u5bf9\u8c61\u662f\u4e00\u4e2apython\u2014\u2014object<\/p>\n\n\n\n<p>\u9700\u8981\u5148\u521d\u59cb\u5316\u4e00\u4e2aPyObject\uff0c\u7528\u6765\u63a5\u53d7\u6211\u83b7\u53d6\u7684\u6570\u636e\uff0c\u6570\u636e\u7c7b\u578b\u4ecd\u7136\u662f\u4e00\u4e2aPyObject\u7c7b\u578b\uff0c\u8fd9\u4e2a\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u6765\uff0c\u56e0\u4e3a\u6211\u8fd4\u56de\u7684\u662f\u4e00\u4e2alist\uff0c\u6240\u4ee5\u540e\u7eed\u6211\u8fd8\u8981\u7528list\u6765\u5904\u7406\u5b83\u3002\u6839\u636e\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u76f8\u4fe1\u8bfb\u8005\u4e5f\u5f88\u5bb9\u6613\u7406\u89e3PyArg_Parse\u51fd\u6570\u7684\u4f5c\u7528\uff0c\u5c31\u662f\u628a\u4e00\u4e2a\u5143\u7d20\u8f6c\u5316\u6210C++\u652f\u6301\u7684\u6570\u636e\u7ed3\u6784\u7c7b\u578b\uff0c\u4e0d\u8fc7\u6211\u8fd9\u91cc\u8c8c\u4f3c\u6709\u70b9\u591a\u4f59\u4e86\u2026\u2026\u6bd5\u7adf\u5f53\u65f6\u4e5f\u662f\u6478\u7d22\u7740\u5199\u7684\uff0c\u5728\u5199\u8fd9\u7bc7\u6587\u7ae0\u7684\u8fc7\u7a0b\u4e2d\u7406\u89e3\u4e5f\u5728\u4e0d\u65ad\u52a0\u6df1\u3002<\/p>\n\n\n\n<p>\u63a5\u7740\u5c31\u662f\u5c06\u8fd9\u4e9b\u5185\u5bb9\u63d0\u53d6\u51fa\u6765\uff0c\u4e0b\u9762\u76f4\u63a5\u9644\u4e0a\u4e00\u6bb5\u4ee3\u7801\uff0c\u4ee3\u7801\u662fAI\u5199\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u4e00\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    int sizepts0=PyObject_Size(leftpts);\n    int sizepts1=PyObject_Size(rightpts);\n \n    PyObject *iterpts0=PyObject_GetIter(leftpts);\n    PyObject *iterpts1=PyObject_GetIter(rightpts);\n    \/\/\u8f93\u51fa\u5de6\u76ee\u7684pts\n    while(true)\n    {\n        PyObject *next=PyIter_Next(iterpts0);\/\/\u6570\u636e\u7684\u884c\u6570\uff0c\u5728pts\u91cc\u5206\u522b\u4e3a1,2,3\u884c\n        if(!next)\n        {\n            break;\n        }\n        if(!PyList_Check(next))\n        {\n            int foosize0=PyObject_Size(next);\/\/\u8fd9\u4e00\u884c\u7684\u5927\u5c0f\uff0c\u4e5f\u5c31\u662f\u5217\u6570\n            cout&lt;&lt;foosize0;\n            PyObject *_iterpts0=PyObject_GetIter(next);\/\/\u7d22\u5f15\u5230\u8fd9\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\n            while(true)\n            {\n                PyObject *next2=PyIter_Next(_iterpts0);\/\/\u5229\u7528\u8fed\u4ee3\u5668\u9010\u4e2a\u8f93\u51fa\n                if(!next2)\n                {\n                    break;\n                }\n                if(!PyList_Check(next2))\n                {\n                    double foo=PyFloat_AsDouble(next2);\n                    cout&lt;&lt;foo&lt;&lt;\" \";\n                }\n            }\n            cout&lt;&lt;endl;\n        }\n    }<\/code><\/pre>\n\n\n\n<p>\u6574\u6bb5\u4ee3\u7801\u7684\u6838\u5fc3\u903b\u8f91\u8bf4\u767d\u4e86\u5c31\u662f\u5265\u6d0b\u8471\uff0c\u4e00\u5c42\u4e00\u5c42\u62bd\u4e1d\u5265\u8327\uff0c\u5176\u5b9e\u6839\u636e\u8fd9\u6bb5\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u63a8\u77e5\u4ed6\u7684\u6570\u636e\u7ed3\u6784<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/39.104.51.85\/wp-content\/uploads\/2025\/01\/image-58-1024x401.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" width=\"1024\" height=\"401\" data-original=\"http:\/\/39.104.51.85\/wp-content\/uploads\/2025\/01\/image-58-1024x401.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-564\"  sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<p>\u4e0a\u9762\u7684\u903b\u8f91\u76f8\u5f53\u4e8e\u662f\u4e00\u5f00\u59cb\u53ef\u4ee5\u83b7\u5f97head\u6307\u9488\uff0c\u6839\u636e\u8fd9\u4e2a\u6307\u9488\u53ef\u4ee5\u62ff\u5230\u4e00\u5171\u6709\u51e0\u4e2anodehead\uff0c\u63a5\u7740\u9010\u5c42\u904d\u5386\uff0c\u53ef\u4ee5\u5427node1.1~node3.3\u7ed9\u904d\u5386\u5b8c\uff0c\u5e26\u4e00\u70b9\u6570\u636e\u7ed3\u6784\u7684\u77e5\u8bc6<\/p>\n\n\n\n<p>\u91cc\u9762\u6d89\u53ca\u5230\u7684\u51fd\u6570\u6211\u8ba4\u4e3a\u90fd\u53ef\u4ee5\u6839\u636e\u51fd\u6570\u540d\u79f0\u63a8\u65ad\u529f\u80fd\uff0c\u5c31\u4e0d\u591a\u8bb2\u4e86<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%8F%98%E9%87%8F%E4%B8%8E%E8%99%9A%E6%8B%9F%E8%B5%84%E6%BA%90%E7%9A%84%E9%87%8A%E6%94%BE\"><\/span>\u53d8\u91cf\u4e0e\u865a\u62df\u8d44\u6e90\u7684\u91ca\u653e<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u5728\u6700\u7ec8\u8c03\u7528\u5b8cpython\u4e4b\u540e\uff0c\u9700\u8981\u628a\u6d89\u53ca\u5230\u7684\u53d8\u91cf\u548cpython\u865a\u62df\u673a\u90fd\u91ca\u653e\u6389\uff0c\u907f\u514d\u53d1\u751f\u5185\u5b58\u6cc4\u6f0f\u7684\u60c5\u51b5\uff0c\u7528\u5230\u7684\u51fd\u6570\u4e5f\u5f88\u7b80\u5355<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Py_DECREF(p);\/\/p\uff1a\u5f85\u91ca\u653e\u7684\u53d8\u91cf\nPy_Finalize();\/\/\u91ca\u653epython\u865a\u62df\u673a<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u505a\u6709\u5173\u65e0\u4eba\u673a\u89c6\u89c9\u7684\u9879\u76ee\uff0c\u7531\u4e8esuperpoint\u7f51\u7edc\u548cros\u7cfb\u7edf\u7684\u8054\u52a8\u6bd4\u8f83\u56f0\u96be\u590d\u6742\uff0c\u6545\u6700\u540e\u51b3\u5b9a\u76f4\u63a5\u5c06su [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[],"class_list":["post-559","post","type-post","status-publish","format-standard","hentry","category-4","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/39.104.51.85\/index.php\/wp-json\/wp\/v2\/posts\/559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/39.104.51.85\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/39.104.51.85\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/39.104.51.85\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/39.104.51.85\/index.php\/wp-json\/wp\/v2\/comments?post=559"}],"version-history":[{"count":3,"href":"http:\/\/39.104.51.85\/index.php\/wp-json\/wp\/v2\/posts\/559\/revisions"}],"predecessor-version":[{"id":565,"href":"http:\/\/39.104.51.85\/index.php\/wp-json\/wp\/v2\/posts\/559\/revisions\/565"}],"wp:attachment":[{"href":"http:\/\/39.104.51.85\/index.php\/wp-json\/wp\/v2\/media?parent=559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/39.104.51.85\/index.php\/wp-json\/wp\/v2\/categories?post=559"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/39.104.51.85\/index.php\/wp-json\/wp\/v2\/tags?post=559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}