RE: reference counting and PyTuple_SetItem

Discussion in 'Python' started by Tim Peters, Jun 10, 2004.

  1. Tim Peters

    Tim Peters Guest

    [Anne Wilson]
    ....
    > while (<some condition>)
    > {
    > pyRHost = PyString_FromString(rHost);


    You now own a reference to pyRHost.

    > Py_INCREF(pyRHost); /* ---> Note 3 <--- */


    Now you own two references to it.

    > PyTuple_SetItem(pyFiveMinArgs, 4, pyRHost);


    Now you own one reference; PyTuple_SetItem stole one.

    > PyTuple_SetItem(pyOneHourArgs, 4, pyRHost);


    Now you own no references. You must not call Py_DECREF on pyRHost after
    this point (unless you obtain a new reference to it first).

    Apart from all that, you're cheating in ways that can hurt you: tuples are
    supposed to be immutable objects, and it's not strictly legal to mutate them
    inside the loop. You'd be much better off not trying to micro-optimize
    (i.e., build a new tuple each time you need one -- never use PyTuple_SetItem
    on the same index twice for a given tuple; the code as-is is too clever to
    ever work <0.9 wink>).
     
    Tim Peters, Jun 10, 2004
    #1
    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. Anne Wilson

    reference counting and PyTuple_SetItem

    Anne Wilson, Jun 10, 2004, in forum: Python
    Replies:
    0
    Views:
    732
    Anne Wilson
    Jun 10, 2004
  2. Anne Wilson
    Replies:
    0
    Views:
    440
    Anne Wilson
    Jun 10, 2004
  3. Tim Peters
    Replies:
    0
    Views:
    405
    Tim Peters
    Jun 11, 2004
  4. mathieu
    Replies:
    8
    Views:
    531
    Juha Nieminen
    Aug 31, 2008
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    237
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page