<p dir="auto">Even if the beta_distribution of the parameters shouldn't &lt; 0, function rk_beta in distribution.c should check the arguments</p>
<p dir="auto">I'm currently trying to hunt down a <code class="notranslate">SystemError</code>, and could use some help. Basically, it occurs when attempting certain failing conversions from unicode to string arrays:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np

CHAR = &quot;\x80&quot;  # Anything &gt;= 0x80, so we raise a UnicodeEncodeError.
CHAIN = 1  # How many errors will be chained together.
ITEMSIZE = 3  # dtype itemsize, anything except 0, 1, 2, or 4.

nasty = np.array([CHAR] * (128 * CHAIN + 1), dtype=f&quot;U{ITEMSIZE}&quot;)
nasty.astype(&quot;S&quot;)"><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-v">CHAR</span> <span class="pl-c1">=</span> <span class="pl-s">"<span class="pl-cce">\x80</span>"</span>  <span class="pl-c"># Anything &gt;= 0x80, so we raise a UnicodeEncodeError.</span>
<span class="pl-v">CHAIN</span> <span class="pl-c1">=</span> <span class="pl-c1">1</span>  <span class="pl-c"># How many errors will be chained together.</span>
<span class="pl-v">ITEMSIZE</span> <span class="pl-c1">=</span> <span class="pl-c1">3</span>  <span class="pl-c"># dtype itemsize, anything except 0, 1, 2, or 4.</span>

<span class="pl-s1">nasty</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">array</span>([<span class="pl-v">CHAR</span>] <span class="pl-c1">*</span> (<span class="pl-c1">128</span> <span class="pl-c1">*</span> <span class="pl-v">CHAIN</span> <span class="pl-c1">+</span> <span class="pl-c1">1</span>), <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s">f"U<span class="pl-s1"><span class="pl-kos">{</span><span class="pl-v">ITEMSIZE</span><span class="pl-kos">}</span></span>"</span>)
<span class="pl-s1">nasty</span>.<span class="pl-en">astype</span>(<span class="pl-s">"S"</span>)</pre></div>
<p dir="auto">To recap, the error only happens with:</p>
<ul dir="auto">
<li>A total array size of 129 or more (shape doesn't matter). Every additional 128 elements adds a new chained exception.</li>
<li>A unicode itemsize not equal to 0, 1, 2, or 4.</li>
</ul>
<p dir="auto">On CPython 3.9.0a4 and NumPy 1.18.2, the above gives me:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="UnicodeEncodeError: 'ascii' codec can't encode character '\x80' in position 0: ordinal not in range(128)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  ...
SystemError: &lt;class 'UnicodeEncodeError'&gt; returned a result with an error set"><pre class="notranslate"><code class="notranslate">UnicodeEncodeError: 'ascii' codec can't encode character '\x80' in position 0: ordinal not in range(128)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  ...
SystemError: &lt;class 'UnicodeEncodeError'&gt; returned a result with an error set
</code></pre></div>
<p dir="auto">I've spent quite a while digging around <code class="notranslate">dtype_transfer.c</code>, but I'm afraid that I can't find exactly where this is happening. My hunch is that an error raised during processing of a 128-element block isn't caught until all other blocks are finished as well. If several blocks fail, Python complains that the previous exceptions aren't being handled.</p>
<p dir="auto">If somebody could help me figure out exactly what's going on here (or even provide a good guess), I would definitely appreciate it.</p>