Use "backslashreplace" instead of "unicode-escape". It is not implementation depended and escapes only non-encodable characters. Also simplify the code. (cherry picked from commit 4b860fd)
4b860fd