What's wrong?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kishkin

    #1

    What's wrong?

    Hello everyone!

    I wanted to write python script with defined function. From that script
    I wanted to call C module, which in it's turn call defined python
    function. Sounds simple. But I've got some troubles in realization. I'm
    beginner at python so it's normal, I think. :)

    Here's the code. I'll apreciate if anyone can answer me what's wrong
    here.

    -------------------------------------------------------------------
    power.c:

    #include "Python.h"

    static PyObject *
    power(PyObject *self, PyObject *args)
    {
    PyObject *result, *temp;

    int num;

    if (PyArg_ParseTup le(args, "iO", &num, &temp))
    {
    if (!PyCallable_Ch ack(temp)) //here's error, but I don't know
    why...
    {
    return NULL;
    }
    }

    result = Py_BuildValue(" i", num*num);

    PyEval_CallObje ct(temp, result);
    Py_DECREF(resul t);

    Py_INCREF(Py_No ne);
    retrun Py_None;
    }

    static struct PyMethodDef power_methods[] = {
    {"power", power, METH_VARARGS, "powering number"},
    {NULL, NULL, 0, NULL}
    };

    void initpower()
    {
    (void) Py_InitModule(" power", power_methods);
    }
    -------------------------------------------------------------------

    -------------------------------------------------------------------
    power.py:

    import power

    def sst(some):
    print some

    power.power(9, sst)
    -------------------------------------------------------------------

  • Steven D'Aprano

    #2
    Re: What's wrong?

    kishkin wrote:
    [color=blue]
    > Hello everyone!
    >
    > I wanted to write python script with defined function. From that script
    > I wanted to call C module, which in it's turn call defined python
    > function. Sounds simple. But I've got some troubles in realization. I'm
    > beginner at python so it's normal, I think. :)
    >
    > Here's the code. I'll apreciate if anyone can answer me what's wrong
    > here.[/color]

    [snip]
    [color=blue]
    > if (!PyCallable_Ch ack(temp)) //here's error,[/color]
    but > I don't know why...

    Er, do we have to guess what error you are getting?

    No, no, I love to guess... should the function be spelt
    PyCallable_Choc k with an o instead of an a?



    --
    Steven.

    Comment

    • Fredrik Lundh

      #3
      Re: What's wrong?

      "kishkin" <kishkin@gmail. com> wrote:
      [color=blue]
      > I wanted to write python script with defined function. From that script
      > I wanted to call C module, which in it's turn call defined python
      > function. Sounds simple. But I've got some troubles in realization. I'm
      > beginner at python so it's normal, I think. :)
      >
      > Here's the code. I'll apreciate if anyone can answer me what's wrong
      > here.[/color]

      instead of letting everyone guess, maybe you could explain *why* you
      think something's wrong with your code ?

      (I suspect that if you try to write down what happens when you try
      to compile and run this code, you'll figure out how to fix it all by your-
      self)

      </F>
      [color=blue]
      > -------------------------------------------------------------------
      > power.c:
      >
      > #include "Python.h"
      >
      > static PyObject *
      > power(PyObject *self, PyObject *args)
      > {
      > PyObject *result, *temp;
      >
      > int num;
      >
      > if (PyArg_ParseTup le(args, "iO", &num, &temp))
      > {
      > if (!PyCallable_Ch ack(temp)) //here's error, but I don't know
      > why...
      > {
      > return NULL;
      > }
      > }[/color]

      </F>



      Comment

      • kishkin

        #4
        Re: What's wrong?

        :)) Oа course it PyCallable_Chec k there!!! :)) And error is that
        PyCallable_Chec k returned 0.

        power.c compiles without errors.

        error is here:
        -----------------------------------
        [kishkin@linuxa-miho 1]$ make -f makefile.power
        gcc power.c -g -I/usr/local/include/python2.4 -fpic -shared -o power.so
        [kishkin@linuxa-miho 1]$ python power.py
        Exception exceptions.Type Error: 'argument list must be a tuple' in
        'garbage collection' ignored
        Fatal Python error: enexpected exception during garbage collection
        Aborted
        -----------------------------------

        Comment

        • kishkin

          #5
          Re: What's wrong?

          Thank you for fast response! And I'm sorry for bad style of asking
          questions!

          Comment

          • Fredrik Lundh

            #6
            Re: What's wrong?

            "kishkin" wrote:
            [color=blue]
            > Thank you for fast response! And I'm sorry for bad style of asking
            > questions![/color]

            did you (with the help of your compiler) figure out what was wrong?

            </F>



            Comment

            • Fredrik Lundh

              #7
              Re: What's wrong?

              "kishkin" wrote:
              [color=blue]
              > :)) O? course it PyCallable_Chec k there!!! :)) And error is that
              > PyCallable_Chec k returned 0.
              >
              > power.c compiles without errors.
              >
              > error is here:
              > -----------------------------------
              > [kishkin@linuxa-miho 1]$ make -f makefile.power
              > gcc power.c -g -I/usr/local/include/python2.4 -fpic -shared -o power.so
              > [kishkin@linuxa-miho 1]$ python power.py
              > Exception exceptions.Type Error: 'argument list must be a tuple' in
              > 'garbage collection' ignored
              > Fatal Python error: enexpected exception during garbage collection[/color]

              enexpected? are you typing things in by hand, or does your computer
              have a bad RAM chip ?
              [color=blue]
              > Aborted
              > -----------------------------------[/color]

              try changing

              result = Py_BuildValue(" i", num*num);
              PyEval_CallObje ct(temp, result);
              Py_DECREF(resul t);

              to

              result = Py_BuildValue(" (i)", num*num);
              PyEval_CallObje ct(temp, result);
              Py_DECREF(resul t);

              or, more convenient

              result = PyObject_CallFu nction(temp, "i", num*num);
              if (!result)
              return NULL;

              (note that you don't really need to check for callables; the CallFunction
              API will do that for you, and generate a proper exception if you pass in
              the wrong thing).

              </F>



              Comment

              • kishkin

                #8
                Re: What's wrong?

                Man! It's alive! :)

                Now next question: where are you living? I must know that for buy you
                some beer!!

                Thanks a lot!!!

                Comment

                Working...