It seems we already have bots running lit under Python 3, so as I'm making more changes, I need to test under Python 3. Unfortunately, there are some Windows differences that this bot doesn't exercise, and so I can't verify that my changes work without getting the suite Python 3 clean on Windows.
There were two main problems, both having to do with str / bytes compatibility.
- On Windows we sometimes re-open stdout as binary instead of text. But this means we have to write bytes instead of str in Py3.
- We were using str.decode('string_escape'), but a) string_escape doesn't even exist in Py3 (you have to use unicode_escape), and b) you can't decode a str in Py3, you can only decode a bytes.
The patch here solves this by:
a) providing a wrapper that takes a str and encodes as utf8 bytes if and only if we re-opened stdout in binary mode.
b) Making the unescaper normalize on bytes up front, then decoding as either string_escape or unicode_escape depending on the version. The result is always a str, which can then be passed to the function in a).
After this patch there's only 1 test still failing for me under Python 3, but it's non critical to verifying that my future patches don't break anything.
So I have to admit that here, I'm not sure exactly what corner cases are problematic... but would lit.util.to_string give you the correct result?