bundles / numpy 2.4.4 / docs
Doc
NpyString API
docs/reference:c-api:strings
This API allows access to the UTF-8 string data stored in NumPy StringDType arrays. See NEP-55 <NEP55> for more in-depth details into the design of StringDType.
Examples
Loading a String
Say we are writing a ufunc implementation for StringDType. If we are given const char *buf pointer to the beginning of a StringDType array entry, and a PyArray_Descr * pointer to the array descriptor, one can access the underlying string data like so:
npy_string_allocator *allocator = NpyString_acquire_allocator( (PyArray_StringDTypeObject *)descr); npy_static_string sdata = {0, NULL}; npy_packed_static_string *packed_string = (npy_packed_static_string *)buf; int is_null = 0; is_null = NpyString_load(allocator, packed_string, &sdata); if (is_null == -1) { // failed to load string, set error return -1; } else if (is_null) { // handle missing string // sdata->buf is NULL // sdata->size is 0 } else { // sdata->buf is a pointer to the beginning of a string // sdata->size is the size of the string } NpyString_release_allocator(allocator);
Packing a String
This example shows how to pack a new string entry into an array:
char *str = "Hello world"; size_t size = 11; npy_packed_static_string *packed_string = (npy_packed_static_string *)buf; npy_string_allocator *allocator = NpyString_acquire_allocator( (PyArray_StringDTypeObject *)descr); // copy contents of str into packed_string if (NpyString_pack(allocator, packed_string, str, size) == -1) { // string packing failed, set error return -1; } // packed_string contains a copy of "Hello world" NpyString_release_allocator(allocator);
Types
Functions
Acquire the mutex locking the allocator attached to
descr.NpyString_release_allocatormust be called on the allocator returned by this function exactly once. Note that functions requiring the GIL should not be called while the allocator mutex is held, as doing so may cause deadlocks.
Simultaneously acquire the mutexes locking the allocators attached to multiple descriptors. Writes a pointer to the associated allocator in the allocators array for each StringDType descriptor in the array. If any of the descriptors are not StringDType instances, write NULL to the allocators array for that entry.
n_descriptorsis the number of descriptors in the descrs array that should be examined. Any descriptor aftern_descriptorselements is ignored. A buffer overflow will happen if thedescrsarray does not contain n_descriptors elements.If pointers to the same descriptor are passed multiple times, only acquires the allocator mutex once but sets identical allocator pointers appropriately. The allocator mutexes must be released after this function returns, see
NpyString_release_allocators.Note that functions requiring the GIL should not be called while the allocator mutex is held, as doing so may cause deadlocks.
Release the mutex locking an allocator. This must be called exactly once after acquiring the allocator mutex and all operations requiring the allocator are done.
If you need to release multiple allocators, see NpyString_release_allocators, which can correctly handle releasing the allocator once when given several references to the same allocator.
Release the mutexes locking N allocators.
lengthis the length of the allocators array. NULL entries are ignored.If pointers to the same allocator are passed multiple times, only releases the allocator mutex once.
Extract the packed contents of
packed_stringintounpacked_string.The
unpacked_stringis a read-only view onto thepacked_stringdata and should not be used to modify the string data. Ifpacked_stringis the null string, setsunpacked_string.bufto the NULL pointer. Returns -1 if unpacking the string fails, returns 1 ifpacked_stringis the null string, and returns 0 otherwise.A useful pattern is to define a stack-allocated npy_static_string instance initialized to
{0, NULL}and pass a pointer to the stack-allocated unpacked string to this function. This function can be used to simultaneously unpack a string and determine if it is a null string.
Pack the null string into
packed_string. Returns 0 on success and -1 on failure.
Copy and pack the first
sizeentries of the buffer pointed to bybufinto thepacked_string. Returns 0 on success and -1 on failure.