<p dir="auto">Related to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="294607323" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/10528" data-hovercard-type="issue" data-hovercard-url="/numpy/numpy/issues/10528/hovercard" href="https://github.com/numpy/numpy/issues/10528">#10528</a>, but for <code class="notranslate">np.dtype</code> instead of <code class="notranslate">np.array</code></p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import ctypes
class PackedStructure(ctypes.Structure):
    _pack_ = 1
    _fields_ = [
        ('a', ctypes.c_uint8),
        ('b', ctypes.c_uint16)
    ]
actual = np.dtype(PackedStructure)
expected = np.dtype([
    ('a', np.uint8),
    ('b', np.uint16)
])"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">ctypes</span>
<span class="pl-k">class</span> <span class="pl-v">PackedStructure</span>(<span class="pl-s1">ctypes</span>.<span class="pl-v">Structure</span>):
    <span class="pl-s1">_pack_</span> <span class="pl-c1">=</span> <span class="pl-c1">1</span>
    <span class="pl-s1">_fields_</span> <span class="pl-c1">=</span> [
        (<span class="pl-s">'a'</span>, <span class="pl-s1">ctypes</span>.<span class="pl-s1">c_uint8</span>),
        (<span class="pl-s">'b'</span>, <span class="pl-s1">ctypes</span>.<span class="pl-s1">c_uint16</span>)
    ]
<span class="pl-s1">actual</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">dtype</span>(<span class="pl-v">PackedStructure</span>)
<span class="pl-s1">expected</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">dtype</span>([
    (<span class="pl-s">'a'</span>, <span class="pl-s1">np</span>.<span class="pl-s1">uint8</span>),
    (<span class="pl-s">'b'</span>, <span class="pl-s1">np</span>.<span class="pl-s1">uint16</span>)
])</pre></div>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class Foo(ctypes.Structure):
    _fields_ = [('one', ctypes.c_uint8), ('two', ctypes.c_uint32)]
    _pack_ = 2"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-v">Foo</span>(<span class="pl-s1">ctypes</span>.<span class="pl-v">Structure</span>):
    <span class="pl-s1">_fields_</span> <span class="pl-c1">=</span> [(<span class="pl-s">'one'</span>, <span class="pl-s1">ctypes</span>.<span class="pl-s1">c_uint8</span>), (<span class="pl-s">'two'</span>, <span class="pl-s1">ctypes</span>.<span class="pl-s1">c_uint32</span>)]
    <span class="pl-s1">_pack_</span> <span class="pl-c1">=</span> <span class="pl-c1">2</span></pre></div>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; ctypes.sizeof(Foo())  # packed to alignment 2
6
&gt;&gt;&gt; np.dtype(Foo).itemsize  # default alignment incorrectly used
8"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">ctypes</span>.<span class="pl-en">sizeof</span>(<span class="pl-v">Foo</span>())  <span class="pl-c"># packed to alignment 2</span>
<span class="pl-c1">6</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">np</span>.<span class="pl-en">dtype</span>(<span class="pl-v">Foo</span>).<span class="pl-s1">itemsize</span>  <span class="pl-c"># default alignment incorrectly used</span>
<span class="pl-c1">8</span></pre></div>
<p dir="auto">Fix should be fairly straightforward by changing:</p>
<p dir="auto"></p><div class="Box Box--condensed my-2">
  <div class="Box-header f6">
    <p class="mb-0 text-bold">
      <a href="https://github.com/numpy/numpy/blob/44810f07ec6a281d96d6a69c105ce6ae59aec718/numpy/core/_dtype_ctypes.py#L35-L46">numpy/numpy/core/_dtype_ctypes.py</a>
    </p>
    <p class="mb-0 color-fg-muted">
        Lines 35 to 46
      in
      <a data-pjax="true" class="commit-tease-sha" href="/numpy/numpy/commit/44810f07ec6a281d96d6a69c105ce6ae59aec718">44810f0</a>
    </p>
  </div>
  <div itemprop="text" class="Box-body p-0 blob-wrapper blob-wrapper-embedded data">
    <table class="highlight tab-size mb-0 js-file-line-container" data-tab-size="8" data-paste-markdown-skip="">

        <tbody><tr class="border-0">
          <td id="L35" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="35"></td>
          <td id="LC35" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line"> <span class="pl-k">def</span> <span class="pl-en">_from_ctypes_structure</span>(<span class="pl-s1">t</span>): </td>
        </tr>

        <tr class="border-0">
          <td id="L36" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="36"></td>
          <td id="LC36" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     <span class="pl-c"># TODO: gh-10533, gh-10532</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L37" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="37"></td>
          <td id="LC37" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     <span class="pl-s1">fields</span> <span class="pl-c1">=</span> [] </td>
        </tr>

        <tr class="border-0">
          <td id="L38" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="38"></td>
          <td id="LC38" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     <span class="pl-k">for</span> <span class="pl-s1">item</span> <span class="pl-c1">in</span> <span class="pl-s1">t</span>.<span class="pl-s1">_fields_</span>: </td>
        </tr>

        <tr class="border-0">
          <td id="L39" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="39"></td>
          <td id="LC39" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">         <span class="pl-k">if</span> <span class="pl-en">len</span>(<span class="pl-s1">item</span>) <span class="pl-c1">&gt;</span> <span class="pl-c1">2</span>: </td>
        </tr>

        <tr class="border-0">
          <td id="L40" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="40"></td>
          <td id="LC40" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">             <span class="pl-k">raise</span> <span class="pl-v">TypeError</span>( </td>
        </tr>

        <tr class="border-0">
          <td id="L41" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="41"></td>
          <td id="LC41" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">                 <span class="pl-s">"ctypes bitfields have no dtype equivalent"</span>) </td>
        </tr>

        <tr class="border-0">
          <td id="L42" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="42"></td>
          <td id="LC42" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">         <span class="pl-s1">fname</span>, <span class="pl-s1">ftyp</span> <span class="pl-c1">=</span> <span class="pl-s1">item</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L43" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="43"></td>
          <td id="LC43" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">         <span class="pl-s1">fields</span>.<span class="pl-en">append</span>((<span class="pl-s1">fname</span>, <span class="pl-en">dtype_from_ctypes_type</span>(<span class="pl-s1">ftyp</span>))) </td>
        </tr>

        <tr class="border-0">
          <td id="L44" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="44"></td>
          <td id="LC44" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">  </td>
        </tr>

        <tr class="border-0">
          <td id="L45" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="45"></td>
          <td id="LC45" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     <span class="pl-c"># by default, ctypes structs are aligned</span> </td>
        </tr>

        <tr class="border-0">
          <td id="L46" class="blob-num border-0 px-3 py-0 color-bg-default" data-line-number="46"></td>
          <td id="LC46" class="blob-code border-0 px-3 py-0 color-bg-default blob-code-inner js-file-line">     <span class="pl-k">return</span> <span class="pl-s1">np</span>.<span class="pl-en">dtype</span>(<span class="pl-s1">fields</span>, <span class="pl-s1">align</span><span class="pl-c1">=</span><span class="pl-c1">True</span>) </td>
        </tr>
    </tbody></table>
  </div>
</div>
<p></p>