Reputation: 1029
I'm reading this tutorial on how to bind Python and C. I'm using Python v3.7.1 so I had to use the new Python interface (different from the example), so now I have:
adder.c
#include <Python.h>
static PyObject *addList_add(PyObject *self, PyObject *args)
{
PyObject *listObj;
if (!PyArg_ParseTuple(args, "O", &listObj))
return NULL;
Py_ssize_t length = PyList_Size(listObj);
long i, sum = 0;
for (i = 0; i < length; i++)
{
PyObject *temp = PyList_GetItem(listObj, i);
long elem = PyLong_AsLong(temp);
sum += elem;
}
return Py_BuildValue("i", sum);
}
static char addList_docs[] = "add(): add all elements of the list\n";
static PyMethodDef addLust_funcs[] = {
{"add", (PyCFunction)addList_add, METH_VARARGS, addList_docs},
{NULL, NULL, 0, NULL}};
static struct PyModuleDef addList = {
PyModuleDef_HEAD_INIT,
"addList",
addList_docs,
-1,
addLust_funcs};
PyMODINIT_FUNC PyInit_addList(void)
{
return PyModule_Create(&addList);
}
Seems like everything is correct, I can run the Python code by calling the addList.add()
function. But when building the module, I get this output (note that I can run the Python code fine after this error):
$ python setup.py install running install running build running build_ext running install_lib running install_egg_info Traceback (most recent call last): File "setup.py", line 4, in ext_modules=[Extension('addList', ['adder.c'])]) File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\core.py", line 148, in setup dist.run_commands() File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\dist.py", line 966, in run_commands self.run_command(cmd) File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\dist.py", line 985, in run_command cmd_obj.run() File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\command\install.py", line 557, in run self.run_command(cmd_name) File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\cmd.py", line 313, in run_command self.distribution.run_command(command) File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\dist.py", line 984, in run_command cmd_obj.ensure_finalized() File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\cmd.py", line 107, in ensure_finalized self.finalize_options() File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\command\install_egg_info.py", line 26, in finalize_options to_filename(safe_version(self.distribution.get_version())), File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\distutils\command\install_egg_info.py", line 68, in safe_version version = version.replace(' ','.') AttributeError: 'float' object has no attribute 'replace'
setup.py
from distutils.core import setup, Extension
setup(name='addList', version=1.0,
ext_modules=[Extension('addList', ['adder.c'])])
main.py
import addList
l = [1, 2, 3, 5, 4]
print(addList.add(l))
Upvotes: 1
Views: 149
Reputation: 249642
The error message says:
version.replace(' ','.') AttributeError: 'float' object has no attribute 'replace'
Your build script says:
version=1.0
Clearly it expects a string, not a float.
Upvotes: 1