简体   繁体   中英

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

Using 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

Note that I'm aware that datetime.datetime doesn't support years with five digits, so I'm not asking why 10000 doesn't work.

I was wondering where the message of the ValueError exception comes from as I don't see anything like year {year} is out of range in the lib/datetime.py code.

The only similarity is in theline 894 but is not the same message.

The actual implementation of datetime.datetime is in _datetimemodule.c and not in datetime.py .

In the C implementation, you can see, as @wjandrea mentioned PyErr_Format(PyExc_ValueError, "year %i is out of range", year); in function check_date_args in line 420 . That one ends up being called from datetime_new , which is used to construct datetime.datetime , see line 6334 .

And about the question how it goes from datetime.py to the c implementation, there is this line :

from _datetime import *

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM