Sophie

Sophie

distrib > Mandriva > 2010.1 > i586 > by-pkgid > 3ba3bd1608c672ba2129b098a48e9e4d > files > 459

python3-docs-3.2.2-3mdv2010.2.noarch.rpm



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Buffer Protocol &mdash; Python v3.2.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.2.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v3.2.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v3.2.2 documentation" href="../index.html" />
    <link rel="up" title="Abstract Objects Layer" href="abstract.html" />
    <link rel="next" title="Old Buffer Protocol" href="objbuffer.html" />
    <link rel="prev" title="Iterator Protocol" href="iter.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="objbuffer.html" title="Old Buffer Protocol"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="iter.html" title="Iterator Protocol"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
          <li><a href="abstract.html" accesskey="U">Abstract Objects Layer</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="buffer-protocol">
<span id="bufferobjects"></span><h1>Buffer Protocol<a class="headerlink" href="#buffer-protocol" title="Permalink to this headline">¶</a></h1>
<p id="index-0">Certain objects available in Python wrap access to an underlying memory
array or <em>buffer</em>.  Such objects include the built-in <a class="reference internal" href="../library/functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> and
<a class="reference internal" href="../library/functions.html#bytearray" title="bytearray"><tt class="xref py py-class docutils literal"><span class="pre">bytearray</span></tt></a>, and some extension types like <a class="reference internal" href="../library/array.html#array.array" title="array.array"><tt class="xref py py-class docutils literal"><span class="pre">array.array</span></tt></a>.
Third-party libraries may define their own types for special purposes, such
as image processing or numeric analysis.</p>
<p>While each of these types have their own semantics, they share the common
characteristic of being backed by a possibly large memory buffer.  It is
then desireable, in some situations, to access that buffer directly and
without intermediate copying.</p>
<p>Python provides such a facility at the C level in the form of the <em>buffer
protocol</em>.  This protocol has two sides:</p>
<ul class="simple" id="index-1">
<li>on the producer side, a type can export a &#8220;buffer interface&#8221; which allows
objects of that type to expose information about their underlying buffer.
This interface is described in the section <a class="reference internal" href="typeobj.html#buffer-structs"><em>Buffer Object Structures</em></a>;</li>
<li>on the consumer side, several means are available to obtain a pointer to
the raw underlying data of an object (for example a method parameter).</li>
</ul>
<p>Simple objects such as <a class="reference internal" href="../library/functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> and <a class="reference internal" href="../library/functions.html#bytearray" title="bytearray"><tt class="xref py py-class docutils literal"><span class="pre">bytearray</span></tt></a> expose their
underlying buffer in byte-oriented form.  Other forms are possible; for example,
the elements exposed by a <a class="reference internal" href="../library/array.html#array.array" title="array.array"><tt class="xref py py-class docutils literal"><span class="pre">array.array</span></tt></a> can be multi-byte values.</p>
<p>An example consumer of the buffer interface is the <a class="reference internal" href="../library/io.html#io.BufferedIOBase.write" title="io.BufferedIOBase.write"><tt class="xref py py-meth docutils literal"><span class="pre">write()</span></tt></a>
method of file objects: any object that can export a series of bytes through
the buffer interface can be written to a file.  While <tt class="xref py py-meth docutils literal"><span class="pre">write()</span></tt> only
needs read-only access to the internal contents of the object passed to it,
other methods such as <a class="reference internal" href="../library/io.html#io.BufferedIOBase.readinto" title="io.BufferedIOBase.readinto"><tt class="xref py py-meth docutils literal"><span class="pre">readinto()</span></tt></a> need write access
to the contents of their argument.  The buffer interface allows objects to
selectively allow or reject exporting of read-write and read-only buffers.</p>
<p>There are two ways for a consumer of the buffer interface to acquire a buffer
over a target object:</p>
<ul class="simple">
<li>call <a class="reference internal" href="#PyObject_GetBuffer" title="PyObject_GetBuffer"><tt class="xref c c-func docutils literal"><span class="pre">PyObject_GetBuffer()</span></tt></a> with the right parameters;</li>
<li>call <a class="reference internal" href="arg.html#PyArg_ParseTuple" title="PyArg_ParseTuple"><tt class="xref c c-func docutils literal"><span class="pre">PyArg_ParseTuple()</span></tt></a> (or one of its siblings) with one of the
<tt class="docutils literal"><span class="pre">y*</span></tt>, <tt class="docutils literal"><span class="pre">w*</span></tt> or <tt class="docutils literal"><span class="pre">s*</span></tt> <a class="reference internal" href="arg.html#arg-parsing"><em>format codes</em></a>.</li>
</ul>
<p>In both cases, <a class="reference internal" href="#PyBuffer_Release" title="PyBuffer_Release"><tt class="xref c c-func docutils literal"><span class="pre">PyBuffer_Release()</span></tt></a> must be called when the buffer
isn&#8217;t needed anymore.  Failure to do so could lead to various issues such as
resource leaks.</p>
<div class="section" id="the-buffer-structure">
<h2>The buffer structure<a class="headerlink" href="#the-buffer-structure" title="Permalink to this headline">¶</a></h2>
<p>Buffer structures (or simply &#8220;buffers&#8221;) are useful as a way to expose the
binary data from another object to the Python programmer.  They can also be
used as a zero-copy slicing mechanism.  Using their ability to reference a
block of memory, it is possible to expose any data to the Python programmer
quite easily.  The memory could be a large, constant array in a C extension,
it could be a raw block of memory for manipulation before passing to an
operating system library, or it could be used to pass around structured data
in its native, in-memory format.</p>
<p>Contrary to most data types exposed by the Python interpreter, buffers
are not <a class="reference internal" href="structures.html#PyObject" title="PyObject"><tt class="xref c c-type docutils literal"><span class="pre">PyObject</span></tt></a> pointers but rather simple C structures.  This
allows them to be created and copied very simply.  When a generic wrapper
around a buffer is needed, a <a class="reference internal" href="memoryview.html#memoryview-objects"><em>memoryview</em></a> object
can be created.</p>
<dl class="type">
<dt id="Py_buffer">
<tt class="descname">Py_buffer</tt><a class="headerlink" href="#Py_buffer" title="Permalink to this definition">¶</a></dt>
<dd><dl class="member">
<dt id="Py_buffer.buf">
void *<tt class="descname">buf</tt><a class="headerlink" href="#Py_buffer.buf" title="Permalink to this definition">¶</a></dt>
<dd><p>A pointer to the start of the memory for the object.</p>
</dd></dl>

<dl class="member">
<dt>
Py_ssize_t <tt class="descname">len</tt></dt>
<dd><p>The total length of the memory in bytes.</p>
</dd></dl>

<dl class="member">
<dt id="Py_buffer.readonly">
int <tt class="descname">readonly</tt><a class="headerlink" href="#Py_buffer.readonly" title="Permalink to this definition">¶</a></dt>
<dd><p>An indicator of whether the buffer is read only.</p>
</dd></dl>

<dl class="member">
<dt>
const char *<tt class="descname">format</tt></dt>
<dd><p>A <em>NULL</em> terminated string in <a class="reference internal" href="../library/struct.html#module-struct" title="struct: Interpret bytes as packed binary data."><tt class="xref py py-mod docutils literal"><span class="pre">struct</span></tt></a> module style syntax giving
the contents of the elements available through the buffer.  If this is
<em>NULL</em>, <tt class="docutils literal"><span class="pre">&quot;B&quot;</span></tt> (unsigned bytes) is assumed.</p>
</dd></dl>

<dl class="member">
<dt id="Py_buffer.ndim">
int <tt class="descname">ndim</tt><a class="headerlink" href="#Py_buffer.ndim" title="Permalink to this definition">¶</a></dt>
<dd><p>The number of dimensions the memory represents as a multi-dimensional
array.  If it is 0, <tt class="xref c c-data docutils literal"><span class="pre">strides</span></tt> and <tt class="xref c c-data docutils literal"><span class="pre">suboffsets</span></tt> must be
<em>NULL</em>.</p>
</dd></dl>

<dl class="member">
<dt id="Py_buffer.shape">
Py_ssize_t *<tt class="descname">shape</tt><a class="headerlink" href="#Py_buffer.shape" title="Permalink to this definition">¶</a></dt>
<dd><p>An array of <tt class="xref c c-type docutils literal"><span class="pre">Py_ssize_t</span></tt>s the length of <tt class="xref c c-data docutils literal"><span class="pre">ndim</span></tt> giving the
shape of the memory as a multi-dimensional array.  Note that
<tt class="docutils literal"><span class="pre">((*shape)[0]</span> <span class="pre">*</span> <span class="pre">...</span> <span class="pre">*</span> <span class="pre">(*shape)[ndims-1])*itemsize</span></tt> should be equal to
<tt class="xref c c-data docutils literal"><span class="pre">len</span></tt>.</p>
</dd></dl>

<dl class="member">
<dt id="Py_buffer.strides">
Py_ssize_t *<tt class="descname">strides</tt><a class="headerlink" href="#Py_buffer.strides" title="Permalink to this definition">¶</a></dt>
<dd><p>An array of <tt class="xref c c-type docutils literal"><span class="pre">Py_ssize_t</span></tt>s the length of <tt class="xref c c-data docutils literal"><span class="pre">ndim</span></tt> giving the
number of bytes to skip to get to a new element in each dimension.</p>
</dd></dl>

<dl class="member">
<dt id="Py_buffer.suboffsets">
Py_ssize_t *<tt class="descname">suboffsets</tt><a class="headerlink" href="#Py_buffer.suboffsets" title="Permalink to this definition">¶</a></dt>
<dd><p>An array of <tt class="xref c c-type docutils literal"><span class="pre">Py_ssize_t</span></tt>s the length of <tt class="xref c c-data docutils literal"><span class="pre">ndim</span></tt>.  If these
suboffset numbers are greater than or equal to 0, then the value stored
along the indicated dimension is a pointer and the suboffset value
dictates how many bytes to add to the pointer after de-referencing. A
suboffset value that it negative indicates that no de-referencing should
occur (striding in a contiguous memory block).</p>
<p>Here is a function that returns a pointer to the element in an N-D array
pointed to by an N-dimensional index when there are both non-NULL strides
and suboffsets:</p>
<div class="highlight-c"><div class="highlight"><pre><span class="kt">void</span> <span class="o">*</span><span class="nf">get_item_pointer</span><span class="p">(</span><span class="kt">int</span> <span class="n">ndim</span><span class="p">,</span> <span class="kt">void</span> <span class="o">*</span><span class="n">buf</span><span class="p">,</span> <span class="n">Py_ssize_t</span> <span class="o">*</span><span class="n">strides</span><span class="p">,</span>
    <span class="n">Py_ssize_t</span> <span class="o">*</span><span class="n">suboffsets</span><span class="p">,</span> <span class="n">Py_ssize_t</span> <span class="o">*</span><span class="n">indices</span><span class="p">)</span> <span class="p">{</span>
    <span class="kt">char</span> <span class="o">*</span><span class="n">pointer</span> <span class="o">=</span> <span class="p">(</span><span class="kt">char</span><span class="o">*</span><span class="p">)</span><span class="n">buf</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">i</span><span class="p">;</span>
    <span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">ndim</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">pointer</span> <span class="o">+=</span> <span class="n">strides</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="n">indices</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">suboffsets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&gt;=</span><span class="mi">0</span> <span class="p">)</span> <span class="p">{</span>
            <span class="n">pointer</span> <span class="o">=</span> <span class="o">*</span><span class="p">((</span><span class="kt">char</span><span class="o">**</span><span class="p">)</span><span class="n">pointer</span><span class="p">)</span> <span class="o">+</span> <span class="n">suboffsets</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="p">(</span><span class="kt">void</span><span class="o">*</span><span class="p">)</span><span class="n">pointer</span><span class="p">;</span>
 <span class="p">}</span>
</pre></div>
</div>
</dd></dl>

<dl class="member">
<dt id="Py_buffer.itemsize">
Py_ssize_t <tt class="descname">itemsize</tt><a class="headerlink" href="#Py_buffer.itemsize" title="Permalink to this definition">¶</a></dt>
<dd><p>This is a storage for the itemsize (in bytes) of each element of the
shared memory. It is technically un-necessary as it can be obtained
using <a class="reference internal" href="#PyBuffer_SizeFromFormat" title="PyBuffer_SizeFromFormat"><tt class="xref c c-func docutils literal"><span class="pre">PyBuffer_SizeFromFormat()</span></tt></a>, however an exporter may know
this information without parsing the format string and it is necessary
to know the itemsize for proper interpretation of striding. Therefore,
storing it is more convenient and faster.</p>
</dd></dl>

<dl class="member">
<dt id="Py_buffer.internal">
void *<tt class="descname">internal</tt><a class="headerlink" href="#Py_buffer.internal" title="Permalink to this definition">¶</a></dt>
<dd><p>This is for use internally by the exporting object. For example, this
might be re-cast as an integer by the exporter and used to store flags
about whether or not the shape, strides, and suboffsets arrays must be
freed when the buffer is released. The consumer should never alter this
value.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="buffer-related-functions">
<h2>Buffer-related functions<a class="headerlink" href="#buffer-related-functions" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="PyObject_CheckBuffer">
int <tt class="descname">PyObject_CheckBuffer</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*obj</em><big>)</big><a class="headerlink" href="#PyObject_CheckBuffer" title="Permalink to this definition">¶</a></dt>
<dd><p>Return 1 if <em>obj</em> supports the buffer interface otherwise 0.  When 1 is
returned, it doesn&#8217;t guarantee that <a class="reference internal" href="#PyObject_GetBuffer" title="PyObject_GetBuffer"><tt class="xref c c-func docutils literal"><span class="pre">PyObject_GetBuffer()</span></tt></a> will
succeed.</p>
</dd></dl>

<dl class="function">
<dt id="PyObject_GetBuffer">
int <tt class="descname">PyObject_GetBuffer</tt><big>(</big><a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*obj</em>, <a class="reference internal" href="#Py_buffer" title="Py_buffer">Py_buffer</a><em>&nbsp;*view</em>, int<em>&nbsp;flags</em><big>)</big><a class="headerlink" href="#PyObject_GetBuffer" title="Permalink to this definition">¶</a></dt>
<dd><p>Export a view over some internal data from the target object <em>obj</em>.
<em>obj</em> must not be NULL, and <em>view</em> must point to an existing
<a class="reference internal" href="#Py_buffer" title="Py_buffer"><tt class="xref c c-type docutils literal"><span class="pre">Py_buffer</span></tt></a> structure allocated by the caller (most uses of
this function will simply declare a local variable of type
<a class="reference internal" href="#Py_buffer" title="Py_buffer"><tt class="xref c c-type docutils literal"><span class="pre">Py_buffer</span></tt></a>).  The <em>flags</em> argument is a bit field indicating
what kind of buffer is requested.  The buffer interface allows
for complicated memory layout possibilities; however, some callers
won&#8217;t want to handle all the complexity and instead request a simple
view of the target object (using <a class="reference internal" href="#PyBUF_SIMPLE" title="PyBUF_SIMPLE"><tt class="xref c c-macro docutils literal"><span class="pre">PyBUF_SIMPLE</span></tt></a> for a read-only
view and <a class="reference internal" href="#PyBUF_WRITABLE" title="PyBUF_WRITABLE"><tt class="xref c c-macro docutils literal"><span class="pre">PyBUF_WRITABLE</span></tt></a> for a read-write view).</p>
<p>Some exporters may not be able to share memory in every possible way and
may need to raise errors to signal to some consumers that something is
just not possible. These errors should be a <a class="reference internal" href="../library/exceptions.html#BufferError" title="BufferError"><tt class="xref py py-exc docutils literal"><span class="pre">BufferError</span></tt></a> unless
there is another error that is actually causing the problem. The
exporter can use flags information to simplify how much of the
<a class="reference internal" href="#Py_buffer" title="Py_buffer"><tt class="xref c c-data docutils literal"><span class="pre">Py_buffer</span></tt></a> structure is filled in with non-default values and/or
raise an error if the object can&#8217;t support a simpler view of its memory.</p>
<p>On success, 0 is returned and the <em>view</em> structure is filled with useful
values.  On error, -1 is returned and an exception is raised; the <em>view</em>
is left in an undefined state.</p>
<p>The following are the possible values to the <em>flags</em> arguments.</p>
<dl class="macro">
<dt id="PyBUF_SIMPLE">
<tt class="descname">PyBUF_SIMPLE</tt><a class="headerlink" href="#PyBUF_SIMPLE" title="Permalink to this definition">¶</a></dt>
<dd><p>This is the default flag.  The returned buffer exposes a read-only
memory area.  The format of data is assumed to be raw unsigned bytes,
without any particular structure.  This is a &#8220;stand-alone&#8221; flag
constant.  It never needs to be &#8216;|&#8217;d to the others.  The exporter will
raise an error if it cannot provide such a contiguous buffer of bytes.</p>
</dd></dl>

<dl class="macro">
<dt id="PyBUF_WRITABLE">
<tt class="descname">PyBUF_WRITABLE</tt><a class="headerlink" href="#PyBUF_WRITABLE" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#PyBUF_SIMPLE" title="PyBUF_SIMPLE"><tt class="xref c c-macro docutils literal"><span class="pre">PyBUF_SIMPLE</span></tt></a>, but the returned buffer is writable.  If
the exporter doesn&#8217;t support writable buffers, an error is raised.</p>
</dd></dl>

<dl class="macro">
<dt id="PyBUF_STRIDES">
<tt class="descname">PyBUF_STRIDES</tt><a class="headerlink" href="#PyBUF_STRIDES" title="Permalink to this definition">¶</a></dt>
<dd><p>This implies <a class="reference internal" href="#PyBUF_ND" title="PyBUF_ND"><tt class="xref c c-macro docutils literal"><span class="pre">PyBUF_ND</span></tt></a>.  The returned buffer must provide
strides information (i.e. the strides cannot be NULL).  This would be
used when the consumer can handle strided, discontiguous arrays.
Handling strides automatically assumes you can handle shape.  The
exporter can raise an error if a strided representation of the data is
not possible (i.e. without the suboffsets).</p>
</dd></dl>

<dl class="macro">
<dt id="PyBUF_ND">
<tt class="descname">PyBUF_ND</tt><a class="headerlink" href="#PyBUF_ND" title="Permalink to this definition">¶</a></dt>
<dd><p>The returned buffer must provide shape information.  The memory will be
assumed C-style contiguous (last dimension varies the fastest).  The
exporter may raise an error if it cannot provide this kind of
contiguous buffer.  If this is not given then shape will be <em>NULL</em>.</p>
</dd></dl>

<dl class="macro">
<dt id="PyBUF_C_CONTIGUOUS">
<tt class="descname">PyBUF_C_CONTIGUOUS</tt><a class="headerlink" href="#PyBUF_C_CONTIGUOUS" title="Permalink to this definition">¶</a></dt>
<dt id="PyBUF_F_CONTIGUOUS">
<tt class="descname">PyBUF_F_CONTIGUOUS</tt><a class="headerlink" href="#PyBUF_F_CONTIGUOUS" title="Permalink to this definition">¶</a></dt>
<dt id="PyBUF_ANY_CONTIGUOUS">
<tt class="descname">PyBUF_ANY_CONTIGUOUS</tt><a class="headerlink" href="#PyBUF_ANY_CONTIGUOUS" title="Permalink to this definition">¶</a></dt>
<dd><p>These flags indicate that the contiguity returned buffer must be
respectively, C-contiguous (last dimension varies the fastest), Fortran
contiguous (first dimension varies the fastest) or either one.  All of
these flags imply <a class="reference internal" href="#PyBUF_STRIDES" title="PyBUF_STRIDES"><tt class="xref c c-macro docutils literal"><span class="pre">PyBUF_STRIDES</span></tt></a> and guarantee that the
strides buffer info structure will be filled in correctly.</p>
</dd></dl>

<dl class="macro">
<dt id="PyBUF_INDIRECT">
<tt class="descname">PyBUF_INDIRECT</tt><a class="headerlink" href="#PyBUF_INDIRECT" title="Permalink to this definition">¶</a></dt>
<dd><p>This flag indicates the returned buffer must have suboffsets
information (which can be NULL if no suboffsets are needed).  This can
be used when the consumer can handle indirect array referencing implied
by these suboffsets. This implies <a class="reference internal" href="#PyBUF_STRIDES" title="PyBUF_STRIDES"><tt class="xref c c-macro docutils literal"><span class="pre">PyBUF_STRIDES</span></tt></a>.</p>
</dd></dl>

<dl class="macro">
<dt id="PyBUF_FORMAT">
<tt class="descname">PyBUF_FORMAT</tt><a class="headerlink" href="#PyBUF_FORMAT" title="Permalink to this definition">¶</a></dt>
<dd><p>The returned buffer must have true format information if this flag is
provided.  This would be used when the consumer is going to be checking
for what &#8216;kind&#8217; of data is actually stored.  An exporter should always
be able to provide this information if requested.  If format is not
explicitly requested then the format must be returned as <em>NULL</em> (which
means <tt class="docutils literal"><span class="pre">'B'</span></tt>, or unsigned bytes).</p>
</dd></dl>

<dl class="macro">
<dt id="PyBUF_STRIDED">
<tt class="descname">PyBUF_STRIDED</tt><a class="headerlink" href="#PyBUF_STRIDED" title="Permalink to this definition">¶</a></dt>
<dd><p>This is equivalent to <tt class="docutils literal"><span class="pre">(PyBUF_STRIDES</span> <span class="pre">|</span> <span class="pre">PyBUF_WRITABLE)</span></tt>.</p>
</dd></dl>

<dl class="macro">
<dt id="PyBUF_STRIDED_RO">
<tt class="descname">PyBUF_STRIDED_RO</tt><a class="headerlink" href="#PyBUF_STRIDED_RO" title="Permalink to this definition">¶</a></dt>
<dd><p>This is equivalent to <tt class="docutils literal"><span class="pre">(PyBUF_STRIDES)</span></tt>.</p>
</dd></dl>

<dl class="macro">
<dt id="PyBUF_RECORDS">
<tt class="descname">PyBUF_RECORDS</tt><a class="headerlink" href="#PyBUF_RECORDS" title="Permalink to this definition">¶</a></dt>
<dd><p>This is equivalent to <tt class="docutils literal"><span class="pre">(PyBUF_STRIDES</span> <span class="pre">|</span> <span class="pre">PyBUF_FORMAT</span> <span class="pre">|</span>
<span class="pre">PyBUF_WRITABLE)</span></tt>.</p>
</dd></dl>

<dl class="macro">
<dt id="PyBUF_RECORDS_RO">
<tt class="descname">PyBUF_RECORDS_RO</tt><a class="headerlink" href="#PyBUF_RECORDS_RO" title="Permalink to this definition">¶</a></dt>
<dd><p>This is equivalent to <tt class="docutils literal"><span class="pre">(PyBUF_STRIDES</span> <span class="pre">|</span> <span class="pre">PyBUF_FORMAT)</span></tt>.</p>
</dd></dl>

<dl class="macro">
<dt id="PyBUF_FULL">
<tt class="descname">PyBUF_FULL</tt><a class="headerlink" href="#PyBUF_FULL" title="Permalink to this definition">¶</a></dt>
<dd><p>This is equivalent to <tt class="docutils literal"><span class="pre">(PyBUF_INDIRECT</span> <span class="pre">|</span> <span class="pre">PyBUF_FORMAT</span> <span class="pre">|</span>
<span class="pre">PyBUF_WRITABLE)</span></tt>.</p>
</dd></dl>

<dl class="macro">
<dt id="PyBUF_FULL_RO">
<tt class="descname">PyBUF_FULL_RO</tt><a class="headerlink" href="#PyBUF_FULL_RO" title="Permalink to this definition">¶</a></dt>
<dd><p>This is equivalent to <tt class="docutils literal"><span class="pre">(PyBUF_INDIRECT</span> <span class="pre">|</span> <span class="pre">PyBUF_FORMAT)</span></tt>.</p>
</dd></dl>

<dl class="macro">
<dt id="PyBUF_CONTIG">
<tt class="descname">PyBUF_CONTIG</tt><a class="headerlink" href="#PyBUF_CONTIG" title="Permalink to this definition">¶</a></dt>
<dd><p>This is equivalent to <tt class="docutils literal"><span class="pre">(PyBUF_ND</span> <span class="pre">|</span> <span class="pre">PyBUF_WRITABLE)</span></tt>.</p>
</dd></dl>

<dl class="macro">
<dt id="PyBUF_CONTIG_RO">
<tt class="descname">PyBUF_CONTIG_RO</tt><a class="headerlink" href="#PyBUF_CONTIG_RO" title="Permalink to this definition">¶</a></dt>
<dd><p>This is equivalent to <tt class="docutils literal"><span class="pre">(PyBUF_ND)</span></tt>.</p>
</dd></dl>

</dd></dl>

<dl class="function">
<dt id="PyBuffer_Release">
void <tt class="descname">PyBuffer_Release</tt><big>(</big><a class="reference internal" href="#Py_buffer" title="Py_buffer">Py_buffer</a><em>&nbsp;*view</em><big>)</big><a class="headerlink" href="#PyBuffer_Release" title="Permalink to this definition">¶</a></dt>
<dd><p>Release the buffer <em>view</em>.  This should be called when the buffer is no
longer being used as it may free memory from it.</p>
</dd></dl>

<dl class="function">
<dt id="PyBuffer_SizeFromFormat">
Py_ssize_t <tt class="descname">PyBuffer_SizeFromFormat</tt><big>(</big>const char<em>&nbsp;*</em><big>)</big><a class="headerlink" href="#PyBuffer_SizeFromFormat" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the implied <a class="reference internal" href="#Py_buffer.itemsize" title="Py_buffer.itemsize"><tt class="xref c c-data docutils literal"><span class="pre">itemsize</span></tt></a> from the struct-stype
<tt class="xref c c-data docutils literal"><span class="pre">format</span></tt>.</p>
</dd></dl>

<dl class="function">
<dt id="PyBuffer_IsContiguous">
int <tt class="descname">PyBuffer_IsContiguous</tt><big>(</big><a class="reference internal" href="#Py_buffer" title="Py_buffer">Py_buffer</a><em>&nbsp;*view</em>, char<em>&nbsp;fortran</em><big>)</big><a class="headerlink" href="#PyBuffer_IsContiguous" title="Permalink to this definition">¶</a></dt>
<dd><p>Return 1 if the memory defined by the <em>view</em> is C-style (<em>fortran</em> is
<tt class="docutils literal"><span class="pre">'C'</span></tt>) or Fortran-style (<em>fortran</em> is <tt class="docutils literal"><span class="pre">'F'</span></tt>) contiguous or either one
(<em>fortran</em> is <tt class="docutils literal"><span class="pre">'A'</span></tt>).  Return 0 otherwise.</p>
</dd></dl>

<dl class="function">
<dt id="PyBuffer_FillContiguousStrides">
void <tt class="descname">PyBuffer_FillContiguousStrides</tt><big>(</big>int<em>&nbsp;ndim</em>, Py_ssize_t<em>&nbsp;*shape</em>, Py_ssize_t<em>&nbsp;*strides</em>, Py_ssize_t<em>&nbsp;itemsize</em>, char<em>&nbsp;fortran</em><big>)</big><a class="headerlink" href="#PyBuffer_FillContiguousStrides" title="Permalink to this definition">¶</a></dt>
<dd><p>Fill the <em>strides</em> array with byte-strides of a contiguous (C-style if
<em>fortran</em> is <tt class="docutils literal"><span class="pre">'C'</span></tt> or Fortran-style if <em>fortran</em> is <tt class="docutils literal"><span class="pre">'F'</span></tt>) array of the
given shape with the given number of bytes per element.</p>
</dd></dl>

<dl class="function">
<dt id="PyBuffer_FillInfo">
int <tt class="descname">PyBuffer_FillInfo</tt><big>(</big><a class="reference internal" href="#Py_buffer" title="Py_buffer">Py_buffer</a><em>&nbsp;*view</em>, <a class="reference internal" href="structures.html#PyObject" title="PyObject">PyObject</a><em>&nbsp;*obj</em>, void<em>&nbsp;*buf</em>, Py_ssize_t<em>&nbsp;len</em>, int<em>&nbsp;readonly</em>, int<em>&nbsp;infoflags</em><big>)</big><a class="headerlink" href="#PyBuffer_FillInfo" title="Permalink to this definition">¶</a></dt>
<dd><p>Fill in a buffer-info structure, <em>view</em>, correctly for an exporter that can
only share a contiguous chunk of memory of &#8220;unsigned bytes&#8221; of the given
length.  Return 0 on success and -1 (with raising an error) on error.</p>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Buffer Protocol</a><ul>
<li><a class="reference internal" href="#the-buffer-structure">The buffer structure</a></li>
<li><a class="reference internal" href="#buffer-related-functions">Buffer-related functions</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="iter.html"
                        title="previous chapter">Iterator Protocol</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="objbuffer.html"
                        title="next chapter">Old Buffer Protocol</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/c-api/buffer.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="objbuffer.html" title="Old Buffer Protocol"
             >next</a> |</li>
        <li class="right" >
          <a href="iter.html" title="Iterator Protocol"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
          <li><a href="abstract.html" >Abstract Objects Layer</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2011, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Sep 04, 2011.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>