embed python: how to call a class' function?

David Ang davidang at info.com.ph
Thu May 29 07:07:20 EDT 2003


Hi, i think i need more help than yur provided code...

is info->contentHandler a PyObject*?

my python script processes an xml data.

now on my c code, i just need to get the data that python has proccess...

for instance, i have a on my python script:

class Data

which has

def getDuration(...)
     return self.duration


how do i call getDuration?

many thanks.

davidang at info.com.ph (David Ang) wrote in message news:<8b1c146b.0305280816.62912697 at posting.google.com>...
> Hello,
> 
> thanks very much for your help. i will seek your code when i get back
> to embedding python :)
> 
> 
> Dave Kuhlman <dkuhlman at rexx.com> wrote in message news:<bb0lar$4cvv0$1 at ID-139865.news.dfncis.de>...
> > 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




More information about the Python-list mailing list