[PATCH] D37946: [lit] Fix some Python 3 compatibility issues.
Zachary Turner via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 18 19:34:28 PDT 2017
zturner added inline comments.
================
Comment at: llvm/utils/lit/lit/TestRunner.py:286
+
+ arg = lit.util.to_bytes(arg)
+ codec = 'string_escape' if sys.version_info < (3,0) else 'unicode_escape'
----------------
dlj wrote:
> 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?
No, because `to_string` literally just converts from bytes to string. `string_escape` is kind of a non-standard codec. It literally just adds or removes backslashes as necessary in order to convert between a python string literal and the actual value. For example, if you encode `a\nb` using `string_escape`, then you're saying "I have the actual string literal `a\nb`, give me something that I could use to write that string literal in Python. So it returns `a\\nb`.
On the other hand, if you //decode// `a\nb` using the `string_escape` codec, you're telling it that the string is already escaped, and you want to know what it would look like if printed. So it returns
```
a
b
```
We need this logic here because we're literally parsing a command line, and we're mimicing the shell's escaping rules. This way a person can write `echo "foo\tbar"` and it will actually print `foo<tab>bar`
(Hopefully I got that explanation right)
https://reviews.llvm.org/D37946
More information about the llvm-commits
mailing list