Pyrex problem with cdef'd attribute

Klaas mike.klaas at gmail.com
Fri Jun 8 13:53:43 EDT 2007


On Jun 8, 6:00 am, s... at pobox.com wrote:
> I'm using Pyrex 0.9.5.1a.  I have this simple Pyrex module:

You might get more help on the pyrex list.

>     cdef class Foo:
>         cdef public char attr
>
>         def __init__(self):
>             self.attr = 0
>
>     class Bar(Foo):
>         def __init__(self):
>             Foo.__init__(self)
>             self.attr2 = None
>
>         def mod(self):
>             self.attr = c'B'
>
>     f = Bar()
>     f.mod()
>
> When I run Python and import it an exception is raised:

Yes, since you didn't cdef the class, it is essentially python code.
Python code cannot assign to a cdef class attribute that is not of
type 'object'

> If the mod() method is defined in the base class it works properly.  Is this
> a Pyrex bug or am I not allowed to modify cdef'd attributes in subclasses?
> (Note that I want Bar visible outside the module, hence no "cdef".)

cdef does not affect visibility, IIRC, just whether the class is
compiled into an extension type or not.

This is just from memory though.  Greg would be able to give you a
better answer.

-Mike




More information about the Python-list mailing list