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

[Patch] Also stop using localtime() in timemodule #72335

Closed
EdSchouten mannequin opened this issue Sep 14, 2016 · 14 comments
Closed

[Patch] Also stop using localtime() in timemodule #72335

EdSchouten mannequin opened this issue Sep 14, 2016 · 14 comments
Assignees
Labels
3.7 (EOL) end of life extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@EdSchouten
Copy link
Mannequin

EdSchouten mannequin commented Sep 14, 2016

BPO 28148
Nosy @malemburg, @tim-one, @abalkin, @vstinner, @4kir4, @EdSchouten
PRs
  • [Do Not Merge] Convert Misc/NEWS so that it is managed by towncrier #552
  • Files
  • patch-no-gmtime-localtime: Patch for using gmtime_r()/localtime_r()
  • patch-pytime-localtime-gmtime
  • 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 2016-09-28.21:41:54.164>
    created_at = <Date 2016-09-14.12:35:28.989>
    labels = ['extension-modules', 'type-bug', '3.7']
    title = '[Patch] Also stop using localtime() in timemodule'
    updated_at = <Date 2017-03-31.16:36:25.789>
    user = 'https://github.com/EdSchouten'

    bugs.python.org fields:

    activity = <Date 2017-03-31.16:36:25.789>
    actor = 'dstufft'
    assignee = 'belopolsky'
    closed = True
    closed_date = <Date 2016-09-28.21:41:54.164>
    closer = 'belopolsky'
    components = ['Extension Modules']
    creation = <Date 2016-09-14.12:35:28.989>
    creator = 'EdSchouten'
    dependencies = []
    files = ['44660', '44667']
    hgrepos = []
    issue_num = 28148
    keywords = ['patch']
    message_count = 14.0
    messages = ['276421', '276471', '276472', '276474', '276475', '276479', '276480', '276484', '276487', '276488', '276489', '276490', '277663', '277665']
    nosy_count = 7.0
    nosy_names = ['lemburg', 'tim.peters', 'belopolsky', 'vstinner', 'akira', 'python-dev', 'EdSchouten']
    pr_nums = ['552']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue28148'
    versions = ['Python 3.6', 'Python 3.7']

    @EdSchouten
    Copy link
    Mannequin Author

    EdSchouten mannequin commented Sep 14, 2016

    In bpo-28067, we changed _datetimemodule to stop using localtime() and gmtime(), which is nice. I actually needed such a change for CloudABI (https://mail.python.org/pipermail/python-dev/2016-July/145708.html) which does not provide the thread-unsafe variants. Only localtime_r() and gmtime_r() are provided.

    If we're starting to make use of these functions, let's complete this by also changing timemodule to use them. I've attached a patch.

    Maybe it now makes sense to move the Windows wrappers to some header file, so that they can be shared between both modules. What would be the right location for this?

    @EdSchouten EdSchouten mannequin added the extension-modules C modules in the Modules dir label Sep 14, 2016
    @EdSchouten EdSchouten mannequin changed the title Also stop using localtime() in timemodule [Patch] Also stop using localtime() in timemodule Sep 14, 2016
    @abalkin
    Copy link
    Member

    abalkin commented Sep 14, 2016

    Yes, I think this is a good idea. I was hesitant to make this change while bpo-22798 was open because I thought we may end up exposing changes to tzname caused by localtime and friends.

    I also believe we can classify this as a bug-fix because side-effects of localtime are often undesirable. Nevertheless, I would like to hear from more people before accepting this.

    @abalkin abalkin self-assigned this Sep 14, 2016
    @abalkin abalkin added the type-bug An unexpected behavior, bug, or error label Sep 14, 2016
    @abalkin
    Copy link
    Member

    abalkin commented Sep 14, 2016

    What would be the right location for [the Windows wrappers]?

    I would say Include/pytime.h:

    /**************************************************************************
    Symbols and macros to supply platform-independent interfaces to time related
    functions and constants
    **************************************************************************/

    @EdSchouten
    Copy link
    Mannequin Author

    EdSchouten mannequin commented Sep 14, 2016

    Hi Alexander,

    I'm absolutely no expert when it comes to the Python codebase, so I've got a question. If we're going to movein this to Include/pytime.h, we should likely introduce full wrappers that have a name starting with _PyTime_, right?

    This header seem to be part of the installation, so we should likely not declare any commonly used symbols there.

    @abalkin
    Copy link
    Member

    abalkin commented Sep 14, 2016

    we should likely introduce full wrappers that have a name starting with _PyTime_, right?

    Yes, and I would like to give some thought to what the best API would be. The two choices are to emulate localtime_r on Windows or emulate localtime_s on POSIX. While localtime_r is probably a better known function, localtime_s has been standardized by C11 and may appear on POSIX platforms in the future.

    Also, I think _PyTime_localtime_r/s should include the

     #ifdef EINVAL
             if (errno == 0)
                 errno = EINVAL;
     #endif

    code that is repeated everywhere in the current codebase.

    @EdSchouten
    Copy link
    Mannequin Author

    EdSchouten mannequin commented Sep 14, 2016

    As a person who keeps a close eye on the Austin Group mailing lists (i.e., 'the POSIX working group'), my guess is that it's very unlikely that POSIX will ever add those *_s() extensions. Here's a discussion on Reddit that actually captures all of the arguments pretty well:

    https://www.reddit.com/r/C_Programming/comments/3ivi77/eli5_why_does_glibc_still_not_support_the/

    That said, any API will do. The localtime_r() function has the disadvantage that the return value is a bit odd: can it return any other tm structure than the one provided? localtime_s() is a bit weird in that its input argument is stored after the output argument. Both functions also unnecessarily pass the time_t by reference. Maybe we can just pick a prototype that's as Pythonesque as possible that also fixes these shortcomings. Any thoughts?

    @abalkin
    Copy link
    Member

    abalkin commented Sep 14, 2016

    Maybe we can just pick a prototype that's as Pythonesque as possible that also fixes these shortcomings. Any thoughts?

    Yes, and just call it _PyTime_localtime without the ugly suffix.

    @EdSchouten
    Copy link
    Mannequin Author

    EdSchouten mannequin commented Sep 14, 2016

    Does this patch look all right to you?

    @abalkin
    Copy link
    Member

    abalkin commented Sep 14, 2016

    Very nice. I'll give it a week or two for the others to chime in and commit if I don't hear any objections.

    @abalkin abalkin added the 3.7 (EOL) end of life label Sep 14, 2016
    @abalkin
    Copy link
    Member

    abalkin commented Sep 14, 2016

    I see that you picked localtime_s-like order of arguments. While I have no personal preference, I wonder why you prefer output to follow input. The usual UNIX/C convention is the opposite. Interfaces like sprintf, strcat, strftime and many other have output first. I think the logic is that this is the order in variable assignment OUTPUT = INPUT.

    @EdSchouten
    Copy link
    Mannequin Author

    EdSchouten mannequin commented Sep 14, 2016

    I've been brainwashed by https://google.github.io/styleguide/cppguide.html#Function_Parameter_Ordering over the last couple of years, which is why I thought localtime()/localtime_r()'s way of ordering the arguments made most sense here. ;-)

    @abalkin
    Copy link
    Member

    abalkin commented Sep 14, 2016

    I thought [...]localtime_r()'s way of ordering the arguments made most sense here.

    Right. I keep forgetting which one is localtime_r and which is localtime_s. I don't think there is any preference in the Python codebase. (PEP-8 is silent on this point.)

    Use of time_t instead of time_t* makes it obvious which argument is input, so as I said, for me the order does not matter. Google style guide is reason enough to pick the order.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Sep 28, 2016

    New changeset c81b9107ec42 by Alexander Belopolsky in branch '3.6':
    Issue bpo-28148: Stop using localtime() and gmtime() in the time module.
    https://hg.python.org/cpython/rev/c81b9107ec42

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Sep 28, 2016

    New changeset 3afad465b3e1 by Alexander Belopolsky in branch '3.6':
    Issue bpo-28148: Added a NEWS entry.
    https://hg.python.org/cpython/rev/3afad465b3e1

    @abalkin abalkin closed this as completed Sep 28, 2016
    @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 extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant