[PATCH] D45077: 'cat' command for internal shell - Support Python 3

Reid Kleckner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 3 15:44:02 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL329123: 'cat' command for internal shell - Support Python 3 (authored by rnk, committed by ).
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D45077?vs=140529&id=140870#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D45077

Files:
  llvm/trunk/utils/lit/lit/builtin_commands/cat.py


Index: llvm/trunk/utils/lit/lit/builtin_commands/cat.py
===================================================================
--- llvm/trunk/utils/lit/lit/builtin_commands/cat.py
+++ llvm/trunk/utils/lit/lit/builtin_commands/cat.py
@@ -7,8 +7,10 @@
 
 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 @@
        else:
            newdata.write(chr(intval))
 
-   return newdata.getvalue();
+   return newdata.getvalue().encode()
 
 
 def main(argv):
@@ -42,13 +44,19 @@
         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:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45077.140870.patch
Type: text/x-patch
Size: 1471 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180403/4c5cabc9/attachment.bin>


More information about the llvm-commits mailing list