blob: deae47dd50c5425f272335b892bc9b3196eeda8c [file] [log] [blame]
Brett Cannonf1cfb622003-05-04 21:15:27 +00001"""Supporting definitions for the Python regression tests."""
Guido van Rossum3bead091992-01-27 17:00:37 +00002
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003if __name__ != 'test.support':
4 raise ImportError('support must be imported from the test package')
Barry Warsaw408b6d32002-07-30 23:27:12 +00005
Guido van Rossumd8faa362007-04-27 19:54:29 +00006import contextlib
7import errno
8import socket
Fred Drakecd1b1dd2001-03-21 18:26:33 +00009import sys
Guido van Rossumd8faa362007-04-27 19:54:29 +000010import os
11import os.path
Christian Heimes23daade02008-02-25 12:39:23 +000012import shutil
Thomas Wouters902d6eb2007-01-09 23:18:33 +000013import warnings
Guido van Rossumd8faa362007-04-27 19:54:29 +000014import unittest
Fred Drakecd1b1dd2001-03-21 18:26:33 +000015
Benjamin Peterson744c2cd2008-05-26 16:26:37 +000016__all__ = ["Error", "TestFailed", "TestSkipped", "ResourceDenied", "import_module",
17 "verbose", "use_resources", "max_memuse", "record_original_stdout",
18 "get_original_stdout", "unload", "unlink", "rmtree", "forget",
19 "is_resource_enabled", "requires", "find_unused_port", "bind_port",
20 "fcmp", "have_unicode", "is_jython", "TESTFN", "HOST", "FUZZ",
21 "findfile", "verify", "vereq", "sortdict", "check_syntax_error",
22 "open_urlresource", "WarningMessage", "catch_warning", "CleanImport",
23 "EnvironmentVarGuard", "TransientResource", "captured_output",
24 "captured_stdout", "TransientResource", "transient_internet",
25 "run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest",
26 "BasicTestRunner", "run_unittest", "run_doctest", "threading_setup",
27 "threading_cleanup", "reap_children"]
28
Fred Drake1790dd42000-07-24 06:55:00 +000029class Error(Exception):
Fred Drake004d5e62000-10-23 17:22:08 +000030 """Base class for regression test exceptions."""
Fred Drake1790dd42000-07-24 06:55:00 +000031
32class TestFailed(Error):
Fred Drake004d5e62000-10-23 17:22:08 +000033 """Test failed."""
Fred Drake1790dd42000-07-24 06:55:00 +000034
35class TestSkipped(Error):
Fred Drake004d5e62000-10-23 17:22:08 +000036 """Test skipped.
Fred Drake1790dd42000-07-24 06:55:00 +000037
Fred Drake004d5e62000-10-23 17:22:08 +000038 This can be raised to indicate that a test was deliberatly
39 skipped, but not because a feature wasn't available. For
40 example, if some resource can't be used, such as the network
41 appears to be unavailable, this should be raised instead of
42 TestFailed.
Fred Drake004d5e62000-10-23 17:22:08 +000043 """
Fred Drake1790dd42000-07-24 06:55:00 +000044
Fred Drake9a0db072003-02-03 15:19:30 +000045class ResourceDenied(TestSkipped):
46 """Test skipped because it requested a disallowed resource.
47
48 This is raised when a test calls requires() for a resource that
49 has not be enabled. It is used to distinguish between expected
50 and unexpected skips.
51 """
52
Benjamin Peterson699adb92008-05-08 22:27:58 +000053def import_module(name, deprecated=False):
54 """Import the module to be tested, raising TestSkipped if it is not
55 available."""
56 with catch_warning(record=False):
57 if deprecated:
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +000058 warnings.filterwarnings("ignore", ".+ (module|package)",
59 DeprecationWarning)
Benjamin Peterson699adb92008-05-08 22:27:58 +000060 try:
61 module = __import__(name, level=0)
62 except ImportError:
63 raise TestSkipped("No module named " + name)
64 else:
65 return module
66
Barry Warsawc0fb6052001-08-20 22:29:23 +000067verbose = 1 # Flag set to 0 by regrtest.py
Thomas Wouters477c8d52006-05-27 19:21:47 +000068use_resources = None # Flag set to [] by regrtest.py
69max_memuse = 0 # Disable bigmem tests (they will still be run with
70 # small sizes, to make sure they work.)
Guido van Rossum531661c1996-12-20 02:58:22 +000071
Tim Peters8dee8092001-09-25 20:05:11 +000072# _original_stdout is meant to hold stdout at the time regrtest began.
73# This may be "the real" stdout, or IDLE's emulation of stdout, or whatever.
74# The point is to have some flavor of stdout the user can actually see.
75_original_stdout = None
76def record_original_stdout(stdout):
77 global _original_stdout
78 _original_stdout = stdout
79
80def get_original_stdout():
81 return _original_stdout or sys.stdout
82
Guido van Rossum3bead091992-01-27 17:00:37 +000083def unload(name):
Fred Drake004d5e62000-10-23 17:22:08 +000084 try:
85 del sys.modules[name]
86 except KeyError:
87 pass
Guido van Rossum3bead091992-01-27 17:00:37 +000088
Neal Norwitz0e17f8c2006-01-23 07:51:27 +000089def unlink(filename):
Neal Norwitz0e17f8c2006-01-23 07:51:27 +000090 try:
91 os.unlink(filename)
92 except OSError:
93 pass
94
Christian Heimes23daade02008-02-25 12:39:23 +000095def rmtree(path):
96 try:
97 shutil.rmtree(path)
98 except OSError as e:
99 # Unix returns ENOENT, Windows returns ESRCH.
100 if e.errno not in (errno.ENOENT, errno.ESRCH):
101 raise
102
Guido van Rossum3bead091992-01-27 17:00:37 +0000103def forget(modname):
Brett Cannonf1cfb622003-05-04 21:15:27 +0000104 '''"Forget" a module was ever imported by removing it from sys.modules and
105 deleting any .pyc and .pyo files.'''
Fred Drake004d5e62000-10-23 17:22:08 +0000106 unload(modname)
Fred Drake004d5e62000-10-23 17:22:08 +0000107 for dirname in sys.path:
Skip Montanaro7a98be22007-08-16 14:35:24 +0000108 unlink(os.path.join(dirname, modname + '.pyc'))
Brett Cannonf1cfb622003-05-04 21:15:27 +0000109 # Deleting the .pyo file cannot be within the 'try' for the .pyc since
110 # the chance exists that there is no .pyc (and thus the 'try' statement
111 # is exited) but there is a .pyo file.
Skip Montanaro7a98be22007-08-16 14:35:24 +0000112 unlink(os.path.join(dirname, modname + '.pyo'))
Guido van Rossum3bead091992-01-27 17:00:37 +0000113
Tim Petersb4ee4eb2002-12-04 03:26:57 +0000114def is_resource_enabled(resource):
Brett Cannonf1cfb622003-05-04 21:15:27 +0000115 """Test whether a resource is enabled. Known resources are set by
116 regrtest.py."""
Tim Petersb4ee4eb2002-12-04 03:26:57 +0000117 return use_resources is not None and resource in use_resources
118
Barry Warsawc0fb6052001-08-20 22:29:23 +0000119def requires(resource, msg=None):
Brett Cannonf1cfb622003-05-04 21:15:27 +0000120 """Raise ResourceDenied if the specified resource is not available.
121
122 If the caller's module is __main__ then automatically return True. The
123 possibility of False being returned occurs when regrtest.py is executing."""
Skip Montanarod839ecd2003-04-24 19:06:57 +0000124 # see if the caller's module is __main__ - if so, treat as if
125 # the resource was set
126 if sys._getframe().f_back.f_globals.get("__name__") == "__main__":
127 return
Tim Petersb4ee4eb2002-12-04 03:26:57 +0000128 if not is_resource_enabled(resource):
Barry Warsawc0fb6052001-08-20 22:29:23 +0000129 if msg is None:
130 msg = "Use of the `%s' resource not enabled" % resource
Fred Drake9a0db072003-02-03 15:19:30 +0000131 raise ResourceDenied(msg)
Barry Warsawc0fb6052001-08-20 22:29:23 +0000132
Christian Heimes5e696852008-04-09 08:37:03 +0000133HOST = 'localhost'
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000134
Christian Heimes5e696852008-04-09 08:37:03 +0000135def find_unused_port(family=socket.AF_INET, socktype=socket.SOCK_STREAM):
136 """Returns an unused port that should be suitable for binding. This is
137 achieved by creating a temporary socket with the same family and type as
138 the 'sock' parameter (default is AF_INET, SOCK_STREAM), and binding it to
139 the specified host address (defaults to 0.0.0.0) with the port set to 0,
140 eliciting an unused ephemeral port from the OS. The temporary socket is
141 then closed and deleted, and the ephemeral port is returned.
142
143 Either this method or bind_port() should be used for any tests where a
144 server socket needs to be bound to a particular port for the duration of
145 the test. Which one to use depends on whether the calling code is creating
146 a python socket, or if an unused port needs to be provided in a constructor
147 or passed to an external program (i.e. the -accept argument to openssl's
148 s_server mode). Always prefer bind_port() over find_unused_port() where
149 possible. Hard coded ports should *NEVER* be used. As soon as a server
150 socket is bound to a hard coded port, the ability to run multiple instances
151 of the test simultaneously on the same host is compromised, which makes the
152 test a ticking time bomb in a buildbot environment. On Unix buildbots, this
153 may simply manifest as a failed test, which can be recovered from without
154 intervention in most cases, but on Windows, the entire python process can
155 completely and utterly wedge, requiring someone to log in to the buildbot
156 and manually kill the affected process.
157
158 (This is easy to reproduce on Windows, unfortunately, and can be traced to
159 the SO_REUSEADDR socket option having different semantics on Windows versus
160 Unix/Linux. On Unix, you can't have two AF_INET SOCK_STREAM sockets bind,
161 listen and then accept connections on identical host/ports. An EADDRINUSE
162 socket.error will be raised at some point (depending on the platform and
163 the order bind and listen were called on each socket).
164
165 However, on Windows, if SO_REUSEADDR is set on the sockets, no EADDRINUSE
166 will ever be raised when attempting to bind two identical host/ports. When
167 accept() is called on each socket, the second caller's process will steal
168 the port from the first caller, leaving them both in an awkwardly wedged
169 state where they'll no longer respond to any signals or graceful kills, and
170 must be forcibly killed via OpenProcess()/TerminateProcess().
171
172 The solution on Windows is to use the SO_EXCLUSIVEADDRUSE socket option
173 instead of SO_REUSEADDR, which effectively affords the same semantics as
174 SO_REUSEADDR on Unix. Given the propensity of Unix developers in the Open
175 Source world compared to Windows ones, this is a common mistake. A quick
176 look over OpenSSL's 0.9.8g source shows that they use SO_REUSEADDR when
177 openssl.exe is called with the 's_server' option, for example. See
178 http://bugs.python.org/issue2550 for more info. The following site also
179 has a very thorough description about the implications of both REUSEADDR
180 and EXCLUSIVEADDRUSE on Windows:
181 http://msdn2.microsoft.com/en-us/library/ms740621(VS.85).aspx)
182
183 XXX: although this approach is a vast improvement on previous attempts to
184 elicit unused ports, it rests heavily on the assumption that the ephemeral
185 port returned to us by the OS won't immediately be dished back out to some
186 other process when we close and delete our temporary socket but before our
187 calling code has a chance to bind the returned port. We can deal with this
188 issue if/when we come across it.
189 """
190
191 tempsock = socket.socket(family, socktype)
192 port = bind_port(tempsock)
193 tempsock.close()
194 del tempsock
195 return port
196
197def bind_port(sock, host=HOST):
198 """Bind the socket to a free port and return the port number. Relies on
199 ephemeral ports in order to ensure we are using an unbound port. This is
200 important as many tests may be running simultaneously, especially in a
201 buildbot environment. This method raises an exception if the sock.family
202 is AF_INET and sock.type is SOCK_STREAM, *and* the socket has SO_REUSEADDR
203 or SO_REUSEPORT set on it. Tests should *never* set these socket options
204 for TCP/IP sockets. The only case for setting these options is testing
205 multicasting via multiple UDP sockets.
206
207 Additionally, if the SO_EXCLUSIVEADDRUSE socket option is available (i.e.
208 on Windows), it will be set on the socket. This will prevent anyone else
209 from bind()'ing to our host/port for the duration of the test.
210 """
211
212 if sock.family == socket.AF_INET and sock.type == socket.SOCK_STREAM:
213 if hasattr(socket, 'SO_REUSEADDR'):
214 if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) == 1:
215 raise TestFailed("tests should never set the SO_REUSEADDR " \
216 "socket option on TCP/IP sockets!")
217 if hasattr(socket, 'SO_REUSEPORT'):
218 if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT) == 1:
219 raise TestFailed("tests should never set the SO_REUSEPORT " \
220 "socket option on TCP/IP sockets!")
221 if hasattr(socket, 'SO_EXCLUSIVEADDRUSE'):
222 sock.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
223
224 sock.bind((host, 0))
225 port = sock.getsockname()[1]
226 return port
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000227
Guido van Rossum35fb82a1993-01-26 13:04:43 +0000228FUZZ = 1e-6
229
230def fcmp(x, y): # fuzzy comparison function
Neal Norwitz79212992006-08-21 16:27:31 +0000231 if isinstance(x, float) or isinstance(y, float):
Fred Drake004d5e62000-10-23 17:22:08 +0000232 try:
Fred Drake004d5e62000-10-23 17:22:08 +0000233 fuzz = (abs(x) + abs(y)) * FUZZ
234 if abs(x-y) <= fuzz:
235 return 0
236 except:
237 pass
Neal Norwitz79212992006-08-21 16:27:31 +0000238 elif type(x) == type(y) and isinstance(x, (tuple, list)):
Fred Drake004d5e62000-10-23 17:22:08 +0000239 for i in range(min(len(x), len(y))):
240 outcome = fcmp(x[i], y[i])
Fred Drake132dce22000-12-12 23:11:42 +0000241 if outcome != 0:
Fred Drake004d5e62000-10-23 17:22:08 +0000242 return outcome
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000243 return (len(x) > len(y)) - (len(x) < len(y))
244 return (x > y) - (x < y)
Guido van Rossum35fb82a1993-01-26 13:04:43 +0000245
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000246try:
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000247 str
Neal Norwitz79212992006-08-21 16:27:31 +0000248 have_unicode = True
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000249except NameError:
Neal Norwitz79212992006-08-21 16:27:31 +0000250 have_unicode = False
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000251
Finn Bock57bc5fa2002-11-01 18:02:03 +0000252is_jython = sys.platform.startswith('java')
253
Barry Warsaw559f6682001-03-23 18:04:02 +0000254# Filename used for testing
255if os.name == 'java':
256 # Jython disallows @ in module names
257 TESTFN = '$test'
Martin v. Löwisa94568a2003-05-10 07:36:56 +0000258else:
Barry Warsaw559f6682001-03-23 18:04:02 +0000259 TESTFN = '@test'
Walter Dörwald9b775532007-06-08 14:30:53 +0000260
261 # Assuming sys.getfilesystemencoding()!=sys.getdefaultencoding()
262 # TESTFN_UNICODE is a filename that can be encoded using the
263 # file system encoding, but *not* with the default (ascii) encoding
264 TESTFN_UNICODE = "@test-\xe0\xf2"
265 TESTFN_ENCODING = sys.getfilesystemencoding()
266 # TESTFN_UNICODE_UNENCODEABLE is a filename that should *not* be
267 # able to be encoded by *either* the default or filesystem encoding.
268 # This test really only makes sense on Windows NT platforms
269 # which have special Unicode support in posixmodule.
270 if (not hasattr(sys, "getwindowsversion") or
271 sys.getwindowsversion()[3] < 2): # 0=win32s or 1=9x/ME
272 TESTFN_UNICODE_UNENCODEABLE = None
273 else:
274 # Japanese characters (I think - from bug 846133)
275 TESTFN_UNICODE_UNENCODEABLE = "@test-\u5171\u6709\u3055\u308c\u308b"
276 try:
277 # XXX - Note - should be using TESTFN_ENCODING here - but for
278 # Windows, "mbcs" currently always operates as if in
279 # errors=ignore' mode - hence we get '?' characters rather than
280 # the exception. 'Latin1' operates as we expect - ie, fails.
281 # See [ 850997 ] mbcs encoding ignores errors
282 TESTFN_UNICODE_UNENCODEABLE.encode("Latin1")
283 except UnicodeEncodeError:
284 pass
Martin v. Löwis2411a2d2002-11-09 19:57:26 +0000285 else:
Georg Brandldb028442008-02-05 20:48:58 +0000286 print('WARNING: The filename %r CAN be encoded by the filesystem. '
287 'Unicode filename tests may not be effective'
288 % TESTFN_UNICODE_UNENCODEABLE)
Neal Norwitz26a1eef2002-11-03 00:35:53 +0000289
290# Make sure we can write to TESTFN, try in /tmp if we can't
291fp = None
292try:
293 fp = open(TESTFN, 'w+')
294except IOError:
295 TMP_TESTFN = os.path.join('/tmp', TESTFN)
296 try:
297 fp = open(TMP_TESTFN, 'w+')
298 TESTFN = TMP_TESTFN
299 del TMP_TESTFN
300 except IOError:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000301 print(('WARNING: tests will fail, unable to write to: %s or %s' %
302 (TESTFN, TMP_TESTFN)))
Neal Norwitz26a1eef2002-11-03 00:35:53 +0000303if fp is not None:
304 fp.close()
Neal Norwitz0e17f8c2006-01-23 07:51:27 +0000305 unlink(TESTFN)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000306del fp
Guido van Rossuma8f7e592001-03-13 09:31:07 +0000307
Guido van Rossume26132c1998-04-23 20:13:30 +0000308def findfile(file, here=__file__):
Brett Cannonf1cfb622003-05-04 21:15:27 +0000309 """Try to find a file on sys.path and the working directory. If it is not
310 found the argument passed to the function is returned (this does not
311 necessarily signal failure; could still be the legitimate path)."""
Fred Drake004d5e62000-10-23 17:22:08 +0000312 if os.path.isabs(file):
313 return file
Fred Drake004d5e62000-10-23 17:22:08 +0000314 path = sys.path
315 path = [os.path.dirname(here)] + path
316 for dn in path:
317 fn = os.path.join(dn, file)
318 if os.path.exists(fn): return fn
319 return file
Marc-André Lemburg36619082001-01-17 19:11:13 +0000320
321def verify(condition, reason='test failed'):
Guido van Rossuma1374e42001-01-19 19:01:56 +0000322 """Verify that condition is true. If not, raise TestFailed.
Marc-André Lemburg36619082001-01-17 19:11:13 +0000323
Skip Montanaroc955f892001-01-20 19:12:54 +0000324 The optional argument reason can be given to provide
Tim Peters983874d2001-01-19 05:59:21 +0000325 a better error text.
Tim Petersd2bf3b72001-01-18 02:22:22 +0000326 """
Tim Peters983874d2001-01-19 05:59:21 +0000327
Tim Petersd2bf3b72001-01-18 02:22:22 +0000328 if not condition:
Guido van Rossuma1374e42001-01-19 19:01:56 +0000329 raise TestFailed(reason)
Jeremy Hylton47793992001-02-19 15:35:26 +0000330
Tim Petersc2fe6182001-10-30 23:20:46 +0000331def vereq(a, b):
Tim Peters77902972001-12-29 17:34:57 +0000332 """Raise TestFailed if a == b is false.
333
334 This is better than verify(a == b) because, in case of failure, the
335 error message incorporates repr(a) and repr(b) so you can see the
336 inputs.
337
338 Note that "not (a == b)" isn't necessarily the same as "a != b"; the
339 former is tested.
340 """
341
Tim Petersc2fe6182001-10-30 23:20:46 +0000342 if not (a == b):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000343 raise TestFailed("%r == %r" % (a, b))
Tim Petersc2fe6182001-10-30 23:20:46 +0000344
Tim Peters2f228e72001-05-13 00:19:31 +0000345def sortdict(dict):
346 "Like repr(dict), but in sorted order."
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000347 items = sorted(dict.items())
Tim Peters2f228e72001-05-13 00:19:31 +0000348 reprpairs = ["%r: %r" % pair for pair in items]
349 withcommas = ", ".join(reprpairs)
350 return "{%s}" % withcommas
351
Thomas Wouters89f507f2006-12-13 04:49:30 +0000352def check_syntax_error(testcase, statement):
Jeremy Hylton47793992001-02-19 15:35:26 +0000353 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000354 compile(statement, '<test string>', 'exec')
Jeremy Hylton47793992001-02-19 15:35:26 +0000355 except SyntaxError:
356 pass
357 else:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000358 testcase.fail('Missing SyntaxError: "%s"' % statement)
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000359
Martin v. Löwis234a34a2007-08-30 20:58:02 +0000360def open_urlresource(url, *args, **kw):
Hye-Shik Changaaa2f1d2005-12-10 17:44:27 +0000361 import urllib, urlparse
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000362
Guido van Rossum360e4b82007-05-14 22:51:27 +0000363 requires('urlfetch')
Hye-Shik Changaaa2f1d2005-12-10 17:44:27 +0000364 filename = urlparse.urlparse(url)[2].split('/')[-1] # '/': it's URL!
365
366 for path in [os.path.curdir, os.path.pardir]:
367 fn = os.path.join(path, filename)
368 if os.path.exists(fn):
Martin v. Löwis234a34a2007-08-30 20:58:02 +0000369 return open(fn, *args, **kw)
Hye-Shik Changaaa2f1d2005-12-10 17:44:27 +0000370
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000371 print('\tfetching %s ...' % url, file=get_original_stdout())
Hye-Shik Changaaa2f1d2005-12-10 17:44:27 +0000372 fn, _ = urllib.urlretrieve(url, filename)
Martin v. Löwis234a34a2007-08-30 20:58:02 +0000373 return open(fn, *args, **kw)
Thomas Wouters9fe394c2007-02-05 01:24:16 +0000374
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000375
Guido van Rossumd8faa362007-04-27 19:54:29 +0000376class WarningMessage(object):
377 "Holds the result of the latest showwarning() call"
378 def __init__(self):
379 self.message = None
380 self.category = None
381 self.filename = None
382 self.lineno = None
383
Christian Heimes33fe8092008-04-13 13:53:33 +0000384 def _showwarning(self, message, category, filename, lineno, file=None,
385 line=None):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000386 self.message = message
387 self.category = category
388 self.filename = filename
389 self.lineno = lineno
Christian Heimes33fe8092008-04-13 13:53:33 +0000390 self.line = line
391
392 def reset(self):
393 self._showwarning(*((None,)*6))
394
395 def __str__(self):
396 return ("{message : %r, category : %r, filename : %r, lineno : %s, "
397 "line : %r}" % (self.message,
398 self.category.__name__ if self.category else None,
399 self.filename, self.lineno, self.line))
400
Guido van Rossumd8faa362007-04-27 19:54:29 +0000401
402@contextlib.contextmanager
Benjamin Peterson699adb92008-05-08 22:27:58 +0000403def catch_warning(module=warnings, record=True):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000404 """
405 Guard the warnings filter from being permanently changed and record the
406 data of the last warning that has been issued.
407
408 Use like this:
409
Guido van Rossumaf554a02007-08-16 23:48:43 +0000410 with catch_warning() as w:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000411 warnings.warn("foo")
412 assert str(w.message) == "foo"
413 """
Christian Heimes33fe8092008-04-13 13:53:33 +0000414 original_filters = module.filters[:]
415 original_showwarning = module.showwarning
Benjamin Peterson699adb92008-05-08 22:27:58 +0000416 if record:
417 warning_obj = WarningMessage()
418 module.showwarning = warning_obj._showwarning
Guido van Rossumd8faa362007-04-27 19:54:29 +0000419 try:
Benjamin Peterson699adb92008-05-08 22:27:58 +0000420 yield warning_obj if record else None
Guido van Rossumd8faa362007-04-27 19:54:29 +0000421 finally:
Christian Heimes33fe8092008-04-13 13:53:33 +0000422 module.showwarning = original_showwarning
423 module.filters = original_filters
Guido van Rossumd8faa362007-04-27 19:54:29 +0000424
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000425
426class CleanImport(object):
427 """Context manager to force import to return a new module reference.
428
429 This is useful for testing module-level behaviours, such as
430 the emission of a DepreciationWarning on import.
431
432 Use like this:
433
434 with CleanImport("foo"):
435 __import__("foo") # new reference
436 """
437
438 def __init__(self, *module_names):
439 self.original_modules = sys.modules.copy()
440 for module_name in module_names:
441 if module_name in sys.modules:
442 module = sys.modules[module_name]
443 # It is possible that module_name is just an alias for
444 # another module (e.g. stub for modules renamed in 3.x).
445 # In that case, we also need delete the real module to clear
446 # the import cache.
447 if module.__name__ != module_name:
448 del sys.modules[module.__name__]
449 del sys.modules[module_name]
450
451 def __enter__(self):
452 return self
453
454 def __exit__(self, *ignore_exc):
455 sys.modules.update(self.original_modules)
456
457
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000458class EnvironmentVarGuard(object):
459
460 """Class to help protect the environment variable properly. Can be used as
461 a context manager."""
462
463 def __init__(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000464 self._environ = os.environ
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000465 self._unset = set()
466 self._reset = dict()
467
468 def set(self, envvar, value):
469 if envvar not in self._environ:
470 self._unset.add(envvar)
471 else:
472 self._reset[envvar] = self._environ[envvar]
473 self._environ[envvar] = value
474
475 def unset(self, envvar):
476 if envvar in self._environ:
477 self._reset[envvar] = self._environ[envvar]
478 del self._environ[envvar]
479
480 def __enter__(self):
481 return self
482
483 def __exit__(self, *ignore_exc):
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000484 for envvar, value in self._reset.items():
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000485 self._environ[envvar] = value
486 for unset in self._unset:
487 del self._environ[unset]
488
Guido van Rossumd8faa362007-04-27 19:54:29 +0000489class TransientResource(object):
490
491 """Raise ResourceDenied if an exception is raised while the context manager
492 is in effect that matches the specified exception and attributes."""
493
494 def __init__(self, exc, **kwargs):
495 self.exc = exc
496 self.attrs = kwargs
497
498 def __enter__(self):
499 return self
500
501 def __exit__(self, type_=None, value=None, traceback=None):
502 """If type_ is a subclass of self.exc and value has attributes matching
503 self.attrs, raise ResourceDenied. Otherwise let the exception
504 propagate (if any)."""
505 if type_ is not None and issubclass(self.exc, type_):
506 for attr, attr_value in self.attrs.items():
507 if not hasattr(value, attr):
508 break
509 if getattr(value, attr) != attr_value:
510 break
511 else:
512 raise ResourceDenied("an optional resource is not available")
513
514
515def transient_internet():
516 """Return a context manager that raises ResourceDenied when various issues
517 with the Internet connection manifest themselves as exceptions."""
518 time_out = TransientResource(IOError, errno=errno.ETIMEDOUT)
519 socket_peer_reset = TransientResource(socket.error, errno=errno.ECONNRESET)
520 ioerror_peer_reset = TransientResource(IOError, errno=errno.ECONNRESET)
521 return contextlib.nested(time_out, socket_peer_reset, ioerror_peer_reset)
522
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000523
Thomas Woutersed03b412007-08-28 21:37:11 +0000524@contextlib.contextmanager
Benjamin Petersonad9d48d2008-04-02 21:49:44 +0000525def captured_output(stream_name):
526 """Run the 'with' statement body using a StringIO object in place of a
527 specific attribute on the sys module.
528 Example use (with 'stream_name=stdout')::
Thomas Woutersed03b412007-08-28 21:37:11 +0000529
530 with captured_stdout() as s:
Neal Norwitz752abd02008-05-13 04:55:24 +0000531 print("hello")
Thomas Woutersed03b412007-08-28 21:37:11 +0000532 assert s.getvalue() == "hello"
533 """
534 import io
Benjamin Petersonad9d48d2008-04-02 21:49:44 +0000535 orig_stdout = getattr(sys, stream_name)
536 setattr(sys, stream_name, io.StringIO())
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000537 try:
538 yield getattr(sys, stream_name)
539 finally:
540 setattr(sys, stream_name, orig_stdout)
Benjamin Petersonad9d48d2008-04-02 21:49:44 +0000541
542def captured_stdout():
543 return captured_output("stdout")
Thomas Woutersed03b412007-08-28 21:37:11 +0000544
545
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000546#=======================================================================
Thomas Wouters477c8d52006-05-27 19:21:47 +0000547# Decorator for running a function in a different locale, correctly resetting
548# it afterwards.
549
550def run_with_locale(catstr, *locales):
551 def decorator(func):
552 def inner(*args, **kwds):
553 try:
554 import locale
555 category = getattr(locale, catstr)
556 orig_locale = locale.setlocale(category)
557 except AttributeError:
558 # if the test author gives us an invalid category string
559 raise
560 except:
561 # cannot retrieve original locale, so do nothing
562 locale = orig_locale = None
563 else:
564 for loc in locales:
565 try:
566 locale.setlocale(category, loc)
567 break
568 except:
569 pass
570
571 # now run the function, resetting the locale on exceptions
572 try:
573 return func(*args, **kwds)
574 finally:
575 if locale and orig_locale:
576 locale.setlocale(category, orig_locale)
Neal Norwitz221085d2007-02-25 20:55:47 +0000577 inner.__name__ = func.__name__
Thomas Wouters477c8d52006-05-27 19:21:47 +0000578 inner.__doc__ = func.__doc__
579 return inner
580 return decorator
581
582#=======================================================================
Georg Brandldb028442008-02-05 20:48:58 +0000583# Big-memory-test support. Separate from 'resources' because memory use
584# should be configurable.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000585
586# Some handy shorthands. Note that these are used for byte-limits as well
587# as size-limits, in the various bigmem tests
588_1M = 1024*1024
589_1G = 1024 * _1M
590_2G = 2 * _1G
591
Thomas Woutersd2cf20e2007-08-30 22:57:53 +0000592MAX_Py_ssize_t = sys.maxsize
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000593
Thomas Wouters477c8d52006-05-27 19:21:47 +0000594def set_memlimit(limit):
595 import re
596 global max_memuse
597 sizes = {
598 'k': 1024,
599 'm': _1M,
600 'g': _1G,
601 't': 1024*_1G,
602 }
603 m = re.match(r'(\d+(\.\d+)?) (K|M|G|T)b?$', limit,
604 re.IGNORECASE | re.VERBOSE)
605 if m is None:
606 raise ValueError('Invalid memory limit %r' % (limit,))
607 memlimit = int(float(m.group(1)) * sizes[m.group(3).lower()])
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000608 if memlimit > MAX_Py_ssize_t:
609 memlimit = MAX_Py_ssize_t
610 if memlimit < _2G - 1:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000611 raise ValueError('Memory limit %r too low to be useful' % (limit,))
612 max_memuse = memlimit
613
614def bigmemtest(minsize, memuse, overhead=5*_1M):
615 """Decorator for bigmem tests.
616
617 'minsize' is the minimum useful size for the test (in arbitrary,
618 test-interpreted units.) 'memuse' is the number of 'bytes per size' for
619 the test, or a good estimate of it. 'overhead' specifies fixed overhead,
Christian Heimes33fe8092008-04-13 13:53:33 +0000620 independent of the testsize, and defaults to 5Mb.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000621
622 The decorator tries to guess a good value for 'size' and passes it to
623 the decorated test function. If minsize * memuse is more than the
624 allowed memory use (as defined by max_memuse), the test is skipped.
625 Otherwise, minsize is adjusted upward to use up to max_memuse.
626 """
627 def decorator(f):
628 def wrapper(self):
629 if not max_memuse:
630 # If max_memuse is 0 (the default),
631 # we still want to run the tests with size set to a few kb,
632 # to make sure they work. We still want to avoid using
633 # too much memory, though, but we do that noisily.
634 maxsize = 5147
635 self.failIf(maxsize * memuse + overhead > 20 * _1M)
636 else:
637 maxsize = int((max_memuse - overhead) / memuse)
638 if maxsize < minsize:
639 # Really ought to print 'test skipped' or something
640 if verbose:
641 sys.stderr.write("Skipping %s because of memory "
642 "constraint\n" % (f.__name__,))
643 return
644 # Try to keep some breathing room in memory use
645 maxsize = max(maxsize - 50 * _1M, minsize)
646 return f(self, maxsize)
647 wrapper.minsize = minsize
648 wrapper.memuse = memuse
649 wrapper.overhead = overhead
650 return wrapper
651 return decorator
652
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000653def bigaddrspacetest(f):
654 """Decorator for tests that fill the address space."""
655 def wrapper(self):
656 if max_memuse < MAX_Py_ssize_t:
657 if verbose:
658 sys.stderr.write("Skipping %s because of memory "
659 "constraint\n" % (f.__name__,))
660 else:
661 return f(self)
662 return wrapper
663
Thomas Wouters477c8d52006-05-27 19:21:47 +0000664#=======================================================================
Guido van Rossumd8faa362007-04-27 19:54:29 +0000665# unittest integration.
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000666
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000667class BasicTestRunner:
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000668 def run(self, test):
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000669 result = unittest.TestResult()
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000670 test(result)
671 return result
672
673
Guido van Rossumd8faa362007-04-27 19:54:29 +0000674def _run_suite(suite):
Barry Warsawc88425e2001-09-20 06:31:22 +0000675 """Run tests from a unittest.TestSuite-derived class."""
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000676 if verbose:
Fred Drake84a59342001-03-23 04:21:17 +0000677 runner = unittest.TextTestRunner(sys.stdout, verbosity=2)
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000678 else:
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000679 runner = BasicTestRunner()
Fred Drakecd1b1dd2001-03-21 18:26:33 +0000680
Steve Purcell5ddd1a82001-03-22 08:45:36 +0000681 result = runner.run(suite)
682 if not result.wasSuccessful():
Fred Drake14f6c182001-07-16 18:51:32 +0000683 if len(result.errors) == 1 and not result.failures:
684 err = result.errors[0][1]
685 elif len(result.failures) == 1 and not result.errors:
686 err = result.failures[0][1]
687 else:
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000688 err = "errors occurred; run in verbose mode for details"
Tim Peters2d84f2c2001-09-08 03:37:56 +0000689 raise TestFailed(err)
Tim Petersa0a62222001-09-09 06:12:01 +0000690
Barry Warsawc10d6902001-09-20 06:30:41 +0000691
Walter Dörwald21d3a322003-05-01 17:45:56 +0000692def run_unittest(*classes):
693 """Run tests from unittest.TestCase-derived classes."""
Guido van Rossumd8faa362007-04-27 19:54:29 +0000694 valid_types = (unittest.TestSuite, unittest.TestCase)
Raymond Hettinger9dcbbea2003-04-27 07:54:23 +0000695 suite = unittest.TestSuite()
Walter Dörwald21d3a322003-05-01 17:45:56 +0000696 for cls in classes:
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000697 if isinstance(cls, str):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000698 if cls in sys.modules:
699 suite.addTest(unittest.findTestCases(sys.modules[cls]))
700 else:
701 raise ValueError("str arguments must be keys in sys.modules")
702 elif isinstance(cls, valid_types):
Raymond Hettinger21d99872003-07-16 02:59:32 +0000703 suite.addTest(cls)
704 else:
705 suite.addTest(unittest.makeSuite(cls))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000706 _run_suite(suite)
Raymond Hettinger9dcbbea2003-04-27 07:54:23 +0000707
Barry Warsawc10d6902001-09-20 06:30:41 +0000708
Tim Petersa0a62222001-09-09 06:12:01 +0000709#=======================================================================
710# doctest driver.
711
712def run_doctest(module, verbosity=None):
Tim Peters17111f32001-10-03 04:08:26 +0000713 """Run doctest on the given module. Return (#failures, #tests).
Tim Petersa0a62222001-09-09 06:12:01 +0000714
715 If optional argument verbosity is not specified (or is None), pass
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000716 support's belief about verbosity on to doctest. Else doctest's
Tim Petersbea3fb82001-09-10 01:39:21 +0000717 usual behavior is used (it searches sys.argv for -v).
Tim Petersa0a62222001-09-09 06:12:01 +0000718 """
719
720 import doctest
721
722 if verbosity is None:
723 verbosity = verbose
724 else:
725 verbosity = None
726
Tim Peters342ca752001-09-25 19:13:20 +0000727 # Direct doctest output (normally just errors) to real stdout; doctest
728 # output shouldn't be compared by regrtest.
729 save_stdout = sys.stdout
Tim Peters8dee8092001-09-25 20:05:11 +0000730 sys.stdout = get_original_stdout()
Tim Peters342ca752001-09-25 19:13:20 +0000731 try:
732 f, t = doctest.testmod(module, verbose=verbosity)
733 if f:
734 raise TestFailed("%d of %d doctests failed" % (f, t))
735 finally:
736 sys.stdout = save_stdout
Raymond Hettinger35b34bd2003-05-17 00:58:33 +0000737 if verbose:
Georg Brandldb028442008-02-05 20:48:58 +0000738 print('doctest (%s) ... %d tests with zero failures' %
739 (module.__name__, t))
Raymond Hettinger35b34bd2003-05-17 00:58:33 +0000740 return f, t
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000741
742#=======================================================================
743# Threading support to prevent reporting refleaks when running regrtest.py -R
744
745def threading_setup():
746 import threading
747 return len(threading._active), len(threading._limbo)
748
749def threading_cleanup(num_active, num_limbo):
750 import threading
751 import time
752
753 _MAX_COUNT = 10
754 count = 0
755 while len(threading._active) != num_active and count < _MAX_COUNT:
756 count += 1
757 time.sleep(0.1)
758
759 count = 0
760 while len(threading._limbo) != num_limbo and count < _MAX_COUNT:
761 count += 1
762 time.sleep(0.1)
763
764def reap_children():
765 """Use this function at the end of test_main() whenever sub-processes
766 are started. This will help ensure that no extra children (zombies)
767 stick around to hog resources and create problems when looking
768 for refleaks.
769 """
770
771 # Reap all our dead child processes so we don't leave zombies around.
772 # These hog resources and might be causing some of the buildbots to die.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000773 if hasattr(os, 'waitpid'):
774 any_process = -1
775 while True:
776 try:
777 # This will raise an exception on Windows. That's ok.
778 pid, status = os.waitpid(any_process, os.WNOHANG)
779 if pid == 0:
780 break
781 except:
782 break