Documentation for .decode() differences between 3.14 & 3.15

Is there documentation anywhere detailing the differences in decode() and how it has changed between 3.14 and 3.15?

I have some code which deals with html Post “multipart/form-data” and as far as I recall worked fine in 3.14. However with 3.15.0rc2 this now causes utf-8 can't decode byte 0xff in pos 262.

Could it perhaps be that 3.14 used to automatically detect the encoding but 3.15 now doesn’t do that but defaults to utf-8 instead?

See PEP 686 – Make UTF-8 mode default | peps.python.org

It never auto-detected the encoding. It always used the system locale before Python 3.15. One can use encoding='locale' since Python 3.10 to keep the old behavior.

If this is data then it should remain as bytes rather than being decoded into str. .decode is typically incorrect for a raw bytes stream. This code as you’ve explained it would crash when run on a platform with a UTF-8 locale regardless of Python version.

Indeed. And if you actually need the decoding, you should not have relied on the locale even before 3.15.

Incoming stream from wsgi_input.read is, as far as I can determine, bytes. However that needs to be split out into sections delimited by content_type.boundary and needs to have last occurrence of “–” removed (after right stripping /r/n). Can’t see how I can do this while the object remains as bytes.

As an example here is the last few lines of the Post_data followed by it’s type and content_type:

...
\xb4\xbbm\xaf\x1bqV\xe4\xb8}\r\xa0\x9a7i\x8a( J\xd9\xe3\xd2\x88\xe29b\xe0\xd9\xba\x8d\xb4c
\xb4mz\xf2\xcfY\x855\x1bY\t@>Y\xf2G\x1e\x95\xd7\xcd\xf1\x17\xc3-+x92&BX\x92\xa1
\x07\x1e\xd5\xe5\x9e$\xf1\x95\xed\x97\x8a\r\x9b,s\xc1",\xa3x\xf9\x94\x9e
\xc0\xd5\xf6\xd3\xec\xe6b\xe6\x03\x96\xf9\x8f\xce{\xd4\xc2\xbb\xa8sJZ\xfb\xa7\xff\xd9\r\n
------geckoformboundarye1ed624d5ecca747f8290c3f9108189e--\r\n'

type(post_data)=<class 'bytes'>
content_type='multipart/form-data; boundary=----geckoformboundarye1ed624d5ecca747f8290c3f9108189e'

Avoid posting – (en dash) when you mean - (hyphen-minus). This distinction matters while debugging codecs and Unicode.

One can still do split, strip, and regular expressions on bytes.

content_type should also be received as bytes, otherwise you’d need to keep track of the encoding used.

What I tried to post was hyphen-hyphen, don’t know how I am supposed to do that.

If post_data is left as bytes it errors in:

if post_data.rstrip().endswith("--"):

with

endswith first arg must be bytes or a tuple of bytes, not str

Have to admit I didn’t know that I could use things like .rstrip() / endswith() / .split() with a bytes object. Seems I just need to add .encode() to other parameters like this:

if post_data.rstrip().endswith("--".encode()):

However I question whether the post_data is really bytes or is actually encoded in some way. I have just viewed that file in notepad and it is totally different to what I see printed in the Python terminal when I print(post)data.

you can also use b strings: if post_data.rstrip().endswith(b"--"):

@Standre very helpful, didn’t know that, thanks. That will shorten my code quite a bit.

Many thanks @HexDecimal & @Standre, changed to bytes type all the way through the code, took a while, but now fully working. File from html post_data saves correctly.

Just got the type-hints for these modifications to change now.

Use backticks ` for code and verbatim text, like you’re already doing. `--` becomes --