From: MHammond at skippinet.com.au (Mark Hammond)
Date: Mon, 19 Apr 1999 09:35:18 +1000
Subject: Embedded Late Binding
References: <3719D318.1267@iname.com>
Message-ID: <7fdq7b$mpm$1@m2.c2.telstra-mm.net.au>
Content-Length: 2186
X-UID: 1477                                                  

2 things come to mind here.

* Simplest may be to use the pre-processor.  something like:
#define MAKE_FUNC(FuncName) \
static PyObject *Py##FuncName(PyObject *self, PyObject *args) \
blah blah \
rc = FuncName() \
blah blah\

MAKE_FUNC(OnFileNew);
MAKE_FUNC(OnFileSave);

You still have 400 methods to expose, but they get a lot smaller :-)

Alternatively, this is a bit more complex:
* Create a new "editor function" object.  This would be a Python object that
has a tp_call slot.  The module or object getattr() call could create and
return one of these objects.  Eg:
PyObject *getattr(char *attr)
{
    if (IsEditorFuncName(attr))
        return PyEditorFunction(attr);
}

The 3rd of the 2 alternatives :-)
* Provide a Python wrapper class.  Instead of dealing with Python modules
and functions natively, work with classes.  This is similar to the above
option, but you move the logic into the Python code.

Hope this makes some sense, and at least points you to the right path for
your project...

Mark.

Jussi Jumppanen wrote in message <3719D318.1267 at iname.com>...
> static struct PyMethodDef editor_functions[] =
> {
>   { "get_line_number",  MacroPython::get_line_number, 1 },
>   { "set_line_number",  MacroPython::set_line_number, 1 },
>   { "FileClose"      ,  MacroPython::edit_func      , 1 },
>   { "FileEdit"       ,  MacroPython::edit_func      , 1 },
>   { "FileNew"        ,  MacroPython::edit_func      , 1 },
>   {  .........................                          },
>   {  0,                 0,                            0 },
> };
>
>and then somehow in the C source:
>
> PyObject *MacroPython::edit_func(PyObject *self, PyObject *args)
> {
>   PyObject *pObject = 0;
>
>   if (PyArg_ParseTuple(args, ""))
>   {
>     /* somehow get the function name???? */
>     char *function_name = ???? (ie FileNew, FileOpen etc);
>
>     /* this part is easy enough todo */
>     call editor passing(function_name);
>   }
>   return pObject;
> }
>
>
>Can anyone tell me if this possible with Python 1.5.1?
>
>Or is there a better way to achieve a similar result?
>
>Thanks in advance.
>
>Jussi Jumppanen
>Home Page: http://ourworld.compuserve.com/homepages/jussi/






