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
Improve repr for structseq objects to show named, but unindexed fields #55907
Comments
The current __repr__ for structseq only shows the name/value pairs for the positional part and it ignores the other named fields. For example, os.stat(somefile) returns: but it doesn't show the other named fields and their values: The __reduce__ method for structseq returns both the tuple portion and the dictionary portion. The latter needs to be added to the repr so that information doesn't get hidden from the user. |
I'd like to work on this issue. I found the Objects/structseq.c [1] file. Am I on the right path? Thanks! [1] http://hg.python.org/cpython/file/5b4b70bd2b6f/Objects/structseq.c#l157 |
You are! See the devguide for more setup guidelines. |
+1. Also, the repr() should show the float values of st_mtime and friends, rather than truncated integers. |
hi hi, found this bug after clicking the "Easy issues" link i basically just took Ray's hint to look at the __reduce__ method, and applied it to the __repr__ method in this patch also updated is the test_repr() unittest |
Thanks for the patch. (Yes, I'm looking at this a bit late :-)) First, there seems to be a problem with the repr() of of.stat() results: ./python -c "import os; print(os.stat('LICENSE'))" As you see, fields such as "st_atime" are duplicated. There are other issues with the patch:
|
Added patch for 3.4. The patch demarcates the output by adding a {...} around the dictionary portion. Please let me know if this is the right format or if not required at all. It is a simple change. |
Hmm, does anyone have an opinion for or against the proposed representation in Sunny's patch? Sunny, if you haven't done so, could you sign a contributor's agreement? http://www.python.org/psf/contrib/ |
The previous patch had a wrong mapping between keys and values. The I have sidestepped that issue by placing invisible fields under the dict argument. This also plays well with the current code in The output with the patch is: $./python -c "import os; print(os.stat('LICENSE'))" |
IMHO '*' could be used as a separator, since relation between indexable fields and named, unindexable fields is similar to relation between positional-or-keyword parameters and keyword-only parameters. $./python -c "import os; print(os.stat('LICENSE'))" |
Could somebody pick this up please as it fixes bpo-5907. |
I will work on this |
I have been advised to avoid enhancements like this one, so I am setting this back down. Also, this should be relabeled as easy(c). |
Other problem is that the repr looks like an evaluable expression, but evaluating it will always produce error. >>> st = os.stat('/dev/null')
>>> st
os.stat_result(st_mode=8630, st_ino=6, st_dev=6, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1602523313, st_mtime=1602523313, st_ctime=1602523313)
>>> os.stat_result(st_mode=8630, st_ino=6, st_dev=6, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1602523313, st_mtime=1602523313, st_ctime=1602523313)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: structseq() takes at most 2 keyword arguments (10 given) os.stat_result() accepts only two arguments: a tuple for indexable elements and a dict for non-indexable elements. >>> os.stat_result((8630, 6, 6, 1, 0, 0, 0, 1602523313, 1602523313, 1602523313), {'st_atime': 1602523313.282834, 'st_mtime': 1602523313.282834, 'st_ctime': 1602523313.282834, 'st_atime_ns': 1602523313282834115, 'st_mtime_ns': 1602523313282834115, 'st_ctime_ns': 1602523313282834115, 'st_blksize': 4096, 'st_blocks': 0, 'st_rdev': 259})
os.stat_result(st_mode=8630, st_ino=6, st_dev=6, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1602523313, st_mtime=1602523313, st_ctime=1602523313) But such form looks not very readable, because it lacks names for indexable elements. To solve this we can use an angular form in the repr: <os.stat_result ...> |
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: