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
Comments
The output of repr on an object of type time.struct_time has changed 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 |
Notice that this is no bug: there is no guarantee that repr() can |
Also, the 2.5 behavior was not up to spec either: after eval() you get a (True, you can pass these tuples to the time functions, but an |
While it's true that repr() methods cannot generally be expected to eval So I would describe this as a bug, otherwise you'd have to call it an The question remains, how to fix it? Should the repr output change to (Note: python3.0 shares this new behaviour) |
Or whether to change it at all. It's hard to imagine that |
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. |
I have a feeling that a successful resolution of bpo-1820 will make things simpler for this one, since time.struct_time uses structseq. |
Also see bpo-11698 |
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. |
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 |
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. |
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: