You extension module is called _spam in your setup.py so the function should be init_spam.
On Tue, 18 Aug 2015 22:50 garyr <ga...@fidalgo.net> wrote: > Not according to the Python documentation. > > See the Python documentation Extending and Embedding the Python Interperter > / Extending Python with C or C++ / > A Simple Example: > > The method table must be passed to the interpreter in the module’s > initialization function. The initialization function must be named > initname(), > where name is the name of the module, and should be the only non-static > item > defined in the module file: > > PyMODINIT_FUNC > initspam(void) > { > (void) Py_InitModule("spam", SpamMethods); > } > > I tried doing that and it crashed Python when I imported _spam > > ----- Original Message ----- > From: "Oscar Benjamin" <oscar.j.benja...@gmail.com> > To: "garyr" <ga...@fidalgo.net>; <distutils-sig@python.org> > Sent: Tuesday, August 18, 2015 12:51 PM > Subject: Re: [Distutils] (no subject) > > > > Should the function be called init_spam rather than initspam? > > > > > > On Tue, 18 Aug 2015 19:19 garyr <ga...@fidalgo.net> wrote: > > > > I posted this on comp.lang.python but received no replies. > > > > I tried building the spammodule.c example described in the documentation > > section "Extending Python with C or C++." As shown the code compiles OK > but > > generates a link error: > > > > LINK : error LNK2001: unresolved external symbol init_spam > > build\temp.win32-2.7\Release\_spam.lib : fatal error LNK1120: 1 > unresolved > > externals > > > > I tried changing the name of the initialization function spam_system to > > init_spam and removed the static declaration. This compiled and linked > > without errors but generated a system error when _spam was imported. > > > > I'm using Python 2.7.9. The same error occurs with Python 2.6.9. > > > > The code and the setup.py file are shown below. What do I need to do to > fix > > this? > > > > setup.py: > > > ----------------------------------------------------------------------------- > > from setuptools import setup, Extension > > > > setup(name='spam', > > version='0.1', > > description='test module', > > ext_modules=[Extension('_spam', ['spammodule.c'], > > include_dirs=[C:\Documents and > > Settings\Owner\Miniconda\include], > > )], > > ) > > > > sammodule.c > > -------------------------------------------------- > > #include <python.h> > > static PyObject *SpamError; > > > > static PyObject * > > spam_system(PyObject *self, PyObject *args) > > { > > const char *command; > > int sts; > > > > if (!PyArg_ParseTuple(args, "s", &command)) > > return NULL; > > sts = system(command); > > if (sts < 0) { > > PyErr_SetString(SpamError, "System command failed"); > > return NULL; > > } > > return PyLong_FromLong(sts); > > } > > > > static PyMethodDef SpamMethods[] = { > > > > {"system", spam_system, METH_VARARGS, > > "Execute a shell command."}, > > {NULL, NULL, 0, NULL} /* Sentinel */ > > }; > > > > PyMODINIT_FUNC > > initspam(void) > > { > > PyObject *m; > > > > m = Py_InitModule("spam", SpamMethods); > > if (m == NULL) > > return; > > > > SpamError = PyErr_NewException("spam.error", NULL, NULL); > > Py_INCREF(SpamError); > > PyModule_AddObject(m, "error", SpamError); > > } > > > > > > > > > > > > > > > > _______________________________________________ > > Distutils-SIG maillist - Distutils-SIG@python.org > > https://mail.python.org/mailman/listinfo/distutils-sig > > > > >
_______________________________________________ Distutils-SIG maillist - Distutils-SIG@python.org https://mail.python.org/mailman/listinfo/distutils-sig