title: Returning objects to Python from C
body: <p>I've read the documentation for the Python C-API, and even written a few extension modules.  However, I'm still a bit unclear on the exact semantics when it comes to returning Python objects from a C function.</p>
  
  <p>The limited examples in the Python docs usually show a C function which returns the result of <code>Py_BuildValue</code>.  Now, <code>Py_BuildValue</code> returns a <code>New Reference</code>, and transfers ownership of this reference over to the interpreter.  So, can I extrapolate from this that it is a general rule that any object returned to Python must be a <em>new reference</em>, and that returning an object from a C function is the same as transferring ownership of the object over to the interpreter?</p>
  
  <p>If so, what about cases where you return an object that is already owned by something?  For example, suppose you write a C function which takes in a <code>PyObject*</code> which is a <code>tuple</code>, and you call <code>PyTuple_GetItem</code> on it and return the result.  <code>PyTuple_GetItem</code> returns a <em>borrowed reference</em> - meaning that the item is still "owned" by the tuple.  So, does a C function which returns the result of something like <code>PyTuple_GetItem</code> have to <code>INCREF</code> the result before returning it to the interpreter?</p>
  
  <p>For example:</p>
  
  <pre><code>static PyObject* my_extension_module(PyObject* tup)
  {
     PyObject* item = PyTuple_GetItem(tup, 1);
     if (!item) { /* handle error */ }
  
     return item; // &lt;--- DO WE NEED TO INCREF BEFORE RETURNING HERE?
  }
  </code></pre>