I am building a Python extension (.pyd
) using Visual Studio 2015 C++ project and Python 2.7 32bit.
This is my .cpp
file:
#include <Python.h>
static PyObject* GetTwoInts(PyObject* self, PyObject* args)
{
srand(time(NULL));
int random1 = rand() % 10;
int random2 = rand() % 10;
PyObject * python_val = Py_BuildValue("ii", random1, random2);
return python_val;
}
static PyObject* GetListTwoInts(PyObject* self, PyObject* args)
{
srand(time(NULL));
int random1 = rand() % 10;
int random2 = rand() % 10;
PyObject *val1 = PyInt_FromLong(random1);
PyObject *val2 = PyInt_FromLong(random2);
PyObject *result = PyList_New(2);
PyList_SetItem(result, 0, val1);
PyList_SetItem(result, 1, val2);
PyObject * python_val = Py_BuildValue("ii", result);
return python_val;
}
PyMODINIT_FUNC initUtils(void)
{
static PyMethodDef methods[] = {
{ "GetTwoInts", GetTwoInts, METH_NOARGS,
"Get two C ints as a Python tuple with two random numbers" },
{ "GetListTwoInts", GetListTwoInts, METH_NOARGS,
"Get a list with two random numbers" },
{ NULL, NULL, 0, NULL },
};
PyObject *m = Py_InitModule("Utils", methods);
}
This is the Python source code using the compiled extension:
import sys
import Utils
print help(Utils)
print Utils.GetTwoInts()
print Utils.GetListTwoInts()
This is the output:
(4, 2)
(91213912, 91213912)
So, the Py_BuildValue("ii", random1, random2);
gave me a proper tuple with two random ints, just as expected. However, returning a list in the GetListTwoInts
method gives me invalid numbers (looks like a reference value or a pointer?).
What should I do to return a list of real values instead in the GetListTwoInts
method?
result
? That is your list. – Throesresult
. – Interleave