0

I wrote a c++ module witch should be imported into Python. Below are both Codes, the C++ part and the Python part. The C++ function method_sum should return the double of a value to python.

module.cpp:

#define PY_SSIZE_T_CLEAN
#include <Python.h>

static PyObject *method_sum(PyObject *self, PyObject *args) {
  const int *prop;

  if (!PyArg_ParseTuple(args, "i", &prop)) return NULL;

  int result = *prop + *prop;
  return Py_BuildValue("i", result);
}

static PyMethodDef ModuleMethods[] = {
    {"sum", method_sum, METH_VARARGS, "description of the function"},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef module = {
    PyModuleDef_HEAD_INIT,
    "module",
    "description of the module",
    -1,
    ModuleMethods
};

PyMODINIT_FUNC PyInit_module(void) {
    return PyModule_Create(&module);
}

main.py:

import module

print(module.sum(18))

setup.py:

from distutils.core import setup, Extension

setup(name='module', version='1.0', ext_modules=[Extension('module', ['module.cpp'])])
1
  • 1
    Please supply the expected minimal, reproducible example (MRE). We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. This includes a driver to provoke the error, trace data, and the full error message. Commented May 26, 2021 at 22:17

1 Answer 1

2

I changed method_sum to the following and main.py prints 36 instead of segfaulting.

static PyObject *method_sum(PyObject *self, PyObject *args) {
  int prop;

  if (!PyArg_ParseTuple(args, "i", &prop)) return NULL;

  int result = prop + prop;
  return Py_BuildValue("i", result);
}

The following also works and prop is still a pointer like in the code in the question.

static PyObject *method_sum(PyObject *self, PyObject *args) {
  const int *prop = new int;

  if (!PyArg_ParseTuple(args, "i", prop)) {
    delete prop;
    return NULL;
  }

  int result = *prop + *prop;
  delete prop;
  return Py_BuildValue("i", result);
}
Sign up to request clarification or add additional context in comments.

1 Comment

OP was attempting to parse the int as a const int*, most likely due to incorrectly modifying the ubiquitous spam.system("ls -la") example argument which was a const char* (a C string) . If they did want to pass an int * however, they should use "O" instead of "i", to parse as an arbitrary object.

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.