Skip to content

BUG: centralized helper for output coerce and na_object in stringdtype operations (#31884) - #31952

Merged
charris merged 1 commit into
numpy:maintenance/2.5.xfrom
charris:backport-31884
Jul 10, 2026
Merged

BUG: centralized helper for output coerce and na_object in stringdtype operations (#31884)#31952
charris merged 1 commit into
numpy:maintenance/2.5.xfrom
charris:backport-31884

Conversation

@charris

@charris charris commented Jul 10, 2026

Copy link
Copy Markdown
Member

Backport of #31884.

PR summary

Related to #31825 but issuing it as a separate PR because the two changes don't overlap at all.

We currently have custom logic for determining the correct na_object and coerce value when operations mix StringDType instances. In some cases this can even lead to crashes. The following script segfaults for me because the na_object in the fill character array is not being handled correctly.

Python 3.14.5 (main, May 22 2026, 08:10:06) [Clang 21.0.0 (clang-2100.0.123.102)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> from numpy.dtypes import StringDType
>>> print(np.__version__)
2.5.1
>>> arr = np.array(["a"], dtype="T")
>>> fill = np.array("*", dtype=StringDType(na_object="*"))
>>> print(np.strings.center(arr, 5, fill))
[1]    87011 segmentation fault  python

This centralizes the logic for handling the na_object and coerce argument in ufunc loops and in the comparison machinery into one set of shared helpers.

Also adds tests for this behavior, including the case that leads to a crash above.

First time committer introduction

AI Disclosure

I found this issue using an AI model. I also used an AI model to help with the tests and the approach for the fix.

@charris charris added this to the 2.5.2 release milestone Jul 10, 2026
@charris charris added 00 - Bug 08 - Backport Used to tag backport PRs component: numpy.strings String dtypes and functions labels Jul 10, 2026
@charris
charris merged commit bb246e0 into numpy:maintenance/2.5.x Jul 10, 2026
85 checks passed
@charris
charris deleted the backport-31884 branch July 10, 2026 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug 08 - Backport Used to tag backport PRs component: numpy.strings String dtypes and functions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants