1717
1818import sys
1919import time
20- import cStringIO
20+ import io
2121
2222from iptest .test_env import *
2323from iptest import options , l
2424
2525if not is_silverlight :
2626 import nt
27- from file_util import *
27+ from . file_util import *
2828
29- from type_util import types
29+ from . type_util import types
3030
3131#------------------------------------------------------------------------------
3232def usage (code , msg = '' ):
33- print sys .modules ['__main__' ].__doc__ or 'No doc provided'
34- if msg : print 'Error message: "%s"' % msg
33+ print ( sys .modules ['__main__' ].__doc__ or 'No doc provided' )
34+ if msg : print ( 'Error message: "%s"' % msg )
3535 sys .exit (code )
3636
3737if not is_silverlight :
3838 def get_environ_variable (key ):
39- l = [nt .environ [x ] for x in nt .environ .keys () if x .lower () == key .lower ()]
39+ l = [nt .environ [x ] for x in list ( nt .environ .keys () ) if x .lower () == key .lower ()]
4040 if l : return l [0 ]
4141 else : return None
4242
@@ -199,7 +199,7 @@ def AreEqual(a, b):
199199 Assert (a == b , "expected %r, but found %r" % (b , a ))
200200
201201def AreNotEqual (a , b ):
202- Assert (a <> b , "expected only one of the values to be %r" % a )
202+ Assert (a != b , "expected only one of the values to be %r" % a )
203203
204204def AssertContains (containing_string , substring ):
205205 Assert (substring in containing_string , "%s should be in %s" % (substring , containing_string ))
@@ -209,7 +209,7 @@ def AssertDoesNotContain(containing_string, substring):
209209
210210def SequencesAreEqual (a , b , m = None ):
211211 Assert (len (a ) == len (b ), m or 'sequence lengths differ: expected %d, but found %d' % (len (b ), len (a )))
212- for i in xrange (len (a )):
212+ for i in range (len (a )):
213213 Assert (a [i ] == b [i ], m or 'sequences differ at index %d: expected %r, but found %r' % (i , b [i ], a [i ]))
214214
215215def AlmostEqual (a , b , tolerance = 6 ):
@@ -242,22 +242,22 @@ def AssertInOrNot(l, in_list, not_in_list):
242242def AssertErrorWithMessage (exc , expectedMessage , func , * args , ** kwargs ):
243243 Assert (expectedMessage , "expectedMessage cannot be null" )
244244 try : func (* args , ** kwargs )
245- except exc , inst :
245+ except exc as inst :
246246 Assert (expectedMessage == inst .__str__ (), \
247247 "Exception %r message (%r) does not match %r" % (type (inst ), inst .__str__ (), expectedMessage ))
248248 else : Assert (False , "Expected %r but got no exception" % exc )
249249
250250def AssertErrorWithPartialMessage (exc , expectedMessage , func , * args , ** kwargs ):
251251 Assert (expectedMessage , "expectedMessage cannot be null" )
252252 try : func (* args , ** kwargs )
253- except exc , inst :
253+ except exc as inst :
254254 Assert (expectedMessage in inst .__str__ (), \
255255 "Exception %r message (%r) does not contain %r" % (type (inst ), inst .__str__ (), expectedMessage ))
256256 else : Assert (False , "Expected %r but got no exception" % exc )
257257
258258def AssertErrorWithNumber (exc , expectedErrorNo , func , * args , ** kwargs ):
259259 try : func (* args , ** kwargs )
260- except exc , e :
260+ except exc as e :
261261 AreEqual (e .errno , expectedErrorNo )
262262 else : Fail ("Expected %r but got no exception" % exc )
263263
@@ -272,7 +272,7 @@ def AssertErrorWithMessages(exc, ironPythonMessage, cpythonMessage, func, *args,
272272
273273 Assert (expectedMessage , "expectedMessage cannot be null" )
274274 try : func (* args , ** kwargs )
275- except exc , inst :
275+ except exc as inst :
276276 Assert (expectedMessage == inst .__str__ (), \
277277 "Exception %r message (%r) does not contain %r" % (type (inst ), inst .__str__ (), expectedMessage ))
278278 else : Assert (False , "Expected %r but got no exception" % exc )
@@ -298,7 +298,7 @@ def AssertErrorWithMatch(exc, expectedMessage, func, *args, **kwargs):
298298 import re
299299 Assert (expectedMessage , "expectedMessage cannot be null" )
300300 try : func (* args , ** kwargs )
301- except exc , inst :
301+ except exc as inst :
302302 Assert (re .compile (expectedMessage ).match (inst .__str__ ()), \
303303 "Exception %r message (%r) does not contain %r" % (type (inst ), inst .__str__ (), expectedMessage ))
304304 else : Assert (False , "Expected %r but got no exception" % exc )
@@ -337,7 +337,7 @@ def GetTotalMemory():
337337
338338def _do_nothing (* args ):
339339 for arg in args :
340- print arg
340+ print ( arg )
341341 pass
342342
343343def get_num_iterations ():
@@ -361,7 +361,7 @@ class disabled:
361361 def __init__ (self , reason ):
362362 self .reason = reason
363363 def __call__ (self , f ):
364- return _do_nothing ("Skipping disabled test %s. (Reason: %s)" % (f .func_name , self .reason ))
364+ return _do_nothing ("Skipping disabled test %s. (Reason: %s)" % (f .__name__ , self .reason ))
365365
366366class skip :
367367 def __init__ (self , * platforms ):
@@ -386,11 +386,11 @@ def stdlib_test(self):
386386 def __call__ (self , f ):
387387 #skip questionable tests
388388 if is_silverlight and 'silverlightbug?' in self .platforms :
389- msg = '... TODO, investigate Silverlight failure @ %s' % f .func_name
389+ msg = '... TODO, investigate Silverlight failure @ %s' % f .__name__
390390 return _do_nothing (msg )
391391 elif sys .platform in self .platforms :
392392 msg = '... Decorated with @skip(%s), skipping %s ...' % (
393- self .platforms , f .func_name )
393+ self .platforms , f .__name__ )
394394 return _do_nothing (msg )
395395
396396
@@ -399,7 +399,7 @@ def __call__(self, f):
399399 platform_test = getattr (self , to_skip + '_test' )
400400 if to_skip in self .platforms and platform_test ():
401401 msg = '... Decorated with @skip(%s), skipping %s ...' % (
402- self .platforms , f .func_name )
402+ self .platforms , f .__name__ )
403403 return _do_nothing (msg )
404404 return f
405405
@@ -419,7 +419,7 @@ def __call__(self, f):
419419 elif sys .platform in self .platforms :
420420 return f
421421 else :
422- return _do_nothing ('... Decorated with @runonly(%s), Skipping %s ...' % (self .platforms , f .func_name ))
422+ return _do_nothing ('... Decorated with @runonly(%s), Skipping %s ...' % (self .platforms , f .__name__ ))
423423
424424@runonly ('win32 silverlight cli' )
425425def _func (): pass
@@ -428,55 +428,55 @@ def _func(): pass
428428def skiptest (* args ):
429429 #hack: skip questionable tests:
430430 if is_silverlight and 'silverlightbug?' in args :
431- print '... TODO, whole test module is skipped for Silverlight failure. Need to investigate...'
431+ print ( '... TODO, whole test module is skipped for Silverlight failure. Need to investigate...' )
432432 exit_module ()
433433 elif is_silverlight and 'silverlight' in args :
434- print '... %s, skipping whole test module...' % sys .platform
434+ print ( '... %s, skipping whole test module...' % sys .platform )
435435 exit_module ()
436436 elif is_interactive () and 'interactive' in args :
437- print '... %s, skipping whole test module under "interactive" mode...' % sys .platform
437+ print ( '... %s, skipping whole test module under "interactive" mode...' % sys .platform )
438438 exit_module ()
439439 elif is_stdlib () and 'stdlib' in args :
440- print '... %s, skipping whole test module under "stdlib" mode...' % sys .platform
440+ print ( '... %s, skipping whole test module under "stdlib" mode...' % sys .platform )
441441 exit_module ()
442442
443443 elif is_cli64 and 'cli64' in args :
444- print '... %s, skipping whole test module on 64-bit CLI...' % sys .platform
444+ print ( '... %s, skipping whole test module on 64-bit CLI...' % sys .platform )
445445 exit_module ()
446446
447447 elif get_num_iterations () > 1 and 'multiple_execute' in args :
448- print '... %d invocations, skipping whole test module under "multiple_execute" mode...' % get_num_iterations ()
448+ print ( '... %d invocations, skipping whole test module under "multiple_execute" mode...' % get_num_iterations () )
449449 exit_module ()
450450
451451 if sys .platform in args :
452- print '... %s, skipping whole test module...' % sys .platform
452+ print ( '... %s, skipping whole test module...' % sys .platform )
453453 exit_module ()
454454
455455def exit_module ():
456456 #Have to catch exception for below call. Any better way to exit?
457457 sys .exit (0 )
458458
459459def print_failures (total , failures ):
460- print
460+ print ()
461461 for failure in failures :
462462 name , (extype , ex , tb ) = failure
463- print '------------------------------------'
464- print "Test %s failed throwing %s (%s)" % (name , str (extype ), str (ex ))
463+ print ( '------------------------------------' )
464+ print ( "Test %s failed throwing %s (%s)" % (name , str (extype ), str (ex ) ))
465465 while tb :
466- print ' ... %s in %s line %d' % (tb .tb_frame .f_code .co_name , tb .tb_frame .f_code .co_filename , tb .tb_lineno )
466+ print ( ' ... %s in %s line %d' % (tb .tb_frame .f_code .co_name , tb .tb_frame .f_code .co_filename , tb .tb_lineno ) )
467467 tb = tb .tb_next
468- print
468+ print ()
469469
470470 if is_cli :
471471 if '-X:ExceptionDetail' in System .Environment .GetCommandLineArgs ():
472472 load_iron_python_test ()
473473 from IronPythonTest import TestHelpers
474- print 'CLR Exception: ' ,
475- print TestHelpers .GetContext ().FormatException (ex .clsException )
474+ print ( 'CLR Exception: ' , end = ' ' )
475+ print ( TestHelpers .GetContext ().FormatException (ex .clsException ) )
476476
477- print
477+ print ()
478478 failcount = len (failures )
479- print '%d total, %d passed, %d failed' % (total , total - failcount , failcount )
479+ print ( '%d total, %d passed, %d failed' % (total , total - failcount , failcount ) )
480480
481481def run_test (mod_name , noOutputPlease = False ):
482482 if not options .RUN_TESTS :
@@ -497,12 +497,12 @@ def run_test(mod_name, noOutputPlease=False):
497497 if name .endswith ("_clionly" ) and not is_cli : continue
498498 if name .startswith ("test_" ):
499499 if not includedTests or name in includedTests :
500- for i in xrange ( get_num_iterations ()):
500+ for i in range ( get_num_iterations ()):
501501 if not noOutputPlease :
502502 if hasattr (time , 'clock' ):
503- print ">>> %6.2fs testing %-40s" % (round (time .clock (), 2 ), name , ),
503+ print ( ">>> %6.2fs testing %-40s" % (round (time .clock (), 2 ), name , ), end = ' ' )
504504 else :
505- print ">>> testing %-40s" % name ,
505+ print ( ">>> testing %-40s" % name , end = ' ' )
506506 #obj()
507507 #catches the error and exit at the end of each test
508508 total += 1
@@ -513,25 +513,25 @@ def run_test(mod_name, noOutputPlease=False):
513513 # restore std-in / std-err incase the test corrupted it
514514 sys .stdout = stdout
515515 sys .stderr = stderr
516- print
516+ print ()
517517
518518 except :
519519 failures .append ( (name , sys .exc_info ()) )
520- print "FAIL (%s)" % str (sys .exc_info ()[0 ])
520+ print ( "FAIL (%s)" % str (sys .exc_info ()[0 ]) )
521521
522522 elif not noOutputPlease :
523- print ">>> skipping %-40s" % name
523+ print ( ">>> skipping %-40s" % name )
524524 if failures :
525525 print_failures (total , failures )
526526 if is_cli :
527527 cmd_line = System .Environment .CurrentDirectory + "> " + System .Environment .CommandLine
528- print "Please run the following command to repro:"
529- print "\t " + cmd_line
528+ print ( "Please run the following command to repro:" )
529+ print ( "\t " + cmd_line )
530530
531531 sys .exit (len (failures ))
532532 else :
533- print
534- print '%d tests passed' % total
533+ print ()
534+ print ( '%d tests passed' % total )
535535
536536def run_class (mod_name , verbose = False ):
537537 pass
@@ -557,7 +557,7 @@ def AddReferenceToDlrCore():
557557
558558class stderr_trapper (object ):
559559 def __init__ (self ):
560- self .stderr = cStringIO .StringIO ()
560+ self .stderr = io .StringIO ()
561561 def __enter__ (self ):
562562 self .oldstderr = sys .stderr
563563 sys .stderr = self .stderr
@@ -572,7 +572,7 @@ def __exit__(self, *args):
572572
573573class stdout_trapper (object ):
574574 def __init__ (self ):
575- self .stdout = cStringIO .StringIO ()
575+ self .stdout = io .StringIO ()
576576 def __enter__ (self ):
577577 self .oldstdout , sys .stdout = sys .stdout , self .stdout
578578 return self
@@ -595,17 +595,17 @@ def retry_on_failure(f, *args, **kwargs):
595595 2. If f() fails, it retries invoking it MAX_FAILURE_RETRY times
596596 '''
597597 def t (* args , ** kwargs ):
598- for i in xrange (MAX_FAILURE_RETRY ):
598+ for i in range (MAX_FAILURE_RETRY ):
599599 try :
600600 ret_val = f (* args , ** kwargs )
601601 return ret_val
602- except Exception , e :
603- print "retry_on_failure(%s): failed on attempt '%d':" % (f .__name__ , i + 1 )
604- print e
602+ except Exception as e :
603+ print ( "retry_on_failure(%s): failed on attempt '%d':" % (f .__name__ , i + 1 ) )
604+ print ( e )
605605 excp_info = sys .exc_info ()
606606 continue
607607 # raise w/ excep info to preverve the original stack trace
608- raise excp_info [0 ], excp_info [1 ], excp_info [2 ]
608+ raise excp_info [0 ]( excp_info [1 ]). with_traceback ( excp_info [2 ])
609609
610610 return t
611611
@@ -617,6 +617,6 @@ def force_gc():
617617 gc .collect ()
618618 else :
619619 import System
620- for i in xrange (100 ):
620+ for i in range (100 ):
621621 System .GC .Collect ()
622622 System .GC .WaitForPendingFinalizers ()
0 commit comments