| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
structseq_repr() labeled each visible slot by indexing tp_members by slot position, but tp_members is packed with unnamed fields skipped, so every slot at or after the first unnamed field borrowed the name of a later hidden field. For os.stat_result the integer time slots printed as st_atime/st_mtime/st_ctime (which are actually separate hidden float fields), so the repr disagreed with both st[i] and the .st_atime attribute. Walk the members alongside the visible slots and label unnamed slots positionally as "<unnamed@N>", which is correct regardless of where the unnamed fields sit among the visible ones.
Lower n_in_sequence to 4 in the _testcapi interspersed helper so the trailing
named field ("fifth") is hidden, and expand
test_repr_with_interspersed_unnamed_fields to assert the full surface: repr
(hidden field excluded), field counts, __match_args__, tuple()/len(), and
attribute access to visible and hidden fields (the latter both provided and
defaulted to None).
os.stat_result has three unnamed integer time slots (7-9) whose values are mirrored by the named float fields st_atime/st_mtime/st_ctime (statresult_new fills the latter from the former). The generic struct sequence repr shows those unnamed slots as <unnamed@7..9>, which is uninformative. Add statresult_repr showing the named fields (st_mode..st_size and the float st_atime/st_mtime/st_ctime) by name, matching the attributes. Install it through the type dict via PyObject_SetAttrString so repr() and st.__repr__() resolve to the same function; a raw tp_repr assignment would leave the __repr__ wrapper readied by PyStructSequence_NewType pointing at the generic repr. Other struct sequences keep the generic <unnamed@N>.
| Back | FazBrowse Home | New Git URL |
Walk the members alongside the visible slots and label unnamed slots positionally as <unnamed@N>, which is correct regardless of where the unnamed fields sit among the visible ones.
Before this PR, the member slots are accessed incorrectly in repr when there are unnamed fields. It can cause a spurious SystemError when the index reaches the number of named fields (n_fields - n_unnamed_fields), or display the unnamed fields with <name>=<value> using a later field's name.
For example, a structseq type with WithUnnamed(first, <unnamed>, third, <unnamed>) + [hidden .fifth], before this PR:
After this PR:
Before this PR, for os.stat_result, the unnamed slots printed as the following field names st_atime/st_mtime/st_ctime.
After this PR, a new custom statresult_repr is added analogously to statresult_new. The custom repr shows the fields 10-12 (always initialized in statresult_new) instead of generic <unnamed@N> (introduced in this PR).
Without the custom statresult_repr, the generic <unnamed@N> will be used, which is not that legible.
Fixes #154387