Skip to content
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

Closed
abalkin opened this issue Jun 9, 2018 · 9 comments
Closed

Different behavior between datetime.py and its C accelerator #77993

abalkin opened this issue Jun 9, 2018 · 9 comments
Assignees
Labels
3.7 (EOL) end of life 3.8 only security fixes extension-modules C modules in the Modules dir stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@abalkin
Copy link
Member

abalkin commented Jun 9, 2018

BPO 33812
Nosy @tim-one, @abalkin
PRs
  • bpo-33812: Corrected astimezone for naive datetimes. #7578
  • [3.7] bpo-33812: Corrected astimezone for naive datetimes. (GH-7578) #7600
  • [3.6] bpo-33812: Corrected astimezone for naive datetimes. (GH-7578) #7601
  • 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:

    assignee = 'https://github.com/abalkin'
    closed_at = <Date 2018-06-10.22:06:00.260>
    created_at = <Date 2018-06-09.00:02:26.755>
    labels = ['extension-modules', '3.8', 'type-bug', 'library', '3.7']
    title = 'Different behavior between datetime.py and its C accelerator'
    updated_at = <Date 2018-06-10.22:06:00.259>
    user = 'https://github.com/abalkin'

    bugs.python.org fields:

    activity = <Date 2018-06-10.22:06:00.259>
    actor = 'belopolsky'
    assignee = 'belopolsky'
    closed = True
    closed_date = <Date 2018-06-10.22:06:00.260>
    closer = 'belopolsky'
    components = ['Extension Modules', 'Library (Lib)']
    creation = <Date 2018-06-09.00:02:26.755>
    creator = 'belopolsky'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33812
    keywords = ['patch']
    message_count = 9.0
    messages = ['319121', '319122', '319131', '319134', '319195', '319196', '319245', '319253', '319254']
    nosy_count = 2.0
    nosy_names = ['tim.peters', 'belopolsky']
    pr_nums = ['7578', '7600', '7601']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue33812'
    versions = ['Python 3.6', 'Python 3.7', 'Python 3.8']

    @abalkin
    Copy link
    Member Author

    abalkin commented Jun 9, 2018

    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.

    @abalkin abalkin changed the title Different behavior betwee Different behavior between datetime.py and its C accelerator Jun 9, 2018
    @tim-one
    Copy link
    Member

    tim-one commented Jun 9, 2018

    The message isn't confusing - the definition of "aware" is confusing ;-)

    """
    A datetime object d is aware if d.tzinfo is not None and d.tzinfo.utcoffset(d) does not return None. If d.tzinfo is None, or if d.tzinfo is not None but d.tzinfo.utcoffset(d) returns None, d is naive.
    """

    @abalkin
    Copy link
    Member Author

    abalkin commented Jun 9, 2018

    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.)

    @abalkin abalkin added 3.7 (EOL) end of life 3.8 only security fixes extension-modules C modules in the Modules dir stdlib Python modules in the Lib dir labels Jun 9, 2018
    @abalkin abalkin self-assigned this Jun 9, 2018
    @abalkin abalkin added the type-bug An unexpected behavior, bug, or error label Jun 9, 2018
    @tim-one
    Copy link
    Member

    tim-one commented Jun 9, 2018

    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,

    """
    If self is naive (self.tzinfo is None), it is presumed to represent time in the system timezone.
    """

    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 ...

    """
    def astimezone(self, tz):
    if not isinstance(tz, tzinfo):
    raise TypeError("tz argument must be an instance of tzinfo")
    mytz = self.tzinfo
    if mytz is None:
    raise ValueError("astimezone() requires an aware datetime")

        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:

    fdc860f

    @abalkin
    Copy link
    Member Author

    abalkin commented Jun 10, 2018

    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.

    @tim-one
    Copy link
    Member

    tim-one commented Jun 10, 2018

    I'd call it a bug fix, but I'm really not anal about what people call things ;-)

    @abalkin
    Copy link
    Member Author

    abalkin commented Jun 10, 2018

    New changeset 877b232 by Alexander Belopolsky in branch 'master':
    bpo-33812: Corrected astimezone for naive datetimes. (GH-7578)
    877b232

    @abalkin
    Copy link
    Member Author

    abalkin commented Jun 10, 2018

    New changeset 037e912 by Alexander Belopolsky (Miss Islington (bot)) in branch '3.7':
    bpo-33812: Corrected astimezone for naive datetimes. (GH-7578) (GH-7600)
    037e912

    @abalkin
    Copy link
    Member Author

    abalkin commented Jun 10, 2018

    New changeset 1d4089b by Alexander Belopolsky (Miss Islington (bot)) in branch '3.6':
    bpo-33812: Corrected astimezone for naive datetimes. (GH-7578) (GH-7601)
    1d4089b

    @abalkin abalkin closed this as completed Jun 10, 2018
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes extension-modules C modules in the Modules dir stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants