/Lib/test/regrtest.py

http://unladen-swallow.googlecode.com/ · Python · 1268 lines · 1186 code · 16 blank · 66 comment · 25 complexity · 2d64cd7691880f366ceec901ed5e2c05 MD5 · raw file

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