Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / ext / ownershipRules.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="ext.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="ext.html" title='Extending and Embedding the Python Interpreter' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='last' href='about.html' title='About this document...' />
10<link rel='help' href='about.html' title='About this document...' />
11<link rel="next" href="thinIce.html" />
12<link rel="prev" href="refcountsInPython.html" />
13<link rel="parent" href="refcounts.html" />
14<link rel="next" href="thinIce.html" />
15<meta name='aesop' content='information' />
16<title>1.10.2 Ownership Rules
17</title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="1.10.1 Reference Counting in"
25 href="refcountsInPython.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="1.10 Reference Counts"
28 href="refcounts.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="1.10.3 Thin Ice"
31 href="thinIce.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Extending and Embedding the Python Interpreter</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><img src='../icons/blank.png'
38 border='0' height='32' alt='' width='32' /></td>
39<td class='online-navigation'><img src='../icons/blank.png'
40 border='0' height='32' alt='' width='32' /></td>
41</tr></table>
42<div class='online-navigation'>
43<b class="navlabel">Previous:</b>
44<a class="sectref" rel="prev" href="refcountsInPython.html">1.10.1 Reference Counting in</A>
45<b class="navlabel">Up:</b>
46<a class="sectref" rel="parent" href="refcounts.html">1.10 Reference Counts</A>
47<b class="navlabel">Next:</b>
48<a class="sectref" rel="next" href="thinIce.html">1.10.3 Thin Ice</A>
49</div>
50<hr /></div>
51</DIV>
52<!--End of Navigation Panel-->
53
54<H2><A NAME="SECTION0031020000000000000000"></A><A NAME="ownershipRules"></A>
55<BR>
561.10.2 Ownership Rules
57
58</H2>
59
60<P>
61Whenever an object reference is passed into or out of a function, it
62is part of the function's interface specification whether ownership is
63transferred with the reference or not.
64
65<P>
66Most functions that return a reference to an object pass on ownership
67with the reference. In particular, all functions whose function it is
68to create a new object, such as <tt class="cfunction">PyInt_FromLong()</tt> and
69<tt class="cfunction">Py_BuildValue()</tt>, pass ownership to the receiver. Even if
70the object is not actually new, you still receive ownership of a new
71reference to that object. For instance, <tt class="cfunction">PyInt_FromLong()</tt>
72maintains a cache of popular values and can return a reference to a
73cached item.
74
75<P>
76Many functions that extract objects from other objects also transfer
77ownership with the reference, for instance
78<tt class="cfunction">PyObject_GetAttrString()</tt>. The picture is less clear, here,
79however, since a few common routines are exceptions:
80<tt class="cfunction">PyTuple_GetItem()</tt>, <tt class="cfunction">PyList_GetItem()</tt>,
81<tt class="cfunction">PyDict_GetItem()</tt>, and <tt class="cfunction">PyDict_GetItemString()</tt>
82all return references that you borrow from the tuple, list or
83dictionary.
84
85<P>
86The function <tt class="cfunction">PyImport_AddModule()</tt> also returns a borrowed
87reference, even though it may actually create the object it returns:
88this is possible because an owned reference to the object is stored in
89<code>sys.modules</code>.
90
91<P>
92When you pass an object reference into another function, in general,
93the function borrows the reference from you -- if it needs to store
94it, it will use <tt class="cfunction">Py_INCREF()</tt> to become an independent
95owner. There are exactly two important exceptions to this rule:
96<tt class="cfunction">PyTuple_SetItem()</tt> and <tt class="cfunction">PyList_SetItem()</tt>. These
97functions take over ownership of the item passed to them -- even if
98they fail! (Note that <tt class="cfunction">PyDict_SetItem()</tt> and friends don't
99take over ownership -- they are ``normal.'')
100
101<P>
102When a C function is called from Python, it borrows references to its
103arguments from the caller. The caller owns a reference to the object,
104so the borrowed reference's lifetime is guaranteed until the function
105returns. Only when such a borrowed reference must be stored or passed
106on, it must be turned into an owned reference by calling
107<tt class="cfunction">Py_INCREF()</tt>.
108
109<P>
110The object reference returned from a C function that is called from
111Python must be an owned reference -- ownership is transferred from
112the function to its caller.
113
114<P>
115
116<DIV CLASS="navigation">
117<div class='online-navigation'>
118<p></p><hr />
119<table align="center" width="100%" cellpadding="0" cellspacing="2">
120<tr>
121<td class='online-navigation'><a rel="prev" title="1.10.1 Reference Counting in"
122 href="refcountsInPython.html"><img src='../icons/previous.png'
123 border='0' height='32' alt='Previous Page' width='32' /></A></td>
124<td class='online-navigation'><a rel="parent" title="1.10 Reference Counts"
125 href="refcounts.html"><img src='../icons/up.png'
126 border='0' height='32' alt='Up One Level' width='32' /></A></td>
127<td class='online-navigation'><a rel="next" title="1.10.3 Thin Ice"
128 href="thinIce.html"><img src='../icons/next.png'
129 border='0' height='32' alt='Next Page' width='32' /></A></td>
130<td align="center" width="100%">Extending and Embedding the Python Interpreter</td>
131<td class='online-navigation'><a rel="contents" title="Table of Contents"
132 href="contents.html"><img src='../icons/contents.png'
133 border='0' height='32' alt='Contents' width='32' /></A></td>
134<td class='online-navigation'><img src='../icons/blank.png'
135 border='0' height='32' alt='' width='32' /></td>
136<td class='online-navigation'><img src='../icons/blank.png'
137 border='0' height='32' alt='' width='32' /></td>
138</tr></table>
139<div class='online-navigation'>
140<b class="navlabel">Previous:</b>
141<a class="sectref" rel="prev" href="refcountsInPython.html">1.10.1 Reference Counting in</A>
142<b class="navlabel">Up:</b>
143<a class="sectref" rel="parent" href="refcounts.html">1.10 Reference Counts</A>
144<b class="navlabel">Next:</b>
145<a class="sectref" rel="next" href="thinIce.html">1.10.3 Thin Ice</A>
146</div>
147</div>
148<hr />
149<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
150</DIV>
151<!--End of Navigation Panel-->
152<ADDRESS>
153See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
154</ADDRESS>
155</BODY>
156</HTML>