Python C接口PyArg_语法元组失败

Python C接口PyArg_语法元组失败,python,c,Python,C,我有一个用C编写的Python模块,其中公开了许多函数。其中之一的Python定义为: def SetPowerSupply(voltage, current, supply): 其中电压=浮动,电流=浮动,电源=整数。在C侧,我有: float voltage, current; int supply; if (!PyArg_ParseTuple(args, "ffi", &voltage, &current, &supply)) { // Failed t

我有一个用C编写的Python模块,其中公开了许多函数。其中之一的Python定义为:

def SetPowerSupply(voltage, current, supply):
其中电压=浮动,电流=浮动,电源=整数。在C侧,我有:

float voltage, current;
int supply;

if (!PyArg_ParseTuple(args, "ffi", &voltage, &current, &supply))
{
    // Failed to parse
    // ...
}
我的一个脚本编写者有一个脚本,其中该函数无法解析参数,并抱怨需要一个整数。据我所知,实际上正在传入一个整数,因为如果在错误分支中执行以下操作:

PyObject *num = PyNumber_Float(PyTuple_GetItem(args, 0));
voltage = PyFloat_AsDouble(num);
Py_XDECREF(num);

num = PyNumber_Float(PyTuple_GetItem(args, 1));
current = PyFloat_AsDouble(num);
Py_XDECREF(num);

num = PyNumber_Int(PyTuple_GetItem(args, 2));
supply = PyLong_AsLong(num);
Py_XDECREF(num);
。。。然后一切按预期进行。通过这个模块运行的其他脚本没有表现出这种行为,我看不出有什么不同。它们都调用相同的函数:

SetPowerSupply(37.5, 0.5, 1)
SetPowerSupply(0, 0, 1)
在令人反感的脚本中,我可以这样做:

有什么想法吗

多谢各位


编辑:

该问题是由另一个函数引起的,该函数在此函数之前被多次调用。它是:

if(!PyArg_ParseTuple(args, "s|siss", &board, &component, &pin, &colorStr, &msg))
{
    // Parsing the pin as an int failed, try as a string
    if(!PyArg_ParseTuple(args, "s|ssss", &board, &component, &sPin, &colorStr, &msg))
    {
        // ...
这样做的目的基本上是重载第三个参数以接受字符串或数值。当有人向它输入字符串时,失败解析中的Python错误从未被清除。解决此问题的更新代码如下

if(!PyArg_ParseTuple(args, "s|siss", &board, &component, &pin, &colorStr, &msg))
{
    PyErr_Clear();

    // Parsing the pin as an int failed, try as a string
    if(!PyArg_ParseTuple(args, "s|ssss", &board, &component, &sPin, &colorStr, &msg))
    {
        // ...

非常感谢Ignacio提供的线索。

您的另一个函数在适当的时候无法返回
None
,您无意中捕获了此错误消息。

我根据您的提示发现了问题。这与
none
无关,而是与不清除前一个函数中的错误有关。我将用答案更新原始问题非常感谢。