6 python -m test.regrtest [options] [test_name1 [test_name2 ...]]
7 python path/to/Lib/test/regrtest.py [options] [test_name1 [test_name2 ...]]
10 If no arguments or options are provided, finds all files matching
11 the pattern "test_*" in the Lib/test subdirectory and runs
12 them in alphabetical order (but see -M and -u, below, for exceptions).
14 For more rigorous testing, it is useful to use the following
17 python -E -tt -Wd -3 -m test.regrtest [options] [test_name1 ...]
22 -h/--help -- print this text and exit
26 -v/--verbose -- run tests in verbose mode with output to stdout
27 -w/--verbose2 -- re-run failed tests in verbose mode
28 -W/--verbose3 -- re-run failed tests in verbose mode immediately
29 -q/--quiet -- no output unless one or more tests fail
30 -S/--slow -- print the slowest 10 tests
31 --header -- print header with interpreter info
35 -r/--random -- randomize test execution order (see below)
36 --randseed -- pass a random seed to reproduce a previous random run
37 -f/--fromfile -- read names of tests to run from a file (see below)
38 -x/--exclude -- arguments are tests to *exclude*
39 -s/--single -- single step through a set of tests (see below)
40 -u/--use RES1,RES2,...
41 -- specify which special resource intensive tests to run
43 -- run very large memory-consuming tests
47 -l/--findleaks -- if GC is available detect tests that leak memory
48 -L/--runleaks -- run the leaks(1) command just before exit
49 -R/--huntrleaks RUNCOUNTS
50 -- search for reference leaks (needs debug build, v. slow)
51 -j/--multiprocess PROCESSES
52 -- run PROCESSES processes at once
53 -T/--coverage -- turn on code coverage tracing using the trace module
54 -D/--coverdir DIRECTORY
55 -- Directory where coverage files are put
56 -N/--nocoverdir -- Put coverage files alongside modules
57 -t/--threshold THRESHOLD
58 -- call gc.set_threshold(THRESHOLD)
59 -F/--forever -- run the specified tests in a loop, until an error happens
62 Additional Option Details:
64 -r randomizes test execution order. You can use --randseed=int to provide a
65 int seed value for the randomizer; this is useful for reproducing troublesome
68 -s On the first invocation of regrtest using -s, the first test file found
69 or the first test file given on the command line is run, and the name of
70 the next test is recorded in a file named pynexttest. If run from the
71 Python build directory, pynexttest is located in the 'build' subdirectory,
72 otherwise it is located in tempfile.gettempdir(). On subsequent runs,
73 the test in pynexttest is run, and the next test is written to pynexttest.
74 When the last test has been run, pynexttest is deleted. In this way it
75 is possible to single step through the test files. This is useful when
76 doing memory analysis on the Python interpreter, which process tends to
77 consume too many resources to run the full regression test non-stop.
79 -f reads the names of tests from the file given as f's argument, one
80 or more test names per line. Whitespace is ignored. Blank lines and
81 lines beginning with '#' are ignored. This is especially useful for
82 whittling down failures involving interactions among tests.
84 -L causes the leaks(1) command to be run just before exit if it exists.
85 leaks(1) is available on Mac OS X and presumably on some other
86 FreeBSD-derived systems.
88 -R runs each test several times and examines sys.gettotalrefcount() to
89 see if the test appears to be leaking references. The argument should
90 be of the form stab:run:fname where 'stab' is the number of times the
91 test is run to let gettotalrefcount settle down, 'run' is the number
92 of times further it is run and 'fname' is the name of the file the
93 reports are written to. These parameters all have defaults (5, 4 and
94 "reflog.txt" respectively), and the minimal invocation is '-R :'.
96 -M runs tests that require an exorbitant amount of memory. These tests
97 typically try to ascertain containers keep working when containing more than
98 2 billion objects, which only works on 64-bit systems. There are also some
99 tests that try to exhaust the address space of the process, which only makes
100 sense on 32-bit systems with at least 2Gb of memory. The passed-in memlimit,
101 which is a string in the form of '2.5Gb', determines howmuch memory the
102 tests will limit themselves to (but they may go slightly over.) The number
103 shouldn't be more memory than the machine has (including swap memory). You
104 should also keep in mind that swap memory is generally much, much slower
105 than RAM, and setting memlimit to all available RAM or higher will heavily
106 tax the machine. On the other hand, it is no use running these tests with a
107 limit of less than 2.5Gb, and many require more than 20Gb. Tests that expect
108 to use more than memlimit memory will be skipped. The big-memory tests
109 generally run very, very long.
111 -u is used to specify which special resource intensive tests to run,
112 such as those requiring large file support or network connectivity.
113 The argument is a comma-separated list of words indicating the
114 resources to test. Currently only the following are defined:
116 all - Enable all special resources.
118 audio - Tests that use the audio device. (There are known
119 cases of broken audio drivers that can crash Python or
120 even the Linux kernel.)
122 curses - Tests that use curses and will modify the terminal's
123 state and output modes.
125 largefile - It is okay to run some test that may create huge
126 files. These tests can take a long time and may
127 consume >2GB of disk space temporarily.
129 network - It is okay to run tests that use external network
130 resource, e.g. testing SSL support for sockets.
132 bsddb - It is okay to run the bsddb testsuite, which takes
133 a long time to complete.
135 decimal - Test the decimal module against a large suite that
136 verifies compliance with standards.
138 cpu - Used for certain CPU-heavy tests.
140 subprocess Run all tests for the subprocess module.
142 urlfetch - It is okay to download files required on testing.
144 gui - Run tests that require a running GUI.
146 xpickle - Test pickle and cPickle against Python 2.4, 2.5 and 2.6 to
147 test backwards compatibility. These tests take a long time
150 To enable all resources except one, use '-uall,-<resource>'. For
151 example, to run all the tests except for the bsddb tests, give the
152 option '-uall,-bsddb'.
172 # Some times __path__ and __file__ are not absolute (e.g. while running from
173 # Lib/) and, if we change the CWD to run the tests in a temporary dir, some
174 # imports might fail. This affects only the modules imported before os.chdir().
175 # These modules are searched first in sys.path[0] (so '' -- the CWD) and if
176 # they are found in the CWD their __file__ and __path__ will be relative (this
177 # happens before the chdir). All the modules imported after the chdir, are
178 # not found in the CWD, and since the other paths in sys.path[1:] are absolute
179 # (site.py absolutize them), the __file__ and __path__ will be absolute too.
180 # Therefore it is necessary to absolutize manually the __file__ and __path__ of
181 # the packages to prevent later imports to fail when the CWD is different.
182 for module
in sys
.modules
.itervalues():
183 if hasattr(module
, '__path__'):
184 module
.__path
__ = [os
.path
.abspath(path
) for path
in module
.__path
__]
185 if hasattr(module
, '__file__'):
186 module
.__file
__ = os
.path
.abspath(module
.__file
__)
189 # MacOSX (a.k.a. Darwin) has a default stack size that is too small
190 # for deeply recursive regular expressions. We see this as crashes in
191 # the Python test suite when running test_re.py and test_sre.py. The
192 # fix is to set the stack limit to 2048.
193 # This approach may also be useful for other Unixy platforms that
194 # suffer from small default stack limits.
195 if sys
.platform
== 'darwin':
201 soft
, hard
= resource
.getrlimit(resource
.RLIMIT_STACK
)
202 newsoft
= min(hard
, max(soft
, 1024*2048))
203 resource
.setrlimit(resource
.RLIMIT_STACK
, (newsoft
, hard
))
205 # Test result constants.
213 from test
import test_support
215 RESOURCE_NAMES
= ('audio', 'curses', 'largefile', 'network', 'bsddb',
216 'decimal', 'cpu', 'subprocess', 'urlfetch', 'gui',
219 TEMPDIR
= os
.path
.abspath(tempfile
.gettempdir())
222 def usage(code
, msg
=''):
228 def main(tests
=None, testdir
=None, verbose
=0, quiet
=False,
229 exclude
=False, single
=False, randomize
=False, fromfile
=None,
230 findleaks
=False, use_resources
=None, trace
=False, coverdir
='coverage',
231 runleaks
=False, huntrleaks
=False, verbose2
=False, print_slow
=False,
232 random_seed
=None, use_mp
=None, verbose3
=False, forever
=False,
234 """Execute a test suite.
236 This also parses command-line options and modifies its behavior
239 tests -- a list of strings containing test names (optional)
240 testdir -- the directory in which to look for tests (optional)
242 Users other than the Python test suite will certainly want to
243 specify testdir; if it's omitted, the directory containing the
244 Python test suite is searched for.
246 If the tests argument is omitted, the tests listed on the
247 command-line will be used. If that's empty, too, then all *.py
248 files beginning with test_ will be used.
250 The other default arguments (verbose, quiet, exclude,
251 single, randomize, findleaks, use_resources, trace, coverdir,
252 print_slow, and random_seed) allow programmers calling main()
253 directly to set the values that would normally be set by flags
257 test_support
.record_original_stdout(sys
.stdout
)
259 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'hvqxsSrf:lu:t:TD:NLR:FwWM:j:',
260 ['help', 'verbose', 'verbose2', 'verbose3', 'quiet',
261 'exclude', 'single', 'slow', 'random', 'fromfile', 'findleaks',
262 'use=', 'threshold=', 'trace', 'coverdir=', 'nocoverdir',
263 'runleaks', 'huntrleaks=', 'memlimit=', 'randseed=',
264 'multiprocess=', 'slaveargs=', 'forever', 'header'])
265 except getopt
.error
, msg
:
269 if random_seed
is None:
270 random_seed
= random
.randrange(10000000)
271 if use_resources
is None:
274 if o
in ('-h', '--help'):
276 elif o
in ('-v', '--verbose'):
278 elif o
in ('-w', '--verbose2'):
280 elif o
in ('-W', '--verbose3'):
282 elif o
in ('-q', '--quiet'):
285 elif o
in ('-x', '--exclude'):
287 elif o
in ('-s', '--single'):
289 elif o
in ('-S', '--slow'):
291 elif o
in ('-r', '--randomize'):
293 elif o
== '--randseed':
295 elif o
in ('-f', '--fromfile'):
297 elif o
in ('-l', '--findleaks'):
299 elif o
in ('-L', '--runleaks'):
301 elif o
in ('-t', '--threshold'):
303 gc
.set_threshold(int(a
))
304 elif o
in ('-T', '--coverage'):
306 elif o
in ('-D', '--coverdir'):
307 coverdir
= os
.path
.join(os
.getcwd(), a
)
308 elif o
in ('-N', '--nocoverdir'):
310 elif o
in ('-R', '--huntrleaks'):
311 huntrleaks
= a
.split(':')
312 if len(huntrleaks
) not in (2, 3):
314 usage(2, '-R takes 2 or 3 colon-separated arguments')
315 if not huntrleaks
[0]:
318 huntrleaks
[0] = int(huntrleaks
[0])
319 if not huntrleaks
[1]:
322 huntrleaks
[1] = int(huntrleaks
[1])
323 if len(huntrleaks
) == 2 or not huntrleaks
[2]:
324 huntrleaks
[2:] = ["reflog.txt"]
325 elif o
in ('-M', '--memlimit'):
326 test_support
.set_memlimit(a
)
327 elif o
in ('-u', '--use'):
328 u
= [x
.lower() for x
in a
.split(',')]
331 use_resources
[:] = RESOURCE_NAMES
337 if r
not in RESOURCE_NAMES
:
338 usage(1, 'Invalid -u/--use option: ' + a
)
340 if r
in use_resources
:
341 use_resources
.remove(r
)
342 elif r
not in use_resources
:
343 use_resources
.append(r
)
344 elif o
in ('-F', '--forever'):
346 elif o
in ('-j', '--multiprocess'):
348 elif o
== '--header':
350 elif o
== '--slaveargs':
351 args
, kwargs
= json
.loads(a
)
353 result
= runtest(*args
, **kwargs
)
354 except BaseException
, e
:
355 result
= INTERRUPTED
, e
.__class
__.__name
__
356 print # Force a newline (just in case)
357 print json
.dumps(result
)
360 print >>sys
.stderr
, ("No handler for option {}. Please "
361 "report this as a bug at http://bugs.python.org.").format(o
)
363 if single
and fromfile
:
364 usage(2, "-s and -f don't go together!")
366 usage(2, "-T and -j don't go together!")
367 if use_mp
and findleaks
:
368 usage(2, "-l and -j don't go together!")
373 resource_denieds
= []
374 environment_changed
= []
381 print 'No GC available, disabling findleaks.'
384 # Uncomment the line below to report garbage that is not
385 # freeable by reference counting alone. By default only
386 # garbage that is not collectable by the GC is reported.
387 #gc.set_debug(gc.DEBUG_SAVEALL)
391 filename
= os
.path
.join(TEMPDIR
, 'pynexttest')
393 fp
= open(filename
, 'r')
394 next_test
= fp
.read().strip()
402 fp
= open(os
.path
.join(test_support
.SAVEDCWD
, fromfile
))
404 guts
= line
.split() # assuming no test has whitespace in its name
405 if guts
and not guts
[0].startswith('#'):
409 # Strip .py extensions.
413 stdtests
= STDTESTS
[:]
414 nottests
= NOTTESTS
.copy()
422 # For a partial run, we do not need to clutter the output.
423 if verbose
or header
or not (quiet
or single
or tests
or args
):
424 # Print basic platform information
425 print "==", platform
.python_implementation(), \
426 " ".join(sys
.version
.split())
427 print "== ", platform
.platform(aliased
=True), \
428 "%s-endian" % sys
.byteorder
429 print "== ", os
.getcwd()
430 print "Testing with flags:", sys
.flags
432 alltests
= findtests(testdir
, stdtests
, nottests
)
433 selected
= tests
or args
or alltests
435 selected
= selected
[:1]
437 next_single_test
= alltests
[alltests
.index(selected
[0])+1]
439 next_single_test
= None
441 random
.seed(random_seed
)
442 print "Using random seed", random_seed
443 random
.shuffle(selected
)
446 tracer
= trace
.Trace(ignoredirs
=[sys
.prefix
, sys
.exec_prefix
],
447 trace
=False, count
=True)
450 test_support
.use_resources
= use_resources
451 save_modules
= sys
.modules
.keys()
453 def accumulate_result(test
, result
):
454 ok
, test_time
= result
455 test_times
.append((test_time
, test
))
460 elif ok
== ENV_CHANGED
:
462 environment_changed
.append(test
)
465 elif ok
== RESOURCE_DENIED
:
467 resource_denieds
.append(test
)
470 def test_forever(tests
=list(selected
)):
476 tests
= test_forever()
478 tests
= iter(selected
)
482 from threading
import Thread
484 print "Multiprocess option requires thread support"
486 from Queue
import Queue
487 from subprocess
import Popen
, PIPE
488 debug_output_pat
= re
.compile(r
"\[\d+ refs\]$")
490 def tests_and_args():
493 (test
, verbose
, quiet
),
494 dict(huntrleaks
=huntrleaks
, use_resources
=use_resources
)
496 yield (test
, args_tuple
)
497 pending
= tests_and_args()
498 opt_args
= test_support
.args_from_interpreter_flags()
499 base_cmd
= [sys
.executable
] + opt_args
+ ['-m', 'test.regrtest']
505 test
, args_tuple
= next(pending
)
506 except StopIteration:
507 output
.put((None, None, None, None))
509 # -E is needed by some tests, e.g. test_import
510 popen
= Popen(base_cmd
+ ['--slaveargs', json
.dumps(args_tuple
)],
511 stdout
=PIPE
, stderr
=PIPE
,
512 universal_newlines
=True,
513 close_fds
=(os
.name
!= 'nt'))
514 stdout
, stderr
= popen
.communicate()
515 # Strip last refcount output line if it exists, since it
516 # comes from the shutdown of the interpreter in the subcommand.
517 stderr
= debug_output_pat
.sub("", stderr
)
518 stdout
, _
, result
= stdout
.strip().rpartition("\n")
520 output
.put((None, None, None, None))
522 result
= json
.loads(result
)
524 stdout
= test
+'\n'+stdout
525 output
.put((test
, stdout
.rstrip(), stderr
.rstrip(), result
))
526 except BaseException
:
527 output
.put((None, None, None, None))
529 workers
= [Thread(target
=work
) for i
in range(use_mp
)]
530 for worker
in workers
:
534 while finished
< use_mp
:
535 test
, stdout
, stderr
, result
= output
.get()
542 print >>sys
.stderr
, stderr
543 if result
[0] == INTERRUPTED
:
544 assert result
[1] == 'KeyboardInterrupt'
545 raise KeyboardInterrupt # What else?
546 accumulate_result(test
, result
)
547 except KeyboardInterrupt:
550 for worker
in workers
:
558 # If we're tracing code coverage, then we don't exit with status
559 # if on a false return value from main.
560 tracer
.runctx('runtest(test, verbose, quiet)',
561 globals=globals(), locals=vars())
564 result
= runtest(test
, verbose
, quiet
, huntrleaks
)
565 accumulate_result(test
, result
)
566 if verbose3
and result
[0] == FAILED
:
567 print "Re-running test %r in verbose mode" % test
568 runtest(test
, True, quiet
, huntrleaks
)
569 except KeyboardInterrupt:
577 print "Warning: test created", len(gc
.garbage
),
578 print "uncollectable object(s)."
579 # move the uncollectable objects somewhere so we don't see
581 found_garbage
.extend(gc
.garbage
)
583 # Unload the newly imported modules (best effort finalization)
584 for module
in sys
.modules
.keys():
585 if module
not in save_modules
and module
.startswith("test."):
586 test_support
.unload(module
)
589 # print a newline after ^C
591 print "Test suite interrupted by signal SIGINT."
592 omitted
= set(selected
) - set(good
) - set(bad
) - set(skipped
)
593 print count(len(omitted
), "test"), "omitted:"
595 if good
and not quiet
:
596 if not bad
and not skipped
and not interrupted
and len(good
) > 1:
598 print count(len(good
), "test"), "OK."
600 test_times
.sort(reverse
=True)
601 print "10 slowest tests:"
602 for time
, test
in test_times
[:10]:
603 print "%s: %.1fs" % (test
, time
)
605 bad
= set(bad
) - set(environment_changed
)
607 print count(len(bad
), "test"), "failed:"
609 if environment_changed
:
610 print "{} altered the execution environment:".format(
611 count(len(environment_changed
), "test"))
612 printlist(environment_changed
)
613 if skipped
and not quiet
:
614 print count(len(skipped
), "test"), "skipped:"
620 surprise
= set(skipped
) - e
.getexpected() - set(resource_denieds
)
622 print count(len(surprise
), "skip"), \
623 "unexpected on", plat
+ ":"
626 print "Those skips are all expected on", plat
+ "."
628 print "Ask someone to teach regrtest.py about which tests are"
629 print "expected to get skipped on", plat
+ "."
632 print "Re-running failed tests in verbose mode"
634 print "Re-running test %r in verbose mode" % test
637 test_support
.verbose
= True
638 ok
= runtest(test
, True, quiet
, huntrleaks
)
639 except KeyboardInterrupt:
640 # print a newline separate from the ^C
648 with
open(filename
, 'w') as fp
:
649 fp
.write(next_single_test
+ '\n')
655 r
.write_results(show_missing
=True, summary
=True, coverdir
=coverdir
)
658 os
.system("leaks %d" % os
.getpid())
660 sys
.exit(len(bad
) > 0 or interrupted
)
681 def findtests(testdir
=None, stdtests
=STDTESTS
, nottests
=NOTTESTS
):
682 """Return a list of all applicable test modules."""
683 testdir
= findtestdir(testdir
)
684 names
= os
.listdir(testdir
)
686 others
= set(stdtests
) | nottests
688 modname
, ext
= os
.path
.splitext(name
)
689 if modname
[:5] == "test_" and ext
== ".py" and modname
not in others
:
690 tests
.append(modname
)
691 return stdtests
+ sorted(tests
)
693 def runtest(test
, verbose
, quiet
,
694 huntrleaks
=False, use_resources
=None):
695 """Run a single test.
697 test -- the name of the test
698 verbose -- if true, print more messages
699 quiet -- if true, don't print 'skipped' messages (probably redundant)
700 test_times -- a list of (time, test_name) pairs
701 huntrleaks -- run multiple times to test for leaks; requires a debug
702 build; a triple corresponding to -R's three arguments
703 Returns one of the test result constants:
704 INTERRUPTED KeyboardInterrupt when run under -j
705 RESOURCE_DENIED test skipped because resource denied
706 SKIPPED test skipped for some other reason
707 ENV_CHANGED test failed because it changed the execution environment
712 test_support
.verbose
= verbose
# Tell tests to be moderately quiet
713 if use_resources
is not None:
714 test_support
.use_resources
= use_resources
716 return runtest_inner(test
, verbose
, quiet
, huntrleaks
)
718 cleanup_test_droppings(test
, verbose
)
721 # Unit tests are supposed to leave the execution environment unchanged
722 # once they complete. But sometimes tests have bugs, especially when
723 # tests fail, and the changes to environment go on to mess up other
724 # tests. This can cause issues with buildbot stability, since tests
725 # are run in random order and so problems may appear to come and go.
726 # There are a few things we can save and restore to mitigate this, and
727 # the following context manager handles this task.
729 class saved_test_environment
:
730 """Save bits of the test environment and restore them at block exit.
732 with saved_test_environment(testname, verbose, quiet):
735 Unless quiet is True, a warning is printed to stderr if any of
736 the saved items was changed by the test. The attribute 'changed'
737 is initially False, but is set to True if a change is detected.
739 If verbose is more than 1, the before and after state of changed
740 items is also printed.
745 def __init__(self
, testname
, verbose
=0, quiet
=False):
746 self
.testname
= testname
747 self
.verbose
= verbose
750 # To add things to save and restore, add a name XXX to the resources list
751 # and add corresponding get_XXX/restore_XXX functions. get_XXX should
752 # return the value to be saved and compared against a second call to the
753 # get function when test execution completes. restore_XXX should accept
754 # the saved value and restore the resource using it. It will be called if
755 # and only if a change in the value is detected.
757 # Note: XXX will have any '.' replaced with '_' characters when determining
758 # the corresponding method names.
760 resources
= ('sys.argv', 'cwd', 'sys.stdin', 'sys.stdout', 'sys.stderr',
761 'os.environ', 'sys.path', 'asyncore.socket_map')
763 def get_sys_argv(self
):
764 return id(sys
.argv
), sys
.argv
, sys
.argv
[:]
765 def restore_sys_argv(self
, saved_argv
):
766 sys
.argv
= saved_argv
[1]
767 sys
.argv
[:] = saved_argv
[2]
771 def restore_cwd(self
, saved_cwd
):
774 def get_sys_stdout(self
):
776 def restore_sys_stdout(self
, saved_stdout
):
777 sys
.stdout
= saved_stdout
779 def get_sys_stderr(self
):
781 def restore_sys_stderr(self
, saved_stderr
):
782 sys
.stderr
= saved_stderr
784 def get_sys_stdin(self
):
786 def restore_sys_stdin(self
, saved_stdin
):
787 sys
.stdin
= saved_stdin
789 def get_os_environ(self
):
790 return id(os
.environ
), os
.environ
, dict(os
.environ
)
791 def restore_os_environ(self
, saved_environ
):
792 os
.environ
= saved_environ
[1]
794 os
.environ
.update(saved_environ
[2])
796 def get_sys_path(self
):
797 return id(sys
.path
), sys
.path
, sys
.path
[:]
798 def restore_sys_path(self
, saved_path
):
799 sys
.path
= saved_path
[1]
800 sys
.path
[:] = saved_path
[2]
802 def get_asyncore_socket_map(self
):
803 asyncore
= sys
.modules
.get('asyncore')
804 # XXX Making a copy keeps objects alive until __exit__ gets called.
805 return asyncore
and asyncore
.socket_map
.copy() or {}
806 def restore_asyncore_socket_map(self
, saved_map
):
807 asyncore
= sys
.modules
.get('asyncore')
808 if asyncore
is not None:
809 asyncore
.close_all(ignore_all
=True)
810 asyncore
.socket_map
.update(saved_map
)
812 def resource_info(self
):
813 for name
in self
.resources
:
814 method_suffix
= name
.replace('.', '_')
815 get_name
= 'get_' + method_suffix
816 restore_name
= 'restore_' + method_suffix
817 yield name
, getattr(self
, get_name
), getattr(self
, restore_name
)
820 self
.saved_values
= dict((name
, get()) for name
, get
, restore
821 in self
.resource_info())
824 def __exit__(self
, exc_type
, exc_val
, exc_tb
):
825 saved_values
= self
.saved_values
826 del self
.saved_values
827 for name
, get
, restore
in self
.resource_info():
829 original
= saved_values
.pop(name
)
830 # Check for changes to the resource's value
831 if current
!= original
:
835 print >>sys
.stderr
, (
836 "Warning -- {} was modified by {}".format(
837 name
, self
.testname
))
839 print >>sys
.stderr
, (
840 " Before: {}\n After: {} ".format(
842 # XXX (ncoghlan): for most resources (e.g. sys.path) identity
843 # matters at least as much as value. For others (e.g. cwd),
844 # identity is irrelevant. Should we add a mechanism to check
845 # for substitution in the cases where it matters?
849 def runtest_inner(test
, verbose
, quiet
, huntrleaks
=False):
850 test_support
.unload(test
)
852 capture_stdout
= None
854 capture_stdout
= StringIO
.StringIO()
857 refleak
= False # True if the test leaked references.
859 save_stdout
= sys
.stdout
862 sys
.stdout
= capture_stdout
863 if test
.startswith('test.'):
866 # Always import it from the test package
867 abstest
= 'test.' + test
868 with
saved_test_environment(test
, verbose
, quiet
) as environment
:
869 start_time
= time
.time()
870 the_package
= __import__(abstest
, globals(), locals(), [])
871 the_module
= getattr(the_package
, test
)
872 # Old tests run to completion simply as a side-effect of
873 # being imported. For tests based on unittest or doctest,
874 # explicitly invoke their test_main() function (if it exists).
875 indirect_test
= getattr(the_module
, "test_main", None)
876 if indirect_test
is not None:
879 refleak
= dash_R(the_module
, test
, indirect_test
,
881 test_time
= time
.time() - start_time
883 sys
.stdout
= save_stdout
884 except test_support
.ResourceDenied
, msg
:
886 print test
, "skipped --", msg
888 return RESOURCE_DENIED
, test_time
889 except unittest
.SkipTest
, msg
:
891 print test
, "skipped --", msg
893 return SKIPPED
, test_time
894 except KeyboardInterrupt:
896 except test_support
.TestFailed
, msg
:
897 print >>sys
.stderr
, "test", test
, "failed --", msg
899 return FAILED
, test_time
901 type, value
= sys
.exc_info()[:2]
902 print >>sys
.stderr
, "test", test
, "crashed --", str(type) + ":", value
905 traceback
.print_exc(file=sys
.stderr
)
907 return FAILED
, test_time
910 return FAILED
, test_time
911 if environment
.changed
:
912 return ENV_CHANGED
, test_time
913 # Except in verbose mode, tests should not print anything
914 if verbose
or huntrleaks
:
915 return PASSED
, test_time
916 output
= capture_stdout
.getvalue()
918 return PASSED
, test_time
919 print "test", test
, "produced unexpected output:"
924 return FAILED
, test_time
926 def cleanup_test_droppings(testname
, verbose
):
931 # First kill any dangling references to open files etc.
934 # Try to clean up junk commonly left behind. While tests shouldn't leave
935 # any files or directories behind, when a test fails that can be tedious
936 # for it to arrange. The consequences can be especially nasty on Windows,
937 # since if a test leaves a file open, it cannot be deleted by name (while
938 # there's nothing we can do about that here either, we can display the
939 # name of the offending test, which is a real help).
940 for name
in (test_support
.TESTFN
,
943 if not os
.path
.exists(name
):
946 if os
.path
.isdir(name
):
947 kind
, nuker
= "directory", shutil
.rmtree
948 elif os
.path
.isfile(name
):
949 kind
, nuker
= "file", os
.unlink
951 raise SystemError("os.path says %r exists but is neither "
952 "directory nor file" % name
)
955 print "%r left behind %s %r" % (testname
, kind
, name
)
957 # if we have chmod, fix possible permissions problems
958 # that might prevent cleanup
959 if (hasattr(os
, 'chmod')):
960 os
.chmod(name
, stat
.S_IRWXU | stat
.S_IRWXG | stat
.S_IRWXO
)
962 except Exception, msg
:
963 print >> sys
.stderr
, ("%r left behind %s %r and it couldn't be "
964 "removed: %s" % (testname
, kind
, name
, msg
))
966 def dash_R(the_module
, test
, indirect_test
, huntrleaks
):
967 """Run a test multiple times, looking for reference leaks.
970 False if the test didn't leak references; True if we detected refleaks.
972 # This code is hackish and inelegant, but it seems to do the job.
973 import copy_reg
, _abcoll
, _pyio
975 if not hasattr(sys
, 'gettotalrefcount'):
976 raise Exception("Tracking reference leaks requires a debug build "
979 # Save current values for dash_R_cleanup() to restore.
980 fs
= warnings
.filters
[:]
981 ps
= copy_reg
.dispatch_table
.copy()
982 pic
= sys
.path_importer_cache
.copy()
986 zdc
= None # Run unmodified on platforms without zipimport support
988 zdc
= zipimport
._zip
_directory
_cache
.copy()
990 modules
= _abcoll
, _pyio
991 for abc
in [getattr(mod
, a
) for mod
in modules
for a
in mod
.__all
__]:
992 # XXX isinstance(abc, ABCMeta) leads to infinite recursion
993 if not hasattr(abc
, '_abc_registry'):
995 for obj
in abc
.__subclasses
__() + [abc
]:
996 abcs
[obj
] = obj
._abc
_registry
.copy()
1003 imp
.reload(the_module
)
1006 nwarmup
, ntracked
, fname
= huntrleaks
1007 fname
= os
.path
.join(test_support
.SAVEDCWD
, fname
)
1008 repcount
= nwarmup
+ ntracked
1009 print >> sys
.stderr
, "beginning", repcount
, "repetitions"
1010 print >> sys
.stderr
, ("1234567890"*(repcount
//10 + 1))[:repcount
]
1011 dash_R_cleanup(fs
, ps
, pic
, zdc
, abcs
)
1012 for i
in range(repcount
):
1013 rc_before
= sys
.gettotalrefcount()
1015 sys
.stderr
.write('.')
1016 dash_R_cleanup(fs
, ps
, pic
, zdc
, abcs
)
1017 rc_after
= sys
.gettotalrefcount()
1019 deltas
.append(rc_after
- rc_before
)
1022 msg
= '%s leaked %s references, sum=%s' % (test
, deltas
, sum(deltas
))
1023 print >> sys
.stderr
, msg
1024 with
open(fname
, "a") as refrep
:
1025 print >> refrep
, msg
1030 def dash_R_cleanup(fs
, ps
, pic
, zdc
, abcs
):
1032 import _strptime
, linecache
1033 dircache
= test_support
.import_module('dircache', deprecated
=True)
1034 import urlparse
, urllib
, urllib2
, mimetypes
, doctest
1035 import struct
, filecmp
1036 from distutils
.dir_util
import _path_created
1038 # Clear the warnings registry, so they can be displayed again
1039 for mod
in sys
.modules
.values():
1040 if hasattr(mod
, '__warningregistry__'):
1041 del mod
.__warningregistry
__
1043 # Restore some original values.
1044 warnings
.filters
[:] = fs
1045 copy_reg
.dispatch_table
.clear()
1046 copy_reg
.dispatch_table
.update(ps
)
1047 sys
.path_importer_cache
.clear()
1048 sys
.path_importer_cache
.update(pic
)
1052 pass # Run unmodified on platforms without zipimport support
1054 zipimport
._zip
_directory
_cache
.clear()
1055 zipimport
._zip
_directory
_cache
.update(zdc
)
1058 sys
._clear
_type
_cache
()
1060 # Clear ABC registries, restoring previously saved ABC registries.
1061 for abc
, registry
in abcs
.items():
1062 abc
._abc
_registry
= registry
.copy()
1063 abc
._abc
_cache
.clear()
1064 abc
._abc
_negative
_cache
.clear()
1066 # Clear assorted module caches.
1067 _path_created
.clear()
1069 _strptime
._regex
_cache
.clear()
1070 urlparse
.clear_cache()
1072 urllib2
.install_opener(None)
1074 linecache
.clearcache()
1075 mimetypes
._default
_mime
_types
()
1076 filecmp
._cache
.clear()
1077 struct
._clearcache
()
1078 doctest
.master
= None
1080 # Collect cyclic trash.
1083 def findtestdir(path
=None):
1084 return path
or os
.path
.dirname(__file__
) or os
.curdir
1086 def removepy(names
):
1089 for idx
, name
in enumerate(names
):
1090 basename
, ext
= os
.path
.splitext(name
)
1092 names
[idx
] = basename
1096 return "%d %s" % (n
, word
)
1098 return "%d %ss" % (n
, word
)
1100 def printlist(x
, width
=70, indent
=4):
1101 """Print the elements of iterable x to stdout.
1103 Optional arg width (default 70) is the maximum line length.
1104 Optional arg indent (default 4) is the number of blanks with which to
1108 from textwrap
import fill
1109 blanks
= ' ' * indent
1110 # Print the sorted list: 'x' may be a '--random' list or a set()
1111 print fill(' '.join(str(elt
) for elt
in sorted(x
)), width
,
1112 initial_indent
=blanks
, subsequent_indent
=blanks
)
1114 # Map sys.platform to a string containing the basenames of tests
1115 # expected to be skipped on that platform.
1119 # The _ExpectedSkips constructor adds this to the set of expected
1120 # skips if not os.path.supports_unicode_filenames.
1122 # Controlled by test_timeout.skip_expected. Requires the network
1123 # resource and a socket module.
1125 # Tests that are expected to be skipped everywhere except on one platform
1126 # are also handled separately.
1218 test_threaded_import
1219 test_threadedtempfile
1250 test_threaded_import
1251 test_threadedtempfile
1370 test_multiprocessing
1408 test_multiprocessing
1428 test_multiprocessing
1431 _expectations
['freebsd5'] = _expectations
['freebsd4']
1432 _expectations
['freebsd6'] = _expectations
['freebsd4']
1433 _expectations
['freebsd7'] = _expectations
['freebsd4']
1434 _expectations
['freebsd8'] = _expectations
['freebsd4']
1436 class _ExpectedSkips
:
1439 from test
import test_timeout
1442 if sys
.platform
in _expectations
:
1443 s
= _expectations
[sys
.platform
]
1444 self
.expected
= set(s
.split())
1446 # expected to be skipped on every platform, even Linux
1447 self
.expected
.add('test_linuxaudiodev')
1449 if not os
.path
.supports_unicode_filenames
:
1450 self
.expected
.add('test_pep277')
1452 if test_timeout
.skip_expected
:
1453 self
.expected
.add('test_timeout')
1455 if sys
.maxint
== 9223372036854775807L:
1456 self
.expected
.add('test_imageop')
1458 if sys
.platform
!= "darwin":
1459 MAC_ONLY
= ["test_macos", "test_macostools", "test_aepack",
1460 "test_plistlib", "test_scriptpackages",
1462 for skip
in MAC_ONLY
:
1463 self
.expected
.add(skip
)
1464 elif len(u
'\0'.encode('unicode-internal')) == 4:
1465 self
.expected
.add("test_macostools")
1468 if sys
.platform
!= "win32":
1469 # test_sqlite is only reliable on Windows where the library
1470 # is distributed with Python
1471 WIN_ONLY
= ["test_unicode_file", "test_winreg",
1472 "test_winsound", "test_startfile",
1473 "test_sqlite", "test_msilib"]
1474 for skip
in WIN_ONLY
:
1475 self
.expected
.add(skip
)
1477 if sys
.platform
!= 'irix':
1478 IRIX_ONLY
= ["test_imageop", "test_al", "test_cd", "test_cl",
1479 "test_gl", "test_imgfile"]
1480 for skip
in IRIX_ONLY
:
1481 self
.expected
.add(skip
)
1483 if sys
.platform
!= 'sunos5':
1484 self
.expected
.add('test_sunaudiodev')
1485 self
.expected
.add('test_nis')
1487 if not sys
.py3kwarning
:
1488 self
.expected
.add('test_py3kwarn')
1493 "Return true iff _ExpectedSkips knows about the current platform."
1496 def getexpected(self
):
1497 """Return set of test names we expect to skip on current platform.
1499 self.isvalid() must be true.
1502 assert self
.isvalid()
1503 return self
.expected
1505 if __name__
== '__main__':
1506 # findtestdir() gets the dirname out of __file__, so we have to make it
1507 # absolute before changing the working directory.
1508 # For example __file__ may be relative when running trace or profile.
1510 __file__
= os
.path
.abspath(__file__
)
1513 assert __file__
== os
.path
.abspath(sys
.argv
[0])
1515 # When tests are run from the Python build directory, it is best practice
1516 # to keep the test files in a subfolder. It eases the cleanup of leftover
1517 # files using command "make distclean".
1518 if sysconfig
.is_python_build():
1519 TEMPDIR
= os
.path
.join(sysconfig
.get_config_var('srcdir'), 'build')
1520 TEMPDIR
= os
.path
.abspath(TEMPDIR
)
1521 if not os
.path
.exists(TEMPDIR
):
1524 # Define a writable temp dir that will be used as cwd while running
1525 # the tests. The name of the dir includes the pid to allow parallel
1526 # testing (see the -j option).
1527 TESTCWD
= 'test_python_{}'.format(os
.getpid())
1529 TESTCWD
= os
.path
.join(TEMPDIR
, TESTCWD
)
1531 # Run the tests in a context manager that temporary changes the CWD to a
1532 # temporary and writable directory. If it's not possible to create or
1533 # change the CWD, the original CWD will be used. The original CWD is
1534 # available from test_support.SAVEDCWD.
1535 with test_support
.temp_cwd(TESTCWD
, quiet
=True):