借用和窃取引用的Python C-API函数 [英] Python C-API functions that borrow and steal references
问题描述
Python C-API的标准约定是
The standard convention in the Python C-API is that
-
函数不会从输入参数(即对象)中窃取引用
functions do not steal references from input arguments (that are objects)
返回值和输出参数(即对象)拥有引用
return values and output arguments (that are objects) own a reference
Python C-API中的大多数函数都遵循此约定.但是,也有一些例外.我遇到了以下问题:
Most functions in the Python C-API follow this convention. However, there are some exceptions. I have come across the following:
从输入参数中窃取引用的函数
PyModule_AddObject
具有返回值或借用引用的输出参数的函数
PyErr_Occurred
PyTuple_GetItem
PyTuple_GETITEM
PyDict_GetItem
PyDict_GetItemString
PyDict_Next
在任何地方都有此类功能的完整列表吗?这样的列表在编写Python扩展模块时将是一个有用的参考.
Is there a comprehensive list of such functions anywhere? Such a list would be a useful reference when writing Python extension modules.
推荐答案
在Python 2.7.2 C-API文档中的文本搜索偷"和借"给出了以下列表:
A text search in the Python 2.7.2 C-API docs for the words "steal" and "borrow" gave the following lists:
从输入参数中窃取引用的函数
PyCell_SET (but not PyCell_Set)
PyList_SetItem, PyList_SET_ITEM
PyModule_AddObject
PyTuple_SetItem, PyTuple_SET_ITEM
具有返回值或借用引用的输出参数的函数
all PyArg_Xxx functions
PyCell_GET (but not PyCell_Get)
PyDict_GetItem
PyDict_GetItemString
PyDict_Next
PyErr_Occurred
PyEval_GetBuiltins
PyEval_GetFrame
PyEval_GetGlobals
PyEval_GetLocals
PyFile_Name
PyFunction_GetClosure
PyFunction_GetCode
PyFunction_GetDefaults
PyFunction_GetGlobals
PyFunction_GetModule
PyImport_AddModule
PyImport_GetModuleDict
PyList_GetItem, PyList_GETITEM
PyMethod_Class, PyMethod_GET_CLASS
PyMethod_Function, PyMethod_GET_FUNCTION
PyMethod_Self, PyMethod_GET_SELF
PyModule_GetDict
PyObject_Init
PyObject_InitVar
PySequence_Fast_GET_ITEM
PySys_GetObject
PyThreadState_GetDict
PyTuple_GetItem, PyTuple_GET_ITEM
PyWeakref_GetObject, PyWeakref_GET_OBJECT
Py_InitModule
Py_InitModule3
Py_InitModule4
这篇关于借用和窃取引用的Python C-API函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!