Title: imaplib Time2Internaldate crashing with time.struct_time supplied from datetime.timetuple()
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6, Python 3.5
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: sajicek
Priority: normal Keywords:

Created on 2020-10-10 14:40 by sajicek, last changed 2020-10-10 14:40 by sajicek.

Messages (1)
msg378409 - (view) Author: (sajicek) Date: 2020-10-10 14:40
first of all, I couldn't find any issue related to this, so sorry if it has been solved before.

There seems to be an issue with IMAPLIB in all versions I've checked, when using the Time2Internaldate function with time.struct_time created by datetime.timetuple() extracted from an EmailMessage object (using the email.utils.parsedate_to_datetime function), and even when created with the function crashes the app with
"TypeError: unsupported type for timedelta seconds component: NoneType"
on line 1530 in current master on GH. (delta = timedelta(seconds=gmtoff))

Crash happens because the "date_time.tm_gmtoff" from which the gmtoff variable is constructed, seems to default to None in all instances of time.struct_time object created by datetime.timetuple(), which still passes the try, except clausule on 1520, because it's not an AttributeError. The time.localtime() metioned in the docs asigns the correct value into tm_gmtoff, but the datetime.timetuple() does not.

Possible solutions I've found:
1) inside Time2Internaldate: surround the delta with try and except - in except make the delta = 0
2) inside Time2Internaldate: check the gmtoff for None and if yes, then change it to 0
3) change the default of tm_gmtoff in timetuple from None to 0 (or to a current timezone offset)

or just say in the docs, that time.struct_time created this way (using the datetime.timetuple()) is unsupported

PS: when writing this issue, I've started thinking this might be a datetime lib issue too
Date User Action Args
2020-10-10 14:40:53sajicekcreate