{"id":294250,"date":"2019-09-04T15:00:13","date_gmt":"2019-09-04T15:00:13","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=294250"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=294250","title":{"rendered":"Python \u0438\u0437 C"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\n<div style=\"text-align:center;\"><img decoding=\"async\" alt=\"main\" width=\"500\" src=\"https:\/\/habrastorage.org\/webt\/la\/_7\/ji\/la_7ji4yzcgayh2tad4yycia3yy.jpeg\"><\/div>\n<p>  <\/p>\n<p>\u0412 \u043f\u0440\u043e\u0448\u043b\u043e\u043c \u0433\u043e\u0434\u0443 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u0442\u0430\u0440\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 C \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u043e\u043c \u043d\u0430 <strong>python3<\/strong>. \u041d\u0435 \u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u0438 \u043d\u0430 \u044d\u0442\u0443 \u0442\u0435\u043c\u0443 \u044f \u043f\u043e\u043c\u0443\u0447\u0438\u043b\u0441\u044f \u0438 \u0432 \u0442\u043e\u043c \u0433\u043e\u0434\u0443 \u0438 \u0441\u0435\u0439\u0447\u0430\u0441 \u043a\u043e\u0433\u0434\u0430 \u043f\u0438\u0441\u0430\u043b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0434\u043b\u044f \u0441\u0442\u0430\u0442\u044c\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u0441\u0432\u043e\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043f\u043e \u0442\u043e\u043c\u0443 \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 python3 \u0438\u0437 C \u043f\u043e\u0434 Linux (\u0441 \u0442\u0435\u043c \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b). \u041e\u043f\u0438\u0448\u0443 \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 \u0438 \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u044b, \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c. \u0412\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0438\u0437 \u043c\u043e\u0434\u0443\u043b\u044f. \u0410 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0438 \u043d\u0435 \u0441\u043c\u043e\u0433 \u0438\u0445 \u043f\u043e\u043d\u044f\u0442\u044c.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h2 id=\"pakety\">\u041f\u0430\u043a\u0435\u0442\u044b<\/h2>\n<p>  <\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0435 <strong>Python API<\/strong>. \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043f\u0430\u043a\u0435\u0442\u044b python: <\/p>\n<p>  <\/p>\n<ul>\n<li><strong>apt-get install python3<\/strong><\/li>\n<li><strong>apt-get install python3-dev<\/strong><\/li>\n<li><strong>apt-get install python3-all<\/strong><\/li>\n<li><strong>apt-get install python3-all-dev<\/strong><\/li>\n<li><strong>apt-get install libpython3-all-dev<\/strong><\/li>\n<\/ul>\n<p>  <\/p>\n<h2 id=\"rabota-v-interpretatore\">\u0420\u0430\u0431\u043e\u0442\u0430 \u0432 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0435<\/h2>\n<p>  <\/p>\n<p>\u0421\u0430\u043c\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0435, \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 python \u0438 \u0440\u0430\u0431\u043e\u0442\u0430 \u0432 \u043d\u0435\u043c.<\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u0447\u043d\u044b\u0439 \u0444\u0430\u0439\u043b:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\"> #include &lt;Python.h&gt;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0417\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">Py_Initialize();<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0438\u0434\u0435\u0442 \u0431\u043b\u043e\u043a \u0440\u0430\u0431\u043e\u0442\u044b \u0441 python, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">PyRun_SimpleString(\"print('Hello!')\");<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u044b\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">Py_Finalize();<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">#include &lt;Python.h&gt;  void main() {     \/\/ \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 Python     Py_Initialize();     \/\/ \u0412\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0435     PyRun_SimpleString(\"print('Hello!')\");     \/\/ \u0412\u044b\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 Python     Py_Finalize(); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">gcc simple.c $(python3-config --includes --ldflags) -o simple &amp;&amp; .\/simple Hello!<\/code><\/pre>\n<p>  <\/p>\n<p>\u0410 \u0432\u043e\u0442 \u0442\u0430\u043a \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">gcc $(python3-config --includes --ldflags)  simple.c -o simple &amp;&amp; .\/simple \/tmp\/ccUkmq57.o: In function `main': simple.c:(.text+0x5): undefined reference to `Py_Initialize' simple.c:(.text+0x16): undefined reference to `PyRun_SimpleStringFlags' simple.c:(.text+0x1b): undefined reference to `Py_Finalize' collect2: error: ld returned 1 exit status<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u0441\u0435 \u0438\u0437-\u0437\u0430 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e <strong>python3-config &#8212;includes &#8212;ldflags<\/strong> \u0440\u0430\u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432\u043e\u0442 \u0432 \u0442\u0430\u043a\u0443\u044e \u0448\u0442\u0443\u043a\u0443:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">-I\/usr\/include\/python3.6m -I\/usr\/include\/python3.6m -L\/usr\/lib\/python3.6\/config-3.6m-x86_64-linux-gnu -L\/usr\/lib -lpython3.6m -lpthread -ldl  -lutil -lm  -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions<\/code><\/pre>\n<p>  <\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u0434\u0443\u043c\u0430\u044e \u0432\u0430\u0436\u0435\u043d \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043b\u0438\u043d\u043a\u0435\u0440\u0430 <strong>-Wl<\/strong>. \u041a\u0442\u043e \u0437\u043d\u0430\u0435\u0442 \u0442\u043e\u0447\u043d\u0435\u0435 \u043d\u0430\u043f\u0438\u0448\u0438\u0442\u0435 \u043f\u0440\u043e \u044d\u0442\u043e \u0432 \u043a\u043e\u043c\u0435\u043d\u0442\u0430\u0445, \u0434\u043e\u043f\u043e\u043b\u043d\u044e \u043e\u0442\u0432\u0435\u0442.<\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 python:<br \/>  simple.c<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">#include &lt;Python.h&gt;  void  python() {     \/\/ \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 Python     Py_Initialize();      \/\/ \u0412\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u043c\u0430\u043d\u0434 \u0432 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0435     \/\/ \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0443\u043b\u044f sys     PyRun_SimpleString(\"import sys\");     \/\/ \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043d\u0430\u0448\u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 python     PyRun_SimpleString(\"sys.path.append('.\/src\/python')\");     PyRun_SimpleString(\"import simple\");     PyRun_SimpleString(\"print(simple.get_value(2))\");     PyRun_SimpleString(\"print(simple.get_value(2.0))\");     PyRun_SimpleString(\"print(simple.get_value(\\\"Hello!\\\"))\");      \/\/ \u0412\u044b\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 Python     Py_Finalize();   }  void main() {     puts(\"Test simple:\");      python(); }<\/code><\/pre>\n<p>  <\/p>\n<p>simple.py<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">#!\/usr\/bin\/python3 #-*- coding: utf-8 -*-  def get_value(x):     return x<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u043e \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0438 \u043d\u0435\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u0432\u0435\u0449\u0438, \u043c\u044b \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u0446\u0438\u0438.<\/p>\n<p>  <\/p>\n<h2 id=\"rabota-s-funkciyami-i-peremennymi-modulya\">\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438 \u043c\u043e\u0434\u0443\u043b\u044f<\/h2>\n<p>  <\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435.<br \/>  \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 python \u0438 \u043c\u043e\u0434\u0443\u043b\u044f func.py \u0432 \u043d\u0435\u0433\u043e.:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">PyObject * python_init() {     \/\/ \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440 Python     Py_Initialize();      do {         \/\/ \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0443\u043b\u044f sys         sys = PyImport_ImportModule(\"sys\");         sys_path = PyObject_GetAttrString(sys, \"path\");         \/\/ \u041f\u0443\u0442\u044c \u0434\u043e \u043d\u0430\u0448\u0438\u0445 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u043e\u0432 python         folder_path = PyUnicode_FromString((const char*) \".\/src\/python\");         PyList_Append(sys_path, folder_path);          \/\/ \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 func.py         pName = PyUnicode_FromString(\"func\");         if (!pName) {             break;         }          \/\/ \u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u043c\u043e\u0434\u0443\u043b\u044f         pModule = PyImport_Import(pName);         if (!pModule) {             break;         }          \/\/ \u0421\u043b\u043e\u0432\u0430\u0440\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0445\u0441\u044f \u0432 \u043c\u043e\u0434\u0443\u043b\u0435         pDict = PyModule_GetDict(pModule);         if (!pDict) {             break;         }          return pDict;     } while (0);      \/\/ \u041f\u0435\u0447\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438     PyErr_Print(); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 python:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">void python_clear() {     \/\/ \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u0441\u0438\u0441\u0442\u0435\u043c\u0435     Py_XDECREF(pDict);     Py_XDECREF(pModule);     Py_XDECREF(pName);      Py_XDECREF(folder_path);     Py_XDECREF(sys_path);     Py_XDECREF(sys);      \/\/ \u0412\u044b\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 Python     Py_Finalize(); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u043c\u043e\u0434\u0443\u043b\u044f.<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">\/**  * \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u043d\u0430\u0437\u0430\u0434  *\/ char * python_func_get_str(char *val) {     char *ret = NULL;      \/\/ \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 get_value \u0438\u0437 func.py     pObjct = PyDict_GetItemString(pDict, (const char *) \"get_value\");     if (!pObjct) {         return ret;     }      do {         \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 pObjct \u043d\u0430 \u0433\u043e\u0434\u043d\u043e\u0441\u0442\u044c.         if (!PyCallable_Check(pObjct)) {             break;         }          pVal = PyObject_CallFunction(pObjct, (char *) \"(s)\", val);         if (pVal != NULL) {             PyObject* pResultRepr = PyObject_Repr(pVal);              \/\/ \u0415\u0441\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u043d\u0435 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u043e\u0447\u0438\u0441\u0442\u043a\u0438 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 python \u0435\u0451 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442.             \/\/ \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 pResultRepr \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0431\u0430\u0439\u0442\u043e\u0432.             ret = strdup(PyBytes_AS_STRING(PyUnicode_AsEncodedString(pResultRepr, \"utf-8\", \"ERROR\")));              Py_XDECREF(pResultRepr);             Py_XDECREF(pVal);         } else {             PyErr_Print();         }     } while (0);      Py_XDECREF(pObjct);      return ret; }  \/**  * \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0435\u0439 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 int  *\/ int python_func_get_val(char *val) {     int ret = 0;      \/\/ \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u0441 \u0438\u043c\u0435\u043d\u0435\u043c val     pVal = PyDict_GetItemString(pDict, (const char *) val);     if (!pVal) {         return ret;     }      \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043d\u0430 long     if (PyLong_Check(pVal)) {         ret = _PyLong_AsInt(pVal);     } else {         PyErr_Print();     }      Py_XDECREF(pVal);      return ret; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c\u0441\u044f \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">pVal = PyObject_CallFunction(pObjct, (char *) \"(s)\", val);<\/code><\/pre>\n<p>  <\/p>\n<p><strong>&#171;(s)&#187;<\/strong> \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f 1 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0442\u0438\u043f\u0430 *<em>char <\/em> <strong> \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <\/strong>get_value(x)**. \u0415\u0441\u043b\u0438 \u0431\u044b \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0442\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">pVal = PyObject_CallFunction(pObjct, (char *) \"(sss)\", val1, val2, val3);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c <strong>int<\/strong>, \u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0430\u0441\u044c \u0431\u044b \u043b\u0438\u0442\u0435\u0440\u0430 <strong>i<\/strong>, \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0438\u0445 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 <a href=\"https:\/\/docs.python.org\/3\/c-api\/arg.html#c.Py_BuildValue\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a> python.<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">pVal = PyObject_CallFunction(pObjct, (char *) \"(i)\", my_int);<\/code><\/pre>\n<p>  <\/p>\n<p>func.py:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">#!\/usr\/bin\/python3 #-*- coding: utf-8 -*-  a = 11 b = 22 c = 33  def get_value(x):     return x  def get_bool(self, x):     if x:         return True     else:         return False<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0438 \u043d\u0435 \u0441\u043c\u043e\u0433 \u043f\u043e\u043a\u0430 \u043f\u043e\u043d\u044f\u0442\u044c:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">int main() {     puts(\"Test func:\");      if (!python_init()) {         puts(\"python_init error\");         return -1;     }      puts(\"Strings:\");     printf(\"\\tString: %s\\n\", python_func_get_str(\"Hello from Python!\"));      puts(\"Attrs:\");     printf(\"\\ta: %d\\n\", python_func_get_val(\"a\"));     printf(\"\\tb: %d\\n\", python_func_get_val(\"b\"));     printf(\"\\tc: %d\\n\", python_func_get_val(\"c\"));      python_clear();      return 0; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0445\u043e\u0447\u0443 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c <strong>b<\/strong> \u0438\u043b\u0438 <strong>c<\/strong> \u0438\u0437 <strong>func.py<\/strong>, \u0442\u043e \u043d\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">Py_Finalize();<\/code><\/pre>\n<p>  <\/p>\n<p>\u043f\u043e\u043b\u0443\u0447\u0430\u044e <strong>segmentation fault<\/strong>. \u0421 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e <strong>a<\/strong> \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043d\u0435\u0442.<br \/>  \u041f\u0440\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u0430, \u0442\u043e\u0436\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u043d\u0435\u0442.<\/p>\n<p>  <\/p>\n<h2 id=\"rabota-s-klassom\">\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u043a\u043b\u0430\u0441\u0441\u043e\u043c<\/h2>\n<p>  <\/p>\n<p>\u0422\u0443\u0442 \u0435\u0449\u0435 \u043d\u0435\u043c\u043d\u043e\u0436\u0435\u0447\u043a\u043e \u043f\u043e\u0441\u043b\u043e\u0436\u043d\u0435\u0435.<br \/>  \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 python \u0438 \u043c\u043e\u0434\u0443\u043b\u044f class.py \u0432 \u043d\u0435\u0433\u043e.<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">PyObject * python_init() {     \/\/ \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440 Python     Py_Initialize();      do {         \/\/ \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0443\u043b\u044f sys         sys = PyImport_ImportModule(\"sys\");         sys_path = PyObject_GetAttrString(sys, \"path\");         \/\/ \u041f\u0443\u0442\u044c \u0434\u043e \u043d\u0430\u0448\u0438\u0445 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u043e\u0432 python         folder_path = PyUnicode_FromString((const char*) \".\/src\/python\");         PyList_Append(sys_path, folder_path);          \/\/ \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 Unicode \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438\u0437 UTF-8 \u0441\u0442\u0440\u043e\u043a\u0438         pName = PyUnicode_FromString(\"class\");         if (!pName) {             break;         }          \/\/ \u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043c\u043e\u0434\u0443\u043b\u044c class         pModule = PyImport_Import(pName);         if (!pModule) {             break;         }          \/\/ \u0421\u043b\u043e\u0432\u0430\u0440\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0445\u0441\u044f \u0432 \u043c\u043e\u0434\u0443\u043b\u0435         pDict = PyModule_GetDict(pModule);         if (!pDict) {             break;         }          \/\/ \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 Class \u0438\u0437 class.py         pClass = PyDict_GetItemString(pDict, (const char *) \"Class\");         if (!pClass) {             break;         }          \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 pClass \u043d\u0430 \u0433\u043e\u0434\u043d\u043e\u0441\u0442\u044c.         if (!PyCallable_Check(pClass)) {             break;         }          \/\/ \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 Class         pInstance = PyObject_CallObject(pClass, NULL);          return pInstance;     } while (0);      \/\/ \u041f\u0435\u0447\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438     PyErr_Print(); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u043d\u0430\u0437\u0430\u0434<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">char * python_class_get_str(char *val) {     char *ret = NULL;      pVal = PyObject_CallMethod(pInstance, (char *) \"get_value\", (char *) \"(s)\", val);     if (pVal != NULL) {         PyObject* pResultRepr = PyObject_Repr(pVal);          \/\/ \u0415\u0441\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u043d\u0435 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u043e\u0447\u0438\u0441\u0442\u043a\u0438 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 python \u0435\u0451 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442.         ret = strdup(PyBytes_AS_STRING(PyUnicode_AsEncodedString(pResultRepr, \"utf-8\", \"ERROR\")));          Py_XDECREF(pResultRepr);         Py_XDECREF(pVal);     } else {         PyErr_Print();     }      return ret; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u043d\u0438 \u043a\u0430\u043a\u0438\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u043d\u0435 \u0431\u044b\u043b\u043e, \u0432\u0441\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0431\u0435\u0437 \u043e\u0448\u0438\u0431\u043e\u043a. \u0412 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0430\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u044b, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 <strong>int<\/strong>, <strong>double<\/strong>, <strong>bool<\/strong>.<\/p>\n<p>  <\/p>\n<p>\u041f\u043e\u043a\u0430 \u043f\u0438\u0441\u0430\u043b \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u043e\u0441\u0432\u0435\u0436\u0438\u043b \u0441\u0432\u043e\u0438 \u0437\u043d\u0430\u043d\u0438\u044f )<br \/>  \u041d\u0430\u0434\u0435\u044e\u0441\u044c \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u043e.<\/p>\n<p>  <\/p>\n<h2 id=\"ssylki\">\u0421\u0441\u044b\u043b\u043a\u0438<\/h2>\n<p>  <\/p>\n<p><a href=\"https:\/\/github.com\/dvjdjvu\/python_from_c\">\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0435 \u043a\u043e\u0434\u044b \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432<\/a><\/p>\n<\/div>\n<p>               <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d()}}}t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>     <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/466181\/\"> https:\/\/habr.com\/ru\/post\/466181\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\n<div style=\"text-align:center;\"><img decoding=\"async\" alt=\"main\" width=\"500\" src=\"https:\/\/habrastorage.org\/webt\/la\/_7\/ji\/la_7ji4yzcgayh2tad4yycia3yy.jpeg\"><\/div>\n<p>  <\/p>\n<p>\u0412 \u043f\u0440\u043e\u0448\u043b\u043e\u043c \u0433\u043e\u0434\u0443 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u0442\u0430\u0440\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 C \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u043e\u043c \u043d\u0430 <strong>python3<\/strong>. \u041d\u0435 \u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u0438 \u043d\u0430 \u044d\u0442\u0443 \u0442\u0435\u043c\u0443 \u044f \u043f\u043e\u043c\u0443\u0447\u0438\u043b\u0441\u044f \u0438 \u0432 \u0442\u043e\u043c \u0433\u043e\u0434\u0443 \u0438 \u0441\u0435\u0439\u0447\u0430\u0441 \u043a\u043e\u0433\u0434\u0430 \u043f\u0438\u0441\u0430\u043b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0434\u043b\u044f \u0441\u0442\u0430\u0442\u044c\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u0441\u0432\u043e\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043f\u043e \u0442\u043e\u043c\u0443 \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 python3 \u0438\u0437 C \u043f\u043e\u0434 Linux (\u0441 \u0442\u0435\u043c \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b). \u041e\u043f\u0438\u0448\u0443 \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 \u0438 \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u044b, \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c. \u0412\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0438\u0437 \u043c\u043e\u0434\u0443\u043b\u044f. \u0410 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0438 \u043d\u0435 \u0441\u043c\u043e\u0433 \u0438\u0445 \u043f\u043e\u043d\u044f\u0442\u044c.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-294250","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/294250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=294250"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/294250\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=294250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=294250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=294250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}