Consider the following code:
$ cat bug.py
import sys
if len(sys.argv) > 1:
sys.modules['_datetime'] = None
from datetime import tzinfo, datetime, timezone
class TZ(tzinfo):
def utcoffset(self, t):
pass
print(datetime(2000,1,1,tzinfo=TZ()).astimezone(timezone.utc))
When running with no arguments (with C acceleration), I get
$ ./python.exe bug.py
2000-01-01 00:00:00+00:00
but the pure python code produces an error
$ ./python.exe bug.py pure
Traceback (most recent call last):
File "bug.py", line 10, in <module>
print(datetime(2000,1,1,tzinfo=TZ()).astimezone(timezone.utc))
File ".../Lib/datetime.py", line 1783, in astimezone
raise ValueError("astimezone() requires an aware datetime")
ValueError: astimezone() requires an aware datetime
Note that some kind of error is expected because TZ.utcoffset() returns None instead of a timedelta, but the error message produced by pure python code is confusing. |