Pyrex problem with cdef'd attribute

S

skip

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

Klaas

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
 
S

skip

Klaas> You might get more help on the pyrex list.

Thanks. On the Pyrex website it says that questions are welcome here as
well. I was hoping to avoid yet another mailing list subscription. ;-)

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

So, if I cdef the Bar subclass I should be okay? I'll play around a bit
more and if I continue to confuse myself will subscribe to the pyrex mailing
list (or at least use the somewhat clunky gmane.org interface).

Skip
 
M

Michael Hoffman

I'll play around a bit
more and if I continue to confuse myself will subscribe to the pyrex mailing
list (or at least use the somewhat clunky gmane.org interface).

I find that using a newsreader for gmane stuff is far more convenient. I
use Thunderbird to access various infrequently-used mailing lists via
NNTP and Gmane.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top