Back to index

python3.2  3.2.2
Classes | Functions | Variables
test.regrtest Namespace Reference

Classes

class  saved_test_environment
class  _ExpectedSkips

Functions

def usage
def main
def findtests
def replace_stdout
def runtest
def runtest_inner
def cleanup_test_droppings
def dash_R
def dash_R_cleanup
def warm_char_cache
def findtestdir
def removepy
def count
def printlist
def _make_temp_dir_for_build

Variables

 threading = None
 multiprocessing = None
tuple newsoft = min(hard, max(soft, 1024*2048))
int PASSED = 1
int FAILED = 0
int ENV_CHANGED = 1
int SKIPPED = 2
int RESOURCE_DENIED = 3
int INTERRUPTED = 4
tuple RESOURCE_NAMES
tuple TEMPDIR = os.path.abspath(tempfile.gettempdir())
list STDTESTS
dictionary NOTTESTS
dictionary _expectations
tuple mydir = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))
tuple i = len(sys.path)
tuple __file__ = os.path.abspath(__file__)

Function Documentation

def test.regrtest._make_temp_dir_for_build (   TEMPDIR) [private]

Definition at line 1585 of file regrtest.py.

01585 
01586 def _make_temp_dir_for_build(TEMPDIR):
01587     # When tests are run from the Python build directory, it is best practice
01588     # to keep the test files in a subfolder.  It eases the cleanup of leftover
01589     # files using command "make distclean".
01590     if sysconfig.is_python_build():
01591         TEMPDIR = os.path.join(sysconfig.get_config_var('srcdir'), 'build')
01592         TEMPDIR = os.path.abspath(TEMPDIR)
01593         try:
01594             os.mkdir(TEMPDIR)
01595         except OSError as e:
01596             if e.errno != errno.EEXIST:
01597                 raise
01598 
01599     # Define a writable temp dir that will be used as cwd while running
01600     # the tests. The name of the dir includes the pid to allow parallel
01601     # testing (see the -j option).
01602     TESTCWD = 'test_python_{}'.format(os.getpid())
01603 
01604     TESTCWD = os.path.join(TEMPDIR, TESTCWD)
01605     return TEMPDIR, TESTCWD

Here is the call graph for this function:

def test.regrtest.cleanup_test_droppings (   testname,
  verbose 
)

Definition at line 1094 of file regrtest.py.

01094 
01095 def cleanup_test_droppings(testname, verbose):
01096     import shutil
01097     import stat
01098     import gc
01099 
01100     # First kill any dangling references to open files etc.
01101     # This can also issue some ResourceWarnings which would otherwise get
01102     # triggered during the following test run, and possibly produce failures.
01103     gc.collect()
01104 
01105     # Try to clean up junk commonly left behind.  While tests shouldn't leave
01106     # any files or directories behind, when a test fails that can be tedious
01107     # for it to arrange.  The consequences can be especially nasty on Windows,
01108     # since if a test leaves a file open, it cannot be deleted by name (while
01109     # there's nothing we can do about that here either, we can display the
01110     # name of the offending test, which is a real help).
01111     for name in (support.TESTFN,
01112                  "db_home",
01113                 ):
01114         if not os.path.exists(name):
01115             continue
01116 
01117         if os.path.isdir(name):
01118             kind, nuker = "directory", shutil.rmtree
01119         elif os.path.isfile(name):
01120             kind, nuker = "file", os.unlink
01121         else:
01122             raise SystemError("os.path says %r exists but is neither "
01123                               "directory nor file" % name)
01124 
01125         if verbose:
01126             print("%r left behind %s %r" % (testname, kind, name))
01127         try:
01128             # if we have chmod, fix possible permissions problems
01129             # that might prevent cleanup
01130             if (hasattr(os, 'chmod')):
01131                 os.chmod(name, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
01132             nuker(name)
01133         except Exception as msg:
01134             print(("%r left behind %s %r and it couldn't be "
01135                 "removed: %s" % (testname, kind, name, msg)), file=sys.stderr)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.regrtest.count (   n,
  word 
)

Definition at line 1278 of file regrtest.py.

01278 
01279 def count(n, word):
01280     if n == 1:
01281         return "%d %s" % (n, word)
01282     else:
01283         return "%d %ss" % (n, word)

Here is the caller graph for this function:

def test.regrtest.dash_R (   the_module,
  test,
  indirect_test,
  huntrleaks 
)
Run a test multiple times, looking for reference leaks.

Returns:
    False if the test didn't leak references; True if we detected refleaks.

Definition at line 1136 of file regrtest.py.

01136 
01137 def dash_R(the_module, test, indirect_test, huntrleaks):
01138     """Run a test multiple times, looking for reference leaks.
01139 
01140     Returns:
01141         False if the test didn't leak references; True if we detected refleaks.
01142     """
01143     # This code is hackish and inelegant, but it seems to do the job.
01144     import copyreg, _abcoll
01145 
01146     if not hasattr(sys, 'gettotalrefcount'):
01147         raise Exception("Tracking reference leaks requires a debug build "
01148                         "of Python")
01149 
01150     # Save current values for dash_R_cleanup() to restore.
01151     fs = warnings.filters[:]
01152     ps = copyreg.dispatch_table.copy()
01153     pic = sys.path_importer_cache.copy()
01154     try:
01155         import zipimport
01156     except ImportError:
01157         zdc = None # Run unmodified on platforms without zipimport support
01158     else:
01159         zdc = zipimport._zip_directory_cache.copy()
01160     abcs = {}
01161     for abc in [getattr(_abcoll, a) for a in _abcoll.__all__]:
01162         if not isabstract(abc):
01163             continue
01164         for obj in abc.__subclasses__() + [abc]:
01165             abcs[obj] = obj._abc_registry.copy()
01166 
01167     if indirect_test:
01168         def run_the_test():
01169             indirect_test()
01170     else:
01171         def run_the_test():
01172             del sys.modules[the_module.__name__]
01173             exec('import ' + the_module.__name__)
01174 
01175     deltas = []
01176     nwarmup, ntracked, fname = huntrleaks
01177     fname = os.path.join(support.SAVEDCWD, fname)
01178     repcount = nwarmup + ntracked
01179     print("beginning", repcount, "repetitions", file=sys.stderr)
01180     print(("1234567890"*(repcount//10 + 1))[:repcount], file=sys.stderr)
01181     sys.stderr.flush()
01182     dash_R_cleanup(fs, ps, pic, zdc, abcs)
01183     for i in range(repcount):
01184         rc_before = sys.gettotalrefcount()
01185         run_the_test()
01186         sys.stderr.write('.')
01187         sys.stderr.flush()
01188         dash_R_cleanup(fs, ps, pic, zdc, abcs)
01189         rc_after = sys.gettotalrefcount()
01190         if i >= nwarmup:
01191             deltas.append(rc_after - rc_before)
01192     print(file=sys.stderr)
01193     if any(deltas):
01194         msg = '%s leaked %s references, sum=%s' % (test, deltas, sum(deltas))
01195         print(msg, file=sys.stderr)
01196         sys.stderr.flush()
01197         with open(fname, "a") as refrep:
01198             print(msg, file=refrep)
01199             refrep.flush()
01200         return True
01201     return False

Here is the call graph for this function:

Here is the caller graph for this function:

def test.regrtest.dash_R_cleanup (   fs,
  ps,
  pic,
  zdc,
  abcs 
)

Definition at line 1202 of file regrtest.py.

01202 
01203 def dash_R_cleanup(fs, ps, pic, zdc, abcs):
01204     import gc, copyreg
01205     import _strptime, linecache
01206     import urllib.parse, urllib.request, mimetypes, doctest
01207     import struct, filecmp, _abcoll
01208     from distutils.dir_util import _path_created
01209     from weakref import WeakSet
01210 
01211     # Clear the warnings registry, so they can be displayed again
01212     for mod in sys.modules.values():
01213         if hasattr(mod, '__warningregistry__'):
01214             del mod.__warningregistry__
01215 
01216     # Restore some original values.
01217     warnings.filters[:] = fs
01218     copyreg.dispatch_table.clear()
01219     copyreg.dispatch_table.update(ps)
01220     sys.path_importer_cache.clear()
01221     sys.path_importer_cache.update(pic)
01222     try:
01223         import zipimport
01224     except ImportError:
01225         pass # Run unmodified on platforms without zipimport support
01226     else:
01227         zipimport._zip_directory_cache.clear()
01228         zipimport._zip_directory_cache.update(zdc)
01229 
01230     # clear type cache
01231     sys._clear_type_cache()
01232 
01233     # Clear ABC registries, restoring previously saved ABC registries.
01234     for abc in [getattr(_abcoll, a) for a in _abcoll.__all__]:
01235         if not isabstract(abc):
01236             continue
01237         for obj in abc.__subclasses__() + [abc]:
01238             obj._abc_registry = abcs.get(obj, WeakSet()).copy()
01239             obj._abc_cache.clear()
01240             obj._abc_negative_cache.clear()
01241 
01242     # Flush standard output, so that buffered data is sent to the OS and
01243     # associated Python objects are reclaimed.
01244     for stream in (sys.stdout, sys.stderr, sys.__stdout__, sys.__stderr__):
01245         if stream is not None:
01246             stream.flush()
01247 
01248     # Clear assorted module caches.
01249     _path_created.clear()
01250     re.purge()
01251     _strptime._regex_cache.clear()
01252     urllib.parse.clear_cache()
01253     urllib.request.urlcleanup()
01254     linecache.clearcache()
01255     mimetypes._default_mime_types()
01256     filecmp._cache.clear()
01257     struct._clearcache()
01258     doctest.master = None
01259 
01260     # Collect cyclic trash.
01261     gc.collect()

Here is the call graph for this function:

Here is the caller graph for this function:

def test.regrtest.findtestdir (   path = None)

Definition at line 1267 of file regrtest.py.

01267 
01268 def findtestdir(path=None):
01269     return path or os.path.dirname(__file__) or os.curdir

Here is the caller graph for this function:

def test.regrtest.findtests (   testdir = None,
  stdtests = STDTESTS,
  nottests = NOTTESTS 
)
Return a list of all applicable test modules.

Definition at line 750 of file regrtest.py.

00750 
00751 def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS):
00752     """Return a list of all applicable test modules."""
00753     testdir = findtestdir(testdir)
00754     names = os.listdir(testdir)
00755     tests = []
00756     others = set(stdtests) | nottests
00757     for name in names:
00758         modname, ext = os.path.splitext(name)
00759         if modname[:5] == "test_" and ext == ".py" and modname not in others:
00760             tests.append(modname)
00761     return stdtests + sorted(tests)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.regrtest.main (   tests = None,
  testdir = None,
  verbose = 0,
  quiet = False,
  exclude = False,
  single = False,
  randomize = False,
  fromfile = None,
  findleaks = False,
  use_resources = None,
  trace = False,
  coverdir = 'coverage',
  runleaks = False,
  huntrleaks = False,
  verbose2 = False,
  print_slow = False,
  random_seed = None,
  use_mp = None,
  verbose3 = False,
  forever = False,
  header = False,
  failfast = False,
  match_tests = None 
)
Execute a test suite.

This also parses command-line options and modifies its behavior
accordingly.

tests -- a list of strings containing test names (optional)
testdir -- the directory in which to look for tests (optional)

Users other than the Python test suite will certainly want to
specify testdir; if it's omitted, the directory containing the
Python test suite is searched for.

If the tests argument is omitted, the tests listed on the
command-line will be used.  If that's empty, too, then all *.py
files beginning with test_ will be used.

The other default arguments (verbose, quiet, exclude,
single, randomize, findleaks, use_resources, trace, coverdir,
print_slow, and random_seed) allow programmers calling main()
directly to set the values that would normally be set by flags
on the command line.

Definition at line 246 of file regrtest.py.

00246 
00247          header=False, failfast=False, match_tests=None):
00248     """Execute a test suite.
00249 
00250     This also parses command-line options and modifies its behavior
00251     accordingly.
00252 
00253     tests -- a list of strings containing test names (optional)
00254     testdir -- the directory in which to look for tests (optional)
00255 
00256     Users other than the Python test suite will certainly want to
00257     specify testdir; if it's omitted, the directory containing the
00258     Python test suite is searched for.
00259 
00260     If the tests argument is omitted, the tests listed on the
00261     command-line will be used.  If that's empty, too, then all *.py
00262     files beginning with test_ will be used.
00263 
00264     The other default arguments (verbose, quiet, exclude,
00265     single, randomize, findleaks, use_resources, trace, coverdir,
00266     print_slow, and random_seed) allow programmers calling main()
00267     directly to set the values that would normally be set by flags
00268     on the command line.
00269     """
00270 
00271     replace_stdout()
00272 
00273     support.record_original_stdout(sys.stdout)
00274     try:
00275         opts, args = getopt.getopt(sys.argv[1:], 'hvqxsSrf:lu:t:TD:NLR:FwWM:nj:Gm:',
00276             ['help', 'verbose', 'verbose2', 'verbose3', 'quiet',
00277              'exclude', 'single', 'slow', 'random', 'fromfile', 'findleaks',
00278              'use=', 'threshold=', 'trace', 'coverdir=', 'nocoverdir',
00279              'runleaks', 'huntrleaks=', 'memlimit=', 'randseed=',
00280              'multiprocess=', 'coverage', 'slaveargs=', 'forever', 'debug',
00281              'start=', 'nowindows', 'header', 'failfast', 'match'])
00282     except getopt.error as msg:
00283         usage(msg)
00284 
00285     # Defaults
00286     if random_seed is None:
00287         random_seed = random.randrange(10000000)
00288     if use_resources is None:
00289         use_resources = []
00290     debug = False
00291     start = None
00292     for o, a in opts:
00293         if o in ('-h', '--help'):
00294             print(__doc__)
00295             return
00296         elif o in ('-v', '--verbose'):
00297             verbose += 1
00298         elif o in ('-w', '--verbose2'):
00299             verbose2 = True
00300         elif o in ('-d', '--debug'):
00301             debug = True
00302         elif o in ('-W', '--verbose3'):
00303             verbose3 = True
00304         elif o in ('-G', '--failfast'):
00305             failfast = True
00306         elif o in ('-q', '--quiet'):
00307             quiet = True;
00308             verbose = 0
00309         elif o in ('-x', '--exclude'):
00310             exclude = True
00311         elif o in ('-S', '--start'):
00312             start = a
00313         elif o in ('-s', '--single'):
00314             single = True
00315         elif o in ('-S', '--slow'):
00316             print_slow = True
00317         elif o in ('-r', '--randomize'):
00318             randomize = True
00319         elif o == '--randseed':
00320             random_seed = int(a)
00321         elif o in ('-f', '--fromfile'):
00322             fromfile = a
00323         elif o in ('-m', '--match'):
00324             match_tests = a
00325         elif o in ('-l', '--findleaks'):
00326             findleaks = True
00327         elif o in ('-L', '--runleaks'):
00328             runleaks = True
00329         elif o in ('-t', '--threshold'):
00330             import gc
00331             gc.set_threshold(int(a))
00332         elif o in ('-T', '--coverage'):
00333             trace = True
00334         elif o in ('-D', '--coverdir'):
00335             coverdir = os.path.join(os.getcwd(), a)
00336         elif o in ('-N', '--nocoverdir'):
00337             coverdir = None
00338         elif o in ('-R', '--huntrleaks'):
00339             huntrleaks = a.split(':')
00340             if len(huntrleaks) not in (2, 3):
00341                 print(a, huntrleaks)
00342                 usage('-R takes 2 or 3 colon-separated arguments')
00343             if not huntrleaks[0]:
00344                 huntrleaks[0] = 5
00345             else:
00346                 huntrleaks[0] = int(huntrleaks[0])
00347             if not huntrleaks[1]:
00348                 huntrleaks[1] = 4
00349             else:
00350                 huntrleaks[1] = int(huntrleaks[1])
00351             if len(huntrleaks) == 2 or not huntrleaks[2]:
00352                 huntrleaks[2:] = ["reflog.txt"]
00353             # Avoid false positives due to the character cache in
00354             # stringobject.c filling slowly with random data
00355             warm_char_cache()
00356         elif o in ('-M', '--memlimit'):
00357             support.set_memlimit(a)
00358         elif o in ('-u', '--use'):
00359             u = [x.lower() for x in a.split(',')]
00360             for r in u:
00361                 if r == 'all':
00362                     use_resources[:] = RESOURCE_NAMES
00363                     continue
00364                 remove = False
00365                 if r[0] == '-':
00366                     remove = True
00367                     r = r[1:]
00368                 if r not in RESOURCE_NAMES:
00369                     usage('Invalid -u/--use option: ' + a)
00370                 if remove:
00371                     if r in use_resources:
00372                         use_resources.remove(r)
00373                 elif r not in use_resources:
00374                     use_resources.append(r)
00375         elif o in ('-n', '--nowindows'):
00376             import msvcrt
00377             msvcrt.SetErrorMode(msvcrt.SEM_FAILCRITICALERRORS|
00378                     msvcrt.SEM_NOALIGNMENTFAULTEXCEPT|
00379                     msvcrt.SEM_NOGPFAULTERRORBOX|
00380                     msvcrt.SEM_NOOPENFILEERRORBOX)
00381             try:
00382                 msvcrt.CrtSetReportMode
00383             except AttributeError:
00384                 # release build
00385                 pass
00386             else:
00387                 for m in [msvcrt.CRT_WARN, msvcrt.CRT_ERROR, msvcrt.CRT_ASSERT]:
00388                     msvcrt.CrtSetReportMode(m, msvcrt.CRTDBG_MODE_FILE)
00389                     msvcrt.CrtSetReportFile(m, msvcrt.CRTDBG_FILE_STDERR)
00390         elif o in ('-F', '--forever'):
00391             forever = True
00392         elif o in ('-j', '--multiprocess'):
00393             use_mp = int(a)
00394         elif o == '--header':
00395             header = True
00396         elif o == '--slaveargs':
00397             args, kwargs = json.loads(a)
00398             try:
00399                 result = runtest(*args, **kwargs)
00400             except BaseException as e:
00401                 result = INTERRUPTED, e.__class__.__name__
00402             sys.stdout.flush()
00403             print()   # Force a newline (just in case)
00404             print(json.dumps(result))
00405             sys.exit(0)
00406         else:
00407             print(("No handler for option {}.  Please report this as a bug "
00408                    "at http://bugs.python.org.").format(o), file=sys.stderr)
00409             sys.exit(1)
00410     if single and fromfile:
00411         usage("-s and -f don't go together!")
00412     if use_mp and trace:
00413         usage("-T and -j don't go together!")
00414     if use_mp and findleaks:
00415         usage("-l and -j don't go together!")
00416     if use_mp and support.max_memuse:
00417         usage("-M and -j don't go together!")
00418     if failfast and not (verbose or verbose3):
00419         usage("-G/--failfast needs either -v or -W")
00420 
00421     good = []
00422     bad = []
00423     skipped = []
00424     resource_denieds = []
00425     environment_changed = []
00426     interrupted = False
00427 
00428     if findleaks:
00429         try:
00430             import gc
00431         except ImportError:
00432             print('No GC available, disabling findleaks.')
00433             findleaks = False
00434         else:
00435             # Uncomment the line below to report garbage that is not
00436             # freeable by reference counting alone.  By default only
00437             # garbage that is not collectable by the GC is reported.
00438             #gc.set_debug(gc.DEBUG_SAVEALL)
00439             found_garbage = []
00440 
00441     if single:
00442         filename = os.path.join(TEMPDIR, 'pynexttest')
00443         try:
00444             fp = open(filename, 'r')
00445             next_test = fp.read().strip()
00446             tests = [next_test]
00447             fp.close()
00448         except IOError:
00449             pass
00450 
00451     if fromfile:
00452         tests = []
00453         fp = open(os.path.join(support.SAVEDCWD, fromfile))
00454         count_pat = re.compile(r'\[\s*\d+/\s*\d+\]')
00455         for line in fp:
00456             line = count_pat.sub('', line)
00457             guts = line.split() # assuming no test has whitespace in its name
00458             if guts and not guts[0].startswith('#'):
00459                 tests.extend(guts)
00460         fp.close()
00461 
00462     # Strip .py extensions.
00463     removepy(args)
00464     removepy(tests)
00465 
00466     stdtests = STDTESTS[:]
00467     nottests = NOTTESTS.copy()
00468     if exclude:
00469         for arg in args:
00470             if arg in stdtests:
00471                 stdtests.remove(arg)
00472             nottests.add(arg)
00473         args = []
00474 
00475     # For a partial run, we do not need to clutter the output.
00476     if verbose or header or not (quiet or single or tests or args):
00477         # Print basic platform information
00478         print("==", platform.python_implementation(), *sys.version.split())
00479         print("==  ", platform.platform(aliased=True),
00480                       "%s-endian" % sys.byteorder)
00481         print("==  ", os.getcwd())
00482         print("Testing with flags:", sys.flags)
00483 
00484     alltests = findtests(testdir, stdtests, nottests)
00485     selected = tests or args or alltests
00486     if single:
00487         selected = selected[:1]
00488         try:
00489             next_single_test = alltests[alltests.index(selected[0])+1]
00490         except IndexError:
00491             next_single_test = None
00492     # Remove all the tests that precede start if it's set.
00493     if start:
00494         try:
00495             del tests[:tests.index(start)]
00496         except ValueError:
00497             print("Couldn't find starting test (%s), using all tests" % start)
00498     if randomize:
00499         random.seed(random_seed)
00500         print("Using random seed", random_seed)
00501         random.shuffle(selected)
00502     if trace:
00503         import trace, tempfile
00504         tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,
00505                                          tempfile.gettempdir()],
00506                              trace=False, count=True)
00507 
00508     test_times = []
00509     support.verbose = verbose      # Tell tests to be moderately quiet
00510     support.use_resources = use_resources
00511     save_modules = sys.modules.keys()
00512 
00513     def accumulate_result(test, result):
00514         ok, test_time = result
00515         test_times.append((test_time, test))
00516         if ok == PASSED:
00517             good.append(test)
00518         elif ok == FAILED:
00519             bad.append(test)
00520         elif ok == ENV_CHANGED:
00521             environment_changed.append(test)
00522         elif ok == SKIPPED:
00523             skipped.append(test)
00524         elif ok == RESOURCE_DENIED:
00525             skipped.append(test)
00526             resource_denieds.append(test)
00527 
00528     if forever:
00529         def test_forever(tests=list(selected)):
00530             while True:
00531                 for test in tests:
00532                     yield test
00533                     if bad:
00534                         return
00535         tests = test_forever()
00536         test_count = ''
00537         test_count_width = 3
00538     else:
00539         tests = iter(selected)
00540         test_count = '/{}'.format(len(selected))
00541         test_count_width = len(test_count) - 1
00542 
00543     if use_mp:
00544         try:
00545             from threading import Thread
00546         except ImportError:
00547             print("Multiprocess option requires thread support")
00548             sys.exit(2)
00549         from queue import Queue
00550         from subprocess import Popen, PIPE
00551         debug_output_pat = re.compile(r"\[\d+ refs\]$")
00552         output = Queue()
00553         def tests_and_args():
00554             for test in tests:
00555                 args_tuple = (
00556                     (test, verbose, quiet),
00557                     dict(huntrleaks=huntrleaks, use_resources=use_resources,
00558                          debug=debug, output_on_failure=verbose3,
00559                          failfast=failfast, match_tests=match_tests)
00560                 )
00561                 yield (test, args_tuple)
00562         pending = tests_and_args()
00563         opt_args = support.args_from_interpreter_flags()
00564         base_cmd = [sys.executable] + opt_args + ['-m', 'test.regrtest']
00565         def work():
00566             # A worker thread.
00567             try:
00568                 while True:
00569                     try:
00570                         test, args_tuple = next(pending)
00571                     except StopIteration:
00572                         output.put((None, None, None, None))
00573                         return
00574                     # -E is needed by some tests, e.g. test_import
00575                     popen = Popen(base_cmd + ['--slaveargs', json.dumps(args_tuple)],
00576                                    stdout=PIPE, stderr=PIPE,
00577                                    universal_newlines=True,
00578                                    close_fds=(os.name != 'nt'))
00579                     stdout, stderr = popen.communicate()
00580                     # Strip last refcount output line if it exists, since it
00581                     # comes from the shutdown of the interpreter in the subcommand.
00582                     stderr = debug_output_pat.sub("", stderr)
00583                     stdout, _, result = stdout.strip().rpartition("\n")
00584                     if not result:
00585                         output.put((None, None, None, None))
00586                         return
00587                     result = json.loads(result)
00588                     output.put((test, stdout.rstrip(), stderr.rstrip(), result))
00589             except BaseException:
00590                 output.put((None, None, None, None))
00591                 raise
00592         workers = [Thread(target=work) for i in range(use_mp)]
00593         for worker in workers:
00594             worker.start()
00595         finished = 0
00596         test_index = 1
00597         try:
00598             while finished < use_mp:
00599                 test, stdout, stderr, result = output.get()
00600                 if test is None:
00601                     finished += 1
00602                     continue
00603                 accumulate_result(test, result)
00604                 if not quiet:
00605                     fmt = "[{1:{0}}{2}/{3}] {4}" if bad else "[{1:{0}}{2}] {4}"
00606                     print(fmt.format(
00607                         test_count_width, test_index, test_count,
00608                         len(bad), test))
00609                 if stdout:
00610                     print(stdout)
00611                 if stderr:
00612                     print(stderr, file=sys.stderr)
00613                 if result[0] == INTERRUPTED:
00614                     assert result[1] == 'KeyboardInterrupt'
00615                     raise KeyboardInterrupt   # What else?
00616                 test_index += 1
00617         except KeyboardInterrupt:
00618             interrupted = True
00619             pending.close()
00620         for worker in workers:
00621             worker.join()
00622     else:
00623         for test_index, test in enumerate(tests, 1):
00624             if not quiet:
00625                 fmt = "[{1:{0}}{2}/{3}] {4}" if bad else "[{1:{0}}{2}] {4}"
00626                 print(fmt.format(
00627                     test_count_width, test_index, test_count, len(bad), test))
00628                 sys.stdout.flush()
00629             if trace:
00630                 # If we're tracing code coverage, then we don't exit with status
00631                 # if on a false return value from main.
00632                 tracer.runctx('runtest(test, verbose, quiet)',
00633                               globals=globals(), locals=vars())
00634             else:
00635                 try:
00636                     result = runtest(test, verbose, quiet, huntrleaks, debug,
00637                                      output_on_failure=verbose3,
00638                                      failfast=failfast, match_tests=match_tests)
00639                     accumulate_result(test, result)
00640                 except KeyboardInterrupt:
00641                     interrupted = True
00642                     break
00643                 except:
00644                     raise
00645             if findleaks:
00646                 gc.collect()
00647                 if gc.garbage:
00648                     print("Warning: test created", len(gc.garbage), end=' ')
00649                     print("uncollectable object(s).")
00650                     # move the uncollectable objects somewhere so we don't see
00651                     # them again
00652                     found_garbage.extend(gc.garbage)
00653                     del gc.garbage[:]
00654             # Unload the newly imported modules (best effort finalization)
00655             for module in sys.modules.keys():
00656                 if module not in save_modules and module.startswith("test."):
00657                     support.unload(module)
00658 
00659     if interrupted:
00660         # print a newline after ^C
00661         print()
00662         print("Test suite interrupted by signal SIGINT.")
00663         omitted = set(selected) - set(good) - set(bad) - set(skipped)
00664         print(count(len(omitted), "test"), "omitted:")
00665         printlist(omitted)
00666     if good and not quiet:
00667         if not bad and not skipped and not interrupted and len(good) > 1:
00668             print("All", end=' ')
00669         print(count(len(good), "test"), "OK.")
00670     if print_slow:
00671         test_times.sort(reverse=True)
00672         print("10 slowest tests:")
00673         for time, test in test_times[:10]:
00674             print("%s: %.1fs" % (test, time))
00675     if bad:
00676         bad = sorted(set(bad) - set(environment_changed))
00677         if bad:
00678             print(count(len(bad), "test"), "failed:")
00679             printlist(bad)
00680         if environment_changed:
00681             print("{} altered the execution environment:".format(
00682                      count(len(environment_changed), "test")))
00683             printlist(environment_changed)
00684     if skipped and not quiet:
00685         print(count(len(skipped), "test"), "skipped:")
00686         printlist(skipped)
00687 
00688         e = _ExpectedSkips()
00689         plat = sys.platform
00690         if e.isvalid():
00691             surprise = set(skipped) - e.getexpected() - set(resource_denieds)
00692             if surprise:
00693                 print(count(len(surprise), "skip"), \
00694                       "unexpected on", plat + ":")
00695                 printlist(surprise)
00696             else:
00697                 print("Those skips are all expected on", plat + ".")
00698         else:
00699             print("Ask someone to teach regrtest.py about which tests are")
00700             print("expected to get skipped on", plat + ".")
00701 
00702     if verbose2 and bad:
00703         print("Re-running failed tests in verbose mode")
00704         for test in bad:
00705             print("Re-running test %r in verbose mode" % test)
00706             sys.stdout.flush()
00707             try:
00708                 verbose = True
00709                 ok = runtest(test, True, quiet, huntrleaks, debug)
00710             except KeyboardInterrupt:
00711                 # print a newline separate from the ^C
00712                 print()
00713                 break
00714             except:
00715                 raise
00716 
00717     if single:
00718         if next_single_test:
00719             with open(filename, 'w') as fp:
00720                 fp.write(next_single_test + '\n')
00721         else:
00722             os.unlink(filename)
00723 
00724     if trace:
00725         r = tracer.results()
00726         r.write_results(show_missing=True, summary=True, coverdir=coverdir)
00727 
00728     if runleaks:
00729         os.system("leaks %d" % os.getpid())
00730 
00731     sys.exit(len(bad) > 0 or interrupted)
00732 

Here is the call graph for this function:

Here is the caller graph for this function:

def test.regrtest.printlist (   x,
  width = 70,
  indent = 4 
)
Print the elements of iterable x to stdout.

Optional arg width (default 70) is the maximum line length.
Optional arg indent (default 4) is the number of blanks with which to
begin each line.

Definition at line 1284 of file regrtest.py.

01284 
01285 def printlist(x, width=70, indent=4):
01286     """Print the elements of iterable x to stdout.
01287 
01288     Optional arg width (default 70) is the maximum line length.
01289     Optional arg indent (default 4) is the number of blanks with which to
01290     begin each line.
01291     """
01292 
01293     from textwrap import fill
01294     blanks = ' ' * indent
01295     # Print the sorted list: 'x' may be a '--random' list or a set()
01296     print(fill(' '.join(str(elt) for elt in sorted(x)), width,
01297                initial_indent=blanks, subsequent_indent=blanks))
01298 
01299 # Map sys.platform to a string containing the basenames of tests
01300 # expected to be skipped on that platform.
01301 #
01302 # Special cases:
01303 #     test_pep277
01304 #         The _ExpectedSkips constructor adds this to the set of expected
01305 #         skips if not os.path.supports_unicode_filenames.
01306 #     test_timeout
01307 #         Controlled by test_timeout.skip_expected.  Requires the network
01308 #         resource and a socket module.
01309 #
01310 # Tests that are expected to be skipped everywhere except on one platform
01311 # are also handled separately.

Here is the call graph for this function:

Here is the caller graph for this function:

def test.regrtest.removepy (   names)

Definition at line 1270 of file regrtest.py.

01270 
01271 def removepy(names):
01272     if not names:
01273         return
01274     for idx, name in enumerate(names):
01275         basename, ext = os.path.splitext(name)
01276         if ext == '.py':
01277             names[idx] = basename

Here is the call graph for this function:

Here is the caller graph for this function:

Set stdout encoder error handler to backslashreplace (as stderr error
handler) to avoid UnicodeEncodeError when printing a traceback

Definition at line 762 of file regrtest.py.

00762 
00763 def replace_stdout():
00764     """Set stdout encoder error handler to backslashreplace (as stderr error
00765     handler) to avoid UnicodeEncodeError when printing a traceback"""
00766     if os.name == "nt":
00767         # Replace sys.stdout breaks the stdout newlines on Windows: issue #8533
00768         return
00769 
00770     import atexit
00771 
00772     stdout = sys.stdout
00773     sys.stdout = open(stdout.fileno(), 'w',
00774         encoding=stdout.encoding,
00775         errors="backslashreplace",
00776         closefd=False)
00777 
00778     def restore_stdout():
00779         sys.stdout.close()
00780         sys.stdout = stdout
00781     atexit.register(restore_stdout)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.regrtest.runtest (   test,
  verbose,
  quiet,
  huntrleaks = False,
  debug = False,
  use_resources = None,
  output_on_failure = False,
  failfast = False,
  match_tests = None 
)
Run a single test.

test -- the name of the test
verbose -- if true, print more messages
quiet -- if true, don't print 'skipped' messages (probably redundant)
test_times -- a list of (time, test_name) pairs
huntrleaks -- run multiple times to test for leaks; requires a debug
              build; a triple corresponding to -R's three arguments
output_on_failure -- if true, display test output on failure

Returns one of the test result constants:
    INTERRUPTED      KeyboardInterrupt when run under -j
    RESOURCE_DENIED  test skipped because resource denied
    SKIPPED          test skipped for some other reason
    ENV_CHANGED      test failed because it changed the execution environment
    FAILED           test failed
    PASSED           test passed

Definition at line 784 of file regrtest.py.

00784 
00785             output_on_failure=False, failfast=False, match_tests=None):
00786     """Run a single test.
00787 
00788     test -- the name of the test
00789     verbose -- if true, print more messages
00790     quiet -- if true, don't print 'skipped' messages (probably redundant)
00791     test_times -- a list of (time, test_name) pairs
00792     huntrleaks -- run multiple times to test for leaks; requires a debug
00793                   build; a triple corresponding to -R's three arguments
00794     output_on_failure -- if true, display test output on failure
00795 
00796     Returns one of the test result constants:
00797         INTERRUPTED      KeyboardInterrupt when run under -j
00798         RESOURCE_DENIED  test skipped because resource denied
00799         SKIPPED          test skipped for some other reason
00800         ENV_CHANGED      test failed because it changed the execution environment
00801         FAILED           test failed
00802         PASSED           test passed
00803     """
00804 
00805     if use_resources is not None:
00806         support.use_resources = use_resources
00807     try:
00808         support.match_tests = match_tests
00809         if failfast:
00810             support.failfast = True
00811         if output_on_failure:
00812             support.verbose = True
00813 
00814             # Reuse the same instance to all calls to runtest(). Some
00815             # tests keep a reference to sys.stdout or sys.stderr
00816             # (eg. test_argparse).
00817             if runtest.stringio is None:
00818                 stream = io.StringIO()
00819                 runtest.stringio = stream
00820             else:
00821                 stream = runtest.stringio
00822                 stream.seek(0)
00823                 stream.truncate()
00824 
00825             orig_stdout = sys.stdout
00826             orig_stderr = sys.stderr
00827             try:
00828                 sys.stdout = stream
00829                 sys.stderr = stream
00830                 result = runtest_inner(test, verbose, quiet, huntrleaks,
00831                                        debug, display_failure=False)
00832                 if result[0] == FAILED:
00833                     output = stream.getvalue()
00834                     orig_stderr.write(output)
00835                     orig_stderr.flush()
00836             finally:
00837                 sys.stdout = orig_stdout
00838                 sys.stderr = orig_stderr
00839         else:
00840             support.verbose = verbose  # Tell tests to be moderately quiet
00841             result = runtest_inner(test, verbose, quiet, huntrleaks, debug,
00842                                    display_failure=not verbose)
00843         return result
00844     finally:
00845         cleanup_test_droppings(test, verbose)
00846 runtest.stringio = None
00847 
00848 # Unit tests are supposed to leave the execution environment unchanged
00849 # once they complete.  But sometimes tests have bugs, especially when
00850 # tests fail, and the changes to environment go on to mess up other
00851 # tests.  This can cause issues with buildbot stability, since tests
00852 # are run in random order and so problems may appear to come and go.
00853 # There are a few things we can save and restore to mitigate this, and
00854 # the following context manager handles this task.

Here is the call graph for this function:

Here is the caller graph for this function:

def test.regrtest.runtest_inner (   test,
  verbose,
  quiet,
  huntrleaks = False,
  debug = False,
  display_failure = True 
)

Definition at line 1038 of file regrtest.py.

01038 
01039                   huntrleaks=False, debug=False, display_failure=True):
01040     support.unload(test)
01041 
01042     test_time = 0.0
01043     refleak = False  # True if the test leaked references.
01044     try:
01045         if test.startswith('test.'):
01046             abstest = test
01047         else:
01048             # Always import it from the test package
01049             abstest = 'test.' + test
01050         with saved_test_environment(test, verbose, quiet) as environment:
01051             start_time = time.time()
01052             the_package = __import__(abstest, globals(), locals(), [])
01053             the_module = getattr(the_package, test)
01054             # Old tests run to completion simply as a side-effect of
01055             # being imported.  For tests based on unittest or doctest,
01056             # explicitly invoke their test_main() function (if it exists).
01057             indirect_test = getattr(the_module, "test_main", None)
01058             if indirect_test is not None:
01059                 indirect_test()
01060             if huntrleaks:
01061                 refleak = dash_R(the_module, test, indirect_test,
01062                     huntrleaks)
01063             test_time = time.time() - start_time
01064     except support.ResourceDenied as msg:
01065         if not quiet:
01066             print(test, "skipped --", msg)
01067             sys.stdout.flush()
01068         return RESOURCE_DENIED, test_time
01069     except unittest.SkipTest as msg:
01070         if not quiet:
01071             print(test, "skipped --", msg)
01072             sys.stdout.flush()
01073         return SKIPPED, test_time
01074     except KeyboardInterrupt:
01075         raise
01076     except support.TestFailed as msg:
01077         if display_failure:
01078             print("test", test, "failed --", msg, file=sys.stderr)
01079         else:
01080             print("test", test, "failed", file=sys.stderr)
01081         sys.stderr.flush()
01082         return FAILED, test_time
01083     except:
01084         msg = traceback.format_exc()
01085         print("test", test, "crashed --", msg, file=sys.stderr)
01086         sys.stderr.flush()
01087         return FAILED, test_time
01088     else:
01089         if refleak:
01090             return FAILED, test_time
01091         if environment.changed:
01092             return ENV_CHANGED, test_time
01093         return PASSED, test_time

Here is the call graph for this function:

Here is the caller graph for this function:

def test.regrtest.usage (   msg)

Definition at line 235 of file regrtest.py.

00235 
00236 def usage(msg):
00237     print(msg, file=sys.stderr)
00238     print("Use --help for usage", file=sys.stderr)
00239     sys.exit(2)
00240 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1262 of file regrtest.py.

01262 
01263 def warm_char_cache():
01264     s = bytes(range(256))
01265     for i in range(256):
01266         s[i:i+1]

Here is the caller graph for this function:


Variable Documentation

tuple test.regrtest.__file__ = os.path.abspath(__file__)

Definition at line 1623 of file regrtest.py.

Initial value:
00001 {
00002     'win32':
00003         """        test__locale        test_crypt        test_curses        test_dbm        test_fcntl        test_fork1        test_epoll        test_dbm_gnu        test_dbm_ndbm        test_grp        test_ioctl        test_largefile        test_kqueue        test_openpty        test_ossaudiodev        test_pipes        test_poll        test_posix        test_pty        test_pwd        test_resource        test_signal        test_syslog        test_threadsignals        test_wait3        test_wait4        """,
00004     'linux2':
00005         """        test_curses        test_largefile        test_kqueue        test_ossaudiodev        """,
00006     'unixware7':
00007         """        test_epoll        test_largefile        test_kqueue        test_minidom        test_openpty        test_pyexpat        test_sax        test_sundry        """,
00008     'openunix8':
00009         """        test_epoll        test_largefile        test_kqueue        test_minidom        test_openpty        test_pyexpat        test_sax        test_sundry        """,
00010     'sco_sv3':
00011         """        test_asynchat        test_fork1        test_epoll        test_gettext        test_largefile        test_locale        test_kqueue        test_minidom        test_openpty        test_pyexpat        test_queue        test_sax        test_sundry        test_thread        test_threaded_import        test_threadedtempfile        test_threading        """,
00012     'darwin':
00013         """        test__locale        test_curses        test_epoll        test_dbm_gnu        test_gdb        test_largefile        test_locale        test_minidom        test_ossaudiodev        test_poll        """,
00014     'sunos5':
00015         """        test_curses        test_dbm        test_epoll        test_kqueue        test_dbm_gnu        test_gzip        test_openpty        test_zipfile        test_zlib        """,
00016     'hp-ux11':
00017         """        test_curses        test_epoll        test_dbm_gnu        test_gzip        test_largefile        test_locale        test_kqueue        test_minidom        test_openpty        test_pyexpat        test_sax        test_zipfile        test_zlib        """,
00018     'cygwin':
00019         """        test_curses        test_dbm        test_epoll        test_ioctl        test_kqueue        test_largefile        test_locale        test_ossaudiodev        test_socketserver        """,
00020     'os2emx':
00021         """        test_audioop        test_curses        test_epoll        test_kqueue        test_largefile        test_mmap        test_openpty        test_ossaudiodev        test_pty        test_resource        test_signal        """,
00022     'freebsd4':
00023         """        test_epoll        test_dbm_gnu        test_locale        test_ossaudiodev        test_pep277        test_pty        test_socketserver        test_tcl        test_tk        test_ttk_guionly        test_ttk_textonly        test_timeout        test_urllibnet        test_multiprocessing        """,
00024     'aix5':
00025         """        test_bz2        test_epoll        test_dbm_gnu        test_gzip        test_kqueue        test_ossaudiodev        test_tcl        test_tk        test_ttk_guionly        test_ttk_textonly        test_zipimport        test_zlib        """,
00026     'openbsd3':
00027         """        test_ctypes        test_epoll        test_dbm_gnu        test_locale        test_normalization        test_ossaudiodev        test_pep277        test_tcl        test_tk        test_ttk_guionly        test_ttk_textonly        test_multiprocessing        """,
00028     'netbsd3':
00029         """        test_ctypes        test_curses        test_epoll        test_dbm_gnu        test_locale        test_ossaudiodev        test_pep277        test_tcl        test_tk        test_ttk_guionly        test_ttk_textonly        test_multiprocessing        """,
00030 }

Definition at line 1312 of file regrtest.py.

Definition at line 223 of file regrtest.py.

Definition at line 222 of file regrtest.py.

tuple test.regrtest.i = len(sys.path)

Definition at line 1613 of file regrtest.py.

Definition at line 226 of file regrtest.py.

Definition at line 184 of file regrtest.py.

tuple test.regrtest.mydir = os.path.abspath(os.path.normpath(os.path.dirname(sys.argv[0])))

Definition at line 1612 of file regrtest.py.

tuple test.regrtest.newsoft = min(hard, max(soft, 1024*2048))

Definition at line 217 of file regrtest.py.

Initial value:
00001 {
00002     'test_future1',
00003     'test_future2',
00004 }

Definition at line 745 of file regrtest.py.

Definition at line 221 of file regrtest.py.

Definition at line 225 of file regrtest.py.

Initial value:
00001 ('audio', 'curses', 'largefile', 'network',
00002                   'decimal', 'cpu', 'subprocess', 'urlfetch', 'gui')

Definition at line 230 of file regrtest.py.

Definition at line 224 of file regrtest.py.

Initial value:
00001 [
00002     'test_grammar',
00003     'test_opcodes',
00004     'test_dict',
00005     'test_builtin',
00006     'test_exceptions',
00007     'test_types',
00008     'test_unittest',
00009     'test_doctest',
00010     'test_doctest2',
00011 ]

Definition at line 733 of file regrtest.py.

tuple test.regrtest.TEMPDIR = os.path.abspath(tempfile.gettempdir())

Definition at line 233 of file regrtest.py.

Definition at line 180 of file regrtest.py.