Skip to content

gh-154387: Fix structseq_repr mislabeling of unnamed PyStructSequence fields#154434

Closed
pranavchoudhary-tech wants to merge 9 commits into
python:mainfrom
pranavchoudhary-tech:fix-structseq-repr
Closed

gh-154387: Fix structseq_repr mislabeling of unnamed PyStructSequence fields#154434
pranavchoudhary-tech wants to merge 9 commits into
python:mainfrom
pranavchoudhary-tech:fix-structseq-repr

Conversation

@pranavchoudhary-tech

@pranavchoudhary-tech pranavchoudhary-tech commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Fixes #154387.

structseq_repr incorrectly assumed that all fields printed were sequentially named fields from tp_members. When an unnamed field was encountered (like the timestamp float variants in os.stat_result), it would continue indexing into tp_members array, erroneously labeling unnamed field values with subsequent named field labels from the struct sequence definition.

This PR fixes the bug by resolving the field name accurately based on the expected offset rather than the sequential index, correctly printing unnamed fields without labels and accurately matching named fields.

@pranavchoudhary-tech
pranavchoudhary-tech requested a review from a team as a code owner July 22, 2026 07:16
@pranavchoudhary-tech pranavchoudhary-tech changed the title Fix structseq_repr mislabeling of unnamed PyStructSequence fields gh-154387: Fix structseq_repr mislabeling of unnamed PyStructSequence fields Jul 22, 2026

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is not only about the repr but about labelling and accesses. I do not think this issue is easy for a newcomer as you need more testing here. In addition, not having anyone assigned to an issue does not mean it is free to pick as issues need first to be discussed.

self.assertIn("st_ino=", rep)
self.assertIn("st_dev=", rep)

# Issue #154387: unnamed fields should not be mislabeled with named field names.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not test the same things as the issue. Please be more thorough here.

Comment thread PCbuild/readme.txt

By default, build.bat will build Python in Release configuration for
the 32-bit Win32 platform. It accepts several arguments to change
this behavior, try `build.bat -h` to learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated changes please delete this.

@bedevere-app

bedevere-app Bot commented Jul 22, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

self.assertEqual(
rep,
"os.stat_result(st_mode=0, st_ino=1, st_dev=2, st_nlink=3, "
"st_uid=4, st_gid=5, st_size=6, 7, 8, 9)"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think extra items should actually ne considered if they are un-named, or at least they should be formatted differently. We jeed to discuss the format first because here, this repr would not work (you cannot just use it again as an input).

I do not know whether it is better to simply ignore unnamed fields or do something else.

@pranavchoudhary-tech

Copy link
Copy Markdown
Contributor Author

Hi @picnixz, thank you for taking the time to review this!

You make a great point about the repr output resulting in invalid syntax if we just drop the labels. I completely agree that we need to align on the expected format before proceeding. Do you think we should completely omit the unnamed fields from the repr output, or is there another format the core team would prefer?

I also apologize for jumping the gun and opening a PR before the issue was fully discussed. I have reverted the unrelated change to PCbuild/readme.txt as requested.

Lastly, I saw your comment on the issue tracker about this also affecting "labelling and accesses". I did some digging into Objects/structseq.c and you're absolutely right—the root cause is that tp_members is constructed by omitting the unnamed fields, which breaks the index math (e.g. i - n_unnamed_fields) in structseq_reduce, structseq_new_impl, and structseq_replace. I have a complete fix prepared for all of those underlying access bugs that replaces the index math with an offset lookup.

I will hold off on pushing that code until we reach consensus on what the repr format should be. Let me know how you'd like to proceed!

@picnixz

picnixz commented Jul 22, 2026

Copy link
Copy Markdown
Member

Do you think we should completely omit the unnamed fields from the repr output, or is there another format the core team would prefer?

First of all, I don't want to have a discussion with an LLM agent. If it's for translation, I'm ok, but I don't want an unedited agent answer, it's wasting my time. It's also not aligned with our AI policies: https://devguide.python.org/getting-started/ai-tools/#guidelines-for-using-ai-tools.

I still think it's premature for a PR to be made so I'm clsoing it. In the future, don't rely on agents for discussion unless it's for translation or for understanding the codebase on your end or helping you write the code.

@picnixz picnixz closed this Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

structseq_repr mislabels unnamed PyStructSequence fields (e.g. os.stat_result slots 7–9)

2 participants