我在不使用Cython的情况下编写Python C-Extension.
我想在C中分配一个double数组,在内部函数中使用它(恰好在Fortran中)并返回它.我指出C-Fortran接口在C中完美运行.
static PyObject *
Py_drecur(PyObject *self, PyObject *args)
{
// INPUT
int n;
int ipoly;
double al;
double be;
if (!PyArg_ParseTuple(args, "iidd", &n, &ipoly, &al, &be))
return NULL;
// OUTPUT
int nd = 1;
npy_intp dims[] = {n};
double a[n];
double b[n];
int ierr;
drecur_(n, ipoly, al, be, a, b, ierr);
// Create PyArray
PyObject* alpha = PyArray_SimpleNewFromData(nd, dims, NPY_DOUBLE, a);
PyObject* beta = PyArray_SimpleNewFromData(nd, dims, NPY_DOUBLE, b);
Py_INCREF(alpha);
Py_INCREF(beta);
return Py_BuildValue("OO", alpha, beta);
}
Run Code Online (Sandbox Code Playgroud)
我调试了这段代码,当我尝试从a创建alpha时,我遇到了Segmentation错误.到那里一切正常.功能drecur_工作,如果删除它我会遇到同样的问题.
现在,围绕C数据定义PyArray的标准方法是什么?我找到了文档,但没有很好的例子.还有,内存泄漏怎么样?返回之前INCREF是否正确,以便保留alpha和beta的实例?那些不再需要的解除分配怎么样?
编辑 …
我正在编写Eclipse RCP,我想询问用户在应用程序关闭时是否备份数据库.从文件>退出菜单执行此操作非常简单,因为我定义了一个命令退出:
public class ExitCommand extends AbstractHandler implements IHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final IWorkbench workbench = PlatformUI.getWorkbench();
if (workbench == null)
return null;
// Ask whether the user wants to back up the information
Shell shell = new Shell(workbench.getDisplay());
MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION
| SWT.YES | SWT.NO);
messageBox.setMessage("You are leaving CatSysPD. Do you want to make a backup of the DataBase? (recommended)");
messageBox.setText("On Exit Backup");
int response = messageBox.open();
if (response == …Run Code Online (Sandbox Code Playgroud)