title: Reference count of function arguments in CPython 3.6
body: <p>In order to find out if a parameter passed to a function is a "temporary" (only passed into the function) or also referenced outside I use <code>Py_REFCNT</code>. This is done in a C extension package, but for easier reproducibility I decided to provide a Cython implementation based on IPython magic here.</p>
  
  <p>It seems like something changed for functions that accept multiple arguments (it still works as expected for functions that only take one argument) between CPython 3.5 and CPython 3.6:</p>
  
  <pre><code>In [1]: %load_ext cython
  
  In [2]: %%cython
     ...: cdef extern from "Python.h":
     ...:     Py_ssize_t Py_REFCNT(object o)
     ...:
     ...: cpdef func(o, p):
     ...:     return Py_REFCNT(o)
  </code></pre>
  
  <p>When I run the code on 3.5 it gives me, the expected result:</p>
  
  <pre><code>&gt;&gt;&gt; import numpy as np
  &gt;&gt;&gt; func(np.ones(3), np.ones(3))
  1
  </code></pre>
  
  <p>But with 3.6 it gives me <code>2</code>:</p>
  
  <pre><code>&gt;&gt;&gt; import numpy as np
  &gt;&gt;&gt; func(np.ones(3), np.ones(3))
  2
  </code></pre>
  
  <hr>
  
  <p>In the comments I was asked about the C code so here it is:</p>
  
  <pre><code>static PyObject *
  GetRefCount(PyObject *m, PyObject *args) {
      if (PyTuple_CheckExact(args) &amp;&amp; PyTuple_Size(args) &gt; 0) {
          Py_ssize_t reference_count = Py_REFCNT(PyTuple_GET_ITEM(args, 0));
          return PyLong_FromSsize_t(reference_count);
      }
      PyErr_SetString(PyExc_TypeError, "wrong input");
      return NULL;
  }
  </code></pre>
  
  <p>And the method definition:</p>
  
  <pre><code>    {"getrefcount",                                     /* ml_name */
       (PyCFunction)GetRefCount,                          /* ml_meth */
       METH_VARARGS,                                      /* ml_flags */
       ""                                                 /* ml_doc */
       },
  </code></pre>
  
  <p>The results are the same:</p>
  
  <pre><code>&gt;&gt;&gt; import numpy as np
  &gt;&gt;&gt; getrefcount(np.ones(3))  # 3.5
  1
  &gt;&gt;&gt; getrefcount(np.ones(3))  # 3.6
  2
  </code></pre>
  
  <p>I would like to know where (and why) the reference count is incremented in 3.6. I have looked through the CPython source code / the Python issue tracker but I couldn't find an answer.</p>