PyArg_ParseTupleAndKeywords
我在 C Python 扩展中有以下调用:
static char *kwlist [] = {
"page_slug", "keys", "facets", "categories", "max_level",
"current_level", "level", "parent_slug", "query",
"include_category", "include_ancestor", NULL
};
int max_level, current_level, level = 0;
PyObject *page_slug, *keys, *facets, *categories,
*parent_slug = PyString_FromString(""), *query = Py_None,
*include_category = Py_True, *include_ancestor = Py_True;
if (!PyArg_ParseTupleAndKeywords(
args, kwargs, "OO!O!O!ii|iOOO!O!", kwlist,
&page_slug, &PyList_Type, &keys,
&PyDict_Type, &facets, &PyDict_Type, &categories,
&max_level, ¤t_level, &level, &parent_slug,
&PyBool_Type, &include_category,
&PyBool_Type, &include_ancestor)) {
return NULL;
}
这应该会产生如下签名:
func(page_slug, keys, facets, categories, max_level,
current_level, level=0, parent_slug="", query=None,
include_category=True, include_ancestor=True)
但是,当从 Python 中调用该方法时,TypeError
会引发如下情况:
TypeError: argument 10 must be (null), not bool
谁能解释为什么论证 10NULL
在理论上应该接受 a PyObject*
?还有,如何纠正这个问题?
编辑:
在 Python 中,该函数被称为:
func(page_slug, keys, facets, categories, max_level,
current_level, level=level + 1, parent_slug=slug,
query=query, include_category=include_category,
include_ancestor=include_ancestor)
其中page_slug
, parent_slug
, 和query
是 unicode 对象,keys
是一个列表, max_level
, current_level
, 和level
是整数, 和facets
, 和categories
是 dicts。
不包括布尔参数 (include_ancestor
和include_category
) 有效(或者更确切地说,调用PyArg_ParseTupleAndKeywords
有效,当前在 C 函数中发生的分段错误要晚得多),但包括它们中的任何一个都会导致TypeError
引发。
将呼叫更改PyArg_ParseTupleAndKeyword
为:
PyArg_ParseTupleAndKeywords(
args, kwargs, "OO!O!O!ii|iOOOO", kwlist,
&page_slug, &PyList_Type, &keys,
&PyDict_Type, &facets, &PyDict_Type, &categories,
&max_level, ¤t_level, &level, &parent_slug,
&include_category, &include_ancestor)
include_category
(即删除和的布尔要求include_ancestor
)会导致PyArg_ParseTupleAndKeywords
.