blob: d26d6188520822a17bd60e963d87bb58c94e345f [file] [log] [blame]
Guido van Rossum152494a1996-12-20 03:12:20 +00001#! /usr/bin/env python
2
3"""Regression test.
4
5This will find all modules whose name is "test_*" in the test
6directory, and run them. Various command line options provide
7additional facilities.
8
9Command line options:
10
Michael W. Hudson61147f62004-08-03 11:33:28 +000011-v: verbose -- run tests in verbose mode with output to stdout
Martin v. Löwis04824ce2006-03-10 21:26:16 +000012-w: verbose2 -- re-run failed tests in verbose mode
Guido van Rossumce4a4752007-02-26 22:01:28 +000013-d: debug -- print traceback for failed tests
Michael W. Hudson61147f62004-08-03 11:33:28 +000014-q: quiet -- don't print anything except if a test fails
15-g: generate -- write the output file for a test instead of comparing it
16-x: exclude -- arguments are tests to *exclude*
17-s: single -- run only a single test (see below)
Neal Norwitz801c89b2007-08-12 01:31:40 +000018-S: start -- start running all the tests with the specified one first
Michael W. Hudson61147f62004-08-03 11:33:28 +000019-r: random -- randomize test execution order
20-f: fromfile -- read names of tests to run from a file (see below)
21-l: findleaks -- if GC is available detect tests that leak memory
22-u: use -- specify which special resource intensive tests to run
23-h: help -- print this text and exit
24-t: threshold -- call gc.set_threshold(N)
25-T: coverage -- turn on code coverage using the trace module
Walter Dörwaldaee4da62004-11-12 18:51:27 +000026-D: coverdir -- Directory where coverage files are put
27-N: nocoverdir -- Put coverage files alongside modules
Michael W. Hudson61147f62004-08-03 11:33:28 +000028-L: runleaks -- run the leaks(1) command just before exit
29-R: huntrleaks -- search for reference leaks (needs debug build, v. slow)
Thomas Wouters477c8d52006-05-27 19:21:47 +000030-M: memlimit -- run very large memory-consuming tests
Guido van Rossum152494a1996-12-20 03:12:20 +000031
32If non-option arguments are present, they are names for tests to run,
33unless -x is given, in which case they are names for tests not to run.
34If no test names are given, all tests are run.
Guido van Rossumf58ed251997-03-07 21:04:33 +000035
Guido van Rossuma4122201997-08-18 20:08:24 +000036-v is incompatible with -g and does not compare test output files.
Barry Warsawe11e3de1999-01-28 19:51:51 +000037
Barry Warsaw3b6d0252004-02-07 22:43:03 +000038-T turns on code coverage tracing with the trace module.
39
Walter Dörwaldaee4da62004-11-12 18:51:27 +000040-D specifies the directory where coverage files are put.
41
42-N Put coverage files alongside modules.
43
Guido van Rossum9e9d4f82002-06-07 15:17:03 +000044-s means to run only a single test and exit. This is useful when
45doing memory analysis on the Python interpreter (which tend to consume
46too many resources to run the full regression test non-stop). The
47file /tmp/pynexttest is read to find the next test to run. If this
48file is missing, the first test_*.py file in testdir or on the command
49line is used. (actually tempfile.gettempdir() is used instead of
50/tmp).
Barry Warsawe11e3de1999-01-28 19:51:51 +000051
Neal Norwitz801c89b2007-08-12 01:31:40 +000052-S is used to continue running tests after an aborted run. It will
53maintain the order a standard run (ie, this assumes -r is not used).
54This is useful after the tests have prematurely stopped for some external
55reason and you want to start running from where you left off rather
56than starting from the beginning.
57
Guido van Rossum9e9d4f82002-06-07 15:17:03 +000058-f reads the names of tests from the file given as f's argument, one
59or more test names per line. Whitespace is ignored. Blank lines and
60lines beginning with '#' are ignored. This is especially useful for
61whittling down failures involving interactions among tests.
Tim Petersc5000df2002-06-02 21:42:01 +000062
Skip Montanaro0179a182004-06-06 15:53:18 +000063-L causes the leaks(1) command to be run just before exit if it exists.
64leaks(1) is available on Mac OS X and presumably on some other
65FreeBSD-derived systems.
66
Michael W. Hudson61147f62004-08-03 11:33:28 +000067-R runs each test several times and examines sys.gettotalrefcount() to
68see if the test appears to be leaking references. The argument should
69be of the form stab:run:fname where 'stab' is the number of times the
70test is run to let gettotalrefcount settle down, 'run' is the number
71of times further it is run and 'fname' is the name of the file the
72reports are written to. These parameters all have defaults (5, 4 and
Guido van Rossum2673de52007-08-17 22:58:14 +000073"reflog.txt" respectively), and the minimal invocation is '-R :'.
Michael W. Hudson61147f62004-08-03 11:33:28 +000074
Thomas Wouters477c8d52006-05-27 19:21:47 +000075-M runs tests that require an exorbitant amount of memory. These tests
76typically try to ascertain containers keep working when containing more than
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000772 billion objects, which only works on 64-bit systems. There are also some
78tests that try to exhaust the address space of the process, which only makes
79sense on 32-bit systems with at least 2Gb of memory. The passed-in memlimit,
Thomas Wouters477c8d52006-05-27 19:21:47 +000080which is a string in the form of '2.5Gb', determines howmuch memory the
81tests will limit themselves to (but they may go slightly over.) The number
82shouldn't be more memory than the machine has (including swap memory). You
83should also keep in mind that swap memory is generally much, much slower
84than RAM, and setting memlimit to all available RAM or higher will heavily
85tax the machine. On the other hand, it is no use running these tests with a
86limit of less than 2.5Gb, and many require more than 20Gb. Tests that expect
87to use more than memlimit memory will be skipped. The big-memory tests
88generally run very, very long.
89
Guido van Rossum9e9d4f82002-06-07 15:17:03 +000090-u is used to specify which special resource intensive tests to run,
91such as those requiring large file support or network connectivity.
92The argument is a comma-separated list of words indicating the
93resources to test. Currently only the following are defined:
Barry Warsaw08fca522001-08-20 22:33:46 +000094
Fred Drake3a15dac2002-04-11 16:39:16 +000095 all - Enable all special resources.
96
Guido van Rossum315aa362003-03-11 14:46:48 +000097 audio - Tests that use the audio device. (There are known
98 cases of broken audio drivers that can crash Python or
99 even the Linux kernel.)
100
Andrew M. Kuchling2158df02001-10-22 15:26:09 +0000101 curses - Tests that use curses and will modify the terminal's
102 state and output modes.
Tim Peters1633a2e2001-10-30 05:56:40 +0000103
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000104 largefile - It is okay to run some test that may create huge
105 files. These tests can take a long time and may
106 consume >2GB of disk space temporarily.
Barry Warsaw08fca522001-08-20 22:33:46 +0000107
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000108 network - It is okay to run tests that use external network
109 resource, e.g. testing SSL support for sockets.
Martin v. Löwis1c6b1a22002-11-19 17:47:07 +0000110
111 bsddb - It is okay to run the bsddb testsuite, which takes
112 a long time to complete.
Fred Drake4dd0f7e2002-11-26 21:44:56 +0000113
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000114 decimal - Test the decimal module against a large suite that
115 verifies compliance with standards.
116
Guido van Rossumebe3e162007-05-17 18:20:34 +0000117 compiler - Allow test_tokenize to verify round-trip lexing on
118 every file in the test library.
Jeremy Hylton4336eda2004-08-07 19:25:33 +0000119
Tim Peterseba28be2005-03-28 01:08:02 +0000120 subprocess Run all tests for the subprocess module.
Peter Astrandf7f1bb72005-03-03 20:47:37 +0000121
Hye-Shik Changaaa2f1d2005-12-10 17:44:27 +0000122 urlfetch - It is okay to download files required on testing.
123
Fred Drake4dd0f7e2002-11-26 21:44:56 +0000124To enable all resources except one, use '-uall,-<resource>'. For
125example, to run all the tests except for the bsddb tests, give the
126option '-uall,-bsddb'.
Guido van Rossum152494a1996-12-20 03:12:20 +0000127"""
128
Guido van Rossum152494a1996-12-20 03:12:20 +0000129import os
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000130import sys
Guido van Rossum152494a1996-12-20 03:12:20 +0000131import getopt
Skip Montanaroab1c7912000-06-30 16:39:27 +0000132import random
Guido van Rossumdc15c272002-08-12 21:55:51 +0000133import warnings
Thomas Wouters9ada3d62006-04-21 09:47:09 +0000134import re
Guido van Rossum34d19282007-08-09 01:03:29 +0000135import io
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000136import traceback
Guido van Rossumdc15c272002-08-12 21:55:51 +0000137
138# I see no other way to suppress these warnings;
139# putting them in test_grammar.py has no effect:
Guido van Rossum88b1def2002-08-14 17:54:48 +0000140warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
Guido van Rossumdc15c272002-08-12 21:55:51 +0000141 ".*test.test_grammar$")
Guido van Rossumc34c4fc2002-09-19 00:42:16 +0000142if sys.maxint > 0x7fffffff:
143 # Also suppress them in <string>, because for 64-bit platforms,
144 # that's where test_grammar.py hides them.
145 warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
146 "<string>")
Guido van Rossum152494a1996-12-20 03:12:20 +0000147
Thomas Wouters477c8d52006-05-27 19:21:47 +0000148# Ignore ImportWarnings that only occur in the source tree,
149# (because of modules with the same name as source-directories in Modules/)
150for mod in ("ctypes", "gzip", "zipfile", "tarfile", "encodings.zlib_codec",
151 "test.test_zipimport", "test.test_zlib", "test.test_zipfile",
152 "test.test_codecs", "test.string_tests"):
153 warnings.filterwarnings(module=".*%s$" % (mod,),
154 action="ignore", category=ImportWarning)
155
Guido van Rossumbb484652002-12-02 09:56:21 +0000156# MacOSX (a.k.a. Darwin) has a default stack size that is too small
157# for deeply recursive regular expressions. We see this as crashes in
158# the Python test suite when running test_re.py and test_sre.py. The
159# fix is to set the stack limit to 2048.
160# This approach may also be useful for other Unixy platforms that
161# suffer from small default stack limits.
162if sys.platform == 'darwin':
163 try:
164 import resource
165 except ImportError:
166 pass
167 else:
168 soft, hard = resource.getrlimit(resource.RLIMIT_STACK)
169 newsoft = min(hard, max(soft, 1024*2048))
170 resource.setrlimit(resource.RLIMIT_STACK, (newsoft, hard))
171
Barry Warsaw04f357c2002-07-23 19:04:11 +0000172from test import test_support
Fred Drake3a15dac2002-04-11 16:39:16 +0000173
Raymond Hettinger7c85fa42004-07-01 11:01:35 +0000174RESOURCE_NAMES = ('audio', 'curses', 'largefile', 'network', 'bsddb',
Hye-Shik Changaaa2f1d2005-12-10 17:44:27 +0000175 'decimal', 'compiler', 'subprocess', 'urlfetch')
Fred Drake3a15dac2002-04-11 16:39:16 +0000176
177
Guido van Rossum4e7eba72007-08-17 18:39:15 +0000178def usage(msg):
179 print(msg, file=sys.stderr)
180 print("Use --help for usage", file=sys.stderr)
181 sys.exit(2)
Barry Warsaw08fca522001-08-20 22:33:46 +0000182
183
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000184def main(tests=None, testdir=None, verbose=0, quiet=False, generate=False,
185 exclude=False, single=False, randomize=False, fromfile=None,
Walter Dörwaldaee4da62004-11-12 18:51:27 +0000186 findleaks=False, use_resources=None, trace=False, coverdir='coverage',
Guido van Rossum3de862d2007-08-18 00:10:33 +0000187 runleaks=False, huntrleaks=None, verbose2=False, debug=False,
Neal Norwitz801c89b2007-08-12 01:31:40 +0000188 start=None):
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000189 """Execute a test suite.
190
Thomas Wouters7e474022000-07-16 12:04:32 +0000191 This also parses command-line options and modifies its behavior
Fred Drake004d5e62000-10-23 17:22:08 +0000192 accordingly.
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000193
194 tests -- a list of strings containing test names (optional)
195 testdir -- the directory in which to look for tests (optional)
196
197 Users other than the Python test suite will certainly want to
198 specify testdir; if it's omitted, the directory containing the
Fred Drake004d5e62000-10-23 17:22:08 +0000199 Python test suite is searched for.
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000200
201 If the tests argument is omitted, the tests listed on the
202 command-line will be used. If that's empty, too, then all *.py
203 files beginning with test_ will be used.
Skip Montanaroab1c7912000-06-30 16:39:27 +0000204
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000205 The other default arguments (verbose, quiet, generate, exclude, single,
Walter Dörwaldaee4da62004-11-12 18:51:27 +0000206 randomize, findleaks, use_resources, trace and coverdir) allow programmers
207 calling main() directly to set the values that would normally be set by
208 flags on the command line.
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000209 """
Fred Drake004d5e62000-10-23 17:22:08 +0000210
Tim Peters8dee8092001-09-25 20:05:11 +0000211 test_support.record_original_stdout(sys.stdout)
Guido van Rossum152494a1996-12-20 03:12:20 +0000212 try:
Neal Norwitz801c89b2007-08-12 01:31:40 +0000213 opts, args = getopt.getopt(sys.argv[1:], 'dhvgqxsS:rf:lu:t:TD:NLR:wM:',
Barry Warsaw08fca522001-08-20 22:33:46 +0000214 ['help', 'verbose', 'quiet', 'generate',
Tim Petersc5000df2002-06-02 21:42:01 +0000215 'exclude', 'single', 'random', 'fromfile',
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000216 'findleaks', 'use=', 'threshold=', 'trace',
Walter Dörwaldaee4da62004-11-12 18:51:27 +0000217 'coverdir=', 'nocoverdir', 'runleaks',
Thomas Wouters477c8d52006-05-27 19:21:47 +0000218 'huntrleaks=', 'verbose2', 'memlimit=',
Neal Norwitz801c89b2007-08-12 01:31:40 +0000219 'debug', 'start='
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000220 ])
Guido van Rossumb940e112007-01-10 16:19:56 +0000221 except getopt.error as msg:
Guido van Rossum4e7eba72007-08-17 18:39:15 +0000222 usage(msg)
Barry Warsaw08fca522001-08-20 22:33:46 +0000223
224 # Defaults
225 if use_resources is None:
226 use_resources = []
Guido van Rossum152494a1996-12-20 03:12:20 +0000227 for o, a in opts:
Barry Warsaw08fca522001-08-20 22:33:46 +0000228 if o in ('-h', '--help'):
Guido van Rossum4e7eba72007-08-17 18:39:15 +0000229 print(__doc__)
230 return
Barry Warsaw08fca522001-08-20 22:33:46 +0000231 elif o in ('-v', '--verbose'):
232 verbose += 1
Martin v. Löwis04824ce2006-03-10 21:26:16 +0000233 elif o in ('-w', '--verbose2'):
234 verbose2 = True
Guido van Rossumce4a4752007-02-26 22:01:28 +0000235 elif o in ('-d', '--debug'):
236 debug = True
Barry Warsaw08fca522001-08-20 22:33:46 +0000237 elif o in ('-q', '--quiet'):
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000238 quiet = True;
Barry Warsaw08fca522001-08-20 22:33:46 +0000239 verbose = 0
240 elif o in ('-g', '--generate'):
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000241 generate = True
Barry Warsaw08fca522001-08-20 22:33:46 +0000242 elif o in ('-x', '--exclude'):
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000243 exclude = True
Neal Norwitz801c89b2007-08-12 01:31:40 +0000244 elif o in ('-S', '--start'):
245 start = a
Barry Warsaw08fca522001-08-20 22:33:46 +0000246 elif o in ('-s', '--single'):
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000247 single = True
Barry Warsaw08fca522001-08-20 22:33:46 +0000248 elif o in ('-r', '--randomize'):
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000249 randomize = True
Tim Petersc5000df2002-06-02 21:42:01 +0000250 elif o in ('-f', '--fromfile'):
251 fromfile = a
Barry Warsaw08fca522001-08-20 22:33:46 +0000252 elif o in ('-l', '--findleaks'):
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000253 findleaks = True
Skip Montanaro0179a182004-06-06 15:53:18 +0000254 elif o in ('-L', '--runleaks'):
255 runleaks = True
Guido van Rossum9e9d4f82002-06-07 15:17:03 +0000256 elif o in ('-t', '--threshold'):
257 import gc
258 gc.set_threshold(int(a))
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000259 elif o in ('-T', '--coverage'):
260 trace = True
Walter Dörwaldaee4da62004-11-12 18:51:27 +0000261 elif o in ('-D', '--coverdir'):
262 coverdir = os.path.join(os.getcwd(), a)
263 elif o in ('-N', '--nocoverdir'):
264 coverdir = None
Michael W. Hudson61147f62004-08-03 11:33:28 +0000265 elif o in ('-R', '--huntrleaks'):
266 huntrleaks = a.split(':')
Guido van Rossum2673de52007-08-17 22:58:14 +0000267 if len(huntrleaks) not in (2, 3):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000268 print(a, huntrleaks)
Guido van Rossum2673de52007-08-17 22:58:14 +0000269 usage('-R takes 2 or 3 colon-separated arguments')
270 if not huntrleaks[0]:
Michael W. Hudson61147f62004-08-03 11:33:28 +0000271 huntrleaks[0] = 5
272 else:
273 huntrleaks[0] = int(huntrleaks[0])
Guido van Rossum2673de52007-08-17 22:58:14 +0000274 if not huntrleaks[1]:
Michael W. Hudson61147f62004-08-03 11:33:28 +0000275 huntrleaks[1] = 4
276 else:
277 huntrleaks[1] = int(huntrleaks[1])
Guido van Rossum2673de52007-08-17 22:58:14 +0000278 if len(huntrleaks) == 2 or not huntrleaks[2]:
279 huntrleaks[2:] = ["reflog.txt"]
Thomas Wouters477c8d52006-05-27 19:21:47 +0000280 elif o in ('-M', '--memlimit'):
281 test_support.set_memlimit(a)
Barry Warsaw08fca522001-08-20 22:33:46 +0000282 elif o in ('-u', '--use'):
Guido van Rossumfe3f6962001-09-06 16:09:41 +0000283 u = [x.lower() for x in a.split(',')]
284 for r in u:
Fred Drake3a15dac2002-04-11 16:39:16 +0000285 if r == 'all':
Fred Drake4dd0f7e2002-11-26 21:44:56 +0000286 use_resources[:] = RESOURCE_NAMES
287 continue
288 remove = False
289 if r[0] == '-':
290 remove = True
291 r = r[1:]
Fred Drake3a15dac2002-04-11 16:39:16 +0000292 if r not in RESOURCE_NAMES:
Guido van Rossum4e7eba72007-08-17 18:39:15 +0000293 usage('Invalid -u/--use option: ' + a)
Fred Drake4dd0f7e2002-11-26 21:44:56 +0000294 if remove:
295 if r in use_resources:
296 use_resources.remove(r)
297 elif r not in use_resources:
Andrew MacIntyree41abab2002-04-30 12:11:04 +0000298 use_resources.append(r)
Guido van Rossuma4122201997-08-18 20:08:24 +0000299 if generate and verbose:
Guido van Rossum4e7eba72007-08-17 18:39:15 +0000300 usage("-g and -v don't go together!")
Tim Petersc5000df2002-06-02 21:42:01 +0000301 if single and fromfile:
Guido van Rossum4e7eba72007-08-17 18:39:15 +0000302 usage("-s and -f don't go together!")
Barry Warsaw08fca522001-08-20 22:33:46 +0000303
Guido van Rossum152494a1996-12-20 03:12:20 +0000304 good = []
305 bad = []
306 skipped = []
Fred Drake9a0db072003-02-03 15:19:30 +0000307 resource_denieds = []
Barry Warsawe11e3de1999-01-28 19:51:51 +0000308
Neil Schemenauerd569f232000-09-22 15:29:28 +0000309 if findleaks:
Barry Warsawa873b032000-08-03 15:50:37 +0000310 try:
311 import gc
312 except ImportError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000313 print('No GC available, disabling findleaks.')
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000314 findleaks = False
Barry Warsawa873b032000-08-03 15:50:37 +0000315 else:
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000316 # Uncomment the line below to report garbage that is not
317 # freeable by reference counting alone. By default only
318 # garbage that is not collectable by the GC is reported.
319 #gc.set_debug(gc.DEBUG_SAVEALL)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000320 found_garbage = []
Barry Warsawa873b032000-08-03 15:50:37 +0000321
Barry Warsawe11e3de1999-01-28 19:51:51 +0000322 if single:
323 from tempfile import gettempdir
324 filename = os.path.join(gettempdir(), 'pynexttest')
325 try:
326 fp = open(filename, 'r')
Eric S. Raymondfc170b12001-02-09 11:51:27 +0000327 next = fp.read().strip()
Barry Warsawe11e3de1999-01-28 19:51:51 +0000328 tests = [next]
329 fp.close()
330 except IOError:
331 pass
Tim Petersc5000df2002-06-02 21:42:01 +0000332
333 if fromfile:
334 tests = []
335 fp = open(fromfile)
336 for line in fp:
337 guts = line.split() # assuming no test has whitespace in its name
338 if guts and not guts[0].startswith('#'):
339 tests.extend(guts)
340 fp.close()
341
342 # Strip .py extensions.
343 if args:
344 args = map(removepy, args)
345 if tests:
346 tests = map(removepy, tests)
347
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000348 stdtests = STDTESTS[:]
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000349 nottests = NOTTESTS.copy()
Guido van Rossum152494a1996-12-20 03:12:20 +0000350 if exclude:
Guido van Rossum6c74fea1998-08-25 12:29:08 +0000351 for arg in args:
352 if arg in stdtests:
353 stdtests.remove(arg)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000354 nottests.add(arg)
Guido van Rossum41360a41998-03-26 19:42:58 +0000355 args = []
Guido van Rossum747e1ca1998-08-24 13:48:36 +0000356 tests = tests or args or findtests(testdir, stdtests, nottests)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000357 if single:
358 tests = tests[:1]
Neal Norwitz801c89b2007-08-12 01:31:40 +0000359 # Remove all the tests that precede start if it's set.
360 if start:
361 try:
362 del tests[:tests.index(start)]
363 except ValueError:
364 print("Couldn't find starting test (%s), using all tests" % start)
Skip Montanaroab1c7912000-06-30 16:39:27 +0000365 if randomize:
366 random.shuffle(tests)
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000367 if trace:
368 import trace
369 tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix],
370 trace=False, count=True)
Guido van Rossum41360a41998-03-26 19:42:58 +0000371 test_support.verbose = verbose # Tell tests to be moderately quiet
Barry Warsaw08fca522001-08-20 22:33:46 +0000372 test_support.use_resources = use_resources
Guido van Rossum5796d262000-04-21 21:35:06 +0000373 save_modules = sys.modules.keys()
Guido van Rossum152494a1996-12-20 03:12:20 +0000374 for test in tests:
Guido van Rossum41360a41998-03-26 19:42:58 +0000375 if not quiet:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000376 print(test)
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000377 sys.stdout.flush()
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000378 if trace:
379 # If we're tracing code coverage, then we don't exit with status
380 # if on a false return value from main.
381 tracer.runctx('runtest(test, generate, verbose, quiet, testdir)',
382 globals=globals(), locals=vars())
Guido van Rossum41360a41998-03-26 19:42:58 +0000383 else:
Neal Norwitz14ca3272006-02-28 18:05:43 +0000384 try:
385 ok = runtest(test, generate, verbose, quiet, testdir,
386 huntrleaks)
387 except KeyboardInterrupt:
388 # print a newline separate from the ^C
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000389 print()
Neal Norwitz14ca3272006-02-28 18:05:43 +0000390 break
391 except:
392 raise
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000393 if ok > 0:
394 good.append(test)
395 elif ok == 0:
396 bad.append(test)
397 else:
398 skipped.append(test)
399 if ok == -2:
400 resource_denieds.append(test)
Neil Schemenauerd569f232000-09-22 15:29:28 +0000401 if findleaks:
402 gc.collect()
403 if gc.garbage:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000404 print("Warning: test created", len(gc.garbage), end=' ')
405 print("uncollectable object(s).")
Neil Schemenauer8a00abc2000-10-13 01:32:42 +0000406 # move the uncollectable objects somewhere so we don't see
407 # them again
Neil Schemenauerd569f232000-09-22 15:29:28 +0000408 found_garbage.extend(gc.garbage)
409 del gc.garbage[:]
Guido van Rossum5796d262000-04-21 21:35:06 +0000410 # Unload the newly imported modules (best effort finalization)
411 for module in sys.modules.keys():
Guido van Rossum51931142000-05-05 14:27:39 +0000412 if module not in save_modules and module.startswith("test."):
Guido van Rossum5796d262000-04-21 21:35:06 +0000413 test_support.unload(module)
Jeremy Hylton7a1ea0e2001-10-17 13:45:28 +0000414
415 # The lists won't be sorted if running with -r
416 good.sort()
417 bad.sort()
418 skipped.sort()
Tim Peterse0c446b2001-10-18 21:57:37 +0000419
Guido van Rossum152494a1996-12-20 03:12:20 +0000420 if good and not quiet:
Guido van Rossum41360a41998-03-26 19:42:58 +0000421 if not bad and not skipped and len(good) > 1:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000422 print("All", end=' ')
423 print(count(len(good), "test"), "OK.")
Tim Peters1a4d77b2000-12-30 22:21:22 +0000424 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000425 print("CAUTION: stdout isn't compared in verbose mode:")
426 print("a test that passes in verbose mode may fail without it.")
Guido van Rossum152494a1996-12-20 03:12:20 +0000427 if bad:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000428 print(count(len(bad), "test"), "failed:")
Tim Petersa45da922001-08-12 03:45:50 +0000429 printlist(bad)
Guido van Rossum152494a1996-12-20 03:12:20 +0000430 if skipped and not quiet:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000431 print(count(len(skipped), "test"), "skipped:")
Tim Petersa45da922001-08-12 03:45:50 +0000432 printlist(skipped)
Barry Warsawe11e3de1999-01-28 19:51:51 +0000433
Tim Petersb5b7b782001-08-12 01:20:39 +0000434 e = _ExpectedSkips()
Tim Petersa2be2d62001-08-12 02:01:09 +0000435 plat = sys.platform
Tim Petersb5b7b782001-08-12 01:20:39 +0000436 if e.isvalid():
Raymond Hettingera690a992003-11-16 16:17:49 +0000437 surprise = set(skipped) - e.getexpected() - set(resource_denieds)
Tim Petersb5b7b782001-08-12 01:20:39 +0000438 if surprise:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000439 print(count(len(surprise), "skip"), \
440 "unexpected on", plat + ":")
Tim Petersa45da922001-08-12 03:45:50 +0000441 printlist(surprise)
Tim Petersb5b7b782001-08-12 01:20:39 +0000442 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000443 print("Those skips are all expected on", plat + ".")
Tim Petersb5b7b782001-08-12 01:20:39 +0000444 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000445 print("Ask someone to teach regrtest.py about which tests are")
446 print("expected to get skipped on", plat + ".")
Tim Petersb5b7b782001-08-12 01:20:39 +0000447
Martin v. Löwis04824ce2006-03-10 21:26:16 +0000448 if verbose2 and bad:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000449 print("Re-running failed tests in verbose mode")
Martin v. Löwis04824ce2006-03-10 21:26:16 +0000450 for test in bad:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000451 print("Re-running test %r in verbose mode" % test)
Tim Peters922dd7d2006-03-10 23:37:10 +0000452 sys.stdout.flush()
Martin v. Löwis04824ce2006-03-10 21:26:16 +0000453 try:
454 test_support.verbose = 1
455 ok = runtest(test, generate, 1, quiet, testdir,
Guido van Rossumce4a4752007-02-26 22:01:28 +0000456 huntrleaks, debug)
Martin v. Löwis04824ce2006-03-10 21:26:16 +0000457 except KeyboardInterrupt:
458 # print a newline separate from the ^C
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000459 print()
Martin v. Löwis04824ce2006-03-10 21:26:16 +0000460 break
461 except:
462 raise
463
Barry Warsawe11e3de1999-01-28 19:51:51 +0000464 if single:
465 alltests = findtests(testdir, stdtests, nottests)
466 for i in range(len(alltests)):
467 if tests[0] == alltests[i]:
468 if i == len(alltests) - 1:
469 os.unlink(filename)
470 else:
471 fp = open(filename, 'w')
472 fp.write(alltests[i+1] + '\n')
473 fp.close()
474 break
475 else:
476 os.unlink(filename)
477
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000478 if trace:
479 r = tracer.results()
480 r.write_results(show_missing=True, summary=True, coverdir=coverdir)
481
Skip Montanaro0179a182004-06-06 15:53:18 +0000482 if runleaks:
483 os.system("leaks %d" % os.getpid())
484
Tim Peters5943b4a2003-07-23 00:30:39 +0000485 sys.exit(len(bad) > 0)
Barry Warsaw08fca522001-08-20 22:33:46 +0000486
Guido van Rossum152494a1996-12-20 03:12:20 +0000487
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000488STDTESTS = [
Guido van Rossum152494a1996-12-20 03:12:20 +0000489 'test_grammar',
490 'test_opcodes',
Guido van Rossumd8faa362007-04-27 19:54:29 +0000491 'test_dict',
Guido van Rossum152494a1996-12-20 03:12:20 +0000492 'test_builtin',
493 'test_exceptions',
494 'test_types',
Collin Winter7afaa882007-03-08 19:54:43 +0000495 'test_unittest',
496 'test_doctest',
497 'test_doctest2',
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000498]
Guido van Rossum152494a1996-12-20 03:12:20 +0000499
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000500NOTTESTS = {
Guido van Rossum152494a1996-12-20 03:12:20 +0000501 'test_support',
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +0000502 'test_future1',
503 'test_future2',
Jeremy Hylton8471a352001-08-20 20:33:42 +0000504 'test_future3',
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000505}
Guido van Rossum152494a1996-12-20 03:12:20 +0000506
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000507def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
Guido van Rossum152494a1996-12-20 03:12:20 +0000508 """Return a list of all applicable test modules."""
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000509 if not testdir: testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000510 names = os.listdir(testdir)
511 tests = []
512 for name in names:
Skip Montanaro7a98be22007-08-16 14:35:24 +0000513 if name[:5] == "test_" and name[-3:] == ".py":
Guido van Rossum41360a41998-03-26 19:42:58 +0000514 modname = name[:-3]
515 if modname not in stdtests and modname not in nottests:
516 tests.append(modname)
Guido van Rossum152494a1996-12-20 03:12:20 +0000517 tests.sort()
518 return stdtests + tests
519
Guido van Rossumce4a4752007-02-26 22:01:28 +0000520def runtest(test, generate, verbose, quiet, testdir=None,
Guido van Rossum3de862d2007-08-18 00:10:33 +0000521 huntrleaks=None, debug=False):
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000522 """Run a single test.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000523
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000524 test -- the name of the test
525 generate -- if true, generate output, instead of running the test
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000526 and comparing it to a previously created output file
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000527 verbose -- if true, print more messages
Trent Mickf29f47b2000-08-11 19:02:59 +0000528 quiet -- if true, don't print 'skipped' messages (probably redundant)
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000529 testdir -- test directory
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000530 huntrleaks -- run multiple times to test for leaks; requires a debug
531 build; a triple corresponding to -R's three arguments
Guido van Rossumce4a4752007-02-26 22:01:28 +0000532 debug -- if true, print tracebacks for failed tests regardless of
533 verbose setting
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000534 Return:
535 -2 test skipped because resource denied
536 -1 test skipped for some other reason
537 0 test failed
538 1 test passed
Guido van Rossum6fd83b71998-08-01 17:04:08 +0000539 """
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000540
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000541 try:
542 return runtest_inner(test, generate, verbose, quiet, testdir,
Guido van Rossumce4a4752007-02-26 22:01:28 +0000543 huntrleaks, debug)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000544 finally:
545 cleanup_test_droppings(test, verbose)
546
547def runtest_inner(test, generate, verbose, quiet,
Guido van Rossum3de862d2007-08-18 00:10:33 +0000548 testdir=None, huntrleaks=None, debug=False):
Guido van Rossum152494a1996-12-20 03:12:20 +0000549 test_support.unload(test)
Barry Warsaw3b6d0252004-02-07 22:43:03 +0000550 if not testdir:
551 testdir = findtestdir()
Guido van Rossum152494a1996-12-20 03:12:20 +0000552 outputdir = os.path.join(testdir, "output")
553 outputfile = os.path.join(outputdir, test)
Tim Peters9390cc12001-09-28 20:14:46 +0000554 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000555 cfp = None
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000556 else:
Guido van Rossum34d19282007-08-09 01:03:29 +0000557 cfp = io.StringIO() # XXX Should use io.StringIO()
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000558
Guido van Rossum152494a1996-12-20 03:12:20 +0000559 try:
Tim Peters342ca752001-09-25 19:13:20 +0000560 save_stdout = sys.stdout
Guido van Rossum41360a41998-03-26 19:42:58 +0000561 try:
562 if cfp:
563 sys.stdout = cfp
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000564 print(test) # Output file starts with test name
Barry Warsaw408b6d32002-07-30 23:27:12 +0000565 if test.startswith('test.'):
566 abstest = test
567 else:
568 # Always import it from the test package
569 abstest = 'test.' + test
570 the_package = __import__(abstest, globals(), locals(), [])
571 the_module = getattr(the_package, test)
Tim Petersd9742212001-05-22 18:28:25 +0000572 # Most tests run to completion simply as a side-effect of
573 # being imported. For the benefit of tests that can't run
574 # that way (like test_threaded_import), explicitly invoke
575 # their test_main() function (if it exists).
576 indirect_test = getattr(the_module, "test_main", None)
577 if indirect_test is not None:
578 indirect_test()
Michael W. Hudson61147f62004-08-03 11:33:28 +0000579 if huntrleaks:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000580 dash_R(the_module, test, indirect_test, huntrleaks)
Guido van Rossum41360a41998-03-26 19:42:58 +0000581 finally:
Tim Peters342ca752001-09-25 19:13:20 +0000582 sys.stdout = save_stdout
Guido van Rossumb940e112007-01-10 16:19:56 +0000583 except test_support.ResourceDenied as msg:
Fred Drake9a0db072003-02-03 15:19:30 +0000584 if not quiet:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000585 print(test, "skipped --", msg)
Fred Drake9a0db072003-02-03 15:19:30 +0000586 sys.stdout.flush()
587 return -2
Guido van Rossumb940e112007-01-10 16:19:56 +0000588 except (ImportError, test_support.TestSkipped) as msg:
Trent Mickf29f47b2000-08-11 19:02:59 +0000589 if not quiet:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000590 print(test, "skipped --", msg)
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000591 sys.stdout.flush()
Guido van Rossum41360a41998-03-26 19:42:58 +0000592 return -1
Fred Drakefe5c22a2000-08-18 16:04:05 +0000593 except KeyboardInterrupt:
594 raise
Guido van Rossumb940e112007-01-10 16:19:56 +0000595 except test_support.TestFailed as msg:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000596 print("test", test, "failed --", msg)
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000597 sys.stdout.flush()
Guido van Rossum41360a41998-03-26 19:42:58 +0000598 return 0
Guido van Rossum9e48b271997-07-16 01:56:13 +0000599 except:
Guido van Rossum41360a41998-03-26 19:42:58 +0000600 type, value = sys.exc_info()[:2]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000601 print("test", test, "crashed --", str(type) + ":", value)
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000602 sys.stdout.flush()
Guido van Rossumce4a4752007-02-26 22:01:28 +0000603 if verbose or debug:
Guido van Rossum41360a41998-03-26 19:42:58 +0000604 traceback.print_exc(file=sys.stdout)
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000605 sys.stdout.flush()
Guido van Rossum41360a41998-03-26 19:42:58 +0000606 return 0
Guido van Rossum152494a1996-12-20 03:12:20 +0000607 else:
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000608 if not cfp:
609 return 1
610 output = cfp.getvalue()
Fred Drakee51fe8d2001-05-29 17:10:51 +0000611 if generate:
Fred Drakee51fe8d2001-05-29 17:10:51 +0000612 if output == test + "\n":
613 if os.path.exists(outputfile):
614 # Write it since it already exists (and the contents
615 # may have changed), but let the user know it isn't
616 # needed:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000617 print("output file", outputfile, \
618 "is no longer needed; consider removing it")
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000619 else:
620 # We don't need it, so don't create it.
621 return 1
622 fp = open(outputfile, "w")
623 fp.write(output)
624 fp.close()
625 return 1
626 if os.path.exists(outputfile):
627 fp = open(outputfile, "r")
628 expected = fp.read()
629 fp.close()
630 else:
631 expected = test + "\n"
Michael W. Hudson61147f62004-08-03 11:33:28 +0000632 if output == expected or huntrleaks:
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000633 return 1
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000634 print("test", test, "produced unexpected output:")
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000635 sys.stdout.flush()
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000636 reportdiff(expected, output)
Guido van Rossum3cda93e2002-09-13 21:28:03 +0000637 sys.stdout.flush()
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000638 return 0
639
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000640def cleanup_test_droppings(testname, verbose):
641 import shutil
642
643 # Try to clean up junk commonly left behind. While tests shouldn't leave
644 # any files or directories behind, when a test fails that can be tedious
645 # for it to arrange. The consequences can be especially nasty on Windows,
646 # since if a test leaves a file open, it cannot be deleted by name (while
647 # there's nothing we can do about that here either, we can display the
648 # name of the offending test, which is a real help).
649 for name in (test_support.TESTFN,
650 "db_home",
651 ):
652 if not os.path.exists(name):
653 continue
654
655 if os.path.isdir(name):
656 kind, nuker = "directory", shutil.rmtree
657 elif os.path.isfile(name):
658 kind, nuker = "file", os.unlink
659 else:
660 raise SystemError("os.path says %r exists but is neither "
661 "directory nor file" % name)
662
663 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000664 print("%r left behind %s %r" % (testname, kind, name))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000665 try:
666 nuker(name)
Guido van Rossumb940e112007-01-10 16:19:56 +0000667 except Exception as msg:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000668 print(("%r left behind %s %r and it couldn't be "
669 "removed: %s" % (testname, kind, name, msg)), file=sys.stderr)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000670
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000671def dash_R(the_module, test, indirect_test, huntrleaks):
672 # This code is hackish and inelegant, but it seems to do the job.
Guido van Rossum3de862d2007-08-18 00:10:33 +0000673 import copy_reg, _abcoll
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000674
675 if not hasattr(sys, 'gettotalrefcount'):
676 raise Exception("Tracking reference leaks requires a debug build "
677 "of Python")
678
679 # Save current values for dash_R_cleanup() to restore.
680 fs = warnings.filters[:]
681 ps = copy_reg.dispatch_table.copy()
682 pic = sys.path_importer_cache.copy()
Guido van Rossumc1e315d2007-08-20 19:29:24 +0000683 abcs = {obj: obj._abc_registry.copy()
Guido van Rossum3de862d2007-08-18 00:10:33 +0000684 for abc in [getattr(_abcoll, a) for a in _abcoll.__all__]
685 for obj in abc.__subclasses__() + [abc]}
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000686
687 if indirect_test:
688 def run_the_test():
689 indirect_test()
690 else:
691 def run_the_test():
Guido van Rossume7ba4952007-06-06 23:52:48 +0000692 del sys.modules[the_module.__name__]
693 exec('import ' + the_module.__name__)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000694
695 deltas = []
696 nwarmup, ntracked, fname = huntrleaks
697 repcount = nwarmup + ntracked
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000698 print("beginning", repcount, "repetitions", file=sys.stderr)
699 print(("1234567890"*(repcount//10 + 1))[:repcount], file=sys.stderr)
Guido van Rossum3de862d2007-08-18 00:10:33 +0000700 dash_R_cleanup(fs, ps, pic, abcs)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000701 for i in range(repcount):
702 rc = sys.gettotalrefcount()
703 run_the_test()
704 sys.stderr.write('.')
Neal Norwitzbb217d92007-08-12 00:00:44 +0000705 sys.stderr.flush()
Guido van Rossum3de862d2007-08-18 00:10:33 +0000706 dash_R_cleanup(fs, ps, pic, abcs)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000707 if i >= nwarmup:
708 deltas.append(sys.gettotalrefcount() - rc - 2)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000709 print(file=sys.stderr)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000710 if any(deltas):
Guido van Rossum360e4b82007-05-14 22:51:27 +0000711 msg = '%s leaked %s references, sum=%s' % (test, deltas, sum(deltas))
712 print(msg, file=sys.stderr)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000713 refrep = open(fname, "a")
Guido van Rossum360e4b82007-05-14 22:51:27 +0000714 print(msg, file=refrep)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000715 refrep.close()
716
Guido van Rossum3de862d2007-08-18 00:10:33 +0000717def dash_R_cleanup(fs, ps, pic, abcs):
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000718 import gc, copy_reg
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000719 import _strptime, linecache, dircache
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000720 import urlparse, urllib, urllib2, mimetypes, doctest
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000721 import struct, filecmp, _abcoll
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000722 from distutils.dir_util import _path_created
723
724 # Restore some original values.
725 warnings.filters[:] = fs
726 copy_reg.dispatch_table.clear()
727 copy_reg.dispatch_table.update(ps)
728 sys.path_importer_cache.clear()
729 sys.path_importer_cache.update(pic)
730
Guido van Rossum3de862d2007-08-18 00:10:33 +0000731 # Clear ABC registries, restoring previously saved ABC registries.
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000732 for abc in [getattr(_abcoll, a) for a in _abcoll.__all__]:
733 for obj in abc.__subclasses__() + [abc]:
Guido van Rossumc1e315d2007-08-20 19:29:24 +0000734 obj._abc_registry = abcs.get(obj, {}).copy()
735 obj._abc_cache.clear()
736 obj._abc_negative_cache.clear()
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000737
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000738 # Clear assorted module caches.
739 _path_created.clear()
740 re.purge()
741 _strptime._regex_cache.clear()
742 urlparse.clear_cache()
743 urllib.urlcleanup()
744 urllib2.install_opener(None)
745 dircache.reset()
746 linecache.clearcache()
747 mimetypes._default_mime_types()
748 struct._cache.clear()
749 filecmp._cache.clear()
750 doctest.master = None
751
752 # Collect cyclic trash.
753 gc.collect()
754
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000755def reportdiff(expected, output):
Guido van Rossum0fcca4e2001-09-21 20:31:52 +0000756 import difflib
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000757 print("*" * 70)
Tim Petersc377b162001-09-22 05:31:03 +0000758 a = expected.splitlines(1)
759 b = output.splitlines(1)
Guido van Rossumcf691932001-09-21 21:06:22 +0000760 sm = difflib.SequenceMatcher(a=a, b=b)
761 tuples = sm.get_opcodes()
Tim Petersc377b162001-09-22 05:31:03 +0000762
Guido van Rossumcf691932001-09-21 21:06:22 +0000763 def pair(x0, x1):
Tim Petersc377b162001-09-22 05:31:03 +0000764 # x0:x1 are 0-based slice indices; convert to 1-based line indices.
Guido van Rossumcf691932001-09-21 21:06:22 +0000765 x0 += 1
766 if x0 >= x1:
Tim Petersc377b162001-09-22 05:31:03 +0000767 return "line " + str(x0)
Guido van Rossumcf691932001-09-21 21:06:22 +0000768 else:
Tim Petersc377b162001-09-22 05:31:03 +0000769 return "lines %d-%d" % (x0, x1)
770
Guido van Rossumcf691932001-09-21 21:06:22 +0000771 for op, a0, a1, b0, b1 in tuples:
772 if op == 'equal':
773 pass
Tim Petersc377b162001-09-22 05:31:03 +0000774
Guido van Rossumcf691932001-09-21 21:06:22 +0000775 elif op == 'delete':
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000776 print("***", pair(a0, a1), "of expected output missing:")
Guido van Rossumcf691932001-09-21 21:06:22 +0000777 for line in a[a0:a1]:
Guido van Rossum08c47ba2007-02-09 20:50:08 +0000778 print("-", line, end='')
Tim Petersc377b162001-09-22 05:31:03 +0000779
Guido van Rossumcf691932001-09-21 21:06:22 +0000780 elif op == 'replace':
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000781 print("*** mismatch between", pair(a0, a1), "of expected", \
782 "output and", pair(b0, b1), "of actual output:")
Tim Petersc377b162001-09-22 05:31:03 +0000783 for line in difflib.ndiff(a[a0:a1], b[b0:b1]):
Guido van Rossum08c47ba2007-02-09 20:50:08 +0000784 print(line, end='')
Tim Petersc377b162001-09-22 05:31:03 +0000785
Guido van Rossumcf691932001-09-21 21:06:22 +0000786 elif op == 'insert':
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000787 print("***", pair(b0, b1), "of actual output doesn't appear", \
788 "in expected output after line", str(a1)+":")
Guido van Rossumcf691932001-09-21 21:06:22 +0000789 for line in b[b0:b1]:
Guido van Rossum08c47ba2007-02-09 20:50:08 +0000790 print("+", line, end='')
Tim Petersc377b162001-09-22 05:31:03 +0000791
Guido van Rossumcf691932001-09-21 21:06:22 +0000792 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000793 print("get_opcodes() returned bad tuple?!?!", (op, a0, a1, b0, b1))
Tim Petersc377b162001-09-22 05:31:03 +0000794
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000795 print("*" * 70)
Guido van Rossum152494a1996-12-20 03:12:20 +0000796
797def findtestdir():
798 if __name__ == '__main__':
Guido van Rossum41360a41998-03-26 19:42:58 +0000799 file = sys.argv[0]
Guido van Rossum152494a1996-12-20 03:12:20 +0000800 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000801 file = __file__
Guido van Rossum152494a1996-12-20 03:12:20 +0000802 testdir = os.path.dirname(file) or os.curdir
803 return testdir
804
Tim Petersc5000df2002-06-02 21:42:01 +0000805def removepy(name):
Skip Montanaro7a98be22007-08-16 14:35:24 +0000806 if name.endswith(".py"):
Tim Petersc5000df2002-06-02 21:42:01 +0000807 name = name[:-3]
808 return name
809
Guido van Rossum152494a1996-12-20 03:12:20 +0000810def count(n, word):
811 if n == 1:
Guido van Rossum41360a41998-03-26 19:42:58 +0000812 return "%d %s" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000813 else:
Guido van Rossum41360a41998-03-26 19:42:58 +0000814 return "%d %ss" % (n, word)
Guido van Rossum152494a1996-12-20 03:12:20 +0000815
Tim Petersa45da922001-08-12 03:45:50 +0000816def printlist(x, width=70, indent=4):
Tim Peters7c7efe92002-08-23 17:55:54 +0000817 """Print the elements of iterable x to stdout.
Tim Petersa45da922001-08-12 03:45:50 +0000818
819 Optional arg width (default 70) is the maximum line length.
820 Optional arg indent (default 4) is the number of blanks with which to
821 begin each line.
822 """
823
Tim Petersba78bc42002-07-04 19:45:06 +0000824 from textwrap import fill
825 blanks = ' ' * indent
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000826 print(fill(' '.join(map(str, x)), width,
827 initial_indent=blanks, subsequent_indent=blanks))
Tim Petersa45da922001-08-12 03:45:50 +0000828
Tim Petersde14a302002-04-01 05:04:46 +0000829# Map sys.platform to a string containing the basenames of tests
830# expected to be skipped on that platform.
Tim Peters2a182db2002-10-09 01:07:11 +0000831#
832# Special cases:
833# test_pep277
834# The _ExpectedSkips constructor adds this to the set of expected
835# skips if not os.path.supports_unicode_filenames.
Tim Petersb4ee4eb2002-12-04 03:26:57 +0000836# test_socket_ssl
837# Controlled by test_socket_ssl.skip_expected. Requires the network
838# resource, and a socket module with ssl support.
Neal Norwitz55b61d22003-02-28 19:57:03 +0000839# test_timeout
840# Controlled by test_timeout.skip_expected. Requires the network
841# resource and a socket module.
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000842#
843# Tests that are expected to be skipped everywhere except on one platform
844# are also handled separately.
Tim Petersde14a302002-04-01 05:04:46 +0000845
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000846_expectations = {
847 'win32':
848 """
Tim Petersc7c516a2003-09-20 22:06:13 +0000849 test__locale
Tim Peters78e35f92002-11-22 20:00:34 +0000850 test_bsddb3
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000851 test_commands
852 test_crypt
Tim Petersd7030572001-10-22 22:06:08 +0000853 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000854 test_dbm
855 test_dl
856 test_fcntl
857 test_fork1
858 test_gdbm
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000859 test_grp
Tim Petersfd8e6e52003-03-04 00:26:38 +0000860 test_ioctl
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000861 test_largefile
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000862 test_mhlib
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000863 test_openpty
Tim Petersefc4b122002-12-10 18:47:56 +0000864 test_ossaudiodev
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000865 test_poll
Tim Peters003eb302003-02-17 21:48:48 +0000866 test_posix
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000867 test_pty
868 test_pwd
Tim Peters1e33ffa2002-04-23 23:09:02 +0000869 test_resource
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000870 test_signal
Tim Peterscea2cc42004-08-04 02:32:03 +0000871 test_threadsignals
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000872 test_wait3
873 test_wait4
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000874 """,
875 'linux2':
876 """
Guido van Rossumf66dacd2001-10-23 15:10:55 +0000877 test_curses
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000878 test_dl
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000879 test_largefile
Guido van Rossum4507ec72003-02-14 19:29:22 +0000880 test_ossaudiodev
Guido van Rossumf73e30c2001-08-12 02:22:19 +0000881 """,
Jack Jansen49a806e2001-08-28 14:49:00 +0000882 'mac':
Guido van Rossumaa782362001-09-02 03:58:41 +0000883 """
Jack Jansen67975142003-01-08 16:31:11 +0000884 test_atexit
Guido van Rossumaa782362001-09-02 03:58:41 +0000885 test_bsddb
Jack Jansen67975142003-01-08 16:31:11 +0000886 test_bsddb3
887 test_bz2
Guido van Rossumaa782362001-09-02 03:58:41 +0000888 test_commands
889 test_crypt
Jack Jansenb3be2162001-11-30 14:16:36 +0000890 test_curses
Guido van Rossumaa782362001-09-02 03:58:41 +0000891 test_dbm
892 test_dl
893 test_fcntl
894 test_fork1
Guido van Rossumaa782362001-09-02 03:58:41 +0000895 test_grp
Jack Jansenc4d6bdd2003-03-07 15:38:11 +0000896 test_ioctl
Guido van Rossumaa782362001-09-02 03:58:41 +0000897 test_largefile
Guido van Rossumaa782362001-09-02 03:58:41 +0000898 test_locale
899 test_mmap
Guido van Rossumaa782362001-09-02 03:58:41 +0000900 test_openpty
Jack Jansen67975142003-01-08 16:31:11 +0000901 test_ossaudiodev
Guido van Rossumaa782362001-09-02 03:58:41 +0000902 test_poll
Jack Jansen67975142003-01-08 16:31:11 +0000903 test_popen
Jack Jansen5bb97e62003-02-21 22:33:55 +0000904 test_posix
Guido van Rossumaa782362001-09-02 03:58:41 +0000905 test_pty
906 test_pwd
Jack Jansen67975142003-01-08 16:31:11 +0000907 test_resource
Guido van Rossumaa782362001-09-02 03:58:41 +0000908 test_signal
Guido van Rossumaa782362001-09-02 03:58:41 +0000909 test_sundry
Jack Jansenc4d6bdd2003-03-07 15:38:11 +0000910 test_tarfile
Guido van Rossumaa782362001-09-02 03:58:41 +0000911 """,
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000912 'unixware7':
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000913 """
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000914 test_bsddb
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000915 test_dl
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000916 test_largefile
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000917 test_minidom
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000918 test_openpty
919 test_pyexpat
920 test_sax
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000921 test_sundry
Martin v. Löwis0ace3262001-09-05 14:38:48 +0000922 """,
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000923 'openunix8':
924 """
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000925 test_bsddb
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000926 test_dl
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000927 test_largefile
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000928 test_minidom
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000929 test_openpty
930 test_pyexpat
931 test_sax
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000932 test_sundry
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000933 """,
934 'sco_sv3':
935 """
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000936 test_asynchat
937 test_bsddb
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000938 test_dl
939 test_fork1
940 test_gettext
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000941 test_largefile
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000942 test_locale
943 test_minidom
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000944 test_openpty
945 test_pyexpat
946 test_queue
947 test_sax
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000948 test_sundry
949 test_thread
950 test_threaded_import
951 test_threadedtempfile
952 test_threading
Martin v. Löwis21ee4092002-09-30 16:19:48 +0000953 """,
Jack Jansen8a97f4a2001-12-05 23:27:32 +0000954 'darwin':
Jack Jansen398c2362001-12-02 21:41:36 +0000955 """
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000956 test__locale
957 test_bsddb
958 test_bsddb3
959 test_curses
Jack Jansen398c2362001-12-02 21:41:36 +0000960 test_gdbm
Jack Jansen398c2362001-12-02 21:41:36 +0000961 test_largefile
Jack Jansenacda3392002-12-30 23:03:13 +0000962 test_locale
Jack Jansenacda3392002-12-30 23:03:13 +0000963 test_ossaudiodev
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000964 test_poll
Jack Jansen398c2362001-12-02 21:41:36 +0000965 """,
Guido van Rossum11c3f092002-07-17 15:08:24 +0000966 'sunos5':
967 """
Guido van Rossum11c3f092002-07-17 15:08:24 +0000968 test_bsddb
Guido van Rossum11c3f092002-07-17 15:08:24 +0000969 test_curses
970 test_dbm
Guido van Rossum11c3f092002-07-17 15:08:24 +0000971 test_gdbm
Guido van Rossum11c3f092002-07-17 15:08:24 +0000972 test_gzip
Guido van Rossum11c3f092002-07-17 15:08:24 +0000973 test_openpty
Guido van Rossum11c3f092002-07-17 15:08:24 +0000974 test_zipfile
975 test_zlib
Jeremy Hyltoned375e12002-07-17 15:56:55 +0000976 """,
Skip Montanarob3230212002-03-15 02:54:03 +0000977 'hp-ux11':
978 """
Skip Montanarob3230212002-03-15 02:54:03 +0000979 test_bsddb
Skip Montanarob3230212002-03-15 02:54:03 +0000980 test_curses
981 test_dl
982 test_gdbm
Skip Montanarob3230212002-03-15 02:54:03 +0000983 test_gzip
Skip Montanarob3230212002-03-15 02:54:03 +0000984 test_largefile
Skip Montanarob3230212002-03-15 02:54:03 +0000985 test_locale
986 test_minidom
Skip Montanarob3230212002-03-15 02:54:03 +0000987 test_openpty
988 test_pyexpat
989 test_sax
Skip Montanarob3230212002-03-15 02:54:03 +0000990 test_zipfile
991 test_zlib
992 """,
Martin v. Löwisf90ae202002-06-11 06:22:31 +0000993 'atheos':
Tim Petersc411dba2002-07-16 21:35:23 +0000994 """
Tim Petersc411dba2002-07-16 21:35:23 +0000995 test_curses
996 test_dl
Tim Petersc411dba2002-07-16 21:35:23 +0000997 test_gdbm
Tim Petersc411dba2002-07-16 21:35:23 +0000998 test_largefile
Tim Petersc411dba2002-07-16 21:35:23 +0000999 test_locale
1000 test_mhlib
1001 test_mmap
Tim Petersc411dba2002-07-16 21:35:23 +00001002 test_poll
Tim Petersc411dba2002-07-16 21:35:23 +00001003 test_resource
Tim Petersc411dba2002-07-16 21:35:23 +00001004 """,
Jason Tishler25115942002-12-05 15:18:15 +00001005 'cygwin':
1006 """
Tim Petersb0f89e02002-12-05 17:20:25 +00001007 test_bsddb3
Jason Tishler25115942002-12-05 15:18:15 +00001008 test_curses
1009 test_dbm
Jason Tishlerc23f39c2003-07-22 18:35:58 +00001010 test_ioctl
Jason Tishler25115942002-12-05 15:18:15 +00001011 test_largefile
Jason Tishler25115942002-12-05 15:18:15 +00001012 test_locale
Jason Tishler5c4ded22003-02-05 16:46:01 +00001013 test_ossaudiodev
Jason Tishler25115942002-12-05 15:18:15 +00001014 test_socketserver
Jason Tishler25115942002-12-05 15:18:15 +00001015 """,
Andrew MacIntyrefd07e7d2002-12-31 11:26:50 +00001016 'os2emx':
1017 """
Andrew MacIntyrefd07e7d2002-12-31 11:26:50 +00001018 test_audioop
1019 test_bsddb3
Andrew MacIntyrefd07e7d2002-12-31 11:26:50 +00001020 test_commands
1021 test_curses
1022 test_dl
Andrew MacIntyrefd07e7d2002-12-31 11:26:50 +00001023 test_largefile
Andrew MacIntyrefd07e7d2002-12-31 11:26:50 +00001024 test_mhlib
1025 test_mmap
Andrew MacIntyrefd07e7d2002-12-31 11:26:50 +00001026 test_openpty
1027 test_ossaudiodev
1028 test_pty
1029 test_resource
1030 test_signal
Andrew MacIntyrefd07e7d2002-12-31 11:26:50 +00001031 """,
Guido van Rossum944a6c32003-11-20 22:11:29 +00001032 'freebsd4':
1033 """
Guido van Rossum944a6c32003-11-20 22:11:29 +00001034 test_bsddb
1035 test_bsddb3
Hye-Shik Changf64700a2004-08-18 15:13:41 +00001036 test_gdbm
Guido van Rossum944a6c32003-11-20 22:11:29 +00001037 test_locale
Guido van Rossum944a6c32003-11-20 22:11:29 +00001038 test_ossaudiodev
1039 test_pep277
Hye-Shik Changf64700a2004-08-18 15:13:41 +00001040 test_pty
Guido van Rossum944a6c32003-11-20 22:11:29 +00001041 test_socket_ssl
1042 test_socketserver
Hye-Shik Changf64700a2004-08-18 15:13:41 +00001043 test_tcl
Guido van Rossum944a6c32003-11-20 22:11:29 +00001044 test_timeout
Guido van Rossum944a6c32003-11-20 22:11:29 +00001045 test_urllibnet
Martin v. Löwis56f88112003-06-07 20:01:37 +00001046 """,
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001047 'aix5':
1048 """
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001049 test_bsddb
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001050 test_bsddb3
1051 test_bz2
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001052 test_dl
1053 test_gdbm
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001054 test_gzip
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001055 test_ossaudiodev
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001056 test_tcl
Guido van Rossum8ee3e5a2005-09-14 18:09:42 +00001057 test_zipimport
1058 test_zlib
1059 """,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001060 'openbsd3':
1061 """
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001062 test_bsddb
1063 test_bsddb3
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001064 test_ctypes
1065 test_dl
1066 test_gdbm
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001067 test_locale
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001068 test_normalization
1069 test_ossaudiodev
1070 test_pep277
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001071 test_tcl
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001072 """,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001073 'netbsd3':
1074 """
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001075 test_bsddb
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001076 test_bsddb3
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001077 test_ctypes
1078 test_curses
1079 test_dl
1080 test_gdbm
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001081 test_locale
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001082 test_ossaudiodev
1083 test_pep277
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001084 test_tcl
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001085 """,
Guido van Rossumf73e30c2001-08-12 02:22:19 +00001086}
Martin v. Löwis32d0c1b2004-07-26 12:09:13 +00001087_expectations['freebsd5'] = _expectations['freebsd4']
Hye-Shik Changf64700a2004-08-18 15:13:41 +00001088_expectations['freebsd6'] = _expectations['freebsd4']
Hye-Shik Chang4e422812005-07-17 02:36:59 +00001089_expectations['freebsd7'] = _expectations['freebsd4']
Guido van Rossumf73e30c2001-08-12 02:22:19 +00001090
Tim Petersb5b7b782001-08-12 01:20:39 +00001091class _ExpectedSkips:
1092 def __init__(self):
Tim Peters2a182db2002-10-09 01:07:11 +00001093 import os.path
Tim Petersb4ee4eb2002-12-04 03:26:57 +00001094 from test import test_socket_ssl
Neal Norwitz55b61d22003-02-28 19:57:03 +00001095 from test import test_timeout
Tim Peters1b445d32002-11-24 18:53:11 +00001096
Tim Peters7c7efe92002-08-23 17:55:54 +00001097 self.valid = False
Tim Petersde14a302002-04-01 05:04:46 +00001098 if sys.platform in _expectations:
Guido van Rossumf73e30c2001-08-12 02:22:19 +00001099 s = _expectations[sys.platform]
Raymond Hettingera690a992003-11-16 16:17:49 +00001100 self.expected = set(s.split())
Tim Peters1b445d32002-11-24 18:53:11 +00001101
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001102 # expected to be skipped on every platform, even Linux
Tim Peters2a182db2002-10-09 01:07:11 +00001103 if not os.path.supports_unicode_filenames:
1104 self.expected.add('test_pep277')
Tim Peters1b445d32002-11-24 18:53:11 +00001105
Tim Petersb4ee4eb2002-12-04 03:26:57 +00001106 if test_socket_ssl.skip_expected:
1107 self.expected.add('test_socket_ssl')
1108
Neal Norwitz55b61d22003-02-28 19:57:03 +00001109 if test_timeout.skip_expected:
1110 self.expected.add('test_timeout')
1111
Jack Jansen6afc5e02003-01-29 16:24:16 +00001112 if not sys.platform in ("mac", "darwin"):
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001113 MAC_ONLY = ["test_macostools", "test_aepack",
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001114 "test_plistlib", "test_scriptpackages",
1115 "test_applesingle"]
Neal Norwitz7035c982003-03-29 22:01:17 +00001116 for skip in MAC_ONLY:
1117 self.expected.add(skip)
Tim Petersecd79eb2003-01-29 00:35:32 +00001118
1119 if sys.platform != "win32":
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001120 # test_sqlite is only reliable on Windows where the library
1121 # is distributed with Python
Neal Norwitz7035c982003-03-29 22:01:17 +00001122 WIN_ONLY = ["test_unicode_file", "test_winreg",
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001123 "test_winsound", "test_startfile",
1124 "test_sqlite"]
Neal Norwitz7035c982003-03-29 22:01:17 +00001125 for skip in WIN_ONLY:
1126 self.expected.add(skip)
Tim Petersf2715e02003-02-19 02:35:07 +00001127
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001128 if sys.platform != 'sunos5':
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001129 self.expected.add('test_nis')
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001130
Tim Peters7c7efe92002-08-23 17:55:54 +00001131 self.valid = True
Tim Petersb5b7b782001-08-12 01:20:39 +00001132
1133 def isvalid(self):
1134 "Return true iff _ExpectedSkips knows about the current platform."
1135 return self.valid
1136
1137 def getexpected(self):
1138 """Return set of test names we expect to skip on current platform.
1139
1140 self.isvalid() must be true.
1141 """
1142
1143 assert self.isvalid()
1144 return self.expected
1145
Guido van Rossum152494a1996-12-20 03:12:20 +00001146if __name__ == '__main__':
Barry Warsaw408b6d32002-07-30 23:27:12 +00001147 # Remove regrtest.py's own directory from the module search path. This
1148 # prevents relative imports from working, and relative imports will screw
1149 # up the testing framework. E.g. if both test.test_support and
1150 # test_support are imported, they will not contain the same globals, and
1151 # much of the testing framework relies on the globals in the
1152 # test.test_support module.
1153 mydir = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
1154 i = pathlen = len(sys.path)
1155 while i >= 0:
1156 i -= 1
1157 if os.path.abspath(os.path.normpath(sys.path[i])) == mydir:
1158 del sys.path[i]
1159 if len(sys.path) == pathlen:
Guido van Rossumbe19ed72007-02-09 05:37:30 +00001160 print('Could not find %r in sys.path to remove it' % mydir)
Barry Warsaw08fca522001-08-20 22:33:46 +00001161 main()