Skip to content

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

Open
serhiy-storchaka wants to merge 4 commits intopython:mainfrom
serhiy-storchaka:struct-format
Open

gh-145850: Change some implementation details in struct.Struct#145851
serhiy-storchaka wants to merge 4 commits intopython:mainfrom
serhiy-storchaka:struct-format

Conversation

@serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Mar 12, 2026

  • 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.
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

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

Copy link
Member Author

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Thank you for your review, @vstinner.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

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 :-)

jukarivalladares-boop

This comment was marked as spam.

@StanFromIreland
Copy link
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.

@StanFromIreland
Copy link
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

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.

4 participants