使用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异常的消息来自哪里,因为在year {year} is out of range
代码中看不到lib/datetime.py之类的东西。
唯一的相似之处在line 894中,但不是相同的消息。
datetime.datetime
的实际实现在_datetimemodule.c
中,而不在_datetimemodule.c
中。
在C实现中,您可以看到@wjandrea在datetime.py
中的函数datetime.py
中提到了PyErr_Format(PyExc_ValueError, "year %i is out of range", year);
。最终被check_date_args
调用,后者用于构造line 420,请参见datetime_new
。
关于它如何从datetime.datetime
到c实现的问题,有line 6334:
datetime.py