[Pyrex] Tricky: Wrapping a (void *) cookie

Joseph Koshy jkoshy at FreeBSD.ORG
Fri May 2 14:28:42 CEST 2003


Some Pyrex behaviour I don't quite understand:

I have two classes:

 - 'Foo', with a 'void *' private field (a cookie), 

and, 

 - a class 'Bar' that needs to generate 'Foo' objects 
   with the appropriate values set for the cookie.

Here's some sample Pyrex code:

  ----
  ctypedef void * V
  cdef extern void make_cookie(V *v)

  # Class 'Foo'
  cdef class Foo:
    cdef V foo  # private field 'foo'

  # 'C' Helper function [hack]
  cdef void _set_foo_field(Foo p, V v):
    p.foo = v

  # Class 'Bar'
  cdef class Bar:

    def make_a_foo(self):
      """Make a Foo object with wrapper value 'v'"""

      cdef V v          # local var for the cookie
      cdef Foo f1       # local var for a Foo object (see below)

      make_cookie(&v)     # initialize cookie

      # Try 1
      # the following won't work
      f = Foo()
      f.foo = v           # Error: Cannot convert 'void (*)' to Python object

      # Try 2
      _set_foo_field(p, v)  # this hack works

      # Try 3
      f1 = Foo()            # assignment to the 'cdef' local variable
      f1.foo = v            # works
  ----

What I'd like to do is get the cookie into the 'Foo' object at 
object initialization time itself.  Would that be possible at all?
If not, is ``Try 3'' above valid Pyrex code?

Regards,
Koshy
<jkoshy at freebsd.org>






More information about the Pyrex mailing list