3

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, &current_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_ancestorinclude_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, &current_level, &level, &parent_slug,
        &include_category, &include_ancestor)

include_category(即删除和的布尔要求include_ancestor)会导致PyArg_ParseTupleAndKeywords.

4

1 回答 1

2

你没有query传到PyArg_ParseTupleAndKeywords之后&parent_slug,

调用应该是

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, &current_level, &level, &parent_slug,
        &query, &PyBool_Type, &include_category, 
        &PyBool_Type, &include_ancestor)) {
    return NULL;
}

您的原始调用导致格式和对象列表不匹配,因此引发异常(我认为)。

分解格式和传递的内容

O   &page_slug
O!  &PyList_Type, &keys
O!  &PyDict_Type, &facets
O!  &PyDict_Type, &categories
i   &max_level
i|  &current_level
i   &level
O   &parent_slug
O   ---
O!  &PyBool_Type, &include_category
O!  &PyBool_Type, &include_ancestor
于 2013-07-26T10:25:26.220 回答