[Python-Dev] pycapsule:PyObject * is NULL pointer

2018-02-20 Thread
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);
..
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 ""
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/archive%40mail-archive.com


[Python-Dev] void* from c to python and to c?

2018-02-20 Thread
I have a demand:
c call python ,have a void* pointer  pass to python and python call c,the void* 
pointer need pass to c.
i don't know how to do,have any good idea ? thanks!___
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


Re: [Python-Dev] pycapsule:PyObject * is NULL pointer

2018-02-20 Thread


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"  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 ""
>> 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:\