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

Discussion in 'Python' started by Andrew Degtiariov, Sep 17, 2004.

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

    --
    Andrew Degtiariov
    DA-RIPE
    Andrew Degtiariov, Sep 17, 2004
    #1
    1. Advertising

  2. Re: [implement python code in C] can't set attributes ofbuilt-in/extension type

    Andrew Degtiariov <> writes:

    > 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

    --
    MAN: How can I tell that the past isn't a fiction designed to
    account for the discrepancy between my immediate physical
    sensations and my state of mind?
    -- The Hitch-Hikers Guide to the Galaxy, Episode 12
    Michael Hudson, Sep 17, 2004
    #2
    1. Advertising

  3. On Fri, Sep 17, 2004 at 12:43:56PM +0000, Michael Hudson wrote:
    > Andrew Degtiariov <> writes:
    >
    > > Hello!
    > > I tried to rewrite my python module in C.

    >
    > Are you targetting Python 2.2 or later?

    yes, Python 2.3

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

    Thanks you for tips.

    --
    Andrew Degtiariov
    DA-RIPE
    Andrew Degtiariov, Sep 17, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sweety
    Replies:
    6
    Views:
    4,058
    CBFalconer
    Apr 5, 2004
  2. Replies:
    4
    Views:
    265
  3. Replies:
    5
    Views:
    570
    Malcolm
    Jun 19, 2005
  4. Neal Becker
    Replies:
    8
    Views:
    1,424
    Steve Holden
    Feb 24, 2008
  5. King
    Replies:
    5
    Views:
    256
Loading...

Share This Page