Skip to content

Commit

Permalink
Portable Python script across Python version
Browse files Browse the repository at this point in the history
Using from __future__ import print_function it is possible to have a compatible behavior of `print(...)` across Python version.

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

llvm-svn: 349454
  • Loading branch information
serge-sans-paille-qb committed Dec 18, 2018
1 parent 8583339 commit c0ebe77
Show file tree
Hide file tree
Showing 21 changed files with 184 additions and 164 deletions.
1 change: 1 addition & 0 deletions clang/bindings/python/clang/cindex.py
Expand Up @@ -44,6 +44,7 @@
Most object information is exposed using properties, when the underlying API
call is efficient.
"""
from __future__ import print_function

# TODO
# ====
Expand Down
9 changes: 5 additions & 4 deletions clang/docs/conf.py
Expand Up @@ -11,6 +11,7 @@
# All configuration values have a default; values that are commented out
# serve to show the default.

from __future__ import print_function
import sys, os
from datetime import date

Expand Down Expand Up @@ -233,14 +234,14 @@
header = f.readline().rstrip('\n')

if len(header) != len(title):
print >>sys.stderr, (
print((
"error: invalid header in %r (does not match title)" % (
file_subpath,))
file_subpath,)), file=sys.stderr)
if ' - ' not in title:
print >>sys.stderr, (
print((
("error: invalid title in %r "
"(expected '<name> - <description>')") % (
file_subpath,))
file_subpath,)), file=sys.stderr)

# Split the name out of the title.
name,description = title.split(' - ', 1)
Expand Down
8 changes: 4 additions & 4 deletions clang/docs/tools/dump_ast_matchers.py
Expand Up @@ -41,7 +41,7 @@ def link_if_exists(m):
url = 'https://clang.llvm.org/doxygen/classclang_1_1%s.html' % name
if url not in doxygen_probes:
try:
print 'Probing %s...' % url
print('Probing %s...' % url)
urllib2.urlopen(url)
doxygen_probes[url] = True
except:
Expand Down Expand Up @@ -307,14 +307,14 @@ def act_on_decl(declaration, comment, allowed_types):
if not result_types:
if not comment:
# Only overloads don't have their own doxygen comments; ignore those.
print 'Ignoring "%s"' % name
print('Ignoring "%s"' % name)
else:
print 'Cannot determine result type for "%s"' % name
print('Cannot determine result type for "%s"' % name)
else:
for result_type in result_types:
add_matcher(result_type, name, args, comment)
else:
print '*** Unparsable: "' + declaration + '" ***'
print('*** Unparsable: "' + declaration + '" ***')

def sort_table(matcher_type, matcher_map):
"""Returns the sorted html table for the given row map."""
Expand Down
1 change: 1 addition & 0 deletions clang/tools/clang-format/clang-format-diff.py
Expand Up @@ -21,6 +21,7 @@
svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i
"""
from __future__ import print_function

import argparse
import difflib
Expand Down
1 change: 1 addition & 0 deletions clang/tools/scan-build-py/libscanbuild/arguments.py
Expand Up @@ -12,6 +12,7 @@
It also implements basic validation methods, related to the command.
Validations are mostly calling specific help methods, or mangling values.
"""
from __future__ import print_function

import os
import sys
Expand Down
12 changes: 7 additions & 5 deletions clang/tools/scan-view/bin/scan-view
@@ -1,5 +1,7 @@
#!/usr/bin/env python

from __future__ import print_function

"""The clang static analyzer results viewer.
"""

Expand Down Expand Up @@ -52,10 +54,10 @@ def start_browser(port, options):
sys.stderr.flush()
time.sleep(kSleepTimeout)
else:
print >> sys.stderr, 'WARNING: Unable to detect that server started.'
print('WARNING: Unable to detect that server started.', file=sys.stderr)

if options.debug:
print >> sys.stderr, '%s: Starting webbrowser...' % sys.argv[0]
print('%s: Starting webbrowser...' % sys.argv[0], file=sys.stderr)
webbrowser.open(url)


Expand All @@ -69,9 +71,9 @@ def run(port, options, root):

import ScanView
try:
print 'Starting scan-view at: http://%s:%d' % (options.host,
port)
print ' Use Ctrl-C to exit.'
print('Starting scan-view at: http://%s:%d' % (options.host,
port))
print(' Use Ctrl-C to exit.')
httpd = ScanView.create_server((options.host, port),
options, root)
httpd.serve_forever()
Expand Down
31 changes: 16 additions & 15 deletions clang/tools/scan-view/share/ScanView.py
@@ -1,3 +1,4 @@
from __future__ import print_function
try:
from http.server import HTTPServer, SimpleHTTPRequestHandler
except ImportError:
Expand Down Expand Up @@ -102,20 +103,20 @@ def run(self):
result = None
try:
if self.server.options.debug:
print >>sys.stderr, "%s: SERVER: submitting bug."%(sys.argv[0],)
print("%s: SERVER: submitting bug."%(sys.argv[0],), file=sys.stderr)
self.status = self.reporter.fileReport(self.report, self.parameters)
self.success = True
time.sleep(3)
if self.server.options.debug:
print >>sys.stderr, "%s: SERVER: submission complete."%(sys.argv[0],)
print("%s: SERVER: submission complete."%(sys.argv[0],), file=sys.stderr)
except Reporter.ReportFailure as e:
self.status = e.value
except Exception as e:
s = StringIO.StringIO()
import traceback
print >>s,'<b>Unhandled Exception</b><br><pre>'
traceback.print_exc(e,file=s)
print >>s,'</pre>'
print('<b>Unhandled Exception</b><br><pre>', file=s)
traceback.print_exc(file=s)
print('</pre>', file=s)
self.status = s.getvalue()

class ScanViewServer(HTTPServer):
Expand Down Expand Up @@ -161,16 +162,16 @@ def save_config(self):
def halt(self):
self.halted = True
if self.options.debug:
print >>sys.stderr, "%s: SERVER: halting." % (sys.argv[0],)
print("%s: SERVER: halting." % (sys.argv[0],), file=sys.stderr)

def serve_forever(self):
while not self.halted:
if self.options.debug > 1:
print >>sys.stderr, "%s: SERVER: waiting..." % (sys.argv[0],)
print("%s: SERVER: waiting..." % (sys.argv[0],), file=sys.stderr)
try:
self.handle_request()
except OSError as e:
print 'OSError',e.errno
print('OSError',e.errno)

def finish_request(self, request, client_address):
if self.options.autoReload:
Expand All @@ -183,7 +184,7 @@ def handle_error(self, request, client_address):
info = sys.exc_info()
if info and isinstance(info[1], socket.error):
if self.options.debug > 1:
print >>sys.stderr, "%s: SERVER: ignored socket error." % (sys.argv[0],)
print("%s: SERVER: ignored socket error." % (sys.argv[0],), file=sys.stderr)
return
HTTPServer.handle_error(self, request, client_address)

Expand Down Expand Up @@ -270,8 +271,8 @@ def load_crashes(self):
def handle_exception(self, exc):
import traceback
s = StringIO.StringIO()
print >>s, "INTERNAL ERROR\n"
traceback.print_exc(exc, s)
print("INTERNAL ERROR\n", file=s)
traceback.print_exc(file=s)
f = self.send_string(s.getvalue(), 'text/plain')
if f:
self.copyfile(f, self.wfile)
Expand Down Expand Up @@ -416,8 +417,8 @@ def send_open_report(self, report):

import startfile
if self.server.options.debug:
print >>sys.stderr, '%s: SERVER: opening "%s"'%(sys.argv[0],
file)
print('%s: SERVER: opening "%s"'%(sys.argv[0],
file), file=sys.stderr)

status = startfile.open(file)
if status:
Expand Down Expand Up @@ -696,8 +697,8 @@ def send_head(self, fields=None):
path = posixpath.join(self.server.root, relpath)

if self.server.options.debug > 1:
print >>sys.stderr, '%s: SERVER: sending path "%s"'%(sys.argv[0],
path)
print('%s: SERVER: sending path "%s"'%(sys.argv[0],
path), file=sys.stderr)
return self.send_path(path)

def send_404(self):
Expand Down

0 comments on commit c0ebe77

Please sign in to comment.