[Pyrex] A problem in generating Python extension types.

Daehyok Shin sdhyok at gmail.com
Tue Jan 16 15:06:50 UTC 2007


Thanks for your comment.
In fact, the example I gave was over-simplified.
The following files show exactly what problems I have.
As you can see in sample.pyx, I like to directly access a Child object
from a Parent class in the Python interpreter.
In addition, I like to create the Child class to have a linked list structure.

When I tried test_sample.py, I got a segment error, probably becase of
the linked list structure.
Moreover, I failed to implement the direct access to a contained Child
object from a Parent object.
Do you know solutions for them? I would really appreciate your help.

>>>>>>>>>>>>> sample.pyx <<<<<<<<<<<<<<<<<<<<<

cdef public class Child[object child_object, type child_type]:
  cdef public int ID
  cdef Child child

  def __new__(self):
    self.child = Child()

cdef public class Parent[object parent_object, type parent_type]:
  cdef public int ID
  cdef Child child

  def __new__(self):
    self.child = Child()

>>>>>>>>>>>> test_sample.py <<<<<<<<<<<<<<<<<<<<<<

from sample import *

p = Parent()
p.ID = 10
p.child.ID=100
p.child.child.ID=1000

print p.ID, p.child.ID, p.child.child.ID

>>>>>>>>>>>>>>>>  <<<<<<<<<<<<<<<<<<<<<<<<<<<


On 1/15/07, Lenard Lindstrom <len-l at telus.net> wrote:
> Daehyok Shin wrote:
> > I am trying to replace complex C structures with Python extension
> > types generated from PyRex code. My purpose is to create Python
> > extension types which allows other C programs to access structure
> > variables inside the extension types.
> >
> > A problem I met during the development may be best explained with the
> > following simple example.
> >
> > ---------  C Code --------------------------------
> > struct child_object {
> >   int ID;
> > }
> >
> > struct parent_object {
> >   struct child_object *child;
> > }
> >
> > ----------   PyRex Code  ------------------------
> > public Child[object child_object, type child_type]:
> >   cdef public int ID
> >
> > public Parent[object parent_object, type parent_type]:
> >   cdef Child child
> >
> >   def __new__(self)
> >     self.child = Child() # I got an error message here when compiling
> > generated C code.
> >
> >   def __dealloc_(self):
> >     del self.child
> >
> >
> I can't comment on this code because it isn't value Pyrex.
>
> "1:7: Syntax error in simple statement list"
>
>
> > I succeeded in creating the same C structures from the above PyRex
> > code, but when I compiled the generated C code, I got an error message
> > because self.child expects a pointer to child_object  but Child()
> > returns a pointer to PyObject.
> > Is there any way I can solve this problem so that the extension types
> > act as a commond data models for both Python programs and C programs.
> > Thanks in advance.
> >
> >
> After converting everything into extension class declarations and
> removing the "del self.child" I got it to compile in Pyrex 0.9.4.1. My
> gcc 3.4.5 compiler only generated a warning when compiling the resulting
> C code. So this is likely a Pyrex bug since early versions expected a
> forgiving C compiler.
>
>
> --
> Lenard Lindstrom
> <len-l at telus.net>
>
>
> _______________________________________________
> Pyrex mailing list
> Pyrex at lists.copyleft.no
> http://lists.copyleft.no/mailman/listinfo/pyrex
>

-- 
Daehyok Shin



More information about the Pyrex mailing list