|
我写了一个小的C++扩展库:
// TestNumpy.cpp : 定义 DLL 应用程序的导出函数。
//
#include <iostream>
#include <Python.h>
#include <arrayobject.h>
using namespace std;
PyMODINIT_FUNC init_numpy()
{
import_array();
}
static PyObject *GetArray(PyObject* Self, PyObject* Argvs)
{
int CArrays[3][3] = { {1, 2, 5}, {4, 7, 8}, {1, 0, 8} };
npy_intp Dims[2] = { 3,3 };
PyArrayObject *PyArray = (PyArrayObject *)PyArray_SimpleNewFromData(2, Dims, NPY_INT, CArrays);
return PyArray_Return(PyArray);
}
static PyMethodDef ModulesMethods[] =
{
{"GetArray", GetArray, METH_VARARGS, "Get array from c to python"},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef ModuleDesc =
{
PyModuleDef_HEAD_INIT,
"TestNumpy_Module",
"This module is created by C++. Get array from c to python!",
-1,
ModulesMethods
};
PyMODINIT_FUNC PyInit_TestNumpy(void)
{
init_numpy();
return PyModule_Create(&ModuleDesc);
}
Python 代码:
import TestNumpy as tn
tmp = tn.GetArray()
print(tmp)
打印结果是:
array([[1852914064, 701, 1845666960],
[ 0, 0, 0],
[1852914064, 701, 1867854904]], dtype=int32)
这是什么情况,而且打印值会一直变。
|
|