[Python-Dev] Inplace operations for PyLong objects
Chris Angelico
rosuav at gmail.com
Fri Sep 1 17:12:13 EDT 2017
On Sat, Sep 2, 2017 at 6:35 AM, Joe Jevnik via Python-Dev
<python-dev at 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
More information about the Python-Dev
mailing list