gpt4 book ai didi

python - PyArg_ParseTupleAndKeywords 在指定对象的地方需要一个空参数?

转载 作者:行者123 更新时间:2023-11-28 17:46:04 26 4
gpt4 key购买 nike

我在 C Python 扩展中对 PyArg_ParseTupleAndKeywords 进行了以下调用:

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

谁能解释为什么参数 10 在理论上应该接受 PyObject* 时应该为 NULL?还有,如何纠正这个问题?

编辑:

在 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_slugparent_slugquery是unicode对象,keys是列表, max_levelcurrent_levellevel 是整数,facetscategories 是字典。

不包括 bool 参数(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_categoryinclude_ancestor 的 bool 要求)导致 PyArg_ParseTupleAndKeywords 出现段错误。

最佳答案

&parent_slug, 之后,您没有将 query 传递给 PyArg_ParseTupleAndKeywords

调用应该是

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

关于python - PyArg_ParseTupleAndKeywords 在指定对象的地方需要一个空参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17877911/

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