New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Different behavior between datetime.py and its C accelerator #77993
Comments
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. |
The message isn't confusing - the definition of "aware" is confusing ;-) """ |
So you are suggesting that my datetime(2000,1,1,tzinfo=TZ()) should behave as a naive instance, right? Well, this would be a third behavior different from both current C and Python implementations: >>> print(datetime(2000,1,1).astimezone(timezone.utc))
2000-01-01 05:00:00+00:00 (I am in US/Eastern timezone.) |
I copy/pasted the definitions of "aware" and "naive" from the docs. Your TZ's .utcoffset() returns None, so, yes, any datetime using an instance of that for its tzinfo is naive. In print(datetime(2000,1,1).astimezone(timezone.utc)) the docs for astimezone say, in part, """ So it converted your naive time (viewed as being in your system - EDT - time zone) to UTC. That appears to be using a different definition of "naive" (looking only at whether self.tzinfo is None). The original datetime.py certainly didn't do that ... """ if tz is mytz:
return self
# Convert self to UTC, and attach the new time zone object.
myoffset = self.utcoffset()
if myoffset is None:
raise ValueError("astimezone() requires an aware datetime")
""" So it originally used the definition I quoted first. The "sometimes pretend it's local time anyway" twist appeared to get added here: |
Tim, given that I've updated the documentation, should we treat this as a bug fix or a feature? Note that the type check is definitely a bug-fix (if not a security issue), but I clearly had a wrong definition of "naive" in mind when I was modifying astimezone to support naive instances. In any case, it looks like a news entry is in order. |
I'd call it a bug fix, but I'm really not anal about what people call things ;-) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: