annotate Lib/test/test_support.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 6985c0e85b4b
children 7c3b849d181a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28968
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
1 """Supporting definitions for the Python regression tests."""
13682
7de18f04f85a Restore TestSkipped exception; appears to have disappeared in last checkin.
Fred Drake <fdrake@acm.org>
parents: 13647
diff changeset
2
24442
226ba34bd998 Complete the absolute import patch for the test suite. All relative
Barry Warsaw <barry@python.org>
parents: 21595
diff changeset
3 if __name__ != 'test.test_support':
41539
761ce3946b86 Import and raise statement cleanup.
Collin Winter <collinw@gmail.com>
parents: 41536
diff changeset
4 raise ImportError('test_support must be imported from the test package')
24442
226ba34bd998 Complete the absolute import patch for the test suite. All relative
Barry Warsaw <barry@python.org>
parents: 21595
diff changeset
5
41135
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
6 import contextlib
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
7 import errno
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
8 import socket
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
9 import sys
41539
761ce3946b86 Import and raise statement cleanup.
Collin Winter <collinw@gmail.com>
parents: 41536
diff changeset
10 import os
45506
55eefb43ef6f Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
11 import shutil
40455
480789b3294d Add test.test_support.guard_warnings_filter . This function returns a context
Brett Cannon <bcannon@gmail.com>
parents: 40217
diff changeset
12 import warnings
41539
761ce3946b86 Import and raise statement cleanup.
Collin Winter <collinw@gmail.com>
parents: 41536
diff changeset
13 import unittest
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
14
47316
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
15 __all__ = ["Error", "TestFailed", "TestSkipped", "ResourceDenied", "import_module",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
16 "verbose", "use_resources", "max_memuse", "record_original_stdout",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
17 "get_original_stdout", "unload", "unlink", "rmtree", "forget",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
18 "is_resource_enabled", "requires", "find_unused_port", "bind_port",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
19 "fcmp", "have_unicode", "is_jython", "TESTFN", "HOST", "FUZZ",
58491
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
20 "findfile", "verify", "vereq", "sortdict", "check_syntax_error",
49146
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
21 "open_urlresource", "check_warnings", "CleanImport",
48013
4ec5a0ec631a Remove duplicate entry in __all__.
Georg Brandl <georg@python.org>
parents: 47942
diff changeset
22 "EnvironmentVarGuard", "captured_output",
47316
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
23 "captured_stdout", "TransientResource", "transient_internet",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
24 "run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
25 "BasicTestRunner", "run_unittest", "run_doctest", "threading_setup",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
26 "threading_cleanup", "reap_children"]
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
27
13682
7de18f04f85a Restore TestSkipped exception; appears to have disappeared in last checkin.
Fred Drake <fdrake@acm.org>
parents: 13647
diff changeset
28 class Error(Exception):
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
29 """Base class for regression test exceptions."""
13682
7de18f04f85a Restore TestSkipped exception; appears to have disappeared in last checkin.
Fred Drake <fdrake@acm.org>
parents: 13647
diff changeset
30
7de18f04f85a Restore TestSkipped exception; appears to have disappeared in last checkin.
Fred Drake <fdrake@acm.org>
parents: 13647
diff changeset
31 class TestFailed(Error):
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
32 """Test failed."""
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
13682
7de18f04f85a Restore TestSkipped exception; appears to have disappeared in last checkin.
Fred Drake <fdrake@acm.org>
parents: 13647
diff changeset
34 class TestSkipped(Error):
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
35 """Test skipped.
13682
7de18f04f85a Restore TestSkipped exception; appears to have disappeared in last checkin.
Fred Drake <fdrake@acm.org>
parents: 13647
diff changeset
36
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
37 This can be raised to indicate that a test was deliberatly
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
38 skipped, but not because a feature wasn't available. For
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
39 example, if some resource can't be used, such as the network
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
40 appears to be unavailable, this should be raised instead of
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
41 TestFailed.
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
42 """
13682
7de18f04f85a Restore TestSkipped exception; appears to have disappeared in last checkin.
Fred Drake <fdrake@acm.org>
parents: 13647
diff changeset
43
27636
974f196abb87 test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents: 26527
diff changeset
44 class ResourceDenied(TestSkipped):
974f196abb87 test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents: 26527
diff changeset
45 """Test skipped because it requested a disallowed resource.
974f196abb87 test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents: 26527
diff changeset
46
974f196abb87 test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents: 26527
diff changeset
47 This is raised when a test calls requires() for a resource that
974f196abb87 test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents: 26527
diff changeset
48 has not be enabled. It is used to distinguish between expected
974f196abb87 test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents: 26527
diff changeset
49 and unexpected skips.
974f196abb87 test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents: 26527
diff changeset
50 """
974f196abb87 test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents: 26527
diff changeset
51
46752
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
52 def import_module(name, deprecated=False):
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
53 """Import the module to be tested, raising TestSkipped if it is not
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
54 available."""
49099
ff96a313cd1c warnings.catch_warnings() now returns a list or None instead of the custom
Brett Cannon <bcannon@gmail.com>
parents: 48948
diff changeset
55 with warnings.catch_warnings():
46752
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
56 if deprecated:
46816
884f8426cccf Suppress deprecations for packages as well when using
Brett Cannon <bcannon@gmail.com>
parents: 46757
diff changeset
57 warnings.filterwarnings("ignore", ".+ (module|package)",
884f8426cccf Suppress deprecations for packages as well when using
Brett Cannon <bcannon@gmail.com>
parents: 46757
diff changeset
58 DeprecationWarning)
46752
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
59 try:
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
60 module = __import__(name, level=0)
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
61 except ImportError:
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
62 raise TestSkipped("No module named " + name)
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
63 else:
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
64 return module
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
65
19416
a6ce2d16d739 use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
66 verbose = 1 # Flag set to 0 by regrtest.py
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
67 use_resources = None # Flag set to [] by regrtest.py
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
68 max_memuse = 0 # Disable bigmem tests (they will still be run with
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
69 # small sizes, to make sure they work.)
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
70 real_max_memuse = 0
4791
a2fd80eed27f Add verbose flag for regression test to clear.
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
71
20091
40d4d1cd6c84 Guido points out that sys.__stdout__ is a bit bucket under IDLE. So keep
Tim Peters <tim.peters@gmail.com>
parents: 20088
diff changeset
72 # _original_stdout is meant to hold stdout at the time regrtest began.
40d4d1cd6c84 Guido points out that sys.__stdout__ is a bit bucket under IDLE. So keep
Tim Peters <tim.peters@gmail.com>
parents: 20088
diff changeset
73 # This may be "the real" stdout, or IDLE's emulation of stdout, or whatever.
40d4d1cd6c84 Guido points out that sys.__stdout__ is a bit bucket under IDLE. So keep
Tim Peters <tim.peters@gmail.com>
parents: 20088
diff changeset
74 # The point is to have some flavor of stdout the user can actually see.
40d4d1cd6c84 Guido points out that sys.__stdout__ is a bit bucket under IDLE. So keep
Tim Peters <tim.peters@gmail.com>
parents: 20088
diff changeset
75 _original_stdout = None
40d4d1cd6c84 Guido points out that sys.__stdout__ is a bit bucket under IDLE. So keep
Tim Peters <tim.peters@gmail.com>
parents: 20088
diff changeset
76 def record_original_stdout(stdout):
40d4d1cd6c84 Guido points out that sys.__stdout__ is a bit bucket under IDLE. So keep
Tim Peters <tim.peters@gmail.com>
parents: 20088
diff changeset
77 global _original_stdout
40d4d1cd6c84 Guido points out that sys.__stdout__ is a bit bucket under IDLE. So keep
Tim Peters <tim.peters@gmail.com>
parents: 20088
diff changeset
78 _original_stdout = stdout
40d4d1cd6c84 Guido points out that sys.__stdout__ is a bit bucket under IDLE. So keep
Tim Peters <tim.peters@gmail.com>
parents: 20088
diff changeset
79
40d4d1cd6c84 Guido points out that sys.__stdout__ is a bit bucket under IDLE. So keep
Tim Peters <tim.peters@gmail.com>
parents: 20088
diff changeset
80 def get_original_stdout():
40d4d1cd6c84 Guido points out that sys.__stdout__ is a bit bucket under IDLE. So keep
Tim Peters <tim.peters@gmail.com>
parents: 20088
diff changeset
81 return _original_stdout or sys.stdout
40d4d1cd6c84 Guido points out that sys.__stdout__ is a bit bucket under IDLE. So keep
Tim Peters <tim.peters@gmail.com>
parents: 20088
diff changeset
82
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 def unload(name):
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
84 try:
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
85 del sys.modules[name]
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
86 except KeyError:
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
87 pass
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
36382
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
89 def unlink(filename):
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
90 try:
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
91 os.unlink(filename)
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
92 except OSError:
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
93 pass
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
94
45506
55eefb43ef6f Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
95 def rmtree(path):
55eefb43ef6f Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
96 try:
55eefb43ef6f Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
97 shutil.rmtree(path)
55eefb43ef6f Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
98 except OSError, e:
55eefb43ef6f Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
99 # Unix returns ENOENT, Windows returns ESRCH.
55eefb43ef6f Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
100 if e.errno not in (errno.ENOENT, errno.ESRCH):
55eefb43ef6f Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
101 raise
55eefb43ef6f Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
102
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 def forget(modname):
28968
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
104 '''"Forget" a module was ever imported by removing it from sys.modules and
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
105 deleting any .pyc and .pyo files.'''
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
106 unload(modname)
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
107 for dirname in sys.path:
36382
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
108 unlink(os.path.join(dirname, modname + os.extsep + 'pyc'))
28968
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
109 # Deleting the .pyo file cannot be within the 'try' for the .pyc since
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
110 # the chance exists that there is no .pyc (and thus the 'try' statement
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
111 # is exited) but there is a .pyo file.
36382
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
112 unlink(os.path.join(dirname, modname + os.extsep + 'pyo'))
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113
26527
18dcf80e6f8d Rearrange test_socket_ssl so that a skip is expected iff the network
Tim Peters <tim.peters@gmail.com>
parents: 26139
diff changeset
114 def is_resource_enabled(resource):
28968
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
115 """Test whether a resource is enabled. Known resources are set by
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
116 regrtest.py."""
26527
18dcf80e6f8d Rearrange test_socket_ssl so that a skip is expected iff the network
Tim Peters <tim.peters@gmail.com>
parents: 26139
diff changeset
117 return use_resources is not None and resource in use_resources
18dcf80e6f8d Rearrange test_socket_ssl so that a skip is expected iff the network
Tim Peters <tim.peters@gmail.com>
parents: 26139
diff changeset
118
19416
a6ce2d16d739 use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
119 def requires(resource, msg=None):
28968
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
120 """Raise ResourceDenied if the specified resource is not available.
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
121
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
122 If the caller's module is __main__ then automatically return True. The
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
123 possibility of False being returned occurs when regrtest.py is executing."""
28799
46d26afa8b95 if the test is run directly (__name__ == "__main__") don't actually require
Skip Montanaro <skip@pobox.com>
parents: 28174
diff changeset
124 # see if the caller's module is __main__ - if so, treat as if
46d26afa8b95 if the test is run directly (__name__ == "__main__") don't actually require
Skip Montanaro <skip@pobox.com>
parents: 28174
diff changeset
125 # the resource was set
46d26afa8b95 if the test is run directly (__name__ == "__main__") don't actually require
Skip Montanaro <skip@pobox.com>
parents: 28174
diff changeset
126 if sys._getframe().f_back.f_globals.get("__name__") == "__main__":
46d26afa8b95 if the test is run directly (__name__ == "__main__") don't actually require
Skip Montanaro <skip@pobox.com>
parents: 28174
diff changeset
127 return
26527
18dcf80e6f8d Rearrange test_socket_ssl so that a skip is expected iff the network
Tim Peters <tim.peters@gmail.com>
parents: 26139
diff changeset
128 if not is_resource_enabled(resource):
19416
a6ce2d16d739 use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
129 if msg is None:
a6ce2d16d739 use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
130 msg = "Use of the `%s' resource not enabled" % resource
27636
974f196abb87 test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents: 26527
diff changeset
131 raise ResourceDenied(msg)
19416
a6ce2d16d739 use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
132
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
133 HOST = 'localhost'
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
134
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
135 def find_unused_port(family=socket.AF_INET, socktype=socket.SOCK_STREAM):
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
136 """Returns an unused port that should be suitable for binding. This is
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
137 achieved by creating a temporary socket with the same family and type as
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
138 the 'sock' parameter (default is AF_INET, SOCK_STREAM), and binding it to
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
139 the specified host address (defaults to 0.0.0.0) with the port set to 0,
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
140 eliciting an unused ephemeral port from the OS. The temporary socket is
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
141 then closed and deleted, and the ephemeral port is returned.
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
142
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
143 Either this method or bind_port() should be used for any tests where a
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
144 server socket needs to be bound to a particular port for the duration of
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
145 the test. Which one to use depends on whether the calling code is creating
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
146 a python socket, or if an unused port needs to be provided in a constructor
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
147 or passed to an external program (i.e. the -accept argument to openssl's
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
148 s_server mode). Always prefer bind_port() over find_unused_port() where
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
149 possible. Hard coded ports should *NEVER* be used. As soon as a server
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
150 socket is bound to a hard coded port, the ability to run multiple instances
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
151 of the test simultaneously on the same host is compromised, which makes the
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
152 test a ticking time bomb in a buildbot environment. On Unix buildbots, this
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
153 may simply manifest as a failed test, which can be recovered from without
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
154 intervention in most cases, but on Windows, the entire python process can
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
155 completely and utterly wedge, requiring someone to log in to the buildbot
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
156 and manually kill the affected process.
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
157
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
158 (This is easy to reproduce on Windows, unfortunately, and can be traced to
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
159 the SO_REUSEADDR socket option having different semantics on Windows versus
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
160 Unix/Linux. On Unix, you can't have two AF_INET SOCK_STREAM sockets bind,
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
161 listen and then accept connections on identical host/ports. An EADDRINUSE
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
162 socket.error will be raised at some point (depending on the platform and
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
163 the order bind and listen were called on each socket).
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
164
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
165 However, on Windows, if SO_REUSEADDR is set on the sockets, no EADDRINUSE
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
166 will ever be raised when attempting to bind two identical host/ports. When
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
167 accept() is called on each socket, the second caller's process will steal
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
168 the port from the first caller, leaving them both in an awkwardly wedged
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
169 state where they'll no longer respond to any signals or graceful kills, and
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
170 must be forcibly killed via OpenProcess()/TerminateProcess().
38767
46f3eb474c84 Fix the socket tests so they can be run concurrently. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents: 38055
diff changeset
171
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
172 The solution on Windows is to use the SO_EXCLUSIVEADDRUSE socket option
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
173 instead of SO_REUSEADDR, which effectively affords the same semantics as
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
174 SO_REUSEADDR on Unix. Given the propensity of Unix developers in the Open
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
175 Source world compared to Windows ones, this is a common mistake. A quick
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
176 look over OpenSSL's 0.9.8g source shows that they use SO_REUSEADDR when
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
177 openssl.exe is called with the 's_server' option, for example. See
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
178 http://bugs.python.org/issue2550 for more info. The following site also
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
179 has a very thorough description about the implications of both REUSEADDR
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
180 and EXCLUSIVEADDRUSE on Windows:
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
181 http://msdn2.microsoft.com/en-us/library/ms740621(VS.85).aspx)
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
182
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
183 XXX: although this approach is a vast improvement on previous attempts to
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
184 elicit unused ports, it rests heavily on the assumption that the ephemeral
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
185 port returned to us by the OS won't immediately be dished back out to some
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
186 other process when we close and delete our temporary socket but before our
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
187 calling code has a chance to bind the returned port. We can deal with this
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
188 issue if/when we come across it."""
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
189 tempsock = socket.socket(family, socktype)
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
190 port = bind_port(tempsock)
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
191 tempsock.close()
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
192 del tempsock
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
193 return port
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
194
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
195 def bind_port(sock, host=HOST):
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
196 """Bind the socket to a free port and return the port number. Relies on
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
197 ephemeral ports in order to ensure we are using an unbound port. This is
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
198 important as many tests may be running simultaneously, especially in a
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
199 buildbot environment. This method raises an exception if the sock.family
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
200 is AF_INET and sock.type is SOCK_STREAM, *and* the socket has SO_REUSEADDR
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
201 or SO_REUSEPORT set on it. Tests should *never* set these socket options
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
202 for TCP/IP sockets. The only case for setting these options is testing
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
203 multicasting via multiple UDP sockets.
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
204
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
205 Additionally, if the SO_EXCLUSIVEADDRUSE socket option is available (i.e.
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
206 on Windows), it will be set on the socket. This will prevent anyone else
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
207 from bind()'ing to our host/port for the duration of the test.
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
208 """
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
209 if sock.family == socket.AF_INET and sock.type == socket.SOCK_STREAM:
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
210 if hasattr(socket, 'SO_REUSEADDR'):
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
211 if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) == 1:
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
212 raise TestFailed("tests should never set the SO_REUSEADDR " \
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
213 "socket option on TCP/IP sockets!")
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
214 if hasattr(socket, 'SO_REUSEPORT'):
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
215 if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT) == 1:
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
216 raise TestFailed("tests should never set the SO_REUSEPORT " \
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
217 "socket option on TCP/IP sockets!")
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
218 if hasattr(socket, 'SO_EXCLUSIVEADDRUSE'):
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
219 sock.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
220
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
221 sock.bind((host, 0))
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
222 port = sock.getsockname()[1]
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
223 return port
38767
46f3eb474c84 Fix the socket tests so they can be run concurrently. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents: 38055
diff changeset
224
1197
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
225 FUZZ = 1e-6
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
226
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
227 def fcmp(x, y): # fuzzy comparison function
48109
6f232fcfb657 Backport test.support.fcmp() from 3.0 to silence -3 warnings.
Brett Cannon <bcannon@gmail.com>
parents: 48053
diff changeset
228 if isinstance(x, float) or isinstance(y, float):
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
229 try:
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
230 fuzz = (abs(x) + abs(y)) * FUZZ
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
231 if abs(x-y) <= fuzz:
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
232 return 0
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
233 except:
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
234 pass
48109
6f232fcfb657 Backport test.support.fcmp() from 3.0 to silence -3 warnings.
Brett Cannon <bcannon@gmail.com>
parents: 48053
diff changeset
235 elif type(x) == type(y) and isinstance(x, (tuple, list)):
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
236 for i in range(min(len(x), len(y))):
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
237 outcome = fcmp(x[i], y[i])
15540
9d43129c8c55 Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 15305
diff changeset
238 if outcome != 0:
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
239 return outcome
48109
6f232fcfb657 Backport test.support.fcmp() from 3.0 to silence -3 warnings.
Brett Cannon <bcannon@gmail.com>
parents: 48053
diff changeset
240 return (len(x) > len(y)) - (len(x) < len(y))
6f232fcfb657 Backport test.support.fcmp() from 3.0 to silence -3 warnings.
Brett Cannon <bcannon@gmail.com>
parents: 48053
diff changeset
241 return (x > y) - (x < y)
1197
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
242
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18635
diff changeset
243 try:
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18635
diff changeset
244 unicode
41536
71b5c1e2c09c Change test_support.have_unicode to use True/False instead of 1/0.
Collin Winter <collinw@gmail.com>
parents: 41534
diff changeset
245 have_unicode = True
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18635
diff changeset
246 except NameError:
41536
71b5c1e2c09c Change test_support.have_unicode to use True/False instead of 1/0.
Collin Winter <collinw@gmail.com>
parents: 41534
diff changeset
247 have_unicode = False
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18635
diff changeset
248
25982
e1bf4604c0a5 Patch #631972: Adds an is_jython flag.
Finn Bock <bckfnn@worldonline.dk>
parents: 24442
diff changeset
249 is_jython = sys.platform.startswith('java')
e1bf4604c0a5 Patch #631972: Adds an is_jython flag.
Finn Bock <bckfnn@worldonline.dk>
parents: 24442
diff changeset
250
17213
4d6ee7f2bee6 In Jython, `@' is not allowed in module names. Extend the TESTFN test
Barry Warsaw <barry@python.org>
parents: 17171
diff changeset
251 # Filename used for testing
4d6ee7f2bee6 In Jython, `@' is not allowed in module names. Extend the TESTFN test
Barry Warsaw <barry@python.org>
parents: 17171
diff changeset
252 if os.name == 'java':
4d6ee7f2bee6 In Jython, `@' is not allowed in module names. Extend the TESTFN test
Barry Warsaw <barry@python.org>
parents: 17171
diff changeset
253 # Jython disallows @ in module names
4d6ee7f2bee6 In Jython, `@' is not allowed in module names. Extend the TESTFN test
Barry Warsaw <barry@python.org>
parents: 17171
diff changeset
254 TESTFN = '$test'
29066
90342f395721 Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents: 28968
diff changeset
255 elif os.name == 'riscos':
90342f395721 Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents: 28968
diff changeset
256 TESTFN = 'testfile'
90342f395721 Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents: 28968
diff changeset
257 else:
17213
4d6ee7f2bee6 In Jython, `@' is not allowed in module names. Extend the TESTFN test
Barry Warsaw <barry@python.org>
parents: 17171
diff changeset
258 TESTFN = '@test'
17817
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17815
diff changeset
259 # Unicode name only used if TEST_FN_ENCODING exists for the platform.
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18635
diff changeset
260 if have_unicode:
31078
2ba9a8696560 Add TESTFN_UNICODE_UNENCODEABLE, a unicode filename that can not be
Mark Hammond <mhammond@skippinet.com.au>
parents: 29890
diff changeset
261 # Assuming sys.getfilesystemencoding()!=sys.getdefaultencoding()
2ba9a8696560 Add TESTFN_UNICODE_UNENCODEABLE, a unicode filename that can not be
Mark Hammond <mhammond@skippinet.com.au>
parents: 29890
diff changeset
262 # TESTFN_UNICODE is a filename that can be encoded using the
2ba9a8696560 Add TESTFN_UNICODE_UNENCODEABLE, a unicode filename that can not be
Mark Hammond <mhammond@skippinet.com.au>
parents: 29890
diff changeset
263 # file system encoding, but *not* with the default (ascii) encoding
26139
144d9d48b96c Don't try to convert the test filename to Unicode with -U.
Martin v. Löwis <martin@v.loewis.de>
parents: 26128
diff changeset
264 if isinstance('', unicode):
144d9d48b96c Don't try to convert the test filename to Unicode with -U.
Martin v. Löwis <martin@v.loewis.de>
parents: 26128
diff changeset
265 # python -U
144d9d48b96c Don't try to convert the test filename to Unicode with -U.
Martin v. Löwis <martin@v.loewis.de>
parents: 26128
diff changeset
266 # XXX perhaps unicode() should accept Unicode strings?
31092
de391e9d70ea Typo repair; added some comments and horizontal whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 31087
diff changeset
267 TESTFN_UNICODE = "@test-\xe0\xf2"
26139
144d9d48b96c Don't try to convert the test filename to Unicode with -U.
Martin v. Löwis <martin@v.loewis.de>
parents: 26128
diff changeset
268 else:
31092
de391e9d70ea Typo repair; added some comments and horizontal whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 31087
diff changeset
269 # 2 latin characters.
de391e9d70ea Typo repair; added some comments and horizontal whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 31087
diff changeset
270 TESTFN_UNICODE = unicode("@test-\xe0\xf2", "latin-1")
de391e9d70ea Typo repair; added some comments and horizontal whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 31087
diff changeset
271 TESTFN_ENCODING = sys.getfilesystemencoding()
de391e9d70ea Typo repair; added some comments and horizontal whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 31087
diff changeset
272 # TESTFN_UNICODE_UNENCODEABLE is a filename that should *not* be
31078
2ba9a8696560 Add TESTFN_UNICODE_UNENCODEABLE, a unicode filename that can not be
Mark Hammond <mhammond@skippinet.com.au>
parents: 29890
diff changeset
273 # able to be encoded by *either* the default or filesystem encoding.
31092
de391e9d70ea Typo repair; added some comments and horizontal whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 31087
diff changeset
274 # This test really only makes sense on Windows NT platforms
31087
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
275 # which have special Unicode support in posixmodule.
31092
de391e9d70ea Typo repair; added some comments and horizontal whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 31087
diff changeset
276 if (not hasattr(sys, "getwindowsversion") or
de391e9d70ea Typo repair; added some comments and horizontal whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 31087
diff changeset
277 sys.getwindowsversion()[3] < 2): # 0=win32s or 1=9x/ME
31389
aca1ce1b4b61 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 31092
diff changeset
278 TESTFN_UNICODE_UNENCODEABLE = None
31078
2ba9a8696560 Add TESTFN_UNICODE_UNENCODEABLE, a unicode filename that can not be
Mark Hammond <mhammond@skippinet.com.au>
parents: 29890
diff changeset
279 else:
31087
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
280 # Japanese characters (I think - from bug 846133)
34707
0d44d9071e97 Build with --disable-unicode again. Fixes #1158607.
Martin v. Löwis <martin@v.loewis.de>
parents: 31389
diff changeset
281 TESTFN_UNICODE_UNENCODEABLE = eval('u"@test-\u5171\u6709\u3055\u308c\u308b"')
31087
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
282 try:
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
283 # XXX - Note - should be using TESTFN_ENCODING here - but for
31092
de391e9d70ea Typo repair; added some comments and horizontal whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 31087
diff changeset
284 # Windows, "mbcs" currently always operates as if in
31087
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
285 # errors=ignore' mode - hence we get '?' characters rather than
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
286 # the exception. 'Latin1' operates as we expect - ie, fails.
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
287 # See [ 850997 ] mbcs encoding ignores errors
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
288 TESTFN_UNICODE_UNENCODEABLE.encode("Latin1")
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
289 except UnicodeEncodeError:
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
290 pass
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
291 else:
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
292 print \
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
293 'WARNING: The filename %r CAN be encoded by the filesystem. ' \
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
294 'Unicode filename tests may not be effective' \
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
295 % TESTFN_UNICODE_UNENCODEABLE
26000
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
296
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
297 # Make sure we can write to TESTFN, try in /tmp if we can't
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
298 fp = None
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
299 try:
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
300 fp = open(TESTFN, 'w+')
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
301 except IOError:
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
302 TMP_TESTFN = os.path.join('/tmp', TESTFN)
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
303 try:
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
304 fp = open(TMP_TESTFN, 'w+')
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
305 TESTFN = TMP_TESTFN
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
306 del TMP_TESTFN
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
307 except IOError:
26128
23ef7c163a12 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26000
diff changeset
308 print ('WARNING: tests will fail, unable to write to: %s or %s' %
26000
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
309 (TESTFN, TMP_TESTFN))
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
310 if fp is not None:
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
311 fp.close()
36382
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
312 unlink(TESTFN)
41539
761ce3946b86 Import and raise statement cleanup.
Collin Winter <collinw@gmail.com>
parents: 41536
diff changeset
313 del fp
17036
ad741745cb6c Oops. A RISCOS patch I forgot to check in.
Guido van Rossum <guido@python.org>
parents: 16723
diff changeset
314
8127
047533789359 Move unified findfile() into test_support.py
Guido van Rossum <guido@python.org>
parents: 4791
diff changeset
315 def findfile(file, here=__file__):
28968
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
316 """Try to find a file on sys.path and the working directory. If it is not
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
317 found the argument passed to the function is returned (this does not
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
318 necessarily signal failure; could still be the legitimate path)."""
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
319 if os.path.isabs(file):
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
320 return file
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
321 path = sys.path
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
322 path = [os.path.dirname(here)] + path
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
323 for dn in path:
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
324 fn = os.path.join(dn, file)
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
325 if os.path.exists(fn): return fn
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
326 return file
15957
c1a8deb15f0e This patch removes all uses of "assert" in the regression test suite
Marc-André Lemburg <mal@egenix.com>
parents: 15540
diff changeset
327
58491
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
328 def verify(condition, reason='test failed'):
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
329 """Verify that condition is true. If not, raise TestFailed.
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
330
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
331 The optional argument reason can be given to provide
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
332 a better error text.
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
333 """
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
334
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
335 if not condition:
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
336 raise TestFailed(reason)
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
337
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
338 def vereq(a, b):
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
339 """Raise TestFailed if a == b is false.
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
340
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
341 This is better than verify(a == b) because, in case of failure, the
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
342 error message incorporates repr(a) and repr(b) so you can see the
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
343 inputs.
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
344
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
345 Note that "not (a == b)" isn't necessarily the same as "a != b"; the
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
346 former is tested.
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
347 """
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
348
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
349 if not (a == b):
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
350 raise TestFailed("%r == %r" % (a, b))
6985c0e85b4b Revert r77730 and add back verify and vereq in case other projects use them, but leave the changes in test_pprint and string_tests.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58475
diff changeset
351
17815
d6df8d371b67 Get rid of the superstitious "~" in dict hashing's "i = (~hash) & mask".
Tim Peters <tim.peters@gmail.com>
parents: 17213
diff changeset
352 def sortdict(dict):
d6df8d371b67 Get rid of the superstitious "~" in dict hashing's "i = (~hash) & mask".
Tim Peters <tim.peters@gmail.com>
parents: 17213
diff changeset
353 "Like repr(dict), but in sorted order."
d6df8d371b67 Get rid of the superstitious "~" in dict hashing's "i = (~hash) & mask".
Tim Peters <tim.peters@gmail.com>
parents: 17213
diff changeset
354 items = dict.items()
d6df8d371b67 Get rid of the superstitious "~" in dict hashing's "i = (~hash) & mask".
Tim Peters <tim.peters@gmail.com>
parents: 17213
diff changeset
355 items.sort()
d6df8d371b67 Get rid of the superstitious "~" in dict hashing's "i = (~hash) & mask".
Tim Peters <tim.peters@gmail.com>
parents: 17213
diff changeset
356 reprpairs = ["%r: %r" % pair for pair in items]
d6df8d371b67 Get rid of the superstitious "~" in dict hashing's "i = (~hash) & mask".
Tim Peters <tim.peters@gmail.com>
parents: 17213
diff changeset
357 withcommas = ", ".join(reprpairs)
d6df8d371b67 Get rid of the superstitious "~" in dict hashing's "i = (~hash) & mask".
Tim Peters <tim.peters@gmail.com>
parents: 17213
diff changeset
358 return "{%s}" % withcommas
d6df8d371b67 Get rid of the superstitious "~" in dict hashing's "i = (~hash) & mask".
Tim Peters <tim.peters@gmail.com>
parents: 17213
diff changeset
359
50914
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
360 def make_bad_fd():
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
361 """
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
362 Create an invalid file descriptor by opening and closing a file and return
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
363 its fd.
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
364 """
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
365 file = open(TESTFN, "wb")
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
366 try:
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
367 return file.fileno()
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
368 finally:
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
369 file.close()
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
370 unlink(TESTFN)
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
371
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39417
diff changeset
372 def check_syntax_error(testcase, statement):
16679
db4fc82aa44b Add test for syntax error on "x = 1 + 1".
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16069
diff changeset
373 try:
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39417
diff changeset
374 compile(statement, '<test string>', 'exec')
16679
db4fc82aa44b Add test for syntax error on "x = 1 + 1".
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16069
diff changeset
375 except SyntaxError:
db4fc82aa44b Add test for syntax error on "x = 1 + 1".
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16069
diff changeset
376 pass
db4fc82aa44b Add test for syntax error on "x = 1 + 1".
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16069
diff changeset
377 else:
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39417
diff changeset
378 testcase.fail('Missing SyntaxError: "%s"' % statement)
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
379
35998
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
380 def open_urlresource(url):
57053
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
381 import urlparse, urllib2
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
382
41654
370b91533c53 Fix a bug in test.test_support.open_urlresource().
Collin Winter <collinw@gmail.com>
parents: 41539
diff changeset
383 requires('urlfetch')
35998
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
384 filename = urlparse.urlparse(url)[2].split('/')[-1] # '/': it's URL!
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
385
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
386 for path in [os.path.curdir, os.path.pardir]:
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
387 fn = os.path.join(path, filename)
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
388 if os.path.exists(fn):
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
389 return open(fn)
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
390
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
391 print >> get_original_stdout(), '\tfetching %s ...' % url
57053
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
392 f = urllib2.urlopen(url, timeout=15)
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
393 try:
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
394 with open(filename, "wb") as out:
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
395 s = f.read()
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
396 while s:
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
397 out.write(s)
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
398 s = f.read()
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
399 finally:
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
400 f.close()
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
401 return open(filename)
40732
5dae0d7f683a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40537
diff changeset
402
40537
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
403
49146
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
404 class WarningsRecorder(object):
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
405 """Convenience wrapper for the warnings list returned on
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
406 entry to the warnings.catch_warnings() context manager.
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
407 """
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
408 def __init__(self, warnings_list):
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
409 self.warnings = warnings_list
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
410
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
411 def __getattr__(self, attr):
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
412 if self.warnings:
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
413 return getattr(self.warnings[-1], attr)
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
414 elif attr in warnings.WarningMessage._WARNING_DETAILS:
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
415 return None
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
416 raise AttributeError("%r has no attribute %r" % (self, attr))
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
417
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
418 def reset(self):
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
419 del self.warnings[:]
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
420
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
421 @contextlib.contextmanager
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
422 def check_warnings():
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
423 with warnings.catch_warnings(record=True) as w:
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
424 yield WarningsRecorder(w)
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
425
f28b999a69f8 Issue #3781: Final cleanup of warnings.catch_warnings and its usage in the test suite. Closes issue w.r.t. 2.6 (R: Brett Cannon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 49099
diff changeset
426
46861
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
427 class CleanImport(object):
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
428 """Context manager to force import to return a new module reference.
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
429
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
430 This is useful for testing module-level behaviours, such as
47942
b35c8b1940af Make test.test_support.catch_warnings more robust as discussed on python-dev. Also add explicit tests for itto test_warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents: 47347
diff changeset
431 the emission of a DeprecationWarning on import.
46861
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
432
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
433 Use like this:
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
434
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
435 with CleanImport("foo"):
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
436 __import__("foo") # new reference
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
437 """
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
438
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
439 def __init__(self, *module_names):
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
440 self.original_modules = sys.modules.copy()
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
441 for module_name in module_names:
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
442 if module_name in sys.modules:
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
443 module = sys.modules[module_name]
46863
224e07b65861 Fixed typo in a comment of test_support.CleanImport.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46861
diff changeset
444 # It is possible that module_name is just an alias for
46861
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
445 # another module (e.g. stub for modules renamed in 3.x).
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
446 # In that case, we also need delete the real module to clear
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
447 # the import cache.
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
448 if module.__name__ != module_name:
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
449 del sys.modules[module.__name__]
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
450 del sys.modules[module_name]
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
451
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
452 def __enter__(self):
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
453 return self
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
454
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
455 def __exit__(self, *ignore_exc):
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
456 sys.modules.update(self.original_modules)
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
457
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
458
40537
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
459 class EnvironmentVarGuard(object):
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
460
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
461 """Class to help protect the environment variable properly. Can be used as
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
462 a context manager."""
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
463
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
464 def __init__(self):
53383
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
465 self._changed = {}
40537
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
466
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
467 def set(self, envvar, value):
53383
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
468 # Remember the initial value on the first access
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
469 if envvar not in self._changed:
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
470 self._changed[envvar] = os.environ.get(envvar)
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
471 os.environ[envvar] = value
40537
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
472
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
473 def unset(self, envvar):
53383
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
474 # Remember the initial value on the first access
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
475 if envvar not in self._changed:
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
476 self._changed[envvar] = os.environ.get(envvar)
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
477 if envvar in os.environ:
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
478 del os.environ[envvar]
40537
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
479
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
480 def __enter__(self):
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
481 return self
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
482
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
483 def __exit__(self, *ignore_exc):
53383
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
484 for (k, v) in self._changed.items():
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
485 if v is None:
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
486 if k in os.environ:
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
487 del os.environ[k]
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
488 else:
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
489 os.environ[k] = v
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
490
40537
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
491
41081
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
492 class TransientResource(object):
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
493
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
494 """Raise ResourceDenied if an exception is raised while the context manager
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
495 is in effect that matches the specified exception and attributes."""
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
496
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
497 def __init__(self, exc, **kwargs):
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
498 self.exc = exc
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
499 self.attrs = kwargs
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
500
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
501 def __enter__(self):
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
502 return self
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
503
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
504 def __exit__(self, type_=None, value=None, traceback=None):
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
505 """If type_ is a subclass of self.exc and value has attributes matching
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
506 self.attrs, raise ResourceDenied. Otherwise let the exception
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
507 propagate (if any)."""
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
508 if type_ is not None and issubclass(self.exc, type_):
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
509 for attr, attr_value in self.attrs.iteritems():
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
510 if not hasattr(value, attr):
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
511 break
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
512 if getattr(value, attr) != attr_value:
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
513 break
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
514 else:
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
515 raise ResourceDenied("an optional resource is not available")
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
516
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
517
41135
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
518 def transient_internet():
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
519 """Return a context manager that raises ResourceDenied when various issues
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
520 with the Internet connection manifest themselves as exceptions."""
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
521 time_out = TransientResource(IOError, errno=errno.ETIMEDOUT)
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
522 socket_peer_reset = TransientResource(socket.error, errno=errno.ECONNRESET)
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
523 ioerror_peer_reset = TransientResource(IOError, errno=errno.ECONNRESET)
41136
84478deb4ad9 Fix a typo where the variable name was not updated.
Brett Cannon <bcannon@gmail.com>
parents: 41135
diff changeset
524 return contextlib.nested(time_out, socket_peer_reset, ioerror_peer_reset)
41135
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
525
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
526
42783
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
527 @contextlib.contextmanager
46241
7dff5e41fa7a Generalize test.test_support.test_stdout() with a base context manager so that
Brett Cannon <bcannon@gmail.com>
parents: 45513
diff changeset
528 def captured_output(stream_name):
7dff5e41fa7a Generalize test.test_support.test_stdout() with a base context manager so that
Brett Cannon <bcannon@gmail.com>
parents: 45513
diff changeset
529 """Run the 'with' statement body using a StringIO object in place of a
7dff5e41fa7a Generalize test.test_support.test_stdout() with a base context manager so that
Brett Cannon <bcannon@gmail.com>
parents: 45513
diff changeset
530 specific attribute on the sys module.
7dff5e41fa7a Generalize test.test_support.test_stdout() with a base context manager so that
Brett Cannon <bcannon@gmail.com>
parents: 45513
diff changeset
531 Example use (with 'stream_name=stdout')::
42783
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
532
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
533 with captured_stdout() as s:
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
534 print "hello"
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
535 assert s.getvalue() == "hello"
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
536 """
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
537 import StringIO
46241
7dff5e41fa7a Generalize test.test_support.test_stdout() with a base context manager so that
Brett Cannon <bcannon@gmail.com>
parents: 45513
diff changeset
538 orig_stdout = getattr(sys, stream_name)
7dff5e41fa7a Generalize test.test_support.test_stdout() with a base context manager so that
Brett Cannon <bcannon@gmail.com>
parents: 45513
diff changeset
539 setattr(sys, stream_name, StringIO.StringIO())
46611
3036e21ac661 make test_support's captured_output a bit more robust when exceptions happen
Benjamin Peterson <benjamin@python.org>
parents: 46425
diff changeset
540 try:
3036e21ac661 make test_support's captured_output a bit more robust when exceptions happen
Benjamin Peterson <benjamin@python.org>
parents: 46425
diff changeset
541 yield getattr(sys, stream_name)
3036e21ac661 make test_support's captured_output a bit more robust when exceptions happen
Benjamin Peterson <benjamin@python.org>
parents: 46425
diff changeset
542 finally:
3036e21ac661 make test_support's captured_output a bit more robust when exceptions happen
Benjamin Peterson <benjamin@python.org>
parents: 46425
diff changeset
543 setattr(sys, stream_name, orig_stdout)
46241
7dff5e41fa7a Generalize test.test_support.test_stdout() with a base context manager so that
Brett Cannon <bcannon@gmail.com>
parents: 45513
diff changeset
544
7dff5e41fa7a Generalize test.test_support.test_stdout() with a base context manager so that
Brett Cannon <bcannon@gmail.com>
parents: 45513
diff changeset
545 def captured_stdout():
7dff5e41fa7a Generalize test.test_support.test_stdout() with a base context manager so that
Brett Cannon <bcannon@gmail.com>
parents: 45513
diff changeset
546 return captured_output("stdout")
42783
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
547
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
548
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
549 #=======================================================================
38055
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
550 # Decorator for running a function in a different locale, correctly resetting
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
551 # it afterwards.
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
552
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
553 def run_with_locale(catstr, *locales):
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
554 def decorator(func):
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
555 def inner(*args, **kwds):
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
556 try:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
557 import locale
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
558 category = getattr(locale, catstr)
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
559 orig_locale = locale.setlocale(category)
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
560 except AttributeError:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
561 # if the test author gives us an invalid category string
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
562 raise
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
563 except:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
564 # cannot retrieve original locale, so do nothing
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
565 locale = orig_locale = None
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
566 else:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
567 for loc in locales:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
568 try:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
569 locale.setlocale(category, loc)
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
570 break
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
571 except:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
572 pass
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
573
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
574 # now run the function, resetting the locale on exceptions
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
575 try:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
576 return func(*args, **kwds)
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
577 finally:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
578 if locale and orig_locale:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
579 locale.setlocale(category, orig_locale)
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
580 inner.func_name = func.func_name
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
581 inner.__doc__ = func.__doc__
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
582 return inner
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
583 return decorator
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
584
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
585 #=======================================================================
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
586 # Big-memory-test support. Separate from 'resources' because memory use should be configurable.
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
587
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
588 # Some handy shorthands. Note that these are used for byte-limits as well
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
589 # as size-limits, in the various bigmem tests
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
590 _1M = 1024*1024
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
591 _1G = 1024 * _1M
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
592 _2G = 2 * _1G
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
593 _4G = 4 * _1G
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
594
47347
343aaaa802e8 remove some __getslice__
Benjamin Peterson <benjamin@python.org>
parents: 47316
diff changeset
595 MAX_Py_ssize_t = sys.maxsize
39417
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
596
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
597 def set_memlimit(limit):
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
598 import re
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
599 global max_memuse
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
600 global real_max_memuse
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
601 sizes = {
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
602 'k': 1024,
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
603 'm': _1M,
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
604 'g': _1G,
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
605 't': 1024*_1G,
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
606 }
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
607 m = re.match(r'(\d+(\.\d+)?) (K|M|G|T)b?$', limit,
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
608 re.IGNORECASE | re.VERBOSE)
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
609 if m is None:
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
610 raise ValueError('Invalid memory limit %r' % (limit,))
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
611 memlimit = int(float(m.group(1)) * sizes[m.group(3).lower()])
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
612 real_max_memuse = memlimit
39417
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
613 if memlimit > MAX_Py_ssize_t:
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
614 memlimit = MAX_Py_ssize_t
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
615 if memlimit < _2G - 1:
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
616 raise ValueError('Memory limit %r too low to be useful' % (limit,))
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
617 max_memuse = memlimit
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
618
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
619 def bigmemtest(minsize, memuse, overhead=5*_1M):
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
620 """Decorator for bigmem tests.
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
621
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
622 'minsize' is the minimum useful size for the test (in arbitrary,
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
623 test-interpreted units.) 'memuse' is the number of 'bytes per size' for
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
624 the test, or a good estimate of it. 'overhead' specifies fixed overhead,
46425
29231a973580 spelling
Skip Montanaro <skip@pobox.com>
parents: 46414
diff changeset
625 independent of the testsize, and defaults to 5Mb.
37993
358662e2f273 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37992
diff changeset
626
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
627 The decorator tries to guess a good value for 'size' and passes it to
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
628 the decorated test function. If minsize * memuse is more than the
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
629 allowed memory use (as defined by max_memuse), the test is skipped.
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
630 Otherwise, minsize is adjusted upward to use up to max_memuse.
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
631 """
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
632 def decorator(f):
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
633 def wrapper(self):
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
634 if not max_memuse:
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
635 # If max_memuse is 0 (the default),
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
636 # we still want to run the tests with size set to a few kb,
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
637 # to make sure they work. We still want to avoid using
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
638 # too much memory, though, but we do that noisily.
38015
940ce5a76c5f Do the small-memory run of big-meormy tests using a prime number, rather
Thomas Wouters <thomas@python.org>
parents: 37993
diff changeset
639 maxsize = 5147
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
640 self.failIf(maxsize * memuse + overhead > 20 * _1M)
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
641 else:
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
642 maxsize = int((max_memuse - overhead) / memuse)
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
643 if maxsize < minsize:
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
644 # Really ought to print 'test skipped' or something
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
645 if verbose:
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
646 sys.stderr.write("Skipping %s because of memory "
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
647 "constraint\n" % (f.__name__,))
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
648 return
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
649 # Try to keep some breathing room in memory use
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
650 maxsize = max(maxsize - 50 * _1M, minsize)
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
651 return f(self, maxsize)
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
652 wrapper.minsize = minsize
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
653 wrapper.memuse = memuse
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
654 wrapper.overhead = overhead
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
655 return wrapper
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
656 return decorator
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
657
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
658 def precisionbigmemtest(size, memuse, overhead=5*_1M):
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
659 def decorator(f):
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
660 def wrapper(self):
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
661 if not real_max_memuse:
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
662 maxsize = 5147
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
663 else:
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
664 maxsize = size
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
665
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
666 if real_max_memuse and real_max_memuse < maxsize * memuse:
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
667 if verbose:
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
668 sys.stderr.write("Skipping %s because of memory "
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
669 "constraint\n" % (f.__name__,))
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
670 return
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
671
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
672 return f(self, maxsize)
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
673 wrapper.size = size
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
674 wrapper.memuse = memuse
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
675 wrapper.overhead = overhead
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
676 return wrapper
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
677 return decorator
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
678
39417
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
679 def bigaddrspacetest(f):
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
680 """Decorator for tests that fill the address space."""
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
681 def wrapper(self):
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
682 if max_memuse < MAX_Py_ssize_t:
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
683 if verbose:
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
684 sys.stderr.write("Skipping %s because of memory "
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
685 "constraint\n" % (f.__name__,))
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
686 else:
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
687 return f(self)
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
688 return wrapper
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
689
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
690 #=======================================================================
41539
761ce3946b86 Import and raise statement cleanup.
Collin Winter <collinw@gmail.com>
parents: 41536
diff changeset
691 # unittest integration.
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
692
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17109
diff changeset
693 class BasicTestRunner:
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
694 def run(self, test):
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17109
diff changeset
695 result = unittest.TestResult()
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
696 test(result)
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
697 return result
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
698
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
699
41534
6c4d7678af5e Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents: 41375
diff changeset
700 def _run_suite(suite):
19995
b07cb51d731c run_suite(): Oops, update a docstring.
Barry Warsaw <barry@python.org>
parents: 19994
diff changeset
701 """Run tests from a unittest.TestSuite-derived class."""
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
702 if verbose:
17171
4f68d3f8fdc8 When the regression test is run in verbose mode, make the PyUNIT-based
Fred Drake <fdrake@acm.org>
parents: 17133
diff changeset
703 runner = unittest.TextTestRunner(sys.stdout, verbosity=2)
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
704 else:
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17109
diff changeset
705 runner = BasicTestRunner()
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
706
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17109
diff changeset
707 result = runner.run(suite)
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17109
diff changeset
708 if not result.wasSuccessful():
18635
c8e4728d2ab0 Give more useful information about a failing PyUnit-style test.
Fred Drake <fdrake@acm.org>
parents: 17817
diff changeset
709 if len(result.errors) == 1 and not result.failures:
c8e4728d2ab0 Give more useful information about a failing PyUnit-style test.
Fred Drake <fdrake@acm.org>
parents: 17817
diff changeset
710 err = result.errors[0][1]
c8e4728d2ab0 Give more useful information about a failing PyUnit-style test.
Fred Drake <fdrake@acm.org>
parents: 17817
diff changeset
711 elif len(result.failures) == 1 and not result.errors:
c8e4728d2ab0 Give more useful information about a failing PyUnit-style test.
Fred Drake <fdrake@acm.org>
parents: 17817
diff changeset
712 err = result.failures[0][1]
c8e4728d2ab0 Give more useful information about a failing PyUnit-style test.
Fred Drake <fdrake@acm.org>
parents: 17817
diff changeset
713 else:
56930
044a68bc0d2e Merged revisions 75528 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53383
diff changeset
714 err = "multiple errors occurred"
044a68bc0d2e Merged revisions 75528 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53383
diff changeset
715 if not verbose:
044a68bc0d2e Merged revisions 75528 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53383
diff changeset
716 err += "; run in verbose mode for details"
19807
086ffafaed55 It appears that unittest was changed to stop hoarding raw exception data,
Tim Peters <tim.peters@gmail.com>
parents: 19768
diff changeset
717 raise TestFailed(err)
19817
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19807
diff changeset
718
19994
ab6c14822de1 run_suite(): Factor this out of run_unittest() for tests that build
Barry Warsaw <barry@python.org>
parents: 19819
diff changeset
719
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28863
diff changeset
720 def run_unittest(*classes):
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28863
diff changeset
721 """Run tests from unittest.TestCase-derived classes."""
41534
6c4d7678af5e Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents: 41375
diff changeset
722 valid_types = (unittest.TestSuite, unittest.TestCase)
28863
6d852ebff36f Factor out common boilerplate for test_support
Raymond Hettinger <python@rcn.com>
parents: 28799
diff changeset
723 suite = unittest.TestSuite()
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28863
diff changeset
724 for cls in classes:
41534
6c4d7678af5e Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents: 41375
diff changeset
725 if isinstance(cls, str):
6c4d7678af5e Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents: 41375
diff changeset
726 if cls in sys.modules:
6c4d7678af5e Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents: 41375
diff changeset
727 suite.addTest(unittest.findTestCases(sys.modules[cls]))
6c4d7678af5e Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents: 41375
diff changeset
728 else:
6c4d7678af5e Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents: 41375
diff changeset
729 raise ValueError("str arguments must be keys in sys.modules")
6c4d7678af5e Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents: 41375
diff changeset
730 elif isinstance(cls, valid_types):
29880
3f214b39b4df run_unittest() to support TestCase instances as well as classes. Helps with doctests.
Raymond Hettinger <python@rcn.com>
parents: 29142
diff changeset
731 suite.addTest(cls)
3f214b39b4df run_unittest() to support TestCase instances as well as classes. Helps with doctests.
Raymond Hettinger <python@rcn.com>
parents: 29142
diff changeset
732 else:
3f214b39b4df run_unittest() to support TestCase instances as well as classes. Helps with doctests.
Raymond Hettinger <python@rcn.com>
parents: 29142
diff changeset
733 suite.addTest(unittest.makeSuite(cls))
41534
6c4d7678af5e Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents: 41375
diff changeset
734 _run_suite(suite)
28863
6d852ebff36f Factor out common boilerplate for test_support
Raymond Hettinger <python@rcn.com>
parents: 28799
diff changeset
735
19994
ab6c14822de1 run_suite(): Factor this out of run_unittest() for tests that build
Barry Warsaw <barry@python.org>
parents: 19819
diff changeset
736
19817
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19807
diff changeset
737 #=======================================================================
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19807
diff changeset
738 # doctest driver.
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19807
diff changeset
739
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19807
diff changeset
740 def run_doctest(module, verbosity=None):
20227
92f86436b6be SF bug [#467336] doctest failures w/ new-style classes.
Tim Peters <tim.peters@gmail.com>
parents: 20091
diff changeset
741 """Run doctest on the given module. Return (#failures, #tests).
19817
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19807
diff changeset
742
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19807
diff changeset
743 If optional argument verbosity is not specified (or is None), pass
19819
53e0cae3cf6c Repair late-night doc typos.
Tim Peters <tim.peters@gmail.com>
parents: 19817
diff changeset
744 test_support's belief about verbosity on to doctest. Else doctest's
53e0cae3cf6c Repair late-night doc typos.
Tim Peters <tim.peters@gmail.com>
parents: 19817
diff changeset
745 usual behavior is used (it searches sys.argv for -v).
19817
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19807
diff changeset
746 """
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19807
diff changeset
747
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19807
diff changeset
748 import doctest
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19807
diff changeset
749
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19807
diff changeset
750 if verbosity is None:
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19807
diff changeset
751 verbosity = verbose
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19807
diff changeset
752 else:
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19807
diff changeset
753 verbosity = None
b7396287cf65 Teach regrtest how to pass on doctest failure msgs. This is done via a
Tim Peters <tim.peters@gmail.com>
parents: 19807
diff changeset
754
20088
386fb26e0874 Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents: 20083
diff changeset
755 # Direct doctest output (normally just errors) to real stdout; doctest
386fb26e0874 Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents: 20083
diff changeset
756 # output shouldn't be compared by regrtest.
386fb26e0874 Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents: 20083
diff changeset
757 save_stdout = sys.stdout
20091
40d4d1cd6c84 Guido points out that sys.__stdout__ is a bit bucket under IDLE. So keep
Tim Peters <tim.peters@gmail.com>
parents: 20088
diff changeset
758 sys.stdout = get_original_stdout()
20088
386fb26e0874 Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents: 20083
diff changeset
759 try:
386fb26e0874 Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents: 20083
diff changeset
760 f, t = doctest.testmod(module, verbose=verbosity)
386fb26e0874 Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents: 20083
diff changeset
761 if f:
386fb26e0874 Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents: 20083
diff changeset
762 raise TestFailed("%d of %d doctests failed" % (f, t))
386fb26e0874 Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents: 20083
diff changeset
763 finally:
386fb26e0874 Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents: 20083
diff changeset
764 sys.stdout = save_stdout
29140
e57e22aa56a4 Provide a clue that the doctests have run.
Raymond Hettinger <python@rcn.com>
parents: 29066
diff changeset
765 if verbose:
29142
5da198480b6a Include module name in doctest summary.
Raymond Hettinger <python@rcn.com>
parents: 29140
diff changeset
766 print 'doctest (%s) ... %d tests with zero failures' % (module.__name__, t)
29140
e57e22aa56a4 Provide a clue that the doctests have run.
Raymond Hettinger <python@rcn.com>
parents: 29066
diff changeset
767 return f, t
38867
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
768
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
769 #=======================================================================
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
770 # Threading support to prevent reporting refleaks when running regrtest.py -R
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
771
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
772 def threading_setup():
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
773 import threading
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
774 return len(threading._active), len(threading._limbo)
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
775
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
776 def threading_cleanup(num_active, num_limbo):
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
777 import threading
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
778 import time
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
779
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
780 _MAX_COUNT = 10
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
781 count = 0
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
782 while len(threading._active) != num_active and count < _MAX_COUNT:
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
783 count += 1
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
784 time.sleep(0.1)
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
785
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
786 count = 0
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
787 while len(threading._limbo) != num_limbo and count < _MAX_COUNT:
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
788 count += 1
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
789 time.sleep(0.1)
38961
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
790
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
791 def reap_children():
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
792 """Use this function at the end of test_main() whenever sub-processes
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
793 are started. This will help ensure that no extra children (zombies)
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
794 stick around to hog resources and create problems when looking
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
795 for refleaks.
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
796 """
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
797
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
798 # Reap all our dead child processes so we don't leave zombies around.
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
799 # These hog resources and might be causing some of the buildbots to die.
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
800 if hasattr(os, 'waitpid'):
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
801 any_process = -1
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
802 while True:
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
803 try:
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
804 # This will raise an exception on Windows. That's ok.
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
805 pid, status = os.waitpid(any_process, os.WNOHANG)
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
806 if pid == 0:
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
807 break
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
808 except:
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
809 break