Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / ext / node26.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="ext.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="ext.html" title='Extending and Embedding the Python Interpreter' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="node27.html" />
<link rel="prev" href="dnt-type-methods.html" />
<link rel="parent" href="dnt-type-methods.html" />
<link rel="next" href="node27.html" />
<meta name='aesop' content='information' />
<title>2.2.1 Finalization and De-allocation</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="2.2 Type Methods"
href="dnt-type-methods.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="2.2 Type Methods"
href="dnt-type-methods.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="2.2.2 Object Presentation"
href="node27.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Extending and Embedding the Python Interpreter</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="dnt-type-methods.html">2.2 Type Methods</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="dnt-type-methods.html">2.2 Type Methods</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node27.html">2.2.2 Object Presentation</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H2><A NAME="SECTION004210000000000000000">
2.2.1 Finalization and De-allocation</A>
</H2>
<P>
<a id='l2h-5' xml:id='l2h-5'></a>
<P>
<div class="verbatim"><pre>
destructor tp_dealloc;
</pre></div>
<P>
This function is called when the reference count of the instance of
your type is reduced to zero and the Python interpreter wants to
reclaim it. If your type has memory to free or other clean-up to
perform, put it here. The object itself needs to be freed here as
well. Here is an example of this function:
<P>
<div class="verbatim"><pre>
static void
newdatatype_dealloc(newdatatypeobject * obj)
{
free(obj-&gt;obj_UnderlyingDatatypePtr);
obj-&gt;ob_type-&gt;tp_free(obj);
}
</pre></div>
<P>
One important requirement of the deallocator function is that it
leaves any pending exceptions alone. This is important since
deallocators are frequently called as the interpreter unwinds the
Python stack; when the stack is unwound due to an exception (rather
than normal returns), nothing is done to protect the deallocators from
seeing that an exception has already been set. Any actions which a
deallocator performs which may cause additional Python code to be
executed may detect that an exception has been set. This can lead to
misleading errors from the interpreter. The proper way to protect
against this is to save a pending exception before performing the
unsafe action, and restoring it when done. This can be done using the
<tt class="cfunction">PyErr_Fetch()</tt><a id='l2h-6' xml:id='l2h-6'></a> and
<tt class="cfunction">PyErr_Restore()</tt><a id='l2h-7' xml:id='l2h-7'></a> functions:
<P>
<div class="verbatim"><pre>
static void
my_dealloc(PyObject *obj)
{
MyObject *self = (MyObject *) obj;
PyObject *cbresult;
if (self-&gt;my_callback != NULL) {
PyObject *err_type, *err_value, *err_traceback;
int have_error = PyErr_Occurred() ? 1 : 0;
if (have_error)
PyErr_Fetch(&amp;err_type, &amp;err_value, &amp;err_traceback);
cbresult = PyObject_CallObject(self-&gt;my_callback, NULL);
if (cbresult == NULL)
PyErr_WriteUnraisable(self-&gt;my_callback);
else
Py_DECREF(cbresult);
if (have_error)
PyErr_Restore(err_type, err_value, err_traceback);
Py_DECREF(self-&gt;my_callback);
}
obj-&gt;ob_type-&gt;tp_free((PyObject*)self);
}
</pre></div>
<P>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="2.2 Type Methods"
href="dnt-type-methods.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="2.2 Type Methods"
href="dnt-type-methods.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="2.2.2 Object Presentation"
href="node27.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Extending and Embedding the Python Interpreter</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="dnt-type-methods.html">2.2 Type Methods</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="dnt-type-methods.html">2.2 Type Methods</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node27.html">2.2.2 Object Presentation</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>