struct.Struct() accepts format as a string or a bytes object. It stores the format as the bytes object, but the format getter returns a string (see bpo-21071/gh-65270). While not yet initialized, it stores None, since gh-78724 the getter raises RuntimeError in such case (it returned None prior to gh-65270 and crashed prior to gh-78724). The getter could also raise an UnicodeDecodeError prior to gh-145743.
This all is a Python 2 legacy. We can simply store format as a string, this will simplify the getter. We can also use NULL instead of None as the default value, and use read-only members instead of a getter. This will make the code more strightforward.
This will have user visible effects:
- Non-ASCII string format will lead to ValueError (or better use struct.error?) instead of UnicodeEncodeError.
- Non-ASCII bytes format will lead to
UnicodeDecodeError ValueError instead of struct.error.
- Getting the
format attribute of uninitialized Struct will lead to AttributeError instead of RuntimeError.
These cases normally do not occur in the user code, and the current exceptions is simply an implementation detail.
Linked PRs
struct.Struct()accepts format as a string or a bytes object. It stores the format as the bytes object, but theformatgetter returns a string (see bpo-21071/gh-65270). While not yet initialized, it stores None, since gh-78724 the getter raises RuntimeError in such case (it returned None prior to gh-65270 and crashed prior to gh-78724). The getter could also raise an UnicodeDecodeError prior to gh-145743.This all is a Python 2 legacy. We can simply store format as a string, this will simplify the getter. We can also use NULL instead of None as the default value, and use read-only members instead of a getter. This will make the code more strightforward.
This will have user visible effects:
UnicodeDecodeErrorValueError instead of struct.error.formatattribute of uninitializedStructwill lead to AttributeError instead of RuntimeError.These cases normally do not occur in the user code, and the current exceptions is simply an implementation detail.
Linked PRs