python - 捕获溢出错误

标签 python python-3.x overflowexception

在 Python 3 中,我有一个类表示值的范围 [x,y] 并计算该范围的长度。

如果长度太大,我不确定如何捕获类本身内部的 OverflowError 异常。它仅在使用类实例的外部代码中引发...

class ValRange:
    val_min = None
    val_max = None
    range_len = 0

    def __init__(self, val_min, val_max):
        self.val_min = val_min
        self.val_max = val_max

    def __len__(self):
        if (self.val_min is not None and
             self.val_max is not None):
            length = 0
            try:
                length = int(self.val_max) - int(self.val_min) + 1
            except OverflowError as e:
                # Somehow no exception is caught here...
                print('OverflowError...')
                length = 10**5  # arbitrarily large number
            except Exception as e:
                print(e)
            if length > 0:
                self.range_len = length
        return self.range_len


import traceback
import uuid
x = uuid.UUID('00000000-cb9d-4a99-994d-53a499f260b3')
y = uuid.UUID('ea205f99-0564-4aa0-84c3-1b99fcd679fd')
r = ValRange(x, y)
try:
    print(len(r))
except:
    # The exception is caught in this outer code and not in the class itself. Why?
    print(traceback.format_exc())

# The following, which is equivalent to the operations in the 
# code above, will work. 
a = int(y) - int(x) + 1
print(a)

这是执行时发生的情况:

Traceback (most recent call last):
  File "/home/rira/bugs/overflow.py", line 35, in <module>
    print(len(r))
OverflowError: cannot fit 'int' into an index-sized integer

311207443402617699746040548788952897867

最佳答案

这是因为 OverflowError 不会发生在你神奇的 __len__() 方法中 - Python 完全能够处理比这大得多的整数 - 但在 CPython len() 本身实现为 PyObject_Size()它返回 Py_ssize_t ,仅限于 2^31-1 (32 位)或 2^63-1 (64 位),因此当您的 __len__() 结果被强制

您可以在返回结果之前进行预检查,以确保在溢出发生之前捕获它,例如:

def __len__(self):
    if (self.val_min is not None and self.val_max is not None):
        length = int(self.val_max) - int(self.val_min) + 1
        if length > sys.maxsize:
            print('OverflowError...')
            length = 10**5  # arbitrarily large number
        self.range_len = length
    return self.range_len

关于python - 捕获溢出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50367014/

相关文章:

python - 在pandas python中按开始日期和结束日期过滤数据

python - 在 Pandas 中,根据同一 DataFrame 中的值对匹配多个条件的行进行计数,并将计数添加到列中

python - Pandas :用范围函数替换数据框中的列

python-3.x - 了解使用 Gremlin-Python 添加边的不同方法

c# - C# 中字符的按位补码 vs C overflowexception

C# - 什么会导致溢出检查?

python - 在keras中生成多类分类的混淆矩阵

python - 将 Python 列表变量复制到现有 xlsx excel 文件中

python - 如何在 python 中拆分一串数学表达式?

python - Python 异常中错误编号的含义