Improve gclient Python 3 compatibility
This enables gclient sync and gclient runhooks to run, barring hook script failures.
git cl upload also now works.
The scripts still work with Python 2.
There are no intended behaviour changes.
Bug: 942522
Change-Id: I2ac587b5f803ba7f5bb5e412337ce049f4b1a741
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1524583
Commit-Queue: Raul Tambre <raul@tambre.ee>
Reviewed-by: Dirk Pranke <dpranke@chromium.org>
diff --git a/subprocess2.py b/subprocess2.py
index d899e47..6ec6d50 100644
--- a/subprocess2.py
+++ b/subprocess2.py
@@ -7,12 +7,17 @@
In theory you shouldn't need anything else in subprocess, or this module failed.
"""
-import cStringIO
import codecs
import errno
+import io
import logging
import os
-import Queue
+
+try:
+ import Queue
+except ImportError: # For Py3 compatibility
+ import queue as Queue
+
import subprocess
import sys
import time
@@ -20,7 +25,8 @@
# Cache the string-escape codec to ensure subprocess can find it later.
# See crbug.com/912292#c2 for context.
-codecs.lookup('string-escape')
+if sys.version_info.major == 2:
+ codecs.lookup('string-escape')
# Constants forwarded from subprocess.
PIPE = subprocess.PIPE
@@ -208,7 +214,8 @@
# the list.
kwargs['shell'] = bool(sys.platform=='win32')
- if isinstance(args, basestring):
+ if isinstance(args, str) or (sys.version_info.major == 2 and
+ isinstance(args, unicode)):
tmp_str = args
elif isinstance(args, (list, tuple)):
tmp_str = ' '.join(args)
@@ -248,7 +255,7 @@
try:
with self.popen_lock:
super(Popen, self).__init__(args, **kwargs)
- except OSError, e:
+ except OSError as e:
if e.errno == errno.EAGAIN and sys.platform == 'cygwin':
# Convert fork() emulation failure into a CygwinRebaseError().
raise CygwinRebaseError(
@@ -285,7 +292,7 @@
def write_stdin():
try:
- stdin_io = cStringIO.StringIO(input)
+ stdin_io = io.BytesIO(input)
while True:
data = stdin_io.read(1024)
if data:
@@ -451,7 +458,8 @@
"""
stdin = kwargs.pop('stdin', None)
if stdin is not None:
- if isinstance(stdin, basestring):
+ if isinstance(stdin, str) or (sys.version_info.major == 2 and
+ isinstance(stdin, unicode)):
# When stdin is passed as an argument, use it as the actual input data and
# set the Popen() parameter accordingly.
kwargs['stdin'] = PIPE