OSDN Git Service

'cat' command for internal shell - Support Python 3
authorReid Kleckner <rnk@google.com>
Tue, 3 Apr 2018 22:38:25 +0000 (22:38 +0000)
committerReid Kleckner <rnk@google.com>
Tue, 3 Apr 2018 22:38:25 +0000 (22:38 +0000)
LLVM Bug Id : 36449

Revision 328563 caused tests to fail under python 3.

This patch modified cat.py file to support both python 2 and 3.
This patch also fixes CRLF issues on Windows.

Patch by Chamal de Silva

Differential Revision: https://reviews.llvm.org/D45077

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@329123 91177308-0d34-0410-b5e6-96231b3b80d8

utils/lit/lit/builtin_commands/cat.py

index b4d3d99..fab9dcc 100644 (file)
@@ -7,8 +7,10 @@ except ImportError:
 
 def convertToCaretAndMNotation(data):
    newdata = StringIO()
-   for char in data:
-       intval = ord(char)
+   if isinstance(data, str):
+       data = bytearray(data)
+
+   for intval in data:
        if intval == 9 or intval == 10:
            newdata.write(chr(intval))
            continue
@@ -23,7 +25,7 @@ def convertToCaretAndMNotation(data):
        else:
            newdata.write(chr(intval))
 
-   return newdata.getvalue();
+   return newdata.getvalue().encode()
 
 
 def main(argv):
@@ -42,13 +44,19 @@ def main(argv):
         if option == "-v" or option == "--show-nonprinting":
             show_nonprinting = True;
 
+    writer = getattr(sys.stdout, 'buffer', None)
+    if writer is None:
+        writer = sys.stdout
+        if sys.platform == "win32":
+            import os, msvcrt
+            msvcrt.setmode(sys.stdout.fileno(),os.O_BINARY)
     for filename in filenames:
         try:
             fileToCat = open(filename,"rb")
             contents = fileToCat.read()
             if show_nonprinting:
                 contents = convertToCaretAndMNotation(contents)
-            sys.stdout.write(contents)
+            writer.write(contents)
             sys.stdout.flush()
             fileToCat.close()
         except IOError as error: