Commit | Line | Data |
---|---|---|
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="prev" href="node29.html" /> | |
12 | <link rel="parent" href="node28.html" /> | |
13 | <link rel="next" href="node31.html" /> | |
14 | <meta name='aesop' content='information' /> | |
15 | <title>2.2.3.2 Type-specific Attribute Management</title> | |
16 | </head> | |
17 | <body> | |
18 | <DIV CLASS="navigation"> | |
19 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
20 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
21 | <tr> | |
22 | <td class='online-navigation'><a rel="prev" title="2.2.3.1 Generic Attribute Management" | |
23 | href="node29.html"><img src='../icons/previous.png' | |
24 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
25 | <td class='online-navigation'><a rel="parent" title="2.2.3 Attribute Management" | |
26 | href="node28.html"><img src='../icons/up.png' | |
27 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
28 | <td class='online-navigation'><a rel="next" title="2.2.4 Object Comparison" | |
29 | href="node31.html"><img src='../icons/next.png' | |
30 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
31 | <td align="center" width="100%">Extending and Embedding the Python Interpreter</td> | |
32 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
33 | href="contents.html"><img src='../icons/contents.png' | |
34 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
35 | <td class='online-navigation'><img src='../icons/blank.png' | |
36 | border='0' height='32' alt='' width='32' /></td> | |
37 | <td class='online-navigation'><img src='../icons/blank.png' | |
38 | border='0' height='32' alt='' width='32' /></td> | |
39 | </tr></table> | |
40 | <div class='online-navigation'> | |
41 | <b class="navlabel">Previous:</b> | |
42 | <a class="sectref" rel="prev" href="node29.html">2.2.3.1 Generic Attribute Management</A> | |
43 | <b class="navlabel">Up:</b> | |
44 | <a class="sectref" rel="parent" href="node28.html">2.2.3 Attribute Management</A> | |
45 | <b class="navlabel">Next:</b> | |
46 | <a class="sectref" rel="next" href="node31.html">2.2.4 Object Comparison</A> | |
47 | </div> | |
48 | <hr /></div> | |
49 | </DIV> | |
50 | <!--End of Navigation Panel--> | |
51 | ||
52 | <H3><A NAME="SECTION004232000000000000000"> | |
53 | 2.2.3.2 Type-specific Attribute Management</A> | |
54 | </H3> | |
55 | ||
56 | <P> | |
57 | For simplicity, only the <tt class="ctype">char*</tt> version will be demonstrated | |
58 | here; the type of the name parameter is the only difference between | |
59 | the <tt class="ctype">char*</tt> and <tt class="ctype">PyObject*</tt> flavors of the interface. | |
60 | This example effectively does the same thing as the generic example | |
61 | above, but does not use the generic support added in Python 2.2. The | |
62 | value in showing this is two-fold: it demonstrates how basic attribute | |
63 | management can be done in a way that is portable to older versions of | |
64 | Python, and explains how the handler functions are called, so that if | |
65 | you do need to extend their functionality, you'll understand what | |
66 | needs to be done. | |
67 | ||
68 | <P> | |
69 | The <tt class="member">tp_getattr</tt> handler is called when the object requires an | |
70 | attribute look-up. It is called in the same situations where the | |
71 | <tt class="method">__getattr__()</tt> method of a class would be called. | |
72 | ||
73 | <P> | |
74 | A likely way to handle this is (1) to implement a set of functions | |
75 | (such as <tt class="cfunction">newdatatype_getSize()</tt> and | |
76 | <tt class="cfunction">newdatatype_setSize()</tt> in the example below), (2) provide a | |
77 | method table listing these functions, and (3) provide a getattr | |
78 | function that returns the result of a lookup in that table. The | |
79 | method table uses the same structure as the <tt class="member">tp_methods</tt> field | |
80 | of the type object. | |
81 | ||
82 | <P> | |
83 | Here is an example: | |
84 | ||
85 | <P> | |
86 | <div class="verbatim"><pre> | |
87 | static PyMethodDef newdatatype_methods[] = { | |
88 | {"getSize", (PyCFunction)newdatatype_getSize, METH_VARARGS, | |
89 | "Return the current size."}, | |
90 | {"setSize", (PyCFunction)newdatatype_setSize, METH_VARARGS, | |
91 | "Set the size."}, | |
92 | {NULL, NULL, 0, NULL} /* sentinel */ | |
93 | }; | |
94 | ||
95 | static PyObject * | |
96 | newdatatype_getattr(newdatatypeobject *obj, char *name) | |
97 | { | |
98 | return Py_FindMethod(newdatatype_methods, (PyObject *)obj, name); | |
99 | } | |
100 | </pre></div> | |
101 | ||
102 | <P> | |
103 | The <tt class="member">tp_setattr</tt> handler is called when the | |
104 | <tt class="method">__setattr__()</tt> or <tt class="method">__delattr__()</tt> method of a class | |
105 | instance would be called. When an attribute should be deleted, the | |
106 | third parameter will be <tt class="constant">NULL</tt>. Here is an example that simply raises | |
107 | an exception; if this were really all you wanted, the | |
108 | <tt class="member">tp_setattr</tt> handler should be set to <tt class="constant">NULL</tt>. | |
109 | ||
110 | <P> | |
111 | <div class="verbatim"><pre> | |
112 | static int | |
113 | newdatatype_setattr(newdatatypeobject *obj, char *name, PyObject *v) | |
114 | { | |
115 | (void)PyErr_Format(PyExc_RuntimeError, "Read-only attribute: \%s", name); | |
116 | return -1; | |
117 | } | |
118 | </pre></div> | |
119 | ||
120 | <P> | |
121 | ||
122 | <DIV CLASS="navigation"> | |
123 | <div class='online-navigation'> | |
124 | <p></p><hr /> | |
125 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
126 | <tr> | |
127 | <td class='online-navigation'><a rel="prev" title="2.2.3.1 Generic Attribute Management" | |
128 | href="node29.html"><img src='../icons/previous.png' | |
129 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
130 | <td class='online-navigation'><a rel="parent" title="2.2.3 Attribute Management" | |
131 | href="node28.html"><img src='../icons/up.png' | |
132 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
133 | <td class='online-navigation'><a rel="next" title="2.2.4 Object Comparison" | |
134 | href="node31.html"><img src='../icons/next.png' | |
135 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
136 | <td align="center" width="100%">Extending and Embedding the Python Interpreter</td> | |
137 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
138 | href="contents.html"><img src='../icons/contents.png' | |
139 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
140 | <td class='online-navigation'><img src='../icons/blank.png' | |
141 | border='0' height='32' alt='' width='32' /></td> | |
142 | <td class='online-navigation'><img src='../icons/blank.png' | |
143 | border='0' height='32' alt='' width='32' /></td> | |
144 | </tr></table> | |
145 | <div class='online-navigation'> | |
146 | <b class="navlabel">Previous:</b> | |
147 | <a class="sectref" rel="prev" href="node29.html">2.2.3.1 Generic Attribute Management</A> | |
148 | <b class="navlabel">Up:</b> | |
149 | <a class="sectref" rel="parent" href="node28.html">2.2.3 Attribute Management</A> | |
150 | <b class="navlabel">Next:</b> | |
151 | <a class="sectref" rel="next" href="node31.html">2.2.4 Object Comparison</A> | |
152 | </div> | |
153 | </div> | |
154 | <hr /> | |
155 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
156 | </DIV> | |
157 | <!--End of Navigation Panel--> | |
158 | <ADDRESS> | |
159 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
160 | </ADDRESS> | |
161 | </BODY> | |
162 | </HTML> |