FAQ
Dear All,

I am extending Python with my own C functions. I would like be able to
share one C function amongst different extended commands. Here's a
little pseudo example.


// standard method table with two commands specified "do_stuff" and
"do_stuff2" both of which call the same C function.

static PyMethodDef my_methods[] = {
{"do_stuff", (PyCFunction)Python_Embed::fnDoStuff, METH_VARARGS,
dostuff__doc__ },
{"do_stuff2", (PyCFunction)Python_Embed::fnDoStuff, METH_VARARGS,
dostuff2__doc__ },
{NULL, NULL}
};

PyObject* Python_Embed::fnDoStuff(PyObject* self, PyObject* args)
{

switch(who called me)
case do_stuff:
do something
break;

case do_stuff2
do something different.
break;



}


I looked at args, but I was unable to find anything in the PyObject
worth extracting for this purpose.

Any help would be greatly appreciated.

Heath.


Sent via Deja.com http://www.deja.com/
Before you buy.

Search Discussions

  • Brad Clements at Jul 21, 2000 at 2:00 pm
    Suggest that you create three functions.

    Two "shell" functions that are in the PyMethodDef table, they call the 3rd
    function that does the work, passing in an indicator of "who called me".

    --
    Brad Clements,
    bkc at murkworks.com
    <heath7952 at my-deja.com> wrote in message news:8l85e1$eti$1 at nnrp1.deja.com...
    Dear All,

    I am extending Python with my own C functions. I would like be able to
    share one C function amongst different extended commands. Here's a
    little pseudo example.


    // standard method table with two commands specified "do_stuff" and
    "do_stuff2" both of which call the same C function.

    static PyMethodDef my_methods[] = {
    {"do_stuff", (PyCFunction)Python_Embed::fnDoStuff, METH_VARARGS,
    dostuff__doc__ },
    {"do_stuff2", (PyCFunction)Python_Embed::fnDoStuff, METH_VARARGS,
    dostuff2__doc__ },
    {NULL, NULL}
    };

    PyObject* Python_Embed::fnDoStuff(PyObject* self, PyObject* args)
    {

    switch(who called me)
    case do_stuff:
    do something
    break;

    case do_stuff2
    do something different.
    break;



    }


    I looked at args, but I was unable to find anything in the PyObject
    worth extracting for this purpose.

    Any help would be greatly appreciated.

    Heath.


    Sent via Deja.com http://www.deja.com/
    Before you buy.
  • Paul Prescod at Jul 23, 2000 at 6:35 am

    heath7952 at my-deja.com wrote:
    Dear All,

    I am extending Python with my own C functions. I would like be able to
    share one C function amongst different extended commands. Here's a
    little pseudo example.
    How about this:

    static PyMethodDef my_methods[] = {
    {"do_stuff", (PyCFunction)Python_Embed::fnDoStuff1, METH_VARARGS,
    dostuff__doc__ },
    {"do_stuff2", (PyCFunction)Python_Embed::fnDoStuff2, METH_VARARGS,
    dostuff2__doc__ },
    {NULL, NULL}
    };

    PyObject* fnDoStuff(int what, PyObject* self, PyObject* args)
    {

    switch(who called me)
    case do_stuff:
    do something
    break;

    case do_stuff2
    do something different.
    break;

    }

    PyObject* fnDoStuff1(PyObject* self, PyObject* args)
    {
    fnDoStuff( 1, self, args )
    }

    PyObject* fnDoStuff2(PyObject* self, PyObject* args)
    {
    fnDoStuff( 2, self, args )
    }

    --
    Paul Prescod - Not encumbered by corporate consensus
    "Hardly anything more unwelcome can befall a scientific writer than
    having the foundations of his edifice shaken after the work is
    finished. I have been placed in this position by a letter from
    Mr. Bertrand Russell..."
    - Frege, Appendix of Basic Laws of Arithmetic (of Russell's Paradox)

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 21, '00 at 12:27a
activeJul 23, '00 at 6:35a
posts3
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase