FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-145850: Change some implementation details in struct.Struct by serhiy-storchaka · Pull Request #145851 · python/cpython · GitHub

Repository navigation

gh-145850: Change some implementation details in struct.Struct - #145851

Merged
serhiy-storchaka merged 6 commits into
python:mainfrom
serhiy-storchaka:struct-format
Mar 13, 2026
Merged

serhiy-storchaka merged 6 commits into
python:mainfrom
serhiy-storchaka:struct-format

Conversation

serhiy-storchaka commented Mar 12, 2026 •
edited
Loading

Copy link
Copy Markdown
Member
  • calling it with non-ASCII string format will now raise a ValueError instead of UnicodeEncodeError
  • calling it with non-ASCII bytes format will now raise a ValueError instead of struct.error
  • getting the format attribute of uninitialized object will now raise an AttributeError instead of RuntimeError.

* calling it with non-ASCII string format will now raise a ValueError
  instead of UnicodeEncodeError
* calling it with non-ASCII bytes format will now raise a UnicodeDecodeError
  instead of struct.error
* getting the format attribute of uninitialized object will now raise
  an AttributeError instead of RuntimeError.

vstinner 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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

If we change exceptions, I would prefer a ValueError than an UnicodeDecodeError.

Comment thread Modules/_struct.c Outdated
Comment thread Modules/_struct.c Outdated

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thank you for your review, @vstinner.

Comment thread Modules/_struct.c Outdated
Comment thread Modules/_struct.c Outdated

vstinner 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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM.

Raising ValueError if the format is invalid makes more sense than UnicodeEncodeError or struct.error.

Replacing RuntimeError with AttributeError is more surprising, but I'm fine with AttributeError.

If a project is impacted by this change, it's possible to become compatible with old and new Python versions by catching old and new exceptions. For example, except (UnicodeEncodeError, ValueError):. But I don't expect any project to catch exceptions since they're not supposed to pass invalid format string anyway :-)

This comment was marked as spam.

Copy link
Copy Markdown
Member

CIFuzz found a bug, and it seems related to the changes! To download the testcase, see https://github.com/python/cpython/actions/runs/23007198595/artifacts/5892860339.

Copy link
Copy Markdown
Member

Oh, it is just incompatible with these changes, since they add a ValueError to the list of possible exceptions. Can you please update:

/* Ignore any struct.error exceptions, these can be caused by invalid

Copy link
Copy Markdown
Member Author

Thank you, @StanFromIreland. It is difficult to read the fuzzer output, I suspected that this is some random unrelated failure.

StanFromIreland 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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Fuzzer changes LGTM

Copy link
Copy Markdown
Member

The 3 "Tests / CIFuzz / cpython3" jobs are back to green, good :-)

serhiy-storchaka merged commit 962fb87 into python:main Mar 13, 2026
50 checks passed
serhiy-storchaka deleted the struct-format branch March 13, 2026 11:28
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
…ythonGH-145851)

* calling it with non-ASCII string format will now raise a ValueError
  instead of UnicodeEncodeError
* calling it with non-ASCII bytes format will now raise a ValueError
  instead of struct.error
* getting the format attribute of uninitialized object will now raise
  an AttributeError instead of RuntimeError.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL