2to3?
2to3?
Posted May 29, 2025 21:49 UTC (Thu) by zahlman (guest, #175387)In reply to: 2to3? by Cyberax
Parent article: Recent disruptive changes from Setuptools
PEP 461 is about the % operator for formatting, which still doesn't allow mixed types - it just allows you to have, for example, a byte with value 37 followed by a byte with value 115 in a bytes (or bytearray) and then replace those with a different sequence of bytes.
It's a little hard to imagine how an error handler failing to convert between str and bytes could cause such a headache - because an error handler would presumably be formatting *an error message*, which means the left-hand side should clearly be a string *literal*. And in Python 3 before 3.5, just as today, formatting a bytes into that with % would simply simply give the wrong result (with a 'b' prefix and quotes and escapes). And once you see badly formatted error messages in the logs, you just track them down to the handler according to the template they're formatted into. (Supposing, for the sake of argument, that the formatting isn't actually beneficial in determining what went wrong in the code that had the original error.)
If you mean that code decoded byte data into strings in order to be formatted, and then wasn't properly converted back to bytes, I sympathize; but the original 2.x code in this case *was then already broken* and could have easily failed silently instead (by implicitly using the wrong encoding on either side, or both sides). I understand that many data protocols use "tags" in a binary data stream that are intended to be readable as ASCII (e.g. fourCCs, or HTTP headers interpreted before an encoding has been negotiated); but insertion into such binary data is fundamentally an array-splicing operation, not a text formatting one. (Or if you create HTTP headers from scratch, you must sanitize the data anyway - see https://stackoverflow.com/questions/5251824 - and catching an exception from .encode('ASCII') is as good a way as any.)
