Skip to content

Commit ff5ffd4

Browse files
committed
Merge pull request google#688 from tamland/python3
Python 3 support
2 parents a9b73f8 + 456fc2b commit ff5ffd4

9 files changed

+36
-29
lines changed

googletest/scripts/fuse_gtest_files.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@
6060

6161
import os
6262
import re
63-
import sets
63+
try:
64+
from sets import Set as set # For Python 2.3 compatibility
65+
except ImportError:
66+
pass
6467
import sys
6568

6669
# We assume that this file is in the scripts/ directory in the Google
@@ -90,10 +93,10 @@ def VerifyFileExists(directory, relative_path):
9093
"""
9194

9295
if not os.path.isfile(os.path.join(directory, relative_path)):
93-
print 'ERROR: Cannot find %s in directory %s.' % (relative_path,
94-
directory)
95-
print ('Please either specify a valid project root directory '
96-
'or omit it on the command line.')
96+
print('ERROR: Cannot find %s in directory %s.' % (relative_path,
97+
directory))
98+
print('Please either specify a valid project root directory '
99+
'or omit it on the command line.')
97100
sys.exit(1)
98101

99102

@@ -119,11 +122,11 @@ def VerifyOutputFile(output_dir, relative_path):
119122
# TODO([email protected]): The following user-interaction doesn't
120123
# work with automated processes. We should provide a way for the
121124
# Makefile to force overwriting the files.
122-
print ('%s already exists in directory %s - overwrite it? (y/N) ' %
123-
(relative_path, output_dir))
125+
print('%s already exists in directory %s - overwrite it? (y/N) ' %
126+
(relative_path, output_dir))
124127
answer = sys.stdin.readline().strip()
125128
if answer not in ['y', 'Y']:
126-
print 'ABORTED.'
129+
print('ABORTED.')
127130
sys.exit(1)
128131

129132
# Makes sure the directory holding the output file exists; creates
@@ -146,8 +149,8 @@ def ValidateOutputDir(output_dir):
146149
def FuseGTestH(gtest_root, output_dir):
147150
"""Scans folder gtest_root to generate gtest/gtest.h in output_dir."""
148151

149-
output_file = file(os.path.join(output_dir, GTEST_H_OUTPUT), 'w')
150-
processed_files = sets.Set() # Holds all gtest headers we've processed.
152+
output_file = open(os.path.join(output_dir, GTEST_H_OUTPUT), 'w')
153+
processed_files = set() # Holds all gtest headers we've processed.
151154

152155
def ProcessFile(gtest_header_path):
153156
"""Processes the given gtest header file."""
@@ -159,7 +162,7 @@ def ProcessFile(gtest_header_path):
159162
processed_files.add(gtest_header_path)
160163

161164
# Reads each line in the given gtest header.
162-
for line in file(os.path.join(gtest_root, gtest_header_path), 'r'):
165+
for line in open(os.path.join(gtest_root, gtest_header_path), 'r'):
163166
m = INCLUDE_GTEST_FILE_REGEX.match(line)
164167
if m:
165168
# It's '#include "gtest/..."' - let's process it recursively.
@@ -175,7 +178,7 @@ def ProcessFile(gtest_header_path):
175178
def FuseGTestAllCcToFile(gtest_root, output_file):
176179
"""Scans folder gtest_root to generate gtest/gtest-all.cc in output_file."""
177180

178-
processed_files = sets.Set()
181+
processed_files = set()
179182

180183
def ProcessFile(gtest_source_file):
181184
"""Processes the given gtest source file."""
@@ -187,7 +190,7 @@ def ProcessFile(gtest_source_file):
187190
processed_files.add(gtest_source_file)
188191

189192
# Reads each line in the given gtest source file.
190-
for line in file(os.path.join(gtest_root, gtest_source_file), 'r'):
193+
for line in open(os.path.join(gtest_root, gtest_source_file), 'r'):
191194
m = INCLUDE_GTEST_FILE_REGEX.match(line)
192195
if m:
193196
if 'include/' + m.group(1) == GTEST_SPI_H_SEED:
@@ -218,7 +221,7 @@ def ProcessFile(gtest_source_file):
218221
def FuseGTestAllCc(gtest_root, output_dir):
219222
"""Scans folder gtest_root to generate gtest/gtest-all.cc in output_dir."""
220223

221-
output_file = file(os.path.join(output_dir, GTEST_ALL_CC_OUTPUT), 'w')
224+
output_file = open(os.path.join(output_dir, GTEST_ALL_CC_OUTPUT), 'w')
222225
FuseGTestAllCcToFile(gtest_root, output_file)
223226
output_file.close()
224227

@@ -242,7 +245,7 @@ def main():
242245
# fuse_gtest_files.py GTEST_ROOT_DIR OUTPUT_DIR
243246
FuseGTest(sys.argv[1], sys.argv[2])
244247
else:
245-
print __doc__
248+
print(__doc__)
246249
sys.exit(1)
247250

248251

googletest/test/gtest_env_var_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747

4848
def AssertEq(expected, actual):
4949
if expected != actual:
50-
print 'Expected: %s' % (expected,)
51-
print ' Actual: %s' % (actual,)
50+
print('Expected: %s' % (expected,))
51+
print(' Actual: %s' % (actual,))
5252
raise AssertionError
5353

5454

googletest/test/gtest_filter_unittest.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@
4444

4545
import os
4646
import re
47-
import sets
47+
try:
48+
from sets import Set as set # For Python 2.3 compatibility
49+
except ImportError:
50+
pass
4851
import sys
4952

5053
import gtest_test_utils
@@ -58,7 +61,7 @@
5861
# exception is thrown if the input is anything other than 'True' nor 'False'.
5962
os.environ['EMPTY_VAR'] = ''
6063
child = gtest_test_utils.Subprocess(
61-
[sys.executable, '-c', 'import os; print \'EMPTY_VAR\' in os.environ'])
64+
[sys.executable, '-c', 'import os; print(\'EMPTY_VAR\' in os.environ)'])
6265
CAN_PASS_EMPTY_ENV = eval(child.output)
6366

6467

@@ -71,7 +74,7 @@
7174
os.environ['UNSET_VAR'] = 'X'
7275
del os.environ['UNSET_VAR']
7376
child = gtest_test_utils.Subprocess(
74-
[sys.executable, '-c', 'import os; print \'UNSET_VAR\' not in os.environ'])
77+
[sys.executable, '-c', 'import os; print(\'UNSET_VAR\' not in os.environ)'])
7578
CAN_UNSET_ENV = eval(child.output)
7679

7780

@@ -243,14 +246,14 @@ def AssertPartitionIsValid(self, set_var, list_of_sets):
243246
for slice_var in list_of_sets:
244247
full_partition.extend(slice_var)
245248
self.assertEqual(len(set_var), len(full_partition))
246-
self.assertEqual(sets.Set(set_var), sets.Set(full_partition))
249+
self.assertEqual(set(set_var), set(full_partition))
247250

248251
def AdjustForParameterizedTests(self, tests_to_run):
249252
"""Adjust tests_to_run in case value parameterized tests are disabled."""
250253

251254
global param_tests_present
252255
if not param_tests_present:
253-
return list(sets.Set(tests_to_run) - sets.Set(PARAM_TESTS))
256+
return list(set(tests_to_run) - set(PARAM_TESTS))
254257
else:
255258
return tests_to_run
256259

googletest/test/gtest_output_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def RemoveUnsupportedTests(self, test_output):
279279
def testOutput(self):
280280
output = GetOutputOfAllCommands()
281281

282-
golden_file = open(GOLDEN_PATH, 'rb')
282+
golden_file = open(GOLDEN_PATH, 'r')
283283
# A mis-configured source control system can cause \r appear in EOL
284284
# sequences when we read the golden file irrespective of an operating
285285
# system used. Therefore, we need to strip those \r's from newlines

googletest/test/gtest_test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def GetTestExecutablePath(executable_name, build_dir=None):
178178
'Unable to find the test binary "%s". Please make sure to provide\n'
179179
'a path to the binary via the --build_dir flag or the BUILD_DIR\n'
180180
'environment variable.' % path)
181-
print >> sys.stderr, message
181+
sys.stdout.write(message)
182182
sys.exit(1)
183183

184184
return path

googletest/test/gtest_throw_on_failure_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def SetEnvVar(env_var, value):
7070
def Run(command):
7171
"""Runs a command; returns True/False if its exit code is/isn't 0."""
7272

73-
print 'Running "%s". . .' % ' '.join(command)
73+
print('Running "%s". . .' % ' '.join(command))
7474
p = gtest_test_utils.Subprocess(command)
7575
return p.exited and p.exit_code == 0
7676

googletest/test/gtest_uninitialized_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ def Assert(condition):
4646

4747
def AssertEq(expected, actual):
4848
if expected != actual:
49-
print 'Expected: %s' % (expected,)
50-
print ' Actual: %s' % (actual,)
49+
print('Expected: %s' % (expected,))
50+
print(' Actual: %s' % (actual,))
5151
raise AssertionError
5252

5353

googletest/test/gtest_xml_output_unittest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ def testDefaultOutputFile(self):
209209
'gtest_no_test_unittest')
210210
try:
211211
os.remove(output_file)
212-
except OSError, e:
212+
except OSError:
213+
e = sys.exc_info()[1]
213214
if e.errno != errno.ENOENT:
214215
raise
215216

googletest/test/gtest_xml_test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def AssertEquivalentNodes(self, expected_node, actual_node):
101101
self.assertEquals(
102102
len(expected_children), len(actual_children),
103103
'number of child elements differ in element ' + actual_node.tagName)
104-
for child_id, child in expected_children.iteritems():
104+
for child_id, child in expected_children.items():
105105
self.assert_(child_id in actual_children,
106106
'<%s> is not in <%s> (in element %s)' %
107107
(child_id, actual_children, actual_node.tagName))

0 commit comments

Comments
 (0)