[Pyrex] creating and populating Python/C structs

Christopher Armstrong radeex at gmail.com
Sun Sep 4 08:44:08 CEST 2005


On 9/5/05, Simon Burton <simon at arrowtheory.com> wrote:
> Christopher Armstrong <radeex at gmail.com> wrote:
> > Thanks, I figured Python.h had all that stuff already included. That
> > got my module to compile, but newframe is just returning a string of
> > bytes instead of a PyFrameObject now. If I've got a cdef PyFrameObject
> > *frame in a 'def' python function, how do I return that as a PyObject,
> > i.e., an instance of types.FrameType?
> >
> 
> You need to actually malloc the object using eg. PyFrame_New which
> should be cdef declared to return object (so that pyrex handles increfs for you).
> 
> And: keep an eye on the generated c code, to make sure it's doing what you expect. (!!)

> PS. yes this is tricky, and i don't know all the details of what you will have to do.

Right, I understand I'm doing some weird black magic here. I might try
something with a slightly simpler and less fundamental object than
Frame, to make sure my problems aren't just with it.

Anyway, one issue is that I'm trying to avoid PyFrame_New, which gets
a bunch of stuff from the context (e.g. the thread state you're
supposed to pass it) and I want to avoid that. I guess I can just try
to duplicate what it does without all the extra stuff. I guess that
means using PyObject_GC_NewVar.... I'll thrash around a bit more and
see if I can get any further.

If anyone has any Pyrex code that constructs Python objects starting
with just the struct definition (i.e., not using its C PyFoo_New
functions), I'd appreciate a look.

-- 
  Twisted   |  Christopher Armstrong: International Man of Twistery
   Radix    |    -- http://radix.twistedmatrix.com
            |  Release Manager, Twisted Project
  \\\V///   |    -- http://twistedmatrix.com
   |o O|    |  
w----v----w-+



More information about the Pyrex mailing list