English 中文(简体)
灰色延伸——为什么是使用关键词向和平功能委员会提出的论点的方法
原标题:Python C Extension - Why are methods that use keyword arguments cast to PyCFunction

我正在学习一下Sharma-C的延伸,并对为何必须用关键词来对待PyC的功能感到迷惑。

我对《常设机构》的理解是,它需要两点人来看待PyObjects,并将单一点人归为PyObject,例如。

 PyObject* myFunc(PyObject* self, PyObject* args)

如果我会利用一个使用关键词句的功能,那么这一功能将有三个点人去标语,并将一位点人带回一个PyObject——例如。

 PyObject* myFunc(PyObject* self, PyObject* args, PyObject* keywordArgs)

然而,当我设立单元功能阵列时(关于称为“添加器”的职能):

{ "adder", (PyCFunction)adder, METH_VARARGS | METH_KEYWORDS, "adder method" }

罚款。 它感到,像我这样一片浮舟,现在仍然不得不使用浮体中的非排他性部分。 如果我看不到这项工作的话,我会认为它不会奏效。 我在这里不理解什么?

此外,我还看到有人提到“PyCFunctionWithKeywords”,这似乎有我认为需要的职能签字,但我的汇编者抱怨说(发出警告)存在不一致之处。

Was PyCFunctionWithKeywords deprecated? If not, is there a time when I should/must use it?

最佳回答

If your function handles keyword arguments, then it must correspond to a PyCFunctionWithKeywords. However, C doesn’t do overloading, and the structure built by PyMethodDef is defined to expect a PyCFunction, rather than, say, a completely unchecked void *. So you must cast your PyCFunctionWithKeywords to a PyCFunction to stop the compiler complaining, that’s all.

认为你还必须通过悬挂国旗的ESTH_KEYWORDS,以示你的职责是签署一份PyCFunctionWithKeywords,而不是一个PyC函数。

问题回答

D Olveiro is 100% correct, but (depending on your compiler) you may still get a "cast-function-type" warning, which you can safely ignore. Or (if using a gcc varient) surround with pragma to temporarily turn off that warning:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
static PyMethodDef MyPythonMethods[] = {
    {"myMethod", (PyCFunction)MyMethodFunction, METH_VARARGS, "doc string"},
    ...
    {NULL, NULL, 0, NULL}
};
#pragma GCC diagnostic pop




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签