Commit | Line | Data |
---|---|---|
86530b38 AT |
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="tupleObjects.html" /> | |
13 | <link rel="prev" href="unicodeObjects.html" /> | |
14 | <link rel="parent" href="sequenceObjects.html" /> | |
15 | <link rel="next" href="tupleObjects.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>7.3.3 Buffer Objects </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="7.3.2.2 Methods and Slot" | |
25 | href="unicodeMethodsAndSlots.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="7.3 Sequence Objects" | |
28 | href="sequenceObjects.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="7.3.4 Tuple Objects" | |
31 | href="tupleObjects.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="unicodeMethodsAndSlots.html">7.3.2.2 Methods and Slot</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="sequenceObjects.html">7.3 Sequence Objects</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="tupleObjects.html">7.3.4 Tuple Objects</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION009330000000000000000"></A><A NAME="bufferObjects"></A> | |
56 | <BR> | |
57 | 7.3.3 Buffer Objects | |
58 | </H2> | |
59 | ||
60 | <P> | |
61 | <a id='l2h-548' xml:id='l2h-548'></a>Python objects implemented in C can export a group of functions called | |
62 | the ``buffer<a id='l2h-560' xml:id='l2h-560'></a> interface.'' These functions can | |
63 | be used by an object to expose its data in a raw, byte-oriented | |
64 | format. Clients of the object can use the buffer interface to access | |
65 | the object data directly, without needing to copy it first. | |
66 | ||
67 | <P> | |
68 | Two examples of objects that support | |
69 | the buffer interface are strings and arrays. The string object exposes | |
70 | the character contents in the buffer interface's byte-oriented | |
71 | form. An array can also expose its contents, but it should be noted | |
72 | that array elements may be multi-byte values. | |
73 | ||
74 | <P> | |
75 | An example user of the buffer interface is the file object's | |
76 | <tt class="method">write()</tt> method. Any object that can export a series of bytes | |
77 | through the buffer interface can be written to a file. There are a | |
78 | number of format codes to <tt class="cfunction">PyArg_ParseTuple()</tt> that operate | |
79 | against an object's buffer interface, returning data from the target | |
80 | object. | |
81 | ||
82 | <P> | |
83 | More information on the buffer interface is provided in the section | |
84 | ``Buffer Object Structures'' (section <A href="buffer-structs.html#buffer-structs">10.7</A>), under | |
85 | the description for <tt class="ctype">PyBufferProcs</tt><a id='l2h-561' xml:id='l2h-561'></a>. | |
86 | ||
87 | <P> | |
88 | A ``buffer object'' is defined in the <span class="file">bufferobject.h</span> header | |
89 | (included by <span class="file">Python.h</span>). These objects look very similar to | |
90 | string objects at the Python programming level: they support slicing, | |
91 | indexing, concatenation, and some other standard string | |
92 | operations. However, their data can come from one of two sources: from | |
93 | a block of memory, or from another object which exports the buffer | |
94 | interface. | |
95 | ||
96 | <P> | |
97 | Buffer objects are useful as a way to expose the data from another | |
98 | object's buffer interface to the Python programmer. They can also be | |
99 | used as a zero-copy slicing mechanism. Using their ability to | |
100 | reference a block of memory, it is possible to expose any data to the | |
101 | Python programmer quite easily. The memory could be a large, constant | |
102 | array in a C extension, it could be a raw block of memory for | |
103 | manipulation before passing to an operating system library, or it | |
104 | could be used to pass around structured data in its native, in-memory | |
105 | format. | |
106 | ||
107 | <P> | |
108 | <dl><dt><b><tt class="ctype"><a id='l2h-549' xml:id='l2h-549'>PyBufferObject</a></tt></b></dt> | |
109 | <dd> | |
110 | This subtype of <tt class="ctype">PyObject</tt> represents a buffer object. | |
111 | </dl> | |
112 | ||
113 | <P> | |
114 | <dl><dt>PyTypeObject <b><tt id='l2h-550' xml:id='l2h-550' class="cdata">PyBuffer_Type</tt></b></dt> | |
115 | <dd> | |
116 | The instance of <tt class="ctype">PyTypeObject</tt> which represents the Python | |
117 | buffer type; it is the same object as <code>types.BufferType</code> in the | |
118 | Python layer.<a id='l2h-552' xml:id='l2h-552'></a>. | |
119 | </dd></dl> | |
120 | ||
121 | <P> | |
122 | <dl><dt>int <b><tt id='l2h-553' xml:id='l2h-553' class="cdata">Py_END_OF_BUFFER</tt></b></dt> | |
123 | <dd> | |
124 | This constant may be passed as the <var>size</var> parameter to | |
125 | <tt class="cfunction">PyBuffer_FromObject()</tt> or | |
126 | <tt class="cfunction">PyBuffer_FromReadWriteObject()</tt>. It indicates that the | |
127 | new <tt class="ctype">PyBufferObject</tt> should refer to <var>base</var> object from | |
128 | the specified <var>offset</var> to the end of its exported buffer. Using | |
129 | this enables the caller to avoid querying the <var>base</var> object for | |
130 | its length. | |
131 | </dd></dl> | |
132 | ||
133 | <P> | |
134 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>int <b><tt id='l2h-554' xml:id='l2h-554' class="cfunction">PyBuffer_Check</tt></b>(</nobr></td><td>PyObject *<var>p</var>)</td></tr></table></dt> | |
135 | <dd> | |
136 | Return true if the argument has type <tt class="cdata">PyBuffer_Type</tt>. | |
137 | </dd></dl> | |
138 | ||
139 | <P> | |
140 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject* <b><tt id='l2h-555' xml:id='l2h-555' class="cfunction">PyBuffer_FromObject</tt></b>(</nobr></td><td>PyObject *<var>base</var>, | |
141 | int <var>offset</var>, int <var>size</var>)</td></tr></table></dt> | |
142 | <dd> | |
143 | <div class="refcount-info"> | |
144 | <span class="label">Return value:</span> | |
145 | <span class="value">New reference.</span> | |
146 | </div> | |
147 | Return a new read-only buffer object. This raises | |
148 | <tt class="exception">TypeError</tt> if <var>base</var> doesn't support the read-only | |
149 | buffer protocol or doesn't provide exactly one buffer segment, or it | |
150 | raises <tt class="exception">ValueError</tt> if <var>offset</var> is less than zero. The | |
151 | buffer will hold a reference to the <var>base</var> object, and the | |
152 | buffer's contents will refer to the <var>base</var> object's buffer | |
153 | interface, starting as position <var>offset</var> and extending for | |
154 | <var>size</var> bytes. If <var>size</var> is <tt class="constant">Py_END_OF_BUFFER</tt>, then | |
155 | the new buffer's contents extend to the length of the <var>base</var> | |
156 | object's exported buffer data. | |
157 | </dd></dl> | |
158 | ||
159 | <P> | |
160 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject* <b><tt id='l2h-556' xml:id='l2h-556' class="cfunction">PyBuffer_FromReadWriteObject</tt></b>(</nobr></td><td>PyObject *<var>base</var>, | |
161 | int <var>offset</var>, | |
162 | int <var>size</var>)</td></tr></table></dt> | |
163 | <dd> | |
164 | <div class="refcount-info"> | |
165 | <span class="label">Return value:</span> | |
166 | <span class="value">New reference.</span> | |
167 | </div> | |
168 | Return a new writable buffer object. Parameters and exceptions are | |
169 | similar to those for <tt class="cfunction">PyBuffer_FromObject()</tt>. If the | |
170 | <var>base</var> object does not export the writeable buffer protocol, | |
171 | then <tt class="exception">TypeError</tt> is raised. | |
172 | </dd></dl> | |
173 | ||
174 | <P> | |
175 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject* <b><tt id='l2h-557' xml:id='l2h-557' class="cfunction">PyBuffer_FromMemory</tt></b>(</nobr></td><td>void *<var>ptr</var>, int <var>size</var>)</td></tr></table></dt> | |
176 | <dd> | |
177 | <div class="refcount-info"> | |
178 | <span class="label">Return value:</span> | |
179 | <span class="value">New reference.</span> | |
180 | </div> | |
181 | Return a new read-only buffer object that reads from a specified | |
182 | location in memory, with a specified size. The caller is | |
183 | responsible for ensuring that the memory buffer, passed in as | |
184 | <var>ptr</var>, is not deallocated while the returned buffer object | |
185 | exists. Raises <tt class="exception">ValueError</tt> if <var>size</var> is less than | |
186 | zero. Note that <tt class="constant">Py_END_OF_BUFFER</tt> may <em>not</em> be | |
187 | passed for the <var>size</var> parameter; <tt class="exception">ValueError</tt> will be | |
188 | raised in that case. | |
189 | </dd></dl> | |
190 | ||
191 | <P> | |
192 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject* <b><tt id='l2h-558' xml:id='l2h-558' class="cfunction">PyBuffer_FromReadWriteMemory</tt></b>(</nobr></td><td>void *<var>ptr</var>, int <var>size</var>)</td></tr></table></dt> | |
193 | <dd> | |
194 | <div class="refcount-info"> | |
195 | <span class="label">Return value:</span> | |
196 | <span class="value">New reference.</span> | |
197 | </div> | |
198 | Similar to <tt class="cfunction">PyBuffer_FromMemory()</tt>, but the returned | |
199 | buffer is writable. | |
200 | </dd></dl> | |
201 | ||
202 | <P> | |
203 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject* <b><tt id='l2h-559' xml:id='l2h-559' class="cfunction">PyBuffer_New</tt></b>(</nobr></td><td>int <var>size</var>)</td></tr></table></dt> | |
204 | <dd> | |
205 | <div class="refcount-info"> | |
206 | <span class="label">Return value:</span> | |
207 | <span class="value">New reference.</span> | |
208 | </div> | |
209 | Return a new writable buffer object that maintains its own memory | |
210 | buffer of <var>size</var> bytes. <tt class="exception">ValueError</tt> is returned if | |
211 | <var>size</var> is not zero or positive. Note that the memory buffer (as | |
212 | returned by <tt class="cfunction">PyObject_AsWriteBuffer()</tt>) is not specifically | |
213 | aligned. | |
214 | </dd></dl> | |
215 | ||
216 | <P> | |
217 | ||
218 | <DIV CLASS="navigation"> | |
219 | <div class='online-navigation'> | |
220 | <p></p><hr /> | |
221 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
222 | <tr> | |
223 | <td class='online-navigation'><a rel="prev" title="7.3.2.2 Methods and Slot" | |
224 | href="unicodeMethodsAndSlots.html"><img src='../icons/previous.png' | |
225 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
226 | <td class='online-navigation'><a rel="parent" title="7.3 Sequence Objects" | |
227 | href="sequenceObjects.html"><img src='../icons/up.png' | |
228 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
229 | <td class='online-navigation'><a rel="next" title="7.3.4 Tuple Objects" | |
230 | href="tupleObjects.html"><img src='../icons/next.png' | |
231 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
232 | <td align="center" width="100%">Python/C API Reference Manual</td> | |
233 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
234 | href="contents.html"><img src='../icons/contents.png' | |
235 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
236 | <td class='online-navigation'><img src='../icons/blank.png' | |
237 | border='0' height='32' alt='' width='32' /></td> | |
238 | <td class='online-navigation'><a rel="index" title="Index" | |
239 | href="genindex.html"><img src='../icons/index.png' | |
240 | border='0' height='32' alt='Index' width='32' /></A></td> | |
241 | </tr></table> | |
242 | <div class='online-navigation'> | |
243 | <b class="navlabel">Previous:</b> | |
244 | <a class="sectref" rel="prev" href="unicodeMethodsAndSlots.html">7.3.2.2 Methods and Slot</A> | |
245 | <b class="navlabel">Up:</b> | |
246 | <a class="sectref" rel="parent" href="sequenceObjects.html">7.3 Sequence Objects</A> | |
247 | <b class="navlabel">Next:</b> | |
248 | <a class="sectref" rel="next" href="tupleObjects.html">7.3.4 Tuple Objects</A> | |
249 | </div> | |
250 | </div> | |
251 | <hr /> | |
252 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
253 | </DIV> | |
254 | <!--End of Navigation Panel--> | |
255 | <ADDRESS> | |
256 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
257 | </ADDRESS> | |
258 | </BODY> | |
259 | </HTML> |