NpyString API#

在 2.0 版本加入.

这个 API 允许访问存储在 NumPy StringDType 数组中的 UTF-8 字符串数据.有关 StringDType 设计的更深入的详细信息,请参阅 NEP-55 .

示例#

加载字符串#

假设我们正在为 StringDType 编写一个 ufunc 实现.如果我们得到一个 const char buf 指针,指向 StringDType 数组条目的开头,以及一个指向数组描述符的 PyArray_Descr * 指针,则可以像这样访问底层字符串数据:

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);

打包字符串#

此示例演示了如何将新的字符串条目打包到数组中:

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);

类型#

type npy_packed_static_string#

一个不透明的结构体,表示“packed”编码的字符串.数组缓冲区中的单个条目是此结构体的实例.直接访问结构体中的数据是未定义的,并且库的未来版本可能会更改字符串的打包表示形式.

type npy_static_string#

一个 unpack 过的字符串,允许访问 UTF-8 字符串数据.

typedef struct npy_unpacked_static_string {
    size_t size;
    const char *buf;
} npy_static_string;
size_t size#

字符串的大小,以字节为单位.

const char *buf#

字符串缓冲区.保存 UTF-8 编码的字节.目前不以空字符串结尾,但我们可能会决定在将来添加空终止,因此不要依赖于空终止的存在与否.

请注意,这是一个 const 缓冲区. 如果你想改变数组中的条目,你应该创建一个新的字符串并将其打包到数组条目中.

type npy_string_allocator#

指向处理字符串分配的对象的 opaque 指针.在使用分配器之前,必须获取分配器锁,并在完成与分配器管理的字符串的交互后释放锁.

type PyArray_StringDTypeObject#

Python 中支持 StringDType 实例的 C 结构.属性存储创建对象时使用的设置,一个 npy_string_allocator 实例,用于管理与 DType 实例关联的数组的字符串分配,以及几个缓存有关缺失字符串对象信息的属性,这些信息通常在强制转换和 ufunc 循环实现中需要.

typedef struct {
    PyArray_Descr base;
    PyObject *na_object;
    char coerce;
    char has_nan_na;
    char has_string_na;
    char array_owned;
    npy_static_string default_string;
    npy_static_string na_name;
    npy_string_allocator *allocator;
} PyArray_StringDTypeObject;
PyArray_Descr base#

基础对象. 使用此成员访问所有描述符对象的通用字段.

PyObject *na_object#

对表示空值的对象的引用. 如果没有空值(默认值),则这将为 NULL.

char coerce#

如果启用了字符串强制,则为 1,否则为 0.

char has_nan_na#

如果缺失的字符串对象(如果有)类似于 NaN,则为 1,否则为 0.

char has_string_na#

如果缺失的字符串对象(如果有)是一个字符串,则为 1,否则为 0.

char array_owned#

如果 array 拥有 StringDType 实例,则为 1,否则为 0.

npy_static_string default_string#

在操作中使用的默认字符串.如果缺少的字符串对象是一个字符串,这将包含缺少的字符串的字符串数据.

npy_static_string na_name#

缺失的字符串对象(如果有)的名称.否则为空字符串.

npy_string_allocator allocator#

与拥有此描述符实例的数组相关联的分配器实例. 只有在获取 allocator_lock 之后才能直接访问分配器,并且在不再需要分配器后应立即释放锁

函数#

npy_string_allocator *NpyString_acquire_allocator(const PyArray_StringDTypeObject *descr)#

获取锁定附加到 descr 的分配器的互斥锁.必须对该函数返回的分配器调用一次 NpyString_release_allocator .请注意,在持有分配器互斥锁时,不应调用需要GIL的函数,因为这样做可能会导致死锁.

void NpyString_acquire_allocators(size_t n_descriptors, PyArray_Descr *const descrs[], npy_string_allocator *allocators[])#

同时获取锁定附加到多个描述符的分配器的互斥锁.在allocators数组中为数组中的每个StringDType描述符写入指向关联的分配器的指针.如果任何描述符不是StringDType实例,则在该条目的allocators数组中写入NULL.

n_descriptors 是descrs数组中应该检查的描述符的数量. n_descriptors 元素之后的任何描述符都将被忽略.如果 descrs 数组不包含n_descriptors元素,则会发生缓冲区溢出.

如果多次传递指向同一描述符的指针,则仅获取一次分配器互斥锁,但会适当地设置相同的分配器指针.在此函数返回后必须释放分配器互斥锁,请参阅 NpyString_release_allocators .

请注意,在持有分配器互斥锁时,不应调用需要GIL的函数,因为这样做可能会导致死锁.

void NpyString_release_allocator(npy_string_allocator *allocator)#

释放锁定分配器的互斥锁.在获取分配器互斥锁并且完成所有需要分配器的操作后,必须正好被调用一次.

如果您需要释放多个分配器,请参阅NpyString_release_allocators,当给定对同一分配器的多个引用时,它可以正确处理一次释放分配器.

void NpyString_release_allocators(size_t length, npy_string_allocator *allocators[])#

释放锁定N个分配器的互斥锁. length 是allocators数组的长度.NULL条目将被忽略.

如果多次传递指向同一分配器的指针,则仅释放一次分配器互斥锁.

int NpyString_load(npy_string_allocator *allocator, const npy_packed_static_string *packed_string, npy_static_string *unpacked_string)#

packed_string 的压缩内容提取到 unpacked_string 中.

unpacked_stringpacked_string 数据的只读视图,不应用于修改字符串数据.如果 packed_string 是空字符串,则将 unpacked_string.buf 设置为NULL指针.如果解包字符串失败,则返回-1;如果 packed_string 是空字符串,则返回1;否则返回0.

一个有用的模式是定义一个栈分配的npy_static_string实例,初始化为 {0, NULL} ,并将指向该栈分配的解包字符串的指针传递给此函数.此函数可用于同时解包字符串并确定它是否为空字符串.

int NpyString_pack_null(npy_string_allocator *allocator, npy_packed_static_string *packed_string)#

将空字符串打包到 packed_string 中.成功返回0,失败返回-1.

int NpyString_pack(npy_string_allocator *allocator, npy_packed_static_string *packed_string, const char *buf, size_t size)#

buf 指向的缓冲区的前 size 个条目复制并打包到 packed_string 中.成功返回0,失败返回-1.