home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / python2.4 / test / regrtest.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  30.4 KB  |  847 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Regression test.
  5.  
  6. This will find all modules whose name is "test_*" in the test
  7. directory, and run them.  Various command line options provide
  8. additional facilities.
  9.  
  10. Command line options:
  11.  
  12. -v: verbose    -- run tests in verbose mode with output to stdout
  13. -w: verbose2   -- re-run failed tests in verbose mode
  14. -q: quiet      -- don\'t print anything except if a test fails
  15. -g: generate   -- write the output file for a test instead of comparing it
  16. -x: exclude    -- arguments are tests to *exclude*
  17. -s: single     -- run only a single test (see below)
  18. -r: random     -- randomize test execution order
  19. -f: fromfile   -- read names of tests to run from a file (see below)
  20. -l: findleaks  -- if GC is available detect tests that leak memory
  21. -u: use        -- specify which special resource intensive tests to run
  22. -h: help       -- print this text and exit
  23. -t: threshold  -- call gc.set_threshold(N)
  24. -T: coverage   -- turn on code coverage using the trace module
  25. -D: coverdir   -- Directory where coverage files are put
  26. -N: nocoverdir -- Put coverage files alongside modules
  27. -L: runleaks   -- run the leaks(1) command just before exit
  28. -R: huntrleaks -- search for reference leaks (needs debug build, v. slow)
  29.  
  30. If non-option arguments are present, they are names for tests to run,
  31. unless -x is given, in which case they are names for tests not to run.
  32. If no test names are given, all tests are run.
  33.  
  34. -v is incompatible with -g and does not compare test output files.
  35.  
  36. -T turns on code coverage tracing with the trace module.
  37.  
  38. -D specifies the directory where coverage files are put.
  39.  
  40. -N Put coverage files alongside modules.
  41.  
  42. -s means to run only a single test and exit.  This is useful when
  43. doing memory analysis on the Python interpreter (which tend to consume
  44. too many resources to run the full regression test non-stop).  The
  45. file /tmp/pynexttest is read to find the next test to run.  If this
  46. file is missing, the first test_*.py file in testdir or on the command
  47. line is used.  (actually tempfile.gettempdir() is used instead of
  48. /tmp).
  49.  
  50. -f reads the names of tests from the file given as f\'s argument, one
  51. or more test names per line.  Whitespace is ignored.  Blank lines and
  52. lines beginning with \'#\' are ignored.  This is especially useful for
  53. whittling down failures involving interactions among tests.
  54.  
  55. -L causes the leaks(1) command to be run just before exit if it exists.
  56. leaks(1) is available on Mac OS X and presumably on some other
  57. FreeBSD-derived systems.
  58.  
  59. -R runs each test several times and examines sys.gettotalrefcount() to
  60. see if the test appears to be leaking references.  The argument should
  61. be of the form stab:run:fname where \'stab\' is the number of times the
  62. test is run to let gettotalrefcount settle down, \'run\' is the number
  63. of times further it is run and \'fname\' is the name of the file the
  64. reports are written to.  These parameters all have defaults (5, 4 and
  65. "reflog.txt" respectively), so the minimal invocation is \'-R ::\'.
  66.  
  67. -u is used to specify which special resource intensive tests to run,
  68. such as those requiring large file support or network connectivity.
  69. The argument is a comma-separated list of words indicating the
  70. resources to test.  Currently only the following are defined:
  71.  
  72.     all -       Enable all special resources.
  73.  
  74.     audio -     Tests that use the audio device.  (There are known
  75.                 cases of broken audio drivers that can crash Python or
  76.                 even the Linux kernel.)
  77.  
  78.     curses -    Tests that use curses and will modify the terminal\'s
  79.                 state and output modes.
  80.  
  81.     largefile - It is okay to run some test that may create huge
  82.                 files.  These tests can take a long time and may
  83.                 consume >2GB of disk space temporarily.
  84.  
  85.     network -   It is okay to run tests that use external network
  86.                 resource, e.g. testing SSL support for sockets.
  87.  
  88.     bsddb -     It is okay to run the bsddb testsuite, which takes
  89.                 a long time to complete.
  90.  
  91.     decimal -   Test the decimal module against a large suite that
  92.                 verifies compliance with standards.
  93.  
  94.     compiler -  Test the compiler package by compiling all the source
  95.                 in the standard library and test suite.  This takes
  96.                 a long time.
  97.  
  98.     subprocess  Run all tests for the subprocess module.
  99.  
  100. To enable all resources except one, use \'-uall,-<resource>\'.  For
  101. example, to run all the tests except for the bsddb tests, give the
  102. option \'-uall,-bsddb\'.
  103. '''
  104. import os
  105. import sys
  106. import getopt
  107. import random
  108. import warnings
  109. import sre
  110. import cStringIO
  111. import traceback
  112. warnings.filterwarnings('ignore', 'hex/oct constants', FutureWarning, '.*test.test_grammar$')
  113. if sys.maxint > 2147483647:
  114.     warnings.filterwarnings('ignore', 'hex/oct constants', FutureWarning, '<string>')
  115.  
  116. if sys.platform == 'darwin':
  117.     
  118.     try:
  119.         import resource
  120.     except ImportError:
  121.         pass
  122.  
  123.     (soft, hard) = resource.getrlimit(resource.RLIMIT_STACK)
  124.     newsoft = min(hard, max(soft, 1024 * 2048))
  125.     resource.setrlimit(resource.RLIMIT_STACK, (newsoft, hard))
  126.  
  127. from test import test_support
  128. RESOURCE_NAMES = ('audio', 'curses', 'largefile', 'network', 'bsddb', 'decimal', 'compiler', 'subprocess')
  129.  
  130. def usage(code, msg = ''):
  131.     print __doc__
  132.     if msg:
  133.         print msg
  134.     
  135.     sys.exit(code)
  136.  
  137.  
  138. def main(tests = None, testdir = None, verbose = 0, quiet = False, generate = False, exclude = False, single = False, randomize = False, fromfile = None, findleaks = False, use_resources = None, trace = False, coverdir = 'coverage', runleaks = False, huntrleaks = False, verbose2 = False):
  139.     """Execute a test suite.
  140.  
  141.     This also parses command-line options and modifies its behavior
  142.     accordingly.
  143.  
  144.     tests -- a list of strings containing test names (optional)
  145.     testdir -- the directory in which to look for tests (optional)
  146.  
  147.     Users other than the Python test suite will certainly want to
  148.     specify testdir; if it's omitted, the directory containing the
  149.     Python test suite is searched for.
  150.  
  151.     If the tests argument is omitted, the tests listed on the
  152.     command-line will be used.  If that's empty, too, then all *.py
  153.     files beginning with test_ will be used.
  154.  
  155.     The other default arguments (verbose, quiet, generate, exclude, single,
  156.     randomize, findleaks, use_resources, trace and coverdir) allow programmers
  157.     calling main() directly to set the values that would normally be set by
  158.     flags on the command line.
  159.     """
  160.     test_support.record_original_stdout(sys.stdout)
  161.     
  162.     try:
  163.         (opts, args) = getopt.getopt(sys.argv[1:], 'hvgqxsrf:lu:t:TD:NLR:w', [
  164.             'help',
  165.             'verbose',
  166.             'quiet',
  167.             'generate',
  168.             'exclude',
  169.             'single',
  170.             'random',
  171.             'fromfile',
  172.             'findleaks',
  173.             'use=',
  174.             'threshold=',
  175.             'trace',
  176.             'coverdir=',
  177.             'nocoverdir',
  178.             'runleaks',
  179.             'huntrleaks=',
  180.             'verbose2'])
  181.     except getopt.error:
  182.         msg = None
  183.         usage(2, msg)
  184.  
  185.     if use_resources is None:
  186.         use_resources = []
  187.     
  188.     for o, a in opts:
  189.         if o in ('-h', '--help'):
  190.             usage(0)
  191.             continue
  192.         if o in ('-v', '--verbose'):
  193.             verbose += 1
  194.             continue
  195.         if o in ('-w', '--verbose2'):
  196.             verbose2 = True
  197.             continue
  198.         if o in ('-q', '--quiet'):
  199.             quiet = True
  200.             verbose = 0
  201.             continue
  202.         if o in ('-g', '--generate'):
  203.             generate = True
  204.             continue
  205.         if o in ('-x', '--exclude'):
  206.             exclude = True
  207.             continue
  208.         if o in ('-s', '--single'):
  209.             single = True
  210.             continue
  211.         if o in ('-r', '--randomize'):
  212.             randomize = True
  213.             continue
  214.         if o in ('-f', '--fromfile'):
  215.             fromfile = a
  216.             continue
  217.         if o in ('-l', '--findleaks'):
  218.             findleaks = True
  219.             continue
  220.         if o in ('-L', '--runleaks'):
  221.             runleaks = True
  222.             continue
  223.         if o in ('-t', '--threshold'):
  224.             import gc
  225.             gc.set_threshold(int(a))
  226.             continue
  227.         if o in ('-T', '--coverage'):
  228.             trace = True
  229.             continue
  230.         if o in ('-D', '--coverdir'):
  231.             coverdir = os.path.join(os.getcwd(), a)
  232.             continue
  233.         if o in ('-N', '--nocoverdir'):
  234.             coverdir = None
  235.             continue
  236.         if o in ('-R', '--huntrleaks'):
  237.             huntrleaks = a.split(':')
  238.             if len(huntrleaks) != 3:
  239.                 print a, huntrleaks
  240.                 usage(2, '-R takes three colon-separated arguments')
  241.             
  242.             if len(huntrleaks[0]) == 0:
  243.                 huntrleaks[0] = 5
  244.             else:
  245.                 huntrleaks[0] = int(huntrleaks[0])
  246.             if len(huntrleaks[1]) == 0:
  247.                 huntrleaks[1] = 4
  248.             else:
  249.                 huntrleaks[1] = int(huntrleaks[1])
  250.             if len(huntrleaks[2]) == 0:
  251.                 huntrleaks[2] = 'reflog.txt'
  252.             
  253.         len(huntrleaks[2]) == 0
  254.         if o in ('-u', '--use'):
  255.             u = [ x.lower() for x in a.split(',') ]
  256.             for r in u:
  257.                 remove = False
  258.                 if r[0] == '-':
  259.                     remove = True
  260.                     r = r[1:]
  261.                 
  262.                 if r not in RESOURCE_NAMES:
  263.                     usage(1, 'Invalid -u/--use option: ' + a)
  264.                 
  265.                 if remove:
  266.                     if r in use_resources:
  267.                         use_resources.remove(r)
  268.                     
  269.                 r in use_resources
  270.                 if r not in use_resources:
  271.                     use_resources.append(r)
  272.                     continue
  273.             
  274.     
  275.     if generate and verbose:
  276.         usage(2, "-g and -v don't go together!")
  277.     
  278.     if single and fromfile:
  279.         usage(2, "-s and -f don't go together!")
  280.     
  281.     good = []
  282.     bad = []
  283.     skipped = []
  284.     resource_denieds = []
  285.     if findleaks:
  286.         
  287.         try:
  288.             import gc
  289.         except ImportError:
  290.             print 'No GC available, disabling findleaks.'
  291.             findleaks = False
  292.  
  293.         found_garbage = []
  294.     
  295.     if single:
  296.         gettempdir = gettempdir
  297.         import tempfile
  298.         filename = os.path.join(gettempdir(), 'pynexttest')
  299.         
  300.         try:
  301.             fp = open(filename, 'r')
  302.             next = fp.read().strip()
  303.             tests = [
  304.                 next]
  305.             fp.close()
  306.         except IOError:
  307.             pass
  308.         except:
  309.             None<EXCEPTION MATCH>IOError
  310.         
  311.  
  312.     None<EXCEPTION MATCH>IOError
  313.     if fromfile:
  314.         tests = []
  315.         fp = open(fromfile)
  316.         for line in fp:
  317.             guts = line.split()
  318.             if guts and not guts[0].startswith('#'):
  319.                 tests.extend(guts)
  320.                 continue
  321.         
  322.         fp.close()
  323.     
  324.     if args:
  325.         args = map(removepy, args)
  326.     
  327.     if tests:
  328.         tests = map(removepy, tests)
  329.     
  330.     stdtests = STDTESTS[:]
  331.     nottests = NOTTESTS[:]
  332.     if exclude:
  333.         for arg in args:
  334.             if arg in stdtests:
  335.                 stdtests.remove(arg)
  336.                 continue
  337.         
  338.         nottests[:0] = args
  339.         args = []
  340.     
  341.     if not tests and args:
  342.         pass
  343.     tests = findtests(testdir, stdtests, nottests)
  344.     if single:
  345.         tests = tests[:1]
  346.     
  347.     if randomize:
  348.         random.shuffle(tests)
  349.     
  350.     if trace:
  351.         import trace
  352.         tracer = trace.Trace(ignoredirs = [
  353.             sys.prefix,
  354.             sys.exec_prefix], trace = False, count = True)
  355.     
  356.     test_support.verbose = verbose
  357.     test_support.use_resources = use_resources
  358.     save_modules = sys.modules.keys()
  359.     for test in tests:
  360.         if not quiet:
  361.             print test
  362.             sys.stdout.flush()
  363.         
  364.         if trace:
  365.             tracer.runctx('runtest(test, generate, verbose, quiet, testdir)', globals = globals(), locals = vars())
  366.         else:
  367.             ok = runtest(test, generate, verbose, quiet, testdir, huntrleaks)
  368.             if ok > 0:
  369.                 good.append(test)
  370.             elif ok == 0:
  371.                 bad.append(test)
  372.             else:
  373.                 skipped.append(test)
  374.                 if ok == -2:
  375.                     resource_denieds.append(test)
  376.                 
  377.         if findleaks:
  378.             gc.collect()
  379.             if gc.garbage:
  380.                 print 'Warning: test created', len(gc.garbage), 'uncollectable object(s).'
  381.                 found_garbage.extend(gc.garbage)
  382.                 del gc.garbage[:]
  383.             
  384.         
  385.         for module in sys.modules.keys():
  386.             if module not in save_modules and module.startswith('test.'):
  387.                 test_support.unload(module)
  388.                 continue
  389.         
  390.     
  391.     good.sort()
  392.     bad.sort()
  393.     skipped.sort()
  394.     if good and not quiet:
  395.         if not bad and not skipped and len(good) > 1:
  396.             print 'All',
  397.         
  398.         print count(len(good), 'test'), 'OK.'
  399.         if verbose:
  400.             print "CAUTION:  stdout isn't compared in verbose mode:"
  401.             print 'a test that passes in verbose mode may fail without it.'
  402.         
  403.     
  404.     if bad:
  405.         print count(len(bad), 'test'), 'failed:'
  406.         printlist(bad)
  407.     
  408.     if skipped and not quiet:
  409.         print count(len(skipped), 'test'), 'skipped:'
  410.         printlist(skipped)
  411.         e = _ExpectedSkips()
  412.         plat = sys.platform
  413.         if e.isvalid():
  414.             surprise = set(skipped) - e.getexpected() - set(resource_denieds)
  415.             if surprise:
  416.                 print count(len(surprise), 'skip'), 'unexpected on', plat + ':'
  417.                 printlist(surprise)
  418.             else:
  419.                 print 'Those skips are all expected on', plat + '.'
  420.         else:
  421.             print 'Ask someone to teach regrtest.py about which tests are'
  422.             print 'expected to get skipped on', plat + '.'
  423.     
  424.     if verbose2 and bad:
  425.         print 'Re-running failed tests in verbose mode'
  426.         for test in bad:
  427.             print 'Re-running test %r in verbose mode' % test
  428.             sys.stdout.flush()
  429.             
  430.             try:
  431.                 test_support.verbose = 1
  432.                 ok = runtest(test, generate, 1, quiet, testdir, huntrleaks)
  433.             continue
  434.             except KeyboardInterrupt:
  435.                 print 
  436.                 break
  437.                 continue
  438.                 raise 
  439.                 continue
  440.             
  441.  
  442.         
  443.     
  444.     if single:
  445.         alltests = findtests(testdir, stdtests, nottests)
  446.         for i in range(len(alltests)):
  447.             if tests[0] == alltests[i]:
  448.                 if i == len(alltests) - 1:
  449.                     os.unlink(filename)
  450.                 else:
  451.                     fp = open(filename, 'w')
  452.                     fp.write(alltests[i + 1] + '\n')
  453.                     fp.close()
  454.                 break
  455.                 continue
  456.         
  457.     
  458.     if trace:
  459.         r = tracer.results()
  460.         r.write_results(show_missing = True, summary = True, coverdir = coverdir)
  461.     
  462.     if runleaks:
  463.         os.system('leaks %d' % os.getpid())
  464.     
  465.     sys.exit(len(bad) > 0)
  466.  
  467. STDTESTS = [
  468.     'test_grammar',
  469.     'test_opcodes',
  470.     'test_operations',
  471.     'test_builtin',
  472.     'test_exceptions',
  473.     'test_types']
  474. NOTTESTS = [
  475.     'test_support',
  476.     'test_future1',
  477.     'test_future2',
  478.     'test_future3']
  479.  
  480. def findtests(testdir = None, stdtests = STDTESTS, nottests = NOTTESTS):
  481.     '''Return a list of all applicable test modules.'''
  482.     if not testdir:
  483.         testdir = findtestdir()
  484.     
  485.     names = os.listdir(testdir)
  486.     tests = []
  487.     for name in names:
  488.         if name[:5] == 'test_' and name[-3:] == os.extsep + 'py':
  489.             modname = name[:-3]
  490.             if modname not in stdtests and modname not in nottests:
  491.                 tests.append(modname)
  492.             
  493.         modname not in nottests
  494.     
  495.     tests.sort()
  496.     return stdtests + tests
  497.  
  498.  
  499. def runtest(test, generate, verbose, quiet, testdir = None, huntrleaks = False):
  500.     """Run a single test.
  501.     test -- the name of the test
  502.     generate -- if true, generate output, instead of running the test
  503.     and comparing it to a previously created output file
  504.     verbose -- if true, print more messages
  505.     quiet -- if true, don't print 'skipped' messages (probably redundant)
  506.     testdir -- test directory
  507.     """
  508.     test_support.unload(test)
  509.     if not testdir:
  510.         testdir = findtestdir()
  511.     
  512.     outputdir = os.path.join(testdir, 'output')
  513.     outputfile = os.path.join(outputdir, test)
  514.     if verbose:
  515.         cfp = None
  516.     else:
  517.         cfp = cStringIO.StringIO()
  518.     if huntrleaks:
  519.         refrep = open(huntrleaks[2], 'a')
  520.     
  521.     
  522.     try:
  523.         save_stdout = sys.stdout
  524.         
  525.         try:
  526.             if cfp:
  527.                 sys.stdout = cfp
  528.                 print test
  529.             
  530.             if test.startswith('test.'):
  531.                 abstest = test
  532.             else:
  533.                 abstest = 'test.' + test
  534.             the_package = __import__(abstest, globals(), locals(), [])
  535.             the_module = getattr(the_package, test)
  536.             indirect_test = getattr(the_module, 'test_main', None)
  537.             if indirect_test is not None:
  538.                 indirect_test()
  539.             
  540.             if huntrleaks:
  541.                 import copy_reg
  542.                 fs = warnings.filters[:]
  543.                 ps = copy_reg.dispatch_table.copy()
  544.                 pic = sys.path_importer_cache.copy()
  545.                 import gc
  546.                 
  547.                 def cleanup():
  548.                     import _strptime
  549.                     import urlparse
  550.                     import warnings
  551.                     import dircache
  552.                     _path_created = _path_created
  553.                     import distutils.dir_util
  554.                     _path_created.clear()
  555.                     warnings.filters[:] = fs
  556.                     gc.collect()
  557.                     sre.purge()
  558.                     _strptime._regex_cache.clear()
  559.                     urlparse.clear_cache()
  560.                     copy_reg.dispatch_table.clear()
  561.                     copy_reg.dispatch_table.update(ps)
  562.                     sys.path_importer_cache.clear()
  563.                     sys.path_importer_cache.update(pic)
  564.                     dircache.reset()
  565.  
  566.                 if indirect_test:
  567.                     
  568.                     def run_the_test():
  569.                         indirect_test()
  570.  
  571.                 else:
  572.                     
  573.                     def run_the_test():
  574.                         reload(the_module)
  575.  
  576.                 deltas = []
  577.                 repcount = huntrleaks[0] + huntrleaks[1]
  578.                 print >>sys.stderr, 'beginning', repcount, 'repetitions'
  579.                 print >>sys.stderr, '1234567890' * (repcount // 10 + 1)[:repcount]
  580.                 for i in range(repcount):
  581.                     rc = sys.gettotalrefcount()
  582.                     run_the_test()
  583.                     sys.stderr.write('.')
  584.                     cleanup()
  585.                     deltas.append(sys.gettotalrefcount() - rc - 2)
  586.                 
  587.                 print >>sys.stderr
  588.                 if max(map(abs, deltas[-huntrleaks[1]:])) > 0:
  589.                     print >>sys.stderr, test, 'leaked', deltas[-huntrleaks[1]:], 'references'
  590.                     print >>refrep, test, 'leaked', deltas[-huntrleaks[1]:], 'references'
  591.                 
  592.         finally:
  593.             sys.stdout = save_stdout
  594.  
  595.     except test_support.ResourceDenied:
  596.         msg = None
  597.         if not quiet:
  598.             print test, 'skipped --', msg
  599.             sys.stdout.flush()
  600.         
  601.         return -2
  602.     except (ImportError, test_support.TestSkipped):
  603.         msg = None
  604.         if not quiet:
  605.             print test, 'skipped --', msg
  606.             sys.stdout.flush()
  607.         
  608.         return -1
  609.     except KeyboardInterrupt:
  610.         raise 
  611.     except test_support.TestFailed:
  612.         msg = None
  613.         print 'test', test, 'failed --', msg
  614.         sys.stdout.flush()
  615.         return 0
  616.     except:
  617.         (type, value) = sys.exc_info()[:2]
  618.         print 'test', test, 'crashed --', str(type) + ':', value
  619.         sys.stdout.flush()
  620.         if verbose:
  621.             traceback.print_exc(file = sys.stdout)
  622.             sys.stdout.flush()
  623.         
  624.         return 0
  625.  
  626.     if not cfp:
  627.         return 1
  628.     
  629.     output = cfp.getvalue()
  630.     if generate:
  631.         if output == test + '\n':
  632.             if os.path.exists(outputfile):
  633.                 print 'output file', outputfile, 'is no longer needed; consider removing it'
  634.             else:
  635.                 return 1
  636.         
  637.         fp = open(outputfile, 'w')
  638.         fp.write(output)
  639.         fp.close()
  640.         return 1
  641.     
  642.     if os.path.exists(outputfile):
  643.         fp = open(outputfile, 'r')
  644.         expected = fp.read()
  645.         fp.close()
  646.     else:
  647.         expected = test + '\n'
  648.     if output == expected or huntrleaks:
  649.         return 1
  650.     
  651.     print 'test', test, 'produced unexpected output:'
  652.     sys.stdout.flush()
  653.     reportdiff(expected, output)
  654.     sys.stdout.flush()
  655.     return 0
  656.  
  657.  
  658. def reportdiff(expected, output):
  659.     import difflib
  660.     print '*' * 70
  661.     a = expected.splitlines(1)
  662.     b = output.splitlines(1)
  663.     sm = difflib.SequenceMatcher(a = a, b = b)
  664.     tuples = sm.get_opcodes()
  665.     
  666.     def pair(x0, x1):
  667.         x0 += 1
  668.         if x0 >= x1:
  669.             return 'line ' + str(x0)
  670.         else:
  671.             return 'lines %d-%d' % (x0, x1)
  672.  
  673.     for op, a0, a1, b0, b1 in tuples:
  674.         if op == 'equal':
  675.             continue
  676.         if op == 'delete':
  677.             print '***', pair(a0, a1), 'of expected output missing:'
  678.             for line in a[a0:a1]:
  679.                 print '-', line,
  680.             
  681.         if op == 'replace':
  682.             print '*** mismatch between', pair(a0, a1), 'of expected', 'output and', pair(b0, b1), 'of actual output:'
  683.             for line in difflib.ndiff(a[a0:a1], b[b0:b1]):
  684.                 print line,
  685.             
  686.         if op == 'insert':
  687.             print '***', pair(b0, b1), "of actual output doesn't appear", 'in expected output after line', str(a1) + ':'
  688.             for line in b[b0:b1]:
  689.                 print '+', line,
  690.             
  691.         print 'get_opcodes() returned bad tuple?!?!', (op, a0, a1, b0, b1)
  692.     
  693.     print '*' * 70
  694.  
  695.  
  696. def findtestdir():
  697.     if __name__ == '__main__':
  698.         file = sys.argv[0]
  699.     else:
  700.         file = __file__
  701.     if not os.path.dirname(file):
  702.         pass
  703.     testdir = os.curdir
  704.     return testdir
  705.  
  706.  
  707. def removepy(name):
  708.     if name.endswith(os.extsep + 'py'):
  709.         name = name[:-3]
  710.     
  711.     return name
  712.  
  713.  
  714. def count(n, word):
  715.     if n == 1:
  716.         return '%d %s' % (n, word)
  717.     else:
  718.         return '%d %ss' % (n, word)
  719.  
  720.  
  721. def printlist(x, width = 70, indent = 4):
  722.     '''Print the elements of iterable x to stdout.
  723.  
  724.     Optional arg width (default 70) is the maximum line length.
  725.     Optional arg indent (default 4) is the number of blanks with which to
  726.     begin each line.
  727.     '''
  728.     fill = fill
  729.     import textwrap
  730.     blanks = ' ' * indent
  731.     print fill(' '.join(map(str, x)), width, initial_indent = blanks, subsequent_indent = blanks)
  732.  
  733. _expectations = {
  734.     'win32': '\n        test__locale\n        test_applesingle\n        test_al\n        test_bsddb185\n        test_bsddb3\n        test_cd\n        test_cl\n        test_commands\n        test_crypt\n        test_curses\n        test_dbm\n        test_dl\n        test_fcntl\n        test_fork1\n        test_gdbm\n        test_gl\n        test_grp\n        test_imgfile\n        test_ioctl\n        test_largefile\n        test_linuxaudiodev\n        test_mhlib\n        test_nis\n        test_openpty\n        test_ossaudiodev\n        test_poll\n        test_posix\n        test_pty\n        test_pwd\n        test_resource\n        test_signal\n        test_sunaudiodev\n        test_threadsignals\n        test_timing\n        ',
  735.     'linux2': '\n        test_al\n        test_applesingle\n        test_bsddb185\n        test_cd\n        test_cl\n        test_curses\n        test_dl\n        test_gl\n        test_imgfile\n        test_largefile\n        test_linuxaudiodev\n        test_nis\n        test_ntpath\n        test_ossaudiodev\n        test_sunaudiodev\n        ',
  736.     'mac': '\n        test_al\n        test_atexit\n        test_bsddb\n        test_bsddb185\n        test_bsddb3\n        test_bz2\n        test_cd\n        test_cl\n        test_commands\n        test_crypt\n        test_curses\n        test_dbm\n        test_dl\n        test_fcntl\n        test_fork1\n        test_gl\n        test_grp\n        test_ioctl\n        test_imgfile\n        test_largefile\n        test_linuxaudiodev\n        test_locale\n        test_mmap\n        test_nis\n        test_ntpath\n        test_openpty\n        test_ossaudiodev\n        test_poll\n        test_popen\n        test_popen2\n        test_posix\n        test_pty\n        test_pwd\n        test_resource\n        test_signal\n        test_sunaudiodev\n        test_sundry\n        test_tarfile\n        test_timing\n        ',
  737.     'unixware7': '\n        test_al\n        test_applesingle\n        test_bsddb\n        test_bsddb185\n        test_cd\n        test_cl\n        test_dl\n        test_gl\n        test_imgfile\n        test_largefile\n        test_linuxaudiodev\n        test_minidom\n        test_nis\n        test_ntpath\n        test_openpty\n        test_pyexpat\n        test_sax\n        test_sunaudiodev\n        test_sundry\n        ',
  738.     'openunix8': '\n        test_al\n        test_applesingle\n        test_bsddb\n        test_bsddb185\n        test_cd\n        test_cl\n        test_dl\n        test_gl\n        test_imgfile\n        test_largefile\n        test_linuxaudiodev\n        test_minidom\n        test_nis\n        test_ntpath\n        test_openpty\n        test_pyexpat\n        test_sax\n        test_sunaudiodev\n        test_sundry\n        ',
  739.     'sco_sv3': '\n        test_al\n        test_applesingle\n        test_asynchat\n        test_bsddb\n        test_bsddb185\n        test_cd\n        test_cl\n        test_dl\n        test_fork1\n        test_gettext\n        test_gl\n        test_imgfile\n        test_largefile\n        test_linuxaudiodev\n        test_locale\n        test_minidom\n        test_nis\n        test_ntpath\n        test_openpty\n        test_pyexpat\n        test_queue\n        test_sax\n        test_sunaudiodev\n        test_sundry\n        test_thread\n        test_threaded_import\n        test_threadedtempfile\n        test_threading\n        ',
  740.     'riscos': '\n        test_al\n        test_applesingle\n        test_asynchat\n        test_atexit\n        test_bsddb\n        test_bsddb185\n        test_bsddb3\n        test_cd\n        test_cl\n        test_commands\n        test_crypt\n        test_dbm\n        test_dl\n        test_fcntl\n        test_fork1\n        test_gdbm\n        test_gl\n        test_grp\n        test_imgfile\n        test_largefile\n        test_linuxaudiodev\n        test_locale\n        test_mmap\n        test_nis\n        test_ntpath\n        test_openpty\n        test_poll\n        test_popen2\n        test_pty\n        test_pwd\n        test_strop\n        test_sunaudiodev\n        test_sundry\n        test_thread\n        test_threaded_import\n        test_threadedtempfile\n        test_threading\n        test_timing\n        ',
  741.     'darwin': '\n        test__locale\n        test_al\n        test_bsddb\n        test_bsddb3\n        test_cd\n        test_cl\n        test_curses\n        test_dl\n        test_gdbm\n        test_gl\n        test_imgfile\n        test_largefile\n        test_linuxaudiodev\n        test_locale\n        test_minidom\n        test_nis\n        test_ntpath\n        test_ossaudiodev\n        test_poll\n        test_sunaudiodev\n        ',
  742.     'sunos5': '\n        test_al\n        test_applesingle\n        test_bsddb\n        test_bsddb185\n        test_cd\n        test_cl\n        test_curses\n        test_dbm\n        test_gdbm\n        test_gl\n        test_gzip\n        test_imgfile\n        test_linuxaudiodev\n        test_openpty\n        test_zipfile\n        test_zlib\n        ',
  743.     'hp-ux11': '\n        test_al\n        test_applesingle\n        test_bsddb\n        test_bsddb185\n        test_cd\n        test_cl\n        test_curses\n        test_dl\n        test_gdbm\n        test_gl\n        test_gzip\n        test_imgfile\n        test_largefile\n        test_linuxaudiodev\n        test_locale\n        test_minidom\n        test_nis\n        test_ntpath\n        test_openpty\n        test_pyexpat\n        test_sax\n        test_sunaudiodev\n        test_zipfile\n        test_zlib\n        ',
  744.     'atheos': '\n        test_al\n        test_applesingle\n        test_bsddb185\n        test_cd\n        test_cl\n        test_curses\n        test_dl\n        test_gdbm\n        test_gl\n        test_imgfile\n        test_largefile\n        test_linuxaudiodev\n        test_locale\n        test_mhlib\n        test_mmap\n        test_nis\n        test_poll\n        test_popen2\n        test_resource\n        test_sunaudiodev\n        ',
  745.     'cygwin': '\n        test_al\n        test_applesingle\n        test_bsddb185\n        test_bsddb3\n        test_cd\n        test_cl\n        test_curses\n        test_dbm\n        test_gl\n        test_imgfile\n        test_ioctl\n        test_largefile\n        test_linuxaudiodev\n        test_locale\n        test_nis\n        test_ossaudiodev\n        test_socketserver\n        test_sunaudiodev\n        ',
  746.     'os2emx': '\n        test_al\n        test_applesingle\n        test_audioop\n        test_bsddb185\n        test_bsddb3\n        test_cd\n        test_cl\n        test_commands\n        test_curses\n        test_dl\n        test_gl\n        test_imgfile\n        test_largefile\n        test_linuxaudiodev\n        test_mhlib\n        test_mmap\n        test_nis\n        test_openpty\n        test_ossaudiodev\n        test_pty\n        test_resource\n        test_signal\n        test_sunaudiodev\n        ',
  747.     'freebsd4': '\n        test_aepack\n        test_al\n        test_applesingle\n        test_bsddb\n        test_bsddb3\n        test_cd\n        test_cl\n        test_gdbm\n        test_gl\n        test_imgfile\n        test_linuxaudiodev\n        test_locale\n        test_macfs\n        test_macostools\n        test_nis\n        test_normalization\n        test_ossaudiodev\n        test_pep277\n        test_plistlib\n        test_pty\n        test_scriptpackages\n        test_socket_ssl\n        test_socketserver\n        test_sunaudiodev\n        test_tcl\n        test_timeout\n        test_unicode_file\n        test_urllibnet\n        test_winreg\n        test_winsound\n        ',
  748.     'aix5': '\n        test_aepack\n        test_al\n        test_applesingle\n        test_bsddb\n        test_bsddb185\n        test_bsddb3\n        test_bz2\n        test_cd\n        test_cl\n        test_dl\n        test_gdbm\n        test_gl\n        test_gzip\n        test_imgfile\n        test_linuxaudiodev\n        test_macfs\n        test_macostools\n        test_nis\n        test_ossaudiodev\n        test_sunaudiodev\n        test_tcl\n        test_winreg\n        test_winsound\n        test_zipimport\n        test_zlib\n        ' }
  749. _expectations['freebsd5'] = _expectations['freebsd4']
  750. _expectations['freebsd6'] = _expectations['freebsd4']
  751.  
  752. class _ExpectedSkips:
  753.     
  754.     def __init__(self):
  755.         import os.path as os
  756.         test_normalization = test_normalization
  757.         import test
  758.         test_socket_ssl = test_socket_ssl
  759.         import test
  760.         test_timeout = test_timeout
  761.         import test
  762.         test_codecmaps_cn = test_codecmaps_cn
  763.         test_codecmaps_jp = test_codecmaps_jp
  764.         import test
  765.         test_codecmaps_kr = test_codecmaps_kr
  766.         test_codecmaps_tw = test_codecmaps_tw
  767.         import test
  768.         test_codecmaps_hk = test_codecmaps_hk
  769.         import test
  770.         self.valid = False
  771.         if sys.platform in _expectations:
  772.             s = _expectations[sys.platform]
  773.             self.expected = set(s.split())
  774.             if not os.path.supports_unicode_filenames:
  775.                 self.expected.add('test_pep277')
  776.             
  777.             if test_normalization.skip_expected:
  778.                 self.expected.add('test_normalization')
  779.             
  780.             if test_socket_ssl.skip_expected:
  781.                 self.expected.add('test_socket_ssl')
  782.             
  783.             if test_timeout.skip_expected:
  784.                 self.expected.add('test_timeout')
  785.             
  786.             for cc in ('cn', 'jp', 'kr', 'tw', 'hk'):
  787.                 if eval('test_codecmaps_' + cc).skip_expected:
  788.                     self.expected.add('test_codecmaps_' + cc)
  789.                     continue
  790.             
  791.             if sys.maxint == 0x7FFFFFFFFFFFFFFFL:
  792.                 self.expected.add('test_rgbimg')
  793.                 self.expected.add('test_imageop')
  794.             
  795.             if sys.platform not in ('mac', 'darwin'):
  796.                 MAC_ONLY = [
  797.                     'test_macostools',
  798.                     'test_macfs',
  799.                     'test_aepack',
  800.                     'test_plistlib',
  801.                     'test_scriptpackages']
  802.                 for skip in MAC_ONLY:
  803.                     self.expected.add(skip)
  804.                 
  805.             
  806.             if sys.platform != 'win32':
  807.                 WIN_ONLY = [
  808.                     'test_unicode_file',
  809.                     'test_winreg',
  810.                     'test_winsound']
  811.                 for skip in WIN_ONLY:
  812.                     self.expected.add(skip)
  813.                 
  814.             
  815.             self.valid = True
  816.         
  817.  
  818.     
  819.     def isvalid(self):
  820.         '''Return true iff _ExpectedSkips knows about the current platform.'''
  821.         return self.valid
  822.  
  823.     
  824.     def getexpected(self):
  825.         '''Return set of test names we expect to skip on current platform.
  826.  
  827.         self.isvalid() must be true.
  828.         '''
  829.         if not self.isvalid():
  830.             raise AssertionError
  831.         return self.expected
  832.  
  833.  
  834. if __name__ == '__main__':
  835.     mydir = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
  836.     i = pathlen = len(sys.path)
  837.     while i >= 0:
  838.         i -= 1
  839.         if os.path.abspath(os.path.normpath(sys.path[i])) == mydir:
  840.             del sys.path[i]
  841.         
  842.     if len(sys.path) == pathlen:
  843.         print 'Could not find %r in sys.path to remove it' % mydir
  844.     
  845.     main()
  846.  
  847.