- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 Python C-API 的新手,正在浏览一些源代码以获取其中的部分内容。
这是我在包含扩展模块的包的 C 源代码中找到的函数的最小版本:
#define PY_SSIZE_T_CLEAN
#include <Python.h>
static PyObject *
modulename_myfunc(PyObject *self, PyObject *args) {
// Call PyArg_ParseTuple, etc ...
// Dummy values; in the real function they are calculated
int is_found = 1;
Py_ssize_t n_bytes_found = 1024;
PyObject *result;
result = Py_BuildValue("(Oi)",
is_found ? Py_True : Py_False, // Py_INCREF?
n_bytes_found);
return result;
}
这是否会因为未能在 Py_True
或 Py_False
上使用 Py_INCREF
而引入小的内存泄漏? C-API docs for Boolean object似乎非常明确地总是需要增加/减少 Py_True
和 Py_False
。
如果确实需要引入 Py_INCREF
,如何在此处最恰本地使用它,假设 Py_RETURN_TRUE
/Py_RETURN_FALSE
不是真的适用,因为正在返回一个元组吗?
最佳答案
此处未使用 Py_INCREF
的原因是 Py_BuildValue
,当传递一个带有“O”的对象时,将为您增加引用计数:
O (object) [PyObject *]
Pass a Python object untouched (except for its reference count, which is incremented by one). If the object passed in is a NULL pointer, it is assumed that this was caused because the call producing the argument found an error and set an exception. Therefore, Py_BuildValue() will return NULL but won’t raise an exception. If no exception has been raised yet, SystemError is set.
您将在 CPython 中看到类似的用法 itself例如。
关于python - 递增 Py_True/Py_False refcount 总是必要的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58263736/
我正在使用 this solution在二进制矩阵中找到与图像边界对齐的矩形。假设现在我想找到一个不与图像边框对齐的矩形,并且我不知道它的方向;找到它的最快方法是什么? 为了示例,让我们寻找一个仅包含
else: 行在这个 Python 程序中是否正确/必要? from random import randrange for n in range(10): r = randrange(0,1
在 TDPL 7.1.5.1 中讨论了将 Widget w2 分配给 w1 并且作者指出“将 w2 逐个字段分配给 w1 会将 w2.array 分配给 w1.array——一个简单的数组边界分配,而
我是一名优秀的程序员,十分优秀!