[Q]An error with my module in C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jia,Lu

    #1

    [Q]An error with my module in C

    hello,all.

    I wrote a module in C as below, BUT msg() method cannot work
    allright.

    #include <stdio.h>
    #include <python2.4/Python.h>

    static PyObject *Roka_msg(PyObj ect *self,PyObject *args)
    {
    printf("Roka Python lib. Version 1.0\n");
    }

    static PyObject *Roka_func(PyOb ject *self,PyObject *args)
    {
    long arg;
    if(!PyArg_Parse Tuple(args,"l", &arg)){
    return NULL;
    }
    return Py_BuildValue(" l",arg*2);
    }

    //----------------------------------------------------------

    static struct PyMethodDef functions[]=
    {
    {"msg",Roka_msg ,METH_VARARGS},
    {"func",Roka_fu nc,METH_VARARGS },
    {NULL,NULL,0},
    };


    void initRoka(void)
    {
    (void)Py_InitMo dule("Roka",fun ctions);
    }
    -------------------------------------------------------------------------
    python result:
    >>>import Roka
    >>>Roka.msg()
    Roka Python lib. Version 1.0
    Segmentation fault

    I throw out a Segmentation fault after display my message.
    Can anyone tell me why?

    thanks.

  • Robert Kern

    #2
    Re: [Q]An error with my module in C

    Jia,Lu wrote:
    hello,all.
    >
    I wrote a module in C as below, BUT msg() method cannot work
    allright.
    >
    #include <stdio.h>
    #include <python2.4/Python.h>
    This isn't related to your error, but you should include Python.h before other
    headers, and it should be just this:

    #include "Python.h"

    distutils will make sure the include path is correct.
    static PyObject *Roka_msg(PyObj ect *self,PyObject *args)
    {
    printf("Roka Python lib. Version 1.0\n");
    }
    You actually need to return a PyObject* here. Specifically, you want to return
    None after incrementing its reference count:

    Py_INCREF(Py_No ne);
    return Py_None;

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco

    Comment

    Working...