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
NULL
理論的には、引数 10 が受け入れられるべきであると予想される理由を誰か説明でき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
は int、 、facets
、categories
は dict です。
ブール値の引数 (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
ます。