1#!/usr/local/bin/python2.7
2
3"""
4Usage:
5
6python -m test.regrtest [options] [test_name1 [test_name2 ...]]
7python path/to/Lib/test/regrtest.py [options] [test_name1 [test_name2 ...]]
8
9
10If no arguments or options are provided, finds all files matching
11the pattern "test_*" in the Lib/test subdirectory and runs
12them in alphabetical order (but see -M and -u, below, for exceptions).
13
14For more rigorous testing, it is useful to use the following
15command line:
16
17python -E -tt -Wd -3 -m test.regrtest [options] [test_name1 ...]
18
19
20Options:
21
22-h/--help       -- print this text and exit
23
24Verbosity
25
26-v/--verbose    -- run tests in verbose mode with output to stdout
27-w/--verbose2   -- re-run failed tests in verbose mode
28-W/--verbose3   -- re-run failed tests in verbose mode immediately
29-q/--quiet      -- no output unless one or more tests fail
30-S/--slowest    -- print the slowest 10 tests
31   --header     -- print header with interpreter info
32
33Selecting tests
34
35-r/--randomize  -- randomize test execution order (see below)
36   --randseed   -- pass a random seed to reproduce a previous random run
37-f/--fromfile   -- read names of tests to run from a file (see below)
38-x/--exclude    -- arguments are tests to *exclude*
39-s/--single     -- single step through a set of tests (see below)
40-m/--match PAT  -- match test cases and methods with glob pattern PAT
41--matchfile FILENAME -- filters tests using a text file, one pattern per line
42-G/--failfast   -- fail as soon as a test fails (only with -v or -W)
43-u/--use RES1,RES2,...
44                -- specify which special resource intensive tests to run
45-M/--memlimit LIMIT
46                -- run very large memory-consuming tests
47
48Special runs
49
50-l/--findleaks  -- if GC is available detect tests that leak memory
51-L/--runleaks   -- run the leaks(1) command just before exit
52-R/--huntrleaks RUNCOUNTS
53                -- search for reference leaks (needs debug build, v. slow)
54-j/--multiprocess PROCESSES
55                -- run PROCESSES processes at once
56-T/--coverage   -- turn on code coverage tracing using the trace module
57-D/--coverdir DIRECTORY
58                -- Directory where coverage files are put
59-N/--nocoverdir -- Put coverage files alongside modules
60-t/--threshold THRESHOLD
61                -- call gc.set_threshold(THRESHOLD)
62-F/--forever    -- run the specified tests in a loop, until an error happens
63-P/--pgo        -- enable Profile Guided Optimization training
64--testdir       -- execute test files in the specified directory
65                   (instead of the Python stdlib test suite)
66--list-tests    -- only write the name of tests that will be run,
67                   don't execute them
68--list-cases    -- only write the name of test cases that will be run,
69                   don't execute them
70--fail-env-changed  -- if a test file alters the environment, mark the test
71                       as failed
72
73
74Additional Option Details:
75
76-r randomizes test execution order. You can use --randseed=int to provide an
77int seed value for the randomizer; this is useful for reproducing troublesome
78test orders.
79
80-s On the first invocation of regrtest using -s, the first test file found
81or the first test file given on the command line is run, and the name of
82the next test is recorded in a file named pynexttest.  If run from the
83Python build directory, pynexttest is located in the 'build' subdirectory,
84otherwise it is located in tempfile.gettempdir().  On subsequent runs,
85the test in pynexttest is run, and the next test is written to pynexttest.
86When the last test has been run, pynexttest is deleted.  In this way it
87is possible to single step through the test files.  This is useful when
88doing memory analysis on the Python interpreter, which process tends to
89consume too many resources to run the full regression test non-stop.
90
91-f reads the names of tests from the file given as f's argument, one
92or more test names per line.  Whitespace is ignored.  Blank lines and
93lines beginning with '#' are ignored.  This is especially useful for
94whittling down failures involving interactions among tests.
95
96-L causes the leaks(1) command to be run just before exit if it exists.
97leaks(1) is available on Mac OS X and presumably on some other
98FreeBSD-derived systems.
99
100-R runs each test several times and examines sys.gettotalrefcount() to
101see if the test appears to be leaking references.  The argument should
102be of the form stab:run:fname where 'stab' is the number of times the
103test is run to let gettotalrefcount settle down, 'run' is the number
104of times further it is run and 'fname' is the name of the file the
105reports are written to.  These parameters all have defaults (5, 4 and
106"reflog.txt" respectively), and the minimal invocation is '-R :'.
107
108-M runs tests that require an exorbitant amount of memory. These tests
109typically try to ascertain containers keep working when containing more than
1102 billion objects, which only works on 64-bit systems. There are also some
111tests that try to exhaust the address space of the process, which only makes
112sense on 32-bit systems with at least 2Gb of memory. The passed-in memlimit,
113which is a string in the form of '2.5Gb', determines howmuch memory the
114tests will limit themselves to (but they may go slightly over.) The number
115shouldn't be more memory than the machine has (including swap memory). You
116should also keep in mind that swap memory is generally much, much slower
117than RAM, and setting memlimit to all available RAM or higher will heavily
118tax the machine. On the other hand, it is no use running these tests with a
119limit of less than 2.5Gb, and many require more than 20Gb. Tests that expect
120to use more than memlimit memory will be skipped. The big-memory tests
121generally run very, very long.
122
123-u is used to specify which special resource intensive tests to run,
124such as those requiring large file support or network connectivity.
125The argument is a comma-separated list of words indicating the
126resources to test.  Currently only the following are defined:
127
128    all -       Enable all special resources.
129
130    audio -     Tests that use the audio device.  (There are known
131                cases of broken audio drivers that can crash Python or
132                even the Linux kernel.)
133
134    curses -    Tests that use curses and will modify the terminal's
135                state and output modes.
136
137    largefile - It is okay to run some test that may create huge
138                files.  These tests can take a long time and may
139                consume >2GB of disk space temporarily.
140
141    network -   It is okay to run tests that use external network
142                resource, e.g. testing SSL support for sockets.
143
144    bsddb -     It is okay to run the bsddb testsuite, which takes
145                a long time to complete.
146
147    decimal -   Test the decimal module against a large suite that
148                verifies compliance with standards.
149
150    cpu -       Used for certain CPU-heavy tests.
151
152    subprocess  Run all tests for the subprocess module.
153
154    urlfetch -  It is okay to download files required on testing.
155
156    gui -       Run tests that require a running GUI.
157
158    xpickle -   Test pickle and cPickle against Python 2.4, 2.5 and 2.6 to
159                test backwards compatibility. These tests take a long time
160                to run.
161
162To enable all resources except one, use '-uall,-<resource>'.  For
163example, to run all the tests except for the bsddb tests, give the
164option '-uall,-bsddb'.
165
166--matchfile filters tests using a text file, one pattern per line.
167Pattern examples:
168
169- test method: test_stat_attributes
170- test class: FileTests
171- test identifier: test_os.FileTests.test_stat_attributes
172"""
173
174import StringIO
175import datetime
176import getopt
177import imp
178import json
179import math
180import os
181import platform
182import random
183import re
184import shutil
185import sys
186import sysconfig
187import tempfile
188import time
189import traceback
190import unittest
191import warnings
192
193
194# Some times __path__ and __file__ are not absolute (e.g. while running from
195# Lib/) and, if we change the CWD to run the tests in a temporary dir, some
196# imports might fail.  This affects only the modules imported before os.chdir().
197# These modules are searched first in sys.path[0] (so '' -- the CWD) and if
198# they are found in the CWD their __file__ and __path__ will be relative (this
199# happens before the chdir).  All the modules imported after the chdir, are
200# not found in the CWD, and since the other paths in sys.path[1:] are absolute
201# (site.py absolutize them), the __file__ and __path__ will be absolute too.
202# Therefore it is necessary to absolutize manually the __file__ and __path__ of
203# the packages to prevent later imports to fail when the CWD is different.
204for module in sys.modules.itervalues():
205    if hasattr(module, '__path__'):
206        module.__path__ = [os.path.abspath(path) for path in module.__path__]
207    if hasattr(module, '__file__'):
208        module.__file__ = os.path.abspath(module.__file__)
209
210
211# MacOSX (a.k.a. Darwin) has a default stack size that is too small
212# for deeply recursive regular expressions.  We see this as crashes in
213# the Python test suite when running test_re.py and test_sre.py.  The
214# fix is to set the stack limit to 2048.
215# This approach may also be useful for other Unixy platforms that
216# suffer from small default stack limits.
217if sys.platform == 'darwin':
218    try:
219        import resource
220    except ImportError:
221        pass
222    else:
223        soft, hard = resource.getrlimit(resource.RLIMIT_STACK)
224        newsoft = min(hard, max(soft, 1024*2048))
225        resource.setrlimit(resource.RLIMIT_STACK, (newsoft, hard))
226
227# Windows, Tkinter, and resetting the environment after each test don't
228# mix well.  To alleviate test failures due to Tcl/Tk not being able to
229# find its library, get the necessary environment massage done once early.
230if sys.platform == 'win32':
231    try:
232        import FixTk
233    except Exception:
234        pass
235
236# Test result constants.
237PASSED = 1
238FAILED = 0
239ENV_CHANGED = -1
240SKIPPED = -2
241RESOURCE_DENIED = -3
242INTERRUPTED = -4
243CHILD_ERROR = -5   # error in a child process
244TEST_DID_NOT_RUN = -6   # error in a child process
245
246# Minimum duration of a test to display its duration or to mention that
247# the test is running in background
248PROGRESS_MIN_TIME = 30.0   # seconds
249
250# Display the running tests if nothing happened last N seconds
251PROGRESS_UPDATE = 30.0   # seconds
252
253from test import support
254
255ALL_RESOURCES = ('audio', 'curses', 'largefile', 'network', 'bsddb',
256                 'decimal', 'cpu', 'subprocess', 'urlfetch', 'gui',
257                 'xpickle')
258
259# Other resources excluded from --use=all:
260#
261# - extralagefile (ex: test_zipfile64): really too slow to be enabled
262#   "by default"
263RESOURCE_NAMES = ALL_RESOURCES + ('extralargefile',)
264
265TEMPDIR = os.path.abspath(tempfile.gettempdir())
266
267
268def usage(code, msg=''):
269    print __doc__
270    if msg: print msg
271    sys.exit(code)
272
273
274def format_duration(seconds):
275    ms = int(math.ceil(seconds * 1e3))
276    seconds, ms = divmod(ms, 1000)
277    minutes, seconds = divmod(seconds, 60)
278    hours, minutes = divmod(minutes, 60)
279
280    parts = []
281    if hours:
282        parts.append('%s hour' % hours)
283    if minutes:
284        parts.append('%s min' % minutes)
285    if seconds:
286        parts.append('%s sec' % seconds)
287    if ms:
288        parts.append('%s ms' % ms)
289    if not parts:
290        return '0 ms'
291
292    parts = parts[:2]
293    return ' '.join(parts)
294
295
296_FORMAT_TEST_RESULT = {
297    PASSED: '%s passed',
298    FAILED: '%s failed',
299    ENV_CHANGED: '%s failed (env changed)',
300    SKIPPED: '%s skipped',
301    RESOURCE_DENIED: '%s skipped (resource denied)',
302    INTERRUPTED: '%s interrupted',
303    CHILD_ERROR: '%s crashed',
304    TEST_DID_NOT_RUN: '%s run no tests',
305}
306
307
308def format_test_result(test_name, result):
309    fmt = _FORMAT_TEST_RESULT.get(result, "%s")
310    return fmt % test_name
311
312
313def cpu_count():
314    # first try os.sysconf() to prevent loading the big multiprocessing module
315    try:
316        return os.sysconf('SC_NPROCESSORS_ONLN')
317    except (AttributeError, ValueError):
318        pass
319
320    # try multiprocessing.cpu_count()
321    try:
322        import multiprocessing
323    except ImportError:
324        pass
325    else:
326        return multiprocessing.cpu_count()
327
328    return None
329
330
331def unload_test_modules(save_modules):
332    # Unload the newly imported modules (best effort finalization)
333    for module in sys.modules.keys():
334        if module not in save_modules and module.startswith("test."):
335            support.unload(module)
336
337
338def main(tests=None, testdir=None, verbose=0, quiet=False,
339         exclude=False, single=False, randomize=False, fromfile=None,
340         findleaks=False, use_resources=None, trace=False, coverdir='coverage',
341         runleaks=False, huntrleaks=False, verbose2=False, print_slow=False,
342         random_seed=None, use_mp=None, verbose3=False, forever=False,
343         header=False, pgo=False, failfast=False, match_tests=None):
344    """Execute a test suite.
345
346    This also parses command-line options and modifies its behavior
347    accordingly.
348
349    tests -- a list of strings containing test names (optional)
350    testdir -- the directory in which to look for tests (optional)
351
352    Users other than the Python test suite will certainly want to
353    specify testdir; if it's omitted, the directory containing the
354    Python test suite is searched for.
355
356    If the tests argument is omitted, the tests listed on the
357    command-line will be used.  If that's empty, too, then all *.py
358    files beginning with test_ will be used.
359
360    The other default arguments (verbose, quiet, exclude,
361    single, randomize, findleaks, use_resources, trace, coverdir,
362    print_slow, and random_seed) allow programmers calling main()
363    directly to set the values that would normally be set by flags
364    on the command line.
365    """
366    regrtest_start_time = time.time()
367
368    support.record_original_stdout(sys.stdout)
369    try:
370        opts, args = getopt.getopt(sys.argv[1:], 'hvqxsSrf:lu:t:TD:NLR:FwWM:j:PGm:',
371            ['help', 'verbose', 'verbose2', 'verbose3', 'quiet',
372             'exclude', 'single', 'slow', 'slowest', 'randomize', 'fromfile=',
373             'findleaks',
374             'use=', 'threshold=', 'trace', 'coverdir=', 'nocoverdir',
375             'runleaks', 'huntrleaks=', 'memlimit=', 'randseed=',
376             'multiprocess=', 'slaveargs=', 'forever', 'header', 'pgo',
377             'failfast', 'match=', 'testdir=', 'list-tests', 'list-cases',
378             'coverage', 'matchfile=', 'fail-env-changed', 'cleanup'])
379    except getopt.error, msg:
380        usage(2, msg)
381
382    # Defaults
383    if random_seed is None:
384        random_seed = random.randrange(10000000)
385    if use_resources is None:
386        use_resources = []
387    slaveargs = None
388    list_tests = False
389    list_cases_opt = False
390    fail_env_changed = False
391    cleanup_tests = False
392    for o, a in opts:
393        if o in ('-h', '--help'):
394            usage(0)
395        elif o in ('-v', '--verbose'):
396            verbose += 1
397        elif o in ('-w', '--verbose2'):
398            verbose2 = True
399        elif o in ('-W', '--verbose3'):
400            verbose3 = True
401        elif o in ('-G', '--failfast'):
402            failfast = True
403        elif o in ('-q', '--quiet'):
404            quiet = True;
405            verbose = 0
406        elif o in ('-x', '--exclude'):
407            exclude = True
408        elif o in ('-s', '--single'):
409            single = True
410        elif o in ('-S', '--slow', '--slowest'):
411            print_slow = True
412        elif o in ('-r', '--randomize'):
413            randomize = True
414        elif o == '--randseed':
415            random_seed = int(a)
416        elif o in ('-f', '--fromfile'):
417            fromfile = a
418        elif o in ('-m', '--match'):
419            if match_tests is None:
420                match_tests = []
421            match_tests.append(a)
422        elif o == '--matchfile':
423            if match_tests is None:
424                match_tests = []
425            filename = os.path.join(support.SAVEDCWD, a)
426            with open(filename) as fp:
427                for line in fp:
428                    match_tests.append(line.strip())
429        elif o in ('-l', '--findleaks'):
430            findleaks = True
431        elif o in ('-L', '--runleaks'):
432            runleaks = True
433        elif o in ('-t', '--threshold'):
434            import gc
435            gc.set_threshold(int(a))
436        elif o in ('-T', '--coverage'):
437            trace = True
438        elif o in ('-D', '--coverdir'):
439            coverdir = os.path.join(os.getcwd(), a)
440        elif o in ('-N', '--nocoverdir'):
441            coverdir = None
442        elif o in ('-R', '--huntrleaks'):
443            huntrleaks = a.split(':')
444            if len(huntrleaks) not in (2, 3):
445                print a, huntrleaks
446                usage(2, '-R takes 2 or 3 colon-separated arguments')
447            if not huntrleaks[0]:
448                huntrleaks[0] = 5
449            else:
450                huntrleaks[0] = int(huntrleaks[0])
451            if not huntrleaks[1]:
452                huntrleaks[1] = 4
453            else:
454                huntrleaks[1] = int(huntrleaks[1])
455            if len(huntrleaks) == 2 or not huntrleaks[2]:
456                huntrleaks[2:] = ["reflog.txt"]
457        elif o in ('-M', '--memlimit'):
458            support.set_memlimit(a)
459        elif o in ('-u', '--use'):
460            u = [x.lower() for x in a.split(',')]
461            for r in u:
462                if r == 'all':
463                    use_resources[:] = ALL_RESOURCES
464                    continue
465                remove = False
466                if r[0] == '-':
467                    remove = True
468                    r = r[1:]
469                if r not in RESOURCE_NAMES:
470                    usage(1, 'Invalid -u/--use option: ' + a)
471                if remove:
472                    if r in use_resources:
473                        use_resources.remove(r)
474                elif r not in use_resources:
475                    use_resources.append(r)
476        elif o in ('-F', '--forever'):
477            forever = True
478        elif o in ('-j', '--multiprocess'):
479            use_mp = int(a)
480        elif o == '--header':
481            header = True
482        elif o == '--slaveargs':
483            slaveargs = a
484        elif o in ('-P', '--pgo'):
485            pgo = True
486        elif o == '--testdir':
487            testdir = a
488        elif o == '--list-tests':
489            list_tests = True
490        elif o == '--list-cases':
491            list_cases_opt = True
492        elif o == '--fail-env-changed':
493            fail_env_changed = True
494        elif o == '--cleanup':
495            cleanup_tests = True
496        else:
497            print >>sys.stderr, ("No handler for option {}.  Please "
498                "report this as a bug at http://bugs.python.org.").format(o)
499            sys.exit(1)
500    if single and fromfile:
501        usage(2, "-s and -f don't go together!")
502    if use_mp and trace:
503        usage(2, "-T and -j don't go together!")
504    if use_mp and findleaks:
505        usage(2, "-l and -j don't go together!")
506    if failfast and not (verbose or verbose3):
507        usage("-G/--failfast needs either -v or -W")
508
509    if testdir:
510        testdir = os.path.abspath(testdir)
511
512        # Prepend test directory to sys.path, so runtest() will be able
513        # to locate tests
514        sys.path.insert(0, testdir)
515
516    # Make sure that '' and Lib/test/ are not in sys.path
517    regrtest_dir = os.path.abspath(os.path.dirname(__file__))
518    for path in ('', regrtest_dir):
519        try:
520            sys.path.remove(path)
521        except ValueError:
522            pass
523
524    if huntrleaks:
525        warmup, repetitions, _ = huntrleaks
526        if warmup < 1 or repetitions < 1:
527            msg = ("Invalid values for the --huntrleaks/-R parameters. The "
528                   "number of warmups and repetitions must be at least 1 "
529                   "each (1:1).")
530            print >>sys.stderr, msg
531            sys.exit(2)
532
533    if cleanup_tests:
534        import glob
535
536        os.chdir(support.SAVEDCWD)
537        path = os.path.join(TEMPDIR, 'test_python_*')
538        print("Cleanup %s directory" % TEMPDIR)
539        for name in glob.glob(path):
540            if os.path.isdir(name):
541                print("Remove directory: %s" % name)
542                support.rmtree(name)
543            else:
544                print("Remove file: %s" % name)
545                support.unlink(name)
546        sys.exit(0)
547
548
549    if slaveargs is not None:
550        args, kwargs = json.loads(slaveargs)
551        if testdir:
552            kwargs['testdir'] = testdir
553        try:
554            result = runtest(*args, **kwargs)
555        except BaseException, e:
556            result = INTERRUPTED, e.__class__.__name__
557        print   # Force a newline (just in case)
558        print json.dumps(result)
559        sys.exit(0)
560
561    good = []
562    bad = []
563    skipped = []
564    resource_denieds = []
565    environment_changed = []
566    rerun = []
567    run_no_tests = []
568    first_result = None
569    interrupted = False
570
571    if findleaks:
572        try:
573            import gc
574        except ImportError:
575            print 'No GC available, disabling findleaks.'
576            findleaks = False
577        else:
578            # Uncomment the line below to report garbage that is not
579            # freeable by reference counting alone.  By default only
580            # garbage that is not collectable by the GC is reported.
581            #gc.set_debug(gc.DEBUG_SAVEALL)
582            found_garbage = []
583
584    if single:
585        filename = os.path.join(TEMPDIR, 'pynexttest')
586        try:
587            fp = open(filename, 'r')
588            next_test = fp.read().strip()
589            tests = [next_test]
590            fp.close()
591        except IOError:
592            pass
593
594    if fromfile:
595        tests = []
596        fp = open(os.path.join(support.SAVEDCWD, fromfile))
597        for line in fp:
598            guts = line.split() # assuming no test has whitespace in its name
599            if guts and not guts[0].startswith('#'):
600                tests.extend(guts)
601        fp.close()
602
603    # Strip .py extensions.
604    removepy(args)
605    removepy(tests)
606
607    stdtests = STDTESTS[:]
608    nottests = NOTTESTS.copy()
609    if exclude:
610        for arg in args:
611            if arg in stdtests:
612                stdtests.remove(arg)
613            nottests.add(arg)
614        args = []
615
616    if huntrleaks:
617        # FIXME: bpo-31731: test_io hangs with --huntrleaks
618        print("Warning: bpo-31731: test_io hangs with --huntrleaks: "
619              "exclude the test")
620        nottests.add('test_io')
621
622    display_header = (verbose or header or not (quiet or single or tests or args)) and (not pgo)
623    alltests = findtests(testdir, stdtests, nottests)
624    selected = tests or args or alltests
625    if single:
626        selected = selected[:1]
627        try:
628            next_single_test = alltests[alltests.index(selected[0])+1]
629        except IndexError:
630            next_single_test = None
631
632    if list_tests:
633        for name in selected:
634            print(name)
635        sys.exit(0)
636
637    if list_cases_opt:
638        list_cases(testdir, selected, match_tests)
639        sys.exit(0)
640
641    if trace:
642        import trace
643        tracer = trace.Trace(trace=False, count=True)
644
645    test_times = []
646    support.use_resources = use_resources
647    save_modules = set(sys.modules)
648
649    def accumulate_result(test, result):
650        ok, test_time = result
651        if ok not in (CHILD_ERROR, INTERRUPTED):
652            test_times.append((test_time, test))
653        if ok == PASSED:
654            good.append(test)
655        elif ok in (FAILED, CHILD_ERROR):
656            bad.append(test)
657        elif ok == ENV_CHANGED:
658            environment_changed.append(test)
659        elif ok == SKIPPED:
660            skipped.append(test)
661        elif ok == RESOURCE_DENIED:
662            skipped.append(test)
663            resource_denieds.append(test)
664        elif ok == TEST_DID_NOT_RUN:
665            run_no_tests.append(test)
666        elif ok != INTERRUPTED:
667            raise ValueError("invalid test result: %r" % ok)
668
669    if forever:
670        def test_forever(tests=list(selected)):
671            while True:
672                for test in tests:
673                    yield test
674                    if bad:
675                        return
676                    if fail_env_changed and environment_changed:
677                        return
678        tests = test_forever()
679        test_count = ''
680        test_count_width = 3
681    else:
682        tests = iter(selected)
683        test_count = '/{}'.format(len(selected))
684        test_count_width = len(test_count) - 1
685
686    def display_progress(test_index, test):
687        # "[ 51/405/1] test_tcl"
688        line = "{1:{0}}{2}".format(test_count_width, test_index, test_count)
689        fails = len(bad) + len(environment_changed)
690        if fails and not pgo:
691            line = '{}/{}'.format(line, fails)
692        line = '[{}]'.format(line)
693
694        # add the system load prefix: "load avg: 1.80 "
695        if hasattr(os, 'getloadavg'):
696            load_avg_1min = os.getloadavg()[0]
697            line = "load avg: {:.2f} {}".format(load_avg_1min, line)
698
699        # add the timestamp prefix:  "0:01:05 "
700        test_time = time.time() - regrtest_start_time
701        test_time = datetime.timedelta(seconds=int(test_time))
702        line = "%s %s" % (test_time, line)
703
704        # add the test name
705        line = "{} {}".format(line, test)
706
707        print(line)
708        sys.stdout.flush()
709
710    # For a partial run, we do not need to clutter the output.
711    if display_header:
712        # Print basic platform information
713        print "==", platform.python_implementation(), \
714                    " ".join(sys.version.split())
715        print "==  ", platform.platform(aliased=True), \
716                      "%s-endian" % sys.byteorder
717        print "==  ", os.getcwd()
718        ncpu = cpu_count()
719        if ncpu:
720            print "== CPU count:", ncpu
721
722    if huntrleaks:
723        warmup, repetitions, _ = huntrleaks
724        if warmup < 3:
725            print("WARNING: Running tests with --huntrleaks/-R and less than "
726                  "3 warmup repetitions can give false positives!")
727
728    if randomize:
729        random.seed(random_seed)
730        print "Using random seed", random_seed
731        random.shuffle(selected)
732
733    if use_mp:
734        try:
735            from threading import Thread
736        except ImportError:
737            print "Multiprocess option requires thread support"
738            sys.exit(2)
739        from Queue import Queue, Empty
740        from subprocess import Popen, PIPE
741        debug_output_pat = re.compile(r"\[\d+ refs\]$")
742        output = Queue()
743        def tests_and_args():
744            for test in tests:
745                args_tuple = (
746                    (test, verbose, quiet),
747                    dict(huntrleaks=huntrleaks, use_resources=use_resources,
748                         failfast=failfast,
749                         match_tests=match_tests,
750                         pgo=pgo)
751                )
752                yield (test, args_tuple)
753        pending = tests_and_args()
754        opt_args = support.args_from_interpreter_flags()
755        base_cmd = [sys.executable] + opt_args + ['-m', 'test.regrtest']
756        # required to spawn a new process with PGO flag on/off
757        if pgo:
758            base_cmd = base_cmd + ['--pgo']
759
760        class MultiprocessThread(Thread):
761            current_test = None
762            start_time = None
763
764            def runtest(self):
765                try:
766                    test, args_tuple = next(pending)
767                except StopIteration:
768                    output.put((None, None, None, None))
769                    return True
770
771                # -E is needed by some tests, e.g. test_import
772                args = base_cmd + ['--slaveargs', json.dumps(args_tuple)]
773                if testdir:
774                    args.extend(('--testdir', testdir))
775                try:
776                    self.start_time = time.time()
777                    self.current_test = test
778                    popen = Popen(args,
779                                  stdout=PIPE, stderr=PIPE,
780                                  universal_newlines=True,
781                                  close_fds=(os.name != 'nt'))
782                    stdout, stderr = popen.communicate()
783                    retcode = popen.wait()
784                finally:
785                    self.current_test = None
786
787                # Strip last refcount output line if it exists, since it
788                # comes from the shutdown of the interpreter in the subcommand.
789                stderr = debug_output_pat.sub("", stderr)
790
791                if retcode == 0:
792                    stdout, _, result = stdout.strip().rpartition("\n")
793                    if not result:
794                        output.put((None, None, None, None))
795                        return True
796
797                    result = json.loads(result)
798                else:
799                    result = (CHILD_ERROR, "Exit code %s" % retcode)
800
801                output.put((test, stdout.rstrip(), stderr.rstrip(), result))
802                return False
803
804            def run(self):
805                try:
806                    stop = False
807                    while not stop:
808                        stop = self.runtest()
809                except BaseException:
810                    output.put((None, None, None, None))
811                    raise
812
813        workers = [MultiprocessThread() for i in range(use_mp)]
814        print("Run tests in parallel using %s child processes"
815              % len(workers))
816        for worker in workers:
817            worker.start()
818
819        def get_running(workers):
820            running = []
821            for worker in workers:
822                current_test = worker.current_test
823                if not current_test:
824                    continue
825                dt = time.time() - worker.start_time
826                if dt >= PROGRESS_MIN_TIME:
827                    running.append('%s (%s)' % (current_test, format_duration(dt)))
828            return running
829
830        finished = 0
831        test_index = 1
832        get_timeout = max(PROGRESS_UPDATE, PROGRESS_MIN_TIME)
833        try:
834            while finished < use_mp:
835                try:
836                    item = output.get(timeout=get_timeout)
837                except Empty:
838                    running = get_running(workers)
839                    if running and not pgo:
840                        print('running: %s' % ', '.join(running))
841                        sys.stdout.flush()
842                    continue
843
844                test, stdout, stderr, result = item
845                if test is None:
846                    finished += 1
847                    continue
848                accumulate_result(test, result)
849                if not quiet:
850                    ok, test_time = result
851                    text = format_test_result(test, ok)
852                    if (ok not in (CHILD_ERROR, INTERRUPTED)
853                        and test_time >= PROGRESS_MIN_TIME
854                        and not pgo):
855                        text += ' (%s)' % format_duration(test_time)
856                    running = get_running(workers)
857                    if running and not pgo:
858                        text += ' -- running: %s' % ', '.join(running)
859                    display_progress(test_index, text)
860
861                if stdout:
862                    print(stdout)
863                sys.stdout.flush()
864                if stderr and not pgo:
865                    print >>sys.stderr, stderr
866                sys.stderr.flush()
867
868                if result[0] == INTERRUPTED:
869                    assert result[1] == 'KeyboardInterrupt'
870                    raise KeyboardInterrupt   # What else?
871
872                test_index += 1
873        except KeyboardInterrupt:
874            interrupted = True
875            pending.close()
876        for worker in workers:
877            worker.join()
878    else:
879        print("Run tests sequentially")
880
881        previous_test = None
882        for test_index, test in enumerate(tests, 1):
883            if not quiet:
884                text = test
885                if previous_test:
886                    text = '%s -- %s' % (text, previous_test)
887                display_progress(test_index, text)
888
889            def local_runtest():
890                result = runtest(test, verbose, quiet, huntrleaks, None, pgo,
891                                 failfast=failfast,
892                                 match_tests=match_tests,
893                                 testdir=testdir)
894                accumulate_result(test, result)
895                return result
896
897            start_time = time.time()
898            if trace:
899                # If we're tracing code coverage, then we don't exit with status
900                # if on a false return value from main.
901                ns = dict(locals())
902                tracer.runctx('result = local_runtest()',
903                              globals=globals(), locals=ns)
904                result = ns['result']
905            else:
906                try:
907                    result = local_runtest()
908                    if verbose3 and result[0] == FAILED:
909                        if not pgo:
910                            print "Re-running test %r in verbose mode" % test
911                        runtest(test, True, quiet, huntrleaks, None, pgo,
912                                testdir=testdir)
913                except KeyboardInterrupt:
914                    interrupted = True
915                    break
916                except:
917                    raise
918
919            test_time = time.time() - start_time
920            previous_test = format_test_result(test, result[0])
921            if test_time >= PROGRESS_MIN_TIME:
922                previous_test = "%s in %s" % (previous_test,
923                                              format_duration(test_time))
924            elif result[0] == PASSED:
925                # be quiet: say nothing if the test passed shortly
926                previous_test = None
927
928            if findleaks:
929                gc.collect()
930                if gc.garbage:
931                    print "Warning: test created", len(gc.garbage),
932                    print "uncollectable object(s)."
933                    # move the uncollectable objects somewhere so we don't see
934                    # them again
935                    found_garbage.extend(gc.garbage)
936                    del gc.garbage[:]
937
938            unload_test_modules(save_modules)
939
940
941    def get_tests_result():
942        result = []
943        if bad:
944            result.append("FAILURE")
945        elif fail_env_changed and environment_changed:
946            result.append("ENV CHANGED")
947        elif not any((good, bad, skipped, interrupted, environment_changed)):
948            result.append("NO TEST RUN")
949
950        if interrupted:
951            result.append("INTERRUPTED")
952
953        if not result:
954            result.append("SUCCESS")
955
956        result = ', '.join(result)
957        if first_result:
958            result = '%s then %s' % (first_result, result)
959        return result
960
961
962    def display_result():
963        # print a newline after ^C
964        print
965        print("== Tests result: %s ==" % get_tests_result())
966
967        if interrupted and not pgo:
968            print
969            print "Test suite interrupted by signal SIGINT."
970            omitted = set(selected) - set(good) - set(bad) - set(skipped)
971            print count(len(omitted), "test"), "omitted:"
972            printlist(omitted)
973
974        if good and not quiet and not pgo:
975            print
976            if not bad and not skipped and not interrupted and len(good) > 1:
977                print "All",
978            print count(len(good), "test"), "OK."
979
980        if print_slow:
981            test_times.sort(reverse=True)
982            print
983            print "10 slowest tests:"
984            for test_time, test in test_times[:10]:
985                print("- %s: %.1fs" % (test, test_time))
986
987        if bad and not pgo:
988            print
989            print count(len(bad), "test"), "failed:"
990            printlist(bad)
991
992        if environment_changed and not pgo:
993            print
994            print "{} altered the execution environment:".format(
995                count(len(environment_changed), "test"))
996            printlist(environment_changed)
997
998        if skipped and not quiet and not pgo:
999            print
1000            print count(len(skipped), "test"), "skipped:"
1001            printlist(skipped)
1002
1003            e = _ExpectedSkips()
1004            plat = sys.platform
1005            if e.isvalid():
1006                surprise = set(skipped) - e.getexpected() - set(resource_denieds)
1007                if surprise:
1008                    print count(len(surprise), "skip"), \
1009                          "unexpected on", plat + ":"
1010                    printlist(surprise)
1011                else:
1012                    print "Those skips are all expected on", plat + "."
1013            else:
1014                print "Ask someone to teach regrtest.py about which tests are"
1015                print "expected to get skipped on", plat + "."
1016
1017        if rerun:
1018            print("")
1019            print("%s:" % count(len(rerun), "re-run test"))
1020            printlist(rerun)
1021
1022        if run_no_tests:
1023            print("")
1024            print("%s run no tests:" % count(len(run_no_tests), "test"))
1025            printlist(run_no_tests)
1026
1027
1028    display_result()
1029
1030    if verbose2 and bad:
1031        first_result = get_tests_result()
1032
1033        print
1034        print "Re-running failed tests in verbose mode"
1035        rerun = bad[:]
1036        for test in rerun:
1037            print "Re-running test %r in verbose mode" % test
1038            sys.stdout.flush()
1039            try:
1040                support.verbose = True
1041                ok = runtest(test, True, quiet, huntrleaks, None, pgo,
1042                             match_tests=match_tests, testdir=testdir)
1043            except KeyboardInterrupt:
1044                # print a newline separate from the ^C
1045                print
1046                break
1047            else:
1048                if ok[0] in {PASSED, ENV_CHANGED, SKIPPED, RESOURCE_DENIED}:
1049                    bad.remove(test)
1050        else:
1051            if bad:
1052                print count(len(bad), "test"), "failed again:"
1053                printlist(bad)
1054
1055        display_result()
1056
1057    if single:
1058        if next_single_test:
1059            with open(filename, 'w') as fp:
1060                fp.write(next_single_test + '\n')
1061        else:
1062            os.unlink(filename)
1063
1064    if trace:
1065        r = tracer.results()
1066        r.write_results(show_missing=True, summary=True, coverdir=coverdir)
1067
1068    if runleaks:
1069        os.system("leaks %d" % os.getpid())
1070
1071    print
1072    duration = time.time() - regrtest_start_time
1073    print("Total duration: %s" % format_duration(duration))
1074
1075    print("Tests result: %s" % get_tests_result())
1076
1077    if bad:
1078        sys.exit(2)
1079    if interrupted:
1080        sys.exit(130)
1081    if fail_env_changed and environment_changed:
1082        sys.exit(3)
1083    sys.exit(0)
1084
1085
1086STDTESTS = [
1087    'test_grammar',
1088    'test_opcodes',
1089    'test_dict',
1090    'test_builtin',
1091    'test_exceptions',
1092    'test_types',
1093    'test_unittest',
1094    'test_doctest',
1095    'test_doctest2',
1096]
1097
1098NOTTESTS = {
1099    'test_support',
1100    'test_future1',
1101    'test_future2',
1102}
1103
1104def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
1105    """Return a list of all applicable test modules."""
1106    testdir = findtestdir(testdir)
1107    names = os.listdir(testdir)
1108    tests = []
1109    others = set(stdtests) | nottests
1110    for name in names:
1111        modname, ext = os.path.splitext(name)
1112        if modname[:5] == "test_" and ext == ".py" and modname not in others:
1113            tests.append(modname)
1114    return stdtests + sorted(tests)
1115
1116def runtest(test, verbose, quiet,
1117            huntrleaks=False, use_resources=None, pgo=False,
1118            failfast=False, match_tests=None, testdir=None):
1119    """Run a single test.
1120
1121    test -- the name of the test
1122    verbose -- if true, print more messages
1123    quiet -- if true, don't print 'skipped' messages (probably redundant)
1124    test_times -- a list of (time, test_name) pairs
1125    huntrleaks -- run multiple times to test for leaks; requires a debug
1126                  build; a triple corresponding to -R's three arguments
1127    pgo -- if true, do not print unnecessary info when running the test
1128           for Profile Guided Optimization build
1129
1130    Returns one of the test result constants:
1131        CHILD_ERROR      Child process crashed
1132        INTERRUPTED      KeyboardInterrupt when run under -j
1133        RESOURCE_DENIED  test skipped because resource denied
1134        SKIPPED          test skipped for some other reason
1135        ENV_CHANGED      test failed because it changed the execution environment
1136        FAILED           test failed
1137        PASSED           test passed
1138        EMPTY_TEST_SUITE test ran no subtests.
1139    """
1140
1141    support.verbose = verbose  # Tell tests to be moderately quiet
1142    if use_resources is not None:
1143        support.use_resources = use_resources
1144    try:
1145        support.set_match_tests(match_tests)
1146        # reset the environment_altered flag to detect if a test altered
1147        # the environment
1148        support.environment_altered = False
1149        if failfast:
1150            support.failfast = True
1151
1152        return runtest_inner(test, verbose, quiet, huntrleaks, pgo, testdir)
1153    finally:
1154        cleanup_test_droppings(test, verbose)
1155
1156
1157# Unit tests are supposed to leave the execution environment unchanged
1158# once they complete.  But sometimes tests have bugs, especially when
1159# tests fail, and the changes to environment go on to mess up other
1160# tests.  This can cause issues with buildbot stability, since tests
1161# are run in random order and so problems may appear to come and go.
1162# There are a few things we can save and restore to mitigate this, and
1163# the following context manager handles this task.
1164
1165class saved_test_environment:
1166    """Save bits of the test environment and restore them at block exit.
1167
1168        with saved_test_environment(testname, verbose, quiet):
1169            #stuff
1170
1171    Unless quiet is True, a warning is printed to stderr if any of
1172    the saved items was changed by the test.  The attribute 'changed'
1173    is initially False, but is set to True if a change is detected.
1174
1175    If verbose is more than 1, the before and after state of changed
1176    items is also printed.
1177    """
1178
1179    changed = False
1180
1181    def __init__(self, testname, verbose=0, quiet=False, pgo=False):
1182        self.testname = testname
1183        self.verbose = verbose
1184        self.quiet = quiet
1185        self.pgo = pgo
1186
1187    # To add things to save and restore, add a name XXX to the resources list
1188    # and add corresponding get_XXX/restore_XXX functions.  get_XXX should
1189    # return the value to be saved and compared against a second call to the
1190    # get function when test execution completes.  restore_XXX should accept
1191    # the saved value and restore the resource using it.  It will be called if
1192    # and only if a change in the value is detected.
1193    #
1194    # Note: XXX will have any '.' replaced with '_' characters when determining
1195    # the corresponding method names.
1196
1197    resources = ('sys.argv', 'cwd', 'sys.stdin', 'sys.stdout', 'sys.stderr',
1198                 'os.environ', 'sys.path', 'asyncore.socket_map',
1199                 'files',
1200                )
1201
1202    def get_sys_argv(self):
1203        return id(sys.argv), sys.argv, sys.argv[:]
1204    def restore_sys_argv(self, saved_argv):
1205        sys.argv = saved_argv[1]
1206        sys.argv[:] = saved_argv[2]
1207
1208    def get_cwd(self):
1209        return os.getcwd()
1210    def restore_cwd(self, saved_cwd):
1211        os.chdir(saved_cwd)
1212
1213    def get_sys_stdout(self):
1214        return sys.stdout
1215    def restore_sys_stdout(self, saved_stdout):
1216        sys.stdout = saved_stdout
1217
1218    def get_sys_stderr(self):
1219        return sys.stderr
1220    def restore_sys_stderr(self, saved_stderr):
1221        sys.stderr = saved_stderr
1222
1223    def get_sys_stdin(self):
1224        return sys.stdin
1225    def restore_sys_stdin(self, saved_stdin):
1226        sys.stdin = saved_stdin
1227
1228    def get_os_environ(self):
1229        return id(os.environ), os.environ, dict(os.environ)
1230    def restore_os_environ(self, saved_environ):
1231        os.environ = saved_environ[1]
1232        os.environ.clear()
1233        os.environ.update(saved_environ[2])
1234
1235    def get_sys_path(self):
1236        return id(sys.path), sys.path, sys.path[:]
1237    def restore_sys_path(self, saved_path):
1238        sys.path = saved_path[1]
1239        sys.path[:] = saved_path[2]
1240
1241    def get_asyncore_socket_map(self):
1242        asyncore = sys.modules.get('asyncore')
1243        # XXX Making a copy keeps objects alive until __exit__ gets called.
1244        return asyncore and asyncore.socket_map.copy() or {}
1245    def restore_asyncore_socket_map(self, saved_map):
1246        asyncore = sys.modules.get('asyncore')
1247        if asyncore is not None:
1248            asyncore.close_all(ignore_all=True)
1249            asyncore.socket_map.update(saved_map)
1250
1251    def get_support_TESTFN(self):
1252        if os.path.isfile(support.TESTFN):
1253            result = 'f'
1254        elif os.path.isdir(support.TESTFN):
1255            result = 'd'
1256        else:
1257            result = None
1258        return result
1259    def restore_support_TESTFN(self, saved_value):
1260        if saved_value is None:
1261            if os.path.isfile(support.TESTFN):
1262                os.unlink(support.TESTFN)
1263            elif os.path.isdir(support.TESTFN):
1264                shutil.rmtree(support.TESTFN)
1265
1266    def get_files(self):
1267        return sorted(fn + ('/' if os.path.isdir(fn) else '')
1268                      for fn in os.listdir(os.curdir))
1269    def restore_files(self, saved_value):
1270        fn = support.TESTFN
1271        if fn not in saved_value and (fn + '/') not in saved_value:
1272            if os.path.isfile(fn):
1273                support.unlink(fn)
1274            elif os.path.isdir(fn):
1275                support.rmtree(fn)
1276
1277    def resource_info(self):
1278        for name in self.resources:
1279            method_suffix = name.replace('.', '_')
1280            get_name = 'get_' + method_suffix
1281            restore_name = 'restore_' + method_suffix
1282            yield name, getattr(self, get_name), getattr(self, restore_name)
1283
1284    def __enter__(self):
1285        self.saved_values = dict((name, get()) for name, get, restore
1286                                                   in self.resource_info())
1287        return self
1288
1289    def __exit__(self, exc_type, exc_val, exc_tb):
1290        saved_values = self.saved_values
1291        del self.saved_values
1292
1293        # Read support.environment_altered, set by support helper functions
1294        self.changed |= support.environment_altered
1295
1296        for name, get, restore in self.resource_info():
1297            current = get()
1298            original = saved_values.pop(name)
1299            # Check for changes to the resource's value
1300            if current != original:
1301                self.changed = True
1302                restore(original)
1303                if not self.quiet and not self.pgo:
1304                    print >>sys.stderr, (
1305                          "Warning -- {} was modified by {}".format(
1306                                                 name, self.testname))
1307                    print >>sys.stderr, (
1308                          "  Before: {}\n  After:  {} ".format(
1309                                              original, current))
1310            # XXX (ncoghlan): for most resources (e.g. sys.path) identity
1311            # matters at least as much as value. For others (e.g. cwd),
1312            # identity is irrelevant. Should we add a mechanism to check
1313            # for substitution in the cases where it matters?
1314        return False
1315
1316
1317def post_test_cleanup():
1318    support.reap_children()
1319
1320def runtest_inner(test, verbose, quiet, huntrleaks=False, pgo=False, testdir=None):
1321    support.unload(test)
1322    if verbose:
1323        capture_stdout = None
1324    else:
1325        capture_stdout = StringIO.StringIO()
1326
1327    test_time = 0.0
1328    refleak = False  # True if the test leaked references.
1329    try:
1330        save_stdout = sys.stdout
1331        try:
1332            if capture_stdout:
1333                sys.stdout = capture_stdout
1334            abstest = get_abs_module(testdir, test)
1335            clear_caches()
1336            with saved_test_environment(test, verbose, quiet, pgo) as environment:
1337                start_time = time.time()
1338                the_package = __import__(abstest, globals(), locals(), [])
1339                if abstest.startswith('test.'):
1340                    the_module = getattr(the_package, test)
1341                else:
1342                    the_module = the_package
1343                # Old tests run to completion simply as a side-effect of
1344                # being imported.  For tests based on unittest or doctest,
1345                # explicitly invoke their test_main() function (if it exists).
1346                indirect_test = getattr(the_module, "test_main", None)
1347                if huntrleaks:
1348                    refleak = dash_R(the_module, test, indirect_test,
1349                        huntrleaks, quiet)
1350                else:
1351                    if indirect_test is not None:
1352                        indirect_test()
1353                test_time = time.time() - start_time
1354            post_test_cleanup()
1355        finally:
1356            sys.stdout = save_stdout
1357    except support.ResourceDenied, msg:
1358        if not quiet and not pgo:
1359            print test, "skipped --", msg
1360            sys.stdout.flush()
1361        return RESOURCE_DENIED, test_time
1362    except unittest.SkipTest, msg:
1363        if not quiet and not pgo:
1364            print test, "skipped --", msg
1365            sys.stdout.flush()
1366        return SKIPPED, test_time
1367    except KeyboardInterrupt:
1368        raise
1369    except support.TestFailed, msg:
1370        if not pgo:
1371            print >>sys.stderr, "test", test, "failed --", msg
1372        sys.stderr.flush()
1373        return FAILED, test_time
1374    except support.TestDidNotRun:
1375        return TEST_DID_NOT_RUN, test_time
1376    except:
1377        type, value = sys.exc_info()[:2]
1378        if not pgo:
1379            print >>sys.stderr, "test", test, "crashed --", str(type) + ":", value
1380        sys.stderr.flush()
1381        if verbose and not pgo:
1382            traceback.print_exc(file=sys.stderr)
1383            sys.stderr.flush()
1384        return FAILED, test_time
1385    else:
1386        if refleak:
1387            return FAILED, test_time
1388        if environment.changed:
1389            return ENV_CHANGED, test_time
1390        # Except in verbose mode, tests should not print anything
1391        if verbose or huntrleaks:
1392            return PASSED, test_time
1393        output = capture_stdout.getvalue()
1394        if not output:
1395            return PASSED, test_time
1396        print "test", test, "produced unexpected output:"
1397        print "*" * 70
1398        print output
1399        print "*" * 70
1400        sys.stdout.flush()
1401        return FAILED, test_time
1402
1403def cleanup_test_droppings(testname, verbose):
1404    import stat
1405    import gc
1406
1407    # First kill any dangling references to open files etc.
1408    gc.collect()
1409
1410    # Try to clean up junk commonly left behind.  While tests shouldn't leave
1411    # any files or directories behind, when a test fails that can be tedious
1412    # for it to arrange.  The consequences can be especially nasty on Windows,
1413    # since if a test leaves a file open, it cannot be deleted by name (while
1414    # there's nothing we can do about that here either, we can display the
1415    # name of the offending test, which is a real help).
1416    for name in (support.TESTFN,
1417                 "db_home",
1418                ):
1419        if not os.path.exists(name):
1420            continue
1421
1422        if os.path.isdir(name):
1423            kind, nuker = "directory", shutil.rmtree
1424        elif os.path.isfile(name):
1425            kind, nuker = "file", os.unlink
1426        else:
1427            raise SystemError("os.path says %r exists but is neither "
1428                              "directory nor file" % name)
1429
1430        if verbose:
1431            print "%r left behind %s %r" % (testname, kind, name)
1432        try:
1433            # if we have chmod, fix possible permissions problems
1434            # that might prevent cleanup
1435            if (hasattr(os, 'chmod')):
1436                os.chmod(name, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
1437            nuker(name)
1438        except Exception, msg:
1439            print >> sys.stderr, ("%r left behind %s %r and it couldn't be "
1440                "removed: %s" % (testname, kind, name, msg))
1441
1442def dash_R(the_module, test, indirect_test, huntrleaks, quiet):
1443    """Run a test multiple times, looking for reference leaks.
1444
1445    Returns:
1446        False if the test didn't leak references; True if we detected refleaks.
1447    """
1448    # This code is hackish and inelegant, but it seems to do the job.
1449    import copy_reg, _abcoll, _pyio
1450
1451    if not hasattr(sys, 'gettotalrefcount'):
1452        raise Exception("Tracking reference leaks requires a debug build "
1453                        "of Python")
1454
1455    # Avoid false positives due to various caches
1456    # filling slowly with random data:
1457    warm_caches()
1458
1459    # Save current values for dash_R_cleanup() to restore.
1460    fs = warnings.filters[:]
1461    ps = copy_reg.dispatch_table.copy()
1462    pic = sys.path_importer_cache.copy()
1463    try:
1464        import zipimport
1465    except ImportError:
1466        zdc = None # Run unmodified on platforms without zipimport support
1467    else:
1468        zdc = zipimport._zip_directory_cache.copy()
1469    abcs = {}
1470    modules = _abcoll, _pyio
1471    for abc in [getattr(mod, a) for mod in modules for a in mod.__all__]:
1472        # XXX isinstance(abc, ABCMeta) leads to infinite recursion
1473        if not hasattr(abc, '_abc_registry'):
1474            continue
1475        for obj in abc.__subclasses__() + [abc]:
1476            abcs[obj] = obj._abc_registry.copy()
1477
1478    # bpo-31217: Integer pool to get a single integer object for the same
1479    # value. The pool is used to prevent false alarm when checking for memory
1480    # block leaks. Fill the pool with values in -1000..1000 which are the most
1481    # common (reference, memory block, file descriptor) differences.
1482    int_pool = {value: value for value in range(-1000, 1000)}
1483    def get_pooled_int(value):
1484        return int_pool.setdefault(value, value)
1485
1486    if indirect_test:
1487        def run_the_test():
1488            indirect_test()
1489    else:
1490        def run_the_test():
1491            imp.reload(the_module)
1492
1493    deltas = []
1494    nwarmup, ntracked, fname = huntrleaks
1495    fname = os.path.join(support.SAVEDCWD, fname)
1496
1497    # Pre-allocate to ensure that the loop doesn't allocate anything new
1498    repcount = nwarmup + ntracked
1499    rc_deltas = [0] * repcount
1500    fd_deltas = [0] * repcount
1501    rep_range = list(range(repcount))
1502
1503    if not quiet:
1504        print >> sys.stderr, "beginning", repcount, "repetitions"
1505        print >> sys.stderr, ("1234567890"*(repcount//10 + 1))[:repcount]
1506
1507    dash_R_cleanup(fs, ps, pic, zdc, abcs)
1508
1509    # initialize variables to make pyflakes quiet
1510    rc_before = fd_before = 0
1511
1512    for i in rep_range:
1513        run_the_test()
1514
1515        if not quiet:
1516            sys.stderr.write('.')
1517
1518        dash_R_cleanup(fs, ps, pic, zdc, abcs)
1519
1520        rc_after = sys.gettotalrefcount()
1521        fd_after = support.fd_count()
1522        rc_deltas[i] = get_pooled_int(rc_after - rc_before)
1523        fd_deltas[i] = get_pooled_int(fd_after - fd_before)
1524        rc_before = rc_after
1525        fd_before = fd_after
1526
1527    if not quiet:
1528        print >> sys.stderr
1529
1530    # These checkers return False on success, True on failure
1531    def check_rc_deltas(deltas):
1532        # Checker for reference counters and memomry blocks.
1533        #
1534        # bpo-30776: Try to ignore false positives:
1535        #
1536        #   [3, 0, 0]
1537        #   [0, 1, 0]
1538        #   [8, -8, 1]
1539        #
1540        # Expected leaks:
1541        #
1542        #   [5, 5, 6]
1543        #   [10, 1, 1]
1544        return all(delta >= 1 for delta in deltas)
1545
1546    def check_fd_deltas(deltas):
1547        return any(deltas)
1548
1549    failed = False
1550    for deltas, item_name, checker in [
1551        (rc_deltas, 'references', check_rc_deltas),
1552        (fd_deltas, 'file descriptors', check_fd_deltas)
1553    ]:
1554        deltas = deltas[nwarmup:]
1555        if checker(deltas):
1556            msg = '%s leaked %s %s, sum=%s' % (test, deltas, item_name, sum(deltas))
1557            print >> sys.stderr, msg
1558            with open(fname, "a") as refrep:
1559                print >> refrep, msg
1560                refrep.flush()
1561            failed = True
1562    return failed
1563
1564def dash_R_cleanup(fs, ps, pic, zdc, abcs):
1565    import gc, copy_reg
1566
1567    # Restore some original values.
1568    warnings.filters[:] = fs
1569    copy_reg.dispatch_table.clear()
1570    copy_reg.dispatch_table.update(ps)
1571    sys.path_importer_cache.clear()
1572    sys.path_importer_cache.update(pic)
1573    try:
1574        import zipimport
1575    except ImportError:
1576        pass # Run unmodified on platforms without zipimport support
1577    else:
1578        zipimport._zip_directory_cache.clear()
1579        zipimport._zip_directory_cache.update(zdc)
1580
1581    # clear type cache
1582    sys._clear_type_cache()
1583
1584    # Clear ABC registries, restoring previously saved ABC registries.
1585    for abc, registry in abcs.items():
1586        abc._abc_registry = registry.copy()
1587        abc._abc_cache.clear()
1588        abc._abc_negative_cache.clear()
1589
1590    clear_caches()
1591
1592def clear_caches():
1593    import gc
1594
1595    # Clear the warnings registry, so they can be displayed again
1596    for mod in sys.modules.values():
1597        if hasattr(mod, '__warningregistry__'):
1598            del mod.__warningregistry__
1599
1600    # Clear assorted module caches.
1601    # Don't worry about resetting the cache if the module is not loaded
1602    try:
1603        distutils_dir_util = sys.modules['distutils.dir_util']
1604    except KeyError:
1605        pass
1606    else:
1607        distutils_dir_util._path_created.clear()
1608
1609    re.purge()
1610
1611    try:
1612        _strptime = sys.modules['_strptime']
1613    except KeyError:
1614        pass
1615    else:
1616        _strptime._regex_cache.clear()
1617
1618    try:
1619        urlparse = sys.modules['urlparse']
1620    except KeyError:
1621        pass
1622    else:
1623        urlparse.clear_cache()
1624
1625    try:
1626        urllib = sys.modules['urllib']
1627    except KeyError:
1628        pass
1629    else:
1630        urllib.urlcleanup()
1631
1632    try:
1633        urllib2 = sys.modules['urllib2']
1634    except KeyError:
1635        pass
1636    else:
1637        urllib2.install_opener(None)
1638
1639    try:
1640        dircache = sys.modules['dircache']
1641    except KeyError:
1642        pass
1643    else:
1644        dircache.reset()
1645
1646    try:
1647        linecache = sys.modules['linecache']
1648    except KeyError:
1649        pass
1650    else:
1651        linecache.clearcache()
1652
1653    try:
1654        mimetypes = sys.modules['mimetypes']
1655    except KeyError:
1656        pass
1657    else:
1658        mimetypes._default_mime_types()
1659
1660    try:
1661        filecmp = sys.modules['filecmp']
1662    except KeyError:
1663        pass
1664    else:
1665        filecmp._cache.clear()
1666
1667    try:
1668        struct = sys.modules['struct']
1669    except KeyError:
1670        pass
1671    else:
1672        struct._clearcache()
1673
1674    try:
1675        doctest = sys.modules['doctest']
1676    except KeyError:
1677        pass
1678    else:
1679        doctest.master = None
1680
1681    try:
1682        ctypes = sys.modules['ctypes']
1683    except KeyError:
1684        pass
1685    else:
1686        ctypes._reset_cache()
1687
1688    # Collect cyclic trash.
1689    support.gc_collect()
1690
1691def warm_caches():
1692    """Create explicitly internal singletons which are created on demand
1693    to prevent false positive when hunting reference leaks."""
1694    # char cache
1695    for i in range(256):
1696        chr(i)
1697    # unicode cache
1698    for i in range(256):
1699        unichr(i)
1700    # int cache
1701    list(range(-5, 257))
1702
1703def findtestdir(path=None):
1704    return path or os.path.dirname(__file__) or os.curdir
1705
1706def removepy(names):
1707    if not names:
1708        return
1709    for idx, name in enumerate(names):
1710        basename, ext = os.path.splitext(name)
1711        if ext == '.py':
1712            names[idx] = basename
1713
1714def count(n, word):
1715    if n == 1:
1716        return "%d %s" % (n, word)
1717    else:
1718        return "%d %ss" % (n, word)
1719
1720def printlist(x, width=70, indent=4, file=None):
1721    """Print the elements of iterable x to stdout.
1722
1723    Optional arg width (default 70) is the maximum line length.
1724    Optional arg indent (default 4) is the number of blanks with which to
1725    begin each line.
1726    """
1727
1728    from textwrap import fill
1729    blanks = ' ' * indent
1730    # Print the sorted list: 'x' may be a '--random' list or a set()
1731    print >>file, fill(' '.join(str(elt) for elt in sorted(x)), width,
1732                       initial_indent=blanks, subsequent_indent=blanks)
1733
1734def get_abs_module(testdir, test):
1735    if test.startswith('test.') or testdir:
1736        return test
1737    else:
1738        # Always import it from the test package
1739        return 'test.' + test
1740
1741def _list_cases(suite):
1742    for test in suite:
1743        if isinstance(test, unittest.TestSuite):
1744            _list_cases(test)
1745        elif isinstance(test, unittest.TestCase):
1746            if support.match_test(test):
1747                print(test.id())
1748
1749def list_cases(testdir, selected, match_tests):
1750    support.verbose = False
1751    support.set_match_tests(match_tests)
1752
1753    save_modules = set(sys.modules)
1754    skipped = []
1755    for test in selected:
1756        abstest = get_abs_module(testdir, test)
1757        try:
1758            suite = unittest.defaultTestLoader.loadTestsFromName(abstest)
1759            _list_cases(suite)
1760        except unittest.SkipTest:
1761            skipped.append(test)
1762
1763        unload_test_modules(save_modules)
1764
1765    if skipped:
1766        print >>sys.stderr
1767        print >>sys.stderr, count(len(skipped), "test"), "skipped:"
1768        printlist(skipped, file=sys.stderr)
1769
1770# Map sys.platform to a string containing the basenames of tests
1771# expected to be skipped on that platform.
1772#
1773# Special cases:
1774#     test_pep277
1775#         The _ExpectedSkips constructor adds this to the set of expected
1776#         skips if not os.path.supports_unicode_filenames.
1777#     test_timeout
1778#         Controlled by test_timeout.skip_expected.  Requires the network
1779#         resource and a socket module.
1780#
1781# Tests that are expected to be skipped everywhere except on one platform
1782# are also handled separately.
1783
1784_expectations = {
1785    'win32':
1786        """
1787        test__locale
1788        test_bsddb185
1789        test_bsddb3
1790        test_commands
1791        test_crypt
1792        test_curses
1793        test_dbm
1794        test_dl
1795        test_fcntl
1796        test_fork1
1797        test_epoll
1798        test_gdbm
1799        test_grp
1800        test_ioctl
1801        test_largefile
1802        test_kqueue
1803        test_mhlib
1804        test_openpty
1805        test_ossaudiodev
1806        test_pipes
1807        test_poll
1808        test_posix
1809        test_pty
1810        test_pwd
1811        test_resource
1812        test_signal
1813        test_spwd
1814        test_threadsignals
1815        test_timing
1816        test_wait3
1817        test_wait4
1818        """,
1819    'linux2':
1820        """
1821        test_bsddb185
1822        test_curses
1823        test_dl
1824        test_largefile
1825        test_kqueue
1826        test_ossaudiodev
1827        """,
1828    'unixware7':
1829        """
1830        test_bsddb
1831        test_bsddb185
1832        test_dl
1833        test_epoll
1834        test_largefile
1835        test_kqueue
1836        test_minidom
1837        test_openpty
1838        test_pyexpat
1839        test_sax
1840        test_sundry
1841        """,
1842    'openunix8':
1843        """
1844        test_bsddb
1845        test_bsddb185
1846        test_dl
1847        test_epoll
1848        test_largefile
1849        test_kqueue
1850        test_minidom
1851        test_openpty
1852        test_pyexpat
1853        test_sax
1854        test_sundry
1855        """,
1856    'sco_sv3':
1857        """
1858        test_asynchat
1859        test_bsddb
1860        test_bsddb185
1861        test_dl
1862        test_fork1
1863        test_epoll
1864        test_gettext
1865        test_largefile
1866        test_locale
1867        test_kqueue
1868        test_minidom
1869        test_openpty
1870        test_pyexpat
1871        test_queue
1872        test_sax
1873        test_sundry
1874        test_thread
1875        test_threaded_import
1876        test_threadedtempfile
1877        test_threading
1878        """,
1879    'riscos':
1880        """
1881        test_asynchat
1882        test_atexit
1883        test_bsddb
1884        test_bsddb185
1885        test_bsddb3
1886        test_commands
1887        test_crypt
1888        test_dbm
1889        test_dl
1890        test_fcntl
1891        test_fork1
1892        test_epoll
1893        test_gdbm
1894        test_grp
1895        test_largefile
1896        test_locale
1897        test_kqueue
1898        test_mmap
1899        test_openpty
1900        test_poll
1901        test_popen2
1902        test_pty
1903        test_pwd
1904        test_strop
1905        test_sundry
1906        test_thread
1907        test_threaded_import
1908        test_threadedtempfile
1909        test_threading
1910        test_timing
1911        """,
1912    'darwin':
1913        """
1914        test__locale
1915        test_bsddb
1916        test_bsddb3
1917        test_curses
1918        test_epoll
1919        test_gdb
1920        test_gdbm
1921        test_largefile
1922        test_locale
1923        test_kqueue
1924        test_minidom
1925        test_ossaudiodev
1926        test_poll
1927        """,
1928    'sunos5':
1929        """
1930        test_bsddb
1931        test_bsddb185
1932        test_curses
1933        test_dbm
1934        test_epoll
1935        test_kqueue
1936        test_gdbm
1937        test_gzip
1938        test_openpty
1939        test_zipfile
1940        test_zlib
1941        """,
1942    'hp-ux11':
1943        """
1944        test_bsddb
1945        test_bsddb185
1946        test_curses
1947        test_dl
1948        test_epoll
1949        test_gdbm
1950        test_gzip
1951        test_largefile
1952        test_locale
1953        test_kqueue
1954        test_minidom
1955        test_openpty
1956        test_pyexpat
1957        test_sax
1958        test_zipfile
1959        test_zlib
1960        """,
1961    'atheos':
1962        """
1963        test_bsddb185
1964        test_curses
1965        test_dl
1966        test_gdbm
1967        test_epoll
1968        test_largefile
1969        test_locale
1970        test_kqueue
1971        test_mhlib
1972        test_mmap
1973        test_poll
1974        test_popen2
1975        test_resource
1976        """,
1977    'cygwin':
1978        """
1979        test_bsddb185
1980        test_bsddb3
1981        test_curses
1982        test_dbm
1983        test_epoll
1984        test_ioctl
1985        test_kqueue
1986        test_largefile
1987        test_locale
1988        test_ossaudiodev
1989        test_socketserver
1990        """,
1991    'os2emx':
1992        """
1993        test_audioop
1994        test_bsddb185
1995        test_bsddb3
1996        test_commands
1997        test_curses
1998        test_dl
1999        test_epoll
2000        test_kqueue
2001        test_largefile
2002        test_mhlib
2003        test_mmap
2004        test_openpty
2005        test_ossaudiodev
2006        test_pty
2007        test_resource
2008        test_signal
2009        """,
2010    'freebsd4':
2011        """
2012        test_bsddb
2013        test_bsddb3
2014        test_epoll
2015        test_gdbm
2016        test_locale
2017        test_ossaudiodev
2018        test_pep277
2019        test_pty
2020        test_socketserver
2021        test_tcl
2022        test_tk
2023        test_ttk_guionly
2024        test_ttk_textonly
2025        test_timeout
2026        test_urllibnet
2027        test_multiprocessing
2028        """,
2029    'aix5':
2030        """
2031        test_bsddb
2032        test_bsddb185
2033        test_bsddb3
2034        test_bz2
2035        test_dl
2036        test_epoll
2037        test_gdbm
2038        test_gzip
2039        test_kqueue
2040        test_ossaudiodev
2041        test_tcl
2042        test_tk
2043        test_ttk_guionly
2044        test_ttk_textonly
2045        test_zipimport
2046        test_zlib
2047        """,
2048    'openbsd3':
2049        """
2050        test_ascii_formatd
2051        test_bsddb
2052        test_bsddb3
2053        test_ctypes
2054        test_dl
2055        test_epoll
2056        test_gdbm
2057        test_locale
2058        test_normalization
2059        test_ossaudiodev
2060        test_pep277
2061        test_tcl
2062        test_tk
2063        test_ttk_guionly
2064        test_ttk_textonly
2065        test_multiprocessing
2066        """,
2067    'netbsd3':
2068        """
2069        test_ascii_formatd
2070        test_bsddb
2071        test_bsddb185
2072        test_bsddb3
2073        test_ctypes
2074        test_curses
2075        test_dl
2076        test_epoll
2077        test_gdbm
2078        test_locale
2079        test_ossaudiodev
2080        test_pep277
2081        test_tcl
2082        test_tk
2083        test_ttk_guionly
2084        test_ttk_textonly
2085        test_multiprocessing
2086        """,
2087}
2088_expectations['freebsd5'] = _expectations['freebsd4']
2089_expectations['freebsd6'] = _expectations['freebsd4']
2090_expectations['freebsd7'] = _expectations['freebsd4']
2091_expectations['freebsd8'] = _expectations['freebsd4']
2092
2093class _ExpectedSkips:
2094    def __init__(self):
2095        import os.path
2096        from test import test_timeout
2097
2098        self.valid = False
2099        if sys.platform in _expectations:
2100            s = _expectations[sys.platform]
2101            self.expected = set(s.split())
2102
2103            # expected to be skipped on every platform, even Linux
2104            self.expected.add('test_linuxaudiodev')
2105
2106            if not os.path.supports_unicode_filenames:
2107                self.expected.add('test_pep277')
2108
2109            if test_timeout.skip_expected:
2110                self.expected.add('test_timeout')
2111
2112            if sys.maxint == 9223372036854775807L:
2113                self.expected.add('test_imageop')
2114
2115            if sys.platform != "darwin":
2116                MAC_ONLY = ["test_macos", "test_macostools", "test_aepack",
2117                            "test_plistlib", "test_scriptpackages",
2118                            "test_applesingle"]
2119                for skip in MAC_ONLY:
2120                    self.expected.add(skip)
2121            elif len(u'\0'.encode('unicode-internal')) == 4:
2122                self.expected.add("test_macostools")
2123
2124
2125            if sys.platform != "win32":
2126                # test_sqlite is only reliable on Windows where the library
2127                # is distributed with Python
2128                WIN_ONLY = ["test_unicode_file", "test_winreg",
2129                            "test_winsound", "test_startfile",
2130                            "test_sqlite", "test_msilib"]
2131                for skip in WIN_ONLY:
2132                    self.expected.add(skip)
2133
2134            if sys.platform != 'irix':
2135                IRIX_ONLY = ["test_imageop", "test_al", "test_cd", "test_cl",
2136                             "test_gl", "test_imgfile"]
2137                for skip in IRIX_ONLY:
2138                    self.expected.add(skip)
2139
2140            if sys.platform != 'sunos5':
2141                self.expected.add('test_sunaudiodev')
2142                self.expected.add('test_nis')
2143
2144            if not sys.py3kwarning:
2145                self.expected.add('test_py3kwarn')
2146
2147            self.valid = True
2148
2149    def isvalid(self):
2150        "Return true iff _ExpectedSkips knows about the current platform."
2151        return self.valid
2152
2153    def getexpected(self):
2154        """Return set of test names we expect to skip on current platform.
2155
2156        self.isvalid() must be true.
2157        """
2158
2159        assert self.isvalid()
2160        return self.expected
2161
2162def main_in_temp_cwd():
2163    """Run main() in a temporary working directory."""
2164    global TEMPDIR
2165
2166    # When tests are run from the Python build directory, it is best practice
2167    # to keep the test files in a subfolder.  It eases the cleanup of leftover
2168    # files using command "make distclean".
2169    if sysconfig.is_python_build():
2170        TEMPDIR = os.path.join(sysconfig.get_config_var('srcdir'), 'build')
2171        TEMPDIR = os.path.abspath(TEMPDIR)
2172        if not os.path.exists(TEMPDIR):
2173            os.mkdir(TEMPDIR)
2174
2175    # Define a writable temp dir that will be used as cwd while running
2176    # the tests. The name of the dir includes the pid to allow parallel
2177    # testing (see the -j option).
2178    TESTCWD = 'test_python_{}'.format(os.getpid())
2179
2180    TESTCWD = os.path.join(TEMPDIR, TESTCWD)
2181
2182    # Run the tests in a context manager that temporary changes the CWD to a
2183    # temporary and writable directory. If it's not possible to create or
2184    # change the CWD, the original CWD will be used. The original CWD is
2185    # available from support.SAVEDCWD.
2186    with support.temp_cwd(TESTCWD, quiet=True):
2187        main()
2188
2189if __name__ == '__main__':
2190    # findtestdir() gets the dirname out of __file__, so we have to make it
2191    # absolute before changing the working directory.
2192    # For example __file__ may be relative when running trace or profile.
2193    # See issue #9323.
2194    global __file__
2195    __file__ = os.path.abspath(__file__)
2196
2197    # sanity check
2198    assert __file__ == os.path.abspath(sys.argv[0])
2199
2200    main_in_temp_cwd()
2201