A simple Python extension in C

Posted by celil on Stack Overflow See other posts from Stack Overflow or by celil
Published on 2010-06-16T05:38:03Z Indexed on 2010/06/16 5:52 UTC
Read the original article Hit count: 255

Filed under:
|
|

I am trying to create a simple python extension module. I compiled the following code into a transit.so dynamic module

#include <python2.6/Python.h>

static PyObject*
_print(PyObject* self, PyObject* args)
{
    return Py_BuildValue("i", 10);
}

static PyMethodDef TransitMethods[] = {
    {"print", _print, METH_VARARGS, ""},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
inittransit(void)
{
    Py_InitModule("transit", TransitMethods);
}

However, trying to call this from python

import transit
transit.print()

I obtain an error message

  File "test.py", line 2
    transit.print()
                ^
SyntaxError: invalid syntax

What's wrong with my code?

© Stack Overflow or respective owner

Related posts about python

Related posts about c