Fwd: timedelta object recursion bug

MRAB python at mrabarnett.plus.com
Thu Jul 28 12:18:01 EDT 2022


On 28/07/2022 10:54, Ben Hirsig wrote:
> Hi, I noticed this when using the requests library in the response.elapsed
> object (type timedelta). Tested using the standard datetime library alone
> with the example displayed on
> https://docs.python.org/3/library/datetime.html#examples-of-usage-timedelta
> 
> 
> 
> It appears as though the timedelta object recursively adds its own
> attributes (min, max, resolution) as further timedelta objects. I’m not
> sure how deep they go, but presumably hitting the recursion limit.
> 
> 
> 
>>from datetime import timedelta
> 
>>year = timedelta(days=365)
> 
>>print(year.max)
> 
>    999999999 days, 23:59:59.999999
> 
>>print(year.max.min.max.resolution.max.min)
> 
>    -999999999 days, 0:00:00
> 
> 
> 
> I’m using 3.10.3
> 
It's not recursion, it's a reference cycle. In fact, more than one:

 >>> from datetime import timedelta
 >>> year = timedelta(days=365)
 >>> type(year)
<class 'datetime.timedelta'>
 >>> type(year.max)
<class 'datetime.timedelta'>
 >>> year.max is year.max.max
True
 >>> type(year.min)
<class 'datetime.timedelta'>
 >>> year.min is year.min.min
True


More information about the Python-list mailing list