python - 日期时间模块中异常消息的来源。值错误: year 10000 is out of range

标签 python python-3.x datetime cpython

使用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/

相关文章:

python - 保存部分 sklearn 管道

python - 解析未声明的参数

python - Gio.MemoryInputStream 关​​闭时不释放内存

python - 将JSON字符串数组转换为Python中的对象数组

python - 使用reportlab构建表时设置表的缩进

python - 如何装饰子类中的所有继承方法

python - 参数必须是字节或unicode,得到 '_Element'

javascript - JavaScript 上日期对象的问题

php - "phpinfo(): It is not safe to rely on the system' s 时区设置...”

ruby - 如何修复 "uninitialized constant Context::DateTime"错误?