1152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum#! /usr/bin/env python
2152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
33bfa8832076201bebd6348f1df0cb84645d0d462R. David Murray"""
43bfa8832076201bebd6348f1df0cb84645d0d462R. David MurrayUsage:
5152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
63bfa8832076201bebd6348f1df0cb84645d0d462R. David Murraypython -m test.regrtest [options] [test_name1 [test_name2 ...]]
73bfa8832076201bebd6348f1df0cb84645d0d462R. David Murraypython path/to/Lib/test/regrtest.py [options] [test_name1 [test_name2 ...]]
8152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
93bfa8832076201bebd6348f1df0cb84645d0d462R. David Murray
103bfa8832076201bebd6348f1df0cb84645d0d462R. David MurrayIf no arguments or options are provided, finds all files matching
113bfa8832076201bebd6348f1df0cb84645d0d462R. David Murraythe pattern "test_*" in the Lib/test subdirectory and runs
123bfa8832076201bebd6348f1df0cb84645d0d462R. David Murraythem in alphabetical order (but see -M and -u, below, for exceptions).
133bfa8832076201bebd6348f1df0cb84645d0d462R. David Murray
143bfa8832076201bebd6348f1df0cb84645d0d462R. David MurrayFor more rigorous testing, it is useful to use the following
153bfa8832076201bebd6348f1df0cb84645d0d462R. David Murraycommand line:
163bfa8832076201bebd6348f1df0cb84645d0d462R. David Murray
173bfa8832076201bebd6348f1df0cb84645d0d462R. David Murraypython -E -tt -Wd -3 -m test.regrtest [options] [test_name1 ...]
183bfa8832076201bebd6348f1df0cb84645d0d462R. David Murray
193bfa8832076201bebd6348f1df0cb84645d0d462R. David Murray
203bfa8832076201bebd6348f1df0cb84645d0d462R. David MurrayOptions:
21152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
228319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl-h/--help       -- print this text and exit
238319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl
248319170abbe5bd9bc88deee384001a89b59f50fbGeorg BrandlVerbosity
258319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl
268319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl-v/--verbose    -- run tests in verbose mode with output to stdout
278319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl-w/--verbose2   -- re-run failed tests in verbose mode
288319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl-W/--verbose3   -- re-run failed tests in verbose mode immediately
293bfa8832076201bebd6348f1df0cb84645d0d462R. David Murray-q/--quiet      -- no output unless one or more tests fail
308319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl-S/--slow       -- print the slowest 10 tests
318c688bea2613dfb3f270a97d7c409a1ccd0b0d5dAntoine Pitrou   --header     -- print header with interpreter info
328319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl
338319170abbe5bd9bc88deee384001a89b59f50fbGeorg BrandlSelecting tests
348319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl
353684c79e00ad923fa2400458c1a02d6afa3c5ce8Chris Jerdonek-r/--randomize  -- randomize test execution order (see below)
368c688bea2613dfb3f270a97d7c409a1ccd0b0d5dAntoine Pitrou   --randseed   -- pass a random seed to reproduce a previous random run
378319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl-f/--fromfile   -- read names of tests to run from a file (see below)
388319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl-x/--exclude    -- arguments are tests to *exclude*
393bfa8832076201bebd6348f1df0cb84645d0d462R. David Murray-s/--single     -- single step through a set of tests (see below)
408319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl-u/--use RES1,RES2,...
418319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl                -- specify which special resource intensive tests to run
428319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl-M/--memlimit LIMIT
438319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl                -- run very large memory-consuming tests
448319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl
458319170abbe5bd9bc88deee384001a89b59f50fbGeorg BrandlSpecial runs
468319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl
478319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl-l/--findleaks  -- if GC is available detect tests that leak memory
488319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl-L/--runleaks   -- run the leaks(1) command just before exit
498319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl-R/--huntrleaks RUNCOUNTS
508319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl                -- search for reference leaks (needs debug build, v. slow)
518319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl-j/--multiprocess PROCESSES
528319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl                -- run PROCESSES processes at once
533bfa8832076201bebd6348f1df0cb84645d0d462R. David Murray-T/--coverage   -- turn on code coverage tracing using the trace module
548319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl-D/--coverdir DIRECTORY
558319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl                -- Directory where coverage files are put
568319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl-N/--nocoverdir -- Put coverage files alongside modules
578319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl-t/--threshold THRESHOLD
588319170abbe5bd9bc88deee384001a89b59f50fbGeorg Brandl                -- call gc.set_threshold(THRESHOLD)
593bfa8832076201bebd6348f1df0cb84645d0d462R. David Murray-F/--forever    -- run the specified tests in a loop, until an error happens
609537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon-P/--pgo        -- enable Profile Guided Optimization training
61152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
623bfa8832076201bebd6348f1df0cb84645d0d462R. David Murray
633bfa8832076201bebd6348f1df0cb84645d0d462R. David MurrayAdditional Option Details:
64f58ed2596706b97c16174a839c1ed6f6b1f87fa6Guido van Rossum
65c72e66a0489ee53aaf89c677c5b709d30b359532Serhiy Storchaka-r randomizes test execution order. You can use --randseed=int to provide an
66a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winterint seed value for the randomizer; this is useful for reproducing troublesome
67a199f0198600996c6a9de3a3d914f0bc2101d244Collin Wintertest orders.
68a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter
693bfa8832076201bebd6348f1df0cb84645d0d462R. David Murray-s On the first invocation of regrtest using -s, the first test file found
703bfa8832076201bebd6348f1df0cb84645d0d462R. David Murrayor the first test file given on the command line is run, and the name of
713bfa8832076201bebd6348f1df0cb84645d0d462R. David Murraythe next test is recorded in a file named pynexttest.  If run from the
723bfa8832076201bebd6348f1df0cb84645d0d462R. David MurrayPython build directory, pynexttest is located in the 'build' subdirectory,
733bfa8832076201bebd6348f1df0cb84645d0d462R. David Murrayotherwise it is located in tempfile.gettempdir().  On subsequent runs,
743bfa8832076201bebd6348f1df0cb84645d0d462R. David Murraythe test in pynexttest is run, and the next test is written to pynexttest.
753bfa8832076201bebd6348f1df0cb84645d0d462R. David MurrayWhen the last test has been run, pynexttest is deleted.  In this way it
763bfa8832076201bebd6348f1df0cb84645d0d462R. David Murrayis possible to single step through the test files.  This is useful when
773bfa8832076201bebd6348f1df0cb84645d0d462R. David Murraydoing memory analysis on the Python interpreter, which process tends to
783bfa8832076201bebd6348f1df0cb84645d0d462R. David Murrayconsume too many resources to run the full regression test non-stop.
79e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw
809e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum-f reads the names of tests from the file given as f's argument, one
819e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossumor more test names per line.  Whitespace is ignored.  Blank lines and
829e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossumlines beginning with '#' are ignored.  This is especially useful for
839e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossumwhittling down failures involving interactions among tests.
84c5000dfc4098f8547461e790a91536a923124261Tim Peters
850179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaro-L causes the leaks(1) command to be run just before exit if it exists.
860179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaroleaks(1) is available on Mac OS X and presumably on some other
870179a18034b2316a70655d0f05bfbb20a0881f17Skip MontanaroFreeBSD-derived systems.
880179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaro
8961147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson-R runs each test several times and examines sys.gettotalrefcount() to
9061147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudsonsee if the test appears to be leaking references.  The argument should
9161147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudsonbe of the form stab:run:fname where 'stab' is the number of times the
9261147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudsontest is run to let gettotalrefcount settle down, 'run' is the number
9361147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudsonof times further it is run and 'fname' is the name of the file the
9461147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudsonreports are written to.  These parameters all have defaults (5, 4 and
9512d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna"reflog.txt" respectively), and the minimal invocation is '-R :'.
9661147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson
97abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouters-M runs tests that require an exorbitant amount of memory. These tests
98abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouterstypically try to ascertain containers keep working when containing more than
9997ff04789de3e37af585648de70260a54a29bd47Armin Rigo2 billion objects, which only works on 64-bit systems. There are also some
10097ff04789de3e37af585648de70260a54a29bd47Armin Rigotests that try to exhaust the address space of the process, which only makes
10197ff04789de3e37af585648de70260a54a29bd47Armin Rigosense on 32-bit systems with at least 2Gb of memory. The passed-in memlimit,
102abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouterswhich is a string in the form of '2.5Gb', determines howmuch memory the
103abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouterstests will limit themselves to (but they may go slightly over.) The number
104abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Woutersshouldn't be more memory than the machine has (including swap memory). You
105abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Woutersshould also keep in mind that swap memory is generally much, much slower
106abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Woutersthan RAM, and setting memlimit to all available RAM or higher will heavily
107abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouterstax the machine. On the other hand, it is no use running these tests with a
108abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouterslimit of less than 2.5Gb, and many require more than 20Gb. Tests that expect
109abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Woutersto use more than memlimit memory will be skipped. The big-memory tests
110abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Woutersgenerally run very, very long.
111abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouters
1129e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum-u is used to specify which special resource intensive tests to run,
1139e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossumsuch as those requiring large file support or network connectivity.
1149e9d4f8ed8d467d0558251f43c5decc754712d53Guido van RossumThe argument is a comma-separated list of words indicating the
1159e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossumresources to test.  Currently only the following are defined:
11608fca5212528e073600c27c70a34eeef445f393bBarry Warsaw
1173a15dace3606d6ea9d59486c5d080a1cb4192ff4Fred Drake    all -       Enable all special resources.
1183a15dace3606d6ea9d59486c5d080a1cb4192ff4Fred Drake
119315aa361fc60d3328aad3a5dcfd42f08213c25fbGuido van Rossum    audio -     Tests that use the audio device.  (There are known
120315aa361fc60d3328aad3a5dcfd42f08213c25fbGuido van Rossum                cases of broken audio drivers that can crash Python or
121315aa361fc60d3328aad3a5dcfd42f08213c25fbGuido van Rossum                even the Linux kernel.)
122315aa361fc60d3328aad3a5dcfd42f08213c25fbGuido van Rossum
1232158df0b4d8358b5efdcac3024e8cc6d6c92d981Andrew M. Kuchling    curses -    Tests that use curses and will modify the terminal's
1242158df0b4d8358b5efdcac3024e8cc6d6c92d981Andrew M. Kuchling                state and output modes.
1251633a2e3452b40d0e9bb1f15ab16cd6b90f15a19Tim Peters
1269e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum    largefile - It is okay to run some test that may create huge
1279e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum                files.  These tests can take a long time and may
1289e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum                consume >2GB of disk space temporarily.
12908fca5212528e073600c27c70a34eeef445f393bBarry Warsaw
1309e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum    network -   It is okay to run tests that use external network
1319e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum                resource, e.g. testing SSL support for sockets.
1321c6b1a2b4ea38955a3f0514f4709bafd0be96c5eMartin v. Löwis
1331c6b1a2b4ea38955a3f0514f4709bafd0be96c5eMartin v. Löwis    bsddb -     It is okay to run the bsddb testsuite, which takes
1341c6b1a2b4ea38955a3f0514f4709bafd0be96c5eMartin v. Löwis                a long time to complete.
1354dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake
1367c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger    decimal -   Test the decimal module against a large suite that
1377c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger                verifies compliance with standards.
1387c85fa4a5203912aca564ce719a0fdd0fd5411e5Raymond Hettinger
139d989f820c880e7dff16293f6d2861330b5019130Antoine Pitrou    cpu -       Used for certain CPU-heavy tests.
1404336eda88640695b23790e4f52de9ee92c7fee73Jeremy Hylton
141eba28bea9b8ef7df010e65c630b8c0f7009c6005Tim Peters    subprocess  Run all tests for the subprocess module.
142f7f1bb7ff5039626c856efafb68a4d338ab96642Peter Astrand
143aaa2f1dea706daf2a5f431d97a3e3120dba652d2Hye-Shik Chang    urlfetch -  It is okay to download files required on testing.
144aaa2f1dea706daf2a5f431d97a3e3120dba652d2Hye-Shik Chang
145b1a98de25ecd3e11fc3093de31fe844233dcd389Guilherme Polo    gui -       Run tests that require a running GUI.
146b1a98de25ecd3e11fc3093de31fe844233dcd389Guilherme Polo
147f8089c7789d61979fd195e67186a77039534cc87Collin Winter    xpickle -   Test pickle and cPickle against Python 2.4, 2.5 and 2.6 to
148f8089c7789d61979fd195e67186a77039534cc87Collin Winter                test backwards compatibility. These tests take a long time
149f8089c7789d61979fd195e67186a77039534cc87Collin Winter                to run.
150f8089c7789d61979fd195e67186a77039534cc87Collin Winter
1514dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred DrakeTo enable all resources except one, use '-uall,-<resource>'.  For
1524dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drakeexample, to run all the tests except for the bsddb tests, give the
1534dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drakeoption '-uall,-bsddb'.
154152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum"""
155152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
15691a2c89288125b9509ce3fa8436f49a029031be4Michael Foordimport StringIO
157152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossumimport getopt
158ce8e33a095030e7af94f58f9da196b240bdf0476Senthil Kumaranimport json
15936dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskinimport os
160ab1c7918f683e540ae5651c1b585ecda16ae352dSkip Montanaroimport random
16194a9c09e109706af64ae8796882baab2af25be2fNeal Norwitzimport re
162bb8013137550418d89437f85630952d03c8fbc4bSerhiy Storchakaimport shutil
16336dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskinimport sys
16436dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskinimport time
1653b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsawimport traceback
16636dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskinimport warnings
167888a39b54c4f47ee25d53b157e2c50402627cd0bBenjamin Petersonimport unittest
1688f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melottiimport tempfile
169dde5b94875460e704099593766f4a5bc3279483dEzio Melottiimport imp
170b7c20028fe2ebd6cc0f77374dcb30fab732d99abFlorent Xiclunaimport platform
1710932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xiclunaimport sysconfig
172dc15c27f505930a69c73f8c2baf1f9caff9252efGuido van Rossum
173152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
1748f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti# Some times __path__ and __file__ are not absolute (e.g. while running from
1758f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti# Lib/) and, if we change the CWD to run the tests in a temporary dir, some
1768f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti# imports might fail.  This affects only the modules imported before os.chdir().
1778f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti# These modules are searched first in sys.path[0] (so '' -- the CWD) and if
1788f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti# they are found in the CWD their __file__ and __path__ will be relative (this
1798f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti# happens before the chdir).  All the modules imported after the chdir, are
1808f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti# not found in the CWD, and since the other paths in sys.path[1:] are absolute
1818f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti# (site.py absolutize them), the __file__ and __path__ will be absolute too.
1828f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti# Therefore it is necessary to absolutize manually the __file__ and __path__ of
1838f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti# the packages to prevent later imports to fail when the CWD is different.
1848f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melottifor module in sys.modules.itervalues():
1858f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    if hasattr(module, '__path__'):
1868f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti        module.__path__ = [os.path.abspath(path) for path in module.__path__]
1878f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    if hasattr(module, '__file__'):
1888f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti        module.__file__ = os.path.abspath(module.__file__)
1898f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti
1908f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti
191bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum# MacOSX (a.k.a. Darwin) has a default stack size that is too small
192bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum# for deeply recursive regular expressions.  We see this as crashes in
193bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum# the Python test suite when running test_re.py and test_sre.py.  The
194bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum# fix is to set the stack limit to 2048.
195bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum# This approach may also be useful for other Unixy platforms that
196bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum# suffer from small default stack limits.
197bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossumif sys.platform == 'darwin':
198bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum    try:
199bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum        import resource
200bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum    except ImportError:
201bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum        pass
202bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum    else:
203bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum        soft, hard = resource.getrlimit(resource.RLIMIT_STACK)
204bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum        newsoft = min(hard, max(soft, 1024*2048))
205bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum        resource.setrlimit(resource.RLIMIT_STACK, (newsoft, hard))
206bb48465273d2aa98fc7669e99b0d5fb1c57962deGuido van Rossum
2074734372aa0c7ae481bb6b9de0b5c3f9323c670fdZachary Ware# Windows, Tkinter, and resetting the environment after each test don't
2084734372aa0c7ae481bb6b9de0b5c3f9323c670fdZachary Ware# mix well.  To alleviate test failures due to Tcl/Tk not being able to
2094734372aa0c7ae481bb6b9de0b5c3f9323c670fdZachary Ware# find its library, get the necessary environment massage done once early.
2104734372aa0c7ae481bb6b9de0b5c3f9323c670fdZachary Wareif sys.platform == 'win32':
2114734372aa0c7ae481bb6b9de0b5c3f9323c670fdZachary Ware    try:
2124734372aa0c7ae481bb6b9de0b5c3f9323c670fdZachary Ware        import FixTk
2134734372aa0c7ae481bb6b9de0b5c3f9323c670fdZachary Ware    except Exception:
2144734372aa0c7ae481bb6b9de0b5c3f9323c670fdZachary Ware        pass
2154734372aa0c7ae481bb6b9de0b5c3f9323c670fdZachary Ware
2164045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl# Test result constants.
2174045e88b8b43b2748e7be7ed3f0cae932e612b73Georg BrandlPASSED = 1
2184045e88b8b43b2748e7be7ed3f0cae932e612b73Georg BrandlFAILED = 0
2194045e88b8b43b2748e7be7ed3f0cae932e612b73Georg BrandlENV_CHANGED = -1
2204045e88b8b43b2748e7be7ed3f0cae932e612b73Georg BrandlSKIPPED = -2
2214045e88b8b43b2748e7be7ed3f0cae932e612b73Georg BrandlRESOURCE_DENIED = -3
2224045e88b8b43b2748e7be7ed3f0cae932e612b73Georg BrandlINTERRUPTED = -4
2234045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl
22404f357cffef6d764f2f0ff2671dabde75ec250d1Barry Warsawfrom test import test_support
2253a15dace3606d6ea9d59486c5d080a1cb4192ff4Fred Drake
226e2886fd3ca357c4cf7c1584375d9b9bb55d476b7Benjamin PetersonRESOURCE_NAMES = ('audio', 'curses', 'largefile', 'network', 'bsddb',
227d989f820c880e7dff16293f6d2861330b5019130Antoine Pitrou                  'decimal', 'cpu', 'subprocess', 'urlfetch', 'gui',
228f8089c7789d61979fd195e67186a77039534cc87Collin Winter                  'xpickle')
2293a15dace3606d6ea9d59486c5d080a1cb4192ff4Fred Drake
2301273566cb75e188af7cc38c08df21ba9e07260f4Florent XiclunaTEMPDIR = os.path.abspath(tempfile.gettempdir())
2311273566cb75e188af7cc38c08df21ba9e07260f4Florent Xicluna
2323a15dace3606d6ea9d59486c5d080a1cb4192ff4Fred Drake
23308fca5212528e073600c27c70a34eeef445f393bBarry Warsawdef usage(code, msg=''):
23408fca5212528e073600c27c70a34eeef445f393bBarry Warsaw    print __doc__
23508fca5212528e073600c27c70a34eeef445f393bBarry Warsaw    if msg: print msg
23608fca5212528e073600c27c70a34eeef445f393bBarry Warsaw    sys.exit(code)
23708fca5212528e073600c27c70a34eeef445f393bBarry Warsaw
23808fca5212528e073600c27c70a34eeef445f393bBarry Warsaw
239ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arcdef main(tests=None, testdir=None, verbose=0, quiet=False,
2403b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw         exclude=False, single=False, randomize=False, fromfile=None,
241aee4da6b83e49de68fed0c6b999aa109eab98f0dWalter Dörwald         findleaks=False, use_resources=None, trace=False, coverdir='coverage',
242a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter         runleaks=False, huntrleaks=False, verbose2=False, print_slow=False,
2438c688bea2613dfb3f270a97d7c409a1ccd0b0d5dAntoine Pitrou         random_seed=None, use_mp=None, verbose3=False, forever=False,
2449537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon         header=False, pgo=False):
2456fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    """Execute a test suite.
2466fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum
2477e47402264cf87b9bbb61fc9ff610af08add7c7bThomas Wouters    This also parses command-line options and modifies its behavior
248004d5e6880940ddbb38460986ac62ee0f1bae97dFred Drake    accordingly.
2496fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum
2506fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    tests -- a list of strings containing test names (optional)
2516fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    testdir -- the directory in which to look for tests (optional)
2526fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum
2536fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    Users other than the Python test suite will certainly want to
2546fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    specify testdir; if it's omitted, the directory containing the
255004d5e6880940ddbb38460986ac62ee0f1bae97dFred Drake    Python test suite is searched for.
2566fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum
2576fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    If the tests argument is omitted, the tests listed on the
2586fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    command-line will be used.  If that's empty, too, then all *.py
2596fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    files beginning with test_ will be used.
260ab1c7918f683e540ae5651c1b585ecda16ae352dSkip Montanaro
261ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc    The other default arguments (verbose, quiet, exclude,
26212d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna    single, randomize, findleaks, use_resources, trace, coverdir,
26312d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna    print_slow, and random_seed) allow programmers calling main()
26412d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna    directly to set the values that would normally be set by flags
26512d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna    on the command line.
2666fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    """
267004d5e6880940ddbb38460986ac62ee0f1bae97dFred Drake
2688dee809410e2d433bb0be5d8a1699736b90db0b5Tim Peters    test_support.record_original_stdout(sys.stdout)
269152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    try:
2709537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        opts, args = getopt.getopt(sys.argv[1:], 'hvqxsSrf:lu:t:TD:NLR:FwWM:j:P',
27171a015ac1cf80e2f7af989f28e68c8f070964092Georg Brandl            ['help', 'verbose', 'verbose2', 'verbose3', 'quiet',
2723684c79e00ad923fa2400458c1a02d6afa3c5ce8Chris Jerdonek             'exclude', 'single', 'slow', 'randomize', 'fromfile=', 'findleaks',
27371a015ac1cf80e2f7af989f28e68c8f070964092Georg Brandl             'use=', 'threshold=', 'trace', 'coverdir=', 'nocoverdir',
27471a015ac1cf80e2f7af989f28e68c8f070964092Georg Brandl             'runleaks', 'huntrleaks=', 'memlimit=', 'randseed=',
2759537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon             'multiprocess=', 'slaveargs=', 'forever', 'header', 'pgo'])
276152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    except getopt.error, msg:
27708fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        usage(2, msg)
27808fca5212528e073600c27c70a34eeef445f393bBarry Warsaw
27908fca5212528e073600c27c70a34eeef445f393bBarry Warsaw    # Defaults
280a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter    if random_seed is None:
281a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter        random_seed = random.randrange(10000000)
28208fca5212528e073600c27c70a34eeef445f393bBarry Warsaw    if use_resources is None:
28308fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        use_resources = []
284152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    for o, a in opts:
28508fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        if o in ('-h', '--help'):
28608fca5212528e073600c27c70a34eeef445f393bBarry Warsaw            usage(0)
28708fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        elif o in ('-v', '--verbose'):
28808fca5212528e073600c27c70a34eeef445f393bBarry Warsaw            verbose += 1
28904824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis        elif o in ('-w', '--verbose2'):
29004824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis            verbose2 = True
29171a015ac1cf80e2f7af989f28e68c8f070964092Georg Brandl        elif o in ('-W', '--verbose3'):
29271a015ac1cf80e2f7af989f28e68c8f070964092Georg Brandl            verbose3 = True
29308fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        elif o in ('-q', '--quiet'):
2943b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw            quiet = True;
29508fca5212528e073600c27c70a34eeef445f393bBarry Warsaw            verbose = 0
29608fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        elif o in ('-x', '--exclude'):
2973b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw            exclude = True
29808fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        elif o in ('-s', '--single'):
2993b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw            single = True
30036dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin        elif o in ('-S', '--slow'):
30136dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin            print_slow = True
30208fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        elif o in ('-r', '--randomize'):
3033b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw            randomize = True
304a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter        elif o == '--randseed':
305a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter            random_seed = int(a)
306c5000dfc4098f8547461e790a91536a923124261Tim Peters        elif o in ('-f', '--fromfile'):
307c5000dfc4098f8547461e790a91536a923124261Tim Peters            fromfile = a
30808fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        elif o in ('-l', '--findleaks'):
3093b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw            findleaks = True
3100179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaro        elif o in ('-L', '--runleaks'):
3110179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaro            runleaks = True
3129e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum        elif o in ('-t', '--threshold'):
3139e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum            import gc
3149e9d4f8ed8d467d0558251f43c5decc754712d53Guido van Rossum            gc.set_threshold(int(a))
3153b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw        elif o in ('-T', '--coverage'):
3163b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw            trace = True
317aee4da6b83e49de68fed0c6b999aa109eab98f0dWalter Dörwald        elif o in ('-D', '--coverdir'):
318aee4da6b83e49de68fed0c6b999aa109eab98f0dWalter Dörwald            coverdir = os.path.join(os.getcwd(), a)
319aee4da6b83e49de68fed0c6b999aa109eab98f0dWalter Dörwald        elif o in ('-N', '--nocoverdir'):
320aee4da6b83e49de68fed0c6b999aa109eab98f0dWalter Dörwald            coverdir = None
32161147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson        elif o in ('-R', '--huntrleaks'):
32261147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson            huntrleaks = a.split(':')
32312d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna            if len(huntrleaks) not in (2, 3):
32461147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson                print a, huntrleaks
32512d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna                usage(2, '-R takes 2 or 3 colon-separated arguments')
32612d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna            if not huntrleaks[0]:
32761147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson                huntrleaks[0] = 5
32861147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson            else:
32961147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson                huntrleaks[0] = int(huntrleaks[0])
33012d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna            if not huntrleaks[1]:
33161147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson                huntrleaks[1] = 4
33261147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson            else:
33361147f63d9eb20bdd34d5f7549f8379155aefd60Michael W. Hudson                huntrleaks[1] = int(huntrleaks[1])
33412d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna            if len(huntrleaks) == 2 or not huntrleaks[2]:
33512d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna                huntrleaks[2:] = ["reflog.txt"]
336abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouters        elif o in ('-M', '--memlimit'):
337abd08884a685d3724798664f7c2f0aab7a6640c8Thomas Wouters            test_support.set_memlimit(a)
33808fca5212528e073600c27c70a34eeef445f393bBarry Warsaw        elif o in ('-u', '--use'):
339fe3f6969f54cfd3df24f54572a809e0deb47064fGuido van Rossum            u = [x.lower() for x in a.split(',')]
340fe3f6969f54cfd3df24f54572a809e0deb47064fGuido van Rossum            for r in u:
3413a15dace3606d6ea9d59486c5d080a1cb4192ff4Fred Drake                if r == 'all':
3424dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                    use_resources[:] = RESOURCE_NAMES
3434dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                    continue
3444dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                remove = False
3454dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                if r[0] == '-':
3464dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                    remove = True
3474dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                    r = r[1:]
3483a15dace3606d6ea9d59486c5d080a1cb4192ff4Fred Drake                if r not in RESOURCE_NAMES:
3493a15dace3606d6ea9d59486c5d080a1cb4192ff4Fred Drake                    usage(1, 'Invalid -u/--use option: ' + a)
3504dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                if remove:
3514dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                    if r in use_resources:
3524dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                        use_resources.remove(r)
3534dd0f7ef7a0a7e7abe2043bbf8804fde962a8de3Fred Drake                elif r not in use_resources:
354e41abab33b0b467acd6bdc7d73ce4b5cef4fd5bfAndrew MacIntyre                    use_resources.append(r)
3557f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray        elif o in ('-F', '--forever'):
3567f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray            forever = True
3574698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        elif o in ('-j', '--multiprocess'):
3584698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            use_mp = int(a)
3598c688bea2613dfb3f270a97d7c409a1ccd0b0d5dAntoine Pitrou        elif o == '--header':
3608c688bea2613dfb3f270a97d7c409a1ccd0b0d5dAntoine Pitrou            header = True
3614698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        elif o == '--slaveargs':
3624698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            args, kwargs = json.loads(a)
3634698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            try:
3644698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                result = runtest(*args, **kwargs)
3654698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            except BaseException, e:
3664045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl                result = INTERRUPTED, e.__class__.__name__
3674698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            print   # Force a newline (just in case)
3684698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            print json.dumps(result)
3694698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            sys.exit(0)
3709537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        elif o in ('-P', '--pgo'):
37165918e01be239cbf5cec7970ad655da605adf768Brett Cannon            pgo = True
372da82689daa858ca8d89179db2480f7d57d914aedR. David Murray        else:
373da82689daa858ca8d89179db2480f7d57d914aedR. David Murray            print >>sys.stderr, ("No handler for option {}.  Please "
374da82689daa858ca8d89179db2480f7d57d914aedR. David Murray                "report this as a bug at http://bugs.python.org.").format(o)
375da82689daa858ca8d89179db2480f7d57d914aedR. David Murray            sys.exit(1)
376c5000dfc4098f8547461e790a91536a923124261Tim Peters    if single and fromfile:
377c5000dfc4098f8547461e790a91536a923124261Tim Peters        usage(2, "-s and -f don't go together!")
3784698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    if use_mp and trace:
3794698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        usage(2, "-T and -j don't go together!")
3804698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    if use_mp and findleaks:
3814698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        usage(2, "-l and -j don't go together!")
38208fca5212528e073600c27c70a34eeef445f393bBarry Warsaw
383152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    good = []
384152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    bad = []
385152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    skipped = []
3869a0db07c2ffd4e4b3ae75d5820dc6b4152b3582bFred Drake    resource_denieds = []
38714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    environment_changed = []
38875c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna    interrupted = False
389e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw
390d569f23da94babd616751cd46eea38963e4edfa1Neil Schemenauer    if findleaks:
391a873b03ebb24076b3664ba694eeed4ab07d176d9Barry Warsaw        try:
392a873b03ebb24076b3664ba694eeed4ab07d176d9Barry Warsaw            import gc
393a873b03ebb24076b3664ba694eeed4ab07d176d9Barry Warsaw        except ImportError:
3948a00abc0ff6544a7004a86b4c96e23ca23ac66dcNeil Schemenauer            print 'No GC available, disabling findleaks.'
3953b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw            findleaks = False
396a873b03ebb24076b3664ba694eeed4ab07d176d9Barry Warsaw        else:
3978a00abc0ff6544a7004a86b4c96e23ca23ac66dcNeil Schemenauer            # Uncomment the line below to report garbage that is not
3988a00abc0ff6544a7004a86b4c96e23ca23ac66dcNeil Schemenauer            # freeable by reference counting alone.  By default only
3998a00abc0ff6544a7004a86b4c96e23ca23ac66dcNeil Schemenauer            # garbage that is not collectable by the GC is reported.
4008a00abc0ff6544a7004a86b4c96e23ca23ac66dcNeil Schemenauer            #gc.set_debug(gc.DEBUG_SAVEALL)
401d569f23da94babd616751cd46eea38963e4edfa1Neil Schemenauer            found_garbage = []
402a873b03ebb24076b3664ba694eeed4ab07d176d9Barry Warsaw
403e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw    if single:
4041273566cb75e188af7cc38c08df21ba9e07260f4Florent Xicluna        filename = os.path.join(TEMPDIR, 'pynexttest')
405e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw        try:
406e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw            fp = open(filename, 'r')
4077f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray            next_test = fp.read().strip()
4087f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray            tests = [next_test]
409e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw            fp.close()
410e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw        except IOError:
411e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw            pass
412c5000dfc4098f8547461e790a91536a923124261Tim Peters
413c5000dfc4098f8547461e790a91536a923124261Tim Peters    if fromfile:
414c5000dfc4098f8547461e790a91536a923124261Tim Peters        tests = []
4158f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti        fp = open(os.path.join(test_support.SAVEDCWD, fromfile))
416c5000dfc4098f8547461e790a91536a923124261Tim Peters        for line in fp:
417c5000dfc4098f8547461e790a91536a923124261Tim Peters            guts = line.split() # assuming no test has whitespace in its name
418c5000dfc4098f8547461e790a91536a923124261Tim Peters            if guts and not guts[0].startswith('#'):
419c5000dfc4098f8547461e790a91536a923124261Tim Peters                tests.extend(guts)
420c5000dfc4098f8547461e790a91536a923124261Tim Peters        fp.close()
421c5000dfc4098f8547461e790a91536a923124261Tim Peters
422c5000dfc4098f8547461e790a91536a923124261Tim Peters    # Strip .py extensions.
4230932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    removepy(args)
4240932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    removepy(tests)
425c5000dfc4098f8547461e790a91536a923124261Tim Peters
4266c74fea07d42ac6bc3bc078fb13f903f6cdbbcb9Guido van Rossum    stdtests = STDTESTS[:]
42712d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna    nottests = NOTTESTS.copy()
428152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    if exclude:
4296c74fea07d42ac6bc3bc078fb13f903f6cdbbcb9Guido van Rossum        for arg in args:
4306c74fea07d42ac6bc3bc078fb13f903f6cdbbcb9Guido van Rossum            if arg in stdtests:
4316c74fea07d42ac6bc3bc078fb13f903f6cdbbcb9Guido van Rossum                stdtests.remove(arg)
43212d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna            nottests.add(arg)
43341360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        args = []
43458526417ed778df681db17fbabbf0d86fe3c9b67Florent Xicluna
43558526417ed778df681db17fbabbf0d86fe3c9b67Florent Xicluna    # For a partial run, we do not need to clutter the output.
4368c688bea2613dfb3f270a97d7c409a1ccd0b0d5dAntoine Pitrou    if verbose or header or not (quiet or single or tests or args):
4379537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        if not pgo:
4389537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon            # Print basic platform information
4399537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon            print "==", platform.python_implementation(), \
4409537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                        " ".join(sys.version.split())
4419537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon            print "==  ", platform.platform(aliased=True), \
4429537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                          "%s-endian" % sys.byteorder
4439537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon            print "==  ", os.getcwd()
4449537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon            print "Testing with flags:", sys.flags
44558526417ed778df681db17fbabbf0d86fe3c9b67Florent Xicluna
44698e3df38fd863af8f399739e461f84058e7bcfe0R. David Murray    alltests = findtests(testdir, stdtests, nottests)
44775c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna    selected = tests or args or alltests
448e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw    if single:
44975c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        selected = selected[:1]
45098e3df38fd863af8f399739e461f84058e7bcfe0R. David Murray        try:
45175c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna            next_single_test = alltests[alltests.index(selected[0])+1]
45298e3df38fd863af8f399739e461f84058e7bcfe0R. David Murray        except IndexError:
45398e3df38fd863af8f399739e461f84058e7bcfe0R. David Murray            next_single_test = None
454ab1c7918f683e540ae5651c1b585ecda16ae352dSkip Montanaro    if randomize:
455a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter        random.seed(random_seed)
456a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter        print "Using random seed", random_seed
45775c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        random.shuffle(selected)
4583b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw    if trace:
4593b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw        import trace
4603b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw        tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix],
4613b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw                             trace=False, count=True)
4627f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray
46336dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin    test_times = []
46408fca5212528e073600c27c70a34eeef445f393bBarry Warsaw    test_support.use_resources = use_resources
4653c459de2c6e8b8a7bbb4d5250a5a4b271182b9afBenjamin Peterson    save_modules = set(sys.modules)
4664698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou
4674698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    def accumulate_result(test, result):
4684698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        ok, test_time = result
4694698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        test_times.append((test_time, test))
4704045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        if ok == PASSED:
4714698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            good.append(test)
4724045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        elif ok == FAILED:
4734698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            bad.append(test)
4744045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        elif ok == ENV_CHANGED:
4754045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl            environment_changed.append(test)
4764045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        elif ok == SKIPPED:
4774045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl            skipped.append(test)
4784045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        elif ok == RESOURCE_DENIED:
4794698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            skipped.append(test)
4804045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl            resource_denieds.append(test)
4814698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou
4827f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray    if forever:
48375c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        def test_forever(tests=list(selected)):
4847f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray            while True:
4857f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                for test in tests:
4867f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    yield test
4877f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    if bad:
4887f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                        return
4897f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray        tests = test_forever()
49032b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner        test_count = ''
49132b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner        test_count_width = 3
4927f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray    else:
49375c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        tests = iter(selected)
49432b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner        test_count = '/{}'.format(len(selected))
49532b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner        test_count_width = len(test_count) - 1
4967f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray
4974698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    if use_mp:
4981b4a69d79bcdd50829ce871be703606211965e72Victor Stinner        try:
4991b4a69d79bcdd50829ce871be703606211965e72Victor Stinner            from threading import Thread
5001b4a69d79bcdd50829ce871be703606211965e72Victor Stinner        except ImportError:
5011b4a69d79bcdd50829ce871be703606211965e72Victor Stinner            print "Multiprocess option requires thread support"
5021b4a69d79bcdd50829ce871be703606211965e72Victor Stinner            sys.exit(2)
503a4f46e129294c686ef1effdd89c459bd9a624e6dGeorg Brandl        from Queue import Queue
504a4f46e129294c686ef1effdd89c459bd9a624e6dGeorg Brandl        from subprocess import Popen, PIPE
5054698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        debug_output_pat = re.compile(r"\[\d+ refs\]$")
5064698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        output = Queue()
5077f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray        def tests_and_args():
5087f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray            for test in tests:
5097f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                args_tuple = (
510561b96f98c8d396fe9e95feb9e02833cdd9025baR. David Murray                    (test, verbose, quiet),
511eb2cb9737435d6b12beb48ec8e65dc55142349adBrett Cannon                    dict(huntrleaks=huntrleaks, use_resources=use_resources,
512eb2cb9737435d6b12beb48ec8e65dc55142349adBrett Cannon                         pgo=pgo)
5137f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                )
5147f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                yield (test, args_tuple)
5157f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray        pending = tests_and_args()
516a226c9100d52b3f1cefc084e9ab9b863e10655fdAntoine Pitrou        opt_args = test_support.args_from_interpreter_flags()
517a226c9100d52b3f1cefc084e9ab9b863e10655fdAntoine Pitrou        base_cmd = [sys.executable] + opt_args + ['-m', 'test.regrtest']
5189537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        # required to spawn a new process with PGO flag on/off
5199537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        if pgo:
5209537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon            base_cmd = base_cmd + ['--pgo']
5214698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        def work():
5224698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            # A worker thread.
52314ca327f998511da5e89f819204c54fa582dd696Neal Norwitz            try:
5244698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                while True:
5254698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    try:
5267f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                        test, args_tuple = next(pending)
5277f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    except StopIteration:
528282396f27addf4ae6586865ccf449f60753a106cR. David Murray                        output.put((None, None, None, None))
5294698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                        return
53079c3bd80eda7babde76885973fce157339bbffdaAntoine Pitrou                    # -E is needed by some tests, e.g. test_import
531a226c9100d52b3f1cefc084e9ab9b863e10655fdAntoine Pitrou                    popen = Popen(base_cmd + ['--slaveargs', json.dumps(args_tuple)],
532282396f27addf4ae6586865ccf449f60753a106cR. David Murray                                   stdout=PIPE, stderr=PIPE,
5330dd8e1da286499e4d8bffebabe939b6ca3c1345bAntoine Pitrou                                   universal_newlines=True,
5340dd8e1da286499e4d8bffebabe939b6ca3c1345bAntoine Pitrou                                   close_fds=(os.name != 'nt'))
535282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    stdout, stderr = popen.communicate()
536282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    # Strip last refcount output line if it exists, since it
537282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    # comes from the shutdown of the interpreter in the subcommand.
538282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    stderr = debug_output_pat.sub("", stderr)
539282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    stdout, _, result = stdout.strip().rpartition("\n")
5407f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    if not result:
5417f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                        output.put((None, None, None, None))
5427f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                        return
5434698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    result = json.loads(result)
544282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    output.put((test, stdout.rstrip(), stderr.rstrip(), result))
5454698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            except BaseException:
546282396f27addf4ae6586865ccf449f60753a106cR. David Murray                output.put((None, None, None, None))
54714ca327f998511da5e89f819204c54fa582dd696Neal Norwitz                raise
5484698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        workers = [Thread(target=work) for i in range(use_mp)]
5494698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        for worker in workers:
5504698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            worker.start()
5514698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        finished = 0
55232b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner        test_index = 1
5537f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray        try:
5547f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray            while finished < use_mp:
5557f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                test, stdout, stderr, result = output.get()
5567f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                if test is None:
5577f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    finished += 1
5587f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    continue
5597f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                if stdout:
5607f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    print stdout
5619537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                if stderr and not pgo:
5627f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    print >>sys.stderr, stderr
563cf807c04a36821ff3288dfd16927d28236e60238Antoine Pitrou                sys.stdout.flush()
564cf807c04a36821ff3288dfd16927d28236e60238Antoine Pitrou                sys.stderr.flush()
5657f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                if result[0] == INTERRUPTED:
5667f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    assert result[1] == 'KeyboardInterrupt'
5677f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    raise KeyboardInterrupt   # What else?
5687f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                accumulate_result(test, result)
56932b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                if not quiet:
57032b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                    fmt = "[{1:{0}}{2}/{3}] {4}" if bad else "[{1:{0}}{2}] {4}"
57132b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                    print(fmt.format(
57232b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                        test_count_width, test_index, test_count,
57332b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                        len(bad), test))
57432b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                test_index += 1
5757f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray        except KeyboardInterrupt:
57675c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna            interrupted = True
5777f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray            pending.close()
5784698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        for worker in workers:
5794698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            worker.join()
5804698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    else:
58165918e01be239cbf5cec7970ad655da605adf768Brett Cannon        for test_index, test in enumerate(tests, 1):
5824698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            if not quiet:
58332b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                fmt = "[{1:{0}}{2}/{3}] {4}" if bad else "[{1:{0}}{2}] {4}"
58432b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                print(fmt.format(
58532b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                    test_count_width, test_index, test_count, len(bad), test))
5864698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                sys.stdout.flush()
5874698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            if trace:
5884698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                # If we're tracing code coverage, then we don't exit with status
5894698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                # if on a false return value from main.
590561b96f98c8d396fe9e95feb9e02833cdd9025baR. David Murray                tracer.runctx('runtest(test, verbose, quiet)',
5914698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                              globals=globals(), locals=vars())
5923b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw            else:
5934698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                try:
5949537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                    result = runtest(test, verbose, quiet, huntrleaks, None, pgo)
5954045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl                    accumulate_result(test, result)
5964045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl                    if verbose3 and result[0] == FAILED:
5979537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                        if not pgo:
5989537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                            print "Re-running test %r in verbose mode" % test
5999537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                        runtest(test, True, quiet, huntrleaks, None, pgo)
6004698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                except KeyboardInterrupt:
60175c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna                    interrupted = True
6024698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    break
6034698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                except:
6044698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    raise
6054698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            if findleaks:
6064698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                gc.collect()
6074698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                if gc.garbage:
6084698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    print "Warning: test created", len(gc.garbage),
6094698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    print "uncollectable object(s)."
6104698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    # move the uncollectable objects somewhere so we don't see
6114698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    # them again
6124698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    found_garbage.extend(gc.garbage)
6134698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    del gc.garbage[:]
6144698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            # Unload the newly imported modules (best effort finalization)
6154698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            for module in sys.modules.keys():
6164698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                if module not in save_modules and module.startswith("test."):
6174698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    test_support.unload(module)
6187a1ea0e88034cdc03d546f0947fc8ecb30f4435fJeremy Hylton
6199537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon    if interrupted and not pgo:
62075c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        # print a newline after ^C
62175c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        print
62275c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        print "Test suite interrupted by signal SIGINT."
62375c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        omitted = set(selected) - set(good) - set(bad) - set(skipped)
62475c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        print count(len(omitted), "test"), "omitted:"
62575c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        printlist(omitted)
6269537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon    if good and not quiet and not pgo:
62775c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        if not bad and not skipped and not interrupted and len(good) > 1:
62841360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum            print "All",
62941360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        print count(len(good), "test"), "OK."
63036dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin    if print_slow:
63136dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin        test_times.sort(reverse=True)
63236dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin        print "10 slowest tests:"
63336dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin        for time, test in test_times[:10]:
63436dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin            print "%s: %.1fs" % (test, time)
6359537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon    if bad and not pgo:
636ce698f3aa2e535685d0321709de7fa5d3f6de86fZachary Ware        print count(len(bad), "test"), "failed:"
637ce698f3aa2e535685d0321709de7fa5d3f6de86fZachary Ware        printlist(bad)
6389537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon    if environment_changed and not pgo:
639ce698f3aa2e535685d0321709de7fa5d3f6de86fZachary Ware        print "{} altered the execution environment:".format(
640ce698f3aa2e535685d0321709de7fa5d3f6de86fZachary Ware            count(len(environment_changed), "test"))
641ce698f3aa2e535685d0321709de7fa5d3f6de86fZachary Ware        printlist(environment_changed)
6429537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon    if skipped and not quiet and not pgo:
643a45da92484ceececf1cf32b4dfb16004945b001eTim Peters        print count(len(skipped), "test"), "skipped:"
644a45da92484ceececf1cf32b4dfb16004945b001eTim Peters        printlist(skipped)
645e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw
646b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        e = _ExpectedSkips()
647a2be2d624a47e420266606f0cb2a2e030797f650Tim Peters        plat = sys.platform
648b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        if e.isvalid():
649a690a9967e715663b7a421c9ebdad91381cdf1e4Raymond Hettinger            surprise = set(skipped) - e.getexpected() - set(resource_denieds)
650b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters            if surprise:
651b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters                print count(len(surprise), "skip"), \
652a45da92484ceececf1cf32b4dfb16004945b001eTim Peters                      "unexpected on", plat + ":"
653a45da92484ceececf1cf32b4dfb16004945b001eTim Peters                printlist(surprise)
654b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters            else:
655b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters                print "Those skips are all expected on", plat + "."
656b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        else:
657b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters            print "Ask someone to teach regrtest.py about which tests are"
658b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters            print "expected to get skipped on", plat + "."
659b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
66004824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis    if verbose2 and bad:
66104824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis        print "Re-running failed tests in verbose mode"
662a4777f1befa51c4f9c192c7390294e67667a8190Zachary Ware        for test in bad[:]:
663922dd7d49da75b07e27f917db0166f67bdbef3d3Tim Peters            print "Re-running test %r in verbose mode" % test
664922dd7d49da75b07e27f917db0166f67bdbef3d3Tim Peters            sys.stdout.flush()
66504824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis            try:
6665fe5cf6df20607033096c05d68cef7222a35697cNeal Norwitz                test_support.verbose = True
6679537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                ok = runtest(test, True, quiet, huntrleaks, None, pgo)
66804824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis            except KeyboardInterrupt:
66904824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis                # print a newline separate from the ^C
67004824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis                print
67104824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis                break
672a4777f1befa51c4f9c192c7390294e67667a8190Zachary Ware            else:
673a4777f1befa51c4f9c192c7390294e67667a8190Zachary Ware                if ok[0] in {PASSED, ENV_CHANGED, SKIPPED, RESOURCE_DENIED}:
674a4777f1befa51c4f9c192c7390294e67667a8190Zachary Ware                    bad.remove(test)
675a4777f1befa51c4f9c192c7390294e67667a8190Zachary Ware        else:
676a4777f1befa51c4f9c192c7390294e67667a8190Zachary Ware            if bad:
677a4777f1befa51c4f9c192c7390294e67667a8190Zachary Ware                print count(len(bad), "test"), "failed again:"
678a4777f1befa51c4f9c192c7390294e67667a8190Zachary Ware                printlist(bad)
67904824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis
680e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw    if single:
68198e3df38fd863af8f399739e461f84058e7bcfe0R. David Murray        if next_single_test:
68298e3df38fd863af8f399739e461f84058e7bcfe0R. David Murray            with open(filename, 'w') as fp:
68398e3df38fd863af8f399739e461f84058e7bcfe0R. David Murray                fp.write(next_single_test + '\n')
684e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw        else:
685e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw            os.unlink(filename)
686e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw
6873b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw    if trace:
6883b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw        r = tracer.results()
6893b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw        r.write_results(show_missing=True, summary=True, coverdir=coverdir)
6903b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw
6910179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaro    if runleaks:
6920179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaro        os.system("leaks %d" % os.getpid())
6930179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaro
69475c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna    sys.exit(len(bad) > 0 or interrupted)
69508fca5212528e073600c27c70a34eeef445f393bBarry Warsaw
696152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
6976fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van RossumSTDTESTS = [
698152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_grammar',
699152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_opcodes',
700f567ca3e1ae851ea6e7418da3df47e034841a08dCollin Winter    'test_dict',
701152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_builtin',
702152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_exceptions',
703152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_types',
7045545314ba7d14abda0a5f6bc50cbef122167f205Collin Winter    'test_unittest',
7055545314ba7d14abda0a5f6bc50cbef122167f205Collin Winter    'test_doctest',
7065545314ba7d14abda0a5f6bc50cbef122167f205Collin Winter    'test_doctest2',
70712d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna]
708152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
70912d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent XiclunaNOTTESTS = {
710152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_support',
71162e2c7e3dfffd8465a54b99fc6d3c2a60acab350Jeremy Hylton    'test_future1',
71262e2c7e3dfffd8465a54b99fc6d3c2a60acab350Jeremy Hylton    'test_future2',
71312d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna}
714152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
7156fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossumdef findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
716152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    """Return a list of all applicable test modules."""
7170932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    testdir = findtestdir(testdir)
718152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    names = os.listdir(testdir)
719152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    tests = []
72012d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna    others = set(stdtests) | nottests
721152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    for name in names:
7220932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna        modname, ext = os.path.splitext(name)
7230932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna        if modname[:5] == "test_" and ext == ".py" and modname not in others:
7240932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna            tests.append(modname)
7250932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    return stdtests + sorted(tests)
726152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
7274698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitroudef runtest(test, verbose, quiet,
7289537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon            huntrleaks=False, use_resources=None, pgo=False):
7296fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    """Run a single test.
730e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
7316fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    test -- the name of the test
7326fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    verbose -- if true, print more messages
733f29f47b38beae54db959d0dd2f0800d5dd3fc174Trent Mick    quiet -- if true, don't print 'skipped' messages (probably redundant)
73436dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin    test_times -- a list of (time, test_name) pairs
735e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    huntrleaks -- run multiple times to test for leaks; requires a debug
736e55848695347c67effeb08aedcf2ce3d256524cdTim Peters                  build; a triple corresponding to -R's three arguments
73765918e01be239cbf5cec7970ad655da605adf768Brett Cannon    pgo -- if true, do not print unnecessary info when running the test
7389537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon           for Profile Guided Optimization build
73965918e01be239cbf5cec7970ad655da605adf768Brett Cannon
7404045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl    Returns one of the test result constants:
7414045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        INTERRUPTED      KeyboardInterrupt when run under -j
7424045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        RESOURCE_DENIED  test skipped because resource denied
7434045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        SKIPPED          test skipped for some other reason
7444045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        ENV_CHANGED      test failed because it changed the execution environment
7454045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        FAILED           test failed
7464045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        PASSED           test passed
7476fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    """
74806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
7494698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    test_support.verbose = verbose  # Tell tests to be moderately quiet
7504698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    if use_resources is not None:
7514698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        test_support.use_resources = use_resources
752e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    try:
7539537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        return runtest_inner(test, verbose, quiet, huntrleaks, pgo)
754e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    finally:
755e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        cleanup_test_droppings(test, verbose)
756e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
75714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
75814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# Unit tests are supposed to leave the execution environment unchanged
75914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# once they complete.  But sometimes tests have bugs, especially when
76014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# tests fail, and the changes to environment go on to mess up other
76114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# tests.  This can cause issues with buildbot stability, since tests
76214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# are run in random order and so problems may appear to come and go.
76314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# There are a few things we can save and restore to mitigate this, and
76414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# the following context manager handles this task.
76514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
76614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murrayclass saved_test_environment:
76714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    """Save bits of the test environment and restore them at block exit.
76814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
7692ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan        with saved_test_environment(testname, verbose, quiet):
77014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray            #stuff
77114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
77214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    Unless quiet is True, a warning is printed to stderr if any of
77314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    the saved items was changed by the test.  The attribute 'changed'
77414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    is initially False, but is set to True if a change is detected.
7752ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan
7762ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    If verbose is more than 1, the before and after state of changed
7772ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    items is also printed.
77814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    """
77914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
78014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    changed = False
78114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
7829537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon    def __init__(self, testname, verbose=0, quiet=False, pgo=False):
78314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        self.testname = testname
7842ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan        self.verbose = verbose
78514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        self.quiet = quiet
7869537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        self.pgo = pgo
78714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
78814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    # To add things to save and restore, add a name XXX to the resources list
78914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    # and add corresponding get_XXX/restore_XXX functions.  get_XXX should
79014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    # return the value to be saved and compared against a second call to the
79114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    # get function when test execution completes.  restore_XXX should accept
79214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    # the saved value and restore the resource using it.  It will be called if
7932ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    # and only if a change in the value is detected.
7942ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    #
7952ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    # Note: XXX will have any '.' replaced with '_' characters when determining
7962ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    # the corresponding method names.
79714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
7982ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    resources = ('sys.argv', 'cwd', 'sys.stdin', 'sys.stdout', 'sys.stderr',
799da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip                 'os.environ', 'sys.path', 'asyncore.socket_map',
800c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka                 'files',
801da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip                )
80214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
80314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_sys_argv(self):
8048157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        return id(sys.argv), sys.argv, sys.argv[:]
80514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_sys_argv(self, saved_argv):
8068157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        sys.argv = saved_argv[1]
8078157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        sys.argv[:] = saved_argv[2]
80814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
80914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_cwd(self):
81014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return os.getcwd()
81114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_cwd(self, saved_cwd):
81214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        os.chdir(saved_cwd)
81314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
81414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_sys_stdout(self):
81514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return sys.stdout
81614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_sys_stdout(self, saved_stdout):
81714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        sys.stdout = saved_stdout
81814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
81914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_sys_stderr(self):
82014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return sys.stderr
82114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_sys_stderr(self, saved_stderr):
82214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        sys.stderr = saved_stderr
82314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
82414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_sys_stdin(self):
82514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return sys.stdin
82614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_sys_stdin(self, saved_stdin):
82714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        sys.stdin = saved_stdin
82814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
82914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_os_environ(self):
8308157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        return id(os.environ), os.environ, dict(os.environ)
83114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_os_environ(self, saved_environ):
8328157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        os.environ = saved_environ[1]
83314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        os.environ.clear()
8348157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        os.environ.update(saved_environ[2])
83514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
83614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_sys_path(self):
8378157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        return id(sys.path), sys.path, sys.path[:]
83814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_sys_path(self, saved_path):
8398157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        sys.path = saved_path[1]
8408157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        sys.path[:] = saved_path[2]
84114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
8421e8a75414ff57ff8fa42d59d5717be9639c55b12Antoine Pitrou    def get_asyncore_socket_map(self):
8431e8a75414ff57ff8fa42d59d5717be9639c55b12Antoine Pitrou        asyncore = sys.modules.get('asyncore')
844a71bfc46d08bfc63fcc3322f24beb2c7faa37efaAntoine Pitrou        # XXX Making a copy keeps objects alive until __exit__ gets called.
845a71bfc46d08bfc63fcc3322f24beb2c7faa37efaAntoine Pitrou        return asyncore and asyncore.socket_map.copy() or {}
8461e8a75414ff57ff8fa42d59d5717be9639c55b12Antoine Pitrou    def restore_asyncore_socket_map(self, saved_map):
8471e8a75414ff57ff8fa42d59d5717be9639c55b12Antoine Pitrou        asyncore = sys.modules.get('asyncore')
8481e8a75414ff57ff8fa42d59d5717be9639c55b12Antoine Pitrou        if asyncore is not None:
849a71bfc46d08bfc63fcc3322f24beb2c7faa37efaAntoine Pitrou            asyncore.close_all(ignore_all=True)
8501e8a75414ff57ff8fa42d59d5717be9639c55b12Antoine Pitrou            asyncore.socket_map.update(saved_map)
8511e8a75414ff57ff8fa42d59d5717be9639c55b12Antoine Pitrou
852da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip    def get_test_support_TESTFN(self):
853da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip        if os.path.isfile(test_support.TESTFN):
854da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip            result = 'f'
855da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip        elif os.path.isdir(test_support.TESTFN):
856da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip            result = 'd'
857da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip        else:
858da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip            result = None
859da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip        return result
860da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip    def restore_test_support_TESTFN(self, saved_value):
861da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip        if saved_value is None:
862da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip            if os.path.isfile(test_support.TESTFN):
863da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip                os.unlink(test_support.TESTFN)
864da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip            elif os.path.isdir(test_support.TESTFN):
865da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip                shutil.rmtree(test_support.TESTFN)
866da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip
867c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka    def get_files(self):
868c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka        return sorted(fn + ('/' if os.path.isdir(fn) else '')
869c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka                      for fn in os.listdir(os.curdir))
870c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka    def restore_files(self, saved_value):
871ac0d1893b93542b7fd80134bc51b867e44108262Victor Stinner        fn = test_support.TESTFN
872c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka        if fn not in saved_value and (fn + '/') not in saved_value:
873c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka            if os.path.isfile(fn):
874ac0d1893b93542b7fd80134bc51b867e44108262Victor Stinner                test_support.unlink(fn)
875c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka            elif os.path.isdir(fn):
876ac0d1893b93542b7fd80134bc51b867e44108262Victor Stinner                test_support.rmtree(fn)
877c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka
8782ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    def resource_info(self):
8792ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan        for name in self.resources:
8802ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            method_suffix = name.replace('.', '_')
8812ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            get_name = 'get_' + method_suffix
8822ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            restore_name = 'restore_' + method_suffix
8832ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            yield name, getattr(self, get_name), getattr(self, restore_name)
8842ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan
88514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def __enter__(self):
8862ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan        self.saved_values = dict((name, get()) for name, get, restore
8872ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                                                   in self.resource_info())
88814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return self
88914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
89014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def __exit__(self, exc_type, exc_val, exc_tb):
891a71bfc46d08bfc63fcc3322f24beb2c7faa37efaAntoine Pitrou        saved_values = self.saved_values
892a71bfc46d08bfc63fcc3322f24beb2c7faa37efaAntoine Pitrou        del self.saved_values
8932ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan        for name, get, restore in self.resource_info():
8942ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            current = get()
895a71bfc46d08bfc63fcc3322f24beb2c7faa37efaAntoine Pitrou            original = saved_values.pop(name)
8962ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            # Check for changes to the resource's value
8972ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            if current != original:
89814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                self.changed = True
8992ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                restore(original)
9009537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                if not self.quiet and not self.pgo:
9012ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                    print >>sys.stderr, (
9022ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                          "Warning -- {} was modified by {}".format(
9032ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                                                 name, self.testname))
9049537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                    if self.verbose > 1 and not self.pgo:
9052ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                        print >>sys.stderr, (
906a3e97ad5bac9439f82c86a355775238655b5be87Nick Coghlan                              "  Before: {}\n  After:  {} ".format(
907a3e97ad5bac9439f82c86a355775238655b5be87Nick Coghlan                                                  original, current))
9082ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            # XXX (ncoghlan): for most resources (e.g. sys.path) identity
9092ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            # matters at least as much as value. For others (e.g. cwd),
9102ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            # identity is irrelevant. Should we add a mechanism to check
9112ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            # for substitution in the cases where it matters?
91214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return False
91314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
91414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
9159537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannondef runtest_inner(test, verbose, quiet, huntrleaks=False, pgo=False):
916152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    test_support.unload(test)
9179390cc15da5cd6920bd41bb4cd146d5d0601345fTim Peters    if verbose:
918ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        capture_stdout = None
9190fcca4e815e3dbb28c73108376079a94ad6ee8deGuido van Rossum    else:
92091a2c89288125b9509ce3fa8436f49a029031be4Michael Foord        capture_stdout = StringIO.StringIO()
92106c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
9224698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    test_time = 0.0
9230f489743ef18ab2da39695d945c79b7a24df8554Collin Winter    refleak = False  # True if the test leaked references.
924152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    try:
925342ca75d9552ff5c606c465d1392a32e44257fe5Tim Peters        save_stdout = sys.stdout
92641360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        try:
927ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc            if capture_stdout:
928ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc                sys.stdout = capture_stdout
929408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw            if test.startswith('test.'):
930408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw                abstest = test
931408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw            else:
932408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw                # Always import it from the test package
933408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw                abstest = 'test.' + test
934ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka            clear_caches()
9359537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon            with saved_test_environment(test, verbose, quiet, pgo) as environment:
93614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                start_time = time.time()
93714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                the_package = __import__(abstest, globals(), locals(), [])
93814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                the_module = getattr(the_package, test)
93914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                # Old tests run to completion simply as a side-effect of
94014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                # being imported.  For tests based on unittest or doctest,
94114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                # explicitly invoke their test_main() function (if it exists).
94214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                indirect_test = getattr(the_module, "test_main", None)
94314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                if indirect_test is not None:
94414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                    indirect_test()
94514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                if huntrleaks:
94614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                    refleak = dash_R(the_module, test, indirect_test,
94714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                        huntrleaks)
94814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                test_time = time.time() - start_time
94941360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        finally:
950342ca75d9552ff5c606c465d1392a32e44257fe5Tim Peters            sys.stdout = save_stdout
9519a0db07c2ffd4e4b3ae75d5820dc6b4152b3582bFred Drake    except test_support.ResourceDenied, msg:
9529537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        if not quiet and not pgo:
9539a0db07c2ffd4e4b3ae75d5820dc6b4152b3582bFred Drake            print test, "skipped --", msg
9549a0db07c2ffd4e4b3ae75d5820dc6b4152b3582bFred Drake            sys.stdout.flush()
9554045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        return RESOURCE_DENIED, test_time
956612f1d5f48d609b4a1b0afd4d64499ff4e5f629dR. David Murray    except unittest.SkipTest, msg:
9579537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        if not quiet and not pgo:
958de4742b87f7775fc3d9fa76cd640e7cdd5ef69a2Fred Drake            print test, "skipped --", msg
9593cda93ebf60e501350f42fdab72c18eab54718fcGuido van Rossum            sys.stdout.flush()
9604045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        return SKIPPED, test_time
961fe5c22a85e6740012143cdbdc384bd2ebc788c27Fred Drake    except KeyboardInterrupt:
962fe5c22a85e6740012143cdbdc384bd2ebc788c27Fred Drake        raise
963152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    except test_support.TestFailed, msg:
9649537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        if not pgo:
9659537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon            print >>sys.stderr, "test", test, "failed --", msg
9660960bc586ee5a2a53c5c8c92bd17f03d4d75efefR. David Murray        sys.stderr.flush()
9674045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        return FAILED, test_time
9689e48b272b96aabf597b7aedd358ab890ddbf4c98Guido van Rossum    except:
96941360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        type, value = sys.exc_info()[:2]
9709537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        if not pgo:
9719537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon            print >>sys.stderr, "test", test, "crashed --", str(type) + ":", value
9720960bc586ee5a2a53c5c8c92bd17f03d4d75efefR. David Murray        sys.stderr.flush()
9739537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        if verbose and not pgo:
9740960bc586ee5a2a53c5c8c92bd17f03d4d75efefR. David Murray            traceback.print_exc(file=sys.stderr)
9750960bc586ee5a2a53c5c8c92bd17f03d4d75efefR. David Murray            sys.stderr.flush()
9764045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        return FAILED, test_time
977152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    else:
9780f489743ef18ab2da39695d945c79b7a24df8554Collin Winter        if refleak:
9794045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl            return FAILED, test_time
98014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        if environment.changed:
98193c21714946d5fa287bb4aa1d9acb46d55f0b742Georg Brandl            return ENV_CHANGED, test_time
982ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        # Except in verbose mode, tests should not print anything
983ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        if verbose or huntrleaks:
9844045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl            return PASSED, test_time
985ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        output = capture_stdout.getvalue()
986ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        if not output:
9874045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl            return PASSED, test_time
9880fcca4e815e3dbb28c73108376079a94ad6ee8deGuido van Rossum        print "test", test, "produced unexpected output:"
989ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        print "*" * 70
990ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        print output
991ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        print "*" * 70
9923cda93ebf60e501350f42fdab72c18eab54718fcGuido van Rossum        sys.stdout.flush()
9934045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        return FAILED, test_time
9940fcca4e815e3dbb28c73108376079a94ad6ee8deGuido van Rossum
995e55848695347c67effeb08aedcf2ce3d256524cdTim Petersdef cleanup_test_droppings(testname, verbose):
996a9b4d4777a4018e7fcaa92b6e587992819501b5eBenjamin Peterson    import stat
9970d75957d91167bb4363a9db7d0d923376a73f678Antoine Pitrou    import gc
9980d75957d91167bb4363a9db7d0d923376a73f678Antoine Pitrou
9990d75957d91167bb4363a9db7d0d923376a73f678Antoine Pitrou    # First kill any dangling references to open files etc.
10000d75957d91167bb4363a9db7d0d923376a73f678Antoine Pitrou    gc.collect()
1001e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
1002e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # Try to clean up junk commonly left behind.  While tests shouldn't leave
1003e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # any files or directories behind, when a test fails that can be tedious
1004e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # for it to arrange.  The consequences can be especially nasty on Windows,
1005e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # since if a test leaves a file open, it cannot be deleted by name (while
1006e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # there's nothing we can do about that here either, we can display the
1007e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # name of the offending test, which is a real help).
1008e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    for name in (test_support.TESTFN,
1009e55848695347c67effeb08aedcf2ce3d256524cdTim Peters                 "db_home",
1010e55848695347c67effeb08aedcf2ce3d256524cdTim Peters                ):
1011e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        if not os.path.exists(name):
1012e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            continue
1013e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
1014e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        if os.path.isdir(name):
1015e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            kind, nuker = "directory", shutil.rmtree
1016e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        elif os.path.isfile(name):
1017e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            kind, nuker = "file", os.unlink
1018e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        else:
1019e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            raise SystemError("os.path says %r exists but is neither "
1020e55848695347c67effeb08aedcf2ce3d256524cdTim Peters                              "directory nor file" % name)
1021e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
1022e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        if verbose:
1023e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            print "%r left behind %s %r" % (testname, kind, name)
1024e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        try:
1025a9b4d4777a4018e7fcaa92b6e587992819501b5eBenjamin Peterson            # if we have chmod, fix possible permissions problems
1026a9b4d4777a4018e7fcaa92b6e587992819501b5eBenjamin Peterson            # that might prevent cleanup
1027a9b4d4777a4018e7fcaa92b6e587992819501b5eBenjamin Peterson            if (hasattr(os, 'chmod')):
1028a9b4d4777a4018e7fcaa92b6e587992819501b5eBenjamin Peterson                os.chmod(name, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
1029e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            nuker(name)
1030e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        except Exception, msg:
1031e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            print >> sys.stderr, ("%r left behind %s %r and it couldn't be "
1032e55848695347c67effeb08aedcf2ce3d256524cdTim Peters                "removed: %s" % (testname, kind, name, msg))
1033e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
103406c5c008193164f04ebcc14ca5176080cf303c54Tim Petersdef dash_R(the_module, test, indirect_test, huntrleaks):
10350f489743ef18ab2da39695d945c79b7a24df8554Collin Winter    """Run a test multiple times, looking for reference leaks.
10360f489743ef18ab2da39695d945c79b7a24df8554Collin Winter
10370f489743ef18ab2da39695d945c79b7a24df8554Collin Winter    Returns:
10380f489743ef18ab2da39695d945c79b7a24df8554Collin Winter        False if the test didn't leak references; True if we detected refleaks.
10390f489743ef18ab2da39695d945c79b7a24df8554Collin Winter    """
104006c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    # This code is hackish and inelegant, but it seems to do the job.
1041e55df1fa2a9be432c5c22d7eec8b395227fa4405Amaury Forgeot d'Arc    import copy_reg, _abcoll, _pyio
104206c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
104306c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    if not hasattr(sys, 'gettotalrefcount'):
104406c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        raise Exception("Tracking reference leaks requires a debug build "
104506c5c008193164f04ebcc14ca5176080cf303c54Tim Peters                        "of Python")
104606c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
104706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    # Save current values for dash_R_cleanup() to restore.
104806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    fs = warnings.filters[:]
1049dffbf5f5421cbeb20237280c0bd70f989269f844Georg Brandl    ps = copy_reg.dispatch_table.copy()
105006c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    pic = sys.path_importer_cache.copy()
10514976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan    try:
10524976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan        import zipimport
10534976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan    except ImportError:
10545503579db68f22e0ddca030f2aa5cd48373e08c0Benjamin Peterson        zdc = None # Run unmodified on platforms without zipimport support
10554976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan    else:
10564976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan        zdc = zipimport._zip_directory_cache.copy()
105764c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum    abcs = {}
1058e55df1fa2a9be432c5c22d7eec8b395227fa4405Amaury Forgeot d'Arc    modules = _abcoll, _pyio
10594d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc    for abc in [getattr(mod, a) for mod in modules for a in mod.__all__]:
10604d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc        # XXX isinstance(abc, ABCMeta) leads to infinite recursion
10614d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc        if not hasattr(abc, '_abc_registry'):
10626b29dd05c8fff4fc69ab4d68baef86fb7e91548cChristian Heimes            continue
106364c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum        for obj in abc.__subclasses__() + [abc]:
106464c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum            abcs[obj] = obj._abc_registry.copy()
106564c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum
106606c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    if indirect_test:
106706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        def run_the_test():
106806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters            indirect_test()
106906c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    else:
107006c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        def run_the_test():
1071dde5b94875460e704099593766f4a5bc3279483dEzio Melotti            imp.reload(the_module)
107206c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
107306c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    deltas = []
107406c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    nwarmup, ntracked, fname = huntrleaks
10758f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    fname = os.path.join(test_support.SAVEDCWD, fname)
107606c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    repcount = nwarmup + ntracked
107706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    print >> sys.stderr, "beginning", repcount, "repetitions"
107806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    print >> sys.stderr, ("1234567890"*(repcount//10 + 1))[:repcount]
10795503579db68f22e0ddca030f2aa5cd48373e08c0Benjamin Peterson    dash_R_cleanup(fs, ps, pic, zdc, abcs)
108006c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    for i in range(repcount):
1081ac1d93167ce2e98a7cf6896526b3f961463962c6Collin Winter        rc_before = sys.gettotalrefcount()
108206c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        run_the_test()
108306c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        sys.stderr.write('.')
10845503579db68f22e0ddca030f2aa5cd48373e08c0Benjamin Peterson        dash_R_cleanup(fs, ps, pic, zdc, abcs)
1085ac1d93167ce2e98a7cf6896526b3f961463962c6Collin Winter        rc_after = sys.gettotalrefcount()
108606c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        if i >= nwarmup:
1087ac1d93167ce2e98a7cf6896526b3f961463962c6Collin Winter            deltas.append(rc_after - rc_before)
108806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    print >> sys.stderr
108906c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    if any(deltas):
1090d9841039dbd0e37746d46f6e46ce1d82b85a21b3Neal Norwitz        msg = '%s leaked %s references, sum=%s' % (test, deltas, sum(deltas))
1091d9841039dbd0e37746d46f6e46ce1d82b85a21b3Neal Norwitz        print >> sys.stderr, msg
10924698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        with open(fname, "a") as refrep:
10934698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            print >> refrep, msg
10944698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            refrep.flush()
10950f489743ef18ab2da39695d945c79b7a24df8554Collin Winter        return True
10960f489743ef18ab2da39695d945c79b7a24df8554Collin Winter    return False
109706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
10985503579db68f22e0ddca030f2aa5cd48373e08c0Benjamin Petersondef dash_R_cleanup(fs, ps, pic, zdc, abcs):
1099dffbf5f5421cbeb20237280c0bd70f989269f844Georg Brandl    import gc, copy_reg
1100607bff1ebe81e869697e228322da4c308e8753a5Amaury Forgeot d'Arc
110106c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    # Restore some original values.
110206c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    warnings.filters[:] = fs
1103dffbf5f5421cbeb20237280c0bd70f989269f844Georg Brandl    copy_reg.dispatch_table.clear()
1104dffbf5f5421cbeb20237280c0bd70f989269f844Georg Brandl    copy_reg.dispatch_table.update(ps)
110506c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    sys.path_importer_cache.clear()
110606c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    sys.path_importer_cache.update(pic)
11074976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan    try:
11084976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan        import zipimport
11094976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan    except ImportError:
11104976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan        pass # Run unmodified on platforms without zipimport support
11114976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan    else:
11124976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan        zipimport._zip_directory_cache.clear()
11134976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan        zipimport._zip_directory_cache.update(zdc)
111406c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
1115908caac52e8e62baa1ee54e4e650e1cd3ac37907Christian Heimes    # clear type cache
1116422051a3675271e179995ad4a0f056ff94395d55Christian Heimes    sys._clear_type_cache()
1117908caac52e8e62baa1ee54e4e650e1cd3ac37907Christian Heimes
111864c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum    # Clear ABC registries, restoring previously saved ABC registries.
11194d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc    for abc, registry in abcs.items():
11204d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc        abc._abc_registry = registry.copy()
11214d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc        abc._abc_cache.clear()
11224d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc        abc._abc_negative_cache.clear()
112364c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum
1124ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    clear_caches()
1125ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka
1126ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchakadef clear_caches():
1127ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    import gc
1128ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka
1129ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    # Clear the warnings registry, so they can be displayed again
1130ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    for mod in sys.modules.values():
1131ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        if hasattr(mod, '__warningregistry__'):
1132ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka            del mod.__warningregistry__
1133ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka
113406c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    # Clear assorted module caches.
1135ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    # Don't worry about resetting the cache if the module is not loaded
1136ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    try:
1137ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        distutils_dir_util = sys.modules['distutils.dir_util']
1138ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    except KeyError:
1139ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        pass
1140ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    else:
1141ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        distutils_dir_util._path_created.clear()
1142ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka
114306c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    re.purge()
1144ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka
11450e30317891c41ada25d5459644484aef0bc8e88cMeador Inge    try:
1146ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        _strptime = sys.modules['_strptime']
1147ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    except KeyError:
1148ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        pass
1149ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    else:
1150ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        _strptime._regex_cache.clear()
1151ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka
1152ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    try:
1153ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        urlparse = sys.modules['urlparse']
1154ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    except KeyError:
1155ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        pass
1156ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    else:
1157ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        urlparse.clear_cache()
1158ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka
1159ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    try:
1160ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        urllib = sys.modules['urllib']
1161ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    except KeyError:
1162ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        pass
1163ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    else:
1164ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        urllib.urlcleanup()
1165ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka
1166ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    try:
1167ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        urllib2 = sys.modules['urllib2']
1168ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    except KeyError:
1169ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        pass
1170ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    else:
1171ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        urllib2.install_opener(None)
1172ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka
1173ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    try:
1174ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        dircache = sys.modules['dircache']
1175ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    except KeyError:
1176ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        pass
1177ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    else:
1178ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        dircache.reset()
1179ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka
1180ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    try:
1181ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        linecache = sys.modules['linecache']
1182ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    except KeyError:
1183ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        pass
1184ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    else:
1185ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        linecache.clearcache()
1186ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka
1187ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    try:
1188ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        mimetypes = sys.modules['mimetypes']
1189ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    except KeyError:
1190ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        pass
1191ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    else:
1192ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        mimetypes._default_mime_types()
1193ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka
1194ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    try:
1195ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        filecmp = sys.modules['filecmp']
1196ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    except KeyError:
1197ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        pass
1198ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    else:
1199ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        filecmp._cache.clear()
1200ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka
1201ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    try:
1202ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        struct = sys.modules['struct']
1203ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    except KeyError:
1204ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        pass
1205ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    else:
1206ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        struct._clearcache()
1207ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka
1208ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    try:
1209ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        doctest = sys.modules['doctest']
1210ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    except KeyError:
1211ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        pass
1212ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    else:
1213ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        doctest.master = None
1214ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka
1215ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    try:
1216ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka        ctypes = sys.modules['ctypes']
1217ac33bd7d1d55fbf82a72bbc8d21ebf05202c65b2Serhiy Storchaka    except KeyError:
12180e30317891c41ada25d5459644484aef0bc8e88cMeador Inge        pass
12190e30317891c41ada25d5459644484aef0bc8e88cMeador Inge    else:
12200e30317891c41ada25d5459644484aef0bc8e88cMeador Inge        ctypes._reset_cache()
122106c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
122206c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    # Collect cyclic trash.
122306c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    gc.collect()
122406c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
12250932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xiclunadef findtestdir(path=None):
12260932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    return path or os.path.dirname(__file__) or os.curdir
1227152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
12280932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xiclunadef removepy(names):
12290932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    if not names:
12300932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna        return
12310932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    for idx, name in enumerate(names):
12320932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna        basename, ext = os.path.splitext(name)
12330932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna        if ext == '.py':
12340932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna            names[idx] = basename
1235c5000dfc4098f8547461e790a91536a923124261Tim Peters
1236152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossumdef count(n, word):
1237152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    if n == 1:
123841360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        return "%d %s" % (n, word)
1239152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    else:
124041360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        return "%d %ss" % (n, word)
1241152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
1242a45da92484ceececf1cf32b4dfb16004945b001eTim Petersdef printlist(x, width=70, indent=4):
12437c7efe90737d4636633127a95a6cab1a55d57cf4Tim Peters    """Print the elements of iterable x to stdout.
1244a45da92484ceececf1cf32b4dfb16004945b001eTim Peters
1245a45da92484ceececf1cf32b4dfb16004945b001eTim Peters    Optional arg width (default 70) is the maximum line length.
1246a45da92484ceececf1cf32b4dfb16004945b001eTim Peters    Optional arg indent (default 4) is the number of blanks with which to
1247a45da92484ceececf1cf32b4dfb16004945b001eTim Peters    begin each line.
1248a45da92484ceececf1cf32b4dfb16004945b001eTim Peters    """
1249a45da92484ceececf1cf32b4dfb16004945b001eTim Peters
1250ba78bc4a3216b51398bab59157572a51c38b9ef4Tim Peters    from textwrap import fill
1251ba78bc4a3216b51398bab59157572a51c38b9ef4Tim Peters    blanks = ' ' * indent
1252355adc5a451ce3b22b66a3af883d35e89e2a0eabFlorent Xicluna    # Print the sorted list: 'x' may be a '--random' list or a set()
1253355adc5a451ce3b22b66a3af883d35e89e2a0eabFlorent Xicluna    print fill(' '.join(str(elt) for elt in sorted(x)), width,
1254ba78bc4a3216b51398bab59157572a51c38b9ef4Tim Peters               initial_indent=blanks, subsequent_indent=blanks)
1255a45da92484ceececf1cf32b4dfb16004945b001eTim Peters
1256de14a30d1d70073d36f207fe432e4adad5178399Tim Peters# Map sys.platform to a string containing the basenames of tests
1257de14a30d1d70073d36f207fe432e4adad5178399Tim Peters# expected to be skipped on that platform.
12582a182dbf3f2520ad753792068391775d102b13dfTim Peters#
12592a182dbf3f2520ad753792068391775d102b13dfTim Peters# Special cases:
12602a182dbf3f2520ad753792068391775d102b13dfTim Peters#     test_pep277
12612a182dbf3f2520ad753792068391775d102b13dfTim Peters#         The _ExpectedSkips constructor adds this to the set of expected
12622a182dbf3f2520ad753792068391775d102b13dfTim Peters#         skips if not os.path.supports_unicode_filenames.
126355b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz#     test_timeout
126455b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz#         Controlled by test_timeout.skip_expected.  Requires the network
126555b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz#         resource and a socket module.
1266b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl#
1267b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl# Tests that are expected to be skipped everywhere except on one platform
1268b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl# are also handled separately.
1269de14a30d1d70073d36f207fe432e4adad5178399Tim Peters
1270f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum_expectations = {
1271f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum    'win32':
1272f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        """
1273c7c516aa5140d255f18ffd2e8569434f0904f5dbTim Peters        test__locale
1274823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
127578e35f931128d017d5955841eac8c397ff32595cTim Peters        test_bsddb3
1276f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_commands
1277f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_crypt
1278d703057752d52cef6082a920879a0b62cdeffacaTim Peters        test_curses
1279f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_dbm
1280f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_dl
1281f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_fcntl
1282f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_fork1
12830e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1284f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_gdbm
1285f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_grp
1286fd8e6e599051de07cdb8e9abc9dbadf37c5fca0cTim Peters        test_ioctl
1287f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_largefile
12880e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1289f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_mhlib
1290f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_openpty
1291efc4b121694888e00072f5a079de5496afecb05aTim Peters        test_ossaudiodev
129227d9ee32abac7a580b1ffc980522535bdd20f09bGeorg Brandl        test_pipes
1293f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_poll
1294003eb3088283927e3155f2e2317d3d516edd9645Tim Peters        test_posix
1295f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_pty
1296f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_pwd
12971e33ffa5c719ec611e182681c4a5f2ceb62a08f5Tim Peters        test_resource
1298f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_signal
12997ece150395f9908870c3baa883e465ec9fe4dd9cSerhiy Storchaka        test_spwd
1300cea2cc4a21e7e2ba84f6dc11682821e502bd1f5fTim Peters        test_threadsignals
1301f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_timing
1302b84de02f75c906fbcf519b4c89b9dbdd18837276Tim Peters        test_wait3
1303b84de02f75c906fbcf519b4c89b9dbdd18837276Tim Peters        test_wait4
1304f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        """,
1305f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum    'linux2':
1306f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        """
1307823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1308f66dacdb017c7481c3ba4f0743d5446146de33c8Guido van Rossum        test_curses
1309f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_dl
1310f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_largefile
13110e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
13124507ec70cff35468f4b1767382f38ecebd4a29a2Guido van Rossum        test_ossaudiodev
1313f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        """,
131421ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis    'unixware7':
13150ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        """
13160ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_bsddb
1317823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
13180ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_dl
13190e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
13200ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_largefile
13210e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
13220ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_minidom
13230ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_openpty
13240ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_pyexpat
13250ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_sax
13260ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_sundry
13270ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        """,
132821ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis    'openunix8':
132921ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        """
133021ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_bsddb
1331823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
133221ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_dl
13330e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
133421ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_largefile
13350e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
133621ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_minidom
133721ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_openpty
133821ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_pyexpat
133921ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_sax
134021ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_sundry
134121ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        """,
134221ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis    'sco_sv3':
134321ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        """
134421ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_asynchat
134521ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_bsddb
1346823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
134721ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_dl
134821ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_fork1
13490e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
135021ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_gettext
135121ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_largefile
135221ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_locale
13530e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
135421ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_minidom
135521ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_openpty
135621ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_pyexpat
135721ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_queue
135821ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_sax
135921ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_sundry
136021ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_thread
136121ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_threaded_import
136221ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_threadedtempfile
136321ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_threading
136421ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        """,
1365e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum    'riscos':
1366e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        """
1367e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_asynchat
1368a94568a7535de60f1144e4eea0d027b87017a4b4Martin v. Löwis        test_atexit
1369e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_bsddb
1370823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1371a94568a7535de60f1144e4eea0d027b87017a4b4Martin v. Löwis        test_bsddb3
1372e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_commands
1373e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_crypt
1374e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_dbm
1375e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_dl
1376e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_fcntl
1377e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_fork1
13780e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1379e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_gdbm
1380e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_grp
1381e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_largefile
1382e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_locale
13830e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1384e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_mmap
1385e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_openpty
1386e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_poll
1387e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_popen2
1388e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_pty
1389e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_pwd
1390e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_strop
1391e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_sundry
1392e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_thread
1393e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_threaded_import
1394e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_threadedtempfile
1395e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_threading
1396e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_timing
1397e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        """,
13988a97f4a380a7a356730e48406f8269c3efe5e6ebJack Jansen    'darwin':
1399398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        """
14002bfb94c871caeae622174485264f407d9df354b9Brett Cannon        test__locale
1401acda3394bbfb1db3b22673a80cb2d7e3c68b3da9Jack Jansen        test_bsddb
14029d4270070a5d0c3bcd381d52024d811f3b0a0e39Guido van Rossum        test_bsddb3
1403398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        test_curses
14040e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
140529cdfe696a40253989de0ca26204c7169b59fae3Skip Montanaro        test_gdb
1406398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        test_gdbm
1407398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        test_largefile
1408acda3394bbfb1db3b22673a80cb2d7e3c68b3da9Jack Jansen        test_locale
14090e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1410398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        test_minidom
1411acda3394bbfb1db3b22673a80cb2d7e3c68b3da9Jack Jansen        test_ossaudiodev
1412398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        test_poll
1413398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        """,
141411c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum    'sunos5':
141511c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        """
141611c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_bsddb
1417823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
141811c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_curses
141911c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_dbm
14200e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
14210e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
142211c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_gdbm
142311c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_gzip
142411c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_openpty
142511c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_zipfile
142611c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_zlib
1427ed375e18d10d37bfea1769aa1fe69795df6cbc10Jeremy Hylton        """,
1428b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro    'hp-ux11':
1429b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        """
1430b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_bsddb
1431823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1432b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_curses
1433b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_dl
14340e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1435b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_gdbm
1436b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_gzip
1437b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_largefile
1438b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_locale
14390e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1440b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_minidom
1441b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_openpty
1442b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_pyexpat
1443b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_sax
1444b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_zipfile
1445b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_zlib
1446b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        """,
1447f90ae20354ceb501f0ba0b6459df17f1a8005a47Martin v. Löwis    'atheos':
1448c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        """
1449823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1450c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_curses
1451c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_dl
1452c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_gdbm
14530e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1454c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_largefile
1455c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_locale
14560e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1457c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_mhlib
1458c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_mmap
1459c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_poll
1460c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_popen2
1461c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_resource
1462c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        """,
146325115940604e6051aa2fb16be88c6b8f64577251Jason Tishler    'cygwin':
146425115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        """
1465823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1466b0f89e05ad9aaf229033bbc81be43c1f826e3930Tim Peters        test_bsddb3
146725115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        test_curses
146825115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        test_dbm
14690e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1470c23f39ca9dc540ace879fcf9746254d94293b86fJason Tishler        test_ioctl
14710e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
147225115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        test_largefile
147325115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        test_locale
14745c4ded2c3b59aa134b82ab17cdfe7ab633194ca6Jason Tishler        test_ossaudiodev
147525115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        test_socketserver
147625115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        """,
1477fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre    'os2emx':
1478fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        """
1479fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_audioop
1480823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1481fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_bsddb3
1482fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_commands
1483fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_curses
1484fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_dl
14850e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
14860e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1487fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_largefile
1488fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_mhlib
1489fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_mmap
1490fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_openpty
1491fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_ossaudiodev
1492fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_pty
1493fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_resource
1494fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_signal
1495fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        """,
1496944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum    'freebsd4':
1497944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        """
1498944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_bsddb
1499944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_bsddb3
15000e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1501f64700a512e89c322f46f3cabda3bf58573eda54Hye-Shik Chang        test_gdbm
1502944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_locale
1503944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_ossaudiodev
1504944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_pep277
1505f64700a512e89c322f46f3cabda3bf58573eda54Hye-Shik Chang        test_pty
1506944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_socketserver
1507f64700a512e89c322f46f3cabda3bf58573eda54Hye-Shik Chang        test_tcl
1508bbb7efd72b3360f0523087cff6868f8fa02fe754Guilherme Polo        test_tk
15097a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_guionly
15107a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_textonly
1511944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_timeout
1512944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_urllibnet
151337040cdace1982772e5f35e4acfa13861d72065dJesse Noller        test_multiprocessing
151456f88113b7272be8403f9b722c943e679e23d362Martin v. Löwis        """,
15158ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum    'aix5':
15168ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        """
15178ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_bsddb
15188ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_bsddb185
15198ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_bsddb3
15208ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_bz2
15218ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_dl
15220e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
15238ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_gdbm
15248ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_gzip
15250e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
15268ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_ossaudiodev
15278ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_tcl
1528bbb7efd72b3360f0523087cff6868f8fa02fe754Guilherme Polo        test_tk
15297a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_guionly
15307a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_textonly
15318ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_zipimport
15328ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_zlib
15338ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        """,
15346da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis    'openbsd3':
15356da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        """
153627e024023cca704e44b0d5f577415d061f89f7d7Florent Xicluna        test_ascii_formatd
15376da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_bsddb
15386da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_bsddb3
15396da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_ctypes
15406da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_dl
15410e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
15426da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_gdbm
15436da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_locale
15446da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_normalization
15456da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_ossaudiodev
15466da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_pep277
15476da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_tcl
1548bbb7efd72b3360f0523087cff6868f8fa02fe754Guilherme Polo        test_tk
15497a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_guionly
15507a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_textonly
155137040cdace1982772e5f35e4acfa13861d72065dJesse Noller        test_multiprocessing
15526da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        """,
15530870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl    'netbsd3':
15540870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        """
155527e024023cca704e44b0d5f577415d061f89f7d7Florent Xicluna        test_ascii_formatd
15560870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_bsddb
15570870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_bsddb185
15580870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_bsddb3
15590870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_ctypes
15600870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_curses
15610870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_dl
15620e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
15630870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_gdbm
15640870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_locale
15650870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_ossaudiodev
15660870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_pep277
15670870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_tcl
1568bbb7efd72b3360f0523087cff6868f8fa02fe754Guilherme Polo        test_tk
15697a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_guionly
15707a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_textonly
157137040cdace1982772e5f35e4acfa13861d72065dJesse Noller        test_multiprocessing
15720870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        """,
1573f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum}
157432d0c1b458bbdda8d8895f5b5bedfb4644f839e7Martin v. Löwis_expectations['freebsd5'] = _expectations['freebsd4']
1575f64700a512e89c322f46f3cabda3bf58573eda54Hye-Shik Chang_expectations['freebsd6'] = _expectations['freebsd4']
15764e422817eb1bc5a6a42365001ad45683ae07e559Hye-Shik Chang_expectations['freebsd7'] = _expectations['freebsd4']
1577ea684743daa0c198ab327d07832eca48a9578c68Hye-Shik Chang_expectations['freebsd8'] = _expectations['freebsd4']
1578f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum
1579b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Petersclass _ExpectedSkips:
1580b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters    def __init__(self):
15812a182dbf3f2520ad753792068391775d102b13dfTim Peters        import os.path
158255b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz        from test import test_timeout
15831b445d3fcfcc06e5360e83b978efdb9b1c980278Tim Peters
15847c7efe90737d4636633127a95a6cab1a55d57cf4Tim Peters        self.valid = False
1585de14a30d1d70073d36f207fe432e4adad5178399Tim Peters        if sys.platform in _expectations:
1586f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum            s = _expectations[sys.platform]
1587a690a9967e715663b7a421c9ebdad91381cdf1e4Raymond Hettinger            self.expected = set(s.split())
15881b445d3fcfcc06e5360e83b978efdb9b1c980278Tim Peters
1589b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl            # expected to be skipped on every platform, even Linux
1590b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl            self.expected.add('test_linuxaudiodev')
1591b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl
15922a182dbf3f2520ad753792068391775d102b13dfTim Peters            if not os.path.supports_unicode_filenames:
15932a182dbf3f2520ad753792068391775d102b13dfTim Peters                self.expected.add('test_pep277')
15941b445d3fcfcc06e5360e83b978efdb9b1c980278Tim Peters
159555b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz            if test_timeout.skip_expected:
159655b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz                self.expected.add('test_timeout')
159755b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz
1598fba73698240660d9154b6917b87dd333d6fb8284Martin v. Löwis            if sys.maxint == 9223372036854775807L:
1599fba73698240660d9154b6917b87dd333d6fb8284Martin v. Löwis                self.expected.add('test_imageop')
1600fba73698240660d9154b6917b87dd333d6fb8284Martin v. Löwis
16019545a23c7ffb35417d451d24cc3b0339627965a7Ronald Oussoren            if sys.platform != "darwin":
16028f40f0635b9d233cf731c442c2ee21101c0f768cAmaury Forgeot d'Arc                MAC_ONLY = ["test_macos", "test_macostools", "test_aepack",
1603b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                            "test_plistlib", "test_scriptpackages",
1604b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                            "test_applesingle"]
16057035c98c5c0eec7f88fab146e72b2a7e4aaf64a5Neal Norwitz                for skip in MAC_ONLY:
16067035c98c5c0eec7f88fab146e72b2a7e4aaf64a5Neal Norwitz                    self.expected.add(skip)
16076f5a2b52ae769ba1833aef8a52b6ab89921ee8c6Benjamin Peterson            elif len(u'\0'.encode('unicode-internal')) == 4:
16086f5a2b52ae769ba1833aef8a52b6ab89921ee8c6Benjamin Peterson                self.expected.add("test_macostools")
16096f5a2b52ae769ba1833aef8a52b6ab89921ee8c6Benjamin Peterson
1610ecd79eb7dbde19ea2adbf2a912caa5b284b477b9Tim Peters
1611ecd79eb7dbde19ea2adbf2a912caa5b284b477b9Tim Peters            if sys.platform != "win32":
1612b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                # test_sqlite is only reliable on Windows where the library
1613b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                # is distributed with Python
16147035c98c5c0eec7f88fab146e72b2a7e4aaf64a5Neal Norwitz                WIN_ONLY = ["test_unicode_file", "test_winreg",
1615b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                            "test_winsound", "test_startfile",
1616fe2f1ad5b5ac92cc153ff9fdbd88f6058d50a75fRoss Lagerwall                            "test_sqlite", "test_msilib"]
16177035c98c5c0eec7f88fab146e72b2a7e4aaf64a5Neal Norwitz                for skip in WIN_ONLY:
16187035c98c5c0eec7f88fab146e72b2a7e4aaf64a5Neal Norwitz                    self.expected.add(skip)
1619f2715e076435b74638acb81512c2ee014f75aea2Tim Peters
1620dc48b74497b67a449dd622fdaa7d69e7bff65a5eBrett Cannon            if sys.platform != 'irix':
1621b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                IRIX_ONLY = ["test_imageop", "test_al", "test_cd", "test_cl",
1622b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                             "test_gl", "test_imgfile"]
1623dc48b74497b67a449dd622fdaa7d69e7bff65a5eBrett Cannon                for skip in IRIX_ONLY:
1624dc48b74497b67a449dd622fdaa7d69e7bff65a5eBrett Cannon                    self.expected.add(skip)
1625dc48b74497b67a449dd622fdaa7d69e7bff65a5eBrett Cannon
1626b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl            if sys.platform != 'sunos5':
1627b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                self.expected.add('test_sunaudiodev')
1628b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                self.expected.add('test_nis')
1629b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl
1630d290b04ee3645f8dd2e30c329c5ead2db16ece62Steven Bethard            if not sys.py3kwarning:
1631d290b04ee3645f8dd2e30c329c5ead2db16ece62Steven Bethard                self.expected.add('test_py3kwarn')
1632e8e22cf3c0d9e977bc9f13cfc535c026f92bc7aaSteven Bethard
16337c7efe90737d4636633127a95a6cab1a55d57cf4Tim Peters            self.valid = True
1634b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
1635b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters    def isvalid(self):
1636b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        "Return true iff _ExpectedSkips knows about the current platform."
1637b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        return self.valid
1638b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
1639b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters    def getexpected(self):
1640b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        """Return set of test names we expect to skip on current platform.
1641b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
1642b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        self.isvalid() must be true.
1643b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        """
1644b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
1645b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        assert self.isvalid()
1646b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        return self.expected
1647b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
1648152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossumif __name__ == '__main__':
1649928474561c736d87787c99d1c7198ab062318baaFlorent Xicluna    # findtestdir() gets the dirname out of __file__, so we have to make it
1650928474561c736d87787c99d1c7198ab062318baaFlorent Xicluna    # absolute before changing the working directory.
1651928474561c736d87787c99d1c7198ab062318baaFlorent Xicluna    # For example __file__ may be relative when running trace or profile.
1652928474561c736d87787c99d1c7198ab062318baaFlorent Xicluna    # See issue #9323.
1653928474561c736d87787c99d1c7198ab062318baaFlorent Xicluna    __file__ = os.path.abspath(__file__)
1654928474561c736d87787c99d1c7198ab062318baaFlorent Xicluna
1655928474561c736d87787c99d1c7198ab062318baaFlorent Xicluna    # sanity check
16560932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    assert __file__ == os.path.abspath(sys.argv[0])
16570932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna
16580932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    # When tests are run from the Python build directory, it is best practice
16590932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    # to keep the test files in a subfolder.  It eases the cleanup of leftover
16600932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    # files using command "make distclean".
16610932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    if sysconfig.is_python_build():
16621273566cb75e188af7cc38c08df21ba9e07260f4Florent Xicluna        TEMPDIR = os.path.join(sysconfig.get_config_var('srcdir'), 'build')
16631273566cb75e188af7cc38c08df21ba9e07260f4Florent Xicluna        TEMPDIR = os.path.abspath(TEMPDIR)
16641273566cb75e188af7cc38c08df21ba9e07260f4Florent Xicluna        if not os.path.exists(TEMPDIR):
16651273566cb75e188af7cc38c08df21ba9e07260f4Florent Xicluna            os.mkdir(TEMPDIR)
16668f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti
16678f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    # Define a writable temp dir that will be used as cwd while running
16688f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    # the tests. The name of the dir includes the pid to allow parallel
16698f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    # testing (see the -j option).
16708f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    TESTCWD = 'test_python_{}'.format(os.getpid())
16718f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti
16721273566cb75e188af7cc38c08df21ba9e07260f4Florent Xicluna    TESTCWD = os.path.join(TEMPDIR, TESTCWD)
16738f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti
16748f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    # Run the tests in a context manager that temporary changes the CWD to a
16758f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    # temporary and writable directory. If it's not possible to create or
16768f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    # change the CWD, the original CWD will be used. The original CWD is
16778f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    # available from test_support.SAVEDCWD.
16788f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    with test_support.temp_cwd(TESTCWD, quiet=True):
16798f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti        main()
1680