annotate Lib/test/test_support.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents e45186edac91
children d74b8edd7a8c
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
54085
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
8 import functools
41135
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
9 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
10 import sys
41539
761ce3946b86 Import and raise statement cleanup.
Collin Winter <collinw@gmail.com>
parents: 41536
diff changeset
11 import os
52350
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
12 import platform
45506
55eefb43ef6f Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
13 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
14 import warnings
41539
761ce3946b86 Import and raise statement cleanup.
Collin Winter <collinw@gmail.com>
parents: 41536
diff changeset
15 import unittest
52491
f4c5e3a64be2 Change more tests to use import_module for the modules that
R. David Murray <rdmurray@bitdance.com>
parents: 52456
diff changeset
16 import importlib
53644
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
17 import UserDict
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
18
52353
e6009b9cd908 remove test_support.TestSkipped and just use unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 52350
diff changeset
19 __all__ = ["Error", "TestFailed", "ResourceDenied", "import_module",
47316
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
20 "verbose", "use_resources", "max_memuse", "record_original_stdout",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
21 "get_original_stdout", "unload", "unlink", "rmtree", "forget",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
22 "is_resource_enabled", "requires", "find_unused_port", "bind_port",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
23 "fcmp", "have_unicode", "is_jython", "TESTFN", "HOST", "FUZZ",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
24 "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
25 "open_urlresource", "check_warnings", "CleanImport",
48013
4ec5a0ec631a Remove duplicate entry in __all__.
Georg Brandl <georg@python.org>
parents: 47942
diff changeset
26 "EnvironmentVarGuard", "captured_output",
47316
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
27 "captured_stdout", "TransientResource", "transient_internet",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
28 "run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
29 "BasicTestRunner", "run_unittest", "run_doctest", "threading_setup",
52350
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
30 "threading_cleanup", "reap_children", "cpython_only",
52491
f4c5e3a64be2 Change more tests to use import_module for the modules that
R. David Murray <rdmurray@bitdance.com>
parents: 52456
diff changeset
31 "check_impl_detail", "get_attribute"]
47316
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
32
13682
7de18f04f85a Restore TestSkipped exception; appears to have disappeared in last checkin.
Fred Drake <fdrake@acm.org>
parents: 13647
diff changeset
33 class Error(Exception):
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
34 """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
35
7de18f04f85a Restore TestSkipped exception; appears to have disappeared in last checkin.
Fred Drake <fdrake@acm.org>
parents: 13647
diff changeset
36 class TestFailed(Error):
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
37 """Test failed."""
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
52354
14cc92ae94c9 fix naming
Benjamin Peterson <benjamin@python.org>
parents: 52353
diff changeset
39 class ResourceDenied(unittest.SkipTest):
27636
974f196abb87 test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents: 26527
diff changeset
40 """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
41
974f196abb87 test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents: 26527
diff changeset
42 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
43 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
44 and unexpected skips.
974f196abb87 test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents: 26527
diff changeset
45 """
974f196abb87 test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents: 26527
diff changeset
46
53048
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
47 @contextlib.contextmanager
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
48 def _ignore_deprecated_imports(ignore=True):
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
49 """Context manager to suppress package and module deprecation
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
50 warnings when importing them.
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
51
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
52 If ignore is False, this context manager has no effect."""
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
53 if ignore:
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
54 with warnings.catch_warnings():
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
55 warnings.filterwarnings("ignore", ".+ (module|package)",
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
56 DeprecationWarning)
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
57 yield
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
58 else:
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
59 yield
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
60
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
61
46752
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
62 def import_module(name, deprecated=False):
52573
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
63 """Import and return the module to be tested, raising SkipTest if
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
64 it is not available.
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
65
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
66 If deprecated is True, any module or package deprecation messages
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
67 will be suppressed."""
53048
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
68 with _ignore_deprecated_imports(deprecated):
46752
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
69 try:
53048
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
70 return importlib.import_module(name)
52555
7641a3f0b2ec A few more test skips via import_module, and change import_module to
R. David Murray <rdmurray@bitdance.com>
parents: 52491
diff changeset
71 except ImportError, msg:
7641a3f0b2ec A few more test skips via import_module, and change import_module to
R. David Murray <rdmurray@bitdance.com>
parents: 52491
diff changeset
72 raise unittest.SkipTest(str(msg))
53048
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
73
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
74
53315
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
75 def _save_and_remove_module(name, orig_modules):
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
76 """Helper function to save and remove a module from sys.modules
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
77
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
78 Return value is True if the module was in sys.modules and
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
79 False otherwise."""
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
80 saved = True
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
81 try:
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
82 orig_modules[name] = sys.modules[name]
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
83 except KeyError:
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
84 saved = False
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
85 else:
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
86 del sys.modules[name]
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
87 return saved
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
88
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
89
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
90 def _save_and_block_module(name, orig_modules):
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
91 """Helper function to save and block a module in sys.modules
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
92
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
93 Return value is True if the module was in sys.modules and
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
94 False otherwise."""
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
95 saved = True
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
96 try:
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
97 orig_modules[name] = sys.modules[name]
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
98 except KeyError:
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
99 saved = False
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
100 sys.modules[name] = 0
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
101 return saved
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
102
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
103
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
104 def import_fresh_module(name, fresh=(), blocked=(), deprecated=False):
53048
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
105 """Imports and returns a module, deliberately bypassing the sys.modules cache
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
106 and importing a fresh copy of the module. Once the import is complete,
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
107 the sys.modules cache is restored to its original state.
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
108
53315
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
109 Modules named in fresh are also imported anew if needed by the import.
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
110
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
111 Importing of modules named in blocked is prevented while the fresh import
53048
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
112 takes place.
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
113
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
114 If deprecated is True, any module or package deprecation messages
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
115 will be suppressed."""
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
116 # NOTE: test_heapq and test_warnings include extra sanity checks to make
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
117 # sure that this utility function is working as expected
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
118 with _ignore_deprecated_imports(deprecated):
53315
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
119 # Keep track of modules saved for later restoration as well
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
120 # as those which just need a blocking entry removed
53048
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
121 orig_modules = {}
53315
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
122 names_to_remove = []
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
123 _save_and_remove_module(name, orig_modules)
53048
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
124 try:
53315
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
125 for fresh_name in fresh:
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
126 _save_and_remove_module(fresh_name, orig_modules)
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
127 for blocked_name in blocked:
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
128 if not _save_and_block_module(blocked_name, orig_modules):
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
129 names_to_remove.append(blocked_name)
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
130 fresh_module = importlib.import_module(name)
53048
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
131 finally:
53315
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
132 for orig_name, module in orig_modules.items():
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
133 sys.modules[orig_name] = module
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
134 for name_to_remove in names_to_remove:
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
135 del sys.modules[name_to_remove]
e9595cb5c786 Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents: 53048
diff changeset
136 return fresh_module
53048
fb41fe67e061 Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents: 52573
diff changeset
137
46752
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
138
52573
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
139 def get_attribute(obj, name):
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
140 """Get an attribute, raising SkipTest if AttributeError is raised."""
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
141 try:
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
142 attribute = getattr(obj, name)
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
143 except AttributeError:
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
144 raise unittest.SkipTest("module %s has no attribute %s" % (
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
145 obj.__name__, name))
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
146 else:
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
147 return attribute
52456
f2c90fd05d66 Add import_function method to test.test_support, and modify a number of
R. David Murray <rdmurray@bitdance.com>
parents: 52359
diff changeset
148
f2c90fd05d66 Add import_function method to test.test_support, and modify a number of
R. David Murray <rdmurray@bitdance.com>
parents: 52359
diff changeset
149
19416
a6ce2d16d739 use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
150 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
151 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
152 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
153 # 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
154 real_max_memuse = 0
4791
a2fd80eed27f Add verbose flag for regression test to clear.
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
155
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
156 # _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
157 # 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
158 # 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
159 _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
160 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
161 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
162 _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
163
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
164 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
165 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
166
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 def unload(name):
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
168 try:
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
169 del sys.modules[name]
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
170 except KeyError:
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
171 pass
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
172
36382
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
173 def unlink(filename):
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
174 try:
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
175 os.unlink(filename)
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
176 except OSError:
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
177 pass
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
178
45506
55eefb43ef6f Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
179 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
180 try:
55eefb43ef6f Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
181 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
182 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
183 # 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
184 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
185 raise
55eefb43ef6f Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
186
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 def forget(modname):
28968
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
188 '''"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
189 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
190 unload(modname)
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
191 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
192 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
193 # 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
194 # 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
195 # 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
196 unlink(os.path.join(dirname, modname + os.extsep + 'pyo'))
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
197
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
198 def is_resource_enabled(resource):
28968
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
199 """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
200 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
201 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
202
19416
a6ce2d16d739 use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
203 def requires(resource, msg=None):
28968
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
204 """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
205
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
206 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
207 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
208 # 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
209 # the resource was set
52334
1fbec29f89e8 this can be slightly less ugly
Benjamin Peterson <benjamin@python.org>
parents: 52320
diff changeset
210 if sys._getframe(1).f_globals.get("__name__") == "__main__":
28799
46d26afa8b95 if the test is run directly (__name__ == "__main__") don't actually require
Skip Montanaro <skip@pobox.com>
parents: 28174
diff changeset
211 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
212 if not is_resource_enabled(resource):
19416
a6ce2d16d739 use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
213 if msg is None:
a6ce2d16d739 use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
214 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
215 raise ResourceDenied(msg)
19416
a6ce2d16d739 use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
216
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
217 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
218
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
219 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
220 """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
221 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
222 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
223 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
224 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
225 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
226
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
227 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
228 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
229 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
230 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
231 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
232 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
233 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
234 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
235 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
236 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
237 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
238 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
239 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
240 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
241
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
242 (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
243 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
244 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
245 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
246 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
247 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
248
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
249 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
250 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
251 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
252 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
253 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
254 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
255
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
256 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
257 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
258 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
259 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
260 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
261 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
262 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
263 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
264 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
265 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
266
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
267 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
268 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
269 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
270 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
271 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
272 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
273 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
274 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
275 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
276 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
277 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
278
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
279 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
280 """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
281 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
282 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
283 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
284 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
285 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
286 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
287 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
288
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
289 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
290 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
291 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
292 """
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
293 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
294 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
295 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
296 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
297 "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
298 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
299 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
300 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
301 "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
302 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
303 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
304
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
305 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
306 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
307 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
308
1197
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
309 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
310
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
311 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
312 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
313 try:
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
314 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
315 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
316 return 0
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
317 except:
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
318 pass
48109
6f232fcfb657 Backport test.support.fcmp() from 3.0 to silence -3 warnings.
Brett Cannon <bcannon@gmail.com>
parents: 48053
diff changeset
319 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
320 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
321 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
322 if outcome != 0:
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
323 return outcome
48109
6f232fcfb657 Backport test.support.fcmp() from 3.0 to silence -3 warnings.
Brett Cannon <bcannon@gmail.com>
parents: 48053
diff changeset
324 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
325 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
326
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18635
diff changeset
327 try:
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18635
diff changeset
328 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
329 have_unicode = True
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18635
diff changeset
330 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
331 have_unicode = False
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18635
diff changeset
332
25982
e1bf4604c0a5 Patch #631972: Adds an is_jython flag.
Finn Bock <bckfnn@worldonline.dk>
parents: 24442
diff changeset
333 is_jython = sys.platform.startswith('java')
e1bf4604c0a5 Patch #631972: Adds an is_jython flag.
Finn Bock <bckfnn@worldonline.dk>
parents: 24442
diff changeset
334
17213
4d6ee7f2bee6 In Jython, `@' is not allowed in module names. Extend the TESTFN test
Barry Warsaw <barry@python.org>
parents: 17171
diff changeset
335 # 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
336 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
337 # 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
338 TESTFN = '$test'
29066
90342f395721 Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents: 28968
diff changeset
339 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
340 TESTFN = 'testfile'
90342f395721 Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents: 28968
diff changeset
341 else:
17213
4d6ee7f2bee6 In Jython, `@' is not allowed in module names. Extend the TESTFN test
Barry Warsaw <barry@python.org>
parents: 17171
diff changeset
342 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
343 # 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
344 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
345 # 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
346 # 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
347 # 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
348 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
349 # 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
350 # 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
351 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
352 else:
31092
de391e9d70ea Typo repair; added some comments and horizontal whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 31087
diff changeset
353 # 2 latin characters.
de391e9d70ea Typo repair; added some comments and horizontal whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 31087
diff changeset
354 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
355 TESTFN_ENCODING = sys.getfilesystemencoding()
de391e9d70ea Typo repair; added some comments and horizontal whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 31087
diff changeset
356 # 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
357 # 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
358 # 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
359 # 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
360 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
361 sys.getwindowsversion()[3] < 2): # 0=win32s or 1=9x/ME
31389
aca1ce1b4b61 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 31092
diff changeset
362 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
363 else:
31087
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
364 # 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
365 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
366 try:
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
367 # 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
368 # 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
369 # 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
370 # 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
371 # 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
372 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
373 except UnicodeEncodeError:
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
374 pass
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
375 else:
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
376 print \
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
377 '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
378 '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
379 % TESTFN_UNICODE_UNENCODEABLE
26000
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
380
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
381 # 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
382 fp = None
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
383 try:
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
384 fp = open(TESTFN, 'w+')
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
385 except IOError:
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
386 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
387 try:
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
388 fp = open(TMP_TESTFN, 'w+')
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
389 TESTFN = TMP_TESTFN
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
390 del TMP_TESTFN
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
391 except IOError:
26128
23ef7c163a12 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26000
diff changeset
392 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
393 (TESTFN, TMP_TESTFN))
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
394 if fp is not None:
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
395 fp.close()
36382
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
396 unlink(TESTFN)
41539
761ce3946b86 Import and raise statement cleanup.
Collin Winter <collinw@gmail.com>
parents: 41536
diff changeset
397 del fp
17036
ad741745cb6c Oops. A RISCOS patch I forgot to check in.
Guido van Rossum <guido@python.org>
parents: 16723
diff changeset
398
8127
047533789359 Move unified findfile() into test_support.py
Guido van Rossum <guido@python.org>
parents: 4791
diff changeset
399 def findfile(file, here=__file__):
28968
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
400 """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
401 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
402 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
403 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
404 return file
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
405 path = sys.path
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
406 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
407 for dn in path:
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
408 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
409 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
410 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
411
c1a8deb15f0e This patch removes all uses of "assert" in the regression test suite
Marc-André Lemburg <mal@egenix.com>
parents: 15540
diff changeset
412 def verify(condition, reason='test failed'):
16039
4a6658e18f36 Change verify() function to raise TestFailed, not AssertionError.
Guido van Rossum <guido@python.org>
parents: 16024
diff changeset
413 """Verify that condition is true. If not, raise TestFailed.
15957
c1a8deb15f0e This patch removes all uses of "assert" in the regression test suite
Marc-André Lemburg <mal@egenix.com>
parents: 15540
diff changeset
414
16069
68a803b4d618 docstring typo
Skip Montanaro <skip@pobox.com>
parents: 16039
diff changeset
415 The optional argument reason can be given to provide
16024
0f8127366a3f Use constructor form of "raise"; normalize <wink> docstrings.
Tim Peters <tim.peters@gmail.com>
parents: 15977
diff changeset
416 a better error text.
0f8127366a3f Use constructor form of "raise"; normalize <wink> docstrings.
Tim Peters <tim.peters@gmail.com>
parents: 15977
diff changeset
417 """
15977
f16545f58345 Whitespace normalization. Leaving tokenize_tests.py alone for now.
Tim Peters <tim.peters@gmail.com>
parents: 15957
diff changeset
418
f16545f58345 Whitespace normalization. Leaving tokenize_tests.py alone for now.
Tim Peters <tim.peters@gmail.com>
parents: 15957
diff changeset
419 if not condition:
16039
4a6658e18f36 Change verify() function to raise TestFailed, not AssertionError.
Guido van Rossum <guido@python.org>
parents: 16024
diff changeset
420 raise TestFailed(reason)
16679
db4fc82aa44b Add test for syntax error on "x = 1 + 1".
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16069
diff changeset
421
20780
ab2e315849a7 Fix bad bug in structseq slicing (NULL pointers in result). Reported by
Tim Peters <tim.peters@gmail.com>
parents: 20260
diff changeset
422 def vereq(a, b):
21595
e56047db6538 test_support: add a docstring to vereq().
Tim Peters <tim.peters@gmail.com>
parents: 20780
diff changeset
423 """Raise TestFailed if a == b is false.
e56047db6538 test_support: add a docstring to vereq().
Tim Peters <tim.peters@gmail.com>
parents: 20780
diff changeset
424
e56047db6538 test_support: add a docstring to vereq().
Tim Peters <tim.peters@gmail.com>
parents: 20780
diff changeset
425 This is better than verify(a == b) because, in case of failure, the
e56047db6538 test_support: add a docstring to vereq().
Tim Peters <tim.peters@gmail.com>
parents: 20780
diff changeset
426 error message incorporates repr(a) and repr(b) so you can see the
e56047db6538 test_support: add a docstring to vereq().
Tim Peters <tim.peters@gmail.com>
parents: 20780
diff changeset
427 inputs.
e56047db6538 test_support: add a docstring to vereq().
Tim Peters <tim.peters@gmail.com>
parents: 20780
diff changeset
428
e56047db6538 test_support: add a docstring to vereq().
Tim Peters <tim.peters@gmail.com>
parents: 20780
diff changeset
429 Note that "not (a == b)" isn't necessarily the same as "a != b"; the
e56047db6538 test_support: add a docstring to vereq().
Tim Peters <tim.peters@gmail.com>
parents: 20780
diff changeset
430 former is tested.
e56047db6538 test_support: add a docstring to vereq().
Tim Peters <tim.peters@gmail.com>
parents: 20780
diff changeset
431 """
e56047db6538 test_support: add a docstring to vereq().
Tim Peters <tim.peters@gmail.com>
parents: 20780
diff changeset
432
20780
ab2e315849a7 Fix bad bug in structseq slicing (NULL pointers in result). Reported by
Tim Peters <tim.peters@gmail.com>
parents: 20260
diff changeset
433 if not (a == b):
41539
761ce3946b86 Import and raise statement cleanup.
Collin Winter <collinw@gmail.com>
parents: 41536
diff changeset
434 raise TestFailed("%r == %r" % (a, b))
20780
ab2e315849a7 Fix bad bug in structseq slicing (NULL pointers in result). Reported by
Tim Peters <tim.peters@gmail.com>
parents: 20260
diff changeset
435
17815
d6df8d371b67 Get rid of the superstitious "~" in dict hashing's "i = (~hash) & mask".
Tim Peters <tim.peters@gmail.com>
parents: 17213
diff changeset
436 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
437 "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
438 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
439 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
440 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
441 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
442 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
443
50914
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
444 def make_bad_fd():
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
445 """
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
446 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
447 its fd.
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
448 """
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
449 file = open(TESTFN, "wb")
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
450 try:
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
451 return file.fileno()
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
452 finally:
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
453 file.close()
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
454 unlink(TESTFN)
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
455
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39417
diff changeset
456 def check_syntax_error(testcase, statement):
52320
a9515f29b38c this is better written using assertRaises
Benjamin Peterson <benjamin@python.org>
parents: 50914
diff changeset
457 testcase.assertRaises(SyntaxError, compile, statement,
a9515f29b38c this is better written using assertRaises
Benjamin Peterson <benjamin@python.org>
parents: 50914
diff changeset
458 '<test string>', 'exec')
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
459
35998
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
460 def open_urlresource(url):
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
461 import urllib, urlparse
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
462
41654
370b91533c53 Fix a bug in test.test_support.open_urlresource().
Collin Winter <collinw@gmail.com>
parents: 41539
diff changeset
463 requires('urlfetch')
35998
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
464 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
465
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
466 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
467 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
468 if os.path.exists(fn):
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
469 return open(fn)
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
470
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
471 print >> get_original_stdout(), '\tfetching %s ...' % url
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
472 fn, _ = urllib.urlretrieve(url, filename)
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
473 return open(fn)
40732
5dae0d7f683a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40537
diff changeset
474
40537
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
475
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
476 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
477 """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
478 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
479 """
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
480 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
481 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
482
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
483 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
484 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
485 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
486 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
487 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
488 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
489
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
490 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
491 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
492
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
493 @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
494 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
495 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
496 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
497
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
498
46861
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
499 class CleanImport(object):
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
500 """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
501
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
502 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
503 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
504
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
505 Use like this:
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
506
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
507 with CleanImport("foo"):
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
508 __import__("foo") # new reference
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
509 """
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
510
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
511 def __init__(self, *module_names):
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
512 self.original_modules = sys.modules.copy()
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
513 for module_name in module_names:
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
514 if module_name in sys.modules:
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
515 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
516 # 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
517 # 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
518 # 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
519 # the import cache.
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
520 if module.__name__ != module_name:
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
521 del sys.modules[module.__name__]
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
522 del sys.modules[module_name]
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
523
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
524 def __enter__(self):
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
525 return self
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
526
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
527 def __exit__(self, *ignore_exc):
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
528 sys.modules.update(self.original_modules)
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
529
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
530
53644
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
531 class EnvironmentVarGuard(UserDict.DictMixin):
40537
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
532
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
533 """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
534 a context manager."""
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
535
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
536 def __init__(self):
53644
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
537 self._environ = os.environ
53382
5a46a82206e7 Issue #5837: Certain sequences of calls to set() and unset() for
Walter Dörwald <walter@livinglogic.de>
parents: 53315
diff changeset
538 self._changed = {}
40537
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
539
53644
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
540 def __getitem__(self, envvar):
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
541 return self._environ[envvar]
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
542
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
543 def __setitem__(self, envvar, value):
53382
5a46a82206e7 Issue #5837: Certain sequences of calls to set() and unset() for
Walter Dörwald <walter@livinglogic.de>
parents: 53315
diff changeset
544 # Remember the initial value on the first access
5a46a82206e7 Issue #5837: Certain sequences of calls to set() and unset() for
Walter Dörwald <walter@livinglogic.de>
parents: 53315
diff changeset
545 if envvar not in self._changed:
53644
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
546 self._changed[envvar] = self._environ.get(envvar)
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
547 self._environ[envvar] = value
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
548
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
549 def __delitem__(self, envvar):
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
550 # Remember the initial value on the first access
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
551 if envvar not in self._changed:
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
552 self._changed[envvar] = self._environ.get(envvar)
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
553 if envvar in self._environ:
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
554 del self._environ[envvar]
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
555
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
556 def keys(self):
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
557 return self._environ.keys()
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
558
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
559 def set(self, envvar, value):
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
560 self[envvar] = value
40537
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
561
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
562 def unset(self, envvar):
53644
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
563 del self[envvar]
40537
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
564
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
565 def __enter__(self):
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
566 return self
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
567
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
568 def __exit__(self, *ignore_exc):
53382
5a46a82206e7 Issue #5837: Certain sequences of calls to set() and unset() for
Walter Dörwald <walter@livinglogic.de>
parents: 53315
diff changeset
569 for (k, v) in self._changed.items():
5a46a82206e7 Issue #5837: Certain sequences of calls to set() and unset() for
Walter Dörwald <walter@livinglogic.de>
parents: 53315
diff changeset
570 if v is None:
53644
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
571 if k in self._environ:
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
572 del self._environ[k]
53382
5a46a82206e7 Issue #5837: Certain sequences of calls to set() and unset() for
Walter Dörwald <walter@livinglogic.de>
parents: 53315
diff changeset
573 else:
53644
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
574 self._environ[k] = v
53382
5a46a82206e7 Issue #5837: Certain sequences of calls to set() and unset() for
Walter Dörwald <walter@livinglogic.de>
parents: 53315
diff changeset
575
40537
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
576
41081
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
577 class TransientResource(object):
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
578
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
579 """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
580 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
581
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
582 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
583 self.exc = exc
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
584 self.attrs = kwargs
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
585
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
586 def __enter__(self):
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
587 return self
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
588
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
589 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
590 """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
591 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
592 propagate (if any)."""
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
593 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
594 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
595 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
596 break
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
597 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
598 break
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
599 else:
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
600 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
601
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
602
41135
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
603 def transient_internet():
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
604 """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
605 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
606 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
607 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
608 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
609 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
610
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
611
42783
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
612 @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
613 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
614 """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
615 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
616 Example use (with 'stream_name=stdout')::
42783
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
617
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
618 with captured_stdout() as s:
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
619 print "hello"
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
620 assert s.getvalue() == "hello"
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
621 """
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
622 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
623 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
624 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
625 try:
3036e21ac661 make test_support's captured_output a bit more robust when exceptions happen
Benjamin Peterson <benjamin@python.org>
parents: 46425
diff changeset
626 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
627 finally:
3036e21ac661 make test_support's captured_output a bit more robust when exceptions happen
Benjamin Peterson <benjamin@python.org>
parents: 46425
diff changeset
628 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
629
7dff5e41fa7a Generalize test.test_support.test_stdout() with a base context manager so that
Brett Cannon <bcannon@gmail.com>
parents: 45513
diff changeset
630 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
631 return captured_output("stdout")
42783
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
632
52350
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
633 def gc_collect():
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
634 """Force as many objects as possible to be collected.
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
635
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
636 In non-CPython implementations of Python, this is needed because timely
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
637 deallocation is not guaranteed by the garbage collector. (Even in CPython
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
638 this can be the case in case of reference cycles.) This means that __del__
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
639 methods may be called later than expected and weakrefs may remain alive for
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
640 longer than expected. This function tries its best to force all garbage
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
641 objects to disappear.
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
642 """
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
643 import gc
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
644 gc.collect()
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
645 gc.collect()
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
646 gc.collect()
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
647
42783
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
648
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
649 #=======================================================================
38055
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
650 # 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
651 # it afterwards.
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
652
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
653 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
654 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
655 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
656 try:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
657 import locale
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
658 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
659 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
660 except AttributeError:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
661 # 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
662 raise
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
663 except:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
664 # 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
665 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
666 else:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
667 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
668 try:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
669 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
670 break
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
671 except:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
672 pass
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
673
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
674 # 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
675 try:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
676 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
677 finally:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
678 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
679 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
680 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
681 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
682 return inner
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
683 return decorator
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
684
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
685 #=======================================================================
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
686 # 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
687
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
688 # 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
689 # 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
690 _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
691 _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
692 _2G = 2 * _1G
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
693 _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
694
47347
343aaaa802e8 remove some __getslice__
Benjamin Peterson <benjamin@python.org>
parents: 47316
diff changeset
695 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
696
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
697 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
698 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
699 global max_memuse
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
700 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
701 sizes = {
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
702 '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
703 '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
704 '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
705 '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
706 }
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
707 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
708 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
709 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
710 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
711 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
712 real_max_memuse = memlimit
39417
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
713 if memlimit > MAX_Py_ssize_t:
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
714 memlimit = MAX_Py_ssize_t
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
715 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
716 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
717 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
718
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
719 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
720 """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
721
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
722 '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
723 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
724 the test, or a good estimate of it. 'overhead' specifies fixed overhead,
46425
29231a973580 spelling
Skip Montanaro <skip@pobox.com>
parents: 46414
diff changeset
725 independent of the testsize, and defaults to 5Mb.
37993
358662e2f273 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37992
diff changeset
726
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
727 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
728 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
729 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
730 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
731 """
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
732 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
733 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
734 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
735 # 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
736 # 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
737 # 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
738 # 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
739 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
740 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
741 else:
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
742 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
743 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
744 # 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
745 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
746 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
747 "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
748 return
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
749 # 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
750 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
751 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
752 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
753 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
754 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
755 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
756 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
757
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
758 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
759 def decorator(f):
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
760 def wrapper(self):
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
761 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
762 maxsize = 5147
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
763 else:
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
764 maxsize = size
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
765
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
766 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
767 if verbose:
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
768 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
769 "constraint\n" % (f.__name__,))
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
770 return
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
771
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
772 return f(self, maxsize)
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
773 wrapper.size = size
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
774 wrapper.memuse = memuse
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
775 wrapper.overhead = overhead
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
776 return wrapper
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
777 return decorator
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 48109
diff changeset
778
39417
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
779 def bigaddrspacetest(f):
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
780 """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
781 def wrapper(self):
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
782 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
783 if verbose:
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
784 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
785 "constraint\n" % (f.__name__,))
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
786 else:
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
787 return f(self)
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
788 return wrapper
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
789
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
790 #=======================================================================
41539
761ce3946b86 Import and raise statement cleanup.
Collin Winter <collinw@gmail.com>
parents: 41536
diff changeset
791 # 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
792
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17109
diff changeset
793 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
794 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
795 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
796 test(result)
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
797 return result
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
798
52350
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
799 def _id(obj):
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
800 return obj
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
801
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
802 def requires_resource(resource):
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
803 if resource_is_enabled(resource):
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
804 return _id
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
805 else:
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
806 return unittest.skip("resource {0!r} is not enabled".format(resource))
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
807
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
808 def cpython_only(test):
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
809 """
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
810 Decorator for tests only applicable on CPython.
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
811 """
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
812 return impl_detail(cpython=True)(test)
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
813
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
814 def impl_detail(msg=None, **guards):
52359
f26c1cd6c985 ** is required here
Benjamin Peterson <benjamin@python.org>
parents: 52356
diff changeset
815 if check_impl_detail(**guards):
52350
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
816 return _id
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
817 if msg is None:
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
818 guardnames, default = _parse_guards(guards)
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
819 if default:
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
820 msg = "implementation detail not available on {0}"
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
821 else:
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
822 msg = "implementation detail specific to {0}"
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
823 guardnames = sorted(guardnames.keys())
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
824 msg = msg.format(' or '.join(guardnames))
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
825 return unittest.skip(msg)
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
826
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
827 def _parse_guards(guards):
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
828 # Returns a tuple ({platform_name: run_me}, default_value)
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
829 if not guards:
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
830 return ({'cpython': True}, False)
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
831 is_true = guards.values()[0]
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
832 assert guards.values() == [is_true] * len(guards) # all True or all False
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
833 return (guards, not is_true)
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
834
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
835 # Use the following check to guard CPython's implementation-specific tests --
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
836 # or to run them only on the implementation(s) guarded by the arguments.
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
837 def check_impl_detail(**guards):
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
838 """This function returns True or False depending on the host platform.
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
839 Examples:
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
840 if check_impl_detail(): # only on CPython (default)
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
841 if check_impl_detail(jython=True): # only on Jython
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
842 if check_impl_detail(cpython=False): # everywhere except on CPython
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
843 """
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
844 guards, default = _parse_guards(guards)
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
845 return guards.get(platform.python_implementation().lower(), default)
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
846
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
847
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
848
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
849 def _run_suite(suite):
19995
b07cb51d731c run_suite(): Oops, update a docstring.
Barry Warsaw <barry@python.org>
parents: 19994
diff changeset
850 """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
851 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
852 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
853 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
854 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
855
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17109
diff changeset
856 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
857 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
858 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
859 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
860 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
861 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
862 else:
43559
306d5395dfa2 Let the O/S supply a port if none of the default ports can be used.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42783
diff changeset
863 err = "errors occurred; 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
864 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
865
19994
ab6c14822de1 run_suite(): Factor this out of run_unittest() for tests that build
Barry Warsaw <barry@python.org>
parents: 19819
diff changeset
866
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28863
diff changeset
867 def run_unittest(*classes):
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28863
diff changeset
868 """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
869 valid_types = (unittest.TestSuite, unittest.TestCase)
28863
6d852ebff36f Factor out common boilerplate for test_support
Raymond Hettinger <python@rcn.com>
parents: 28799
diff changeset
870 suite = unittest.TestSuite()
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28863
diff changeset
871 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
872 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
873 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
874 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
875 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
876 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
877 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
878 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
879 else:
3f214b39b4df run_unittest() to support TestCase instances as well as classes. Helps with doctests.
Raymond Hettinger <python@rcn.com>
parents: 29142
diff changeset
880 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
881 _run_suite(suite)
28863
6d852ebff36f Factor out common boilerplate for test_support
Raymond Hettinger <python@rcn.com>
parents: 28799
diff changeset
882
19994
ab6c14822de1 run_suite(): Factor this out of run_unittest() for tests that build
Barry Warsaw <barry@python.org>
parents: 19819
diff changeset
883
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
884 #=======================================================================
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
885 # 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
886
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
887 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
888 """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
889
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
890 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
891 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
892 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
893 """
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
894
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
895 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
896
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
897 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
898 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
899 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
900 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
901
20088
386fb26e0874 Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents: 20083
diff changeset
902 # 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
903 # 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
904 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
905 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
906 try:
386fb26e0874 Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents: 20083
diff changeset
907 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
908 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
909 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
910 finally:
386fb26e0874 Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents: 20083
diff changeset
911 sys.stdout = save_stdout
29140
e57e22aa56a4 Provide a clue that the doctests have run.
Raymond Hettinger <python@rcn.com>
parents: 29066
diff changeset
912 if verbose:
29142
5da198480b6a Include module name in doctest summary.
Raymond Hettinger <python@rcn.com>
parents: 29140
diff changeset
913 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
914 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
915
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
916 #=======================================================================
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
917 # 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
918
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
919 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
920 import threading
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
921 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
922
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
923 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
924 import threading
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
925 import time
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
926
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
927 _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
928 count = 0
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
929 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
930 count += 1
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
931 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
932
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
933 count = 0
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
934 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
935 count += 1
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
936 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
937
54085
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
938 def reap_threads(func):
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
939 @functools.wraps(func)
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
940 def decorator(*args):
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
941 key = threading_setup()
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
942 try:
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
943 return func(*args)
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
944 finally:
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
945 threading_cleanup(*key)
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
946 return decorator
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
947
38961
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
948 def reap_children():
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
949 """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
950 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
951 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
952 for refleaks.
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
953 """
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
954
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
955 # 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
956 # 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
957 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
958 any_process = -1
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
959 while True:
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
960 try:
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
961 # 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
962 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
963 if pid == 0:
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
964 break
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
965 except:
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
966 break