On Sat, Sep 2, 2017 at 6:35 AM, Joe Jevnik via Python-Dev
<python-dev@python.org> wrote:
> Is it true that checking for refcount == 1 is enough? What if a user wrote:
>
> args = (compute_integer(), 5)
> # give away args to someone
> int.__iadd__(*args)
>
> here `args[0]` still has refcount=1 because only `args` owns this integer.

This particular example is safe, because the arguments get passed
individually - so 'args' has one reference, plus there's one more for
the actual function call (what would be 'self' if it were implemented
in Python). There may be other examples that are more dangerous, but
my suspicion is that the nature of += with anything other than a
simple name will defeat the optimization, since the owning collection
will retain a reference until __iadd__ returns.

ChrisA
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to