Pyrex problem with cdef'd attribute

skip at pobox.com skip at pobox.com
Fri Jun 8 15:00:43 CEST 2007


I'm using Pyrex 0.9.5.1a.  I have this simple Pyrex module:

    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:

    % python
    Python 2.4.2 (#1, Feb 23 2006, 12:48:31)
    [GCC 3.4.1] on sunos5
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import badattr
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "badattr.pyx", line 16, in badattr
        f.mod()
      File "badattr.pyx", line 13, in badattr.Bar.mod
        self.attr = c'B'
    TypeError: bad argument type for built-in operation

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

Thanks,

Skip



More information about the Python-list mailing list