| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="api.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="api.html" title='Python/C API Reference Manual' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="common-structs.html" /> |
| 13 | <link rel="prev" href="newTypes.html" /> |
| 14 | <link rel="parent" href="newTypes.html" /> |
| 15 | <link rel="next" href="common-structs.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>10.1 Allocating Objects on the Heap </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="10. Object Implementation Support" |
| 25 | href="newTypes.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="10. Object Implementation Support" |
| 28 | href="newTypes.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="10.2 Common Object Structures" |
| 31 | href="common-structs.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python/C API Reference Manual</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'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="newTypes.html">10. Object Implementation Support</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="newTypes.html">10. Object Implementation Support</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="common-structs.html">10.2 Common Object Structures</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION0012100000000000000000"></A><A NAME="allocating-objects"></A> |
| 56 | <BR> |
| 57 | 10.1 Allocating Objects on the Heap |
| 58 | |
| 59 | </H1> |
| 60 | |
| 61 | <P> |
| 62 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject* <b><tt id='l2h-905' xml:id='l2h-905' class="cfunction">_PyObject_New</tt></b>(</nobr></td><td>PyTypeObject *<var>type</var>)</td></tr></table></dt> |
| 63 | <dd> |
| 64 | <div class="refcount-info"> |
| 65 | <span class="label">Return value:</span> |
| 66 | <span class="value">New reference.</span> |
| 67 | </div> |
| 68 | </dd></dl> |
| 69 | |
| 70 | <P> |
| 71 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyVarObject* <b><tt id='l2h-906' xml:id='l2h-906' class="cfunction">_PyObject_NewVar</tt></b>(</nobr></td><td>PyTypeObject *<var>type</var>, int <var>size</var>)</td></tr></table></dt> |
| 72 | <dd> |
| 73 | <div class="refcount-info"> |
| 74 | <span class="label">Return value:</span> |
| 75 | <span class="value">New reference.</span> |
| 76 | </div> |
| 77 | </dd></dl> |
| 78 | |
| 79 | <P> |
| 80 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-907' xml:id='l2h-907' class="cfunction">_PyObject_Del</tt></b>(</nobr></td><td>PyObject *<var>op</var>)</td></tr></table></dt> |
| 81 | <dd> |
| 82 | </dd></dl> |
| 83 | |
| 84 | <P> |
| 85 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject* <b><tt id='l2h-908' xml:id='l2h-908' class="cfunction">PyObject_Init</tt></b>(</nobr></td><td>PyObject *<var>op</var>, |
| 86 | PyTypeObject *<var>type</var>)</td></tr></table></dt> |
| 87 | <dd> |
| 88 | <div class="refcount-info"> |
| 89 | <span class="label">Return value:</span> |
| 90 | <span class="value">Borrowed reference.</span> |
| 91 | </div> |
| 92 | Initialize a newly-allocated object <var>op</var> with its type and |
| 93 | initial reference. Returns the initialized object. If <var>type</var> |
| 94 | indicates that the object participates in the cyclic garbage |
| 95 | detector, it is added to the detector's set of observed objects. |
| 96 | Other fields of the object are not affected. |
| 97 | </dd></dl> |
| 98 | |
| 99 | <P> |
| 100 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyVarObject* <b><tt id='l2h-909' xml:id='l2h-909' class="cfunction">PyObject_InitVar</tt></b>(</nobr></td><td>PyVarObject *<var>op</var>, |
| 101 | PyTypeObject *<var>type</var>, int <var>size</var>)</td></tr></table></dt> |
| 102 | <dd> |
| 103 | This does everything <tt class="cfunction">PyObject_Init()</tt> does, and also |
| 104 | initializes the length information for a variable-size object. |
| 105 | </dd></dl> |
| 106 | |
| 107 | <P> |
| 108 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr><var>TYPE</var>* <b><tt id='l2h-910' xml:id='l2h-910' class="cfunction">PyObject_New</tt></b>(</nobr></td><td>TYPE, PyTypeObject *<var>type</var>)</td></tr></table></dt> |
| 109 | <dd> |
| 110 | <div class="refcount-info"> |
| 111 | <span class="label">Return value:</span> |
| 112 | <span class="value">New reference.</span> |
| 113 | </div> |
| 114 | Allocate a new Python object using the C structure type <var>TYPE</var> |
| 115 | and the Python type object <var>type</var>. Fields not defined by the |
| 116 | Python object header are not initialized; the object's reference |
| 117 | count will be one. The size of the memory |
| 118 | allocation is determined from the <tt class="member">tp_basicsize</tt> field of the |
| 119 | type object. |
| 120 | </dd></dl> |
| 121 | |
| 122 | <P> |
| 123 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr><var>TYPE</var>* <b><tt id='l2h-911' xml:id='l2h-911' class="cfunction">PyObject_NewVar</tt></b>(</nobr></td><td>TYPE, PyTypeObject *<var>type</var>, |
| 124 | int <var>size</var>)</td></tr></table></dt> |
| 125 | <dd> |
| 126 | <div class="refcount-info"> |
| 127 | <span class="label">Return value:</span> |
| 128 | <span class="value">New reference.</span> |
| 129 | </div> |
| 130 | Allocate a new Python object using the C structure type <var>TYPE</var> |
| 131 | and the Python type object <var>type</var>. Fields not defined by the |
| 132 | Python object header are not initialized. The allocated memory |
| 133 | allows for the <var>TYPE</var> structure plus <var>size</var> fields of the |
| 134 | size given by the <tt class="member">tp_itemsize</tt> field of <var>type</var>. This is |
| 135 | useful for implementing objects like tuples, which are able to |
| 136 | determine their size at construction time. Embedding the array of |
| 137 | fields into the same allocation decreases the number of allocations, |
| 138 | improving the memory management efficiency. |
| 139 | </dd></dl> |
| 140 | |
| 141 | <P> |
| 142 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-912' xml:id='l2h-912' class="cfunction">PyObject_Del</tt></b>(</nobr></td><td>PyObject *<var>op</var>)</td></tr></table></dt> |
| 143 | <dd> |
| 144 | Releases memory allocated to an object using |
| 145 | <tt class="cfunction">PyObject_New()</tt> or <tt class="cfunction">PyObject_NewVar()</tt>. This |
| 146 | is normally called from the <tt class="member">tp_dealloc</tt> handler specified in |
| 147 | the object's type. The fields of the object should not be accessed |
| 148 | after this call as the memory is no longer a valid Python object. |
| 149 | </dd></dl> |
| 150 | |
| 151 | <P> |
| 152 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr><var>TYPE</var>* <b><tt id='l2h-913' xml:id='l2h-913' class="cfunction">PyObject_NEW</tt></b>(</nobr></td><td>TYPE, PyTypeObject *<var>type</var>)</td></tr></table></dt> |
| 153 | <dd> |
| 154 | <div class="refcount-info"> |
| 155 | <span class="label">Return value:</span> |
| 156 | <span class="value">New reference.</span> |
| 157 | </div> |
| 158 | Macro version of <tt class="cfunction">PyObject_New()</tt>, to gain performance at |
| 159 | the expense of safety. This does not check <var>type</var> for a <tt class="constant">NULL</tt> |
| 160 | value. |
| 161 | </dd></dl> |
| 162 | |
| 163 | <P> |
| 164 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr><var>TYPE</var>* <b><tt id='l2h-914' xml:id='l2h-914' class="cfunction">PyObject_NEW_VAR</tt></b>(</nobr></td><td>TYPE, PyTypeObject *<var>type</var>, |
| 165 | int <var>size</var>)</td></tr></table></dt> |
| 166 | <dd> |
| 167 | <div class="refcount-info"> |
| 168 | <span class="label">Return value:</span> |
| 169 | <span class="value">New reference.</span> |
| 170 | </div> |
| 171 | Macro version of <tt class="cfunction">PyObject_NewVar()</tt>, to gain performance |
| 172 | at the expense of safety. This does not check <var>type</var> for a |
| 173 | <tt class="constant">NULL</tt> value. |
| 174 | </dd></dl> |
| 175 | |
| 176 | <P> |
| 177 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-915' xml:id='l2h-915' class="cfunction">PyObject_DEL</tt></b>(</nobr></td><td>PyObject *<var>op</var>)</td></tr></table></dt> |
| 178 | <dd> |
| 179 | Macro version of <tt class="cfunction">PyObject_Del()</tt>. |
| 180 | </dd></dl> |
| 181 | |
| 182 | <P> |
| 183 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject* <b><tt id='l2h-916' xml:id='l2h-916' class="cfunction">Py_InitModule</tt></b>(</nobr></td><td>char *<var>name</var>, |
| 184 | PyMethodDef *<var>methods</var>)</td></tr></table></dt> |
| 185 | <dd> |
| 186 | <div class="refcount-info"> |
| 187 | <span class="label">Return value:</span> |
| 188 | <span class="value">Borrowed reference.</span> |
| 189 | </div> |
| 190 | Create a new module object based on a name and table of functions, |
| 191 | returning the new module object. |
| 192 | |
| 193 | <P> |
| 194 | |
| 195 | <span class="versionnote">Changed in version 2.3: |
| 196 | Older versions of Python did not support <tt class="constant">NULL</tt> as |
| 197 | the value for the <var>methods</var> argument.</span> |
| 198 | |
| 199 | </dd></dl> |
| 200 | |
| 201 | <P> |
| 202 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject* <b><tt id='l2h-917' xml:id='l2h-917' class="cfunction">Py_InitModule3</tt></b>(</nobr></td><td>char *<var>name</var>, |
| 203 | PyMethodDef *<var>methods</var>, |
| 204 | char *<var>doc</var>)</td></tr></table></dt> |
| 205 | <dd> |
| 206 | <div class="refcount-info"> |
| 207 | <span class="label">Return value:</span> |
| 208 | <span class="value">Borrowed reference.</span> |
| 209 | </div> |
| 210 | Create a new module object based on a name and table of functions, |
| 211 | returning the new module object. If <var>doc</var> is non-<tt class="constant">NULL</tt>, it will |
| 212 | be used to define the docstring for the module. |
| 213 | |
| 214 | <P> |
| 215 | |
| 216 | <span class="versionnote">Changed in version 2.3: |
| 217 | Older versions of Python did not support <tt class="constant">NULL</tt> as |
| 218 | the value for the <var>methods</var> argument.</span> |
| 219 | |
| 220 | </dd></dl> |
| 221 | |
| 222 | <P> |
| 223 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject* <b><tt id='l2h-918' xml:id='l2h-918' class="cfunction">Py_InitModule4</tt></b>(</nobr></td><td>char *<var>name</var>, |
| 224 | PyMethodDef *<var>methods</var>, |
| 225 | char *<var>doc</var>, PyObject *<var>self</var>, |
| 226 | int <var>apiver</var>)</td></tr></table></dt> |
| 227 | <dd> |
| 228 | <div class="refcount-info"> |
| 229 | <span class="label">Return value:</span> |
| 230 | <span class="value">Borrowed reference.</span> |
| 231 | </div> |
| 232 | Create a new module object based on a name and table of functions, |
| 233 | returning the new module object. If <var>doc</var> is non-<tt class="constant">NULL</tt>, it will |
| 234 | be used to define the docstring for the module. If <var>self</var> is |
| 235 | non-<tt class="constant">NULL</tt>, it will passed to the functions of the module as their |
| 236 | (otherwise <tt class="constant">NULL</tt>) first parameter. (This was added as an |
| 237 | experimental feature, and there are no known uses in the current |
| 238 | version of Python.) For <var>apiver</var>, the only value which should |
| 239 | be passed is defined by the constant <tt class="constant">PYTHON_API_VERSION</tt>. |
| 240 | |
| 241 | <P> |
| 242 | <span class="note"><b class="label">Note:</b> |
| 243 | Most uses of this function should probably be using |
| 244 | the <tt class="cfunction">Py_InitModule3()</tt> instead; only use this if you are |
| 245 | sure you need it.</span> |
| 246 | |
| 247 | <P> |
| 248 | |
| 249 | <span class="versionnote">Changed in version 2.3: |
| 250 | Older versions of Python did not support <tt class="constant">NULL</tt> as |
| 251 | the value for the <var>methods</var> argument.</span> |
| 252 | |
| 253 | </dd></dl> |
| 254 | |
| 255 | <P> |
| 256 | DL_IMPORT |
| 257 | |
| 258 | <P> |
| 259 | <dl><dt>PyObject <b><tt id='l2h-919' xml:id='l2h-919' class="cdata">_Py_NoneStruct</tt></b></dt> |
| 260 | <dd> |
| 261 | Object which is visible in Python as <code>None</code>. This should only |
| 262 | be accessed using the <code>Py_None</code> macro, which evaluates to a |
| 263 | pointer to this object. |
| 264 | </dd></dl> |
| 265 | |
| 266 | <P> |
| 267 | |
| 268 | <DIV CLASS="navigation"> |
| 269 | <div class='online-navigation'> |
| 270 | <p></p><hr /> |
| 271 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 272 | <tr> |
| 273 | <td class='online-navigation'><a rel="prev" title="10. Object Implementation Support" |
| 274 | href="newTypes.html"><img src='../icons/previous.png' |
| 275 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 276 | <td class='online-navigation'><a rel="parent" title="10. Object Implementation Support" |
| 277 | href="newTypes.html"><img src='../icons/up.png' |
| 278 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 279 | <td class='online-navigation'><a rel="next" title="10.2 Common Object Structures" |
| 280 | href="common-structs.html"><img src='../icons/next.png' |
| 281 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 282 | <td align="center" width="100%">Python/C API Reference Manual</td> |
| 283 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 284 | href="contents.html"><img src='../icons/contents.png' |
| 285 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 286 | <td class='online-navigation'><img src='../icons/blank.png' |
| 287 | border='0' height='32' alt='' width='32' /></td> |
| 288 | <td class='online-navigation'><a rel="index" title="Index" |
| 289 | href="genindex.html"><img src='../icons/index.png' |
| 290 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 291 | </tr></table> |
| 292 | <div class='online-navigation'> |
| 293 | <b class="navlabel">Previous:</b> |
| 294 | <a class="sectref" rel="prev" href="newTypes.html">10. Object Implementation Support</A> |
| 295 | <b class="navlabel">Up:</b> |
| 296 | <a class="sectref" rel="parent" href="newTypes.html">10. Object Implementation Support</A> |
| 297 | <b class="navlabel">Next:</b> |
| 298 | <a class="sectref" rel="next" href="common-structs.html">10.2 Common Object Structures</A> |
| 299 | </div> |
| 300 | </div> |
| 301 | <hr /> |
| 302 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 303 | </DIV> |
| 304 | <!--End of Navigation Panel--> |
| 305 | <ADDRESS> |
| 306 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 307 | </ADDRESS> |
| 308 | </BODY> |
| 309 | </HTML> |