embed python: how to call a class' function?

Dave Kuhlman dkuhlman at rexx.com
Tue May 27 17:29:05 EDT 2003


David Ang wrote:

> provide code pls.

Here is some sample code -- It (1) checks to determine if the
object (info->contentHandler) has an attribute "characters"; (2)
creates a buffer of characters; (3) calls the method "characters"
in the object passing one argument (the character buffer):

    if ((info->handlers.characters != NULL) &&
        (PyObject_HasAttrString(info->contentHandler, "characters")))
    {
        allocate = 0;
        if (len < 1024)
        {
            pBuf = buf;
        }
        else
        {
            pBuf = (char *)malloc(len + 1);
            allocate = 1;
        } /* if */
        strncpy(pBuf, ch, len);
        pBuf[len] = '\0';
        result = PyObject_CallMethod(info->contentHandler, "characters",
            "s", pBuf);
        if (allocate)
        {
            free(pBuf);
        } /* if */
        if (PyErr_Occurred())
        {
            PyErr_Print();
        } /* if */
    } /* if */

> 
> also, where are the function references for embed python? i can't
> find it on the docs, ie python library refence etc.,

See:

    http://www.python.org/doc/current/ext/ext.html
    http://www.python.org/doc/current/api/api.html

And, especially:

    http://www.python.org/doc/current/api/object.html

If you really do mean to call a *class* method as opposed to an
*instance* method, then I believe the above code will still serve
as a model.  But, the object will be the class itself, not an
instance of the class.

  - Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
dkuhlman at rexx.com




More information about the Python-list mailing list