Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
BUG: core: Don't call INCREF/DECREF on descr in NpyStringAcquireAlloc…
…ator. (#31456)
  • Loading branch information
WarrenWeckesser authored and charris committed May 18, 2026
commit e0e38767d5d0f848ab44befeedcad71e8ef589c7
9 changes: 2 additions & 7 deletions numpy/_core/src/common/raii_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,9 @@ class SaveThreadState
//
// Instead of
//
// Py_INCREF(descr);
// npy_string_allocator *allocator = NpyString_acquire_allocator(descr);
// [code that uses allocator]
// NpyString_release_allocator(allocator);
// Py_DECREF(descr);
//
// use
//
Expand All @@ -141,19 +139,16 @@ class SaveThreadState
//
class NpyStringAcquireAllocator
{
PyArray_StringDTypeObject *_descr;
npy_string_allocator *_allocator;

public:

NpyStringAcquireAllocator(PyArray_StringDTypeObject *descr) : _descr(descr) {
Py_INCREF(_descr);
_allocator = NpyString_acquire_allocator(_descr);
NpyStringAcquireAllocator(PyArray_StringDTypeObject *descr) {
_allocator = NpyString_acquire_allocator(descr);
}

~NpyStringAcquireAllocator() {
NpyString_release_allocator(_allocator);
Py_DECREF(_descr);
}

NpyStringAcquireAllocator(const NpyStringAcquireAllocator&) = delete;
Expand Down
1 change: 1 addition & 0 deletions numpy/_core/tests/test_stringdtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ def test_self_casts(dtype, dtype2, strings):
["this", "is", "an", "array"],
["€", "", "😊"],
["A¢☃€ 😊", " A☃€¢😊", "☃€😊 A¢", "😊☃A¢ €"],
["short", "12345678"] * 1000,
],
)
class TestStringLikeCasts:
Expand Down
Loading