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

repr of time.struct_time type does not eval #50157

Closed
jwm mannequin opened this issue May 2, 2009 · 11 comments
Closed

repr of time.struct_time type does not eval #50157

jwm mannequin opened this issue May 2, 2009 · 11 comments
Labels
3.12 bugs and security fixes extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@jwm
Copy link
Mannequin

jwm mannequin commented May 2, 2009

BPO 5907
Nosy @loewis, @birkenfeld, @rhettinger, @abalkin, @merwok, @bitdancer, @lilydjwg
Dependencies
  • bpo-1820: Enhance Object/structseq.c to match namedtuple and tuple api
  • bpo-8413: String interpolation doesn't work with sys.version_info
  • 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 = None
    closed_at = None
    created_at = <Date 2009-05-02.16:30:42.125>
    labels = ['extension-modules', 'type-feature', 'tests', '3.7']
    title = 'repr of time.struct_time type does not eval'
    updated_at = <Date 2016-10-03.16:20:14.137>
    user = 'https://bugs.python.org/jwm'

    bugs.python.org fields:

    activity = <Date 2016-10-03.16:20:14.137>
    actor = 'belopolsky'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Extension Modules', 'Tests']
    creation = <Date 2009-05-02.16:30:42.125>
    creator = 'jwm'
    dependencies = ['1820', '8413']
    files = []
    hgrepos = []
    issue_num = 5907
    keywords = []
    message_count = 9.0
    messages = ['86958', '86963', '86979', '87042', '87053', '133333', '133336', '133356', '197168']
    nosy_count = 10.0
    nosy_names = ['loewis', 'georg.brandl', 'rhettinger', 'belopolsky', 'eric.araujo', 'Arfrever', 'r.david.murray', 'jwm', 'santoso.wijaya', 'lilydjwg']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'needs patch'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue5907'
    versions = ['Python 3.7']

    @jwm
    Copy link
    Mannequin Author

    jwm mannequin commented May 2, 2009

    The output of repr on an object of type time.struct_time has changed
    from 2.5 to 2.6, and can no longer be read in with an eval.

    2.5 behaviour:

    Python 2.5.4 (r254:67916, Apr  4 2009, 17:55:16) 
    [GCC 4.3.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import time
    >>> t1 = time.gmtime()
    >>> t1_repr = repr(t1)
    >>> t1_repr
    '(2009, 5, 2, 16, 16, 43, 5, 122, 0)'
    >>> t2 = eval(t1_repr)
    >>> t1 == t2
    True

    Meanwhile in 2.6:

    Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
    [GCC 4.3.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import time
    >>> t1 = time.gmtime()
    >>> t1_repr = repr(t1)
    >>> t1_repr
    'time.struct_time(tm_year=2009, tm_mon=5, tm_mday=2, tm_hour=16,
    tm_min=20, tm_sec=54, tm_wday=5, tm_yday=122, tm_isdst=0)'
    >>> t2 = eval(t1_repr)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1, in <module>
    TypeError: structseq() takes at most 2 arguments (9 given)

    Obviously returning a time.time_struct based representation is the right
    thing to do, but the repr of this flavour should eval.

    @jwm jwm mannequin added type-bug An unexpected behavior, bug, or error extension-modules C modules in the Modules dir tests Tests in the Lib/test dir labels May 2, 2009
    @loewis
    Copy link
    Mannequin

    loewis mannequin commented May 2, 2009

    Notice that this is no bug: there is no guarantee that repr() can
    eval(). For some types it does, for others, it doesn't.

    @loewis loewis mannequin added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels May 2, 2009
    @birkenfeld
    Copy link
    Member

    Also, the 2.5 behavior was not up to spec either: after eval() you get a
    simple tuple, while the object before was a time.struct_time object.

    (True, you can pass these tuples to the time functions, but an
    eval()able repr() should reproduce the object exactly.)

    @jwm
    Copy link
    Mannequin Author

    jwm mannequin commented May 3, 2009

    While it's true that repr() methods cannot generally be expected to eval
    back into an object of the same type, the old repr behaviour of the
    struct_time object did effectively do so. Admittedly it was a kludge due
    to the time module functions originally producing a tuple before the
    advent of the struct_time type, but code that expected repr->eval
    behaviour worked up to 2.5. It's also evident that who ever changed the
    behaviour of time.struct_time.__repr__ intended that to sill be the
    case, otherwise it would have produced a string of the form <time_struct
    ...>

    So I would describe this as a bug, otherwise you'd have to call it an
    undocumented behaviour change :-)

    The question remains, how to fix it? Should the repr output change to
    time.struct_time((tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec,
    tm_wday, tm_yday, tm_isdst)) or implement the keyword argument form?

    (Note: python3.0 shares this new behaviour)

    @loewis
    Copy link
    Mannequin

    loewis mannequin commented May 3, 2009

    The question remains, how to fix it?

    Or whether to change it at all. It's hard to imagine that
    applications may rely on that aspect of the behavior - they
    have to use eval, after all.

    @abalkin abalkin self-assigned this Jun 12, 2010
    @abalkin
    Copy link
    Member

    abalkin commented Apr 8, 2011

    Or whether to change it at all. It's hard to imagine that
    applications may rely on that aspect of the behavior - they
    have to use eval, after all.

    I would like to see this fixed. When working in interactive session, I find myself adding [:] to struct_time objects because otherwise I cannot copy the result and paste it back to CLI session.

    Actually, the bigger problem for me is that struct_time repr is too verbose with repeated tm_ prefixes distracting from the data I want to see. Chances are I will continue adding [:] or [:6] when I inspect struct_time values even if this issue gets fixed.

    Overall I am +0 on fixing this.

    @abalkin abalkin removed their assignment Apr 8, 2011
    @santosowijaya
    Copy link
    Mannequin

    santosowijaya mannequin commented Apr 8, 2011

    I have a feeling that a successful resolution of bpo-1820 will make things simpler for this one, since time.struct_time uses structseq.

    @rhettinger
    Copy link
    Contributor

    Also see bpo-11698

    @rhettinger rhettinger self-assigned this Apr 8, 2011
    @bitdancer
    Copy link
    Member

    It is very surprising that evaling the repr doesn't work, since struct_time is documented to have a named tuple interface, and evaling the repr for a namedtuple works. So hopefully fixing bpo-1820 will also fix this.

    @rhettinger rhettinger removed their assignment Oct 10, 2013
    @abalkin abalkin added the 3.7 (EOL) end of life label Oct 3, 2016
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @iritkatriel iritkatriel removed the 3.7 (EOL) end of life label May 31, 2022
    @AA-Turner AA-Turner added 3.12 bugs and security fixes and removed tests Tests in the Lib/test dir labels May 31, 2022
    @AA-Turner
    Copy link
    Member

    Confirmed this still fails:

    >>> eval(repr(time.gmtime()))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1, in <module>
    TypeError: structseq() takes at most 2 keyword arguments (9 given)

    A

    @merwok
    Copy link
    Member

    merwok commented Jun 1, 2022

    Reprs often round-trip but it is not a requirement, and if it’s not explicitly documented by the object then changing it would not qualify as a bug. Given the age of this ticket, I would suggest closing it.

    @AA-Turner AA-Turner closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.12 bugs and security fixes extension-modules C modules in the Modules dir type-feature A feature request or enhancement
    Projects
    Archived in project
    Development

    No branches or pull requests

    7 participants