Python 3 support for Python scripts (without compromising Python 2)

It’s better to be prepared for the future than…to not be.

This is mostly the result of running 2to3 on all .py files, with some
small shims to maintain compatibility with Python 2.
http_transport_test_server.py was slightly more involved, requiring many
objects to change from “str” to “bytes”.

The #! lines and invokers still haven’t changed, so these scripts will
still normally be interpreted by Python 2.

Change-Id: Idda3c5650f967401a5942c4d8abee86151642a2e
Reviewed-on: https://chromium-review.googlesource.com/797434
Reviewed-by: Robert Sesek <rsesek@chromium.org>
Commit-Queue: Mark Mentovai <mark@chromium.org>
This commit is contained in:
Mark Mentovai 2017-11-29 13:26:55 -05:00 committed by Commit Bot
parent 1ee9fee37c
commit ef3ce94fbf
5 changed files with 80 additions and 45 deletions

View File

@ -17,6 +17,9 @@
import os import os
import sys import sys
if sys.version_info[0] < 3:
range = xrange
def ChooseDependencyPath(local_path, external_path): def ChooseDependencyPath(local_path, external_path):
"""Chooses between a dependency located at local path and an external path. """Chooses between a dependency located at local path and an external path.
@ -78,7 +81,7 @@ def main(args):
# Check to make sure that no target_arch was specified. target_arch may be # Check to make sure that no target_arch was specified. target_arch may be
# set during a cross build, such as a cross build for Android. # set during a cross build, such as a cross build for Android.
has_target_arch = False has_target_arch = False
for arg_index in xrange(0, len(args)): for arg_index in range(0, len(args)):
arg = args[arg_index] arg = args[arg_index]
if (arg.startswith('-Dtarget_arch=') or if (arg.startswith('-Dtarget_arch=') or
(arg == '-D' and arg_index + 1 < len(args) and (arg == '-D' and arg_index + 1 < len(args) and

View File

@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from __future__ import print_function
import os import os
import subprocess import subprocess
import sys import sys
@ -25,7 +27,7 @@ import sys
# location in the recipe. # location in the recipe.
def main(args): def main(args):
if len(args) != 1: if len(args) != 1:
print >> sys.stderr, 'usage: run_tests.py <binary_dir>' print('usage: run_tests.py <binary_dir>', file=sys.stderr)
return 1 return 1
crashpad_dir = \ crashpad_dir = \
@ -54,16 +56,16 @@ def main(args):
] ]
for test in tests: for test in tests:
print '-' * 80 print('-' * 80)
print test print(test)
print '-' * 80 print('-' * 80)
subprocess.check_call(os.path.join(binary_dir, test)) subprocess.check_call(os.path.join(binary_dir, test))
if sys.platform == 'win32': if sys.platform == 'win32':
script = 'snapshot/win/end_to_end_test.py' script = 'snapshot/win/end_to_end_test.py'
print '-' * 80 print('-' * 80)
print script print(script)
print '-' * 80 print('-' * 80)
subprocess.check_call( subprocess.check_call(
[sys.executable, os.path.join(crashpad_dir, script), binary_dir]) [sys.executable, os.path.join(crashpad_dir, script), binary_dir])

View File

@ -37,7 +37,7 @@ def main(args):
elif os.path.exists(output_dir): elif os.path.exists(output_dir):
os.unlink(output_dir) os.unlink(output_dir)
os.makedirs(output_dir, 0755) os.makedirs(output_dir, 0o755)
doxy_file = os.path.join('doc', 'support', 'crashpad.doxy') doxy_file = os.path.join('doc', 'support', 'crashpad.doxy')
subprocess.check_call(['doxygen', doxy_file]) subprocess.check_call(['doxygen', doxy_file])

View File

@ -14,6 +14,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from __future__ import print_function
import os import os
import platform import platform
import pywintypes import pywintypes
@ -99,7 +101,7 @@ def NamedPipeExistsAndReady(pipe_name):
""" """
try: try:
win32pipe.WaitNamedPipe(pipe_name, win32pipe.NMPWAIT_WAIT_FOREVER) win32pipe.WaitNamedPipe(pipe_name, win32pipe.NMPWAIT_WAIT_FOREVER)
except pywintypes.error, e: except pywintypes.error as e:
if e[0] == winerror.ERROR_FILE_NOT_FOUND: if e[0] == winerror.ERROR_FILE_NOT_FOUND:
return False return False
raise raise
@ -135,7 +137,7 @@ def GetDumpFromProgram(
printed = False printed = False
while not NamedPipeExistsAndReady(pipe_name): while not NamedPipeExistsAndReady(pipe_name):
if not printed: if not printed:
print 'Waiting for crashpad_handler to be ready...' print('Waiting for crashpad_handler to be ready...')
printed = True printed = True
time.sleep(0.001) time.sleep(0.001)
@ -145,7 +147,7 @@ def GetDumpFromProgram(
os.path.join(out_dir, 'crashpad_handler.com'), os.path.join(out_dir, 'crashpad_handler.com'),
test_database] + test_database] +
list(args)) list(args))
print 'Running %s' % os.path.basename(command[0]) print('Running %s' % os.path.basename(command[0]))
exit_code = subprocess.call(command) exit_code = subprocess.call(command)
if exit_code != expect_exit_code: if exit_code != expect_exit_code:
raise subprocess.CalledProcessError(exit_code, executable_name) raise subprocess.CalledProcessError(exit_code, executable_name)
@ -219,16 +221,16 @@ class CdbRun(object):
if match_obj: if match_obj:
# Matched. Consume up to end of match. # Matched. Consume up to end of match.
self.out = self.out[match_obj.end(0):] self.out = self.out[match_obj.end(0):]
print 'ok - %s' % message print('ok - %s' % message)
sys.stdout.flush() sys.stdout.flush()
else: else:
print >>sys.stderr, '-' * 80 print('-' * 80, file=sys.stderr)
print >>sys.stderr, 'FAILED - %s' % message print('FAILED - %s' % message, file=sys.stderr)
print >>sys.stderr, '-' * 80 print('-' * 80, file=sys.stderr)
print >>sys.stderr, 'did not match:\n %s' % pattern print('did not match:\n %s' % pattern, file=sys.stderr)
print >>sys.stderr, '-' * 80 print('-' * 80, file=sys.stderr)
print >>sys.stderr, 'remaining output was:\n %s' % self.out print('remaining output was:\n %s' % self.out, file=sys.stderr)
print >>sys.stderr, '-' * 80 print('-' * 80, file=sys.stderr)
sys.stderr.flush() sys.stderr.flush()
global g_had_failures global g_had_failures
g_had_failures = True g_had_failures = True
@ -430,12 +432,12 @@ def RunTests(cdb_path,
def main(args): def main(args):
try: try:
if len(args) != 1: if len(args) != 1:
print >>sys.stderr, 'must supply binary dir' print('must supply binary dir', file=sys.stderr)
return 1 return 1
cdb_path = GetCdbPath() cdb_path = GetCdbPath()
if not cdb_path: if not cdb_path:
print >>sys.stderr, 'could not find cdb' print('could not find cdb', file=sys.stderr)
return 1 return 1
# Make sure we can download Windows symbols. # Make sure we can download Windows symbols.

View File

@ -30,17 +30,25 @@ because parsing chunked encoding is safer and easier in a memory-safe language.
This could easily have been written in C++ instead. This could easily have been written in C++ instead.
""" """
import BaseHTTPServer import os
import struct import struct
import sys import sys
import zlib import zlib
if sys.platform == 'win32':
import msvcrt
if sys.version_info[0] < 3:
import BaseHTTPServer as http_server
else:
import http.server as http_server
class BufferedReadFile(object): class BufferedReadFile(object):
"""A File-like object that stores all read contents into a buffer.""" """A File-like object that stores all read contents into a buffer."""
def __init__(self, real_file): def __init__(self, real_file):
self.file = real_file self.file = real_file
self.buffer = "" self.buffer = b''
def read(self, size=-1): def read(self, size=-1):
buf = self.file.read(size) buf = self.file.read(size)
@ -59,27 +67,27 @@ class BufferedReadFile(object):
self.file.close() self.file.close()
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): class RequestHandler(http_server.BaseHTTPRequestHandler):
# Everything to be written to stdout is collected into this string. It cant # Everything to be written to stdout is collected into this string. It cant
# be written to stdout until after the HTTP transaction is complete, because # be written to stdout until after the HTTP transaction is complete, because
# stdout is a pipe being read by a test program thats also the HTTP client. # stdout is a pipe being read by a test program thats also the HTTP client.
# The test program expects to complete the entire HTTP transaction before it # The test program expects to complete the entire HTTP transaction before it
# even starts reading this scripts stdout. If the stdout pipe buffer fills up # even starts reading this scripts stdout. If the stdout pipe buffer fills up
# during an HTTP transaction, deadlock would result. # during an HTTP transaction, deadlock would result.
raw_request = '' raw_request = b''
response_code = 500 response_code = 500
response_body = '' response_body = b''
def handle_one_request(self): def handle_one_request(self):
# Wrap the rfile in the buffering file object so that the raw header block # Wrap the rfile in the buffering file object so that the raw header block
# can be written to stdout after it is parsed. # can be written to stdout after it is parsed.
self.rfile = BufferedReadFile(self.rfile) self.rfile = BufferedReadFile(self.rfile)
BaseHTTPServer.BaseHTTPRequestHandler.handle_one_request(self) http_server.BaseHTTPRequestHandler.handle_one_request(self)
def do_POST(self): def do_POST(self):
RequestHandler.raw_request = self.rfile.buffer RequestHandler.raw_request = self.rfile.buffer
self.rfile.buffer = '' self.rfile.buffer = b''
if self.headers.get('Transfer-Encoding', '').lower() == 'chunked': if self.headers.get('Transfer-Encoding', '').lower() == 'chunked':
if 'Content-Length' in self.headers: if 'Content-Length' in self.headers:
@ -102,13 +110,13 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
self.end_headers() self.end_headers()
if self.response_code == 200: if self.response_code == 200:
self.wfile.write(self.response_body) self.wfile.write(self.response_body)
self.wfile.write('\r\n') self.wfile.write(b'\r\n')
def handle_chunked_encoding(self): def handle_chunked_encoding(self):
"""This parses a "Transfer-Encoding: Chunked" body in accordance with """This parses a "Transfer-Encoding: Chunked" body in accordance with RFC
RFC 7230 §4.1. This returns the result as a string. 7230 §4.1. This returns the result as a string.
""" """
body = '' body = b''
chunk_size = self.read_chunk_size() chunk_size = self.read_chunk_size()
while chunk_size > 0: while chunk_size > 0:
# Read the body. # Read the body.
@ -120,7 +128,7 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
if chunk_size == 0: if chunk_size == 0:
# Read through any trailer fields. # Read through any trailer fields.
trailer_line = self.rfile.readline() trailer_line = self.rfile.readline()
while trailer_line.strip() != '': while trailer_line.strip() != b'':
trailer_line = self.rfile.readline() trailer_line = self.rfile.readline()
# Read the chunk size. # Read the chunk size.
@ -131,10 +139,10 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
# Read the whole line, including the \r\n. # Read the whole line, including the \r\n.
chunk_size_and_ext_line = self.rfile.readline() chunk_size_and_ext_line = self.rfile.readline()
# Look for a chunk extension. # Look for a chunk extension.
chunk_size_end = chunk_size_and_ext_line.find(';') chunk_size_end = chunk_size_and_ext_line.find(b';')
if chunk_size_end == -1: if chunk_size_end == -1:
# No chunk extensions; just encounter the end of line. # No chunk extensions; just encounter the end of line.
chunk_size_end = chunk_size_and_ext_line.find('\r') chunk_size_end = chunk_size_and_ext_line.find(b'\r')
if chunk_size_end == -1: if chunk_size_end == -1:
self.send_response(400) # Bad request. self.send_response(400) # Bad request.
return -1 return -1
@ -145,29 +153,49 @@ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
pass pass
def StdioBinaryEquivalent(file):
"""Return a file object equivalent to sys.stdin or sys.stdout capable of
reading or writing binary bytes.
struct.unpack consumes bytes, and struct.pack produces bytes. These are
distinct from str in Python 3 (but not 2). In order to read and write these
from stdin and stdout, the underlying binary buffer must be used in place of
the upper-layer text wrapper. This function returns a suitable file.
There is no underlying buffer in Python 2, but on Windows, the file mode must
still be set to binary in order to cleanly pass binary data. Note that in this
case, the mode of |file| itself is changed, as its not distinct from the
returned file.
"""
if hasattr(file, 'buffer'):
file = file.buffer
elif sys.platform == 'win32':
msvcrt.setmode(file.fileno(), os.O_BINARY)
return file
def Main(): def Main():
if sys.platform == 'win32': in_file = StdioBinaryEquivalent(sys.stdin)
import os, msvcrt out_file = StdioBinaryEquivalent(sys.stdout)
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
# Start the server. # Start the server.
server = BaseHTTPServer.HTTPServer(('127.0.0.1', 0), RequestHandler) server = http_server.HTTPServer(('127.0.0.1', 0), RequestHandler)
# Write the port as an unsigned short to the parent process. # Write the port as an unsigned short to the parent process.
sys.stdout.write(struct.pack('=H', server.server_address[1])) out_file.write(struct.pack('=H', server.server_address[1]))
sys.stdout.flush() out_file.flush()
# Read the desired test response code as an unsigned short and the desired # Read the desired test response code as an unsigned short and the desired
# response body as a 16-byte string from the parent process. # response body as a 16-byte string from the parent process.
RequestHandler.response_code, RequestHandler.response_body = \ RequestHandler.response_code, RequestHandler.response_body = \
struct.unpack('=H16s', sys.stdin.read(struct.calcsize('=H16s'))) struct.unpack('=H16s', in_file.read(struct.calcsize('=H16s')))
# Handle the request. # Handle the request.
server.handle_request() server.handle_request()
# Share the entire request with the test program, which will validate it. # Share the entire request with the test program, which will validate it.
sys.stdout.write(RequestHandler.raw_request) out_file.write(RequestHandler.raw_request)
sys.stdout.flush() out_file.flush()
if __name__ == '__main__': if __name__ == '__main__':
Main() Main()