使用Python 3.8:
In [12]: datetime(10000, month=2, day=1)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-12-f4737756e718> in <module>
----> 1 datetime(10000, month=2, day=1)
ValueError: year 10000 is out of range
请注意,我知道 datetime.datetime 不支持五位数字的年份,所以我不是问为什么 10000 不起作用。
我想知道 ValueError 异常的消息来自哪里,因为我在 lib/datetime.py 中没有看到类似 year {year} is out of range
的内容。代码。
唯一的相似之处在于 line 894但不是同一条消息。
最佳答案
datetime.datetime
的实际实现位于 _datetimemodule.c
而不是 datetime.py
.
在 C 实现中,您可以看到,正如 @wjandrea 在函数 check_date_args
中提到的 PyErr_Format(PyExc_ValueError, "year %i is out of range",year);
在 line 420 。
该函数最终从 datetime_new
调用,它用于构造 datetime.datetime
,请参阅 line 6334 .
关于如何从 datetime.py
到 c 实现的问题,有 this line :
from _datetime import *
关于python - 日期时间模块中异常消息的来源。值错误: year 10000 is out of range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59921116/