[implement python code in C] can't set attributes of built-in/extension type

A

Andrew Degtiariov

Hello!
I tried to rewrite my python module in C. My module is successfully
imported but i can't assing anything to class attributes (tp_setattr in
PyTypeObject for class FlowReportRow filled up by pointer to
FlowReportRowObjectSetAttr function and printf in ones show the function does
not called)
Where I was mistaken? (you may see the module sources at
http://astral.ua/~ad/Report.c)

I wrote simple test:
import Reports


row = Reports.FlowReportRow
print dir(row)
row.show_local_ip = 1

And it displays:
ad@odin:share>python test.py
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'add', 'add2']
Traceback (most recent call last):
File "test.py", line 6, in ?
row.show_local_ip = 1
TypeError: can't set attributes of built-in/extension type
'FlowReportRow'
ad@odin:share>
 
M

Michael Hudson

Andrew Degtiariov said:
Hello!
I tried to rewrite my python module in C.

Are you targetting Python 2.2 or later?
My module is successfully imported but i can't assing anything to
class attributes (tp_setattr in PyTypeObject for class FlowReportRow
filled up by pointer to FlowReportRowObjectSetAttr function and
printf in ones show the function does not called) Where I was
mistaken? (you may see the module sources at
http://astral.ua/~ad/Report.c)

Argh! You're making life very tedious for yourself...

Do you know what you can do with the tp_members and tp_getsets field
of the type object? I think a C macro expanding to a couple getset
defintions would make your code more pleasant.

I think implementing tp_getattro and not tp_getattr will solve your
immediate problem.

Cheers,
mwh
 
A

Andrew Degtiariov

Are you targetting Python 2.2 or later?
yes, Python 2.3
Argh! You're making life very tedious for yourself...

Do you know what you can do with the tp_members and tp_getsets field
of the type object? I think a C macro expanding to a couple getset
defintions would make your code more pleasant.

I think implementing tp_getattro and not tp_getattr will solve your
immediate problem.
Thanks you for tips.
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top