Bac*_*ach 8 python errno overflowexception
OverflowError在经过一些愚蠢的计算之后捕获Python ,我检查了错误args并看到它是一个包含整数作为其第一个坐标的元组.我假设这是某种错误号(errno).但是,我找不到任何文档或参考资料.
例:
try:
1e4**100
except OverflowError as ofe:
print ofe.args
## prints '(34, 'Numerical result out of range')'
Run Code Online (Sandbox Code Playgroud)
你知道34在这种情况下意味着什么吗?你知道这个例外的其他可能的错误号吗?
标准库中有一个名为的模块errno:
该模块提供标准的errno系统符号.每个符号的值是相应的整数值.名称和描述来自linux/include/errno.h,它应该是非常全面的.
/usr/include/linux/errno.h包括/usr/include/asm/errno.h那包括/usr/include/asm-generic/errno-base.h.
me@my_pc:~$ cat /usr/include/asm-generic/errno-base.h | grep 34
#define ERANGE 34 /* Math result not representable */
Run Code Online (Sandbox Code Playgroud)
现在我们知道34错误代码代表ERANGE.
1e4**100与处理float_pow功能从对象/ floatobject.c.该函数的部分源代码:
static PyObject *
float_pow(PyObject *v, PyObject *w, PyObject *z)
{
// 107 lines omitted
if (errno != 0) {
/* We do not expect any errno value other than ERANGE, but
* the range of libm bugs appears unbounded.
*/
PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :
PyExc_ValueError);
return NULL;
}
return PyFloat_FromDouble(ix);
}
Run Code Online (Sandbox Code Playgroud)
因此,1e4**100导致ERANGE错误(导致PyExc_OverflowError)然后更高级别的OverflowError异常引发.