Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / ext / node30.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="prev" href="node29.html" />
<link rel="parent" href="node28.html" />
<link rel="next" href="node31.html" />
<meta name='aesop' content='information' />
<title>2.2.3.2 Type-specific Attribute Management</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.3.1 Generic Attribute Management"
href="node29.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.3 Attribute Management"
href="node28.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.4 Object Comparison"
href="node31.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="node29.html">2.2.3.1 Generic Attribute Management</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="node28.html">2.2.3 Attribute Management</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node31.html">2.2.4 Object Comparison</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H3><A NAME="SECTION004232000000000000000">
2.2.3.2 Type-specific Attribute Management</A>
</H3>
<P>
For simplicity, only the <tt class="ctype">char*</tt> version will be demonstrated
here; the type of the name parameter is the only difference between
the <tt class="ctype">char*</tt> and <tt class="ctype">PyObject*</tt> flavors of the interface.
This example effectively does the same thing as the generic example
above, but does not use the generic support added in Python 2.2. The
value in showing this is two-fold: it demonstrates how basic attribute
management can be done in a way that is portable to older versions of
Python, and explains how the handler functions are called, so that if
you do need to extend their functionality, you'll understand what
needs to be done.
<P>
The <tt class="member">tp_getattr</tt> handler is called when the object requires an
attribute look-up. It is called in the same situations where the
<tt class="method">__getattr__()</tt> method of a class would be called.
<P>
A likely way to handle this is (1) to implement a set of functions
(such as <tt class="cfunction">newdatatype_getSize()</tt> and
<tt class="cfunction">newdatatype_setSize()</tt> in the example below), (2) provide a
method table listing these functions, and (3) provide a getattr
function that returns the result of a lookup in that table. The
method table uses the same structure as the <tt class="member">tp_methods</tt> field
of the type object.
<P>
Here is an example:
<P>
<div class="verbatim"><pre>
static PyMethodDef newdatatype_methods[] = {
{"getSize", (PyCFunction)newdatatype_getSize, METH_VARARGS,
"Return the current size."},
{"setSize", (PyCFunction)newdatatype_setSize, METH_VARARGS,
"Set the size."},
{NULL, NULL, 0, NULL} /* sentinel */
};
static PyObject *
newdatatype_getattr(newdatatypeobject *obj, char *name)
{
return Py_FindMethod(newdatatype_methods, (PyObject *)obj, name);
}
</pre></div>
<P>
The <tt class="member">tp_setattr</tt> handler is called when the
<tt class="method">__setattr__()</tt> or <tt class="method">__delattr__()</tt> method of a class
instance would be called. When an attribute should be deleted, the
third parameter will be <tt class="constant">NULL</tt>. Here is an example that simply raises
an exception; if this were really all you wanted, the
<tt class="member">tp_setattr</tt> handler should be set to <tt class="constant">NULL</tt>.
<P>
<div class="verbatim"><pre>
static int
newdatatype_setattr(newdatatypeobject *obj, char *name, PyObject *v)
{
(void)PyErr_Format(PyExc_RuntimeError, "Read-only attribute: \%s", name);
return -1;
}
</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.3.1 Generic Attribute Management"
href="node29.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.3 Attribute Management"
href="node28.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.4 Object Comparison"
href="node31.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="node29.html">2.2.3.1 Generic Attribute Management</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="node28.html">2.2.3 Attribute Management</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node31.html">2.2.4 Object Comparison</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>