Issue 11698: Improve repr for structseq objects to show named, but unindexed fields (original) (raw)

Issue11698

process

Status: open Resolution:
Dependencies: Superseder:
Assigned To: pitrou Nosy List: Arfrever, berker.peksag, dlam, eric.araujo, eric.snow, ezio.melotti, lilydjwg, mblahay, pconnell, pitrou, rhettinger, serhiy.storchaka, sunfinite
Priority: low Keywords: easy, patch

Created on 2011-03-27 22:14 by rhettinger, last changed 2022-04-11 14:57 by admin.

Files
File name Uploaded Description Edit
issue11698.patch dlam,2012-07-16 05:18
structseq_2.patch sunfinite,2013-11-07 12:45 Show named but unindexed fields under the dict keyword argument review
Messages (14)
msg132372 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-03-27 22:14
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: posix.stat_result(st_mode=33277, st_ino=8468407, st_dev=234881026, st_nlink=1, st_uid=0, st_gid=80, st_size=25424, st_atime=1301263901, st_mtime=1298229258, st_ctime=1298283922) but it doesn't show the other named fields and their values: {'st_ctime': 1298283922.0, 'st_rdev': 0, 'st_mtime': 1298229258.0, 'st_blocks': 56, 'st_flags': 0, 'st_gen': 0, 'st_atime': 1301263901.0, 'st_blksize': 4096, 'st_birthtime': 1298229258.0} 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.
msg153754 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2012-02-20 01:55
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
msg153755 - (view) Author: Γ‰ric Araujo (eric.araujo) * (Python committer) Date: 2012-02-20 01:56
You are! See the devguide for more setup guidelines.
msg155261 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-03-09 19:49
+1. Also, the repr() should show the float values of st_mtime and friends, rather than truncated integers.
msg165570 - (view) Author: David Lam (dlam) * Date: 2012-07-16 05:18
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
msg188416 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-05-04 22:20
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'))" posix.stat_result(st_mode=33204, st_ino=6553619, st_dev=2053, st_nlink=1, st_uid=1000, st_gid=1000, st_size=15089, st_atime=1367693898, st_mtime=1365264866, st_ctime=1366481591, st_atime=1367693898.528636, st_mtime=1365264866.4163036, st_ctime=1366481591.9862735, st_atime_ns=1367693898528635928, st_mtime_ns=1365264866416303676, st_ctime_ns=1366481591986273627, st_blksize=4096, st_blocks=32, st_rdev=0) As you see, fields such as "st_atime" are duplicated. There are other issues with the patch: * C variable declarations should always be at the beginning of blocks (otherwise it's not C89-compliant) * C++-style comments (//) are forbidden * I don't understand in which circumstances `Py_TYPE(obj)->tp_members[i-n_unnamed_fields].name` can be NULL
msg200624 - (view) Author: Sunny K (sunfinite) * Date: 2013-10-20 19:45
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.
msg200718 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-10-21 08:25
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/ Thanks!
msg202332 - (view) Author: Sunny K (sunfinite) * Date: 2013-11-07 12:45
The previous patch had a wrong mapping between keys and values. The current implementation of repr means that duplicated keys will be present when invisible fields are included. See points 2 and 3 in http://bugs.python.org/issue1820#msg202330 for more explanation. I have sidestepped that issue by placing invisible fields under the dict argument. This also plays well with the current code in structseq_new and eval(repr(obj)) works. The output with the patch is: $./python -c "import os; print(os.stat('LICENSE'))" os.stat_result(st_mode=33188, st_ino=577299, st_dev=64512, st_nlink=1, st_uid=33616, st_gid=600, st_size=12749, st_atime=1382696747, st_mtime=1382361968, st_ctime=1382361968, dict={'st_atime':1382696747.0, 'st_mtime':1382361968.0, 'st_ctime':1382361968.0, 'st_atime_ns':1382696747000000000, 'st_mtime_ns':1382361968000000000, 'st_ctime_ns':1382361968000000000, 'st_blksize':4096, 'st_blocks':32, 'st_rdev':0})
msg202364 - (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * (Python triager) Date: 2013-11-07 16:44
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'))" os.stat_result(st_mode=33188, st_ino=577299, st_dev=64512, st_nlink=1, st_uid=33616, st_gid=600, st_size=12749, st_atime=1382696747, st_mtime=1382361968, st_ctime=1382361968, *, st_atime=1382696747.0, st_mtime=1382361968.0, st_ctime=1382361968.0, st_atime_ns=1382696747000000000, st_mtime_ns=1382361968000000000, st_ctime_ns=1382361968000000000, st_blksize=4096, st_blocks=32, st_rdev=0)
msg224473 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-08-01 08:30
Could somebody pick this up please as it fixes #5907.
msg341578 - (view) Author: Michael Blahay (mblahay) * Date: 2019-05-06 17:50
I will work on this
msg341591 - (view) Author: Michael Blahay (mblahay) * Date: 2019-05-06 18:37
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).
msg378933 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-10-19 08:47
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 "", line 1, in 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 ...>
History
Date User Action Args
2022-04-11 14:57:15 admin set github: 55907
2020-10-19 08:47:32 serhiy.storchaka set nosy: + serhiy.storchakamessages: +
2019-05-06 18:37:50 mblahay set nosy:rhettinger, pitrou, ezio.melotti, eric.araujo, Arfrever, eric.snow, lilydjwg, berker.peksag, dlam, pconnell, sunfinite, mblahaymessages: +
2019-05-06 17:50:03 mblahay set nosy: + mblahaymessages: +
2018-01-29 20:52:31 rhettinger set versions: + Python 3.8, - Python 3.7
2016-10-03 21:16:12 BreamoreBoy set nosy: - BreamoreBoy
2016-10-03 16πŸ”ž19 belopolsky set stage: patch reviewversions: + Python 3.7, - Python 3.4
2015-01-21 08:15:15 lilydjwg set nosy: + lilydjwg
2014-08-01 08:30:28 BreamoreBoy set nosy: + BreamoreBoymessages: +
2013-11-07 16:44:28 Arfrever set messages: +
2013-11-07 12:45:28 sunfinite set files: + structseq_2.patchmessages: +
2013-11-07 12:42:36 sunfinite set files: - structseq.patch
2013-10-21 08:25:31 pitrou set messages: +
2013-10-21 05:40:07 rhettinger set assignee: pitrou
2013-10-20 19:45:07 sunfinite set files: + structseq.patchnosy: + sunfinitemessages: +
2013-07-12 07:44:40 rhettinger set versions: + Python 3.4, - Python 3.3
2013-06-25 16:42:15 eric.snow set nosy: + eric.snow
2013-05-04 22:20:29 pitrou set messages: +
2013-04-19 19:30:14 pconnell set nosy: + pconnell
2012-08-23 14:37:27 moijes12 set nosy: - moijes12
2012-08-23 14:26:17 moijes12 set nosy: + moijes12
2012-07-16 15:03:12 Arfrever set nosy: + Arfrever
2012-07-16 05πŸ”ž26 dlam set files: + issue11698.patchnosy: + dlammessages: + keywords: + patch
2012-03-09 19:49:30 pitrou set nosy: + pitroumessages: +
2012-02-20 03:42:02 ezio.melotti set nosy: + ezio.melotti
2012-02-20 01:56:39 eric.araujo set messages: +
2012-02-20 01:55:05 berker.peksag set nosy: + berker.peksagmessages: +
2011-07-13 15:41:10 eric.araujo set nosy: + eric.araujo
2011-03-27 22:14:56 rhettinger create