簡體   English   中英

datetime 模塊中異常消息的來源。 ValueError:10000 年超出范圍

[英]Origin of exception message in datetime module. ValueError: year 10000 is out of range

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

唯一的相似之處在於第894 行,但不是同一條消息。

datetime.datetime的實際實現在_datetimemodule.c而不是datetime.py

在 C 實現中,您可以看到,正如@wjandrea 提到的PyErr_Format(PyExc_ValueError, "year %i is out of range", year); 第 420 行的函數check_date_args中。 即一端向上被調用datetime_new ,其被用於構建datetime.datetime ,見線6334

關於它如何從datetime.py到 c 實現的問題,有這一行

from _datetime import *

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM