[Pyrex] creating and populating Python/C structs

Simon Burton simon at arrowtheory.com
Sun Sep 4 18:13:00 CEST 2005


On Sun, 4 Sep 2005 16:44:08 +1000
Christopher Armstrong <radeex at gmail.com> wrote:

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


cdef extern from *:
#   PyFrame_New(PyThreadState *, PyCodeObject *, PyObject *, PyObject *)
   object PyFrame_New(object ts, object co, object globs, object locs)
   object PyThreadState_Get()

cdef extern from "compile.h":
  pass

cdef extern from "frameobject.h":
    struct PyCodeObject:
        pass
    struct PyDictObject:
        pass
    struct PyObject:
        pass
    # Just defining the members that we need to manipulate
    struct _frame:
        _frame *f_back
        PyCodeObject *f_code
        PyObject *f_builtins
        PyObject *f_globals
        PyObject *f_locals
        PyObject *f_exc_type, *f_exc_value, *f_exc_traceback
        int f_lineno
    ctypedef _frame PyFrameObject


def newframe(back,
             code,
             builtins,
             globals,
             locals,
             exc_type,
             exc_value,
             exc_traceback,
             int lineno):
    cdef PyFrameObject *c_frame
    ts = PyThreadState_Get()
    frame = PyFrame_New( ts, code, globals, locals )
    c_frame = <PyFrameObject*>frame
#    c_frame.f_back = <PyFrameObject *>back # <--- can set these, if you want
#    c_frame.f_code = <PyCodeObject *>code
#    c_frame.f_builtins = <PyObject *>builtins
#    c_frame.f_globals = <PyObject *>globals
#    c_frame.f_locals = <PyObject *>locals
#    c_frame.f_exc_type = <PyObject *>exc_type
#    c_frame.f_exc_value = <PyObject *>exc_value
#    c_frame.f_exc_traceback = <PyObject *>exc_traceback
#    c_frame.f_lineno = lineno
    return frame

does it work ?


-- 
Simon Burton, B.Sc.
Licensed PO Box 8066
ANU Canberra 2601
Australia
Ph. 61 02 6249 6940
http://arrowtheory.com 



More information about the Pyrex mailing list