[Pythonmac-SIG] Getting closer on C module extension building.
Lou Pecora
pecora@anvil.nrl.navy.mil
Mon, 10 Apr 2000 22:54:38 -0400
Ok, maybe I'm homing in on things and with some help will be able to supply
a little demo for new Python/Mac people on how to do a (very) simple C
module to extend Python. But I still need some help. Here's what I have
(a bit long sorry):
----- C Header: C_test.h ------------------------------------------
/* Header to test of C modules for Python: C_test.c */
/* ==== Includes =====================================*/
#include "Python.h"
/* ==== Prototypes =================================== */
static PyObject *square(PyObject *self, PyObject *args);
void initC_test();
----- C source: C_test.c ------------------------------------------
/* A file to test imorting C modules to Python.
The function square just squares the input number and returns result. */
#include "Python.h"
#include "C_test.h"
/* ==== square function ====================== */
static PyObject *square(PyObject *self, PyObject *args)
{
double x,y;
if (!PyArg_ParseTuple(args, "d", &x))
return NULL;
y=x*x;
return Py_BuildValue("d", y);
}
/* ==== Set up the methods table ====================== */
static PyMethodDef C_testMethods[] = {
{"square", square, METH_VARARGS},
{NULL, NULL} /* Sentinel */
};
/* ==== Initialize the C_test functions ====================== */
void initC_test() {
(void) Py_InitModule("C_test", C_testMethods);
}
------ Codewarrior project ----------------------------------
Set to produce a shared library with no main (__start removed from the
linker main: option -- had some online help with that one).
Library module is named: C_test.ppc.slb and compiles with no errors or
warnings. Shows up nicely with seemingly correct icon.
----- Import attempts in Python IDE ---------------------------
Setting a path to the module's folder and typing
import C_test yields
>>> import C_test
Traceback (innermost last):
File "<input>", line 1, in ?
ImportError: initC_test: The specified symbol was not found.
OK. initC_test is there. Removing the module from the folder causes the
error message that it can't find the file so I know Python knows to look
for the shared lib. module.
Anyone know why it can't find the initC_test symbol/function? Thanks.
More questions to come, I'm sure.
Cheers,
Lou Pecora