
30.03.20 20:07, Andrew Barnert via Python-ideas пише:
Sadly, this isn’t possible. Large amounts of C code—including builtins and stdlib—won’t let you duck type as a string; as it will do a type check and expect an actual str (and if you subclass str, it will ignore your methods and use the PyUnicode APIs to get your base class’s storage directly as a buffer instead). So, no type, either C or Python, can really be a drop-in replacement for str. At best you can have something that you have to call str() on half the time.
I agree with this. It is not possible with the current PyUnicode implementation and the current C API. And even if we can make it possible for most cases, it will significantly complicate the code and the benefit will likely be not worth the cost.
That’s why there’s no MutableStr on PyPI, and no UTF8Str, no EncodedStr that can act as both a bytes and a str by remembering its encoding (Nick Coghlan’s motivating example for changing this back in the early 3.x days), etc.
It is not so hard to implement EncodedStr (but it will look not like you expect). I was going to add it and did some preparations which make it possible. You have just to add the __bytes__ method to string subclass to make bytes(encoded_str) working (it might be enough for my purposes). Or add support of the buffer protocol if you want larger compatibility with bytes, but you can not do this in pure Python. I abandoned this idea because the need (compatibility with some Python 2 pickles) was not large.