datetime模块中的异常消息来源。 ValueError:年10000超出范围

问题描述 投票:2回答:1

使用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中,但不是相同的消息。

python python-3.x datetime cpython
1个回答
1
投票

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
© www.soinside.com 2019 - 2024. All rights reserved.