hi @MRAB i try again,it not ok. test files: 1.cpython.cpp ---c call python maintenance.py 2.maintenance.py python maintenance.py call pycapsule.cpp(dll) the code file,see attachment.
At 2018-02-21 11:18:18, "MRAB" <pyt...@mrabarnett.plus.com> wrote: >On 2018-02-21 02:28, 赵亚 wrote: > >This "python-dev" list is for development of the Python language itself. >I think that this message should instead have been sent to "python-list". > >> i have question:call,c-->python-->c. >> 1.the c pointer void* abc="123" by pycapsule in the c code. >> ...... >> void* lpContext = "abc"; >> PyObject * lpPyContext = PyCapsule_New(lpContext, "Context",NULL); >> ...... > >Is this C code calling the function 'test' in the Python code? > >If yes, are you sure that lpPyContext should be in pArgs at index 1 and >not at index 0? > >The function 'test' expects argument 0 => lpContext, argument 1 => >lpRequest, argument 2 => lpAnswer. > >> PyTuple_SetItem(pArgs, 1, lpPyContext); >> PyObject* pyResult = PyObject_CallObject(pFunc, pArgs); >> >> 2.c call python: >> >> in the python code: >> import ctypes >> pycapsule = ctypes.windll.LoadLibrary("C:\Users\zhaoya16975\Documents\Visual >> Studio 2017\Projects\cpython\Release\pycapsule.dll") >> def test( lpContext,lpRequest,lpAnswer): >> print lpContext >> pycapsule.hello() >> pycapsule.GetContext(lpContext) >> ......... >> the lpContest is "<capsule object "Context" at 0x02747e00>" >> but,i can't lpContext in the pycapsule.GetContest: >> the capsule is null poniter,the GetContext no execute!! >> >> void* FUNCTION_CALL_MODE GetContext(PyObject *capsule) { >> printf(" GetContext......\n"); >> //if (!PyCapsule_IsValid((PyObject *)capsule, "Context")) { >> // printf(" the Capsule Context is no Valid\n"); >> // return NULL; >> //} >> //return PyCapsule_GetPointer((PyObject *)capsule, "Context"); >> return NULL; >> >> } >> >> FUNCTION_CALL_MODE is __stdcall i think no problem.because, if >> GetContext(PyObject *capsule) -->GetContext() is ok. PyObject *capsule >> from python to c unknown error occurred. >> >> i hope c call python pass void* ,and python call c pass void* ,but the >> capsule call no success? >> >_______________________________________________ >Python-Dev mailing list >Python-Dev@python.org >https://mail.python.org/mailman/listinfo/python-dev >Unsubscribe: >https://mail.python.org/mailman/options/python-dev/zhaoya881010%40163.com
maintenance.py
Description: Binary data
// cpython.cpp : ¶¨Òå¿ØÖÆÌ¨Ó¦ÓóÌÐòµÄÈë¿Úµã¡£ // #include "stdafx.h" #include "cpython.h" PyObject *g_pName= NULL; PyObject *g_pModule = NULL; int FUNCTION_CALL_MODE ReProc(void* lpContext,void* lpRequest,void* lpAnswer) { PyObject *pDict = NULL; PyObject *pFunc = NULL; PyObject *pArgs = NULL; PyObject * lpPyContext = PyCapsule_New(lpContext, "Context",NULL); PyObject * lpPyRequest = PyCapsule_New(lpRequest, "Request", NULL); PyObject * lpPyAnswer = PyCapsule_New(lpAnswer, "Answer", NULL); pDict = PyModule_GetDict(g_pModule); if (!pDict) { printf("Can't find dict in py_add!\n"); return -1; } pFunc = PyDict_GetItemString(pDict, "test"); if (!pFunc || !PyCallable_Check(pFunc)) { printf("Can't find function!\n"); getchar(); return -1; } pArgs = PyTuple_New(3); //PyTuple_SetItem(pArgs, 0, Py_BuildValue("i", 2006)); PyTuple_SetItem(pArgs, 0, lpPyContext); PyTuple_SetItem(pArgs, 1, lpPyRequest); PyTuple_SetItem(pArgs, 2, lpPyAnswer); //µ÷ÓÃpythonµÄReqProcº¯Êý PyObject* pyResult = PyObject_CallObject(pFunc, pArgs); if (pArgs) { Py_DECREF(pArgs); } } int FUNCTION_CALL_MODE OnInit() { //³õʼ»¯£¬ÔØÈëpythonµÄÀ©Õ¹Ä£¿é Py_Initialize(); //Åжϳõʼ»¯ÊÇ·ñ³É¹¦ if (!Py_IsInitialized()) { printf("Python init failed!\n"); return -1; } //PyRun_SimpleString Ϊºê£¬Ö´ÐÐÒ»¶Îpython´úÂë //µ¼È뵱ǰ·¾¶ //C:\Users\zhaoya16975\PycharmProjects\complugin\coreplugin.py PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append(\"C:\\Users\\zhaoya16975\\go\\src\\GoProxy\\plugin\\py\")"); //¼ÓÔØÃûΪpy_addµÄpython½Å±¾ g_pName = PyString_FromString("maintenance"); g_pModule = PyImport_Import(g_pName); if (!g_pModule) { printf("Load coreplugin.py failed!\n"); getchar(); return -1; } //¹Ø±Õpythonµ÷Óà //Py_Finalize(); return 0; } void FUNCTION_CALL_MODE OnClose() { } void main() { //¼ÓÔØ³õʼ»¯py²å¼þ OnInit(); // void* lpContext; void* lpRequest; void* lpAnswer; lpContext = "abc"; lpRequest = "def"; lpAnswer = "ghi"; ReProc(lpContext, lpRequest, lpAnswer); }
cpython.h
Description: Binary data
#include "pycapsule.h" extern "C" { void* FUNCTION_CALL_MODE hello(char* b) { printf(" hello......%s\n",b); return NULL; } void* FUNCTION_CALL_MODE GetContext(PyObject *capsule) { printf(" GetContext......\n"); //if (!PyCapsule_IsValid((PyObject *)capsule, "Context")) { // printf(" the Capsule Context is no Valid\n"); // return NULL; //} //return PyCapsule_GetPointer((PyObject *)capsule, "Context"); return NULL; } void* FUNCTION_CALL_MODE GetRequest(PyObject *capsule) { printf(" GetRequest......\n"); if (!PyCapsule_IsValid(capsule, "Request")) { printf(" the Capsule Request is no Valid\n"); return NULL; } return PyCapsule_GetPointer(capsule, "Request"); } void* FUNCTION_CALL_MODE GetAnswer(PyObject *capsule) { printf(" GetAnswer......\n"); if (!PyCapsule_IsValid(capsule, "Answer")) { printf(" the Capsule Answer is no Valid\n"); return NULL; } return PyCapsule_GetPointer(capsule, "Answer"); } PyObject * FUNCTION_CALL_MODE VoidptrToPyobject(void* lpdata, const char* name) { printf(" VoidptrToPyobject......\n"); return PyCapsule_New(lpdata, name, NULL); } void * FUNCTION_CALL_MODE PyobjectToVoidptr(PyObject * capsule, const char* name) { printf(" VoidptrToPyobject......\n"); void* lpdata = PyCapsule_GetPointer(capsule, name); printf("VoidptrToPyobject :%s\n", (char*)lpdata); return lpdata; } PyObject * FUNCTION_CALL_MODE VoidptrToPyobjectS() { printf(" VoidptrToPyobject1......\n"); void * lpdata = "123"; const char* name = "zhaoya"; return PyCapsule_New(lpdata, name, NULL); } }
pycapsule.def
Description: Binary data
pycapsule.h
Description: Binary data
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com