Следуя расширению C для Python, у моего модуля, похоже, отсутствует его содержимое. Хотя сборка и импорт модуля не проблема, использование функции в модуле не удается. Я использую Python 3.7 на macOS.
testmodule.c
#define PY_SSIZE_T_CLEAN
#include <Python.h>
static PyObject* add(PyObject *self, PyObject *args) {
const long long x, y;
if (!PyArg_ParseTuple(args, "LL", &x, &y)) {
return NULL;
}
return PyLong_FromLongLong(x + y);
}
static PyMethodDef TestMethods[] = {
{"add", add, METH_VARARGS, "Add two numbers."},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef testmodule = {
PyModuleDef_HEAD_INIT,
"test",
NULL,
-1,
TestMethods
};
PyMODINIT_FUNC PyInit_test(void)
{
return PyModule_Create(&testmodule);
}
setup.py
from distutils.core import setup, Extension
module1 = Extension('test', sources=['testmodule.c'])
setup(name='Test',
version='1.0',
description='Test package',
ext_modules=[module1])
Тест и ошибка
>>> import test
>>> test.add(4, 5)
AttributeError: module 'test' has no attribute 'add'