2

I am building learning how to build extensions for Numpy. The tutorial I am following is this one:

https://gist.github.com/kanhua/8f1eb7c67f5a031633121b6b187b8dc9

My code looks as follows:

module.cpp

#include "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/include/python3.7m/Python.h"

#include "numpy/ndarraytypes.h"
#include "numpy/ufuncobject.h"
#include "numpy/npy_3kcompat.h"

#include <iostream>

PyObject* get_dimension(PyObject *dummy, PyObject* args) {
    PyObject *arg1=NULL;
    PyObject *arr1=NULL;
    int nd = 1;

    if (!PyArg_ParseTuple(args, "O", &arg1))
        return NULL;

    // THIS LINE PRINTS A VALID ADDRESS
    std::cout << arg1 << std::endl;

    //=====================
    // PROBLEM AT THIS LINE
    //=====================
    arr1 = PyArray_FROM_OTF(arg1, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);

    return PyInt_FromLong(nd);
}

static PyMethodDef matrixsolvers_methods[] = {
    // The first property is the name exposed to Python, fast_tanh, the second is the C++
    // function name that contains the implementation.
    { "get_dimension", (PyCFunction) get_dimension, METH_VARARGS, nullptr },

    // Terminate the array with an object containing nulls.
    { nullptr, nullptr, 0, nullptr }
};

static PyModuleDef matrixsolvers_module = {
    PyModuleDef_HEAD_INIT,
    "matrixsolvers",                        // Module name to use with Python import statements
    "Provides some functions, but faster",  // Module description
    0,
    matrixsolvers_methods                   // Structure that defines the methods of the module
};

PyMODINIT_FUNC PyInit_matrixsolvers() {
    return PyModule_Create(&matrixsolvers_module);
}

setup.py

from distutils.core import setup, Extension
from numpy import get_include

ms_module = Extension('matrixsolvers', sources=['module.cpp'], include_dirs=[get_include()])

setup(name='matrixsolvers', version='1.0',
      description='Python Package that implements matrix solvers in C++',
      ext_modules=[ms_module]
      )

main.py

from matrixsolvers import get_dimension
import numpy as np

get_dimension(np.array([1,2,3]))

To run my code, I run the following in the terminal:

pip3 install . 
python3 main.py

Now this all seems to work fine, but I get a segfault due to the line that calls PyArray_FROM_OTF.

Could anyone please explain what I am doing wrong?

Any guidance is much appreciated.

1
  • Did you ever find a solution for this? Commented Oct 27, 2020 at 22:28

1 Answer 1

2

add import_array(); before return PyModule_Create(&matrixsolvers_module);

See https://numpy.org/devdocs/user/c-info.how-to-extend.html for details.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, you saved me a day of headache!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.