r350318 - Portable Python script across Python version

Serge Guelton via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 3 06:26:57 PST 2019


Author: serge_sans_paille
Date: Thu Jan  3 06:26:56 2019
New Revision: 350318

URL: http://llvm.org/viewvc/llvm-project?rev=350318&view=rev
Log:
Portable Python script across Python version

StringIO is obsoleted in Python3, replaced by io.BytesIO or io.StringIO depending on the use.

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

Modified:
    cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py
    cfe/trunk/tools/clang-format/clang-format-diff.py
    cfe/trunk/tools/scan-view/share/ScanView.py

Modified: cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py?rev=350318&r1=350317&r2=350318&view=diff
==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py (original)
+++ cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py Thu Jan  3 06:26:56 2019
@@ -6,6 +6,7 @@ if 'CLANG_LIBRARY_PATH' in os.environ:
 from contextlib import contextmanager
 import gc
 import os
+import sys
 import tempfile
 import unittest
 
@@ -93,10 +94,10 @@ int SOME_DEFINE;
         self.assertEqual(spellings[-1], 'y')
 
     def test_unsaved_files_2(self):
-        try:
-            from StringIO import StringIO
-        except:
+        if sys.version_info.major >= 3:
             from io import StringIO
+        else:
+            from io import BytesIO as StringIO
         tu = TranslationUnit.from_source('fake.c', unsaved_files = [
                 ('fake.c', StringIO('int x;'))])
         spellings = [c.spelling for c in tu.cursor.get_children()]

Modified: cfe/trunk/tools/clang-format/clang-format-diff.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/clang-format-diff.py?rev=350318&r1=350317&r2=350318&view=diff
==============================================================================
--- cfe/trunk/tools/clang-format/clang-format-diff.py (original)
+++ cfe/trunk/tools/clang-format/clang-format-diff.py Thu Jan  3 06:26:56 2019
@@ -28,10 +28,11 @@ import difflib
 import re
 import subprocess
 import sys
-try:
-  from StringIO import StringIO
-except ImportError:
-   from io import StringIO
+
+if sys.version_info.major >= 3:
+    from io import StringIO
+else:
+    from io import BytesIO as StringIO
 
 
 def main():

Modified: cfe/trunk/tools/scan-view/share/ScanView.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/scan-view/share/ScanView.py?rev=350318&r1=350317&r2=350318&view=diff
==============================================================================
--- cfe/trunk/tools/scan-view/share/ScanView.py (original)
+++ cfe/trunk/tools/scan-view/share/ScanView.py Thu Jan  3 06:26:56 2019
@@ -13,7 +13,12 @@ except ImportError:
     from urllib.parse import urlparse, unquote
 
 import posixpath
-import StringIO
+
+if sys.version_info.major >= 3:
+    from io import StringIO, BytesIO
+else:
+    from io import BytesIO, BytesIO as StringIO
+
 import re
 import shutil
 import threading
@@ -117,7 +122,7 @@ class ReporterThread(threading.Thread):
         except Reporter.ReportFailure as e:
             self.status = e.value
         except Exception as e:
-            s = StringIO.StringIO()
+            s = StringIO()
             import traceback
             print('<b>Unhandled Exception</b><br><pre>', file=s)
             traceback.print_exc(file=s)
@@ -275,7 +280,7 @@ class ScanViewRequestHandler(SimpleHTTPR
 
     def handle_exception(self, exc):
         import traceback
-        s = StringIO.StringIO()
+        s = StringIO()
         print("INTERNAL ERROR\n", file=s)
         traceback.print_exc(file=s)
         f = self.send_string(s.getvalue(), 'text/plain')
@@ -739,15 +744,16 @@ File Bug</h3>
         return f
 
     def send_string(self, s, ctype='text/html', headers=True, mtime=None):
+        encoded_s = s.encode()
         if headers:
             self.send_response(200)
             self.send_header("Content-type", ctype)
-            self.send_header("Content-Length", str(len(s)))
+            self.send_header("Content-Length", str(len(encoded_s)))
             if mtime is None:
                 mtime = self.dynamic_mtime
             self.send_header("Last-Modified", self.date_time_string(mtime))
             self.end_headers()
-        return StringIO.StringIO(s)
+        return BytesIO(encoded_s)
 
     def send_patched_file(self, path, ctype):
         # Allow a very limited set of variables. This is pretty gross.




More information about the cfe-commits mailing list