source: python/vendor/Python-2.6.5/Lib/test/regrtest.py

Last change on this file was 2, checked in by Yuri Dario, 15 years ago

Initial import for vendor code.

  • Property svn:eol-style set to native
File size: 37.0 KB
Line 
1#! /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
11-v: verbose -- run tests in verbose mode with output to stdout
12-w: verbose2 -- re-run failed tests in verbose mode
13-q: quiet -- don't print anything except if a test fails
14-x: exclude -- arguments are tests to *exclude*
15-s: single -- run only a single test (see below)
16-S: slow -- print the slowest 10 tests
17-r: random -- randomize test execution order
18-f: fromfile -- read names of tests to run from a file (see below)
19-l: findleaks -- if GC is available detect tests that leak memory
20-u: use -- specify which special resource intensive tests to run
21-h: help -- print this text and exit
22-t: threshold -- call gc.set_threshold(N)
23-T: coverage -- turn on code coverage using the trace module
24-D: coverdir -- Directory where coverage files are put
25-N: nocoverdir -- Put coverage files alongside modules
26-L: runleaks -- run the leaks(1) command just before exit
27-R: huntrleaks -- search for reference leaks (needs debug build, v. slow)
28-M: memlimit -- run very large memory-consuming tests
29
30If non-option arguments are present, they are names for tests to run,
31unless -x is given, in which case they are names for tests not to run.
32If no test names are given, all tests are run.
33
34-T turns on code coverage tracing with the trace module.
35
36-D specifies the directory where coverage files are put.
37
38-N Put coverage files alongside modules.
39
40-s means to run only a single test and exit. This is useful when
41doing memory analysis on the Python interpreter (which tend to consume
42too many resources to run the full regression test non-stop). The
43file /tmp/pynexttest is read to find the next test to run. If this
44file is missing, the first test_*.py file in testdir or on the command
45line is used. (actually tempfile.gettempdir() is used instead of
46/tmp).
47
48-f reads the names of tests from the file given as f's argument, one
49or more test names per line. Whitespace is ignored. Blank lines and
50lines beginning with '#' are ignored. This is especially useful for
51whittling down failures involving interactions among tests.
52
53-L causes the leaks(1) command to be run just before exit if it exists.
54leaks(1) is available on Mac OS X and presumably on some other
55FreeBSD-derived systems.
56
57-R runs each test several times and examines sys.gettotalrefcount() to
58see if the test appears to be leaking references. The argument should
59be of the form stab:run:fname where 'stab' is the number of times the
60test is run to let gettotalrefcount settle down, 'run' is the number
61of times further it is run and 'fname' is the name of the file the
62reports are written to. These parameters all have defaults (5, 4 and
63"reflog.txt" respectively), so the minimal invocation is '-R ::'.
64
65-M runs tests that require an exorbitant amount of memory. These tests
66typically try to ascertain containers keep working when containing more than
672 billion objects, which only works on 64-bit systems. There are also some
68tests that try to exhaust the address space of the process, which only makes
69sense on 32-bit systems with at least 2Gb of memory. The passed-in memlimit,
70which is a string in the form of '2.5Gb', determines howmuch memory the
71tests will limit themselves to (but they may go slightly over.) The number
72shouldn't be more memory than the machine has (including swap memory). You
73should also keep in mind that swap memory is generally much, much slower
74than RAM, and setting memlimit to all available RAM or higher will heavily
75tax the machine. On the other hand, it is no use running these tests with a
76limit of less than 2.5Gb, and many require more than 20Gb. Tests that expect
77to use more than memlimit memory will be skipped. The big-memory tests
78generally run very, very long.
79
80-u is used to specify which special resource intensive tests to run,
81such as those requiring large file support or network connectivity.
82The argument is a comma-separated list of words indicating the
83resources to test. Currently only the following are defined:
84
85 all - Enable all special resources.
86
87 audio - Tests that use the audio device. (There are known
88 cases of broken audio drivers that can crash Python or
89 even the Linux kernel.)
90
91 curses - Tests that use curses and will modify the terminal's
92 state and output modes.
93
94 lib2to3 - Run the tests for 2to3 (They take a while.)
95
96 largefile - It is okay to run some test that may create huge
97 files. These tests can take a long time and may
98 consume >2GB of disk space temporarily.
99
100 network - It is okay to run tests that use external network
101 resource, e.g. testing SSL support for sockets.
102
103 bsddb - It is okay to run the bsddb testsuite, which takes
104 a long time to complete.
105
106 decimal - Test the decimal module against a large suite that
107 verifies compliance with standards.
108
109 compiler - Test the compiler package by compiling all the source
110 in the standard library and test suite. This takes
111 a long time. Enabling this resource also allows
112 test_tokenize to verify round-trip lexing on every
113 file in the test library.
114
115 subprocess Run all tests for the subprocess module.
116
117 urlfetch - It is okay to download files required on testing.
118
119To enable all resources except one, use '-uall,-<resource>'. For
120example, to run all the tests except for the bsddb tests, give the
121option '-uall,-bsddb'.
122"""
123
124import cStringIO
125import getopt
126import os
127import random
128import re
129import sys
130import time
131import traceback
132import warnings
133
134# I see no other way to suppress these warnings;
135# putting them in test_grammar.py has no effect:
136warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
137 ".*test.test_grammar$")
138if sys.maxint > 0x7fffffff:
139 # Also suppress them in <string>, because for 64-bit platforms,
140 # that's where test_grammar.py hides them.
141 warnings.filterwarnings("ignore", "hex/oct constants", FutureWarning,
142 "<string>")
143
144# Ignore ImportWarnings that only occur in the source tree,
145# (because of modules with the same name as source-directories in Modules/)
146for mod in ("ctypes", "gzip", "zipfile", "tarfile", "encodings.zlib_codec",
147 "test.test_zipimport", "test.test_zlib", "test.test_zipfile",
148 "test.test_codecs", "test.string_tests"):
149 warnings.filterwarnings(module=".*%s$" % (mod,),
150 action="ignore", category=ImportWarning)
151
152# MacOSX (a.k.a. Darwin) has a default stack size that is too small
153# for deeply recursive regular expressions. We see this as crashes in
154# the Python test suite when running test_re.py and test_sre.py. The
155# fix is to set the stack limit to 2048.
156# This approach may also be useful for other Unixy platforms that
157# suffer from small default stack limits.
158if sys.platform == 'darwin':
159 try:
160 import resource
161 except ImportError:
162 pass
163 else:
164 soft, hard = resource.getrlimit(resource.RLIMIT_STACK)
165 newsoft = min(hard, max(soft, 1024*2048))
166 resource.setrlimit(resource.RLIMIT_STACK, (newsoft, hard))
167
168from test import test_support
169
170RESOURCE_NAMES = ('audio', 'curses', 'largefile', 'network', 'bsddb',
171 'decimal', 'compiler', 'subprocess', 'urlfetch')
172
173
174def usage(code, msg=''):
175 print __doc__
176 if msg: print msg
177 sys.exit(code)
178
179
180def main(tests=None, testdir=None, verbose=0, quiet=False,
181 exclude=False, single=False, randomize=False, fromfile=None,
182 findleaks=False, use_resources=None, trace=False, coverdir='coverage',
183 runleaks=False, huntrleaks=False, verbose2=False, print_slow=False):
184 """Execute a test suite.
185
186 This also parses command-line options and modifies its behavior
187 accordingly.
188
189 tests -- a list of strings containing test names (optional)
190 testdir -- the directory in which to look for tests (optional)
191
192 Users other than the Python test suite will certainly want to
193 specify testdir; if it's omitted, the directory containing the
194 Python test suite is searched for.
195
196 If the tests argument is omitted, the tests listed on the
197 command-line will be used. If that's empty, too, then all *.py
198 files beginning with test_ will be used.
199
200 The other default arguments (verbose, quiet, exclude,
201 single, randomize, findleaks, use_resources, trace, coverdir, and
202 print_slow) allow programmers calling main() directly to set the
203 values that would normally be set by flags on the command line.
204 """
205
206 test_support.record_original_stdout(sys.stdout)
207 try:
208 opts, args = getopt.getopt(sys.argv[1:], 'hvqxsSrf:lu:t:TD:NLR:wM:',
209 ['help', 'verbose', 'quiet', 'exclude',
210 'single', 'slow', 'random', 'fromfile',
211 'findleaks', 'use=', 'threshold=', 'trace',
212 'coverdir=', 'nocoverdir', 'runleaks',
213 'huntrleaks=', 'verbose2', 'memlimit=',
214 ])
215 except getopt.error, msg:
216 usage(2, msg)
217
218 # Defaults
219 if use_resources is None:
220 use_resources = []
221 for o, a in opts:
222 if o in ('-h', '--help'):
223 usage(0)
224 elif o in ('-v', '--verbose'):
225 verbose += 1
226 elif o in ('-w', '--verbose2'):
227 verbose2 = True
228 elif o in ('-q', '--quiet'):
229 quiet = True;
230 verbose = 0
231 elif o in ('-x', '--exclude'):
232 exclude = True
233 elif o in ('-s', '--single'):
234 single = True
235 elif o in ('-S', '--slow'):
236 print_slow = True
237 elif o in ('-r', '--randomize'):
238 randomize = True
239 elif o in ('-f', '--fromfile'):
240 fromfile = a
241 elif o in ('-l', '--findleaks'):
242 findleaks = True
243 elif o in ('-L', '--runleaks'):
244 runleaks = True
245 elif o in ('-t', '--threshold'):
246 import gc
247 gc.set_threshold(int(a))
248 elif o in ('-T', '--coverage'):
249 trace = True
250 elif o in ('-D', '--coverdir'):
251 coverdir = os.path.join(os.getcwd(), a)
252 elif o in ('-N', '--nocoverdir'):
253 coverdir = None
254 elif o in ('-R', '--huntrleaks'):
255 huntrleaks = a.split(':')
256 if len(huntrleaks) != 3:
257 print a, huntrleaks
258 usage(2, '-R takes three colon-separated arguments')
259 if len(huntrleaks[0]) == 0:
260 huntrleaks[0] = 5
261 else:
262 huntrleaks[0] = int(huntrleaks[0])
263 if len(huntrleaks[1]) == 0:
264 huntrleaks[1] = 4
265 else:
266 huntrleaks[1] = int(huntrleaks[1])
267 if len(huntrleaks[2]) == 0:
268 huntrleaks[2] = "reflog.txt"
269 elif o in ('-M', '--memlimit'):
270 test_support.set_memlimit(a)
271 elif o in ('-u', '--use'):
272 u = [x.lower() for x in a.split(',')]
273 for r in u:
274 if r == 'all':
275 use_resources[:] = RESOURCE_NAMES
276 continue
277 remove = False
278 if r[0] == '-':
279 remove = True
280 r = r[1:]
281 if r not in RESOURCE_NAMES:
282 usage(1, 'Invalid -u/--use option: ' + a)
283 if remove:
284 if r in use_resources:
285 use_resources.remove(r)
286 elif r not in use_resources:
287 use_resources.append(r)
288 else:
289 print >>sys.stderr, ("No handler for option {0}. Please "
290 "report this as a bug at http://bugs.python.org.").format(o)
291 sys.exit(1)
292 if single and fromfile:
293 usage(2, "-s and -f don't go together!")
294
295 good = []
296 bad = []
297 skipped = []
298 resource_denieds = []
299
300 if findleaks:
301 try:
302 import gc
303 except ImportError:
304 print 'No GC available, disabling findleaks.'
305 findleaks = False
306 else:
307 # Uncomment the line below to report garbage that is not
308 # freeable by reference counting alone. By default only
309 # garbage that is not collectable by the GC is reported.
310 #gc.set_debug(gc.DEBUG_SAVEALL)
311 found_garbage = []
312
313 if single:
314 from tempfile import gettempdir
315 filename = os.path.join(gettempdir(), 'pynexttest')
316 try:
317 fp = open(filename, 'r')
318 next = fp.read().strip()
319 tests = [next]
320 fp.close()
321 except IOError:
322 pass
323
324 if fromfile:
325 tests = []
326 fp = open(fromfile)
327 for line in fp:
328 guts = line.split() # assuming no test has whitespace in its name
329 if guts and not guts[0].startswith('#'):
330 tests.extend(guts)
331 fp.close()
332
333 # Strip .py extensions.
334 if args:
335 args = map(removepy, args)
336 if tests:
337 tests = map(removepy, tests)
338
339 stdtests = STDTESTS[:]
340 nottests = NOTTESTS[:]
341 if exclude:
342 for arg in args:
343 if arg in stdtests:
344 stdtests.remove(arg)
345 nottests[:0] = args
346 args = []
347 tests = tests or args or findtests(testdir, stdtests, nottests)
348 if single:
349 tests = tests[:1]
350 if randomize:
351 random.shuffle(tests)
352 if trace:
353 import trace
354 tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix],
355 trace=False, count=True)
356 test_times = []
357 test_support.verbose = verbose # Tell tests to be moderately quiet
358 test_support.use_resources = use_resources
359 save_modules = sys.modules.keys()
360 for test in tests:
361 if not quiet:
362 print test
363 sys.stdout.flush()
364 if trace:
365 # If we're tracing code coverage, then we don't exit with status
366 # if on a false return value from main.
367 tracer.runctx('runtest(test, verbose, quiet,'
368 ' test_times, testdir)',
369 globals=globals(), locals=vars())
370 else:
371 try:
372 ok = runtest(test, verbose, quiet, test_times,
373 testdir, huntrleaks)
374 except KeyboardInterrupt:
375 # print a newline separate from the ^C
376 print
377 break
378 except:
379 raise
380 if ok > 0:
381 good.append(test)
382 elif ok == 0:
383 bad.append(test)
384 else:
385 skipped.append(test)
386 if ok == -2:
387 resource_denieds.append(test)
388 if findleaks:
389 gc.collect()
390 if gc.garbage:
391 print "Warning: test created", len(gc.garbage),
392 print "uncollectable object(s)."
393 # move the uncollectable objects somewhere so we don't see
394 # them again
395 found_garbage.extend(gc.garbage)
396 del gc.garbage[:]
397 # Unload the newly imported modules (best effort finalization)
398 for module in sys.modules.keys():
399 if module not in save_modules and module.startswith("test."):
400 test_support.unload(module)
401
402 # The lists won't be sorted if running with -r
403 good.sort()
404 bad.sort()
405 skipped.sort()
406
407 if good and not quiet:
408 if not bad and not skipped and len(good) > 1:
409 print "All",
410 print count(len(good), "test"), "OK."
411 if print_slow:
412 test_times.sort(reverse=True)
413 print "10 slowest tests:"
414 for time, test in test_times[:10]:
415 print "%s: %.1fs" % (test, time)
416 if bad:
417 print count(len(bad), "test"), "failed:"
418 printlist(bad)
419 if skipped and not quiet:
420 print count(len(skipped), "test"), "skipped:"
421 printlist(skipped)
422
423 e = _ExpectedSkips()
424 plat = sys.platform
425 if e.isvalid():
426 surprise = set(skipped) - e.getexpected() - set(resource_denieds)
427 if surprise:
428 print count(len(surprise), "skip"), \
429 "unexpected on", plat + ":"
430 printlist(surprise)
431 else:
432 print "Those skips are all expected on", plat + "."
433 else:
434 print "Ask someone to teach regrtest.py about which tests are"
435 print "expected to get skipped on", plat + "."
436
437 if verbose2 and bad:
438 print "Re-running failed tests in verbose mode"
439 for test in bad:
440 print "Re-running test %r in verbose mode" % test
441 sys.stdout.flush()
442 try:
443 test_support.verbose = True
444 ok = runtest(test, True, quiet, test_times, testdir,
445 huntrleaks)
446 except KeyboardInterrupt:
447 # print a newline separate from the ^C
448 print
449 break
450 except:
451 raise
452
453 if single:
454 alltests = findtests(testdir, stdtests, nottests)
455 for i in range(len(alltests)):
456 if tests[0] == alltests[i]:
457 if i == len(alltests) - 1:
458 os.unlink(filename)
459 else:
460 fp = open(filename, 'w')
461 fp.write(alltests[i+1] + '\n')
462 fp.close()
463 break
464 else:
465 os.unlink(filename)
466
467 if trace:
468 r = tracer.results()
469 r.write_results(show_missing=True, summary=True, coverdir=coverdir)
470
471 if runleaks:
472 os.system("leaks %d" % os.getpid())
473
474 sys.exit(len(bad) > 0)
475
476
477STDTESTS = [
478 'test_grammar',
479 'test_opcodes',
480 'test_dict',
481 'test_builtin',
482 'test_exceptions',
483 'test_types',
484 'test_unittest',
485 'test_doctest',
486 'test_doctest2',
487 ]
488
489NOTTESTS = [
490 'test_support',
491 'test_future1',
492 'test_future2',
493 ]
494
495def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
496 """Return a list of all applicable test modules."""
497 if not testdir: testdir = findtestdir()
498 names = os.listdir(testdir)
499 tests = []
500 for name in names:
501 if name[:5] == "test_" and name[-3:] == os.extsep+"py":
502 modname = name[:-3]
503 if modname not in stdtests and modname not in nottests:
504 tests.append(modname)
505 tests.sort()
506 return stdtests + tests
507
508def runtest(test, verbose, quiet, test_times,
509 testdir=None, huntrleaks=False):
510 """Run a single test.
511
512 test -- the name of the test
513 verbose -- if true, print more messages
514 quiet -- if true, don't print 'skipped' messages (probably redundant)
515 test_times -- a list of (time, test_name) pairs
516 testdir -- test directory
517 huntrleaks -- run multiple times to test for leaks; requires a debug
518 build; a triple corresponding to -R's three arguments
519 Return:
520 -2 test skipped because resource denied
521 -1 test skipped for some other reason
522 0 test failed
523 1 test passed
524 """
525
526 try:
527 return runtest_inner(test, verbose, quiet, test_times,
528 testdir, huntrleaks)
529 finally:
530 cleanup_test_droppings(test, verbose)
531
532def runtest_inner(test, verbose, quiet, test_times,
533 testdir=None, huntrleaks=False):
534 test_support.unload(test)
535 if not testdir:
536 testdir = findtestdir()
537 if verbose:
538 capture_stdout = None
539 else:
540 capture_stdout = cStringIO.StringIO()
541
542 try:
543 save_stdout = sys.stdout
544 try:
545 if capture_stdout:
546 sys.stdout = capture_stdout
547 if test.startswith('test.'):
548 abstest = test
549 else:
550 # Always import it from the test package
551 abstest = 'test.' + test
552 start_time = time.time()
553 the_package = __import__(abstest, globals(), locals(), [])
554 the_module = getattr(the_package, test)
555 # Old tests run to completion simply as a side-effect of
556 # being imported. For tests based on unittest or doctest,
557 # explicitly invoke their test_main() function (if it exists).
558 indirect_test = getattr(the_module, "test_main", None)
559 if indirect_test is not None:
560 indirect_test()
561 if huntrleaks:
562 dash_R(the_module, test, indirect_test, huntrleaks)
563 test_time = time.time() - start_time
564 test_times.append((test_time, test))
565 finally:
566 sys.stdout = save_stdout
567 except test_support.ResourceDenied, msg:
568 if not quiet:
569 print test, "skipped --", msg
570 sys.stdout.flush()
571 return -2
572 except (ImportError, test_support.TestSkipped), msg:
573 if not quiet:
574 print test, "skipped --", msg
575 sys.stdout.flush()
576 return -1
577 except KeyboardInterrupt:
578 raise
579 except test_support.TestFailed, msg:
580 print "test", test, "failed --", msg
581 sys.stdout.flush()
582 return 0
583 except:
584 type, value = sys.exc_info()[:2]
585 print "test", test, "crashed --", str(type) + ":", value
586 sys.stdout.flush()
587 if verbose:
588 traceback.print_exc(file=sys.stdout)
589 sys.stdout.flush()
590 return 0
591 else:
592 # Except in verbose mode, tests should not print anything
593 if verbose or huntrleaks:
594 return 1
595 output = capture_stdout.getvalue()
596 if not output:
597 return 1
598 print "test", test, "produced unexpected output:"
599 print "*" * 70
600 print output
601 print "*" * 70
602 sys.stdout.flush()
603 return 0
604
605def cleanup_test_droppings(testname, verbose):
606 import shutil
607
608 # Try to clean up junk commonly left behind. While tests shouldn't leave
609 # any files or directories behind, when a test fails that can be tedious
610 # for it to arrange. The consequences can be especially nasty on Windows,
611 # since if a test leaves a file open, it cannot be deleted by name (while
612 # there's nothing we can do about that here either, we can display the
613 # name of the offending test, which is a real help).
614 for name in (test_support.TESTFN,
615 "db_home",
616 ):
617 if not os.path.exists(name):
618 continue
619
620 if os.path.isdir(name):
621 kind, nuker = "directory", shutil.rmtree
622 elif os.path.isfile(name):
623 kind, nuker = "file", os.unlink
624 else:
625 raise SystemError("os.path says %r exists but is neither "
626 "directory nor file" % name)
627
628 if verbose:
629 print "%r left behind %s %r" % (testname, kind, name)
630 try:
631 nuker(name)
632 except Exception, msg:
633 print >> sys.stderr, ("%r left behind %s %r and it couldn't be "
634 "removed: %s" % (testname, kind, name, msg))
635
636def dash_R(the_module, test, indirect_test, huntrleaks):
637 # This code is hackish and inelegant, but it seems to do the job.
638 import copy_reg, _abcoll, io
639
640 if not hasattr(sys, 'gettotalrefcount'):
641 raise Exception("Tracking reference leaks requires a debug build "
642 "of Python")
643
644 # Save current values for dash_R_cleanup() to restore.
645 fs = warnings.filters[:]
646 ps = copy_reg.dispatch_table.copy()
647 pic = sys.path_importer_cache.copy()
648 abcs = {}
649 modules = _abcoll, io
650 for abc in [getattr(mod, a) for mod in modules for a in mod.__all__]:
651 # XXX isinstance(abc, ABCMeta) leads to infinite recursion
652 if not hasattr(abc, '_abc_registry'):
653 continue
654 for obj in abc.__subclasses__() + [abc]:
655 abcs[obj] = obj._abc_registry.copy()
656
657 if indirect_test:
658 def run_the_test():
659 indirect_test()
660 else:
661 def run_the_test():
662 reload(the_module)
663
664 deltas = []
665 nwarmup, ntracked, fname = huntrleaks
666 repcount = nwarmup + ntracked
667 print >> sys.stderr, "beginning", repcount, "repetitions"
668 print >> sys.stderr, ("1234567890"*(repcount//10 + 1))[:repcount]
669 dash_R_cleanup(fs, ps, pic, abcs)
670 for i in range(repcount):
671 rc = sys.gettotalrefcount()
672 run_the_test()
673 sys.stderr.write('.')
674 dash_R_cleanup(fs, ps, pic, abcs)
675 if i >= nwarmup:
676 deltas.append(sys.gettotalrefcount() - rc - 2)
677 print >> sys.stderr
678 if any(deltas):
679 msg = '%s leaked %s references, sum=%s' % (test, deltas, sum(deltas))
680 print >> sys.stderr, msg
681 refrep = open(fname, "a")
682 print >> refrep, msg
683 refrep.close()
684
685def dash_R_cleanup(fs, ps, pic, abcs):
686 import gc, copy_reg
687 import _strptime, linecache
688 dircache = test_support.import_module('dircache', deprecated=True)
689 import urlparse, urllib, urllib2, mimetypes, doctest
690 import struct, filecmp
691 from distutils.dir_util import _path_created
692
693 # Clear the warnings registry, so they can be displayed again
694 for mod in sys.modules.values():
695 if hasattr(mod, '__warningregistry__'):
696 del mod.__warningregistry__
697
698 # Restore some original values.
699 warnings.filters[:] = fs
700 copy_reg.dispatch_table.clear()
701 copy_reg.dispatch_table.update(ps)
702 sys.path_importer_cache.clear()
703 sys.path_importer_cache.update(pic)
704
705 # clear type cache
706 sys._clear_type_cache()
707
708 # Clear ABC registries, restoring previously saved ABC registries.
709 for abc, registry in abcs.items():
710 abc._abc_registry = registry.copy()
711 abc._abc_cache.clear()
712 abc._abc_negative_cache.clear()
713
714 # Clear assorted module caches.
715 _path_created.clear()
716 re.purge()
717 _strptime._regex_cache.clear()
718 urlparse.clear_cache()
719 urllib.urlcleanup()
720 urllib2.install_opener(None)
721 dircache.reset()
722 linecache.clearcache()
723 mimetypes._default_mime_types()
724 filecmp._cache.clear()
725 struct._clearcache()
726 doctest.master = None
727
728 # Collect cyclic trash.
729 gc.collect()
730
731def findtestdir():
732 if __name__ == '__main__':
733 file = sys.argv[0]
734 else:
735 file = __file__
736 testdir = os.path.dirname(file) or os.curdir
737 return testdir
738
739def removepy(name):
740 if name.endswith(os.extsep + "py"):
741 name = name[:-3]
742 return name
743
744def count(n, word):
745 if n == 1:
746 return "%d %s" % (n, word)
747 else:
748 return "%d %ss" % (n, word)
749
750def printlist(x, width=70, indent=4):
751 """Print the elements of iterable x to stdout.
752
753 Optional arg width (default 70) is the maximum line length.
754 Optional arg indent (default 4) is the number of blanks with which to
755 begin each line.
756 """
757
758 from textwrap import fill
759 blanks = ' ' * indent
760 print fill(' '.join(map(str, x)), width,
761 initial_indent=blanks, subsequent_indent=blanks)
762
763# Map sys.platform to a string containing the basenames of tests
764# expected to be skipped on that platform.
765#
766# Special cases:
767# test_pep277
768# The _ExpectedSkips constructor adds this to the set of expected
769# skips if not os.path.supports_unicode_filenames.
770# test_socket_ssl
771# Controlled by test_socket_ssl.skip_expected. Requires the network
772# resource, and a socket module with ssl support.
773# test_timeout
774# Controlled by test_timeout.skip_expected. Requires the network
775# resource and a socket module.
776#
777# Tests that are expected to be skipped everywhere except on one platform
778# are also handled separately.
779
780_expectations = {
781 'win32':
782 """
783 test__locale
784 test_bsddb185
785 test_bsddb3
786 test_commands
787 test_crypt
788 test_curses
789 test_dbm
790 test_dl
791 test_fcntl
792 test_fork1
793 test_epoll
794 test_gdbm
795 test_grp
796 test_ioctl
797 test_largefile
798 test_kqueue
799 test_mhlib
800 test_openpty
801 test_ossaudiodev
802 test_pipes
803 test_poll
804 test_posix
805 test_pty
806 test_pwd
807 test_resource
808 test_signal
809 test_threadsignals
810 test_timing
811 test_wait3
812 test_wait4
813 """,
814 'linux2':
815 """
816 test_bsddb185
817 test_curses
818 test_dl
819 test_largefile
820 test_kqueue
821 test_ossaudiodev
822 """,
823 'mac':
824 """
825 test_atexit
826 test_bsddb
827 test_bsddb185
828 test_bsddb3
829 test_bz2
830 test_commands
831 test_crypt
832 test_curses
833 test_dbm
834 test_dl
835 test_fcntl
836 test_fork1
837 test_epoll
838 test_grp
839 test_ioctl
840 test_largefile
841 test_locale
842 test_kqueue
843 test_mmap
844 test_openpty
845 test_ossaudiodev
846 test_poll
847 test_popen
848 test_popen2
849 test_posix
850 test_pty
851 test_pwd
852 test_resource
853 test_signal
854 test_sundry
855 test_tarfile
856 test_timing
857 """,
858 'unixware7':
859 """
860 test_bsddb
861 test_bsddb185
862 test_dl
863 test_epoll
864 test_largefile
865 test_kqueue
866 test_minidom
867 test_openpty
868 test_pyexpat
869 test_sax
870 test_sundry
871 """,
872 'openunix8':
873 """
874 test_bsddb
875 test_bsddb185
876 test_dl
877 test_epoll
878 test_largefile
879 test_kqueue
880 test_minidom
881 test_openpty
882 test_pyexpat
883 test_sax
884 test_sundry
885 """,
886 'sco_sv3':
887 """
888 test_asynchat
889 test_bsddb
890 test_bsddb185
891 test_dl
892 test_fork1
893 test_epoll
894 test_gettext
895 test_largefile
896 test_locale
897 test_kqueue
898 test_minidom
899 test_openpty
900 test_pyexpat
901 test_queue
902 test_sax
903 test_sundry
904 test_thread
905 test_threaded_import
906 test_threadedtempfile
907 test_threading
908 """,
909 'riscos':
910 """
911 test_asynchat
912 test_atexit
913 test_bsddb
914 test_bsddb185
915 test_bsddb3
916 test_commands
917 test_crypt
918 test_dbm
919 test_dl
920 test_fcntl
921 test_fork1
922 test_epoll
923 test_gdbm
924 test_grp
925 test_largefile
926 test_locale
927 test_kqueue
928 test_mmap
929 test_openpty
930 test_poll
931 test_popen2
932 test_pty
933 test_pwd
934 test_strop
935 test_sundry
936 test_thread
937 test_threaded_import
938 test_threadedtempfile
939 test_threading
940 test_timing
941 """,
942 'darwin':
943 """
944 test__locale
945 test_bsddb
946 test_bsddb3
947 test_curses
948 test_epoll
949 test_gdbm
950 test_largefile
951 test_locale
952 test_kqueue
953 test_minidom
954 test_ossaudiodev
955 test_poll
956 """,
957 'sunos5':
958 """
959 test_bsddb
960 test_bsddb185
961 test_curses
962 test_dbm
963 test_epoll
964 test_kqueue
965 test_gdbm
966 test_gzip
967 test_openpty
968 test_zipfile
969 test_zlib
970 """,
971 'hp-ux11':
972 """
973 test_bsddb
974 test_bsddb185
975 test_curses
976 test_dl
977 test_epoll
978 test_gdbm
979 test_gzip
980 test_largefile
981 test_locale
982 test_kqueue
983 test_minidom
984 test_openpty
985 test_pyexpat
986 test_sax
987 test_zipfile
988 test_zlib
989 """,
990 'atheos':
991 """
992 test_bsddb185
993 test_curses
994 test_dl
995 test_gdbm
996 test_epoll
997 test_largefile
998 test_locale
999 test_kqueue
1000 test_mhlib
1001 test_mmap
1002 test_poll
1003 test_popen2
1004 test_resource
1005 """,
1006 'cygwin':
1007 """
1008 test_bsddb185
1009 test_bsddb3
1010 test_curses
1011 test_dbm
1012 test_epoll
1013 test_ioctl
1014 test_kqueue
1015 test_largefile
1016 test_locale
1017 test_ossaudiodev
1018 test_socketserver
1019 """,
1020 'os2emx':
1021 """
1022 test_audioop
1023 test_bsddb185
1024 test_bsddb3
1025 test_commands
1026 test_curses
1027 test_dl
1028 test_epoll
1029 test_kqueue
1030 test_largefile
1031 test_mhlib
1032 test_mmap
1033 test_openpty
1034 test_ossaudiodev
1035 test_pty
1036 test_resource
1037 test_signal
1038 """,
1039 'freebsd4':
1040 """
1041 test_bsddb
1042 test_bsddb3
1043 test_epoll
1044 test_gdbm
1045 test_locale
1046 test_ossaudiodev
1047 test_pep277
1048 test_pty
1049 test_socket_ssl
1050 test_socketserver
1051 test_tcl
1052 test_timeout
1053 test_urllibnet
1054 test_multiprocessing
1055 """,
1056 'aix5':
1057 """
1058 test_bsddb
1059 test_bsddb185
1060 test_bsddb3
1061 test_bz2
1062 test_dl
1063 test_epoll
1064 test_gdbm
1065 test_gzip
1066 test_kqueue
1067 test_ossaudiodev
1068 test_tcl
1069 test_zipimport
1070 test_zlib
1071 """,
1072 'openbsd3':
1073 """
1074 test_bsddb
1075 test_bsddb3
1076 test_ctypes
1077 test_dl
1078 test_epoll
1079 test_gdbm
1080 test_locale
1081 test_normalization
1082 test_ossaudiodev
1083 test_pep277
1084 test_tcl
1085 test_multiprocessing
1086 """,
1087 'netbsd3':
1088 """
1089 test_bsddb
1090 test_bsddb185
1091 test_bsddb3
1092 test_ctypes
1093 test_curses
1094 test_dl
1095 test_epoll
1096 test_gdbm
1097 test_locale
1098 test_ossaudiodev
1099 test_pep277
1100 test_tcl
1101 test_multiprocessing
1102 """,
1103}
1104_expectations['freebsd5'] = _expectations['freebsd4']
1105_expectations['freebsd6'] = _expectations['freebsd4']
1106_expectations['freebsd7'] = _expectations['freebsd4']
1107_expectations['freebsd8'] = _expectations['freebsd4']
1108
1109class _ExpectedSkips:
1110 def __init__(self):
1111 import os.path
1112 from test import test_timeout
1113
1114 self.valid = False
1115 if sys.platform in _expectations:
1116 s = _expectations[sys.platform]
1117 self.expected = set(s.split())
1118
1119 # expected to be skipped on every platform, even Linux
1120 self.expected.add('test_linuxaudiodev')
1121
1122 if not os.path.supports_unicode_filenames:
1123 self.expected.add('test_pep277')
1124
1125 try:
1126 from test import test_socket_ssl
1127 except ImportError:
1128 pass
1129 else:
1130 if test_socket_ssl.skip_expected:
1131 self.expected.add('test_socket_ssl')
1132
1133 if test_timeout.skip_expected:
1134 self.expected.add('test_timeout')
1135
1136 if sys.maxint == 9223372036854775807L:
1137 self.expected.add('test_imageop')
1138
1139 if not sys.platform in ("mac", "darwin"):
1140 MAC_ONLY = ["test_macos", "test_macostools", "test_aepack",
1141 "test_plistlib", "test_scriptpackages",
1142 "test_applesingle"]
1143 for skip in MAC_ONLY:
1144 self.expected.add(skip)
1145 elif len(u'\0'.encode('unicode-internal')) == 4:
1146 self.expected.add("test_macostools")
1147
1148
1149 if sys.platform != "win32":
1150 # test_sqlite is only reliable on Windows where the library
1151 # is distributed with Python
1152 WIN_ONLY = ["test_unicode_file", "test_winreg",
1153 "test_winsound", "test_startfile",
1154 "test_sqlite"]
1155 for skip in WIN_ONLY:
1156 self.expected.add(skip)
1157
1158 if sys.platform != 'irix':
1159 IRIX_ONLY = ["test_imageop", "test_al", "test_cd", "test_cl",
1160 "test_gl", "test_imgfile"]
1161 for skip in IRIX_ONLY:
1162 self.expected.add(skip)
1163
1164 if sys.platform != 'sunos5':
1165 self.expected.add('test_sunaudiodev')
1166 self.expected.add('test_nis')
1167
1168 if not sys.py3kwarning:
1169 self.expected.add('test_py3kwarn')
1170
1171 self.valid = True
1172
1173 def isvalid(self):
1174 "Return true iff _ExpectedSkips knows about the current platform."
1175 return self.valid
1176
1177 def getexpected(self):
1178 """Return set of test names we expect to skip on current platform.
1179
1180 self.isvalid() must be true.
1181 """
1182
1183 assert self.isvalid()
1184 return self.expected
1185
1186if __name__ == '__main__':
1187 # Remove regrtest.py's own directory from the module search path. This
1188 # prevents relative imports from working, and relative imports will screw
1189 # up the testing framework. E.g. if both test.test_support and
1190 # test_support are imported, they will not contain the same globals, and
1191 # much of the testing framework relies on the globals in the
1192 # test.test_support module.
1193 mydir = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
1194 i = len(sys.path)
1195 while i >= 0:
1196 i -= 1
1197 if os.path.abspath(os.path.normpath(sys.path[i])) == mydir:
1198 del sys.path[i]
1199 main()
Note: See TracBrowser for help on using the repository browser.