Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / ext / node29.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="node30.html" />
12<link rel="prev" href="node28.html" />
13<link rel="parent" href="node28.html" />
14<link rel="next" href="node30.html" />
15<meta name='aesop' content='information' />
16<title>2.2.3.1 Generic Attribute Management</title>
17</head>
18<body>
19<DIV CLASS="navigation">
20<div id='top-navigation-panel' xml:id='top-navigation-panel'>
21<table align="center" width="100%" cellpadding="0" cellspacing="2">
22<tr>
23<td class='online-navigation'><a rel="prev" title="2.2.3 Attribute Management"
24 href="node28.html"><img src='../icons/previous.png'
25 border='0' height='32' alt='Previous Page' width='32' /></A></td>
26<td class='online-navigation'><a rel="parent" title="2.2.3 Attribute Management"
27 href="node28.html"><img src='../icons/up.png'
28 border='0' height='32' alt='Up One Level' width='32' /></A></td>
29<td class='online-navigation'><a rel="next" title="2.2.3.2 Type-specific Attribute Management"
30 href="node30.html"><img src='../icons/next.png'
31 border='0' height='32' alt='Next Page' width='32' /></A></td>
32<td align="center" width="100%">Extending and Embedding the Python Interpreter</td>
33<td class='online-navigation'><a rel="contents" title="Table of Contents"
34 href="contents.html"><img src='../icons/contents.png'
35 border='0' height='32' alt='Contents' width='32' /></A></td>
36<td class='online-navigation'><img src='../icons/blank.png'
37 border='0' height='32' alt='' width='32' /></td>
38<td class='online-navigation'><img src='../icons/blank.png'
39 border='0' height='32' alt='' width='32' /></td>
40</tr></table>
41<div class='online-navigation'>
42<b class="navlabel">Previous:</b>
43<a class="sectref" rel="prev" href="node28.html">2.2.3 Attribute Management</A>
44<b class="navlabel">Up:</b>
45<a class="sectref" rel="parent" href="node28.html">2.2.3 Attribute Management</A>
46<b class="navlabel">Next:</b>
47<a class="sectref" rel="next" href="node30.html">2.2.3.2 Type-specific Attribute Management</A>
48</div>
49<hr /></div>
50</DIV>
51<!--End of Navigation Panel-->
52
53<H3><A NAME="SECTION004231000000000000000">
542.2.3.1 Generic Attribute Management</A>
55</H3>
56
57<P>
58
59<span class="versionnote">New in version 2.2.</span>
60
61<P>
62Most extension types only use <em>simple</em> attributes. So, what
63makes the attributes simple? There are only a couple of conditions
64that must be met:
65
66<P>
67
68<OL>
69<LI>The name of the attributes must be known when
70 <tt class="cfunction">PyType_Ready()</tt> is called.
71
72<P>
73</LI>
74<LI>No special processing is needed to record that an attribute
75 was looked up or set, nor do actions need to be taken based
76 on the value.
77</LI>
78</OL>
79
80<P>
81Note that this list does not place any restrictions on the values of
82the attributes, when the values are computed, or how relevant data is
83stored.
84
85<P>
86When <tt class="cfunction">PyType_Ready()</tt> is called, it uses three tables
87referenced by the type object to create <em>descriptors</em> which are
88placed in the dictionary of the type object. Each descriptor controls
89access to one attribute of the instance object. Each of the tables is
90optional; if all three are <tt class="constant">NULL</tt>, instances of the type will only have
91attributes that are inherited from their base type, and should leave
92the <tt class="member">tp_getattro</tt> and <tt class="member">tp_setattro</tt> fields <tt class="constant">NULL</tt> as
93well, allowing the base type to handle attributes.
94
95<P>
96The tables are declared as three fields of the type object:
97
98<P>
99<div class="verbatim"><pre>
100 struct PyMethodDef *tp_methods;
101 struct PyMemberDef *tp_members;
102 struct PyGetSetDef *tp_getset;
103</pre></div>
104
105<P>
106If <tt class="member">tp_methods</tt> is not <tt class="constant">NULL</tt>, it must refer to an array of
107<tt class="ctype">PyMethodDef</tt> structures. Each entry in the table is an
108instance of this structure:
109
110<P>
111<div class="verbatim"><pre>
112typedef struct PyMethodDef {
113 char *ml_name; /* method name */
114 PyCFunction ml_meth; /* implementation function */
115 int ml_flags; /* flags */
116 char *ml_doc; /* docstring */
117} PyMethodDef;
118</pre></div>
119
120<P>
121One entry should be defined for each method provided by the type; no
122entries are needed for methods inherited from a base type. One
123additional entry is needed at the end; it is a sentinel that marks the
124end of the array. The <tt class="member">ml_name</tt> field of the sentinel must be
125<tt class="constant">NULL</tt>.
126
127<P>
128XXX Need to refer to some unified discussion of the structure fields,
129shared with the next section.
130
131<P>
132The second table is used to define attributes which map directly to
133data stored in the instance. A variety of primitive C types are
134supported, and access may be read-only or read-write. The structures
135in the table are defined as:
136
137<P>
138<div class="verbatim"><pre>
139typedef struct PyMemberDef {
140 char *name;
141 int type;
142 int offset;
143 int flags;
144 char *doc;
145} PyMemberDef;
146</pre></div>
147
148<P>
149For each entry in the table, a descriptor will be constructed and
150added to the type which will be able to extract a value from the
151instance structure. The <tt class="member">type</tt> field should contain one of the
152type codes defined in the <span class="file">structmember.h</span> header; the value will
153be used to determine how to convert Python values to and from C
154values. The <tt class="member">flags</tt> field is used to store flags which control
155how the attribute can be accessed.
156
157<P>
158XXX Need to move some of this to a shared section!
159
160<P>
161The following flag constants are defined in <span class="file">structmember.h</span>;
162they may be combined using bitwise-OR.
163
164<P>
165<div class="center"><table class="realtable">
166 <thead>
167 <tr>
168 <th class="left" >Constant</th>
169 <th class="left" >Meaning</th>
170 </tr>
171 </thead>
172 <tbody>
173 <tr><td class="left" valign="baseline"><tt class="constant">READONLY <a id='l2h-10' xml:id='l2h-10'></a></tt></td>
174 <td class="left" >Never writable.</td></tr>
175 <tr><td class="left" valign="baseline"><tt class="constant">RO <a id='l2h-11' xml:id='l2h-11'></a></tt></td>
176 <td class="left" >Shorthand for <tt class="constant">READONLY</tt>.</td></tr>
177 <tr><td class="left" valign="baseline"><tt class="constant">READ_RESTRICTED <a id='l2h-12' xml:id='l2h-12'></a></tt></td>
178 <td class="left" >Not readable in restricted mode.</td></tr>
179 <tr><td class="left" valign="baseline"><tt class="constant">WRITE_RESTRICTED <a id='l2h-13' xml:id='l2h-13'></a></tt></td>
180 <td class="left" >Not writable in restricted mode.</td></tr>
181 <tr><td class="left" valign="baseline"><tt class="constant">RESTRICTED <a id='l2h-14' xml:id='l2h-14'></a></tt></td>
182 <td class="left" >Not readable or writable in restricted mode.</td></tr></tbody>
183</table></div>
184
185<P>
186An interesting advantage of using the <tt class="member">tp_members</tt> table to
187build descriptors that are used at runtime is that any attribute
188defined this way can have an associated doc string simply by providing
189the text in the table. An application can use the introspection API
190to retrieve the descriptor from the class object, and get the
191doc string using its <tt class="member">__doc__</tt> attribute.
192
193<P>
194As with the <tt class="member">tp_methods</tt> table, a sentinel entry with a
195<tt class="member">name</tt> value of <tt class="constant">NULL</tt> is required.
196
197<P>
198
199<DIV CLASS="navigation">
200<div class='online-navigation'>
201<p></p><hr />
202<table align="center" width="100%" cellpadding="0" cellspacing="2">
203<tr>
204<td class='online-navigation'><a rel="prev" title="2.2.3 Attribute Management"
205 href="node28.html"><img src='../icons/previous.png'
206 border='0' height='32' alt='Previous Page' width='32' /></A></td>
207<td class='online-navigation'><a rel="parent" title="2.2.3 Attribute Management"
208 href="node28.html"><img src='../icons/up.png'
209 border='0' height='32' alt='Up One Level' width='32' /></A></td>
210<td class='online-navigation'><a rel="next" title="2.2.3.2 Type-specific Attribute Management"
211 href="node30.html"><img src='../icons/next.png'
212 border='0' height='32' alt='Next Page' width='32' /></A></td>
213<td align="center" width="100%">Extending and Embedding the Python Interpreter</td>
214<td class='online-navigation'><a rel="contents" title="Table of Contents"
215 href="contents.html"><img src='../icons/contents.png'
216 border='0' height='32' alt='Contents' width='32' /></A></td>
217<td class='online-navigation'><img src='../icons/blank.png'
218 border='0' height='32' alt='' width='32' /></td>
219<td class='online-navigation'><img src='../icons/blank.png'
220 border='0' height='32' alt='' width='32' /></td>
221</tr></table>
222<div class='online-navigation'>
223<b class="navlabel">Previous:</b>
224<a class="sectref" rel="prev" href="node28.html">2.2.3 Attribute Management</A>
225<b class="navlabel">Up:</b>
226<a class="sectref" rel="parent" href="node28.html">2.2.3 Attribute Management</A>
227<b class="navlabel">Next:</b>
228<a class="sectref" rel="next" href="node30.html">2.2.3.2 Type-specific Attribute Management</A>
229</div>
230</div>
231<hr />
232<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
233</DIV>
234<!--End of Navigation Panel-->
235<ADDRESS>
236See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
237</ADDRESS>
238</BODY>
239</HTML>