1515
1616import logging
1717import os
18+ import sys
1819import time
1920import unittest
20- from StringIO import StringIO
21- from imp import load_source
21+
22+ try :
23+ # for Python 2
24+ from StringIO import StringIO
25+ except ImportError :
26+ # for Python 3
27+ from io import StringIO
2228
2329from compare_perf_tests import PerformanceTestResult
2430
2531from test_utils import Mock , MockLoggingHandler , Stub , captured_output
2632
33+
34+ # imp.load_source is deprecated in Python 3.4
35+ if sys .version_info < (3 , 4 ):
36+ from imp import load_source
37+ else :
38+
39+ def load_source (name , path ):
40+ from importlib .machinery import SourceFileLoader
41+
42+ return SourceFileLoader (name , path ).load_module ()
43+
44+
2745# import Benchmark_Driver # doesn't work because it misses '.py' extension
2846Benchmark_Driver = load_source (
2947 "Benchmark_Driver" ,
@@ -46,7 +64,17 @@ def assert_contains(self, texts, output):
4664 def test_requires_command_argument (self ):
4765 with captured_output () as (_ , err ):
4866 self .assertRaises (SystemExit , parse_args , [])
49- self .assert_contains (["usage:" , "COMMAND" , "too few arguments" ], err .getvalue ())
67+
68+ if sys .version_info < (3 , 3 ):
69+ self .assert_contains (
70+ ["usage:" , "COMMAND" , "too few arguments" ], err .getvalue ()
71+ )
72+ else :
73+ # The error message has changed in Python 3.3
74+ self .assert_contains (
75+ ["usage:" , "COMMAND" , "the following arguments are required" ],
76+ err .getvalue (),
77+ )
5078
5179 def test_command_help_lists_commands (self ):
5280 with captured_output () as (out , _ ):
@@ -151,7 +179,14 @@ class SubprocessMock(Mock):
151179 def __init__ (self , responses = None ):
152180 super (SubprocessMock , self ).__init__ (responses )
153181
154- def _check_output (args , stdin = None , stdout = None , stderr = None , shell = False ):
182+ def _check_output (
183+ args ,
184+ stdin = None ,
185+ stdout = None ,
186+ stderr = None ,
187+ shell = False ,
188+ universal_newlines = False ,
189+ ):
155190 return self .record_and_respond (args , stdin , stdout , stderr , shell )
156191
157192 self .check_output = _check_output
@@ -190,8 +225,8 @@ def test_gets_list_of_precommit_benchmarks(self):
190225 self .subprocess_mock .assert_called_all_expected ()
191226 self .assertEqual (driver .tests , ["Benchmark1" , "Benchmark2" ])
192227 self .assertEqual (driver .all_tests , ["Benchmark1" , "Benchmark2" ])
193- self .assertEquals (driver .test_number ["Benchmark1" ], "1" )
194- self .assertEquals (driver .test_number ["Benchmark2" ], "2" )
228+ self .assertEqual (driver .test_number ["Benchmark1" ], "1" )
229+ self .assertEqual (driver .test_number ["Benchmark2" ], "2" )
195230
196231 list_all_tests = (
197232 "/benchmarks/Benchmark_O --list --delim=\t --skip-tags=" .split (" " ),
@@ -330,10 +365,10 @@ def test_parse_results_from_running_benchmarks(self):
330365 """
331366 r = self .driver .run ("b" )
332367 self .assertTrue (self .parser_stub .results_from_string_called )
333- self .assertEquals (r .name , "b1" ) # non-matching name, just 1st result
368+ self .assertEqual (r .name , "b1" ) # non-matching name, just 1st result
334369 r = self .driver .run ()
335370 self .assertTrue (isinstance (r , dict ))
336- self .assertEquals (r ["b1" ].name , "b1" )
371+ self .assertEqual (r ["b1" ].name , "b1" )
337372
338373 def test_measure_memory (self ):
339374 self .driver .run ("b" , measure_memory = True )
@@ -412,7 +447,11 @@ def test_log_results(self):
412447
413448 def assert_log_written (out , log_file , content ):
414449 self .assertEqual (out .getvalue (), "Logging results to: " + log_file + "\n " )
415- with open (log_file , "rU" ) as f :
450+ if sys .version_info < (3 , 0 ):
451+ openmode = "rU"
452+ else :
453+ openmode = "r" # 'U' mode is deprecated in Python 3
454+ with open (log_file , openmode ) as f :
416455 text = f .read ()
417456 self .assertEqual (text , "formatted output" )
418457
0 commit comments