gooderp18绿色标准版
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

1033 lignes
89KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="Buffer Protocol" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/c-api/buffer.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Certain objects available in Python wrap access to an underlying memory array or buffer. Such objects include the built-in bytes and bytearray, and some extension types like array.array. Third-part..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Certain objects available in Python wrap access to an underlying memory array or buffer. Such objects include the built-in bytes and bytearray, and some extension types like array.array. Third-part..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>Buffer Protocol &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="Old Buffer Protocol" href="objbuffer.html" />
  33. <link rel="prev" title="Iterator Protocol" href="iter.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/c-api/buffer.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">Buffer Protocol</a><ul>
  86. <li><a class="reference internal" href="#buffer-structure">Buffer structure</a></li>
  87. <li><a class="reference internal" href="#buffer-request-types">Buffer request types</a><ul>
  88. <li><a class="reference internal" href="#request-independent-fields">request-independent fields</a></li>
  89. <li><a class="reference internal" href="#readonly-format">readonly, format</a></li>
  90. <li><a class="reference internal" href="#shape-strides-suboffsets">shape, strides, suboffsets</a></li>
  91. <li><a class="reference internal" href="#contiguity-requests">contiguity requests</a></li>
  92. <li><a class="reference internal" href="#compound-requests">compound requests</a></li>
  93. </ul>
  94. </li>
  95. <li><a class="reference internal" href="#complex-arrays">Complex arrays</a><ul>
  96. <li><a class="reference internal" href="#numpy-style-shape-and-strides">NumPy-style: shape and strides</a></li>
  97. <li><a class="reference internal" href="#pil-style-shape-strides-and-suboffsets">PIL-style: shape, strides and suboffsets</a></li>
  98. </ul>
  99. </li>
  100. <li><a class="reference internal" href="#buffer-related-functions">Buffer-related functions</a></li>
  101. </ul>
  102. </li>
  103. </ul>
  104. </div>
  105. <div>
  106. <h4>Previous topic</h4>
  107. <p class="topless"><a href="iter.html"
  108. title="previous chapter">Iterator Protocol</a></p>
  109. </div>
  110. <div>
  111. <h4>Next topic</h4>
  112. <p class="topless"><a href="objbuffer.html"
  113. title="next chapter">Old Buffer Protocol</a></p>
  114. </div>
  115. <div role="note" aria-label="source link">
  116. <h3>This Page</h3>
  117. <ul class="this-page-menu">
  118. <li><a href="../bugs.html">Report a Bug</a></li>
  119. <li>
  120. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/buffer.rst"
  121. rel="nofollow">Show Source
  122. </a>
  123. </li>
  124. </ul>
  125. </div>
  126. </nav>
  127. </div>
  128. </div>
  129. <div class="related" role="navigation" aria-label="related navigation">
  130. <h3>Navigation</h3>
  131. <ul>
  132. <li class="right" style="margin-right: 10px">
  133. <a href="../genindex.html" title="General Index"
  134. accesskey="I">index</a></li>
  135. <li class="right" >
  136. <a href="../py-modindex.html" title="Python Module Index"
  137. >modules</a> |</li>
  138. <li class="right" >
  139. <a href="objbuffer.html" title="Old Buffer Protocol"
  140. accesskey="N">next</a> |</li>
  141. <li class="right" >
  142. <a href="iter.html" title="Iterator Protocol"
  143. accesskey="P">previous</a> |</li>
  144. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  145. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  146. <li class="switchers">
  147. <div class="language_switcher_placeholder"></div>
  148. <div class="version_switcher_placeholder"></div>
  149. </li>
  150. <li>
  151. </li>
  152. <li id="cpython-language-and-version">
  153. <a href="../index.html">3.12.3 Documentation</a> &#187;
  154. </li>
  155. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  156. <li class="nav-item nav-item-2"><a href="abstract.html" accesskey="U">Abstract Objects Layer</a> &#187;</li>
  157. <li class="nav-item nav-item-this"><a href="">Buffer Protocol</a></li>
  158. <li class="right">
  159. <div class="inline-search" role="search">
  160. <form class="inline-search" action="../search.html" method="get">
  161. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  162. <input type="submit" value="Go" />
  163. </form>
  164. </div>
  165. |
  166. </li>
  167. <li class="right">
  168. <label class="theme-selector-label">
  169. Theme
  170. <select class="theme-selector" oninput="activateTheme(this.value)">
  171. <option value="auto" selected>Auto</option>
  172. <option value="light">Light</option>
  173. <option value="dark">Dark</option>
  174. </select>
  175. </label> |</li>
  176. </ul>
  177. </div>
  178. <div class="document">
  179. <div class="documentwrapper">
  180. <div class="bodywrapper">
  181. <div class="body" role="main">
  182. <section id="buffer-protocol">
  183. <span id="bufferobjects"></span><span id="index-0"></span><h1>Buffer Protocol<a class="headerlink" href="#buffer-protocol" title="Link to this heading">¶</a></h1>
  184. <p>Certain objects available in Python wrap access to an underlying memory
  185. array or <em>buffer</em>. Such objects include the built-in <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> and
  186. <a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a>, and some extension types like <a class="reference internal" href="../library/array.html#array.array" title="array.array"><code class="xref py py-class docutils literal notranslate"><span class="pre">array.array</span></code></a>.
  187. Third-party libraries may define their own types for special purposes, such
  188. as image processing or numeric analysis.</p>
  189. <p>While each of these types have their own semantics, they share the common
  190. characteristic of being backed by a possibly large memory buffer. It is
  191. then desirable, in some situations, to access that buffer directly and
  192. without intermediate copying.</p>
  193. <p>Python provides such a facility at the C level in the form of the <a class="reference internal" href="#bufferobjects"><span class="std std-ref">buffer
  194. protocol</span></a>. This protocol has two sides:</p>
  195. <ul class="simple" id="index-1">
  196. <li><p>on the producer side, a type can export a “buffer interface” which allows
  197. objects of that type to expose information about their underlying buffer.
  198. This interface is described in the section <a class="reference internal" href="typeobj.html#buffer-structs"><span class="std std-ref">Buffer Object Structures</span></a>;</p></li>
  199. <li><p>on the consumer side, several means are available to obtain a pointer to
  200. the raw underlying data of an object (for example a method parameter).</p></li>
  201. </ul>
  202. <p>Simple objects such as <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> and <a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> expose their
  203. underlying buffer in byte-oriented form. Other forms are possible; for example,
  204. the elements exposed by an <a class="reference internal" href="../library/array.html#array.array" title="array.array"><code class="xref py py-class docutils literal notranslate"><span class="pre">array.array</span></code></a> can be multi-byte values.</p>
  205. <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"><code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code></a>
  206. method of file objects: any object that can export a series of bytes through
  207. the buffer interface can be written to a file. While <code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> only
  208. needs read-only access to the internal contents of the object passed to it,
  209. other methods such as <a class="reference internal" href="../library/io.html#io.BufferedIOBase.readinto" title="io.BufferedIOBase.readinto"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readinto()</span></code></a> need write access
  210. to the contents of their argument. The buffer interface allows objects to
  211. selectively allow or reject exporting of read-write and read-only buffers.</p>
  212. <p>There are two ways for a consumer of the buffer interface to acquire a buffer
  213. over a target object:</p>
  214. <ul class="simple">
  215. <li><p>call <a class="reference internal" href="#c.PyObject_GetBuffer" title="PyObject_GetBuffer"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetBuffer()</span></code></a> with the right parameters;</p></li>
  216. <li><p>call <a class="reference internal" href="arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a> (or one of its siblings) with one of the
  217. <code class="docutils literal notranslate"><span class="pre">y*</span></code>, <code class="docutils literal notranslate"><span class="pre">w*</span></code> or <code class="docutils literal notranslate"><span class="pre">s*</span></code> <a class="reference internal" href="arg.html#arg-parsing"><span class="std std-ref">format codes</span></a>.</p></li>
  218. </ul>
  219. <p>In both cases, <a class="reference internal" href="#c.PyBuffer_Release" title="PyBuffer_Release"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyBuffer_Release()</span></code></a> must be called when the buffer
  220. isn’t needed anymore. Failure to do so could lead to various issues such as
  221. resource leaks.</p>
  222. <section id="buffer-structure">
  223. <span id="id1"></span><h2>Buffer structure<a class="headerlink" href="#buffer-structure" title="Link to this heading">¶</a></h2>
  224. <p>Buffer structures (or simply “buffers”) are useful as a way to expose the
  225. binary data from another object to the Python programmer. They can also be
  226. used as a zero-copy slicing mechanism. Using their ability to reference a
  227. block of memory, it is possible to expose any data to the Python programmer
  228. quite easily. The memory could be a large, constant array in a C extension,
  229. it could be a raw block of memory for manipulation before passing to an
  230. operating system library, or it could be used to pass around structured data
  231. in its native, in-memory format.</p>
  232. <p>Contrary to most data types exposed by the Python interpreter, buffers
  233. are not <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span></code></a> pointers but rather simple C structures. This
  234. allows them to be created and copied very simply. When a generic wrapper
  235. around a buffer is needed, a <a class="reference internal" href="memoryview.html#memoryview-objects"><span class="std std-ref">memoryview</span></a> object
  236. can be created.</p>
  237. <p>For short instructions how to write an exporting object, see
  238. <a class="reference internal" href="typeobj.html#buffer-structs"><span class="std std-ref">Buffer Object Structures</span></a>. For obtaining
  239. a buffer, see <a class="reference internal" href="#c.PyObject_GetBuffer" title="PyObject_GetBuffer"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetBuffer()</span></code></a>.</p>
  240. <dl class="c type">
  241. <dt class="sig sig-object c" id="c.Py_buffer">
  242. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_buffer</span></span></span><a class="headerlink" href="#c.Py_buffer" title="Link to this definition">¶</a><br /></dt>
  243. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> (including all members) since version 3.11.</em><dl class="c member">
  244. <dt class="sig sig-object c" id="c.Py_buffer.buf">
  245. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">buf</span></span></span><a class="headerlink" href="#c.Py_buffer.buf" title="Link to this definition">¶</a><br /></dt>
  246. <dd><p>A pointer to the start of the logical structure described by the buffer
  247. fields. This can be any location within the underlying physical memory
  248. block of the exporter. For example, with negative <a class="reference internal" href="#c.Py_buffer.strides" title="Py_buffer.strides"><code class="xref c c-member docutils literal notranslate"><span class="pre">strides</span></code></a>
  249. the value may point to the end of the memory block.</p>
  250. <p>For <a class="reference internal" href="../glossary.html#term-contiguous"><span class="xref std std-term">contiguous</span></a> arrays, the value points to the beginning of
  251. the memory block.</p>
  252. </dd></dl>
  253. <dl class="c member">
  254. <dt class="sig sig-object c" id="c.Py_buffer.obj">
  255. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">obj</span></span></span><a class="headerlink" href="#c.Py_buffer.obj" title="Link to this definition">¶</a><br /></dt>
  256. <dd><p>A new reference to the exporting object. The reference is owned by
  257. the consumer and automatically released
  258. (i.e. reference count decremented)
  259. and set to <code class="docutils literal notranslate"><span class="pre">NULL</span></code> by
  260. <a class="reference internal" href="#c.PyBuffer_Release" title="PyBuffer_Release"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyBuffer_Release()</span></code></a>. The field is the equivalent of the return
  261. value of any standard C-API function.</p>
  262. <p>As a special case, for <em>temporary</em> buffers that are wrapped by
  263. <a class="reference internal" href="memoryview.html#c.PyMemoryView_FromBuffer" title="PyMemoryView_FromBuffer"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMemoryView_FromBuffer()</span></code></a> or <a class="reference internal" href="#c.PyBuffer_FillInfo" title="PyBuffer_FillInfo"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyBuffer_FillInfo()</span></code></a>
  264. this field is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>. In general, exporting objects MUST NOT
  265. use this scheme.</p>
  266. </dd></dl>
  267. <dl class="c member">
  268. <dt class="sig sig-object c" id="c.Py_buffer.len">
  269. <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">len</span></span></span><a class="headerlink" href="#c.Py_buffer.len" title="Link to this definition">¶</a><br /></dt>
  270. <dd><p><code class="docutils literal notranslate"><span class="pre">product(shape)</span> <span class="pre">*</span> <span class="pre">itemsize</span></code>. For contiguous arrays, this is the length
  271. of the underlying memory block. For non-contiguous arrays, it is the length
  272. that the logical structure would have if it were copied to a contiguous
  273. representation.</p>
  274. <p>Accessing <code class="docutils literal notranslate"><span class="pre">((char</span> <span class="pre">*)buf)[0]</span> <span class="pre">up</span> <span class="pre">to</span> <span class="pre">((char</span> <span class="pre">*)buf)[len-1]</span></code> is only valid
  275. if the buffer has been obtained by a request that guarantees contiguity. In
  276. most cases such a request will be <a class="reference internal" href="#c.PyBUF_SIMPLE" title="PyBUF_SIMPLE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyBUF_SIMPLE</span></code></a> or <a class="reference internal" href="#c.PyBUF_WRITABLE" title="PyBUF_WRITABLE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyBUF_WRITABLE</span></code></a>.</p>
  277. </dd></dl>
  278. <dl class="c member">
  279. <dt class="sig sig-object c" id="c.Py_buffer.readonly">
  280. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">readonly</span></span></span><a class="headerlink" href="#c.Py_buffer.readonly" title="Link to this definition">¶</a><br /></dt>
  281. <dd><p>An indicator of whether the buffer is read-only. This field is controlled
  282. by the <a class="reference internal" href="#c.PyBUF_WRITABLE" title="PyBUF_WRITABLE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyBUF_WRITABLE</span></code></a> flag.</p>
  283. </dd></dl>
  284. <dl class="c member">
  285. <dt class="sig sig-object c" id="c.Py_buffer.itemsize">
  286. <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">itemsize</span></span></span><a class="headerlink" href="#c.Py_buffer.itemsize" title="Link to this definition">¶</a><br /></dt>
  287. <dd><p>Item size in bytes of a single element. Same as the value of <a class="reference internal" href="../library/struct.html#struct.calcsize" title="struct.calcsize"><code class="xref py py-func docutils literal notranslate"><span class="pre">struct.calcsize()</span></code></a>
  288. called on non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code> <a class="reference internal" href="#c.Py_buffer.format" title="Py_buffer.format"><code class="xref c c-member docutils literal notranslate"><span class="pre">format</span></code></a> values.</p>
  289. <p>Important exception: If a consumer requests a buffer without the
  290. <a class="reference internal" href="#c.PyBUF_FORMAT" title="PyBUF_FORMAT"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyBUF_FORMAT</span></code></a> flag, <a class="reference internal" href="#c.Py_buffer.format" title="Py_buffer.format"><code class="xref c c-member docutils literal notranslate"><span class="pre">format</span></code></a> will
  291. be set to <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, but <a class="reference internal" href="#c.Py_buffer.itemsize" title="Py_buffer.itemsize"><code class="xref c c-member docutils literal notranslate"><span class="pre">itemsize</span></code></a> still has
  292. the value for the original format.</p>
  293. <p>If <a class="reference internal" href="#c.Py_buffer.shape" title="Py_buffer.shape"><code class="xref c c-member docutils literal notranslate"><span class="pre">shape</span></code></a> is present, the equality
  294. <code class="docutils literal notranslate"><span class="pre">product(shape)</span> <span class="pre">*</span> <span class="pre">itemsize</span> <span class="pre">==</span> <span class="pre">len</span></code> still holds and the consumer
  295. can use <a class="reference internal" href="#c.Py_buffer.itemsize" title="Py_buffer.itemsize"><code class="xref c c-member docutils literal notranslate"><span class="pre">itemsize</span></code></a> to navigate the buffer.</p>
  296. <p>If <a class="reference internal" href="#c.Py_buffer.shape" title="Py_buffer.shape"><code class="xref c c-member docutils literal notranslate"><span class="pre">shape</span></code></a> is <code class="docutils literal notranslate"><span class="pre">NULL</span></code> as a result of a <a class="reference internal" href="#c.PyBUF_SIMPLE" title="PyBUF_SIMPLE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyBUF_SIMPLE</span></code></a>
  297. or a <a class="reference internal" href="#c.PyBUF_WRITABLE" title="PyBUF_WRITABLE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyBUF_WRITABLE</span></code></a> request, the consumer must disregard
  298. <a class="reference internal" href="#c.Py_buffer.itemsize" title="Py_buffer.itemsize"><code class="xref c c-member docutils literal notranslate"><span class="pre">itemsize</span></code></a> and assume <code class="docutils literal notranslate"><span class="pre">itemsize</span> <span class="pre">==</span> <span class="pre">1</span></code>.</p>
  299. </dd></dl>
  300. <dl class="c member">
  301. <dt class="sig sig-object c" id="c.Py_buffer.format">
  302. <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">format</span></span></span><a class="headerlink" href="#c.Py_buffer.format" title="Link to this definition">¶</a><br /></dt>
  303. <dd><p>A <em>NUL</em> terminated string in <a class="reference internal" href="../library/struct.html#module-struct" title="struct: Interpret bytes as packed binary data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code></a> module style syntax describing
  304. the contents of a single item. If this is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;B&quot;</span></code> (unsigned bytes)
  305. is assumed.</p>
  306. <p>This field is controlled by the <a class="reference internal" href="#c.PyBUF_FORMAT" title="PyBUF_FORMAT"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyBUF_FORMAT</span></code></a> flag.</p>
  307. </dd></dl>
  308. <dl class="c member">
  309. <dt class="sig sig-object c" id="c.Py_buffer.ndim">
  310. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">ndim</span></span></span><a class="headerlink" href="#c.Py_buffer.ndim" title="Link to this definition">¶</a><br /></dt>
  311. <dd><p>The number of dimensions the memory represents as an n-dimensional array.
  312. If it is <code class="docutils literal notranslate"><span class="pre">0</span></code>, <a class="reference internal" href="#c.Py_buffer.buf" title="Py_buffer.buf"><code class="xref c c-member docutils literal notranslate"><span class="pre">buf</span></code></a> points to a single item representing
  313. a scalar. In this case, <a class="reference internal" href="#c.Py_buffer.shape" title="Py_buffer.shape"><code class="xref c c-member docutils literal notranslate"><span class="pre">shape</span></code></a>, <a class="reference internal" href="#c.Py_buffer.strides" title="Py_buffer.strides"><code class="xref c c-member docutils literal notranslate"><span class="pre">strides</span></code></a>
  314. and <a class="reference internal" href="#c.Py_buffer.suboffsets" title="Py_buffer.suboffsets"><code class="xref c c-member docutils literal notranslate"><span class="pre">suboffsets</span></code></a> MUST be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.
  315. The maximum number of dimensions is given by <a class="reference internal" href="#c.PyBUF_MAX_NDIM" title="PyBUF_MAX_NDIM"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyBUF_MAX_NDIM</span></code></a>.</p>
  316. </dd></dl>
  317. <dl class="c member">
  318. <dt class="sig sig-object c" id="c.Py_buffer.shape">
  319. <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">shape</span></span></span><a class="headerlink" href="#c.Py_buffer.shape" title="Link to this definition">¶</a><br /></dt>
  320. <dd><p>An array of <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_ssize_t</span></code></a> of length <a class="reference internal" href="#c.Py_buffer.ndim" title="Py_buffer.ndim"><code class="xref c c-member docutils literal notranslate"><span class="pre">ndim</span></code></a>
  321. indicating the shape of the memory as an n-dimensional array. Note that
  322. <code class="docutils literal notranslate"><span class="pre">shape[0]</span> <span class="pre">*</span> <span class="pre">...</span> <span class="pre">*</span> <span class="pre">shape[ndim-1]</span> <span class="pre">*</span> <span class="pre">itemsize</span></code> MUST be equal to
  323. <a class="reference internal" href="#c.Py_buffer.len" title="Py_buffer.len"><code class="xref c c-member docutils literal notranslate"><span class="pre">len</span></code></a>.</p>
  324. <p>Shape values are restricted to <code class="docutils literal notranslate"><span class="pre">shape[n]</span> <span class="pre">&gt;=</span> <span class="pre">0</span></code>. The case
  325. <code class="docutils literal notranslate"><span class="pre">shape[n]</span> <span class="pre">==</span> <span class="pre">0</span></code> requires special attention. See <a class="reference internal" href="#complex-arrays">complex arrays</a>
  326. for further information.</p>
  327. <p>The shape array is read-only for the consumer.</p>
  328. </dd></dl>
  329. <dl class="c member">
  330. <dt class="sig sig-object c" id="c.Py_buffer.strides">
  331. <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">strides</span></span></span><a class="headerlink" href="#c.Py_buffer.strides" title="Link to this definition">¶</a><br /></dt>
  332. <dd><p>An array of <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_ssize_t</span></code></a> of length <a class="reference internal" href="#c.Py_buffer.ndim" title="Py_buffer.ndim"><code class="xref c c-member docutils literal notranslate"><span class="pre">ndim</span></code></a>
  333. giving the number of bytes to skip to get to a new element in each
  334. dimension.</p>
  335. <p>Stride values can be any integer. For regular arrays, strides are
  336. usually positive, but a consumer MUST be able to handle the case
  337. <code class="docutils literal notranslate"><span class="pre">strides[n]</span> <span class="pre">&lt;=</span> <span class="pre">0</span></code>. See <a class="reference internal" href="#complex-arrays">complex arrays</a> for further information.</p>
  338. <p>The strides array is read-only for the consumer.</p>
  339. </dd></dl>
  340. <dl class="c member">
  341. <dt class="sig sig-object c" id="c.Py_buffer.suboffsets">
  342. <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">suboffsets</span></span></span><a class="headerlink" href="#c.Py_buffer.suboffsets" title="Link to this definition">¶</a><br /></dt>
  343. <dd><p>An array of <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_ssize_t</span></code></a> of length <a class="reference internal" href="#c.Py_buffer.ndim" title="Py_buffer.ndim"><code class="xref c c-member docutils literal notranslate"><span class="pre">ndim</span></code></a>.
  344. If <code class="docutils literal notranslate"><span class="pre">suboffsets[n]</span> <span class="pre">&gt;=</span> <span class="pre">0</span></code>, the values stored along the nth dimension are
  345. pointers and the suboffset value dictates how many bytes to add to each
  346. pointer after de-referencing. A suboffset value that is negative
  347. indicates that no de-referencing should occur (striding in a contiguous
  348. memory block).</p>
  349. <p>If all suboffsets are negative (i.e. no de-referencing is needed), then
  350. this field must be <code class="docutils literal notranslate"><span class="pre">NULL</span></code> (the default value).</p>
  351. <p>This type of array representation is used by the Python Imaging Library
  352. (PIL). See <a class="reference internal" href="#complex-arrays">complex arrays</a> for further information how to access elements
  353. of such an array.</p>
  354. <p>The suboffsets array is read-only for the consumer.</p>
  355. </dd></dl>
  356. <dl class="c member">
  357. <dt class="sig sig-object c" id="c.Py_buffer.internal">
  358. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">internal</span></span></span><a class="headerlink" href="#c.Py_buffer.internal" title="Link to this definition">¶</a><br /></dt>
  359. <dd><p>This is for use internally by the exporting object. For example, this
  360. might be re-cast as an integer by the exporter and used to store flags
  361. about whether or not the shape, strides, and suboffsets arrays must be
  362. freed when the buffer is released. The consumer MUST NOT alter this
  363. value.</p>
  364. </dd></dl>
  365. </dd></dl>
  366. <p>Constants:</p>
  367. <dl class="c macro">
  368. <dt class="sig sig-object c" id="c.PyBUF_MAX_NDIM">
  369. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_MAX_NDIM</span></span></span><a class="headerlink" href="#c.PyBUF_MAX_NDIM" title="Link to this definition">¶</a><br /></dt>
  370. <dd><p>The maximum number of dimensions the memory represents.
  371. Exporters MUST respect this limit, consumers of multi-dimensional
  372. buffers SHOULD be able to handle up to <code class="xref c c-macro docutils literal notranslate"><span class="pre">PyBUF_MAX_NDIM</span></code> dimensions.
  373. Currently set to 64.</p>
  374. </dd></dl>
  375. </section>
  376. <section id="buffer-request-types">
  377. <span id="id2"></span><h2>Buffer request types<a class="headerlink" href="#buffer-request-types" title="Link to this heading">¶</a></h2>
  378. <p>Buffers are usually obtained by sending a buffer request to an exporting
  379. object via <a class="reference internal" href="#c.PyObject_GetBuffer" title="PyObject_GetBuffer"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetBuffer()</span></code></a>. Since the complexity of the logical
  380. structure of the memory can vary drastically, the consumer uses the <em>flags</em>
  381. argument to specify the exact buffer type it can handle.</p>
  382. <p>All <a class="reference internal" href="#c.Py_buffer" title="Py_buffer"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_buffer</span></code></a> fields are unambiguously defined by the request
  383. type.</p>
  384. <section id="request-independent-fields">
  385. <h3>request-independent fields<a class="headerlink" href="#request-independent-fields" title="Link to this heading">¶</a></h3>
  386. <p>The following fields are not influenced by <em>flags</em> and must always be filled in
  387. with the correct values: <a class="reference internal" href="#c.Py_buffer.obj" title="Py_buffer.obj"><code class="xref c c-member docutils literal notranslate"><span class="pre">obj</span></code></a>, <a class="reference internal" href="#c.Py_buffer.buf" title="Py_buffer.buf"><code class="xref c c-member docutils literal notranslate"><span class="pre">buf</span></code></a>,
  388. <a class="reference internal" href="#c.Py_buffer.len" title="Py_buffer.len"><code class="xref c c-member docutils literal notranslate"><span class="pre">len</span></code></a>, <a class="reference internal" href="#c.Py_buffer.itemsize" title="Py_buffer.itemsize"><code class="xref c c-member docutils literal notranslate"><span class="pre">itemsize</span></code></a>, <a class="reference internal" href="#c.Py_buffer.ndim" title="Py_buffer.ndim"><code class="xref c c-member docutils literal notranslate"><span class="pre">ndim</span></code></a>.</p>
  389. </section>
  390. <section id="readonly-format">
  391. <h3>readonly, format<a class="headerlink" href="#readonly-format" title="Link to this heading">¶</a></h3>
  392. <blockquote>
  393. <div><dl class="c macro">
  394. <dt class="sig sig-object c" id="c.PyBUF_WRITABLE">
  395. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_WRITABLE</span></span></span><a class="headerlink" href="#c.PyBUF_WRITABLE" title="Link to this definition">¶</a><br /></dt>
  396. <dd><p>Controls the <a class="reference internal" href="#c.Py_buffer.readonly" title="Py_buffer.readonly"><code class="xref c c-member docutils literal notranslate"><span class="pre">readonly</span></code></a> field. If set, the exporter
  397. MUST provide a writable buffer or else report failure. Otherwise, the
  398. exporter MAY provide either a read-only or writable buffer, but the choice
  399. MUST be consistent for all consumers.</p>
  400. </dd></dl>
  401. <dl class="c macro">
  402. <dt class="sig sig-object c" id="c.PyBUF_FORMAT">
  403. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_FORMAT</span></span></span><a class="headerlink" href="#c.PyBUF_FORMAT" title="Link to this definition">¶</a><br /></dt>
  404. <dd><p>Controls the <a class="reference internal" href="#c.Py_buffer.format" title="Py_buffer.format"><code class="xref c c-member docutils literal notranslate"><span class="pre">format</span></code></a> field. If set, this field MUST
  405. be filled in correctly. Otherwise, this field MUST be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  406. </dd></dl>
  407. </div></blockquote>
  408. <p><a class="reference internal" href="#c.PyBUF_WRITABLE" title="PyBUF_WRITABLE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyBUF_WRITABLE</span></code></a> can be |’d to any of the flags in the next section.
  409. Since <a class="reference internal" href="#c.PyBUF_SIMPLE" title="PyBUF_SIMPLE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyBUF_SIMPLE</span></code></a> is defined as 0, <a class="reference internal" href="#c.PyBUF_WRITABLE" title="PyBUF_WRITABLE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyBUF_WRITABLE</span></code></a>
  410. can be used as a stand-alone flag to request a simple writable buffer.</p>
  411. <p><a class="reference internal" href="#c.PyBUF_FORMAT" title="PyBUF_FORMAT"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyBUF_FORMAT</span></code></a> can be |’d to any of the flags except <a class="reference internal" href="#c.PyBUF_SIMPLE" title="PyBUF_SIMPLE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyBUF_SIMPLE</span></code></a>.
  412. The latter already implies format <code class="docutils literal notranslate"><span class="pre">B</span></code> (unsigned bytes).</p>
  413. </section>
  414. <section id="shape-strides-suboffsets">
  415. <h3>shape, strides, suboffsets<a class="headerlink" href="#shape-strides-suboffsets" title="Link to this heading">¶</a></h3>
  416. <p>The flags that control the logical structure of the memory are listed
  417. in decreasing order of complexity. Note that each flag contains all bits
  418. of the flags below it.</p>
  419. <table class="docutils align-default">
  420. <thead>
  421. <tr class="row-odd"><th class="head"><p>Request</p></th>
  422. <th class="head"><p>shape</p></th>
  423. <th class="head"><p>strides</p></th>
  424. <th class="head"><p>suboffsets</p></th>
  425. </tr>
  426. </thead>
  427. <tbody>
  428. <tr class="row-even"><td><dl class="c macro">
  429. <dt class="sig sig-object c" id="c.PyBUF_INDIRECT">
  430. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_INDIRECT</span></span></span><a class="headerlink" href="#c.PyBUF_INDIRECT" title="Link to this definition">¶</a><br /></dt>
  431. <dd></dd></dl>
  432. </td>
  433. <td><p>yes</p></td>
  434. <td><p>yes</p></td>
  435. <td><p>if needed</p></td>
  436. </tr>
  437. <tr class="row-odd"><td><dl class="c macro">
  438. <dt class="sig sig-object c" id="c.PyBUF_STRIDES">
  439. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_STRIDES</span></span></span><a class="headerlink" href="#c.PyBUF_STRIDES" title="Link to this definition">¶</a><br /></dt>
  440. <dd></dd></dl>
  441. </td>
  442. <td><p>yes</p></td>
  443. <td><p>yes</p></td>
  444. <td><p>NULL</p></td>
  445. </tr>
  446. <tr class="row-even"><td><dl class="c macro">
  447. <dt class="sig sig-object c" id="c.PyBUF_ND">
  448. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_ND</span></span></span><a class="headerlink" href="#c.PyBUF_ND" title="Link to this definition">¶</a><br /></dt>
  449. <dd></dd></dl>
  450. </td>
  451. <td><p>yes</p></td>
  452. <td><p>NULL</p></td>
  453. <td><p>NULL</p></td>
  454. </tr>
  455. <tr class="row-odd"><td><dl class="c macro">
  456. <dt class="sig sig-object c" id="c.PyBUF_SIMPLE">
  457. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_SIMPLE</span></span></span><a class="headerlink" href="#c.PyBUF_SIMPLE" title="Link to this definition">¶</a><br /></dt>
  458. <dd></dd></dl>
  459. </td>
  460. <td><p>NULL</p></td>
  461. <td><p>NULL</p></td>
  462. <td><p>NULL</p></td>
  463. </tr>
  464. </tbody>
  465. </table>
  466. </section>
  467. <section id="contiguity-requests">
  468. <span id="index-2"></span><h3>contiguity requests<a class="headerlink" href="#contiguity-requests" title="Link to this heading">¶</a></h3>
  469. <p>C or Fortran <a class="reference internal" href="../glossary.html#term-contiguous"><span class="xref std std-term">contiguity</span></a> can be explicitly requested,
  470. with and without stride information. Without stride information, the buffer
  471. must be C-contiguous.</p>
  472. <table class="docutils align-default">
  473. <thead>
  474. <tr class="row-odd"><th class="head"><p>Request</p></th>
  475. <th class="head"><p>shape</p></th>
  476. <th class="head"><p>strides</p></th>
  477. <th class="head"><p>suboffsets</p></th>
  478. <th class="head"><p>contig</p></th>
  479. </tr>
  480. </thead>
  481. <tbody>
  482. <tr class="row-even"><td><dl class="c macro">
  483. <dt class="sig sig-object c" id="c.PyBUF_C_CONTIGUOUS">
  484. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_C_CONTIGUOUS</span></span></span><a class="headerlink" href="#c.PyBUF_C_CONTIGUOUS" title="Link to this definition">¶</a><br /></dt>
  485. <dd></dd></dl>
  486. </td>
  487. <td><p>yes</p></td>
  488. <td><p>yes</p></td>
  489. <td><p>NULL</p></td>
  490. <td><p>C</p></td>
  491. </tr>
  492. <tr class="row-odd"><td><dl class="c macro">
  493. <dt class="sig sig-object c" id="c.PyBUF_F_CONTIGUOUS">
  494. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_F_CONTIGUOUS</span></span></span><a class="headerlink" href="#c.PyBUF_F_CONTIGUOUS" title="Link to this definition">¶</a><br /></dt>
  495. <dd></dd></dl>
  496. </td>
  497. <td><p>yes</p></td>
  498. <td><p>yes</p></td>
  499. <td><p>NULL</p></td>
  500. <td><p>F</p></td>
  501. </tr>
  502. <tr class="row-even"><td><dl class="c macro">
  503. <dt class="sig sig-object c" id="c.PyBUF_ANY_CONTIGUOUS">
  504. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_ANY_CONTIGUOUS</span></span></span><a class="headerlink" href="#c.PyBUF_ANY_CONTIGUOUS" title="Link to this definition">¶</a><br /></dt>
  505. <dd></dd></dl>
  506. </td>
  507. <td><p>yes</p></td>
  508. <td><p>yes</p></td>
  509. <td><p>NULL</p></td>
  510. <td><p>C or F</p></td>
  511. </tr>
  512. <tr class="row-odd"><td><p><a class="reference internal" href="#c.PyBUF_ND" title="PyBUF_ND"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyBUF_ND</span></code></a></p></td>
  513. <td><p>yes</p></td>
  514. <td><p>NULL</p></td>
  515. <td><p>NULL</p></td>
  516. <td><p>C</p></td>
  517. </tr>
  518. </tbody>
  519. </table>
  520. </section>
  521. <section id="compound-requests">
  522. <h3>compound requests<a class="headerlink" href="#compound-requests" title="Link to this heading">¶</a></h3>
  523. <p>All possible requests are fully defined by some combination of the flags in
  524. the previous section. For convenience, the buffer protocol provides frequently
  525. used combinations as single flags.</p>
  526. <p>In the following table <em>U</em> stands for undefined contiguity. The consumer would
  527. have to call <a class="reference internal" href="#c.PyBuffer_IsContiguous" title="PyBuffer_IsContiguous"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyBuffer_IsContiguous()</span></code></a> to determine contiguity.</p>
  528. <table class="docutils align-default">
  529. <thead>
  530. <tr class="row-odd"><th class="head"><p>Request</p></th>
  531. <th class="head"><p>shape</p></th>
  532. <th class="head"><p>strides</p></th>
  533. <th class="head"><p>suboffsets</p></th>
  534. <th class="head"><p>contig</p></th>
  535. <th class="head"><p>readonly</p></th>
  536. <th class="head"><p>format</p></th>
  537. </tr>
  538. </thead>
  539. <tbody>
  540. <tr class="row-even"><td><dl class="c macro">
  541. <dt class="sig sig-object c" id="c.PyBUF_FULL">
  542. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_FULL</span></span></span><a class="headerlink" href="#c.PyBUF_FULL" title="Link to this definition">¶</a><br /></dt>
  543. <dd></dd></dl>
  544. </td>
  545. <td><p>yes</p></td>
  546. <td><p>yes</p></td>
  547. <td><p>if needed</p></td>
  548. <td><p>U</p></td>
  549. <td><p>0</p></td>
  550. <td><p>yes</p></td>
  551. </tr>
  552. <tr class="row-odd"><td><dl class="c macro">
  553. <dt class="sig sig-object c" id="c.PyBUF_FULL_RO">
  554. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_FULL_RO</span></span></span><a class="headerlink" href="#c.PyBUF_FULL_RO" title="Link to this definition">¶</a><br /></dt>
  555. <dd></dd></dl>
  556. </td>
  557. <td><p>yes</p></td>
  558. <td><p>yes</p></td>
  559. <td><p>if needed</p></td>
  560. <td><p>U</p></td>
  561. <td><p>1 or 0</p></td>
  562. <td><p>yes</p></td>
  563. </tr>
  564. <tr class="row-even"><td><dl class="c macro">
  565. <dt class="sig sig-object c" id="c.PyBUF_RECORDS">
  566. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_RECORDS</span></span></span><a class="headerlink" href="#c.PyBUF_RECORDS" title="Link to this definition">¶</a><br /></dt>
  567. <dd></dd></dl>
  568. </td>
  569. <td><p>yes</p></td>
  570. <td><p>yes</p></td>
  571. <td><p>NULL</p></td>
  572. <td><p>U</p></td>
  573. <td><p>0</p></td>
  574. <td><p>yes</p></td>
  575. </tr>
  576. <tr class="row-odd"><td><dl class="c macro">
  577. <dt class="sig sig-object c" id="c.PyBUF_RECORDS_RO">
  578. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_RECORDS_RO</span></span></span><a class="headerlink" href="#c.PyBUF_RECORDS_RO" title="Link to this definition">¶</a><br /></dt>
  579. <dd></dd></dl>
  580. </td>
  581. <td><p>yes</p></td>
  582. <td><p>yes</p></td>
  583. <td><p>NULL</p></td>
  584. <td><p>U</p></td>
  585. <td><p>1 or 0</p></td>
  586. <td><p>yes</p></td>
  587. </tr>
  588. <tr class="row-even"><td><dl class="c macro">
  589. <dt class="sig sig-object c" id="c.PyBUF_STRIDED">
  590. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_STRIDED</span></span></span><a class="headerlink" href="#c.PyBUF_STRIDED" title="Link to this definition">¶</a><br /></dt>
  591. <dd></dd></dl>
  592. </td>
  593. <td><p>yes</p></td>
  594. <td><p>yes</p></td>
  595. <td><p>NULL</p></td>
  596. <td><p>U</p></td>
  597. <td><p>0</p></td>
  598. <td><p>NULL</p></td>
  599. </tr>
  600. <tr class="row-odd"><td><dl class="c macro">
  601. <dt class="sig sig-object c" id="c.PyBUF_STRIDED_RO">
  602. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_STRIDED_RO</span></span></span><a class="headerlink" href="#c.PyBUF_STRIDED_RO" title="Link to this definition">¶</a><br /></dt>
  603. <dd></dd></dl>
  604. </td>
  605. <td><p>yes</p></td>
  606. <td><p>yes</p></td>
  607. <td><p>NULL</p></td>
  608. <td><p>U</p></td>
  609. <td><p>1 or 0</p></td>
  610. <td><p>NULL</p></td>
  611. </tr>
  612. <tr class="row-even"><td><dl class="c macro">
  613. <dt class="sig sig-object c" id="c.PyBUF_CONTIG">
  614. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_CONTIG</span></span></span><a class="headerlink" href="#c.PyBUF_CONTIG" title="Link to this definition">¶</a><br /></dt>
  615. <dd></dd></dl>
  616. </td>
  617. <td><p>yes</p></td>
  618. <td><p>NULL</p></td>
  619. <td><p>NULL</p></td>
  620. <td><p>C</p></td>
  621. <td><p>0</p></td>
  622. <td><p>NULL</p></td>
  623. </tr>
  624. <tr class="row-odd"><td><dl class="c macro">
  625. <dt class="sig sig-object c" id="c.PyBUF_CONTIG_RO">
  626. <span class="sig-name descname"><span class="n"><span class="pre">PyBUF_CONTIG_RO</span></span></span><a class="headerlink" href="#c.PyBUF_CONTIG_RO" title="Link to this definition">¶</a><br /></dt>
  627. <dd></dd></dl>
  628. </td>
  629. <td><p>yes</p></td>
  630. <td><p>NULL</p></td>
  631. <td><p>NULL</p></td>
  632. <td><p>C</p></td>
  633. <td><p>1 or 0</p></td>
  634. <td><p>NULL</p></td>
  635. </tr>
  636. </tbody>
  637. </table>
  638. </section>
  639. </section>
  640. <section id="complex-arrays">
  641. <h2>Complex arrays<a class="headerlink" href="#complex-arrays" title="Link to this heading">¶</a></h2>
  642. <section id="numpy-style-shape-and-strides">
  643. <h3>NumPy-style: shape and strides<a class="headerlink" href="#numpy-style-shape-and-strides" title="Link to this heading">¶</a></h3>
  644. <p>The logical structure of NumPy-style arrays is defined by <a class="reference internal" href="#c.Py_buffer.itemsize" title="Py_buffer.itemsize"><code class="xref c c-member docutils literal notranslate"><span class="pre">itemsize</span></code></a>,
  645. <a class="reference internal" href="#c.Py_buffer.ndim" title="Py_buffer.ndim"><code class="xref c c-member docutils literal notranslate"><span class="pre">ndim</span></code></a>, <a class="reference internal" href="#c.Py_buffer.shape" title="Py_buffer.shape"><code class="xref c c-member docutils literal notranslate"><span class="pre">shape</span></code></a> and <a class="reference internal" href="#c.Py_buffer.strides" title="Py_buffer.strides"><code class="xref c c-member docutils literal notranslate"><span class="pre">strides</span></code></a>.</p>
  646. <p>If <code class="docutils literal notranslate"><span class="pre">ndim</span> <span class="pre">==</span> <span class="pre">0</span></code>, the memory location pointed to by <a class="reference internal" href="#c.Py_buffer.buf" title="Py_buffer.buf"><code class="xref c c-member docutils literal notranslate"><span class="pre">buf</span></code></a> is
  647. interpreted as a scalar of size <a class="reference internal" href="#c.Py_buffer.itemsize" title="Py_buffer.itemsize"><code class="xref c c-member docutils literal notranslate"><span class="pre">itemsize</span></code></a>. In that case,
  648. both <a class="reference internal" href="#c.Py_buffer.shape" title="Py_buffer.shape"><code class="xref c c-member docutils literal notranslate"><span class="pre">shape</span></code></a> and <a class="reference internal" href="#c.Py_buffer.strides" title="Py_buffer.strides"><code class="xref c c-member docutils literal notranslate"><span class="pre">strides</span></code></a> are <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  649. <p>If <a class="reference internal" href="#c.Py_buffer.strides" title="Py_buffer.strides"><code class="xref c c-member docutils literal notranslate"><span class="pre">strides</span></code></a> is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, the array is interpreted as
  650. a standard n-dimensional C-array. Otherwise, the consumer must access an
  651. n-dimensional array as follows:</p>
  652. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">ptr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">buf</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">indices</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">strides</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">indices</span><span class="p">[</span><span class="n">n</span><span class="mi">-1</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">strides</span><span class="p">[</span><span class="n">n</span><span class="mi">-1</span><span class="p">];</span>
  653. <span class="n">item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="p">((</span><span class="n">typeof</span><span class="p">(</span><span class="n">item</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="n">ptr</span><span class="p">);</span>
  654. </pre></div>
  655. </div>
  656. <p>As noted above, <a class="reference internal" href="#c.Py_buffer.buf" title="Py_buffer.buf"><code class="xref c c-member docutils literal notranslate"><span class="pre">buf</span></code></a> can point to any location within
  657. the actual memory block. An exporter can check the validity of a buffer with
  658. this function:</p>
  659. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">verify_structure</span><span class="p">(</span><span class="n">memlen</span><span class="p">,</span> <span class="n">itemsize</span><span class="p">,</span> <span class="n">ndim</span><span class="p">,</span> <span class="n">shape</span><span class="p">,</span> <span class="n">strides</span><span class="p">,</span> <span class="n">offset</span><span class="p">):</span>
  660. <span class="w"> </span><span class="sd">&quot;&quot;&quot;Verify that the parameters represent a valid array within</span>
  661. <span class="sd"> the bounds of the allocated memory:</span>
  662. <span class="sd"> char *mem: start of the physical memory block</span>
  663. <span class="sd"> memlen: length of the physical memory block</span>
  664. <span class="sd"> offset: (char *)buf - mem</span>
  665. <span class="sd"> &quot;&quot;&quot;</span>
  666. <span class="k">if</span> <span class="n">offset</span> <span class="o">%</span> <span class="n">itemsize</span><span class="p">:</span>
  667. <span class="k">return</span> <span class="kc">False</span>
  668. <span class="k">if</span> <span class="n">offset</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">offset</span><span class="o">+</span><span class="n">itemsize</span> <span class="o">&gt;</span> <span class="n">memlen</span><span class="p">:</span>
  669. <span class="k">return</span> <span class="kc">False</span>
  670. <span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="n">v</span> <span class="o">%</span> <span class="n">itemsize</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">strides</span><span class="p">):</span>
  671. <span class="k">return</span> <span class="kc">False</span>
  672. <span class="k">if</span> <span class="n">ndim</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
  673. <span class="k">return</span> <span class="n">ndim</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">shape</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">strides</span>
  674. <span class="k">if</span> <span class="mi">0</span> <span class="ow">in</span> <span class="n">shape</span><span class="p">:</span>
  675. <span class="k">return</span> <span class="kc">True</span>
  676. <span class="n">imin</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">strides</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">*</span><span class="p">(</span><span class="n">shape</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">ndim</span><span class="p">)</span>
  677. <span class="k">if</span> <span class="n">strides</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">)</span>
  678. <span class="n">imax</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">(</span><span class="n">strides</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">*</span><span class="p">(</span><span class="n">shape</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">ndim</span><span class="p">)</span>
  679. <span class="k">if</span> <span class="n">strides</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span>
  680. <span class="k">return</span> <span class="mi">0</span> <span class="o">&lt;=</span> <span class="n">offset</span><span class="o">+</span><span class="n">imin</span> <span class="ow">and</span> <span class="n">offset</span><span class="o">+</span><span class="n">imax</span><span class="o">+</span><span class="n">itemsize</span> <span class="o">&lt;=</span> <span class="n">memlen</span>
  681. </pre></div>
  682. </div>
  683. </section>
  684. <section id="pil-style-shape-strides-and-suboffsets">
  685. <h3>PIL-style: shape, strides and suboffsets<a class="headerlink" href="#pil-style-shape-strides-and-suboffsets" title="Link to this heading">¶</a></h3>
  686. <p>In addition to the regular items, PIL-style arrays can contain pointers
  687. that must be followed in order to get to the next element in a dimension.
  688. For example, the regular three-dimensional C-array <code class="docutils literal notranslate"><span class="pre">char</span> <span class="pre">v[2][2][3]</span></code> can
  689. also be viewed as an array of 2 pointers to 2 two-dimensional arrays:
  690. <code class="docutils literal notranslate"><span class="pre">char</span> <span class="pre">(*v[2])[2][3]</span></code>. In suboffsets representation, those two pointers
  691. can be embedded at the start of <a class="reference internal" href="#c.Py_buffer.buf" title="Py_buffer.buf"><code class="xref c c-member docutils literal notranslate"><span class="pre">buf</span></code></a>, pointing
  692. to two <code class="docutils literal notranslate"><span class="pre">char</span> <span class="pre">x[2][3]</span></code> arrays that can be located anywhere in memory.</p>
  693. <p>Here is a function that returns a pointer to the element in an N-D array
  694. pointed to by an N-dimensional index when there are both non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code> strides
  695. and suboffsets:</p>
  696. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="nf">get_item_pointer</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">ndim</span><span class="p">,</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">buf</span><span class="p">,</span><span class="w"> </span><span class="n">Py_ssize_t</span><span class="w"> </span><span class="o">*</span><span class="n">strides</span><span class="p">,</span>
  697. <span class="w"> </span><span class="n">Py_ssize_t</span><span class="w"> </span><span class="o">*</span><span class="n">suboffsets</span><span class="p">,</span><span class="w"> </span><span class="n">Py_ssize_t</span><span class="w"> </span><span class="o">*</span><span class="n">indices</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
  698. <span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">pointer</span><span class="w"> </span><span class="o">=</span><span class="w"> </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>
  699. <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
  700. <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">ndim</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
  701. <span class="w"> </span><span class="n">pointer</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">strides</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">indices</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
  702. <span class="w"> </span><span class="k">if</span><span class="w"> </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="w"> </span><span class="o">&gt;=</span><span class="mi">0</span><span class="w"> </span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
  703. <span class="w"> </span><span class="n">pointer</span><span class="w"> </span><span class="o">=</span><span class="w"> </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="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">suboffsets</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
  704. <span class="w"> </span><span class="p">}</span>
  705. <span class="w"> </span><span class="p">}</span>
  706. <span class="w"> </span><span class="k">return</span><span class="w"> </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>
  707. <span class="p">}</span>
  708. </pre></div>
  709. </div>
  710. </section>
  711. </section>
  712. <section id="buffer-related-functions">
  713. <h2>Buffer-related functions<a class="headerlink" href="#buffer-related-functions" title="Link to this heading">¶</a></h2>
  714. <dl class="c function">
  715. <dt class="sig sig-object c" id="c.PyObject_CheckBuffer">
  716. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_CheckBuffer</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">obj</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_CheckBuffer" title="Link to this definition">¶</a><br /></dt>
  717. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.11.</em><p>Return <code class="docutils literal notranslate"><span class="pre">1</span></code> if <em>obj</em> supports the buffer interface otherwise <code class="docutils literal notranslate"><span class="pre">0</span></code>. When <code class="docutils literal notranslate"><span class="pre">1</span></code> is
  718. returned, it doesn’t guarantee that <a class="reference internal" href="#c.PyObject_GetBuffer" title="PyObject_GetBuffer"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetBuffer()</span></code></a> will
  719. succeed. This function always succeeds.</p>
  720. </dd></dl>
  721. <dl class="c function">
  722. <dt class="sig sig-object c" id="c.PyObject_GetBuffer">
  723. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_GetBuffer</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exporter</span></span>, <a class="reference internal" href="#c.Py_buffer" title="Py_buffer"><span class="n"><span class="pre">Py_buffer</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">view</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">flags</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GetBuffer" title="Link to this definition">¶</a><br /></dt>
  724. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.11.</em><p>Send a request to <em>exporter</em> to fill in <em>view</em> as specified by <em>flags</em>.
  725. If the exporter cannot provide a buffer of the exact type, it MUST raise
  726. <a class="reference internal" href="../library/exceptions.html#BufferError" title="BufferError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BufferError</span></code></a>, set <code class="docutils literal notranslate"><span class="pre">view-&gt;obj</span></code> to <code class="docutils literal notranslate"><span class="pre">NULL</span></code> and
  727. return <code class="docutils literal notranslate"><span class="pre">-1</span></code>.</p>
  728. <p>On success, fill in <em>view</em>, set <code class="docutils literal notranslate"><span class="pre">view-&gt;obj</span></code> to a new reference
  729. to <em>exporter</em> and return 0. In the case of chained buffer providers
  730. that redirect requests to a single object, <code class="docutils literal notranslate"><span class="pre">view-&gt;obj</span></code> MAY
  731. refer to this object instead of <em>exporter</em> (See <a class="reference internal" href="typeobj.html#buffer-structs"><span class="std std-ref">Buffer Object Structures</span></a>).</p>
  732. <p>Successful calls to <a class="reference internal" href="#c.PyObject_GetBuffer" title="PyObject_GetBuffer"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetBuffer()</span></code></a> must be paired with calls
  733. to <a class="reference internal" href="#c.PyBuffer_Release" title="PyBuffer_Release"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyBuffer_Release()</span></code></a>, similar to <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">free()</span></code>.
  734. Thus, after the consumer is done with the buffer, <a class="reference internal" href="#c.PyBuffer_Release" title="PyBuffer_Release"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyBuffer_Release()</span></code></a>
  735. must be called exactly once.</p>
  736. </dd></dl>
  737. <dl class="c function">
  738. <dt class="sig sig-object c" id="c.PyBuffer_Release">
  739. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyBuffer_Release</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_buffer" title="Py_buffer"><span class="n"><span class="pre">Py_buffer</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">view</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyBuffer_Release" title="Link to this definition">¶</a><br /></dt>
  740. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.11.</em><p>Release the buffer <em>view</em> and release the <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a>
  741. (i.e. decrement the reference count) to the view’s supporting object,
  742. <code class="docutils literal notranslate"><span class="pre">view-&gt;obj</span></code>. This function MUST be called when the buffer
  743. is no longer being used, otherwise reference leaks may occur.</p>
  744. <p>It is an error to call this function on a buffer that was not obtained via
  745. <a class="reference internal" href="#c.PyObject_GetBuffer" title="PyObject_GetBuffer"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetBuffer()</span></code></a>.</p>
  746. </dd></dl>
  747. <dl class="c function">
  748. <dt class="sig sig-object c" id="c.PyBuffer_SizeFromFormat">
  749. <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyBuffer_SizeFromFormat</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">format</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyBuffer_SizeFromFormat" title="Link to this definition">¶</a><br /></dt>
  750. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.11.</em><p>Return the implied <a class="reference internal" href="#c.Py_buffer.itemsize" title="Py_buffer.itemsize"><code class="xref c c-member docutils literal notranslate"><span class="pre">itemsize</span></code></a> from <a class="reference internal" href="#c.Py_buffer.format" title="Py_buffer.format"><code class="xref c c-member docutils literal notranslate"><span class="pre">format</span></code></a>.
  751. On error, raise an exception and return -1.</p>
  752. <div class="versionadded">
  753. <p><span class="versionmodified added">New in version 3.9.</span></p>
  754. </div>
  755. </dd></dl>
  756. <dl class="c function">
  757. <dt class="sig sig-object c" id="c.PyBuffer_IsContiguous">
  758. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyBuffer_IsContiguous</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><a class="reference internal" href="#c.Py_buffer" title="Py_buffer"><span class="n"><span class="pre">Py_buffer</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">view</span></span>, <span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="n"><span class="pre">order</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyBuffer_IsContiguous" title="Link to this definition">¶</a><br /></dt>
  759. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.11.</em><p>Return <code class="docutils literal notranslate"><span class="pre">1</span></code> if the memory defined by the <em>view</em> is C-style (<em>order</em> is
  760. <code class="docutils literal notranslate"><span class="pre">'C'</span></code>) or Fortran-style (<em>order</em> is <code class="docutils literal notranslate"><span class="pre">'F'</span></code>) <a class="reference internal" href="../glossary.html#term-contiguous"><span class="xref std std-term">contiguous</span></a> or either one
  761. (<em>order</em> is <code class="docutils literal notranslate"><span class="pre">'A'</span></code>). Return <code class="docutils literal notranslate"><span class="pre">0</span></code> otherwise. This function always succeeds.</p>
  762. </dd></dl>
  763. <dl class="c function">
  764. <dt class="sig sig-object c" id="c.PyBuffer_GetPointer">
  765. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyBuffer_GetPointer</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><a class="reference internal" href="#c.Py_buffer" title="Py_buffer"><span class="n"><span class="pre">Py_buffer</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">view</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">indices</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyBuffer_GetPointer" title="Link to this definition">¶</a><br /></dt>
  766. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.11.</em><p>Get the memory area pointed to by the <em>indices</em> inside the given <em>view</em>.
  767. <em>indices</em> must point to an array of <code class="docutils literal notranslate"><span class="pre">view-&gt;ndim</span></code> indices.</p>
  768. </dd></dl>
  769. <dl class="c function">
  770. <dt class="sig sig-object c" id="c.PyBuffer_FromContiguous">
  771. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyBuffer_FromContiguous</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><a class="reference internal" href="#c.Py_buffer" title="Py_buffer"><span class="n"><span class="pre">Py_buffer</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">view</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">buf</span></span>, <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="n"><span class="pre">len</span></span>, <span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="n"><span class="pre">fort</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyBuffer_FromContiguous" title="Link to this definition">¶</a><br /></dt>
  772. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.11.</em><p>Copy contiguous <em>len</em> bytes from <em>buf</em> to <em>view</em>.
  773. <em>fort</em> can be <code class="docutils literal notranslate"><span class="pre">'C'</span></code> or <code class="docutils literal notranslate"><span class="pre">'F'</span></code> (for C-style or Fortran-style ordering).
  774. <code class="docutils literal notranslate"><span class="pre">0</span></code> is returned on success, <code class="docutils literal notranslate"><span class="pre">-1</span></code> on error.</p>
  775. </dd></dl>
  776. <dl class="c function">
  777. <dt class="sig sig-object c" id="c.PyBuffer_ToContiguous">
  778. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyBuffer_ToContiguous</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">buf</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><a class="reference internal" href="#c.Py_buffer" title="Py_buffer"><span class="n"><span class="pre">Py_buffer</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">src</span></span>, <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="n"><span class="pre">len</span></span>, <span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="n"><span class="pre">order</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyBuffer_ToContiguous" title="Link to this definition">¶</a><br /></dt>
  779. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.11.</em><p>Copy <em>len</em> bytes from <em>src</em> to its contiguous representation in <em>buf</em>.
  780. <em>order</em> can be <code class="docutils literal notranslate"><span class="pre">'C'</span></code> or <code class="docutils literal notranslate"><span class="pre">'F'</span></code> or <code class="docutils literal notranslate"><span class="pre">'A'</span></code> (for C-style or Fortran-style
  781. ordering or either one). <code class="docutils literal notranslate"><span class="pre">0</span></code> is returned on success, <code class="docutils literal notranslate"><span class="pre">-1</span></code> on error.</p>
  782. <p>This function fails if <em>len</em> != <em>src-&gt;len</em>.</p>
  783. </dd></dl>
  784. <dl class="c function">
  785. <dt class="sig sig-object c" id="c.PyObject_CopyData">
  786. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_CopyData</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">dest</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">src</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_CopyData" title="Link to this definition">¶</a><br /></dt>
  787. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.11.</em><p>Copy data from <em>src</em> to <em>dest</em> buffer. Can convert between C-style and
  788. or Fortran-style buffers.</p>
  789. <p><code class="docutils literal notranslate"><span class="pre">0</span></code> is returned on success, <code class="docutils literal notranslate"><span class="pre">-1</span></code> on error.</p>
  790. </dd></dl>
  791. <dl class="c function">
  792. <dt class="sig sig-object c" id="c.PyBuffer_FillContiguousStrides">
  793. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyBuffer_FillContiguousStrides</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">ndims</span></span>, <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">shape</span></span>, <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">strides</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">itemsize</span></span>, <span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="n"><span class="pre">order</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyBuffer_FillContiguousStrides" title="Link to this definition">¶</a><br /></dt>
  794. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.11.</em><p>Fill the <em>strides</em> array with byte-strides of a <a class="reference internal" href="../glossary.html#term-contiguous"><span class="xref std std-term">contiguous</span></a> (C-style if
  795. <em>order</em> is <code class="docutils literal notranslate"><span class="pre">'C'</span></code> or Fortran-style if <em>order</em> is <code class="docutils literal notranslate"><span class="pre">'F'</span></code>) array of the
  796. given shape with the given number of bytes per element.</p>
  797. </dd></dl>
  798. <dl class="c function">
  799. <dt class="sig sig-object c" id="c.PyBuffer_FillInfo">
  800. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyBuffer_FillInfo</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_buffer" title="Py_buffer"><span class="n"><span class="pre">Py_buffer</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">view</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exporter</span></span>, <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">buf</span></span>, <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="n"><span class="pre">len</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">readonly</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">flags</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyBuffer_FillInfo" title="Link to this definition">¶</a><br /></dt>
  801. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.11.</em><p>Handle buffer requests for an exporter that wants to expose <em>buf</em> of size <em>len</em>
  802. with writability set according to <em>readonly</em>. <em>buf</em> is interpreted as a sequence
  803. of unsigned bytes.</p>
  804. <p>The <em>flags</em> argument indicates the request type. This function always fills in
  805. <em>view</em> as specified by flags, unless <em>buf</em> has been designated as read-only
  806. and <a class="reference internal" href="#c.PyBUF_WRITABLE" title="PyBUF_WRITABLE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyBUF_WRITABLE</span></code></a> is set in <em>flags</em>.</p>
  807. <p>On success, set <code class="docutils literal notranslate"><span class="pre">view-&gt;obj</span></code> to a new reference to <em>exporter</em> and
  808. return 0. Otherwise, raise <a class="reference internal" href="../library/exceptions.html#BufferError" title="BufferError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BufferError</span></code></a>, set
  809. <code class="docutils literal notranslate"><span class="pre">view-&gt;obj</span></code> to <code class="docutils literal notranslate"><span class="pre">NULL</span></code> and return <code class="docutils literal notranslate"><span class="pre">-1</span></code>;</p>
  810. <p>If this function is used as part of a <a class="reference internal" href="typeobj.html#buffer-structs"><span class="std std-ref">getbufferproc</span></a>,
  811. <em>exporter</em> MUST be set to the exporting object and <em>flags</em> must be passed
  812. unmodified. Otherwise, <em>exporter</em> MUST be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  813. </dd></dl>
  814. </section>
  815. </section>
  816. <div class="clearer"></div>
  817. </div>
  818. </div>
  819. </div>
  820. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  821. <div class="sphinxsidebarwrapper">
  822. <div>
  823. <h3><a href="../contents.html">Table of Contents</a></h3>
  824. <ul>
  825. <li><a class="reference internal" href="#">Buffer Protocol</a><ul>
  826. <li><a class="reference internal" href="#buffer-structure">Buffer structure</a></li>
  827. <li><a class="reference internal" href="#buffer-request-types">Buffer request types</a><ul>
  828. <li><a class="reference internal" href="#request-independent-fields">request-independent fields</a></li>
  829. <li><a class="reference internal" href="#readonly-format">readonly, format</a></li>
  830. <li><a class="reference internal" href="#shape-strides-suboffsets">shape, strides, suboffsets</a></li>
  831. <li><a class="reference internal" href="#contiguity-requests">contiguity requests</a></li>
  832. <li><a class="reference internal" href="#compound-requests">compound requests</a></li>
  833. </ul>
  834. </li>
  835. <li><a class="reference internal" href="#complex-arrays">Complex arrays</a><ul>
  836. <li><a class="reference internal" href="#numpy-style-shape-and-strides">NumPy-style: shape and strides</a></li>
  837. <li><a class="reference internal" href="#pil-style-shape-strides-and-suboffsets">PIL-style: shape, strides and suboffsets</a></li>
  838. </ul>
  839. </li>
  840. <li><a class="reference internal" href="#buffer-related-functions">Buffer-related functions</a></li>
  841. </ul>
  842. </li>
  843. </ul>
  844. </div>
  845. <div>
  846. <h4>Previous topic</h4>
  847. <p class="topless"><a href="iter.html"
  848. title="previous chapter">Iterator Protocol</a></p>
  849. </div>
  850. <div>
  851. <h4>Next topic</h4>
  852. <p class="topless"><a href="objbuffer.html"
  853. title="next chapter">Old Buffer Protocol</a></p>
  854. </div>
  855. <div role="note" aria-label="source link">
  856. <h3>This Page</h3>
  857. <ul class="this-page-menu">
  858. <li><a href="../bugs.html">Report a Bug</a></li>
  859. <li>
  860. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/buffer.rst"
  861. rel="nofollow">Show Source
  862. </a>
  863. </li>
  864. </ul>
  865. </div>
  866. </div>
  867. <div id="sidebarbutton" title="Collapse sidebar">
  868. <span>«</span>
  869. </div>
  870. </div>
  871. <div class="clearer"></div>
  872. </div>
  873. <div class="related" role="navigation" aria-label="related navigation">
  874. <h3>Navigation</h3>
  875. <ul>
  876. <li class="right" style="margin-right: 10px">
  877. <a href="../genindex.html" title="General Index"
  878. >index</a></li>
  879. <li class="right" >
  880. <a href="../py-modindex.html" title="Python Module Index"
  881. >modules</a> |</li>
  882. <li class="right" >
  883. <a href="objbuffer.html" title="Old Buffer Protocol"
  884. >next</a> |</li>
  885. <li class="right" >
  886. <a href="iter.html" title="Iterator Protocol"
  887. >previous</a> |</li>
  888. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  889. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  890. <li class="switchers">
  891. <div class="language_switcher_placeholder"></div>
  892. <div class="version_switcher_placeholder"></div>
  893. </li>
  894. <li>
  895. </li>
  896. <li id="cpython-language-and-version">
  897. <a href="../index.html">3.12.3 Documentation</a> &#187;
  898. </li>
  899. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  900. <li class="nav-item nav-item-2"><a href="abstract.html" >Abstract Objects Layer</a> &#187;</li>
  901. <li class="nav-item nav-item-this"><a href="">Buffer Protocol</a></li>
  902. <li class="right">
  903. <div class="inline-search" role="search">
  904. <form class="inline-search" action="../search.html" method="get">
  905. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  906. <input type="submit" value="Go" />
  907. </form>
  908. </div>
  909. |
  910. </li>
  911. <li class="right">
  912. <label class="theme-selector-label">
  913. Theme
  914. <select class="theme-selector" oninput="activateTheme(this.value)">
  915. <option value="auto" selected>Auto</option>
  916. <option value="light">Light</option>
  917. <option value="dark">Dark</option>
  918. </select>
  919. </label> |</li>
  920. </ul>
  921. </div>
  922. <div class="footer">
  923. &copy;
  924. <a href="../copyright.html">
  925. Copyright
  926. </a>
  927. 2001-2024, Python Software Foundation.
  928. <br />
  929. This page is licensed under the Python Software Foundation License Version 2.
  930. <br />
  931. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  932. <br />
  933. See <a href="/license.html">History and License</a> for more information.<br />
  934. <br />
  935. The Python Software Foundation is a non-profit corporation.
  936. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  937. <br />
  938. <br />
  939. Last updated on Apr 09, 2024 (13:47 UTC).
  940. <a href="/bugs.html">Found a bug</a>?
  941. <br />
  942. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  943. </div>
  944. </body>
  945. </html>
上海开阖软件有限公司 沪ICP备12045867号-1