Skip to main content
 首页 » 编程设计

python之在 Python C API 中检查对象是 int 还是类 int

2025年01月19日11shihaiming

有没有类似于PyInt_Check的操作/PyLong_Check考虑到类型是否有 __int__ 方法?

到目前为止我能找到的最接近的解决方法是

int check_int(PyObject *obj) 
{ 
    long lng; 
    int over; 
 
    lng = PyLong_AsLongAndOverflow(obj, &over); 
    if(lng == -1 && over == 0 && PyErr_Occurred()) { 
        PyErr_Clear(); 
#if PY_MAJOR_VERSION <= 2 
        lng = PyInt_AsLong(obj); 
        if(lng == -1L && PyErr_Occurred()) { 
            PyErr_Clear(); 
            return 0; 
        } 
#else 
        return 0; 
#endif 
    } 
    return 1; 
} 

这里的问题是我正在有效地做类似的事情

def isint(obj): 
    try: 
        int(obj) 
    except TypeError: 
        return False 
    else: 
        return True 

但是,这是 C,我更愿意做类似的事情

def isint(obj): 
    return isinstance(obj, int) or hasattr(type(obj), '__int__') 

我希望这样的检查已经存在,因为 PyInt_AsLong和 PyLong_AsLongAndOverflow已经执行了。我只想知道一个对象是否可能是一个整数,而根本不获取整数值。

话虽如此,我明白了实际获取值的意义,因为 hasattr(type(obj), '__int__') 实际上并不能保证该对象可以合理地用作整数:例如,如果属性不是函数或只是引发错误。在这种情况下,“否”可能是一个有效的答案。

请您参考如下方法:

最接近函数的是PyNumber_Long , 或 PyNumber_Int在 Python 2 上。这两个函数实际上执行转换。他们还会考虑像 __trunc__ 这样的方法,并将字符串转换为整数,就像从 Python 级代码调用 int 一样。

如果要检查是否存在__int__转换方法,可以直接查找对应的slot:

if (o->ob_type->tp_as_number and o->ob_type->tp_as_number->nb_int) { 
    do_whatever(); 
}