regrtest.py revision 282396f27addf4ae6586865ccf449f60753a106c
1152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum#! /usr/bin/env python
2152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
35f2e0e5ccb988cdf65137034b33ee57198cc23b9Martin v. Löwis"""Regression test.
4152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
5152494aea24669a3d74460fa460a4ed45696bc75Guido van RossumThis will find all modules whose name is "test_*" in the test
6152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossumdirectory, and run them.  Various command line options provide
7152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossumadditional facilities.
8152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
9152494aea24669a3d74460fa460a4ed45696bc75Guido van RossumCommand line options:
10152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
1161147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson-v: verbose    -- run tests in verbose mode with output to stdout
1204824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis-w: verbose2   -- re-run failed tests in verbose mode
1361147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson-q: quiet      -- don't print anything except if a test fails
1461147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson-x: exclude    -- arguments are tests to *exclude*
1561147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson-s: single     -- run only a single test (see below)
1636dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin-S: slow       -- print the slowest 10 tests
1761147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson-r: random     -- randomize test execution order
1861147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson-f: fromfile   -- read names of tests to run from a file (see below)
1961147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson-l: findleaks  -- if GC is available detect tests that leak memory
2061147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson-u: use        -- specify which special resource intensive tests to run
2161147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson-h: help       -- print this text and exit
2261147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson-t: threshold  -- call gc.set_threshold(N)
2361147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson-T: coverage   -- turn on code coverage using the trace module
24aee4da6b83e49de68fed0c6b999aa109eab98f0dWalter Dörwald-D: coverdir   -- Directory where coverage files are put
25aee4da6b83e49de68fed0c6b999aa109eab98f0dWalter Dörwald-N: nocoverdir -- Put coverage files alongside modules
2661147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson-L: runleaks   -- run the leaks(1) command just before exit
2761147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson-R: huntrleaks -- search for reference leaks (needs debug build, v. slow)
28abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouters-M: memlimit   -- run very large memory-consuming tests
294698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou-j: multiprocess -- run several processes at once
30152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
31152494aea24669a3d74460fa460a4ed45696bc75Guido van RossumIf non-option arguments are present, they are names for tests to run,
32152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossumunless -x is given, in which case they are names for tests not to run.
33152494aea24669a3d74460fa460a4ed45696bc75Guido van RossumIf no test names are given, all tests are run.
34f58ed2596706b97c16174a839c1ed6f6b1f87fa6Guido van Rossum
35a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter-r randomizes test execution order. You can use --randseed=int to provide a
36a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winterint seed value for the randomizer; this is useful for reproducing troublesome
37a199f0198600996c6a9de3a3d914f0bc2101d244Collin Wintertest orders.
38a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter
393b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw-T turns on code coverage tracing with the trace module.
403b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw
41aee4da6b83e49de68fed0c6b999aa109eab98f0dWalter Dörwald-D specifies the directory where coverage files are put.
42aee4da6b83e49de68fed0c6b999aa109eab98f0dWalter Dörwald
43aee4da6b83e49de68fed0c6b999aa109eab98f0dWalter Dörwald-N Put coverage files alongside modules.
44aee4da6b83e49de68fed0c6b999aa109eab98f0dWalter Dörwald
459e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum-s means to run only a single test and exit.  This is useful when
469e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossumdoing memory analysis on the Python interpreter (which tend to consume
479e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossumtoo many resources to run the full regression test non-stop).  The
489e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossumfile /tmp/pynexttest is read to find the next test to run.  If this
499e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossumfile is missing, the first test_*.py file in testdir or on the command
509e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossumline is used.  (actually tempfile.gettempdir() is used instead of
519e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum/tmp).
52e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw
539e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum-f reads the names of tests from the file given as f's argument, one
549e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossumor more test names per line.  Whitespace is ignored.  Blank lines and
559e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossumlines beginning with '#' are ignored.  This is especially useful for
569e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossumwhittling down failures involving interactions among tests.
57c5000dfc4098f8547461e790a91536a923124261Tim Peters
580179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaro-L causes the leaks(1) command to be run just before exit if it exists.
590179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaroleaks(1) is available on Mac OS X and presumably on some other
600179a18034b2316a70655d0f05bfbb20a0881f17Skip MontanaroFreeBSD-derived systems.
610179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaro
6261147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson-R runs each test several times and examines sys.gettotalrefcount() to
6361147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudsonsee if the test appears to be leaking references.  The argument should
6461147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudsonbe of the form stab:run:fname where 'stab' is the number of times the
6561147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudsontest is run to let gettotalrefcount settle down, 'run' is the number
6661147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudsonof times further it is run and 'fname' is the name of the file the
6761147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudsonreports are written to.  These parameters all have defaults (5, 4 and
6861147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson"reflog.txt" respectively), so the minimal invocation is '-R ::'.
6961147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson
70abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouters-M runs tests that require an exorbitant amount of memory. These tests
71abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouterstypically try to ascertain containers keep working when containing more than
7297ff04789de3e37af585648de70260a54a29bd47Armin Rigo2 billion objects, which only works on 64-bit systems. There are also some
7397ff04789de3e37af585648de70260a54a29bd47Armin Rigotests that try to exhaust the address space of the process, which only makes
7497ff04789de3e37af585648de70260a54a29bd47Armin Rigosense on 32-bit systems with at least 2Gb of memory. The passed-in memlimit,
75abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouterswhich is a string in the form of '2.5Gb', determines howmuch memory the
76abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouterstests will limit themselves to (but they may go slightly over.) The number
77abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Woutersshouldn't be more memory than the machine has (including swap memory). You
78abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Woutersshould also keep in mind that swap memory is generally much, much slower
79abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Woutersthan RAM, and setting memlimit to all available RAM or higher will heavily
80abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouterstax the machine. On the other hand, it is no use running these tests with a
81abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouterslimit of less than 2.5Gb, and many require more than 20Gb. Tests that expect
82abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Woutersto use more than memlimit memory will be skipped. The big-memory tests
83abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Woutersgenerally run very, very long.
84abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouters
859e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum-u is used to specify which special resource intensive tests to run,
869e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossumsuch as those requiring large file support or network connectivity.
879e9d4f8ed8d467d0558251f43c5decc754712d53Guido van RossumThe argument is a comma-separated list of words indicating the
889e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossumresources to test.  Currently only the following are defined:
8908fca5212528e073600c27c70a34eeef445f393bBarry Warsaw
903a15dace3606d6ea9d59486c5d080a1cb4192ff4Fred Drake    all -       Enable all special resources.
913a15dace3606d6ea9d59486c5d080a1cb4192ff4Fred Drake
92315aa361fc60d3328aad3a5dcfd42f08213c25fbGuido van Rossum    audio -     Tests that use the audio device.  (There are known
93315aa361fc60d3328aad3a5dcfd42f08213c25fbGuido van Rossum                cases of broken audio drivers that can crash Python or
94315aa361fc60d3328aad3a5dcfd42f08213c25fbGuido van Rossum                even the Linux kernel.)
95315aa361fc60d3328aad3a5dcfd42f08213c25fbGuido van Rossum
962158df0b4d8358b5efdcac3024e8cc6d6c92d981Andrew M. Kuchling    curses -    Tests that use curses and will modify the terminal's
972158df0b4d8358b5efdcac3024e8cc6d6c92d981Andrew M. Kuchling                state and output modes.
981633a2e3452b40d0e9bb1f15ab16cd6b90f15a19Tim Peters
9987988b675323ebca3b15c52bad0dbc6b5422c2b0Benjamin Peterson    lib2to3 -   Run the tests for 2to3 (They take a while.)
10087988b675323ebca3b15c52bad0dbc6b5422c2b0Benjamin Peterson
1019e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum    largefile - It is okay to run some test that may create huge
1029e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum                files.  These tests can take a long time and may
1039e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum                consume >2GB of disk space temporarily.
10408fca5212528e073600c27c70a34eeef445f393bBarry Warsaw
1059e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum    network -   It is okay to run tests that use external network
1069e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum                resource, e.g. testing SSL support for sockets.
1071c6b1a2b4ea38955a3f0514f4709bafd0be96c5eMartin v. Löwis
1081c6b1a2b4ea38955a3f0514f4709bafd0be96c5eMartin v. Löwis    bsddb -     It is okay to run the bsddb testsuite, which takes
1091c6b1a2b4ea38955a3f0514f4709bafd0be96c5eMartin v. Löwis                a long time to complete.
1104dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake
1117c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger    decimal -   Test the decimal module against a large suite that
1127c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger                verifies compliance with standards.
1137c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger
1144336eda88640695b23790e4f52de9ee92c7fee73Jeremy Hylton    compiler -  Test the compiler package by compiling all the source
1154336eda88640695b23790e4f52de9ee92c7fee73Jeremy Hylton                in the standard library and test suite.  This takes
11668c04534182f2c09783b6506701a8bc25c98b4a9Raymond Hettinger                a long time.  Enabling this resource also allows
11768c04534182f2c09783b6506701a8bc25c98b4a9Raymond Hettinger                test_tokenize to verify round-trip lexing on every
11868c04534182f2c09783b6506701a8bc25c98b4a9Raymond Hettinger                file in the test library.
1194336eda88640695b23790e4f52de9ee92c7fee73Jeremy Hylton
120eba28bea9b8ef7df010e65c630b8c0f7009c6005Tim Peters    subprocess  Run all tests for the subprocess module.
121f7f1bb7ff5039626c856efafb68a4d338ab96642Peter Astrand
122aaa2f1dea706daf2a5f431d97a3e3120dba652d2Hye-Shik Chang    urlfetch -  It is okay to download files required on testing.
123aaa2f1dea706daf2a5f431d97a3e3120dba652d2Hye-Shik Chang
124b1a98de25ecd3e11fc3093de31fe844233dcd389Guilherme Polo    gui -       Run tests that require a running GUI.
125b1a98de25ecd3e11fc3093de31fe844233dcd389Guilherme Polo
126f8089c7789d61979fd195e67186a77039534cc87Collin Winter    xpickle -   Test pickle and cPickle against Python 2.4, 2.5 and 2.6 to
127f8089c7789d61979fd195e67186a77039534cc87Collin Winter                test backwards compatibility. These tests take a long time
128f8089c7789d61979fd195e67186a77039534cc87Collin Winter                to run.
129f8089c7789d61979fd195e67186a77039534cc87Collin Winter
1304dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred DrakeTo enable all resources except one, use '-uall,-<resource>'.  For
1314dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drakeexample, to run all the tests except for the bsddb tests, give the
1324dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drakeoption '-uall,-bsddb'.
133152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum"""
134152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
13536dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskinimport cStringIO
136152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossumimport getopt
1374698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrouimport json
13836dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskinimport os
139ab1c7918f683e540ae5651c1b585ecda16ae352dSkip Montanaroimport random
14094a9c09e109706af64ae8796882baab2af25be2fNeal Norwitzimport re
14136dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskinimport sys
14236dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskinimport time
1433b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsawimport traceback
14436dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskinimport warnings
145888a39b54c4f47ee25d53b157e2c50402627cd0bBenjamin Petersonimport unittest
146dc15c27f505930a69c73f8c2baf1f9caff9252efGuido van Rossum
147dc15c27f505930a69c73f8c2baf1f9caff9252efGuido van Rossum# I see no other way to suppress these warnings;
148dc15c27f505930a69c73f8c2baf1f9caff9252efGuido van Rossum# putting them in test_grammar.py has no effect:
14988b1defb6f770c4f74788e8296b88fc31c3936ceGuido van Rossumwarnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
150dc15c27f505930a69c73f8c2baf1f9caff9252efGuido van Rossum                        ".*test.test_grammar$")
151c34c4fc3ab497ec1dd4f8c921798927d1b6d13e2Guido van Rossumif sys.maxint > 0x7fffffff:
152c34c4fc3ab497ec1dd4f8c921798927d1b6d13e2Guido van Rossum    # Also suppress them in <string>, because for 64-bit platforms,
153c34c4fc3ab497ec1dd4f8c921798927d1b6d13e2Guido van Rossum    # that's where test_grammar.py hides them.
154c34c4fc3ab497ec1dd4f8c921798927d1b6d13e2Guido van Rossum    warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
155c34c4fc3ab497ec1dd4f8c921798927d1b6d13e2Guido van Rossum                            "<string>")
156152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
1579df4e6f6735af274813cf1b611ee1a342955ad63Thomas Wouters# Ignore ImportWarnings that only occur in the source tree,
1589df4e6f6735af274813cf1b611ee1a342955ad63Thomas Wouters# (because of modules with the same name as source-directories in Modules/)
159076ba2129bb08f56c3af9397748ae1df41daba06Thomas Woutersfor mod in ("ctypes", "gzip", "zipfile", "tarfile", "encodings.zlib_codec",
160076ba2129bb08f56c3af9397748ae1df41daba06Thomas Wouters            "test.test_zipimport", "test.test_zlib", "test.test_zipfile",
161076ba2129bb08f56c3af9397748ae1df41daba06Thomas Wouters            "test.test_codecs", "test.string_tests"):
1629df4e6f6735af274813cf1b611ee1a342955ad63Thomas Wouters    warnings.filterwarnings(module=".*%s$" % (mod,),
1639df4e6f6735af274813cf1b611ee1a342955ad63Thomas Wouters                            action="ignore", category=ImportWarning)
1649df4e6f6735af274813cf1b611ee1a342955ad63Thomas Wouters
165bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum# MacOSX (a.k.a. Darwin) has a default stack size that is too small
166bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum# for deeply recursive regular expressions.  We see this as crashes in
167bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum# the Python test suite when running test_re.py and test_sre.py.  The
168bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum# fix is to set the stack limit to 2048.
169bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum# This approach may also be useful for other Unixy platforms that
170bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum# suffer from small default stack limits.
171bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossumif sys.platform == 'darwin':
172bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum    try:
173bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum        import resource
174bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum    except ImportError:
175bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum        pass
176bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum    else:
177bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum        soft, hard = resource.getrlimit(resource.RLIMIT_STACK)
178bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum        newsoft = min(hard, max(soft, 1024*2048))
179bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum        resource.setrlimit(resource.RLIMIT_STACK, (newsoft, hard))
180bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum
18104f357cffef6d764f2f0ff2671dabde75ec250d1Barry Warsawfrom test import test_support
1823a15dace3606d6ea9d59486c5d080a1cb4192ff4Fred Drake
183e2886fd3ca357c4cf7c1584375d9b9bb55d476b7Benjamin PetersonRESOURCE_NAMES = ('audio', 'curses', 'largefile', 'network', 'bsddb',
184f8089c7789d61979fd195e67186a77039534cc87Collin Winter                  'decimal', 'compiler', 'subprocess', 'urlfetch', 'gui',
185f8089c7789d61979fd195e67186a77039534cc87Collin Winter                  'xpickle')
1863a15dace3606d6ea9d59486c5d080a1cb4192ff4Fred Drake
1873a15dace3606d6ea9d59486c5d080a1cb4192ff4Fred Drake
18808fca5212528e073600c27c70a34eeef445f393bBarry Warsawdef usage(code, msg=''):
18908fca5212528e073600c27c70a34eeef445f393bBarry Warsaw    print __doc__
19008fca5212528e073600c27c70a34eeef445f393bBarry Warsaw    if msg: print msg
19108fca5212528e073600c27c70a34eeef445f393bBarry Warsaw    sys.exit(code)
19208fca5212528e073600c27c70a34eeef445f393bBarry Warsaw
19308fca5212528e073600c27c70a34eeef445f393bBarry Warsaw
194ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arcdef main(tests=None, testdir=None, verbose=0, quiet=False,
1953b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw         exclude=False, single=False, randomize=False, fromfile=None,
196aee4da6b83e49de68fed0c6b999aa109eab98f0dWalter Dörwald         findleaks=False, use_resources=None, trace=False, coverdir='coverage',
197a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter         runleaks=False, huntrleaks=False, verbose2=False, print_slow=False,
1984698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou         random_seed=None, use_mp=None):
1996fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    """Execute a test suite.
2006fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum
2017e47402264cf87b9bbb61fc9ff610af08add7c7bThomas Wouters    This also parses command-line options and modifies its behavior
202004d5e6880940ddbb38460986ac62ee0f1bae97dFred Drake    accordingly.
2036fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum
2046fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    tests -- a list of strings containing test names (optional)
2056fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    testdir -- the directory in which to look for tests (optional)
2066fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum
2076fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    Users other than the Python test suite will certainly want to
2086fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    specify testdir; if it's omitted, the directory containing the
209004d5e6880940ddbb38460986ac62ee0f1bae97dFred Drake    Python test suite is searched for.
2106fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum
2116fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    If the tests argument is omitted, the tests listed on the
2126fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    command-line will be used.  If that's empty, too, then all *.py
2136fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    files beginning with test_ will be used.
214ab1c7918f683e540ae5651c1b585ecda16ae352dSkip Montanaro
215ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc    The other default arguments (verbose, quiet, exclude,
216a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter    single, randomize, findleaks, use_resources, trace, coverdir, print_slow and
217a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter    random_seed) allow programmers calling main() directly to set the
21836dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin    values that would normally be set by flags on the command line.
2196fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    """
220004d5e6880940ddbb38460986ac62ee0f1bae97dFred Drake
2218dee809410e2d433bb0be5d8a1699736b90db0b5Tim Peters    test_support.record_original_stdout(sys.stdout)
222152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    try:
2234698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        opts, args = getopt.getopt(sys.argv[1:], 'hvgqxsSrf:lu:t:TD:NLR:wM:j:',
224a5573b31532f59a63d9ff24e415592f60f974da4Brett Cannon                                   ['help', 'verbose', 'quiet', 'exclude',
22536dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin                                    'single', 'slow', 'random', 'fromfile',
2263b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw                                    'findleaks', 'use=', 'threshold=', 'trace',
227aee4da6b83e49de68fed0c6b999aa109eab98f0dWalter Dörwald                                    'coverdir=', 'nocoverdir', 'runleaks',
228abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouters                                    'huntrleaks=', 'verbose2', 'memlimit=',
2294698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                                    'randseed=', 'multiprocess=', 'slaveargs=',
2303b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw                                    ])
231152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    except getopt.error, msg:
23208fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        usage(2, msg)
23308fca5212528e073600c27c70a34eeef445f393bBarry Warsaw
23408fca5212528e073600c27c70a34eeef445f393bBarry Warsaw    # Defaults
235a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter    if random_seed is None:
236a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter        random_seed = random.randrange(10000000)
23708fca5212528e073600c27c70a34eeef445f393bBarry Warsaw    if use_resources is None:
23808fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        use_resources = []
239152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    for o, a in opts:
24008fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        if o in ('-h', '--help'):
24108fca5212528e073600c27c70a34eeef445f393bBarry Warsaw            usage(0)
24208fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        elif o in ('-v', '--verbose'):
24308fca5212528e073600c27c70a34eeef445f393bBarry Warsaw            verbose += 1
24404824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis        elif o in ('-w', '--verbose2'):
24504824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis            verbose2 = True
24608fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        elif o in ('-q', '--quiet'):
2473b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw            quiet = True;
24808fca5212528e073600c27c70a34eeef445f393bBarry Warsaw            verbose = 0
24908fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        elif o in ('-x', '--exclude'):
2503b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw            exclude = True
25108fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        elif o in ('-s', '--single'):
2523b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw            single = True
25336dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin        elif o in ('-S', '--slow'):
25436dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin            print_slow = True
25508fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        elif o in ('-r', '--randomize'):
2563b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw            randomize = True
257a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter        elif o == '--randseed':
258a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter            random_seed = int(a)
259c5000dfc4098f8547461e790a91536a923124261Tim Peters        elif o in ('-f', '--fromfile'):
260c5000dfc4098f8547461e790a91536a923124261Tim Peters            fromfile = a
26108fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        elif o in ('-l', '--findleaks'):
2623b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw            findleaks = True
2630179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaro        elif o in ('-L', '--runleaks'):
2640179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaro            runleaks = True
2659e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum        elif o in ('-t', '--threshold'):
2669e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum            import gc
2679e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum            gc.set_threshold(int(a))
2683b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw        elif o in ('-T', '--coverage'):
2693b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw            trace = True
270aee4da6b83e49de68fed0c6b999aa109eab98f0dWalter Dörwald        elif o in ('-D', '--coverdir'):
271aee4da6b83e49de68fed0c6b999aa109eab98f0dWalter Dörwald            coverdir = os.path.join(os.getcwd(), a)
272aee4da6b83e49de68fed0c6b999aa109eab98f0dWalter Dörwald        elif o in ('-N', '--nocoverdir'):
273aee4da6b83e49de68fed0c6b999aa109eab98f0dWalter Dörwald            coverdir = None
27461147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson        elif o in ('-R', '--huntrleaks'):
27561147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson            huntrleaks = a.split(':')
27661147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson            if len(huntrleaks) != 3:
27761147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson                print a, huntrleaks
27861147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson                usage(2, '-R takes three colon-separated arguments')
27961147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson            if len(huntrleaks[0]) == 0:
28061147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson                huntrleaks[0] = 5
28161147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson            else:
28261147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson                huntrleaks[0] = int(huntrleaks[0])
28361147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson            if len(huntrleaks[1]) == 0:
28461147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson                huntrleaks[1] = 4
28561147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson            else:
28661147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson                huntrleaks[1] = int(huntrleaks[1])
28761147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson            if len(huntrleaks[2]) == 0:
28861147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson                huntrleaks[2] = "reflog.txt"
289abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouters        elif o in ('-M', '--memlimit'):
290abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouters            test_support.set_memlimit(a)
29108fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        elif o in ('-u', '--use'):
292fe3f6969f54cfd3df24f54572a809e0deb47064fGuido van Rossum            u = [x.lower() for x in a.split(',')]
293fe3f6969f54cfd3df24f54572a809e0deb47064fGuido van Rossum            for r in u:
2943a15dace3606d6ea9d59486c5d080a1cb4192ff4Fred Drake                if r == 'all':
2954dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                    use_resources[:] = RESOURCE_NAMES
2964dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                    continue
2974dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                remove = False
2984dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                if r[0] == '-':
2994dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                    remove = True
3004dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                    r = r[1:]
3013a15dace3606d6ea9d59486c5d080a1cb4192ff4Fred Drake                if r not in RESOURCE_NAMES:
3023a15dace3606d6ea9d59486c5d080a1cb4192ff4Fred Drake                    usage(1, 'Invalid -u/--use option: ' + a)
3034dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                if remove:
3044dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                    if r in use_resources:
3054dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                        use_resources.remove(r)
3064dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                elif r not in use_resources:
307e41abab33b0b467acd6bdc7d73ce4b5cef4fd5bfAndrew MacIntyre                    use_resources.append(r)
3084698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        elif o in ('-j', '--multiprocess'):
3094698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            use_mp = int(a)
3104698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        elif o == '--slaveargs':
3114698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            args, kwargs = json.loads(a)
3124698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            try:
3134698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                result = runtest(*args, **kwargs)
3144698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            except BaseException, e:
31514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                result = -4, e.__class__.__name__
3164698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            print   # Force a newline (just in case)
3174698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            print json.dumps(result)
3184698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            sys.exit(0)
319c5000dfc4098f8547461e790a91536a923124261Tim Peters    if single and fromfile:
320c5000dfc4098f8547461e790a91536a923124261Tim Peters        usage(2, "-s and -f don't go together!")
3214698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    if use_mp and trace:
3224698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        usage(2, "-T and -j don't go together!")
3234698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    if use_mp and findleaks:
3244698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        usage(2, "-l and -j don't go together!")
32508fca5212528e073600c27c70a34eeef445f393bBarry Warsaw
326152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    good = []
327152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    bad = []
328152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    skipped = []
3299a0db07c2ffd4e4b3ae75d5820dc6b4152b3582bFred Drake    resource_denieds = []
33014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    environment_changed = []
331e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw
332d569f23da94babd616751cd46eea38963e4edfa1Neil Schemenauer    if findleaks:
333a873b03ebb24076b3664ba694eeed4ab07d176d9Barry Warsaw        try:
334a873b03ebb24076b3664ba694eeed4ab07d176d9Barry Warsaw            import gc
335a873b03ebb24076b3664ba694eeed4ab07d176d9Barry Warsaw        except ImportError:
3368a00abc0ff6544a7004a86b4c96e23ca23ac66dcNeil Schemenauer            print 'No GC available, disabling findleaks.'
3373b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw            findleaks = False
338a873b03ebb24076b3664ba694eeed4ab07d176d9Barry Warsaw        else:
3398a00abc0ff6544a7004a86b4c96e23ca23ac66dcNeil Schemenauer            # Uncomment the line below to report garbage that is not
3408a00abc0ff6544a7004a86b4c96e23ca23ac66dcNeil Schemenauer            # freeable by reference counting alone.  By default only
3418a00abc0ff6544a7004a86b4c96e23ca23ac66dcNeil Schemenauer            # garbage that is not collectable by the GC is reported.
3428a00abc0ff6544a7004a86b4c96e23ca23ac66dcNeil Schemenauer            #gc.set_debug(gc.DEBUG_SAVEALL)
343d569f23da94babd616751cd46eea38963e4edfa1Neil Schemenauer            found_garbage = []
344a873b03ebb24076b3664ba694eeed4ab07d176d9Barry Warsaw
345e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw    if single:
346e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw        from tempfile import gettempdir
347e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw        filename = os.path.join(gettempdir(), 'pynexttest')
348e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw        try:
349e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw            fp = open(filename, 'r')
350fc170b1fd5db978f4e8d4c23c138a7b59df681ecEric S. Raymond            next = fp.read().strip()
351e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw            tests = [next]
352e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw            fp.close()
353e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw        except IOError:
354e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw            pass
355c5000dfc4098f8547461e790a91536a923124261Tim Peters
356c5000dfc4098f8547461e790a91536a923124261Tim Peters    if fromfile:
357c5000dfc4098f8547461e790a91536a923124261Tim Peters        tests = []
358c5000dfc4098f8547461e790a91536a923124261Tim Peters        fp = open(fromfile)
359c5000dfc4098f8547461e790a91536a923124261Tim Peters        for line in fp:
360c5000dfc4098f8547461e790a91536a923124261Tim Peters            guts = line.split() # assuming no test has whitespace in its name
361c5000dfc4098f8547461e790a91536a923124261Tim Peters            if guts and not guts[0].startswith('#'):
362c5000dfc4098f8547461e790a91536a923124261Tim Peters                tests.extend(guts)
363c5000dfc4098f8547461e790a91536a923124261Tim Peters        fp.close()
364c5000dfc4098f8547461e790a91536a923124261Tim Peters
365c5000dfc4098f8547461e790a91536a923124261Tim Peters    # Strip .py extensions.
366c5000dfc4098f8547461e790a91536a923124261Tim Peters    if args:
367c5000dfc4098f8547461e790a91536a923124261Tim Peters        args = map(removepy, args)
368c5000dfc4098f8547461e790a91536a923124261Tim Peters    if tests:
369c5000dfc4098f8547461e790a91536a923124261Tim Peters        tests = map(removepy, tests)
370c5000dfc4098f8547461e790a91536a923124261Tim Peters
3716c74fea07d42ac6bc3bc078fb13f903f6cdbbcb9Guido van Rossum    stdtests = STDTESTS[:]
3726c74fea07d42ac6bc3bc078fb13f903f6cdbbcb9Guido van Rossum    nottests = NOTTESTS[:]
373152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    if exclude:
3746c74fea07d42ac6bc3bc078fb13f903f6cdbbcb9Guido van Rossum        for arg in args:
3756c74fea07d42ac6bc3bc078fb13f903f6cdbbcb9Guido van Rossum            if arg in stdtests:
3766c74fea07d42ac6bc3bc078fb13f903f6cdbbcb9Guido van Rossum                stdtests.remove(arg)
3776c74fea07d42ac6bc3bc078fb13f903f6cdbbcb9Guido van Rossum        nottests[:0] = args
37841360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        args = []
379747e1cade657e1e15c0f51d898de901da723e6a2Guido van Rossum    tests = tests or args or findtests(testdir, stdtests, nottests)
380e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw    if single:
381e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw        tests = tests[:1]
382ab1c7918f683e540ae5651c1b585ecda16ae352dSkip Montanaro    if randomize:
383a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter        random.seed(random_seed)
384a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter        print "Using random seed", random_seed
385ab1c7918f683e540ae5651c1b585ecda16ae352dSkip Montanaro        random.shuffle(tests)
3863b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw    if trace:
3873b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw        import trace
3883b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw        tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix],
3893b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw                             trace=False, count=True)
39036dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin    test_times = []
39108fca5212528e073600c27c70a34eeef445f393bBarry Warsaw    test_support.use_resources = use_resources
3925796d26794eee634a4a06637d99d8d5c58da2bdbGuido van Rossum    save_modules = sys.modules.keys()
3934698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou
3944698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    def accumulate_result(test, result):
3954698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        ok, test_time = result
3964698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        test_times.append((test_time, test))
3974698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        if ok > 0:
3984698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            good.append(test)
39914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        elif -2 < ok <= 0:
4004698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            bad.append(test)
40114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray            if ok == -1:
40214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                environment_changed.append(test)
40341360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        else:
4044698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            skipped.append(test)
40514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray            if ok == -3:
4064698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                resource_denieds.append(test)
4074698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou
4084698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    if use_mp:
4094698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        from threading import Thread
4104698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        from Queue import Queue, Empty
4114698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        from subprocess import Popen, PIPE, STDOUT
4124698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        from collections import deque
4134698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        debug_output_pat = re.compile(r"\[\d+ refs\]$")
4144698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        pending = deque()
4154698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        output = Queue()
4164698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        for test in tests:
4174698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            args_tuple = (
4184698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                (test, verbose, quiet, testdir),
4194698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                dict(huntrleaks=huntrleaks, use_resources=use_resources)
4204698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            )
4214698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            pending.append((test, args_tuple))
4224698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        def work():
4234698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            # A worker thread.
42414ca327f998511da5e89f819204c54fa582dd696Neal Norwitz            try:
4254698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                while True:
4264698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    try:
4274698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                        test, args_tuple = pending.popleft()
4284698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    except IndexError:
429282396f27addf4ae6586865ccf449f60753a106cR. David Murray                        output.put((None, None, None, None))
4304698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                        return
43179c3bd80eda7babde76885973fce157339bbffdaAntoine Pitrou                    # -E is needed by some tests, e.g. test_import
43279c3bd80eda7babde76885973fce157339bbffdaAntoine Pitrou                    popen = Popen([sys.executable, '-E', '-m', 'test.regrtest',
4334698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                                   '--slaveargs', json.dumps(args_tuple)],
434282396f27addf4ae6586865ccf449f60753a106cR. David Murray                                   stdout=PIPE, stderr=PIPE,
4354698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                                   universal_newlines=True, close_fds=True)
436282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    stdout, stderr = popen.communicate()
437282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    # Strip last refcount output line if it exists, since it
438282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    # comes from the shutdown of the interpreter in the subcommand.
439282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    stderr = debug_output_pat.sub("", stderr)
440282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    stdout, _, result = stdout.strip().rpartition("\n")
4414698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    result = json.loads(result)
442282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    if not quiet:
443282396f27addf4ae6586865ccf449f60753a106cR. David Murray                        stdout = test+'\n'+stdout
444282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    output.put((test, stdout.rstrip(), stderr.rstrip(), result))
4454698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            except BaseException:
446282396f27addf4ae6586865ccf449f60753a106cR. David Murray                output.put((None, None, None, None))
44714ca327f998511da5e89f819204c54fa582dd696Neal Norwitz                raise
4484698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        workers = [Thread(target=work) for i in range(use_mp)]
4494698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        for worker in workers:
4504698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            worker.start()
4514698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        finished = 0
4524698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        while finished < use_mp:
453282396f27addf4ae6586865ccf449f60753a106cR. David Murray            test, stdout, stderr, result = output.get()
4544698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            if test is None:
4554698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                finished += 1
4564698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                continue
457282396f27addf4ae6586865ccf449f60753a106cR. David Murray            if stdout:
458282396f27addf4ae6586865ccf449f60753a106cR. David Murray                print stdout
459282396f27addf4ae6586865ccf449f60753a106cR. David Murray            if stderr:
460282396f27addf4ae6586865ccf449f60753a106cR. David Murray                print >>sys.stderr, stderr
46114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray            if result[0] == -4:
4624698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                assert result[1] == 'KeyboardInterrupt'
4634698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                pending.clear()
4644698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                raise KeyboardInterrupt   # What else?
4654698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            accumulate_result(test, result)
4664698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        for worker in workers:
4674698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            worker.join()
4684698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    else:
4694698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        for test in tests:
4704698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            if not quiet:
4714698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                print test
4724698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                sys.stdout.flush()
4734698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            if trace:
4744698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                # If we're tracing code coverage, then we don't exit with status
4754698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                # if on a false return value from main.
4764698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                tracer.runctx('runtest(test, verbose, quiet, testdir)',
4774698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                              globals=globals(), locals=vars())
4783b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw            else:
4794698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                try:
4804698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    result = runtest(test, verbose, quiet,
4814698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                                     testdir, huntrleaks)
4824698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    accumulate_result(test, result)
4834698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                except KeyboardInterrupt:
4844698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    # print a newline separate from the ^C
4854698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    print
4864698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    break
4874698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                except:
4884698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    raise
4894698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            if findleaks:
4904698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                gc.collect()
4914698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                if gc.garbage:
4924698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    print "Warning: test created", len(gc.garbage),
4934698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    print "uncollectable object(s)."
4944698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    # move the uncollectable objects somewhere so we don't see
4954698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    # them again
4964698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    found_garbage.extend(gc.garbage)
4974698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    del gc.garbage[:]
4984698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            # Unload the newly imported modules (best effort finalization)
4994698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            for module in sys.modules.keys():
5004698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                if module not in save_modules and module.startswith("test."):
5014698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    test_support.unload(module)
5027a1ea0e88034cdc03d546f0947fc8ecb30f4435fJeremy Hylton
5037a1ea0e88034cdc03d546f0947fc8ecb30f4435fJeremy Hylton    # The lists won't be sorted if running with -r
5047a1ea0e88034cdc03d546f0947fc8ecb30f4435fJeremy Hylton    good.sort()
5057a1ea0e88034cdc03d546f0947fc8ecb30f4435fJeremy Hylton    bad.sort()
5067a1ea0e88034cdc03d546f0947fc8ecb30f4435fJeremy Hylton    skipped.sort()
50714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    environment_changed.sort()
508e0c446bb4ad67294f42d4cb53b4ff28413bd8ddeTim Peters
509152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    if good and not quiet:
51041360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        if not bad and not skipped and len(good) > 1:
51141360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum            print "All",
51241360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        print count(len(good), "test"), "OK."
51336dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin    if print_slow:
51436dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin        test_times.sort(reverse=True)
51536dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin        print "10 slowest tests:"
51636dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin        for time, test in test_times[:10]:
51736dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin            print "%s: %.1fs" % (test, time)
518152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    if bad:
51914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        bad = set(bad) - set(environment_changed)
52014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        if bad:
52114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray            print count(len(bad), "test"), "failed:"
52214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray            printlist(bad)
52314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        if environment_changed:
52414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray            print "{} altered the execution environment:".format(
52514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                count(len(environment_changed), "test"))
52614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray            printlist(environment_changed)
527152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    if skipped and not quiet:
528a45da92484ceececf1cf32b4dfb16004945b001eTim Peters        print count(len(skipped), "test"), "skipped:"
529a45da92484ceececf1cf32b4dfb16004945b001eTim Peters        printlist(skipped)
530e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw
531b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        e = _ExpectedSkips()
532a2be2d624a47e420266606f0cb2a2e030797f650Tim Peters        plat = sys.platform
533b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        if e.isvalid():
534a690a9967e715663b7a421c9ebdad91381cdf1e4Raymond Hettinger            surprise = set(skipped) - e.getexpected() - set(resource_denieds)
535b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters            if surprise:
536b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters                print count(len(surprise), "skip"), \
537a45da92484ceececf1cf32b4dfb16004945b001eTim Peters                      "unexpected on", plat + ":"
538a45da92484ceececf1cf32b4dfb16004945b001eTim Peters                printlist(surprise)
539b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters            else:
540b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters                print "Those skips are all expected on", plat + "."
541b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        else:
542b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters            print "Ask someone to teach regrtest.py about which tests are"
543b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters            print "expected to get skipped on", plat + "."
544b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
54504824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis    if verbose2 and bad:
54604824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis        print "Re-running failed tests in verbose mode"
54704824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis        for test in bad:
548922dd7d49da75b07e27f917db0166f67bdbef3d3Tim Peters            print "Re-running test %r in verbose mode" % test
549922dd7d49da75b07e27f917db0166f67bdbef3d3Tim Peters            sys.stdout.flush()
55004824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis            try:
5515fe5cf6df20607033096c05d68cef7222a35697cNeal Norwitz                test_support.verbose = True
5524698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                ok = runtest(test, True, quiet, testdir,
55304824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis                             huntrleaks)
55404824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis            except KeyboardInterrupt:
55504824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis                # print a newline separate from the ^C
55604824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis                print
55704824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis                break
55804824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis            except:
55904824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis                raise
56004824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis
561e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw    if single:
562e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw        alltests = findtests(testdir, stdtests, nottests)
563e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw        for i in range(len(alltests)):
564e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw            if tests[0] == alltests[i]:
565e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw                if i == len(alltests) - 1:
566e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw                    os.unlink(filename)
567e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw                else:
568e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw                    fp = open(filename, 'w')
569e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw                    fp.write(alltests[i+1] + '\n')
570e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw                    fp.close()
571e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw                break
572e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw        else:
573e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw            os.unlink(filename)
574e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw
5753b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw    if trace:
5763b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw        r = tracer.results()
5773b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw        r.write_results(show_missing=True, summary=True, coverdir=coverdir)
5783b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw
5790179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaro    if runleaks:
5800179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaro        os.system("leaks %d" % os.getpid())
5810179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaro
5825943b4ac1067e5011b66697396d5df93b5ef9af0Tim Peters    sys.exit(len(bad) > 0)
58308fca5212528e073600c27c70a34eeef445f393bBarry Warsaw
584152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
5856fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van RossumSTDTESTS = [
586152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_grammar',
587152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_opcodes',
588f567ca3e1ae851ea6e7418da3df47e034841a08dCollin Winter    'test_dict',
589152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_builtin',
590152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_exceptions',
591152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_types',
5925545314ba7d14abda0a5f6bc50cbef122167f205Collin Winter    'test_unittest',
5935545314ba7d14abda0a5f6bc50cbef122167f205Collin Winter    'test_doctest',
5945545314ba7d14abda0a5f6bc50cbef122167f205Collin Winter    'test_doctest2',
595152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum   ]
596152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
5976fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van RossumNOTTESTS = [
598152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_support',
59962e2c7e3dfffd8465a54b99fc6d3c2a60acab350Jeremy Hylton    'test_future1',
60062e2c7e3dfffd8465a54b99fc6d3c2a60acab350Jeremy Hylton    'test_future2',
601152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    ]
602152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
6036fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossumdef findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
604152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    """Return a list of all applicable test modules."""
6056fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    if not testdir: testdir = findtestdir()
606152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    names = os.listdir(testdir)
607152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    tests = []
608152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    for name in names:
609e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        if name[:5] == "test_" and name[-3:] == os.extsep+"py":
61041360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum            modname = name[:-3]
61141360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum            if modname not in stdtests and modname not in nottests:
61241360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum                tests.append(modname)
613152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    tests.sort()
614152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    return stdtests + tests
615152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
6164698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitroudef runtest(test, verbose, quiet,
6174698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            testdir=None, huntrleaks=False, use_resources=None):
6186fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    """Run a single test.
619e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
6206fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    test -- the name of the test
6216fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    verbose -- if true, print more messages
622f29f47b38beae54db959d0dd2f0800d5dd3fc174Trent Mick    quiet -- if true, don't print 'skipped' messages (probably redundant)
62336dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin    test_times -- a list of (time, test_name) pairs
6246fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    testdir -- test directory
625e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    huntrleaks -- run multiple times to test for leaks; requires a debug
626e55848695347c67effeb08aedcf2ce3d256524cdTim Peters                  build; a triple corresponding to -R's three arguments
627e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    Return:
62814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        -4  KeyboardInterrupt when run under -j
62914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        -3  test skipped because resource denied
63014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        -2  test skipped for some other reason
63114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        -1  test failed because it changed the execution environment
632e55848695347c67effeb08aedcf2ce3d256524cdTim Peters         0  test failed
633e55848695347c67effeb08aedcf2ce3d256524cdTim Peters         1  test passed
6346fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    """
63506c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
6364698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    test_support.verbose = verbose  # Tell tests to be moderately quiet
6374698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    if use_resources is not None:
6384698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        test_support.use_resources = use_resources
639e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    try:
6404698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        return runtest_inner(test, verbose, quiet,
64136dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin                             testdir, huntrleaks)
642e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    finally:
643e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        cleanup_test_droppings(test, verbose)
644e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
64514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
64614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# Unit tests are supposed to leave the execution environment unchanged
64714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# once they complete.  But sometimes tests have bugs, especially when
64814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# tests fail, and the changes to environment go on to mess up other
64914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# tests.  This can cause issues with buildbot stability, since tests
65014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# are run in random order and so problems may appear to come and go.
65114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# There are a few things we can save and restore to mitigate this, and
65214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# the following context manager handles this task.
65314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
65414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murrayclass saved_test_environment:
65514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    """Save bits of the test environment and restore them at block exit.
65614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
6572ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan        with saved_test_environment(testname, verbose, quiet):
65814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray            #stuff
65914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
66014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    Unless quiet is True, a warning is printed to stderr if any of
66114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    the saved items was changed by the test.  The attribute 'changed'
66214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    is initially False, but is set to True if a change is detected.
6632ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan
6642ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    If verbose is more than 1, the before and after state of changed
6652ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    items is also printed.
66614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    """
66714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
66814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    changed = False
66914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
6702ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    def __init__(self, testname, verbose=0, quiet=False):
67114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        self.testname = testname
6722ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan        self.verbose = verbose
67314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        self.quiet = quiet
67414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
67514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    # To add things to save and restore, add a name XXX to the resources list
67614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    # and add corresponding get_XXX/restore_XXX functions.  get_XXX should
67714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    # return the value to be saved and compared against a second call to the
67814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    # get function when test execution completes.  restore_XXX should accept
67914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    # the saved value and restore the resource using it.  It will be called if
6802ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    # and only if a change in the value is detected.
6812ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    #
6822ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    # Note: XXX will have any '.' replaced with '_' characters when determining
6832ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    # the corresponding method names.
68414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
6852ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    resources = ('sys.argv', 'cwd', 'sys.stdin', 'sys.stdout', 'sys.stderr',
6862ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                 'os.environ', 'sys.path')
68714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
68814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_sys_argv(self):
6898157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        return id(sys.argv), sys.argv, sys.argv[:]
69014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_sys_argv(self, saved_argv):
6918157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        sys.argv = saved_argv[1]
6928157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        sys.argv[:] = saved_argv[2]
69314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
69414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_cwd(self):
69514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return os.getcwd()
69614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_cwd(self, saved_cwd):
69714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        os.chdir(saved_cwd)
69814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
69914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_sys_stdout(self):
70014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return sys.stdout
70114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_sys_stdout(self, saved_stdout):
70214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        sys.stdout = saved_stdout
70314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
70414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_sys_stderr(self):
70514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return sys.stderr
70614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_sys_stderr(self, saved_stderr):
70714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        sys.stderr = saved_stderr
70814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
70914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_sys_stdin(self):
71014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return sys.stdin
71114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_sys_stdin(self, saved_stdin):
71214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        sys.stdin = saved_stdin
71314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
71414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_os_environ(self):
7158157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        return id(os.environ), os.environ, dict(os.environ)
71614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_os_environ(self, saved_environ):
7178157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        os.environ = saved_environ[1]
71814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        os.environ.clear()
7198157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        os.environ.update(saved_environ[2])
72014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
72114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_sys_path(self):
7228157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        return id(sys.path), sys.path, sys.path[:]
72314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_sys_path(self, saved_path):
7248157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        sys.path = saved_path[1]
7258157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        sys.path[:] = saved_path[2]
72614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
7272ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    def resource_info(self):
7282ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan        for name in self.resources:
7292ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            method_suffix = name.replace('.', '_')
7302ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            get_name = 'get_' + method_suffix
7312ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            restore_name = 'restore_' + method_suffix
7322ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            yield name, getattr(self, get_name), getattr(self, restore_name)
7332ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan
73414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def __enter__(self):
7352ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan        self.saved_values = dict((name, get()) for name, get, restore
7362ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                                                   in self.resource_info())
73714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return self
73814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
73914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def __exit__(self, exc_type, exc_val, exc_tb):
7402ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan        for name, get, restore in self.resource_info():
7412ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            current = get()
7422ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            original = self.saved_values[name]
7432ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            # Check for changes to the resource's value
7442ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            if current != original:
74514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                self.changed = True
7462ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                restore(original)
74714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                if not self.quiet:
7482ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                    print >>sys.stderr, (
7492ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                          "Warning -- {} was modified by {}".format(
7502ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                                                 name, self.testname))
7512ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                    if self.verbose > 1:
7522ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                        print >>sys.stderr, (
753a3e97ad5bac9439f82c86a355775238655b5be87Nick Coghlan                              "  Before: {}\n  After:  {} ".format(
754a3e97ad5bac9439f82c86a355775238655b5be87Nick Coghlan                                                  original, current))
7552ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            # XXX (ncoghlan): for most resources (e.g. sys.path) identity
7562ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            # matters at least as much as value. For others (e.g. cwd),
7572ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            # identity is irrelevant. Should we add a mechanism to check
7582ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            # for substitution in the cases where it matters?
75914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return False
76014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
76114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
7624698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitroudef runtest_inner(test, verbose, quiet,
76336dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin                  testdir=None, huntrleaks=False):
764152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    test_support.unload(test)
7653b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw    if not testdir:
7663b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw        testdir = findtestdir()
7679390cc15da5cd6920bd41bb4cd146d5d0601345fTim Peters    if verbose:
768ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        capture_stdout = None
7690fcca4e815e3dbb28c73108376079a94ad6ee8deGuido van Rossum    else:
770ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        capture_stdout = cStringIO.StringIO()
77106c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
7724698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    test_time = 0.0
7730f489743ef18ab2da39695d945c79b7a24df8554Collin Winter    refleak = False  # True if the test leaked references.
774152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    try:
775342ca75d9552ff5c606c465d1392a32e44257fe5Tim Peters        save_stdout = sys.stdout
77641360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        try:
777ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc            if capture_stdout:
778ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc                sys.stdout = capture_stdout
779408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw            if test.startswith('test.'):
780408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw                abstest = test
781408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw            else:
782408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw                # Always import it from the test package
783408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw                abstest = 'test.' + test
7842ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            with saved_test_environment(test, verbose, quiet) as environment:
78514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                start_time = time.time()
78614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                the_package = __import__(abstest, globals(), locals(), [])
78714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                the_module = getattr(the_package, test)
78814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                # Old tests run to completion simply as a side-effect of
78914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                # being imported.  For tests based on unittest or doctest,
79014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                # explicitly invoke their test_main() function (if it exists).
79114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                indirect_test = getattr(the_module, "test_main", None)
79214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                if indirect_test is not None:
79314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                    indirect_test()
79414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                if huntrleaks:
79514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                    refleak = dash_R(the_module, test, indirect_test,
79614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                        huntrleaks)
79714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                test_time = time.time() - start_time
79841360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        finally:
799342ca75d9552ff5c606c465d1392a32e44257fe5Tim Peters            sys.stdout = save_stdout
8009a0db07c2ffd4e4b3ae75d5820dc6b4152b3582bFred Drake    except test_support.ResourceDenied, msg:
8019a0db07c2ffd4e4b3ae75d5820dc6b4152b3582bFred Drake        if not quiet:
8029a0db07c2ffd4e4b3ae75d5820dc6b4152b3582bFred Drake            print test, "skipped --", msg
8039a0db07c2ffd4e4b3ae75d5820dc6b4152b3582bFred Drake            sys.stdout.flush()
80414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return -3, test_time
805612f1d5f48d609b4a1b0afd4d64499ff4e5f629dR. David Murray    except unittest.SkipTest, msg:
806f29f47b38beae54db959d0dd2f0800d5dd3fc174Trent Mick        if not quiet:
807de4742b87f7775fc3d9fa76cd640e7cdd5ef69a2Fred Drake            print test, "skipped --", msg
8083cda93ebf60e501350f42fdab72c18eab54718fcGuido van Rossum            sys.stdout.flush()
80914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return -2, test_time
810fe5c22a85e6740012143cdbdc384bd2ebc788c27Fred Drake    except KeyboardInterrupt:
811fe5c22a85e6740012143cdbdc384bd2ebc788c27Fred Drake        raise
812152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    except test_support.TestFailed, msg:
81341360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        print "test", test, "failed --", msg
8143cda93ebf60e501350f42fdab72c18eab54718fcGuido van Rossum        sys.stdout.flush()
8154698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        return 0, test_time
8169e48b272b96aabf597b7aedd358ab890ddbf4c98Guido van Rossum    except:
81741360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        type, value = sys.exc_info()[:2]
81827c4b39025eb9e20f9d7a36b6aa0bb9c97f9bba5Fred Drake        print "test", test, "crashed --", str(type) + ":", value
8193cda93ebf60e501350f42fdab72c18eab54718fcGuido van Rossum        sys.stdout.flush()
82041360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        if verbose:
82141360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum            traceback.print_exc(file=sys.stdout)
8223cda93ebf60e501350f42fdab72c18eab54718fcGuido van Rossum            sys.stdout.flush()
8234698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        return 0, test_time
824152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    else:
8250f489743ef18ab2da39695d945c79b7a24df8554Collin Winter        if refleak:
8264698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            return 0, test_time
82714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        if environment.changed:
82814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray            return -1, test_time
829ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        # Except in verbose mode, tests should not print anything
830ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        if verbose or huntrleaks:
8314698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            return 1, test_time
832ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        output = capture_stdout.getvalue()
833ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        if not output:
8344698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            return 1, test_time
8350fcca4e815e3dbb28c73108376079a94ad6ee8deGuido van Rossum        print "test", test, "produced unexpected output:"
836ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        print "*" * 70
837ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        print output
838ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        print "*" * 70
8393cda93ebf60e501350f42fdab72c18eab54718fcGuido van Rossum        sys.stdout.flush()
8404698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        return 0, test_time
8410fcca4e815e3dbb28c73108376079a94ad6ee8deGuido van Rossum
842e55848695347c67effeb08aedcf2ce3d256524cdTim Petersdef cleanup_test_droppings(testname, verbose):
843e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    import shutil
844a9b4d4777a4018e7fcaa92b6e587992819501b5eBenjamin Peterson    import stat
845e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
846e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # Try to clean up junk commonly left behind.  While tests shouldn't leave
847e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # any files or directories behind, when a test fails that can be tedious
848e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # for it to arrange.  The consequences can be especially nasty on Windows,
849e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # since if a test leaves a file open, it cannot be deleted by name (while
850e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # there's nothing we can do about that here either, we can display the
851e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # name of the offending test, which is a real help).
852e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    for name in (test_support.TESTFN,
853e55848695347c67effeb08aedcf2ce3d256524cdTim Peters                 "db_home",
854e55848695347c67effeb08aedcf2ce3d256524cdTim Peters                ):
855e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        if not os.path.exists(name):
856e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            continue
857e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
858e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        if os.path.isdir(name):
859e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            kind, nuker = "directory", shutil.rmtree
860e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        elif os.path.isfile(name):
861e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            kind, nuker = "file", os.unlink
862e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        else:
863e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            raise SystemError("os.path says %r exists but is neither "
864e55848695347c67effeb08aedcf2ce3d256524cdTim Peters                              "directory nor file" % name)
865e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
866e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        if verbose:
867e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            print "%r left behind %s %r" % (testname, kind, name)
868e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        try:
869a9b4d4777a4018e7fcaa92b6e587992819501b5eBenjamin Peterson            # if we have chmod, fix possible permissions problems
870a9b4d4777a4018e7fcaa92b6e587992819501b5eBenjamin Peterson            # that might prevent cleanup
871a9b4d4777a4018e7fcaa92b6e587992819501b5eBenjamin Peterson            if (hasattr(os, 'chmod')):
872a9b4d4777a4018e7fcaa92b6e587992819501b5eBenjamin Peterson                os.chmod(name, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
873e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            nuker(name)
874e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        except Exception, msg:
875e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            print >> sys.stderr, ("%r left behind %s %r and it couldn't be "
876e55848695347c67effeb08aedcf2ce3d256524cdTim Peters                "removed: %s" % (testname, kind, name, msg))
877e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
87806c5c008193164f04ebcc14ca5176080cf303c54Tim Petersdef dash_R(the_module, test, indirect_test, huntrleaks):
8790f489743ef18ab2da39695d945c79b7a24df8554Collin Winter    """Run a test multiple times, looking for reference leaks.
8800f489743ef18ab2da39695d945c79b7a24df8554Collin Winter
8810f489743ef18ab2da39695d945c79b7a24df8554Collin Winter    Returns:
8820f489743ef18ab2da39695d945c79b7a24df8554Collin Winter        False if the test didn't leak references; True if we detected refleaks.
8830f489743ef18ab2da39695d945c79b7a24df8554Collin Winter    """
88406c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    # This code is hackish and inelegant, but it seems to do the job.
885e55df1fa2a9be432c5c22d7eec8b395227fa4405Amaury Forgeot d'Arc    import copy_reg, _abcoll, _pyio
88606c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
88706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    if not hasattr(sys, 'gettotalrefcount'):
88806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        raise Exception("Tracking reference leaks requires a debug build "
88906c5c008193164f04ebcc14ca5176080cf303c54Tim Peters                        "of Python")
89006c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
89106c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    # Save current values for dash_R_cleanup() to restore.
89206c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    fs = warnings.filters[:]
893dffbf5f5421cbeb20237280c0bd70f989269f844Georg Brandl    ps = copy_reg.dispatch_table.copy()
89406c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    pic = sys.path_importer_cache.copy()
89564c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum    abcs = {}
896e55df1fa2a9be432c5c22d7eec8b395227fa4405Amaury Forgeot d'Arc    modules = _abcoll, _pyio
8974d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc    for abc in [getattr(mod, a) for mod in modules for a in mod.__all__]:
8984d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc        # XXX isinstance(abc, ABCMeta) leads to infinite recursion
8994d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc        if not hasattr(abc, '_abc_registry'):
9006b29dd05c8fff4fc69ab4d68baef86fb7e91548cChristian Heimes            continue
90164c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum        for obj in abc.__subclasses__() + [abc]:
90264c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum            abcs[obj] = obj._abc_registry.copy()
90364c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum
90406c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    if indirect_test:
90506c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        def run_the_test():
90606c5c008193164f04ebcc14ca5176080cf303c54Tim Peters            indirect_test()
90706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    else:
90806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        def run_the_test():
90906c5c008193164f04ebcc14ca5176080cf303c54Tim Peters            reload(the_module)
91006c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
91106c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    deltas = []
91206c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    nwarmup, ntracked, fname = huntrleaks
91306c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    repcount = nwarmup + ntracked
91406c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    print >> sys.stderr, "beginning", repcount, "repetitions"
91506c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    print >> sys.stderr, ("1234567890"*(repcount//10 + 1))[:repcount]
91664c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum    dash_R_cleanup(fs, ps, pic, abcs)
91706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    for i in range(repcount):
91806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        rc = sys.gettotalrefcount()
91906c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        run_the_test()
92006c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        sys.stderr.write('.')
92164c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum        dash_R_cleanup(fs, ps, pic, abcs)
92206c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        if i >= nwarmup:
92306c5c008193164f04ebcc14ca5176080cf303c54Tim Peters            deltas.append(sys.gettotalrefcount() - rc - 2)
92406c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    print >> sys.stderr
92506c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    if any(deltas):
926d9841039dbd0e37746d46f6e46ce1d82b85a21b3Neal Norwitz        msg = '%s leaked %s references, sum=%s' % (test, deltas, sum(deltas))
927d9841039dbd0e37746d46f6e46ce1d82b85a21b3Neal Norwitz        print >> sys.stderr, msg
9284698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        with open(fname, "a") as refrep:
9294698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            print >> refrep, msg
9304698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            refrep.flush()
9310f489743ef18ab2da39695d945c79b7a24df8554Collin Winter        return True
9320f489743ef18ab2da39695d945c79b7a24df8554Collin Winter    return False
93306c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
93464c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossumdef dash_R_cleanup(fs, ps, pic, abcs):
935dffbf5f5421cbeb20237280c0bd70f989269f844Georg Brandl    import gc, copy_reg
9360aa6e1b8fb8c3ae656518a7e319a5f697158e7bfBrett Cannon    import _strptime, linecache
9370aa6e1b8fb8c3ae656518a7e319a5f697158e7bfBrett Cannon    dircache = test_support.import_module('dircache', deprecated=True)
93806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    import urlparse, urllib, urllib2, mimetypes, doctest
9394d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc    import struct, filecmp
94006c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    from distutils.dir_util import _path_created
94106c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
942607bff1ebe81e869697e228322da4c308e8753a5Amaury Forgeot d'Arc    # Clear the warnings registry, so they can be displayed again
943607bff1ebe81e869697e228322da4c308e8753a5Amaury Forgeot d'Arc    for mod in sys.modules.values():
944607bff1ebe81e869697e228322da4c308e8753a5Amaury Forgeot d'Arc        if hasattr(mod, '__warningregistry__'):
945607bff1ebe81e869697e228322da4c308e8753a5Amaury Forgeot d'Arc            del mod.__warningregistry__
946607bff1ebe81e869697e228322da4c308e8753a5Amaury Forgeot d'Arc
94706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    # Restore some original values.
94806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    warnings.filters[:] = fs
949dffbf5f5421cbeb20237280c0bd70f989269f844Georg Brandl    copy_reg.dispatch_table.clear()
950dffbf5f5421cbeb20237280c0bd70f989269f844Georg Brandl    copy_reg.dispatch_table.update(ps)
95106c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    sys.path_importer_cache.clear()
95206c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    sys.path_importer_cache.update(pic)
95306c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
954908caac52e8e62baa1ee54e4e650e1cd3ac37907Christian Heimes    # clear type cache
955422051a3675271e179995ad4a0f056ff94395d55Christian Heimes    sys._clear_type_cache()
956908caac52e8e62baa1ee54e4e650e1cd3ac37907Christian Heimes
95764c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum    # Clear ABC registries, restoring previously saved ABC registries.
9584d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc    for abc, registry in abcs.items():
9594d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc        abc._abc_registry = registry.copy()
9604d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc        abc._abc_cache.clear()
9614d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc        abc._abc_negative_cache.clear()
96264c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum
96306c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    # Clear assorted module caches.
96406c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    _path_created.clear()
96506c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    re.purge()
96606c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    _strptime._regex_cache.clear()
96706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    urlparse.clear_cache()
96806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    urllib.urlcleanup()
96906c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    urllib2.install_opener(None)
97006c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    dircache.reset()
97106c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    linecache.clearcache()
97206c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    mimetypes._default_mime_types()
973c27d655c00ec08f44abea646ae06456cbcca84b6Tim Peters    filecmp._cache.clear()
97476d19f68e4c824bbcba890d924f20a6133ee0326Christian Heimes    struct._clearcache()
97506c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    doctest.master = None
97606c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
97706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    # Collect cyclic trash.
97806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    gc.collect()
97906c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
980152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossumdef findtestdir():
981152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    if __name__ == '__main__':
98241360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        file = sys.argv[0]
983152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    else:
98441360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        file = __file__
985152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    testdir = os.path.dirname(file) or os.curdir
986152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    return testdir
987152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
988c5000dfc4098f8547461e790a91536a923124261Tim Petersdef removepy(name):
989c5000dfc4098f8547461e790a91536a923124261Tim Peters    if name.endswith(os.extsep + "py"):
990c5000dfc4098f8547461e790a91536a923124261Tim Peters        name = name[:-3]
991c5000dfc4098f8547461e790a91536a923124261Tim Peters    return name
992c5000dfc4098f8547461e790a91536a923124261Tim Peters
993152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossumdef count(n, word):
994152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    if n == 1:
99541360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        return "%d %s" % (n, word)
996152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    else:
99741360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        return "%d %ss" % (n, word)
998152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
999a45da92484ceececf1cf32b4dfb16004945b001eTim Petersdef printlist(x, width=70, indent=4):
10007c7efe90737d4636633127a95a6cab1a55d57cf4Tim Peters    """Print the elements of iterable x to stdout.
1001a45da92484ceececf1cf32b4dfb16004945b001eTim Peters
1002a45da92484ceececf1cf32b4dfb16004945b001eTim Peters    Optional arg width (default 70) is the maximum line length.
1003a45da92484ceececf1cf32b4dfb16004945b001eTim Peters    Optional arg indent (default 4) is the number of blanks with which to
1004a45da92484ceececf1cf32b4dfb16004945b001eTim Peters    begin each line.
1005a45da92484ceececf1cf32b4dfb16004945b001eTim Peters    """
1006a45da92484ceececf1cf32b4dfb16004945b001eTim Peters
1007ba78bc4a3216b51398bab59157572a51c38b9ef4Tim Peters    from textwrap import fill
1008ba78bc4a3216b51398bab59157572a51c38b9ef4Tim Peters    blanks = ' ' * indent
1009ba78bc4a3216b51398bab59157572a51c38b9ef4Tim Peters    print fill(' '.join(map(str, x)), width,
1010ba78bc4a3216b51398bab59157572a51c38b9ef4Tim Peters               initial_indent=blanks, subsequent_indent=blanks)
1011a45da92484ceececf1cf32b4dfb16004945b001eTim Peters
1012de14a30d1d70073d36f207fe432e4adad5178399Tim Peters# Map sys.platform to a string containing the basenames of tests
1013de14a30d1d70073d36f207fe432e4adad5178399Tim Peters# expected to be skipped on that platform.
10142a182dbf3f2520ad753792068391775d102b13dfTim Peters#
10152a182dbf3f2520ad753792068391775d102b13dfTim Peters# Special cases:
10162a182dbf3f2520ad753792068391775d102b13dfTim Peters#     test_pep277
10172a182dbf3f2520ad753792068391775d102b13dfTim Peters#         The _ExpectedSkips constructor adds this to the set of expected
10182a182dbf3f2520ad753792068391775d102b13dfTim Peters#         skips if not os.path.supports_unicode_filenames.
101955b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz#     test_timeout
102055b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz#         Controlled by test_timeout.skip_expected.  Requires the network
102155b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz#         resource and a socket module.
1022b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl#
1023b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl# Tests that are expected to be skipped everywhere except on one platform
1024b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl# are also handled separately.
1025de14a30d1d70073d36f207fe432e4adad5178399Tim Peters
1026f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum_expectations = {
1027f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum    'win32':
1028f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        """
1029c7c516aa5140d255f18ffd2e8569434f0904f5dbTim Peters        test__locale
1030823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
103178e35f931128d017d5955841eac8c397ff32595cTim Peters        test_bsddb3
1032f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_commands
1033f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_crypt
1034d703057752d52cef6082a920879a0b62cdeffacaTim Peters        test_curses
1035f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_dbm
1036f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_dl
1037f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_fcntl
1038f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_fork1
10390e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1040f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_gdbm
1041f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_grp
1042fd8e6e599051de07cdb8e9abc9dbadf37c5fca0cTim Peters        test_ioctl
1043f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_largefile
10440e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1045f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_mhlib
1046f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_openpty
1047efc4b121694888e00072f5a079de5496afecb05aTim Peters        test_ossaudiodev
104827d9ee32abac7a580b1ffc980522535bdd20f09bGeorg Brandl        test_pipes
1049f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_poll
1050003eb3088283927e3155f2e2317d3d516edd9645Tim Peters        test_posix
1051f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_pty
1052f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_pwd
10531e33ffa5c719ec611e182681c4a5f2ceb62a08f5Tim Peters        test_resource
1054f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_signal
1055cea2cc4a21e7e2ba84f6dc11682821e502bd1f5fTim Peters        test_threadsignals
1056f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_timing
1057b84de02f75c906fbcf519b4c89b9dbdd18837276Tim Peters        test_wait3
1058b84de02f75c906fbcf519b4c89b9dbdd18837276Tim Peters        test_wait4
1059f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        """,
1060f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum    'linux2':
1061f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        """
1062823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1063f66dacdb017c7481c3ba4f0743d5446146de33c8Guido van Rossum        test_curses
1064f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_dl
1065f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_largefile
10660e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
10674507ec70cff35468f4b1767382f38ecebd4a29a2Guido van Rossum        test_ossaudiodev
1068f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        """,
106949a806edbb298f2a5be3598bd80c21392cb6968dJack Jansen   'mac':
1070aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        """
1071679751455798fab6425fda749a19e783e58b210eJack Jansen        test_atexit
1072aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_bsddb
1073823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1074679751455798fab6425fda749a19e783e58b210eJack Jansen        test_bsddb3
1075679751455798fab6425fda749a19e783e58b210eJack Jansen        test_bz2
1076aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_commands
1077aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_crypt
1078b3be216b41a4755556a887baa6ab440279fbe1dcJack Jansen        test_curses
1079aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_dbm
1080aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_dl
1081aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_fcntl
1082aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_fork1
10830e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1084aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_grp
1085c4d6bdd58a58b365556abfe60c9f5b5f73c555f8Jack Jansen        test_ioctl
1086aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_largefile
1087aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_locale
10880e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1089aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_mmap
1090aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_openpty
1091679751455798fab6425fda749a19e783e58b210eJack Jansen        test_ossaudiodev
1092aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_poll
1093679751455798fab6425fda749a19e783e58b210eJack Jansen        test_popen
1094aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_popen2
10955bb97e66dcbb110877ac17209c7d2641db6f6006Jack Jansen        test_posix
1096aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_pty
1097aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_pwd
1098679751455798fab6425fda749a19e783e58b210eJack Jansen        test_resource
1099aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_signal
1100aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_sundry
1101c4d6bdd58a58b365556abfe60c9f5b5f73c555f8Jack Jansen        test_tarfile
1102aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        test_timing
1103aa78236636d62a07af4759b64ea89452c6690c7eGuido van Rossum        """,
110421ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis    'unixware7':
11050ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        """
11060ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_bsddb
1107823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
11080ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_dl
11090e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
11100ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_largefile
11110e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
11120ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_minidom
11130ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_openpty
11140ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_pyexpat
11150ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_sax
11160ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_sundry
11170ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        """,
111821ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis    'openunix8':
111921ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        """
112021ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_bsddb
1121823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
112221ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_dl
11230e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
112421ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_largefile
11250e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
112621ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_minidom
112721ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_openpty
112821ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_pyexpat
112921ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_sax
113021ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_sundry
113121ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        """,
113221ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis    'sco_sv3':
113321ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        """
113421ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_asynchat
113521ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_bsddb
1136823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
113721ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_dl
113821ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_fork1
11390e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
114021ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_gettext
114121ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_largefile
114221ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_locale
11430e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
114421ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_minidom
114521ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_openpty
114621ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_pyexpat
114721ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_queue
114821ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_sax
114921ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_sundry
115021ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_thread
115121ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_threaded_import
115221ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_threadedtempfile
115321ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_threading
115421ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        """,
1155e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum    'riscos':
1156e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        """
1157e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_asynchat
1158a94568a7535de60f1144e4eea0d027b87017a4b4Martin v. Löwis        test_atexit
1159e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_bsddb
1160823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1161a94568a7535de60f1144e4eea0d027b87017a4b4Martin v. Löwis        test_bsddb3
1162e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_commands
1163e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_crypt
1164e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_dbm
1165e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_dl
1166e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_fcntl
1167e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_fork1
11680e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1169e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_gdbm
1170e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_grp
1171e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_largefile
1172e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_locale
11730e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1174e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_mmap
1175e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_openpty
1176e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_poll
1177e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_popen2
1178e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_pty
1179e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_pwd
1180e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_strop
1181e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_sundry
1182e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_thread
1183e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_threaded_import
1184e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_threadedtempfile
1185e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_threading
1186e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_timing
1187e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        """,
11888a97f4a380a7a356730e48406f8269c3efe5e6ebJack Jansen    'darwin':
1189398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        """
11902bfb94c871caeae622174485264f407d9df354b9Brett Cannon        test__locale
1191acda3394bbfb1db3b22673a80cb2d7e3c68b3da9Jack Jansen        test_bsddb
11929d4270070a5d0c3bcd381d52024d811f3b0a0e39Guido van Rossum        test_bsddb3
1193398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        test_curses
11940e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1195398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        test_gdbm
1196398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        test_largefile
1197acda3394bbfb1db3b22673a80cb2d7e3c68b3da9Jack Jansen        test_locale
11980e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1199398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        test_minidom
1200acda3394bbfb1db3b22673a80cb2d7e3c68b3da9Jack Jansen        test_ossaudiodev
1201398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        test_poll
1202398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        """,
120311c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum    'sunos5':
120411c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        """
120511c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_bsddb
1206823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
120711c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_curses
120811c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_dbm
12090e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
12100e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
121111c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_gdbm
121211c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_gzip
121311c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_openpty
121411c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_zipfile
121511c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_zlib
1216ed375e18d10d37bfea1769aa1fe69795df6cbc10Jeremy Hylton        """,
1217b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro    'hp-ux11':
1218b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        """
1219b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_bsddb
1220823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1221b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_curses
1222b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_dl
12230e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1224b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_gdbm
1225b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_gzip
1226b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_largefile
1227b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_locale
12280e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1229b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_minidom
1230b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_openpty
1231b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_pyexpat
1232b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_sax
1233b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_zipfile
1234b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_zlib
1235b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        """,
1236f90ae20354ceb501f0ba0b6459df17f1a8005a47Martin v. Löwis    'atheos':
1237c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        """
1238823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1239c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_curses
1240c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_dl
1241c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_gdbm
12420e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1243c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_largefile
1244c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_locale
12450e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1246c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_mhlib
1247c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_mmap
1248c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_poll
1249c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_popen2
1250c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_resource
1251c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        """,
125225115940604e6051aa2fb16be88c6b8f64577251Jason Tishler    'cygwin':
125325115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        """
1254823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1255b0f89e05ad9aaf229033bbc81be43c1f826e3930Tim Peters        test_bsddb3
125625115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        test_curses
125725115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        test_dbm
12580e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1259c23f39ca9dc540ace879fcf9746254d94293b86fJason Tishler        test_ioctl
12600e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
126125115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        test_largefile
126225115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        test_locale
12635c4ded2c3b59aa134b82ab17cdfe7ab633194ca6Jason Tishler        test_ossaudiodev
126425115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        test_socketserver
126525115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        """,
1266fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre    'os2emx':
1267fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        """
1268fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_audioop
1269823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1270fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_bsddb3
1271fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_commands
1272fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_curses
1273fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_dl
12740e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
12750e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1276fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_largefile
1277fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_mhlib
1278fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_mmap
1279fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_openpty
1280fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_ossaudiodev
1281fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_pty
1282fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_resource
1283fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_signal
1284fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        """,
1285944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum    'freebsd4':
1286944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        """
1287944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_bsddb
1288944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_bsddb3
12890e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1290f64700a512e89c322f46f3cabda3bf58573eda54Hye-Shik Chang        test_gdbm
1291944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_locale
1292944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_ossaudiodev
1293944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_pep277
1294f64700a512e89c322f46f3cabda3bf58573eda54Hye-Shik Chang        test_pty
1295944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_socketserver
1296f64700a512e89c322f46f3cabda3bf58573eda54Hye-Shik Chang        test_tcl
1297bbb7efd72b3360f0523087cff6868f8fa02fe754Guilherme Polo        test_tk
12987a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_guionly
12997a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_textonly
1300944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_timeout
1301944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_urllibnet
130237040cdace1982772e5f35e4acfa13861d72065dJesse Noller        test_multiprocessing
130356f88113b7272be8403f9b722c943e679e23d362Martin v. Löwis        """,
13048ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum    'aix5':
13058ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        """
13068ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_bsddb
13078ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_bsddb185
13088ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_bsddb3
13098ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_bz2
13108ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_dl
13110e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
13128ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_gdbm
13138ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_gzip
13140e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
13158ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_ossaudiodev
13168ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_tcl
1317bbb7efd72b3360f0523087cff6868f8fa02fe754Guilherme Polo        test_tk
13187a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_guionly
13197a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_textonly
13208ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_zipimport
13218ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_zlib
13228ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        """,
13236da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis    'openbsd3':
13246da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        """
13256da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_bsddb
13266da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_bsddb3
13276da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_ctypes
13286da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_dl
13290e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
13306da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_gdbm
13316da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_locale
13326da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_normalization
13336da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_ossaudiodev
13346da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_pep277
13356da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_tcl
1336bbb7efd72b3360f0523087cff6868f8fa02fe754Guilherme Polo        test_tk
13377a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_guionly
13387a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_textonly
133937040cdace1982772e5f35e4acfa13861d72065dJesse Noller        test_multiprocessing
13406da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        """,
13410870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl    'netbsd3':
13420870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        """
13430870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_bsddb
13440870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_bsddb185
13450870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_bsddb3
13460870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_ctypes
13470870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_curses
13480870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_dl
13490e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
13500870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_gdbm
13510870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_locale
13520870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_ossaudiodev
13530870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_pep277
13540870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_tcl
1355bbb7efd72b3360f0523087cff6868f8fa02fe754Guilherme Polo        test_tk
13567a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_guionly
13577a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_textonly
135837040cdace1982772e5f35e4acfa13861d72065dJesse Noller        test_multiprocessing
13590870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        """,
1360f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum}
136132d0c1b458bbdda8d8895f5b5bedfb4644f839e7Martin v. Löwis_expectations['freebsd5'] = _expectations['freebsd4']
1362f64700a512e89c322f46f3cabda3bf58573eda54Hye-Shik Chang_expectations['freebsd6'] = _expectations['freebsd4']
13634e422817eb1bc5a6a42365001ad45683ae07e559Hye-Shik Chang_expectations['freebsd7'] = _expectations['freebsd4']
1364ea684743daa0c198ab327d07832eca48a9578c68Hye-Shik Chang_expectations['freebsd8'] = _expectations['freebsd4']
1365f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum
1366b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Petersclass _ExpectedSkips:
1367b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters    def __init__(self):
13682a182dbf3f2520ad753792068391775d102b13dfTim Peters        import os.path
136955b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz        from test import test_timeout
13701b445d3fcfcc06e5360e83b978efdb9b1c980278Tim Peters
13717c7efe90737d4636633127a95a6cab1a55d57cf4Tim Peters        self.valid = False
1372de14a30d1d70073d36f207fe432e4adad5178399Tim Peters        if sys.platform in _expectations:
1373f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum            s = _expectations[sys.platform]
1374a690a9967e715663b7a421c9ebdad91381cdf1e4Raymond Hettinger            self.expected = set(s.split())
13751b445d3fcfcc06e5360e83b978efdb9b1c980278Tim Peters
1376b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl            # expected to be skipped on every platform, even Linux
1377b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl            self.expected.add('test_linuxaudiodev')
1378b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl
13792a182dbf3f2520ad753792068391775d102b13dfTim Peters            if not os.path.supports_unicode_filenames:
13802a182dbf3f2520ad753792068391775d102b13dfTim Peters                self.expected.add('test_pep277')
13811b445d3fcfcc06e5360e83b978efdb9b1c980278Tim Peters
138255b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz            if test_timeout.skip_expected:
138355b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz                self.expected.add('test_timeout')
138455b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz
1385fba73698240660d9154b6917b87dd333d6fb8284Martin v. Löwis            if sys.maxint == 9223372036854775807L:
1386fba73698240660d9154b6917b87dd333d6fb8284Martin v. Löwis                self.expected.add('test_imageop')
1387fba73698240660d9154b6917b87dd333d6fb8284Martin v. Löwis
13886afc5e02fa775517fa0b6455906a8c203baf9cd2Jack Jansen            if not sys.platform in ("mac", "darwin"):
13898f40f0635b9d233cf731c442c2ee21101c0f768cAmaury Forgeot d'Arc                MAC_ONLY = ["test_macos", "test_macostools", "test_aepack",
1390b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                            "test_plistlib", "test_scriptpackages",
1391b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                            "test_applesingle"]
13927035c98c5c0eec7f88fab146e72b2a7e4aaf64a5Neal Norwitz                for skip in MAC_ONLY:
13937035c98c5c0eec7f88fab146e72b2a7e4aaf64a5Neal Norwitz                    self.expected.add(skip)
13946f5a2b52ae769ba1833aef8a52b6ab89921ee8c6Benjamin Peterson            elif len(u'\0'.encode('unicode-internal')) == 4:
13956f5a2b52ae769ba1833aef8a52b6ab89921ee8c6Benjamin Peterson                self.expected.add("test_macostools")
13966f5a2b52ae769ba1833aef8a52b6ab89921ee8c6Benjamin Peterson
1397ecd79eb7dbde19ea2adbf2a912caa5b284b477b9Tim Peters
1398ecd79eb7dbde19ea2adbf2a912caa5b284b477b9Tim Peters            if sys.platform != "win32":
1399b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                # test_sqlite is only reliable on Windows where the library
1400b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                # is distributed with Python
14017035c98c5c0eec7f88fab146e72b2a7e4aaf64a5Neal Norwitz                WIN_ONLY = ["test_unicode_file", "test_winreg",
1402b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                            "test_winsound", "test_startfile",
1403b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                            "test_sqlite"]
14047035c98c5c0eec7f88fab146e72b2a7e4aaf64a5Neal Norwitz                for skip in WIN_ONLY:
14057035c98c5c0eec7f88fab146e72b2a7e4aaf64a5Neal Norwitz                    self.expected.add(skip)
1406f2715e076435b74638acb81512c2ee014f75aea2Tim Peters
1407dc48b74497b67a449dd622fdaa7d69e7bff65a5eBrett Cannon            if sys.platform != 'irix':
1408b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                IRIX_ONLY = ["test_imageop", "test_al", "test_cd", "test_cl",
1409b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                             "test_gl", "test_imgfile"]
1410dc48b74497b67a449dd622fdaa7d69e7bff65a5eBrett Cannon                for skip in IRIX_ONLY:
1411dc48b74497b67a449dd622fdaa7d69e7bff65a5eBrett Cannon                    self.expected.add(skip)
1412dc48b74497b67a449dd622fdaa7d69e7bff65a5eBrett Cannon
1413b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl            if sys.platform != 'sunos5':
1414b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                self.expected.add('test_sunaudiodev')
1415b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                self.expected.add('test_nis')
1416b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl
1417d290b04ee3645f8dd2e30c329c5ead2db16ece62Steven Bethard            if not sys.py3kwarning:
1418d290b04ee3645f8dd2e30c329c5ead2db16ece62Steven Bethard                self.expected.add('test_py3kwarn')
1419e8e22cf3c0d9e977bc9f13cfc535c026f92bc7aaSteven Bethard
14207c7efe90737d4636633127a95a6cab1a55d57cf4Tim Peters            self.valid = True
1421b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
1422b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters    def isvalid(self):
1423b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        "Return true iff _ExpectedSkips knows about the current platform."
1424b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        return self.valid
1425b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
1426b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters    def getexpected(self):
1427b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        """Return set of test names we expect to skip on current platform.
1428b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
1429b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        self.isvalid() must be true.
1430b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        """
1431b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
1432b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        assert self.isvalid()
1433b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        return self.expected
1434b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
1435152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossumif __name__ == '__main__':
1436408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw    # Remove regrtest.py's own directory from the module search path.  This
1437408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw    # prevents relative imports from working, and relative imports will screw
1438408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw    # up the testing framework.  E.g. if both test.test_support and
1439408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw    # test_support are imported, they will not contain the same globals, and
1440408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw    # much of the testing framework relies on the globals in the
1441408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw    # test.test_support module.
1442408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw    mydir = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
1443408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw    i = pathlen = len(sys.path)
1444408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw    while i >= 0:
1445408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw        i -= 1
1446408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw        if os.path.abspath(os.path.normpath(sys.path[i])) == mydir:
1447408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw            del sys.path[i]
14484698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    if '--slaveargs' not in sys.argv and len(sys.path) == pathlen:
1449408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw        print 'Could not find %r in sys.path to remove it' % mydir
145008fca5212528e073600c27c70a34eeef445f393bBarry Warsaw    main()
1451