It's a blunt instrument to be sure, but a necessary one, I think -
otherwise we end with a scattering of API specific solutions rather than a
single consistent approach.
Here's a thought, though: what if we went with Serhiy's "reconfigure"
idea, limited that to seekable streams (resetting then back to the start of
the file) and then also had a "force_reconfigure" that bypassed the safety
checks?
The main problem I see with that idea is that some changes (binary vs text,
universal newlines or not, buffered or not) would potentially require
adding or removing a class from the stream's IO stack, thus rendering it
difficult to handle as an in-place operation.
However the "seekable streams only by default, flag or separate method to
force an encoding change for a non-seekable stream" approach would be
applicable even for the basic "set_encoding" API.
I'm beginning to think that this is all complicated enough that it should
be written up in a PEP for 3.5 before we actually commit anything (even if
what we end up committing is just set_encoding with a "force=False" keyword
only parameter). |