It's implausible to expect Python programmers to correctly handle this, hence fsencode/fsdecode and surrogate escaping.
The problem is universal to every time you interface with the operating system. Sometimes it's locale dependent too and you never know beforehand so a program might blow up in the hands of a customer. While the encode/decode process was tiresome at least it gave predictable results.
If you have to work around your runtime in order to give the parameters you want to the underlying syscalls, you are doing it wrong. The premise to move to unicode was a laudable one, but I wish more attention had been paid to how other languages do it, because anything worse than what Perl 5 did more than a decade earlier isn't that great.
Python's behaviour is only problematic when all of the below is true:
- Running on Linux and,
- using glibc and,
- glibc's locale files were deleted or the locale is otherwise not configured and,
- you try to read/write non-ASCII file names. (In this scenario Python falls back to the only locale it can process by itself, which is 7-bit ASCII -- the problem is not ASCII, rather the 7-bit restriction. I believe there is a bug open that suggests changing this fallback to UTF-8, which would solve this problem.).
The only time I saw that was with rear which did all of the above on purpose to save like 5 MB on the generated images. In all other cases there is no problem with any kind of file name.
Note that this is a severely broken system configuration where a lot of other stuff falls apart as well.
Python 3 is different but not necessarily better in this regard. While I hated the encode/decode on .. every .. single .. function call, py3 pretends a lot of bytestreams are instead unicode (file and path names, for example). This can lead to bugs. It's not that it wasn't worth it, but it could have been done a lot better.
> Which is probably also why Perl6 choose to go the same route
Not at all. Perl 5 did unicode right. The difference between character, glyph, code point and byte is clear and uniform throughout the entire stack. Python could have copied this and it would have been awesome.