So, now that we’ve solidified what values Message._payload can hold, we can do some simplifications. Thanks to Vajrasky Kok for pointing out, in issue 19957 (closed), a simplification in encode_7or8bit(), and to Serhiy Storchaka for pointing out an additional simplification in that same function.
The elimination of the special case for ISO-2022 is especially welcome, as special cases are notoriously fragile. I tried to eliminate it once before, but at that time the model was still a bit inconsistent, so there were cases where ISO-2022 would fail to be encoded correctly. With the recent change to set_payload, this should no longer be a problem.