Extend/Embed Python Method table Dispatcher 
Author Message
 Extend/Embed Python Method table Dispatcher

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}

Quote:
};

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;

Quote:
}

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.*-*-*.com/
Before you buy.



Tue, 07 Jan 2003 03:00:00 GMT  
 Extend/Embed Python Method table Dispatcher
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,

Quote:

> 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.



Tue, 07 Jan 2003 03:00:00 GMT  
 Extend/Embed Python Method table Dispatcher

Quote:

> 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}

Quote:
};

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;

Quote:
}

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

Quote:
}

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

Quote:
}

--
 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)


Thu, 09 Jan 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Embedding: how to extend a C++ instance by Python methods

2. ANN: Win32 Screensaver that uses Python (extending/embedding Python/C)

3. problems extending & embedding python

4. problems with extending and embedding python on win2k

5. Extending Python: rewriting a single method in C

6. I need information about extending and embedding Python

7. More info on embedding/extending Python?

8. Embedding and extending python?

9. Embedding/Extending Python in/with Visual Basic

10. Embedding&Extending Python in DOS?

11. Extending and Embedding Python

12. Extending and embedding Python: mktuple() unresolved

 

 
Powered by phpBB® Forum Software