regrtest.py revision 65918e01be239cbf5cec7970ad655da605adf768
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
65a199f0198600996c6a9de3a3d914f0bc2101d244Collin Winter-r randomizes test execution order. You can use --randseed=int to provide a
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
4655796d26794eee634a4a06637d99d8d5c58da2bdbGuido van Rossum    save_modules = sys.modules.keys()
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),
5117f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    dict(huntrleaks=huntrleaks, use_resources=use_resources)
5127f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                )
5137f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                yield (test, args_tuple)
5147f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray        pending = tests_and_args()
515a226c9100d52b3f1cefc084e9ab9b863e10655fdAntoine Pitrou        opt_args = test_support.args_from_interpreter_flags()
516a226c9100d52b3f1cefc084e9ab9b863e10655fdAntoine Pitrou        base_cmd = [sys.executable] + opt_args + ['-m', 'test.regrtest']
5179537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        # required to spawn a new process with PGO flag on/off
5189537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        if pgo:
5199537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon            base_cmd = base_cmd + ['--pgo']
5204698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        def work():
5214698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            # A worker thread.
52214ca327f998511da5e89f819204c54fa582dd696Neal Norwitz            try:
5234698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                while True:
5244698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    try:
5257f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                        test, args_tuple = next(pending)
5267f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    except StopIteration:
527282396f27addf4ae6586865ccf449f60753a106cR. David Murray                        output.put((None, None, None, None))
5284698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                        return
5299537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                    # required to permit running tests with PGO flag on/off
5309537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                    if pgo:
5319537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                        args_tuple[1]['pgo']=pgo
53279c3bd80eda7babde76885973fce157339bbffdaAntoine Pitrou                    # -E is needed by some tests, e.g. test_import
533a226c9100d52b3f1cefc084e9ab9b863e10655fdAntoine Pitrou                    popen = Popen(base_cmd + ['--slaveargs', json.dumps(args_tuple)],
534282396f27addf4ae6586865ccf449f60753a106cR. David Murray                                   stdout=PIPE, stderr=PIPE,
5350dd8e1da286499e4d8bffebabe939b6ca3c1345bAntoine Pitrou                                   universal_newlines=True,
5360dd8e1da286499e4d8bffebabe939b6ca3c1345bAntoine Pitrou                                   close_fds=(os.name != 'nt'))
537282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    stdout, stderr = popen.communicate()
538282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    # Strip last refcount output line if it exists, since it
539282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    # comes from the shutdown of the interpreter in the subcommand.
540282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    stderr = debug_output_pat.sub("", stderr)
541282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    stdout, _, result = stdout.strip().rpartition("\n")
5427f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    if not result:
5437f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                        output.put((None, None, None, None))
5447f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                        return
5454698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    result = json.loads(result)
546282396f27addf4ae6586865ccf449f60753a106cR. David Murray                    output.put((test, stdout.rstrip(), stderr.rstrip(), result))
5474698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            except BaseException:
548282396f27addf4ae6586865ccf449f60753a106cR. David Murray                output.put((None, None, None, None))
54914ca327f998511da5e89f819204c54fa582dd696Neal Norwitz                raise
5504698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        workers = [Thread(target=work) for i in range(use_mp)]
5514698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        for worker in workers:
5524698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            worker.start()
5534698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        finished = 0
55432b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner        test_index = 1
5557f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray        try:
5567f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray            while finished < use_mp:
5577f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                test, stdout, stderr, result = output.get()
5587f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                if test is None:
5597f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    finished += 1
5607f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    continue
5617f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                if stdout:
5627f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    print stdout
5639537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                if stderr and not pgo:
5647f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    print >>sys.stderr, stderr
565cf807c04a36821ff3288dfd16927d28236e60238Antoine Pitrou                sys.stdout.flush()
566cf807c04a36821ff3288dfd16927d28236e60238Antoine Pitrou                sys.stderr.flush()
5677f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                if result[0] == INTERRUPTED:
5687f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    assert result[1] == 'KeyboardInterrupt'
5697f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                    raise KeyboardInterrupt   # What else?
5707f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray                accumulate_result(test, result)
57132b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                if not quiet:
57232b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                    fmt = "[{1:{0}}{2}/{3}] {4}" if bad else "[{1:{0}}{2}] {4}"
57332b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                    print(fmt.format(
57432b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                        test_count_width, test_index, test_count,
57532b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                        len(bad), test))
57632b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                test_index += 1
5777f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray        except KeyboardInterrupt:
57875c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna            interrupted = True
5797f7eea651f558297ac91d376c8dce462eefef0f3R. David Murray            pending.close()
5804698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        for worker in workers:
5814698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            worker.join()
5824698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    else:
58365918e01be239cbf5cec7970ad655da605adf768Brett Cannon        for test_index, test in enumerate(tests, 1):
5844698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            if not quiet:
58532b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                fmt = "[{1:{0}}{2}/{3}] {4}" if bad else "[{1:{0}}{2}] {4}"
58632b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                print(fmt.format(
58732b1ff90abc960f868e00aa1f4a784a423a3c221Victor Stinner                    test_count_width, test_index, test_count, len(bad), test))
5884698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                sys.stdout.flush()
5894698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            if trace:
5904698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                # If we're tracing code coverage, then we don't exit with status
5914698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                # if on a false return value from main.
592561b96f98c8d396fe9e95feb9e02833cdd9025baR. David Murray                tracer.runctx('runtest(test, verbose, quiet)',
5934698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                              globals=globals(), locals=vars())
5943b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw            else:
5954698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                try:
5969537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                    result = runtest(test, verbose, quiet, huntrleaks, None, pgo)
5974045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl                    accumulate_result(test, result)
5984045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl                    if verbose3 and result[0] == FAILED:
5999537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                        if not pgo:
6009537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                            print "Re-running test %r in verbose mode" % test
6019537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                        runtest(test, True, quiet, huntrleaks, None, pgo)
6024698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                except KeyboardInterrupt:
60375c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna                    interrupted = True
6044698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    break
6054698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                except:
6064698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    raise
6074698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            if findleaks:
6084698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                gc.collect()
6094698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                if gc.garbage:
6104698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    print "Warning: test created", len(gc.garbage),
6114698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    print "uncollectable object(s)."
6124698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    # move the uncollectable objects somewhere so we don't see
6134698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    # them again
6144698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    found_garbage.extend(gc.garbage)
6154698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    del gc.garbage[:]
6164698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            # Unload the newly imported modules (best effort finalization)
6174698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            for module in sys.modules.keys():
6184698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                if module not in save_modules and module.startswith("test."):
6194698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou                    test_support.unload(module)
6207a1ea0e88034cdc03d546f0947fc8ecb30f4435fJeremy Hylton
6219537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon    if interrupted and not pgo:
62275c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        # print a newline after ^C
62375c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        print
62475c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        print "Test suite interrupted by signal SIGINT."
62575c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        omitted = set(selected) - set(good) - set(bad) - set(skipped)
62675c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        print count(len(omitted), "test"), "omitted:"
62775c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        printlist(omitted)
6289537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon    if good and not quiet and not pgo:
62975c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna        if not bad and not skipped and not interrupted and len(good) > 1:
63041360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum            print "All",
63141360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        print count(len(good), "test"), "OK."
63236dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin    if print_slow:
63336dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin        test_times.sort(reverse=True)
63436dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin        print "10 slowest tests:"
63536dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin        for time, test in test_times[:10]:
63636dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin            print "%s: %.1fs" % (test, time)
6379537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon    if bad and not pgo:
638ce698f3aa2e535685d0321709de7fa5d3f6de86fZachary Ware        print count(len(bad), "test"), "failed:"
639ce698f3aa2e535685d0321709de7fa5d3f6de86fZachary Ware        printlist(bad)
6409537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon    if environment_changed and not pgo:
641ce698f3aa2e535685d0321709de7fa5d3f6de86fZachary Ware        print "{} altered the execution environment:".format(
642ce698f3aa2e535685d0321709de7fa5d3f6de86fZachary Ware            count(len(environment_changed), "test"))
643ce698f3aa2e535685d0321709de7fa5d3f6de86fZachary Ware        printlist(environment_changed)
6449537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon    if skipped and not quiet and not pgo:
645a45da92484ceececf1cf32b4dfb16004945b001eTim Peters        print count(len(skipped), "test"), "skipped:"
646a45da92484ceececf1cf32b4dfb16004945b001eTim Peters        printlist(skipped)
647e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw
648b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        e = _ExpectedSkips()
649a2be2d624a47e420266606f0cb2a2e030797f650Tim Peters        plat = sys.platform
650b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        if e.isvalid():
651a690a9967e715663b7a421c9ebdad91381cdf1e4Raymond Hettinger            surprise = set(skipped) - e.getexpected() - set(resource_denieds)
652b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters            if surprise:
653b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters                print count(len(surprise), "skip"), \
654a45da92484ceececf1cf32b4dfb16004945b001eTim Peters                      "unexpected on", plat + ":"
655a45da92484ceececf1cf32b4dfb16004945b001eTim Peters                printlist(surprise)
656b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters            else:
657b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters                print "Those skips are all expected on", plat + "."
658b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        else:
659b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters            print "Ask someone to teach regrtest.py about which tests are"
660b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters            print "expected to get skipped on", plat + "."
661b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
66204824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis    if verbose2 and bad:
66304824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis        print "Re-running failed tests in verbose mode"
664a4777f1befa51c4f9c192c7390294e67667a8190Zachary Ware        for test in bad[:]:
665922dd7d49da75b07e27f917db0166f67bdbef3d3Tim Peters            print "Re-running test %r in verbose mode" % test
666922dd7d49da75b07e27f917db0166f67bdbef3d3Tim Peters            sys.stdout.flush()
66704824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis            try:
6685fe5cf6df20607033096c05d68cef7222a35697cNeal Norwitz                test_support.verbose = True
6699537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                ok = runtest(test, True, quiet, huntrleaks, None, pgo)
67004824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis            except KeyboardInterrupt:
67104824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis                # print a newline separate from the ^C
67204824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis                print
67304824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis                break
674a4777f1befa51c4f9c192c7390294e67667a8190Zachary Ware            else:
675a4777f1befa51c4f9c192c7390294e67667a8190Zachary Ware                if ok[0] in {PASSED, ENV_CHANGED, SKIPPED, RESOURCE_DENIED}:
676a4777f1befa51c4f9c192c7390294e67667a8190Zachary Ware                    bad.remove(test)
677a4777f1befa51c4f9c192c7390294e67667a8190Zachary Ware        else:
678a4777f1befa51c4f9c192c7390294e67667a8190Zachary Ware            if bad:
679a4777f1befa51c4f9c192c7390294e67667a8190Zachary Ware                print count(len(bad), "test"), "failed again:"
680a4777f1befa51c4f9c192c7390294e67667a8190Zachary Ware                printlist(bad)
68104824ce8ed3e4e936487758ca6672b6dec75a8a7Martin v. Löwis
682e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw    if single:
68398e3df38fd863af8f399739e461f84058e7bcfe0R. David Murray        if next_single_test:
68498e3df38fd863af8f399739e461f84058e7bcfe0R. David Murray            with open(filename, 'w') as fp:
68598e3df38fd863af8f399739e461f84058e7bcfe0R. David Murray                fp.write(next_single_test + '\n')
686e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw        else:
687e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw            os.unlink(filename)
688e11e3dee3e4f467d51c9d36e24b0b09e64eab295Barry Warsaw
6893b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw    if trace:
6903b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw        r = tracer.results()
6913b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw        r.write_results(show_missing=True, summary=True, coverdir=coverdir)
6923b6d025d9bc6a0109e9a2ebd28a4864e8007193aBarry Warsaw
6930179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaro    if runleaks:
6940179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaro        os.system("leaks %d" % os.getpid())
6950179a18034b2316a70655d0f05bfbb20a0881f17Skip Montanaro
69675c6676916398a61201cddcf323529cca2eb0e49Florent Xicluna    sys.exit(len(bad) > 0 or interrupted)
69708fca5212528e073600c27c70a34eeef445f393bBarry Warsaw
698152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
6996fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van RossumSTDTESTS = [
700152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_grammar',
701152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_opcodes',
702f567ca3e1ae851ea6e7418da3df47e034841a08dCollin Winter    'test_dict',
703152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_builtin',
704152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_exceptions',
705152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_types',
7065545314ba7d14abda0a5f6bc50cbef122167f205Collin Winter    'test_unittest',
7075545314ba7d14abda0a5f6bc50cbef122167f205Collin Winter    'test_doctest',
7085545314ba7d14abda0a5f6bc50cbef122167f205Collin Winter    'test_doctest2',
70912d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna]
710152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
71112d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent XiclunaNOTTESTS = {
712152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    'test_support',
71362e2c7e3dfffd8465a54b99fc6d3c2a60acab350Jeremy Hylton    'test_future1',
71462e2c7e3dfffd8465a54b99fc6d3c2a60acab350Jeremy Hylton    'test_future2',
71512d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna}
716152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
7176fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossumdef findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
718152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    """Return a list of all applicable test modules."""
7190932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    testdir = findtestdir(testdir)
720152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    names = os.listdir(testdir)
721152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    tests = []
72212d750d6b8f4e25cd32277a954e787fc8ba10ad5Florent Xicluna    others = set(stdtests) | nottests
723152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    for name in names:
7240932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna        modname, ext = os.path.splitext(name)
7250932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna        if modname[:5] == "test_" and ext == ".py" and modname not in others:
7260932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna            tests.append(modname)
7270932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    return stdtests + sorted(tests)
728152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
7294698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitroudef runtest(test, verbose, quiet,
7309537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon            huntrleaks=False, use_resources=None, pgo=False):
7316fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    """Run a single test.
732e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
7336fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    test -- the name of the test
7346fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    verbose -- if true, print more messages
735f29f47b38beae54db959d0dd2f0800d5dd3fc174Trent Mick    quiet -- if true, don't print 'skipped' messages (probably redundant)
73636dbcb9e98ef9f3fdc731f42670b7dfd1154c6d8Jeffrey Yasskin    test_times -- a list of (time, test_name) pairs
737e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    huntrleaks -- run multiple times to test for leaks; requires a debug
738e55848695347c67effeb08aedcf2ce3d256524cdTim Peters                  build; a triple corresponding to -R's three arguments
73965918e01be239cbf5cec7970ad655da605adf768Brett Cannon    pgo -- if true, do not print unnecessary info when running the test
7409537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon           for Profile Guided Optimization build
74165918e01be239cbf5cec7970ad655da605adf768Brett Cannon
7424045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl    Returns one of the test result constants:
7434045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        INTERRUPTED      KeyboardInterrupt when run under -j
7444045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        RESOURCE_DENIED  test skipped because resource denied
7454045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        SKIPPED          test skipped for some other reason
7464045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        ENV_CHANGED      test failed because it changed the execution environment
7474045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        FAILED           test failed
7484045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        PASSED           test passed
7496fd83b7b38d0b2a8c9ff5e5b553a1ea6f7306ef4Guido van Rossum    """
75006c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
7514698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    test_support.verbose = verbose  # Tell tests to be moderately quiet
7524698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    if use_resources is not None:
7534698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        test_support.use_resources = use_resources
754e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    try:
7559537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        return runtest_inner(test, verbose, quiet, huntrleaks, pgo)
756e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    finally:
757e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        cleanup_test_droppings(test, verbose)
758e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
75914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
76014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# Unit tests are supposed to leave the execution environment unchanged
76114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# once they complete.  But sometimes tests have bugs, especially when
76214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# tests fail, and the changes to environment go on to mess up other
76314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# tests.  This can cause issues with buildbot stability, since tests
76414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# are run in random order and so problems may appear to come and go.
76514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# There are a few things we can save and restore to mitigate this, and
76614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray# the following context manager handles this task.
76714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
76814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murrayclass saved_test_environment:
76914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    """Save bits of the test environment and restore them at block exit.
77014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
7712ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan        with saved_test_environment(testname, verbose, quiet):
77214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray            #stuff
77314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
77414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    Unless quiet is True, a warning is printed to stderr if any of
77514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    the saved items was changed by the test.  The attribute 'changed'
77614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    is initially False, but is set to True if a change is detected.
7772ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan
7782ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    If verbose is more than 1, the before and after state of changed
7792ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    items is also printed.
78014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    """
78114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
78214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    changed = False
78314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
7849537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon    def __init__(self, testname, verbose=0, quiet=False, pgo=False):
78514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        self.testname = testname
7862ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan        self.verbose = verbose
78714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        self.quiet = quiet
7889537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        self.pgo = pgo
78914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
79014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    # To add things to save and restore, add a name XXX to the resources list
79114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    # and add corresponding get_XXX/restore_XXX functions.  get_XXX should
79214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    # return the value to be saved and compared against a second call to the
79314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    # get function when test execution completes.  restore_XXX should accept
79414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    # the saved value and restore the resource using it.  It will be called if
7952ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    # and only if a change in the value is detected.
7962ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    #
7972ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    # Note: XXX will have any '.' replaced with '_' characters when determining
7982ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    # the corresponding method names.
79914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
8002ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    resources = ('sys.argv', 'cwd', 'sys.stdin', 'sys.stdout', 'sys.stderr',
801da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip                 'os.environ', 'sys.path', 'asyncore.socket_map',
802c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka                 'files',
803da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip                )
80414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
80514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_sys_argv(self):
8068157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        return id(sys.argv), sys.argv, sys.argv[:]
80714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_sys_argv(self, saved_argv):
8088157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        sys.argv = saved_argv[1]
8098157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        sys.argv[:] = saved_argv[2]
81014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
81114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_cwd(self):
81214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return os.getcwd()
81314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_cwd(self, saved_cwd):
81414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        os.chdir(saved_cwd)
81514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
81614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_sys_stdout(self):
81714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return sys.stdout
81814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_sys_stdout(self, saved_stdout):
81914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        sys.stdout = saved_stdout
82014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
82114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_sys_stderr(self):
82214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return sys.stderr
82314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_sys_stderr(self, saved_stderr):
82414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        sys.stderr = saved_stderr
82514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
82614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_sys_stdin(self):
82714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return sys.stdin
82814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_sys_stdin(self, saved_stdin):
82914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        sys.stdin = saved_stdin
83014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
83114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_os_environ(self):
8328157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        return id(os.environ), os.environ, dict(os.environ)
83314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_os_environ(self, saved_environ):
8348157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        os.environ = saved_environ[1]
83514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        os.environ.clear()
8368157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        os.environ.update(saved_environ[2])
83714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
83814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def get_sys_path(self):
8398157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        return id(sys.path), sys.path, sys.path[:]
84014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def restore_sys_path(self, saved_path):
8418157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        sys.path = saved_path[1]
8428157e19e8fe311e8f18542750125a83cc97943f4Nick Coghlan        sys.path[:] = saved_path[2]
84314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
8441e8a75414ff57ff8fa42d59d5717be9639c55b12Antoine Pitrou    def get_asyncore_socket_map(self):
8451e8a75414ff57ff8fa42d59d5717be9639c55b12Antoine Pitrou        asyncore = sys.modules.get('asyncore')
846a71bfc46d08bfc63fcc3322f24beb2c7faa37efaAntoine Pitrou        # XXX Making a copy keeps objects alive until __exit__ gets called.
847a71bfc46d08bfc63fcc3322f24beb2c7faa37efaAntoine Pitrou        return asyncore and asyncore.socket_map.copy() or {}
8481e8a75414ff57ff8fa42d59d5717be9639c55b12Antoine Pitrou    def restore_asyncore_socket_map(self, saved_map):
8491e8a75414ff57ff8fa42d59d5717be9639c55b12Antoine Pitrou        asyncore = sys.modules.get('asyncore')
8501e8a75414ff57ff8fa42d59d5717be9639c55b12Antoine Pitrou        if asyncore is not None:
851a71bfc46d08bfc63fcc3322f24beb2c7faa37efaAntoine Pitrou            asyncore.close_all(ignore_all=True)
8521e8a75414ff57ff8fa42d59d5717be9639c55b12Antoine Pitrou            asyncore.socket_map.update(saved_map)
8531e8a75414ff57ff8fa42d59d5717be9639c55b12Antoine Pitrou
854da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip    def get_test_support_TESTFN(self):
855da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip        if os.path.isfile(test_support.TESTFN):
856da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip            result = 'f'
857da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip        elif os.path.isdir(test_support.TESTFN):
858da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip            result = 'd'
859da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip        else:
860da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip            result = None
861da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip        return result
862da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip    def restore_test_support_TESTFN(self, saved_value):
863da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip        if saved_value is None:
864da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip            if os.path.isfile(test_support.TESTFN):
865da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip                os.unlink(test_support.TESTFN)
866da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip            elif os.path.isdir(test_support.TESTFN):
867da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip                shutil.rmtree(test_support.TESTFN)
868da563bfa4800688d0a2a771b8335a184738c8df2Vinay Sajip
869c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka    def get_files(self):
870c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka        return sorted(fn + ('/' if os.path.isdir(fn) else '')
871c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka                      for fn in os.listdir(os.curdir))
872c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka    def restore_files(self, saved_value):
873ac0d1893b93542b7fd80134bc51b867e44108262Victor Stinner        fn = test_support.TESTFN
874c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka        if fn not in saved_value and (fn + '/') not in saved_value:
875c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka            if os.path.isfile(fn):
876ac0d1893b93542b7fd80134bc51b867e44108262Victor Stinner                test_support.unlink(fn)
877c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka            elif os.path.isdir(fn):
878ac0d1893b93542b7fd80134bc51b867e44108262Victor Stinner                test_support.rmtree(fn)
879c0a2f2b0084460dc94e473ca0d641029fa7aa448Serhiy Storchaka
8802ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan    def resource_info(self):
8812ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan        for name in self.resources:
8822ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            method_suffix = name.replace('.', '_')
8832ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            get_name = 'get_' + method_suffix
8842ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            restore_name = 'restore_' + method_suffix
8852ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            yield name, getattr(self, get_name), getattr(self, restore_name)
8862ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan
88714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def __enter__(self):
8882ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan        self.saved_values = dict((name, get()) for name, get, restore
8892ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                                                   in self.resource_info())
89014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return self
89114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
89214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray    def __exit__(self, exc_type, exc_val, exc_tb):
893a71bfc46d08bfc63fcc3322f24beb2c7faa37efaAntoine Pitrou        saved_values = self.saved_values
894a71bfc46d08bfc63fcc3322f24beb2c7faa37efaAntoine Pitrou        del self.saved_values
8952ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan        for name, get, restore in self.resource_info():
8962ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            current = get()
897a71bfc46d08bfc63fcc3322f24beb2c7faa37efaAntoine Pitrou            original = saved_values.pop(name)
8982ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            # Check for changes to the resource's value
8992ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            if current != original:
90014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                self.changed = True
9012ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                restore(original)
9029537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                if not self.quiet and not self.pgo:
9032ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                    print >>sys.stderr, (
9042ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                          "Warning -- {} was modified by {}".format(
9052ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                                                 name, self.testname))
9069537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon                    if self.verbose > 1 and not self.pgo:
9072ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan                        print >>sys.stderr, (
908a3e97ad5bac9439f82c86a355775238655b5be87Nick Coghlan                              "  Before: {}\n  After:  {} ".format(
909a3e97ad5bac9439f82c86a355775238655b5be87Nick Coghlan                                                  original, current))
9102ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            # XXX (ncoghlan): for most resources (e.g. sys.path) identity
9112ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            # matters at least as much as value. For others (e.g. cwd),
9122ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            # identity is irrelevant. Should we add a mechanism to check
9132ee358eed358748496dcdc7ea3fe3f66e6e93531Nick Coghlan            # for substitution in the cases where it matters?
91414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        return False
91514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
91614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray
9179537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannondef runtest_inner(test, verbose, quiet, huntrleaks=False, pgo=False):
918152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    test_support.unload(test)
9199390cc15da5cd6920bd41bb4cd146d5d0601345fTim Peters    if verbose:
920ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        capture_stdout = None
9210fcca4e815e3dbb28c73108376079a94ad6ee8deGuido van Rossum    else:
92291a2c89288125b9509ce3fa8436f49a029031be4Michael Foord        capture_stdout = StringIO.StringIO()
92306c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
9244698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou    test_time = 0.0
9250f489743ef18ab2da39695d945c79b7a24df8554Collin Winter    refleak = False  # True if the test leaked references.
926152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    try:
927342ca75d9552ff5c606c465d1392a32e44257fe5Tim Peters        save_stdout = sys.stdout
92841360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        try:
929ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc            if capture_stdout:
930ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc                sys.stdout = capture_stdout
931408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw            if test.startswith('test.'):
932408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw                abstest = test
933408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw            else:
934408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw                # Always import it from the test package
935408b6d34de2b1a6ba690557def435adce9314184Barry Warsaw                abstest = 'test.' + test
9369537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon            with saved_test_environment(test, verbose, quiet, pgo) as environment:
93714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                start_time = time.time()
93814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                the_package = __import__(abstest, globals(), locals(), [])
93914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                the_module = getattr(the_package, test)
94014dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                # Old tests run to completion simply as a side-effect of
94114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                # being imported.  For tests based on unittest or doctest,
94214dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                # explicitly invoke their test_main() function (if it exists).
94314dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                indirect_test = getattr(the_module, "test_main", None)
94414dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                if indirect_test is not None:
94514dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                    indirect_test()
94614dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                if huntrleaks:
94714dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                    refleak = dash_R(the_module, test, indirect_test,
94814dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                        huntrleaks)
94914dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray                test_time = time.time() - start_time
95041360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        finally:
951342ca75d9552ff5c606c465d1392a32e44257fe5Tim Peters            sys.stdout = save_stdout
9529a0db07c2ffd4e4b3ae75d5820dc6b4152b3582bFred Drake    except test_support.ResourceDenied, msg:
9539537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        if not quiet and not pgo:
9549a0db07c2ffd4e4b3ae75d5820dc6b4152b3582bFred Drake            print test, "skipped --", msg
9559a0db07c2ffd4e4b3ae75d5820dc6b4152b3582bFred Drake            sys.stdout.flush()
9564045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        return RESOURCE_DENIED, test_time
957612f1d5f48d609b4a1b0afd4d64499ff4e5f629dR. David Murray    except unittest.SkipTest, msg:
9589537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        if not quiet and not pgo:
959de4742b87f7775fc3d9fa76cd640e7cdd5ef69a2Fred Drake            print test, "skipped --", msg
9603cda93ebf60e501350f42fdab72c18eab54718fcGuido van Rossum            sys.stdout.flush()
9614045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        return SKIPPED, test_time
962fe5c22a85e6740012143cdbdc384bd2ebc788c27Fred Drake    except KeyboardInterrupt:
963fe5c22a85e6740012143cdbdc384bd2ebc788c27Fred Drake        raise
964152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    except test_support.TestFailed, msg:
9659537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        if not pgo:
9669537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon            print >>sys.stderr, "test", test, "failed --", msg
9670960bc586ee5a2a53c5c8c92bd17f03d4d75efefR. David Murray        sys.stderr.flush()
9684045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        return FAILED, test_time
9699e48b272b96aabf597b7aedd358ab890ddbf4c98Guido van Rossum    except:
97041360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        type, value = sys.exc_info()[:2]
9719537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        if not pgo:
9729537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon            print >>sys.stderr, "test", test, "crashed --", str(type) + ":", value
9730960bc586ee5a2a53c5c8c92bd17f03d4d75efefR. David Murray        sys.stderr.flush()
9749537478463ee23b1dbb5f6c95fc6217d7652f88eBrett Cannon        if verbose and not pgo:
9750960bc586ee5a2a53c5c8c92bd17f03d4d75efefR. David Murray            traceback.print_exc(file=sys.stderr)
9760960bc586ee5a2a53c5c8c92bd17f03d4d75efefR. David Murray            sys.stderr.flush()
9774045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        return FAILED, test_time
978152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    else:
9790f489743ef18ab2da39695d945c79b7a24df8554Collin Winter        if refleak:
9804045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl            return FAILED, test_time
98114dcd43d0bf34049bc28f77bc30df5057c34b682R. David Murray        if environment.changed:
98293c21714946d5fa287bb4aa1d9acb46d55f0b742Georg Brandl            return ENV_CHANGED, test_time
983ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        # Except in verbose mode, tests should not print anything
984ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        if verbose or huntrleaks:
9854045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl            return PASSED, test_time
986ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        output = capture_stdout.getvalue()
987ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        if not output:
9884045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl            return PASSED, test_time
9890fcca4e815e3dbb28c73108376079a94ad6ee8deGuido van Rossum        print "test", test, "produced unexpected output:"
990ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        print "*" * 70
991ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        print output
992ea13dc629caed2dd44b1ba5b611e1ba2ac1fd96fAmaury Forgeot d'Arc        print "*" * 70
9933cda93ebf60e501350f42fdab72c18eab54718fcGuido van Rossum        sys.stdout.flush()
9944045e88b8b43b2748e7be7ed3f0cae932e612b73Georg Brandl        return FAILED, test_time
9950fcca4e815e3dbb28c73108376079a94ad6ee8deGuido van Rossum
996e55848695347c67effeb08aedcf2ce3d256524cdTim Petersdef cleanup_test_droppings(testname, verbose):
997a9b4d4777a4018e7fcaa92b6e587992819501b5eBenjamin Peterson    import stat
9980d75957d91167bb4363a9db7d0d923376a73f678Antoine Pitrou    import gc
9990d75957d91167bb4363a9db7d0d923376a73f678Antoine Pitrou
10000d75957d91167bb4363a9db7d0d923376a73f678Antoine Pitrou    # First kill any dangling references to open files etc.
10010d75957d91167bb4363a9db7d0d923376a73f678Antoine Pitrou    gc.collect()
1002e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
1003e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # Try to clean up junk commonly left behind.  While tests shouldn't leave
1004e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # any files or directories behind, when a test fails that can be tedious
1005e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # for it to arrange.  The consequences can be especially nasty on Windows,
1006e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # since if a test leaves a file open, it cannot be deleted by name (while
1007e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # there's nothing we can do about that here either, we can display the
1008e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    # name of the offending test, which is a real help).
1009e55848695347c67effeb08aedcf2ce3d256524cdTim Peters    for name in (test_support.TESTFN,
1010e55848695347c67effeb08aedcf2ce3d256524cdTim Peters                 "db_home",
1011e55848695347c67effeb08aedcf2ce3d256524cdTim Peters                ):
1012e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        if not os.path.exists(name):
1013e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            continue
1014e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
1015e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        if os.path.isdir(name):
1016e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            kind, nuker = "directory", shutil.rmtree
1017e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        elif os.path.isfile(name):
1018e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            kind, nuker = "file", os.unlink
1019e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        else:
1020e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            raise SystemError("os.path says %r exists but is neither "
1021e55848695347c67effeb08aedcf2ce3d256524cdTim Peters                              "directory nor file" % name)
1022e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
1023e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        if verbose:
1024e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            print "%r left behind %s %r" % (testname, kind, name)
1025e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        try:
1026a9b4d4777a4018e7fcaa92b6e587992819501b5eBenjamin Peterson            # if we have chmod, fix possible permissions problems
1027a9b4d4777a4018e7fcaa92b6e587992819501b5eBenjamin Peterson            # that might prevent cleanup
1028a9b4d4777a4018e7fcaa92b6e587992819501b5eBenjamin Peterson            if (hasattr(os, 'chmod')):
1029a9b4d4777a4018e7fcaa92b6e587992819501b5eBenjamin Peterson                os.chmod(name, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
1030e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            nuker(name)
1031e55848695347c67effeb08aedcf2ce3d256524cdTim Peters        except Exception, msg:
1032e55848695347c67effeb08aedcf2ce3d256524cdTim Peters            print >> sys.stderr, ("%r left behind %s %r and it couldn't be "
1033e55848695347c67effeb08aedcf2ce3d256524cdTim Peters                "removed: %s" % (testname, kind, name, msg))
1034e55848695347c67effeb08aedcf2ce3d256524cdTim Peters
103506c5c008193164f04ebcc14ca5176080cf303c54Tim Petersdef dash_R(the_module, test, indirect_test, huntrleaks):
10360f489743ef18ab2da39695d945c79b7a24df8554Collin Winter    """Run a test multiple times, looking for reference leaks.
10370f489743ef18ab2da39695d945c79b7a24df8554Collin Winter
10380f489743ef18ab2da39695d945c79b7a24df8554Collin Winter    Returns:
10390f489743ef18ab2da39695d945c79b7a24df8554Collin Winter        False if the test didn't leak references; True if we detected refleaks.
10400f489743ef18ab2da39695d945c79b7a24df8554Collin Winter    """
104106c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    # This code is hackish and inelegant, but it seems to do the job.
1042e55df1fa2a9be432c5c22d7eec8b395227fa4405Amaury Forgeot d'Arc    import copy_reg, _abcoll, _pyio
104306c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
104406c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    if not hasattr(sys, 'gettotalrefcount'):
104506c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        raise Exception("Tracking reference leaks requires a debug build "
104606c5c008193164f04ebcc14ca5176080cf303c54Tim Peters                        "of Python")
104706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
104806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    # Save current values for dash_R_cleanup() to restore.
104906c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    fs = warnings.filters[:]
1050dffbf5f5421cbeb20237280c0bd70f989269f844Georg Brandl    ps = copy_reg.dispatch_table.copy()
105106c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    pic = sys.path_importer_cache.copy()
10524976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan    try:
10534976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan        import zipimport
10544976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan    except ImportError:
10555503579db68f22e0ddca030f2aa5cd48373e08c0Benjamin Peterson        zdc = None # Run unmodified on platforms without zipimport support
10564976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan    else:
10574976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan        zdc = zipimport._zip_directory_cache.copy()
105864c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum    abcs = {}
1059e55df1fa2a9be432c5c22d7eec8b395227fa4405Amaury Forgeot d'Arc    modules = _abcoll, _pyio
10604d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc    for abc in [getattr(mod, a) for mod in modules for a in mod.__all__]:
10614d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc        # XXX isinstance(abc, ABCMeta) leads to infinite recursion
10624d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc        if not hasattr(abc, '_abc_registry'):
10636b29dd05c8fff4fc69ab4d68baef86fb7e91548cChristian Heimes            continue
106464c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum        for obj in abc.__subclasses__() + [abc]:
106564c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum            abcs[obj] = obj._abc_registry.copy()
106664c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum
106706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    if indirect_test:
106806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        def run_the_test():
106906c5c008193164f04ebcc14ca5176080cf303c54Tim Peters            indirect_test()
107006c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    else:
107106c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        def run_the_test():
1072dde5b94875460e704099593766f4a5bc3279483dEzio Melotti            imp.reload(the_module)
107306c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
107406c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    deltas = []
107506c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    nwarmup, ntracked, fname = huntrleaks
10768f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    fname = os.path.join(test_support.SAVEDCWD, fname)
107706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    repcount = nwarmup + ntracked
107806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    print >> sys.stderr, "beginning", repcount, "repetitions"
107906c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    print >> sys.stderr, ("1234567890"*(repcount//10 + 1))[:repcount]
10805503579db68f22e0ddca030f2aa5cd48373e08c0Benjamin Peterson    dash_R_cleanup(fs, ps, pic, zdc, abcs)
108106c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    for i in range(repcount):
1082ac1d93167ce2e98a7cf6896526b3f961463962c6Collin Winter        rc_before = sys.gettotalrefcount()
108306c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        run_the_test()
108406c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        sys.stderr.write('.')
10855503579db68f22e0ddca030f2aa5cd48373e08c0Benjamin Peterson        dash_R_cleanup(fs, ps, pic, zdc, abcs)
1086ac1d93167ce2e98a7cf6896526b3f961463962c6Collin Winter        rc_after = sys.gettotalrefcount()
108706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters        if i >= nwarmup:
1088ac1d93167ce2e98a7cf6896526b3f961463962c6Collin Winter            deltas.append(rc_after - rc_before)
108906c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    print >> sys.stderr
109006c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    if any(deltas):
1091d9841039dbd0e37746d46f6e46ce1d82b85a21b3Neal Norwitz        msg = '%s leaked %s references, sum=%s' % (test, deltas, sum(deltas))
1092d9841039dbd0e37746d46f6e46ce1d82b85a21b3Neal Norwitz        print >> sys.stderr, msg
10934698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou        with open(fname, "a") as refrep:
10944698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            print >> refrep, msg
10954698d9928ef1547e86a3f692e0c8d77e3f5e869cAntoine Pitrou            refrep.flush()
10960f489743ef18ab2da39695d945c79b7a24df8554Collin Winter        return True
10970f489743ef18ab2da39695d945c79b7a24df8554Collin Winter    return False
109806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
10995503579db68f22e0ddca030f2aa5cd48373e08c0Benjamin Petersondef dash_R_cleanup(fs, ps, pic, zdc, abcs):
1100dffbf5f5421cbeb20237280c0bd70f989269f844Georg Brandl    import gc, copy_reg
11010aa6e1b8fb8c3ae656518a7e319a5f697158e7bfBrett Cannon    import _strptime, linecache
11020aa6e1b8fb8c3ae656518a7e319a5f697158e7bfBrett Cannon    dircache = test_support.import_module('dircache', deprecated=True)
110306c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    import urlparse, urllib, urllib2, mimetypes, doctest
11044d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc    import struct, filecmp
110506c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    from distutils.dir_util import _path_created
110606c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
1107607bff1ebe81e869697e228322da4c308e8753a5Amaury Forgeot d'Arc    # Clear the warnings registry, so they can be displayed again
1108607bff1ebe81e869697e228322da4c308e8753a5Amaury Forgeot d'Arc    for mod in sys.modules.values():
1109607bff1ebe81e869697e228322da4c308e8753a5Amaury Forgeot d'Arc        if hasattr(mod, '__warningregistry__'):
1110607bff1ebe81e869697e228322da4c308e8753a5Amaury Forgeot d'Arc            del mod.__warningregistry__
1111607bff1ebe81e869697e228322da4c308e8753a5Amaury Forgeot d'Arc
111206c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    # Restore some original values.
111306c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    warnings.filters[:] = fs
1114dffbf5f5421cbeb20237280c0bd70f989269f844Georg Brandl    copy_reg.dispatch_table.clear()
1115dffbf5f5421cbeb20237280c0bd70f989269f844Georg Brandl    copy_reg.dispatch_table.update(ps)
111606c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    sys.path_importer_cache.clear()
111706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    sys.path_importer_cache.update(pic)
11184976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan    try:
11194976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan        import zipimport
11204976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan    except ImportError:
11214976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan        pass # Run unmodified on platforms without zipimport support
11224976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan    else:
11234976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan        zipimport._zip_directory_cache.clear()
11244976e3287a9696fb7b99ca8d757e3af1fdb4f763Nick Coghlan        zipimport._zip_directory_cache.update(zdc)
112506c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
1126908caac52e8e62baa1ee54e4e650e1cd3ac37907Christian Heimes    # clear type cache
1127422051a3675271e179995ad4a0f056ff94395d55Christian Heimes    sys._clear_type_cache()
1128908caac52e8e62baa1ee54e4e650e1cd3ac37907Christian Heimes
112964c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum    # Clear ABC registries, restoring previously saved ABC registries.
11304d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc    for abc, registry in abcs.items():
11314d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc        abc._abc_registry = registry.copy()
11324d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc        abc._abc_cache.clear()
11334d0c1170ef1eb0d0acbbcc6da0fa5a7c6cba4a10Amaury Forgeot d'Arc        abc._abc_negative_cache.clear()
113464c06e327d48150fc548cf18a4a7ae0b890e69faGuido van Rossum
113506c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    # Clear assorted module caches.
113606c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    _path_created.clear()
113706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    re.purge()
113806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    _strptime._regex_cache.clear()
113906c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    urlparse.clear_cache()
114006c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    urllib.urlcleanup()
114106c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    urllib2.install_opener(None)
114206c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    dircache.reset()
114306c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    linecache.clearcache()
114406c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    mimetypes._default_mime_types()
1145c27d655c00ec08f44abea646ae06456cbcca84b6Tim Peters    filecmp._cache.clear()
114676d19f68e4c824bbcba890d924f20a6133ee0326Christian Heimes    struct._clearcache()
114706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    doctest.master = None
11480e30317891c41ada25d5459644484aef0bc8e88cMeador Inge    try:
11490e30317891c41ada25d5459644484aef0bc8e88cMeador Inge        import ctypes
11500e30317891c41ada25d5459644484aef0bc8e88cMeador Inge    except ImportError:
11510e30317891c41ada25d5459644484aef0bc8e88cMeador Inge        # Don't worry about resetting the cache if ctypes is not supported
11520e30317891c41ada25d5459644484aef0bc8e88cMeador Inge        pass
11530e30317891c41ada25d5459644484aef0bc8e88cMeador Inge    else:
11540e30317891c41ada25d5459644484aef0bc8e88cMeador Inge        ctypes._reset_cache()
115506c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
115606c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    # Collect cyclic trash.
115706c5c008193164f04ebcc14ca5176080cf303c54Tim Peters    gc.collect()
115806c5c008193164f04ebcc14ca5176080cf303c54Tim Peters
11590932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xiclunadef findtestdir(path=None):
11600932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    return path or os.path.dirname(__file__) or os.curdir
1161152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
11620932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xiclunadef removepy(names):
11630932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    if not names:
11640932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna        return
11650932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    for idx, name in enumerate(names):
11660932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna        basename, ext = os.path.splitext(name)
11670932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna        if ext == '.py':
11680932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna            names[idx] = basename
1169c5000dfc4098f8547461e790a91536a923124261Tim Peters
1170152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossumdef count(n, word):
1171152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    if n == 1:
117241360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        return "%d %s" % (n, word)
1173152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum    else:
117441360a4696f488e49e5409b3b1baf1fff6ae0044Guido van Rossum        return "%d %ss" % (n, word)
1175152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossum
1176a45da92484ceececf1cf32b4dfb16004945b001eTim Petersdef printlist(x, width=70, indent=4):
11777c7efe90737d4636633127a95a6cab1a55d57cf4Tim Peters    """Print the elements of iterable x to stdout.
1178a45da92484ceececf1cf32b4dfb16004945b001eTim Peters
1179a45da92484ceececf1cf32b4dfb16004945b001eTim Peters    Optional arg width (default 70) is the maximum line length.
1180a45da92484ceececf1cf32b4dfb16004945b001eTim Peters    Optional arg indent (default 4) is the number of blanks with which to
1181a45da92484ceececf1cf32b4dfb16004945b001eTim Peters    begin each line.
1182a45da92484ceececf1cf32b4dfb16004945b001eTim Peters    """
1183a45da92484ceececf1cf32b4dfb16004945b001eTim Peters
1184ba78bc4a3216b51398bab59157572a51c38b9ef4Tim Peters    from textwrap import fill
1185ba78bc4a3216b51398bab59157572a51c38b9ef4Tim Peters    blanks = ' ' * indent
1186355adc5a451ce3b22b66a3af883d35e89e2a0eabFlorent Xicluna    # Print the sorted list: 'x' may be a '--random' list or a set()
1187355adc5a451ce3b22b66a3af883d35e89e2a0eabFlorent Xicluna    print fill(' '.join(str(elt) for elt in sorted(x)), width,
1188ba78bc4a3216b51398bab59157572a51c38b9ef4Tim Peters               initial_indent=blanks, subsequent_indent=blanks)
1189a45da92484ceececf1cf32b4dfb16004945b001eTim Peters
1190de14a30d1d70073d36f207fe432e4adad5178399Tim Peters# Map sys.platform to a string containing the basenames of tests
1191de14a30d1d70073d36f207fe432e4adad5178399Tim Peters# expected to be skipped on that platform.
11922a182dbf3f2520ad753792068391775d102b13dfTim Peters#
11932a182dbf3f2520ad753792068391775d102b13dfTim Peters# Special cases:
11942a182dbf3f2520ad753792068391775d102b13dfTim Peters#     test_pep277
11952a182dbf3f2520ad753792068391775d102b13dfTim Peters#         The _ExpectedSkips constructor adds this to the set of expected
11962a182dbf3f2520ad753792068391775d102b13dfTim Peters#         skips if not os.path.supports_unicode_filenames.
119755b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz#     test_timeout
119855b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz#         Controlled by test_timeout.skip_expected.  Requires the network
119955b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz#         resource and a socket module.
1200b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl#
1201b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl# Tests that are expected to be skipped everywhere except on one platform
1202b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl# are also handled separately.
1203de14a30d1d70073d36f207fe432e4adad5178399Tim Peters
1204f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum_expectations = {
1205f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum    'win32':
1206f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        """
1207c7c516aa5140d255f18ffd2e8569434f0904f5dbTim Peters        test__locale
1208823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
120978e35f931128d017d5955841eac8c397ff32595cTim Peters        test_bsddb3
1210f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_commands
1211f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_crypt
1212d703057752d52cef6082a920879a0b62cdeffacaTim Peters        test_curses
1213f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_dbm
1214f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_dl
1215f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_fcntl
1216f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_fork1
12170e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1218f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_gdbm
1219f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_grp
1220fd8e6e599051de07cdb8e9abc9dbadf37c5fca0cTim Peters        test_ioctl
1221f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_largefile
12220e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1223f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_mhlib
1224f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_openpty
1225efc4b121694888e00072f5a079de5496afecb05aTim Peters        test_ossaudiodev
122627d9ee32abac7a580b1ffc980522535bdd20f09bGeorg Brandl        test_pipes
1227f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_poll
1228003eb3088283927e3155f2e2317d3d516edd9645Tim Peters        test_posix
1229f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_pty
1230f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_pwd
12311e33ffa5c719ec611e182681c4a5f2ceb62a08f5Tim Peters        test_resource
1232f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_signal
12337ece150395f9908870c3baa883e465ec9fe4dd9cSerhiy Storchaka        test_spwd
1234cea2cc4a21e7e2ba84f6dc11682821e502bd1f5fTim Peters        test_threadsignals
1235f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_timing
1236b84de02f75c906fbcf519b4c89b9dbdd18837276Tim Peters        test_wait3
1237b84de02f75c906fbcf519b4c89b9dbdd18837276Tim Peters        test_wait4
1238f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        """,
1239f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum    'linux2':
1240f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        """
1241823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1242f66dacdb017c7481c3ba4f0743d5446146de33c8Guido van Rossum        test_curses
1243f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_dl
1244f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        test_largefile
12450e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
12464507ec70cff35468f4b1767382f38ecebd4a29a2Guido van Rossum        test_ossaudiodev
1247f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum        """,
124821ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis    'unixware7':
12490ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        """
12500ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_bsddb
1251823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
12520ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_dl
12530e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
12540ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_largefile
12550e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
12560ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_minidom
12570ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_openpty
12580ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_pyexpat
12590ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_sax
12600ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        test_sundry
12610ace326ed2ec73dfa515c89ad06fcddd6fafa4ceMartin v. Löwis        """,
126221ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis    'openunix8':
126321ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        """
126421ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_bsddb
1265823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
126621ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_dl
12670e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
126821ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_largefile
12690e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
127021ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_minidom
127121ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_openpty
127221ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_pyexpat
127321ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_sax
127421ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_sundry
127521ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        """,
127621ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis    'sco_sv3':
127721ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        """
127821ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_asynchat
127921ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_bsddb
1280823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
128121ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_dl
128221ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_fork1
12830e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
128421ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_gettext
128521ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_largefile
128621ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_locale
12870e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
128821ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_minidom
128921ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_openpty
129021ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_pyexpat
129121ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_queue
129221ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_sax
129321ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_sundry
129421ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_thread
129521ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_threaded_import
129621ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_threadedtempfile
129721ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        test_threading
129821ee4091e10c6f05360bbb60e49aa3639408a612Martin v. Löwis        """,
1299e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum    'riscos':
1300e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        """
1301e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_asynchat
1302a94568a7535de60f1144e4eea0d027b87017a4b4Martin v. Löwis        test_atexit
1303e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_bsddb
1304823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1305a94568a7535de60f1144e4eea0d027b87017a4b4Martin v. Löwis        test_bsddb3
1306e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_commands
1307e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_crypt
1308e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_dbm
1309e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_dl
1310e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_fcntl
1311e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_fork1
13120e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1313e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_gdbm
1314e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_grp
1315e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_largefile
1316e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_locale
13170e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1318e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_mmap
1319e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_openpty
1320e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_poll
1321e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_popen2
1322e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_pty
1323e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_pwd
1324e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_strop
1325e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_sundry
1326e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_thread
1327e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_threaded_import
1328e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_threadedtempfile
1329e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_threading
1330e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        test_timing
1331e2ae77b8b8a62e648bb1864a9b36ef3280984404Guido van Rossum        """,
13328a97f4a380a7a356730e48406f8269c3efe5e6ebJack Jansen    'darwin':
1333398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        """
13342bfb94c871caeae622174485264f407d9df354b9Brett Cannon        test__locale
1335acda3394bbfb1db3b22673a80cb2d7e3c68b3da9Jack Jansen        test_bsddb
13369d4270070a5d0c3bcd381d52024d811f3b0a0e39Guido van Rossum        test_bsddb3
1337398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        test_curses
13380e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
133929cdfe696a40253989de0ca26204c7169b59fae3Skip Montanaro        test_gdb
1340398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        test_gdbm
1341398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        test_largefile
1342acda3394bbfb1db3b22673a80cb2d7e3c68b3da9Jack Jansen        test_locale
13430e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1344398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        test_minidom
1345acda3394bbfb1db3b22673a80cb2d7e3c68b3da9Jack Jansen        test_ossaudiodev
1346398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        test_poll
1347398c236c1ba358a43a34137dc9ca670d76f7eb84Jack Jansen        """,
134811c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum    'sunos5':
134911c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        """
135011c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_bsddb
1351823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
135211c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_curses
135311c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_dbm
13540e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
13550e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
135611c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_gdbm
135711c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_gzip
135811c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_openpty
135911c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_zipfile
136011c3f0999f932e3697347dde15f99a8ad9f9216dGuido van Rossum        test_zlib
1361ed375e18d10d37bfea1769aa1fe69795df6cbc10Jeremy Hylton        """,
1362b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro    'hp-ux11':
1363b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        """
1364b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_bsddb
1365823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1366b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_curses
1367b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_dl
13680e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1369b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_gdbm
1370b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_gzip
1371b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_largefile
1372b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_locale
13730e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1374b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_minidom
1375b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_openpty
1376b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_pyexpat
1377b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_sax
1378b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_zipfile
1379b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        test_zlib
1380b32302176e127a94b714f90c858b6e9c476fc7feSkip Montanaro        """,
1381f90ae20354ceb501f0ba0b6459df17f1a8005a47Martin v. Löwis    'atheos':
1382c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        """
1383823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1384c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_curses
1385c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_dl
1386c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_gdbm
13870e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1388c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_largefile
1389c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_locale
13900e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1391c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_mhlib
1392c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_mmap
1393c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_poll
1394c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_popen2
1395c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        test_resource
1396c411dbaeee29dba87d5432a92fe76ea65d8e25f0Tim Peters        """,
139725115940604e6051aa2fb16be88c6b8f64577251Jason Tishler    'cygwin':
139825115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        """
1399823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1400b0f89e05ad9aaf229033bbc81be43c1f826e3930Tim Peters        test_bsddb3
140125115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        test_curses
140225115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        test_dbm
14030e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1404c23f39ca9dc540ace879fcf9746254d94293b86fJason Tishler        test_ioctl
14050e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
140625115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        test_largefile
140725115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        test_locale
14085c4ded2c3b59aa134b82ab17cdfe7ab633194ca6Jason Tishler        test_ossaudiodev
140925115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        test_socketserver
141025115940604e6051aa2fb16be88c6b8f64577251Jason Tishler        """,
1411fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre    'os2emx':
1412fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        """
1413fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_audioop
1414823ba28b0d436f83ebfc5b9df0d475e60e8ae5eeSkip Montanaro        test_bsddb185
1415fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_bsddb3
1416fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_commands
1417fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_curses
1418fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_dl
14190e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
14200e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
1421fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_largefile
1422fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_mhlib
1423fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_mmap
1424fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_openpty
1425fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_ossaudiodev
1426fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_pty
1427fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_resource
1428fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        test_signal
1429fd07e7dda709edacc16a851da981a628d61ffd72Andrew MacIntyre        """,
1430944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum    'freebsd4':
1431944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        """
1432944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_bsddb
1433944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_bsddb3
14340e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
1435f64700a512e89c322f46f3cabda3bf58573eda54Hye-Shik Chang        test_gdbm
1436944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_locale
1437944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_ossaudiodev
1438944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_pep277
1439f64700a512e89c322f46f3cabda3bf58573eda54Hye-Shik Chang        test_pty
1440944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_socketserver
1441f64700a512e89c322f46f3cabda3bf58573eda54Hye-Shik Chang        test_tcl
1442bbb7efd72b3360f0523087cff6868f8fa02fe754Guilherme Polo        test_tk
14437a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_guionly
14447a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_textonly
1445944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_timeout
1446944a6c32d7f78445e3516636d9fcf1c62e1663ffGuido van Rossum        test_urllibnet
144737040cdace1982772e5f35e4acfa13861d72065dJesse Noller        test_multiprocessing
144856f88113b7272be8403f9b722c943e679e23d362Martin v. Löwis        """,
14498ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum    'aix5':
14508ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        """
14518ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_bsddb
14528ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_bsddb185
14538ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_bsddb3
14548ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_bz2
14558ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_dl
14560e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
14578ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_gdbm
14588ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_gzip
14590e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_kqueue
14608ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_ossaudiodev
14618ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_tcl
1462bbb7efd72b3360f0523087cff6868f8fa02fe754Guilherme Polo        test_tk
14637a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_guionly
14647a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_textonly
14658ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_zipimport
14668ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        test_zlib
14678ee3e5aa9306a00573817e237bed4a482473e818Guido van Rossum        """,
14686da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis    'openbsd3':
14696da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        """
147027e024023cca704e44b0d5f577415d061f89f7d7Florent Xicluna        test_ascii_formatd
14716da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_bsddb
14726da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_bsddb3
14736da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_ctypes
14746da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_dl
14750e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
14766da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_gdbm
14776da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_locale
14786da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_normalization
14796da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_ossaudiodev
14806da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_pep277
14816da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        test_tcl
1482bbb7efd72b3360f0523087cff6868f8fa02fe754Guilherme Polo        test_tk
14837a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_guionly
14847a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_textonly
148537040cdace1982772e5f35e4acfa13861d72065dJesse Noller        test_multiprocessing
14866da56f9428896f635a794ad523bd88190758e6abMartin v. Löwis        """,
14870870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl    'netbsd3':
14880870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        """
148927e024023cca704e44b0d5f577415d061f89f7d7Florent Xicluna        test_ascii_formatd
14900870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_bsddb
14910870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_bsddb185
14920870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_bsddb3
14930870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_ctypes
14940870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_curses
14950870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_dl
14960e9ab5f2f0f907b57c70557e21633ce8c341d1d1Christian Heimes        test_epoll
14970870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_gdbm
14980870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_locale
14990870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_ossaudiodev
15000870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_pep277
15010870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        test_tcl
1502bbb7efd72b3360f0523087cff6868f8fa02fe754Guilherme Polo        test_tk
15037a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_guionly
15047a77ee88af00b853aae328dbb3c87d91a1313052Guilherme Polo        test_ttk_textonly
150537040cdace1982772e5f35e4acfa13861d72065dJesse Noller        test_multiprocessing
15060870687f449140ad2620af5b6f5f44896e8c1cd5Georg Brandl        """,
1507f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum}
150832d0c1b458bbdda8d8895f5b5bedfb4644f839e7Martin v. Löwis_expectations['freebsd5'] = _expectations['freebsd4']
1509f64700a512e89c322f46f3cabda3bf58573eda54Hye-Shik Chang_expectations['freebsd6'] = _expectations['freebsd4']
15104e422817eb1bc5a6a42365001ad45683ae07e559Hye-Shik Chang_expectations['freebsd7'] = _expectations['freebsd4']
1511ea684743daa0c198ab327d07832eca48a9578c68Hye-Shik Chang_expectations['freebsd8'] = _expectations['freebsd4']
1512f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum
1513b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Petersclass _ExpectedSkips:
1514b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters    def __init__(self):
15152a182dbf3f2520ad753792068391775d102b13dfTim Peters        import os.path
151655b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz        from test import test_timeout
15171b445d3fcfcc06e5360e83b978efdb9b1c980278Tim Peters
15187c7efe90737d4636633127a95a6cab1a55d57cf4Tim Peters        self.valid = False
1519de14a30d1d70073d36f207fe432e4adad5178399Tim Peters        if sys.platform in _expectations:
1520f73e30c3e3ba6f2779eadf6bf4c21c6bf3e4c075Guido van Rossum            s = _expectations[sys.platform]
1521a690a9967e715663b7a421c9ebdad91381cdf1e4Raymond Hettinger            self.expected = set(s.split())
15221b445d3fcfcc06e5360e83b978efdb9b1c980278Tim Peters
1523b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl            # expected to be skipped on every platform, even Linux
1524b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl            self.expected.add('test_linuxaudiodev')
1525b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl
15262a182dbf3f2520ad753792068391775d102b13dfTim Peters            if not os.path.supports_unicode_filenames:
15272a182dbf3f2520ad753792068391775d102b13dfTim Peters                self.expected.add('test_pep277')
15281b445d3fcfcc06e5360e83b978efdb9b1c980278Tim Peters
152955b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz            if test_timeout.skip_expected:
153055b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz                self.expected.add('test_timeout')
153155b61d21d8e8409fbb6ca23421f8a3a5c23f5513Neal Norwitz
1532fba73698240660d9154b6917b87dd333d6fb8284Martin v. Löwis            if sys.maxint == 9223372036854775807L:
1533fba73698240660d9154b6917b87dd333d6fb8284Martin v. Löwis                self.expected.add('test_imageop')
1534fba73698240660d9154b6917b87dd333d6fb8284Martin v. Löwis
15359545a23c7ffb35417d451d24cc3b0339627965a7Ronald Oussoren            if sys.platform != "darwin":
15368f40f0635b9d233cf731c442c2ee21101c0f768cAmaury Forgeot d'Arc                MAC_ONLY = ["test_macos", "test_macostools", "test_aepack",
1537b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                            "test_plistlib", "test_scriptpackages",
1538b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                            "test_applesingle"]
15397035c98c5c0eec7f88fab146e72b2a7e4aaf64a5Neal Norwitz                for skip in MAC_ONLY:
15407035c98c5c0eec7f88fab146e72b2a7e4aaf64a5Neal Norwitz                    self.expected.add(skip)
15416f5a2b52ae769ba1833aef8a52b6ab89921ee8c6Benjamin Peterson            elif len(u'\0'.encode('unicode-internal')) == 4:
15426f5a2b52ae769ba1833aef8a52b6ab89921ee8c6Benjamin Peterson                self.expected.add("test_macostools")
15436f5a2b52ae769ba1833aef8a52b6ab89921ee8c6Benjamin Peterson
1544ecd79eb7dbde19ea2adbf2a912caa5b284b477b9Tim Peters
1545ecd79eb7dbde19ea2adbf2a912caa5b284b477b9Tim Peters            if sys.platform != "win32":
1546b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                # test_sqlite is only reliable on Windows where the library
1547b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                # is distributed with Python
15487035c98c5c0eec7f88fab146e72b2a7e4aaf64a5Neal Norwitz                WIN_ONLY = ["test_unicode_file", "test_winreg",
1549b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                            "test_winsound", "test_startfile",
1550fe2f1ad5b5ac92cc153ff9fdbd88f6058d50a75fRoss Lagerwall                            "test_sqlite", "test_msilib"]
15517035c98c5c0eec7f88fab146e72b2a7e4aaf64a5Neal Norwitz                for skip in WIN_ONLY:
15527035c98c5c0eec7f88fab146e72b2a7e4aaf64a5Neal Norwitz                    self.expected.add(skip)
1553f2715e076435b74638acb81512c2ee014f75aea2Tim Peters
1554dc48b74497b67a449dd622fdaa7d69e7bff65a5eBrett Cannon            if sys.platform != 'irix':
1555b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                IRIX_ONLY = ["test_imageop", "test_al", "test_cd", "test_cl",
1556b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                             "test_gl", "test_imgfile"]
1557dc48b74497b67a449dd622fdaa7d69e7bff65a5eBrett Cannon                for skip in IRIX_ONLY:
1558dc48b74497b67a449dd622fdaa7d69e7bff65a5eBrett Cannon                    self.expected.add(skip)
1559dc48b74497b67a449dd622fdaa7d69e7bff65a5eBrett Cannon
1560b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl            if sys.platform != 'sunos5':
1561b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                self.expected.add('test_sunaudiodev')
1562b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl                self.expected.add('test_nis')
1563b2e208f9010e4b1abbbd560d3d4a8f66fa5861d6Georg Brandl
1564d290b04ee3645f8dd2e30c329c5ead2db16ece62Steven Bethard            if not sys.py3kwarning:
1565d290b04ee3645f8dd2e30c329c5ead2db16ece62Steven Bethard                self.expected.add('test_py3kwarn')
1566e8e22cf3c0d9e977bc9f13cfc535c026f92bc7aaSteven Bethard
15677c7efe90737d4636633127a95a6cab1a55d57cf4Tim Peters            self.valid = True
1568b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
1569b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters    def isvalid(self):
1570b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        "Return true iff _ExpectedSkips knows about the current platform."
1571b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        return self.valid
1572b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
1573b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters    def getexpected(self):
1574b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        """Return set of test names we expect to skip on current platform.
1575b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
1576b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        self.isvalid() must be true.
1577b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        """
1578b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
1579b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        assert self.isvalid()
1580b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters        return self.expected
1581b5b7b78414e5f1a42ab4205c110626c9cd7a79b9Tim Peters
1582152494aea24669a3d74460fa460a4ed45696bc75Guido van Rossumif __name__ == '__main__':
1583928474561c736d87787c99d1c7198ab062318baaFlorent Xicluna    # findtestdir() gets the dirname out of __file__, so we have to make it
1584928474561c736d87787c99d1c7198ab062318baaFlorent Xicluna    # absolute before changing the working directory.
1585928474561c736d87787c99d1c7198ab062318baaFlorent Xicluna    # For example __file__ may be relative when running trace or profile.
1586928474561c736d87787c99d1c7198ab062318baaFlorent Xicluna    # See issue #9323.
1587928474561c736d87787c99d1c7198ab062318baaFlorent Xicluna    __file__ = os.path.abspath(__file__)
1588928474561c736d87787c99d1c7198ab062318baaFlorent Xicluna
1589928474561c736d87787c99d1c7198ab062318baaFlorent Xicluna    # sanity check
15900932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    assert __file__ == os.path.abspath(sys.argv[0])
15910932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna
15920932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    # When tests are run from the Python build directory, it is best practice
15930932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    # to keep the test files in a subfolder.  It eases the cleanup of leftover
15940932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    # files using command "make distclean".
15950932dc5671fa1485e6b9e12fa9e14a0c4de2ddb2Florent Xicluna    if sysconfig.is_python_build():
15961273566cb75e188af7cc38c08df21ba9e07260f4Florent Xicluna        TEMPDIR = os.path.join(sysconfig.get_config_var('srcdir'), 'build')
15971273566cb75e188af7cc38c08df21ba9e07260f4Florent Xicluna        TEMPDIR = os.path.abspath(TEMPDIR)
15981273566cb75e188af7cc38c08df21ba9e07260f4Florent Xicluna        if not os.path.exists(TEMPDIR):
15991273566cb75e188af7cc38c08df21ba9e07260f4Florent Xicluna            os.mkdir(TEMPDIR)
16008f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti
16018f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    # Define a writable temp dir that will be used as cwd while running
16028f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    # the tests. The name of the dir includes the pid to allow parallel
16038f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    # testing (see the -j option).
16048f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    TESTCWD = 'test_python_{}'.format(os.getpid())
16058f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti
16061273566cb75e188af7cc38c08df21ba9e07260f4Florent Xicluna    TESTCWD = os.path.join(TEMPDIR, TESTCWD)
16078f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti
16088f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    # Run the tests in a context manager that temporary changes the CWD to a
16098f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    # temporary and writable directory. If it's not possible to create or
16108f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    # change the CWD, the original CWD will be used. The original CWD is
16118f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    # available from test_support.SAVEDCWD.
16128f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti    with test_support.temp_cwd(TESTCWD, quiet=True):
16138f6a28702961430a2217be64d9d53e2ea490f1b3Ezio Melotti        main()
1614