gpt4 book ai didi

python - 在 Py_BuildValue ("y#"之后是否需要 PyBuffer_Release,...)?

转载 作者:太空宇宙 更新时间:2023-11-04 02:22:51 25 4
gpt4 key购买 nike

如果它有所作为,我对有关 Python 3 的答案很感兴趣。

文档声明(herehere)PyBuffer_Release() 应该在 PyArg_Parse*() 之后调用 s*, y*.

没有关于 Py_BuildValue() 的文章。这是一个疏忽,还是在 Py_BuildValue() 的情况下,简单的 Py_DECREF() 就足够了?

这是我的具体案例:

uint8_t buf = (uint8_t *)malloc(bufSize);
PyObject *pyBuf = Py_BuildValue("y#", (char *)buf, bufSize);
free(buf);

// do something with pyBuf

// maybe a PyBuffer_Release(get_underlying_buffer(pyBuf)) here?
Py_DECREF(pyBuf);

最佳答案

我认为不是:

  1. PyArg_Parse*Py_BuildValue 函数中,y# 指的是字符串和长度,而不是缓冲区,因此没有要释放的底层缓冲区对象。

  2. Py_BuildValue 的文档说:

    When memory buffers are passed as parameters to supply data to build objects, as for the s and s# formats, the required data is copied. Buffers provided by the caller are never referenced by the objects created by Py_BuildValue().

    锁定 PyArg_Parse* 使用的缓冲区的目的是,您已经引用 Python 的一些数据到 C,并且您想在 C 中处理它,而不希望它被 Python 修改。在这种情况下,您已将一些数据从 C 语言复制到 Python 中,因此无需保护原始数据免遭修改。

关于python - 在 Py_BuildValue ("y#"之后是否需要 PyBuffer_Release,...)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51172546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com