annotate Lib/test/test_support.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents c863dadc65eb
children 523f309cf558
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
54466
ef0785cfa6af remove function import
Benjamin Peterson <benjamin@python.org>
parents: 54465
diff changeset
9 import gc
41135
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
10 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
11 import sys
41539
761ce3946b86 Import and raise statement cleanup.
Collin Winter <collinw@gmail.com>
parents: 41536
diff changeset
12 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
13 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
14 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
15 import warnings
41539
761ce3946b86 Import and raise statement cleanup.
Collin Winter <collinw@gmail.com>
parents: 41536
diff changeset
16 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
17 import importlib
53644
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
18 import UserDict
63553
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
19 import re
60482
0592e7841777 adjust gc_collect for Jython
Philip Jenvey <pjenvey@underboss.org>
parents: 59683
diff changeset
20 import time
78317
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
21 import struct
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
22 import _testcapi
61027
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
23 try:
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
24 import thread
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
25 except ImportError:
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
26 thread = None
24442
226ba34bd998 Complete the absolute import patch for the test suite. All relative
Barry Warsaw <barry@python.org>
parents: 21595
diff changeset
27
52353
e6009b9cd908 remove test_support.TestSkipped and just use unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 52350
diff changeset
28 __all__ = ["Error", "TestFailed", "ResourceDenied", "import_module",
47316
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
29 "verbose", "use_resources", "max_memuse", "record_original_stdout",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
30 "get_original_stdout", "unload", "unlink", "rmtree", "forget",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
31 "is_resource_enabled", "requires", "find_unused_port", "bind_port",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
32 "fcmp", "have_unicode", "is_jython", "TESTFN", "HOST", "FUZZ",
58856
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
33 "SAVEDCWD", "temp_cwd", "findfile", "sortdict", "check_syntax_error",
59432
e23cba03cc4b Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58971
diff changeset
34 "open_urlresource", "check_warnings", "check_py3k_warnings",
63553
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
35 "CleanImport", "EnvironmentVarGuard", "captured_output",
47316
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
36 "captured_stdout", "TransientResource", "transient_internet",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
37 "run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest",
c5604be7acdf add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents: 46863
diff changeset
38 "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
39 "threading_cleanup", "reap_children", "cpython_only",
69941
827a455a2d13 import_fresh_module ought to be in __all__
Benjamin Peterson <benjamin@python.org>
parents: 68256
diff changeset
40 "check_impl_detail", "get_attribute", "py3k_bytes",
75102
a0f43f4481e0 merge 2.6 with hash randomization fix
Benjamin Peterson <benjamin@python.org>
parents: 73918 75100
diff changeset
41 "import_fresh_module", "threading_cleanup", "reap_children",
a0f43f4481e0 merge 2.6 with hash randomization fix
Benjamin Peterson <benjamin@python.org>
parents: 73918 75100
diff changeset
42 "strip_python_stderr"]
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
43
13682
7de18f04f85a Restore TestSkipped exception; appears to have disappeared in last checkin.
Fred Drake <fdrake@acm.org>
parents: 13647
diff changeset
44 class Error(Exception):
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
45 """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
46
7de18f04f85a Restore TestSkipped exception; appears to have disappeared in last checkin.
Fred Drake <fdrake@acm.org>
parents: 13647
diff changeset
47 class TestFailed(Error):
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
48 """Test failed."""
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
52354
14cc92ae94c9 fix naming
Benjamin Peterson <benjamin@python.org>
parents: 52353
diff changeset
50 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
51 """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
52
974f196abb87 test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents: 26527
diff changeset
53 This is raised when a test calls requires() for a resource that
61027
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
54 has not been enabled. It is used to distinguish between expected
27636
974f196abb87 test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents: 26527
diff changeset
55 and unexpected skips.
974f196abb87 test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents: 26527
diff changeset
56 """
974f196abb87 test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents: 26527
diff changeset
57
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
58 @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
59 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
60 """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
61 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
62
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
63 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
64 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
65 with warnings.catch_warnings():
46816
884f8426cccf Suppress deprecations for packages as well when using
Brett Cannon <bcannon@gmail.com>
parents: 46757
diff changeset
66 warnings.filterwarnings("ignore", ".+ (module|package)",
884f8426cccf Suppress deprecations for packages as well when using
Brett Cannon <bcannon@gmail.com>
parents: 46757
diff changeset
67 DeprecationWarning)
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 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
69 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
70 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
71
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
72
46752
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
73 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
74 """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
75 it is not available.
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
76
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
77 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
78 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
79 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
80 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
81 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
82 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
83 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
84
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
85
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
86 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
87 """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
88
69954
c1a12a308c5b #11910: change import_fresh_module to return None when one of the "fresh" modules can not be imported.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69941
diff changeset
89 Raise ImportError if the module can't be imported."""
70080
49ffa5c30951 Change import_fresh_module to work with packages.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69954
diff changeset
90 # try to import the module and raise an error if it can't be imported
49ffa5c30951 Change import_fresh_module to work with packages.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69954
diff changeset
91 if name not in sys.modules:
69954
c1a12a308c5b #11910: change import_fresh_module to return None when one of the "fresh" modules can not be imported.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69941
diff changeset
92 __import__(name)
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
93 del sys.modules[name]
70080
49ffa5c30951 Change import_fresh_module to work with packages.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69954
diff changeset
94 for modname in list(sys.modules):
49ffa5c30951 Change import_fresh_module to work with packages.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69954
diff changeset
95 if modname == name or modname.startswith(name + '.'):
49ffa5c30951 Change import_fresh_module to work with packages.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69954
diff changeset
96 orig_modules[modname] = sys.modules[modname]
49ffa5c30951 Change import_fresh_module to work with packages.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69954
diff changeset
97 del sys.modules[modname]
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
98
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 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
100 """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
101
69954
c1a12a308c5b #11910: change import_fresh_module to return None when one of the "fresh" modules can not be imported.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69941
diff changeset
102 Return True if the module was in sys.modules, False otherwise."""
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
103 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
104 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
105 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
106 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
107 saved = False
63008
5b1b7f5574c0 Merged revisions 82850 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 62055
diff changeset
108 sys.modules[name] = None
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 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
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
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
112 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
113 """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
114 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
115 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
116
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
117 Modules named in fresh are also imported anew if needed by the import.
69954
c1a12a308c5b #11910: change import_fresh_module to return None when one of the "fresh" modules can not be imported.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69941
diff changeset
118 If one of these modules can't be imported, None is returned.
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
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 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
121 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
122
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
123 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
124 will be suppressed."""
70084
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70080
diff changeset
125 # NOTE: test_heapq, test_json, and test_warnings include extra sanity
5b0fecd2eba0 #5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70080
diff changeset
126 # checks to make sure that this utility function is working as expected
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
127 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
128 # 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
129 # 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
130 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
131 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
132 _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
133 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
134 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
135 _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
136 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
137 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
138 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
139 fresh_module = importlib.import_module(name)
46752
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
140 except ImportError:
69954
c1a12a308c5b #11910: change import_fresh_module to return None when one of the "fresh" modules can not be imported.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 69941
diff changeset
141 fresh_module = None
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
142 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
143 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
144 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
145 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
146 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
147 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
148
46752
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
149
52573
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
150 def get_attribute(obj, name):
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
151 """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
152 try:
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
153 attribute = getattr(obj, name)
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
154 except AttributeError:
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
155 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
156 obj.__name__, name))
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
157 else:
68b7e8217c7e Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents: 52555
diff changeset
158 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
159
46752
9fbc2da54e29 The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 46611
diff changeset
160
19416
a6ce2d16d739 use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
161 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
162 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
163 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
164 # small sizes, to make sure they work.)
48113
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
165 real_max_memuse = 0
4791
a2fd80eed27f Add verbose flag for regression test to clear.
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
166
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
167 # _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
168 # 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
169 # 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
170 _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
171 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
172 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
173 _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
174
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
175 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
176 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
177
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 def unload(name):
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
179 try:
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
180 del sys.modules[name]
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
181 except KeyError:
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
182 pass
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
78554
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
184 if sys.platform.startswith("win"):
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
185 def _waitfor(func, pathname, waitall=False):
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
186 # Peform the operation
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
187 func(pathname)
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
188 # Now setup the wait loop
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
189 if waitall:
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
190 dirname = pathname
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
191 else:
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
192 dirname, name = os.path.split(pathname)
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
193 dirname = dirname or '.'
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
194 # Check for `pathname` to be removed from the filesystem.
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
195 # The exponential backoff of the timeout amounts to a total
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
196 # of ~1 second after which the deletion is probably an error
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
197 # anyway.
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
198 # Testing on a i7@4.3GHz shows that usually only 1 iteration is
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
199 # required when contention occurs.
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
200 timeout = 0.001
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
201 while timeout < 1.0:
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
202 # Note we are only testing for the existance of the file(s) in
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
203 # the contents of the directory regardless of any security or
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
204 # access rights. If we have made it this far, we have sufficient
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
205 # permissions to do that much using Python's equivalent of the
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
206 # Windows API FindFirstFile.
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
207 # Other Windows APIs can fail or give incorrect results when
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
208 # dealing with files that are pending deletion.
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
209 L = os.listdir(dirname)
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
210 if not (L if waitall else name in L):
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
211 return
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
212 # Increase the timeout and try again
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
213 time.sleep(timeout)
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
214 timeout *= 2
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
215 warnings.warn('tests may fail, delete still pending for ' + pathname,
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
216 RuntimeWarning, stacklevel=4)
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
217
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
218 def _unlink(filename):
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
219 _waitfor(os.unlink, filename)
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
220
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
221 def _rmdir(dirname):
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
222 _waitfor(os.rmdir, dirname)
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
223
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
224 def _rmtree(path):
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
225 def _rmtree_inner(path):
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
226 for name in os.listdir(path):
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
227 fullname = os.path.join(path, name)
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
228 if os.path.isdir(fullname):
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
229 _waitfor(_rmtree_inner, fullname, waitall=True)
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
230 os.rmdir(fullname)
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
231 else:
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
232 os.unlink(fullname)
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
233 _waitfor(_rmtree_inner, path, waitall=True)
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
234 _waitfor(os.rmdir, path)
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
235 else:
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
236 _unlink = os.unlink
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
237 _rmdir = os.rmdir
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
238 _rmtree = shutil.rmtree
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
239
36382
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
240 def unlink(filename):
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
241 try:
78554
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
242 _unlink(filename)
36382
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
243 except OSError:
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
244 pass
99d491faa65c Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents: 35998
diff changeset
245
78554
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
246 def rmdir(dirname):
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
247 try:
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
248 _rmdir(dirname)
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
249 except OSError as error:
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
250 # The directory need not exist.
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
251 if error.errno != errno.ENOENT:
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
252 raise
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
253
45506
55eefb43ef6f Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
254 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
255 try:
78554
c863dadc65eb Fix #15496. Add directory removal helpers to make Windows tests more reliable. Patch by Jeremy Kloth
Brian Curtin <brian@python.org>
parents: 78317
diff changeset
256 _rmtree(path)
45506
55eefb43ef6f Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
257 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
258 # 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
259 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
260 raise
55eefb43ef6f Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents: 45468
diff changeset
261
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 def forget(modname):
28968
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
263 '''"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
264 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
265 unload(modname)
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
266 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
267 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
268 # 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
269 # 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
270 # 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
271 unlink(os.path.join(dirname, modname + os.extsep + 'pyo'))
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
272
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
273 def is_resource_enabled(resource):
28968
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
274 """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
275 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
276 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
277
19416
a6ce2d16d739 use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
278 def requires(resource, msg=None):
28968
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
279 """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
280
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
281 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
282 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
283 # 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
284 # the resource was set
52334
1fbec29f89e8 this can be slightly less ugly
Benjamin Peterson <benjamin@python.org>
parents: 52320
diff changeset
285 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
286 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
287 if not is_resource_enabled(resource):
19416
a6ce2d16d739 use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
288 if msg is None:
a6ce2d16d739 use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
289 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
290 raise ResourceDenied(msg)
19416
a6ce2d16d739 use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents: 19343
diff changeset
291
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
292 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
293
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
294 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
295 """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
296 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
297 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
298 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
299 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
300 then closed and deleted, and the ephemeral port is returned.
38767
46f3eb474c84 Fix the socket tests so they can be run concurrently. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents: 38055
diff changeset
301
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
302 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
303 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
304 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
305 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
306 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
307 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
308 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
309 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
310 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
311 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
312 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
313 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
314 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
315 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
316
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
317 (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
318 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
319 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
320 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
321 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
322 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
323
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
324 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
325 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
326 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
327 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
328 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
329 must be forcibly killed via OpenProcess()/TerminateProcess().
44989
e33625895550 Backport r58453:
Neal Norwitz <nnorwitz@gmail.com>
parents: 39417
diff changeset
330
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
331 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
332 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
333 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
334 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
335 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
336 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
337 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
338 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
339 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
340 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
341
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
342 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
343 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
344 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
345 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
346 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
347 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
348 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
349 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
350 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
351 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
352 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
353
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
354 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
355 """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
356 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
357 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
358 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
359 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
360 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
361 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
362 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
363
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
364 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
365 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
366 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
367 """
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
368 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
369 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
370 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
371 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
372 "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
373 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
374 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
375 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
376 "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
377 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
378 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
379
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 46241
diff changeset
380 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
381 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
382 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
383
1197
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
384 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
385
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
386 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
387 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
388 try:
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
389 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
390 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
391 return 0
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
392 except:
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
393 pass
48109
6f232fcfb657 Backport test.support.fcmp() from 3.0 to silence -3 warnings.
Brett Cannon <bcannon@gmail.com>
parents: 48053
diff changeset
394 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
395 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
396 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
397 if outcome != 0:
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
398 return outcome
48109
6f232fcfb657 Backport test.support.fcmp() from 3.0 to silence -3 warnings.
Brett Cannon <bcannon@gmail.com>
parents: 48053
diff changeset
399 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
400 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
401
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18635
diff changeset
402 try:
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18635
diff changeset
403 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
404 have_unicode = True
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18635
diff changeset
405 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
406 have_unicode = False
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 18635
diff changeset
407
25982
e1bf4604c0a5 Patch #631972: Adds an is_jython flag.
Finn Bock <bckfnn@worldonline.dk>
parents: 24442
diff changeset
408 is_jython = sys.platform.startswith('java')
e1bf4604c0a5 Patch #631972: Adds an is_jython flag.
Finn Bock <bckfnn@worldonline.dk>
parents: 24442
diff changeset
409
17213
4d6ee7f2bee6 In Jython, `@' is not allowed in module names. Extend the TESTFN test
Barry Warsaw <barry@python.org>
parents: 17171
diff changeset
410 # 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
411 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
412 # 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
413 TESTFN = '$test'
29066
90342f395721 Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents: 28968
diff changeset
414 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
415 TESTFN = 'testfile'
90342f395721 Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents: 28968
diff changeset
416 else:
17213
4d6ee7f2bee6 In Jython, `@' is not allowed in module names. Extend the TESTFN test
Barry Warsaw <barry@python.org>
parents: 17171
diff changeset
417 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
418 # 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
419 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
420 # 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
421 # 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
422 # 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
423 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
424 # 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
425 # 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
426 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
427 else:
31092
de391e9d70ea Typo repair; added some comments and horizontal whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 31087
diff changeset
428 # 2 latin characters.
de391e9d70ea Typo repair; added some comments and horizontal whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 31087
diff changeset
429 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
430 TESTFN_ENCODING = sys.getfilesystemencoding()
64602
457aa8da490a Recorded merge of revisions 83987 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64542
diff changeset
431 # TESTFN_UNENCODABLE 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
432 # 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
433 # 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
434 # 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
435 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
436 sys.getwindowsversion()[3] < 2): # 0=win32s or 1=9x/ME
64602
457aa8da490a Recorded merge of revisions 83987 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64542
diff changeset
437 TESTFN_UNENCODABLE = None
31078
2ba9a8696560 Add TESTFN_UNICODE_UNENCODEABLE, a unicode filename that can not be
Mark Hammond <mhammond@skippinet.com.au>
parents: 29890
diff changeset
438 else:
31087
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
439 # Japanese characters (I think - from bug 846133)
64602
457aa8da490a Recorded merge of revisions 83987 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64542
diff changeset
440 TESTFN_UNENCODABLE = 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
441 try:
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
442 # 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
443 # 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
444 # 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
445 # 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
446 # See [ 850997 ] mbcs encoding ignores errors
64602
457aa8da490a Recorded merge of revisions 83987 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64542
diff changeset
447 TESTFN_UNENCODABLE.encode("Latin1")
31087
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
448 except UnicodeEncodeError:
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
449 pass
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
450 else:
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
451 print \
8dd159279576 Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents: 31078
diff changeset
452 '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
453 'Unicode filename tests may not be effective' \
64602
457aa8da490a Recorded merge of revisions 83987 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 64542
diff changeset
454 % TESTFN_UNENCODABLE
26000
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
455
58856
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
456
54465
d74b8edd7a8c Issue #6152: New option '-j'/'--multiprocess' for regrtest allows running
Antoine Pitrou <solipsis@pitrou.net>
parents: 54085
diff changeset
457 # Disambiguate TESTFN for parallel testing, while letting it remain a valid
d74b8edd7a8c Issue #6152: New option '-j'/'--multiprocess' for regrtest allows running
Antoine Pitrou <solipsis@pitrou.net>
parents: 54085
diff changeset
458 # module name.
58856
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
459 TESTFN = "{}_{}_tmp".format(TESTFN, os.getpid())
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
460
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
461 # Save the initial cwd
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
462 SAVEDCWD = os.getcwd()
54465
d74b8edd7a8c Issue #6152: New option '-j'/'--multiprocess' for regrtest allows running
Antoine Pitrou <solipsis@pitrou.net>
parents: 54085
diff changeset
463
58856
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
464 @contextlib.contextmanager
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
465 def temp_cwd(name='tempcwd', quiet=False):
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
466 """
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
467 Context manager that creates a temporary directory and set it as CWD.
26000
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
468
58856
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
469 The new CWD is created in the current directory and it's named *name*.
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
470 If *quiet* is False (default) and it's not possible to create or change
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
471 the CWD, an error is raised. If it's True, only a warning is raised
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
472 and the original CWD is used.
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
473 """
77072
d7aff4423172 Issue #8767: Restore building with --disable-unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 75102
diff changeset
474 if have_unicode and isinstance(name, unicode):
58971
dcda985c6403 skip tests with a non-ascii cwd when the file system encoding is ascii
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58856
diff changeset
475 try:
dcda985c6403 skip tests with a non-ascii cwd when the file system encoding is ascii
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58856
diff changeset
476 name = name.encode(sys.getfilesystemencoding() or 'ascii')
dcda985c6403 skip tests with a non-ascii cwd when the file system encoding is ascii
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58856
diff changeset
477 except UnicodeEncodeError:
dcda985c6403 skip tests with a non-ascii cwd when the file system encoding is ascii
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58856
diff changeset
478 if not quiet:
dcda985c6403 skip tests with a non-ascii cwd when the file system encoding is ascii
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58856
diff changeset
479 raise unittest.SkipTest('unable to encode the cwd name with '
dcda985c6403 skip tests with a non-ascii cwd when the file system encoding is ascii
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58856
diff changeset
480 'the filesystem encoding.')
58856
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
481 saved_dir = os.getcwd()
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
482 is_temporary = False
26000
83d21ea9457d Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents: 25982
diff changeset
483 try:
58856
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
484 os.mkdir(name)
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
485 os.chdir(name)
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
486 is_temporary = True
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
487 except OSError:
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
488 if not quiet:
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
489 raise
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
490 warnings.warn('tests may fail, unable to change the CWD to ' + name,
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
491 RuntimeWarning, stacklevel=3)
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
492 try:
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
493 yield os.getcwd()
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
494 finally:
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
495 os.chdir(saved_dir)
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
496 if is_temporary:
d1e2f794d020 #7712: add a temp_cwd context manager to test_support and use it in regrtest to run all the tests in a temporary directory, saving the original CWD in test_support.SAVEDCWD. Thanks to Florent Xicluna who helped with the patch.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58683
diff changeset
497 rmtree(name)
17036
ad741745cb6c Oops. A RISCOS patch I forgot to check in.
Guido van Rossum <guido@python.org>
parents: 16723
diff changeset
498
ad741745cb6c Oops. A RISCOS patch I forgot to check in.
Guido van Rossum <guido@python.org>
parents: 16723
diff changeset
499
59579
27aa46d0214a Do not chdir when running test_xml_etree, and enhance the findfile helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59489
diff changeset
500 def findfile(file, here=__file__, subdir=None):
28968
b174d0927f0e 'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
501 """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
502 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
503 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
504 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
505 return file
59579
27aa46d0214a Do not chdir when running test_xml_etree, and enhance the findfile helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59489
diff changeset
506 if subdir is not None:
27aa46d0214a Do not chdir when running test_xml_etree, and enhance the findfile helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59489
diff changeset
507 file = os.path.join(subdir, file)
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
508 path = sys.path
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
509 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
510 for dn in path:
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13682
diff changeset
511 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
512 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
513 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
514
17815
d6df8d371b67 Get rid of the superstitious "~" in dict hashing's "i = (~hash) & mask".
Tim Peters <tim.peters@gmail.com>
parents: 17213
diff changeset
515 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
516 "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
517 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
518 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
519 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
520 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
521 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
522
50914
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
523 def make_bad_fd():
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
524 """
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
525 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
526 its fd.
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
527 """
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
528 file = open(TESTFN, "wb")
16679
db4fc82aa44b Add test for syntax error on "x = 1 + 1".
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16069
diff changeset
529 try:
50914
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
530 return file.fileno()
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
531 finally:
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
532 file.close()
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
533 unlink(TESTFN)
caa79d238917 Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49146
diff changeset
534
40217
13b8ad679def Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents: 39417
diff changeset
535 def check_syntax_error(testcase, statement):
52320
a9515f29b38c this is better written using assertRaises
Benjamin Peterson <benjamin@python.org>
parents: 50914
diff changeset
536 testcase.assertRaises(SyntaxError, compile, statement,
a9515f29b38c this is better written using assertRaises
Benjamin Peterson <benjamin@python.org>
parents: 50914
diff changeset
537 '<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
538
59643
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
539 def open_urlresource(url, check=None):
57053
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
540 import urlparse, urllib2
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
541
35998
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
542 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
543
55220
2630c075b57c put downloaded test support files in Lib/test/data instead of the cwd
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
544 fn = os.path.join(os.path.dirname(__file__), "data", filename)
59643
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
545
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
546 def check_valid_file(fn):
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
547 f = open(fn)
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
548 if check is None:
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
549 return f
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
550 elif check(f):
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
551 f.seek(0)
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
552 return f
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
553 f.close()
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
554
55220
2630c075b57c put downloaded test support files in Lib/test/data instead of the cwd
Benjamin Peterson <benjamin@python.org>
parents: 55002
diff changeset
555 if os.path.exists(fn):
59643
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
556 f = check_valid_file(fn)
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
557 if f is not None:
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
558 return f
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
559 unlink(fn)
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
560
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
561 # Verify the requirement before downloading the file
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
562 requires('urlfetch')
35998
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
563
96d1fa36964d Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 34707
diff changeset
564 print >> get_original_stdout(), '\tfetching %s ...' % url
57053
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
565 f = urllib2.urlopen(url, timeout=15)
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
566 try:
57052
a01e6cef1256 Use a custom timeout in test_support.open_urlresource.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56980
diff changeset
567 with open(fn, "wb") as out:
57053
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
568 s = f.read()
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
569 while s:
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
570 out.write(s)
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
571 s = f.read()
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
572 finally:
344c47df363f Merged revisions 76037 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56930
diff changeset
573 f.close()
59643
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
574
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
575 f = check_valid_file(fn)
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
576 if f is not None:
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
577 return f
c4dd30b5d07e Issue #7783 and #7787: open_urlresource invalidates the outdated files from the local cache.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59579
diff changeset
578 raise TestFailed('invalid resource "%s"' % fn)
40732
5dae0d7f683a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40537
diff changeset
579
40537
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
580
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
581 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
582 """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
583 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
584 """
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
585 def __init__(self, warnings_list):
63553
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
586 self._warnings = warnings_list
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
587 self._last = 0
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
588
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
589 def __getattr__(self, attr):
63553
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
590 if len(self._warnings) > self._last:
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
591 return getattr(self._warnings[-1], attr)
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
592 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
593 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
594 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
595
63553
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
596 @property
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
597 def warnings(self):
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
598 return self._warnings[self._last:]
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
599
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
600 def reset(self):
63553
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
601 self._last = len(self._warnings)
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
602
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
603
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
604 def _filterwarnings(filters, quiet=False):
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
605 """Catch the warnings, then check if all the expected
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
606 warnings have been raised and re-raise unexpected warnings.
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
607 If 'quiet' is True, only re-raise the unexpected warnings.
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
608 """
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
609 # Clear the warning registry of the calling module
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
610 # in order to re-raise the warnings.
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
611 frame = sys._getframe(2)
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
612 registry = frame.f_globals.get('__warningregistry__')
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
613 if registry:
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
614 registry.clear()
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
615 with warnings.catch_warnings(record=True) as w:
63612
29d34e4d0a9a Merged revisions 78815 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63553
diff changeset
616 # Set filter "always" to record all warnings. Because
29d34e4d0a9a Merged revisions 78815 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63553
diff changeset
617 # test_warnings swap the module, we need to look up in
29d34e4d0a9a Merged revisions 78815 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63553
diff changeset
618 # the sys.modules dictionary.
29d34e4d0a9a Merged revisions 78815 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63553
diff changeset
619 sys.modules['warnings'].simplefilter("always")
63553
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
620 yield WarningsRecorder(w)
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
621 # Filter the recorded warnings
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
622 reraise = [warning.message for warning in w]
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
623 missing = []
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
624 for msg, cat in filters:
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
625 seen = False
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
626 for exc in reraise[:]:
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
627 message = str(exc)
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
628 # Filter out the matching messages
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
629 if (re.match(msg, message, re.I) and
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
630 issubclass(exc.__class__, cat)):
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
631 seen = True
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
632 reraise.remove(exc)
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
633 if not seen and not quiet:
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
634 # This filter caught nothing
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
635 missing.append((msg, cat.__name__))
63619
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63612
diff changeset
636 if reraise:
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63612
diff changeset
637 raise AssertionError("unhandled warning %r" % reraise[0])
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63612
diff changeset
638 if missing:
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63612
diff changeset
639 raise AssertionError("filter (%r, %s) did not catch any warning" %
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63612
diff changeset
640 missing[0])
63553
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
641
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
642
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
643 @contextlib.contextmanager
63553
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
644 def check_warnings(*filters, **kwargs):
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
645 """Context manager to silence warnings.
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
646
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
647 Accept 2-tuples as positional arguments:
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
648 ("message regexp", WarningCategory)
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
649
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
650 Optional argument:
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
651 - if 'quiet' is True, it does not fail if a filter catches nothing
63619
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63612
diff changeset
652 (default True without argument,
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63612
diff changeset
653 default False if some filters are defined)
63553
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
654
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
655 Without argument, it defaults to:
63619
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63612
diff changeset
656 check_warnings(("", Warning), quiet=True)
63553
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
657 """
63619
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63612
diff changeset
658 quiet = kwargs.get('quiet')
63553
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
659 if not filters:
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
660 filters = (("", Warning),)
63619
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63612
diff changeset
661 # Preserve backward compatibility
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63612
diff changeset
662 if quiet is None:
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63612
diff changeset
663 quiet = True
a2f013021e1d Merged revisions 78769,79049 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63612
diff changeset
664 return _filterwarnings(filters, quiet)
63553
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
665
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
666
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
667 @contextlib.contextmanager
59432
e23cba03cc4b Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58971
diff changeset
668 def check_py3k_warnings(*filters, **kwargs):
63553
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
669 """Context manager to silence py3k warnings.
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
670
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
671 Accept 2-tuples as positional arguments:
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
672 ("message regexp", WarningCategory)
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
673
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
674 Optional argument:
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
675 - if 'quiet' is True, it does not fail if a filter catches nothing
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
676 (default False)
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
677
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
678 Without argument, it defaults to:
59432
e23cba03cc4b Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58971
diff changeset
679 check_py3k_warnings(("", DeprecationWarning), quiet=False)
63553
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
680 """
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
681 if sys.py3kwarning:
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
682 if not filters:
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
683 filters = (("", DeprecationWarning),)
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
684 else:
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
685 # It should not raise any py3k warning
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
686 filters = ()
7c3b849d181a Merged revisions 78758 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58491
diff changeset
687 return _filterwarnings(filters, kwargs.get('quiet'))
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
688
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
689
46861
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
690 class CleanImport(object):
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
691 """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
692
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
693 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
694 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
695
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
696 Use like this:
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
697
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
698 with CleanImport("foo"):
58683
30e4246ee803 Fix a typo in a docstring introduced in r77956.
Brett Cannon <bcannon@gmail.com>
parents: 58682
diff changeset
699 importlib.import_module("foo") # new reference
46861
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
700 """
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
701
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
702 def __init__(self, *module_names):
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
703 self.original_modules = sys.modules.copy()
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
704 for module_name in module_names:
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
705 if module_name in sys.modules:
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
706 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
707 # 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
708 # 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
709 # 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
710 # the import cache.
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
711 if module.__name__ != module_name:
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
712 del sys.modules[module.__name__]
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
713 del sys.modules[module_name]
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
714
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
715 def __enter__(self):
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
716 return self
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
717
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
718 def __exit__(self, *ignore_exc):
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
719 sys.modules.update(self.original_modules)
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
720
16f16fce5302 Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46816
diff changeset
721
53644
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
722 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
723
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
724 """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
725 a context manager."""
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
726
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
727 def __init__(self):
53644
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
728 self._environ = os.environ
53383
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
729 self._changed = {}
40537
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
730
53644
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
731 def __getitem__(self, envvar):
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
732 return self._environ[envvar]
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
733
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
734 def __setitem__(self, envvar, value):
53383
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
735 # Remember the initial value on the first access
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
736 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
737 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
738 self._environ[envvar] = value
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
739
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
740 def __delitem__(self, envvar):
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
741 # 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
742 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
743 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
744 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
745 del self._environ[envvar]
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
746
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
747 def keys(self):
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
748 return self._environ.keys()
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
749
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
750 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
751 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
752
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
753 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
754 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
755
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
756 def __enter__(self):
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
757 return self
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
758
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
759 def __exit__(self, *ignore_exc):
53383
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
760 for (k, v) in self._changed.items():
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
761 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
762 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
763 del self._environ[k]
53383
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
764 else:
53644
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53382
diff changeset
765 self._environ[k] = v
56565
b818a7a70a47 Move restoration of the os.environ object into the context manager where it belongs
Nick Coghlan <ncoghlan@gmail.com>
parents: 56563
diff changeset
766 os.environ = self._environ
53382
5a46a82206e7 Issue #5837: Certain sequences of calls to set() and unset() for
Walter Dörwald <walter@livinglogic.de>
parents: 53315
diff changeset
767
40537
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
768
56562
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
769 class DirsOnSysPath(object):
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
770 """Context manager to temporarily add directories to sys.path.
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
771
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
772 This makes a copy of sys.path, appends any directories given
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
773 as positional arguments, then reverts sys.path to the copied
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
774 settings when the context ends.
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
775
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
776 Note that *all* sys.path modifications in the body of the
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
777 context manager, including replacement of the object,
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
778 will be reverted at the end of the block.
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
779 """
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
780
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
781 def __init__(self, *paths):
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
782 self.original_value = sys.path[:]
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
783 self.original_object = sys.path
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
784 sys.path.extend(paths)
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
785
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
786 def __enter__(self):
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
787 return self
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
788
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
789 def __exit__(self, *ignore_exc):
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
790 sys.path = self.original_object
a1088bb4fcf8 Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 55220
diff changeset
791 sys.path[:] = self.original_value
53383
b366a496d499 Merged revisions 71875 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 50914
diff changeset
792
40537
be8240ce52ff Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40455
diff changeset
793
41081
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
794 class TransientResource(object):
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
795
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
796 """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
797 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
798
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
799 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
800 self.exc = exc
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
801 self.attrs = kwargs
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
802
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
803 def __enter__(self):
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
804 return self
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
805
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
806 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
807 """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
808 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
809 propagate (if any)."""
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
810 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
811 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
812 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
813 break
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
814 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
815 break
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
816 else:
be1e2f745669 Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents: 40732
diff changeset
817 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
818
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
819
56585
77ecc5e4f664 Silence a deprecation warning by using the appropriate replacement construct
Nick Coghlan <ncoghlan@gmail.com>
parents: 56565
diff changeset
820 @contextlib.contextmanager
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
821 def transient_internet(resource_name, timeout=30.0, errnos=()):
41135
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
822 """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
823 with the Internet connection manifest themselves as exceptions."""
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
824 default_errnos = [
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
825 ('ECONNREFUSED', 111),
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
826 ('ECONNRESET', 104),
67469
4d1927091b39 Merged revisions 87861,87863 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 67312
diff changeset
827 ('EHOSTUNREACH', 113),
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
828 ('ENETUNREACH', 101),
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
829 ('ETIMEDOUT', 110),
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
830 ]
64542
d008f406dc98 Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
831 default_gai_errnos = [
71271
a9c6f468012e Skip network tests when getaddrinfo() returns EAI_AGAIN, meaning a temporary
Antoine Pitrou <solipsis@pitrou.net>
parents: 70558
diff changeset
832 ('EAI_AGAIN', -3),
73918
5ba1a22c8988 Issue #13453: Catch EAI_FAIL in support.transient_internet.
Charles-François Natali <neologix@free.fr>
parents: 71271
diff changeset
833 ('EAI_FAIL', -4),
64542
d008f406dc98 Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
834 ('EAI_NONAME', -2),
d008f406dc98 Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
835 ('EAI_NODATA', -5),
77786
f41c08d5c9e8 Try to fix failure of a Windows buildbot to capture name resolution errors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77072
diff changeset
836 # Windows defines EAI_NODATA as 11001 but idiotic getaddrinfo()
f41c08d5c9e8 Try to fix failure of a Windows buildbot to capture name resolution errors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77072
diff changeset
837 # implementation actually returns WSANO_DATA i.e. 11004.
f41c08d5c9e8 Try to fix failure of a Windows buildbot to capture name resolution errors.
Antoine Pitrou <solipsis@pitrou.net>
parents: 77072
diff changeset
838 ('WSANO_DATA', 11004),
64542
d008f406dc98 Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
839 ]
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
840
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
841 denied = ResourceDenied("Resource '%s' is not available" % resource_name)
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
842 captured_errnos = errnos
64542
d008f406dc98 Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
843 gai_errnos = []
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
844 if not captured_errnos:
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
845 captured_errnos = [getattr(errno, name, num)
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
846 for (name, num) in default_errnos]
64542
d008f406dc98 Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
847 gai_errnos = [getattr(socket, name, num)
d008f406dc98 Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
848 for (name, num) in default_gai_errnos]
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
849
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
850 def filter_error(err):
64542
d008f406dc98 Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
851 n = getattr(err, 'errno', None)
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
852 if (isinstance(err, socket.timeout) or
64542
d008f406dc98 Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
853 (isinstance(err, socket.gaierror) and n in gai_errnos) or
d008f406dc98 Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64539
diff changeset
854 n in captured_errnos):
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
855 if not verbose:
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
856 sys.stderr.write(denied.args[0] + "\n")
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
857 raise denied
62055
38ddeccf02a9 #8889: rewrite transient_internet so we don't use EAI_NODATA on FreeBSD.
R. David Murray <rdmurray@bitdance.com>
parents: 61954
diff changeset
858
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
859 old_timeout = socket.getdefaulttimeout()
62055
38ddeccf02a9 #8889: rewrite transient_internet so we don't use EAI_NODATA on FreeBSD.
R. David Murray <rdmurray@bitdance.com>
parents: 61954
diff changeset
860 try:
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
861 if timeout is not None:
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
862 socket.setdefaulttimeout(timeout)
56585
77ecc5e4f664 Silence a deprecation warning by using the appropriate replacement construct
Nick Coghlan <ncoghlan@gmail.com>
parents: 56565
diff changeset
863 yield
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
864 except IOError as err:
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
865 # urllib can wrap original socket errors multiple times (!), we must
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
866 # unwrap to get at the original error.
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
867 while True:
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
868 a = err.args
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
869 if len(a) >= 1 and isinstance(a[0], IOError):
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
870 err = a[0]
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
871 # The error can also be wrapped as args[1]:
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
872 # except socket.error as msg:
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
873 # raise IOError('socket error', msg).with_traceback(sys.exc_info()[2])
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
874 elif len(a) >= 2 and isinstance(a[1], IOError):
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
875 err = a[1]
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
876 else:
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
877 break
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
878 filter_error(err)
62055
38ddeccf02a9 #8889: rewrite transient_internet so we don't use EAI_NODATA on FreeBSD.
R. David Murray <rdmurray@bitdance.com>
parents: 61954
diff changeset
879 raise
64539
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
880 # XXX should we catch generic exceptions and look for their
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
881 # __cause__ or __context__?
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
882 finally:
af414b29d467 Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 63008
diff changeset
883 socket.setdefaulttimeout(old_timeout)
41135
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
884
91bd738a4206 Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents: 41081
diff changeset
885
42783
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
886 @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
887 def captured_output(stream_name):
70088
459e2c024420 #7960: fix docstrings for captured_output and captured_stdout.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70084
diff changeset
888 """Return a context manager used by captured_stdout and captured_stdin
459e2c024420 #7960: fix docstrings for captured_output and captured_stdout.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70084
diff changeset
889 that temporarily replaces the sys stream *stream_name* with a StringIO."""
42783
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
890 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
891 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
892 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
893 try:
3036e21ac661 make test_support's captured_output a bit more robust when exceptions happen
Benjamin Peterson <benjamin@python.org>
parents: 46425
diff changeset
894 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
895 finally:
3036e21ac661 make test_support's captured_output a bit more robust when exceptions happen
Benjamin Peterson <benjamin@python.org>
parents: 46425
diff changeset
896 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
897
7dff5e41fa7a Generalize test.test_support.test_stdout() with a base context manager so that
Brett Cannon <bcannon@gmail.com>
parents: 45513
diff changeset
898 def captured_stdout():
70088
459e2c024420 #7960: fix docstrings for captured_output and captured_stdout.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70084
diff changeset
899 """Capture the output of sys.stdout:
459e2c024420 #7960: fix docstrings for captured_output and captured_stdout.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70084
diff changeset
900
459e2c024420 #7960: fix docstrings for captured_output and captured_stdout.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70084
diff changeset
901 with captured_stdout() as s:
459e2c024420 #7960: fix docstrings for captured_output and captured_stdout.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70084
diff changeset
902 print "hello"
459e2c024420 #7960: fix docstrings for captured_output and captured_stdout.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70084
diff changeset
903 self.assertEqual(s.getvalue(), "hello")
459e2c024420 #7960: fix docstrings for captured_output and captured_stdout.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 70084
diff changeset
904 """
46241
7dff5e41fa7a Generalize test.test_support.test_stdout() with a base context manager so that
Brett Cannon <bcannon@gmail.com>
parents: 45513
diff changeset
905 return captured_output("stdout")
42783
f798c366270c Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents: 42584
diff changeset
906
70558
07b43607a905 Issue #12085: Fix test_subprocess for my previous commit
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70088
diff changeset
907 def captured_stderr():
07b43607a905 Issue #12085: Fix test_subprocess for my previous commit
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70088
diff changeset
908 return captured_output("stderr")
07b43607a905 Issue #12085: Fix test_subprocess for my previous commit
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70088
diff changeset
909
56563
2048d04deab4 Don't invoke reload(sys) and use StringIO objects instead of real files to capture stdin and stdout when needed (ensures all sys attributes remain unmodified after test_xmlrpc runs)
Nick Coghlan <ncoghlan@gmail.com>
parents: 56562
diff changeset
910 def captured_stdin():
2048d04deab4 Don't invoke reload(sys) and use StringIO objects instead of real files to capture stdin and stdout when needed (ensures all sys attributes remain unmodified after test_xmlrpc runs)
Nick Coghlan <ncoghlan@gmail.com>
parents: 56562
diff changeset
911 return captured_output("stdin")
2048d04deab4 Don't invoke reload(sys) and use StringIO objects instead of real files to capture stdin and stdout when needed (ensures all sys attributes remain unmodified after test_xmlrpc runs)
Nick Coghlan <ncoghlan@gmail.com>
parents: 56562
diff changeset
912
52350
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
913 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
914 """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
915
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
916 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
917 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
918 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
919 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
920 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
921 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
922 """
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
923 gc.collect()
60482
0592e7841777 adjust gc_collect for Jython
Philip Jenvey <pjenvey@underboss.org>
parents: 59683
diff changeset
924 if is_jython:
0592e7841777 adjust gc_collect for Jython
Philip Jenvey <pjenvey@underboss.org>
parents: 59683
diff changeset
925 time.sleep(0.1)
52350
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
926 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
927 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
928
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
929
78317
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
930 _header = '2P'
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
931 if hasattr(sys, "gettotalrefcount"):
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
932 _header = '2P' + _header
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
933 _vheader = _header + 'P'
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
934
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
935 def calcobjsize(fmt):
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
936 return struct.calcsize(_header + fmt + '0P')
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
937
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
938 def calcvobjsize(fmt):
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
939 return struct.calcsize(_vheader + fmt + '0P')
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
940
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
941
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
942 _TPFLAGS_HAVE_GC = 1<<14
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
943 _TPFLAGS_HEAPTYPE = 1<<9
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
944
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
945 def check_sizeof(test, o, size):
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
946 result = sys.getsizeof(o)
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
947 # add GC header size
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
948 if ((type(o) == type) and (o.__flags__ & _TPFLAGS_HEAPTYPE) or\
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
949 ((type(o) != type) and (type(o).__flags__ & _TPFLAGS_HAVE_GC))):
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
950 size += _testcapi.SIZEOF_PYGC_HEAD
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
951 msg = 'wrong size for %s: got %d, expected %d' \
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
952 % (type(o), result, size)
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
953 test.assertEqual(result, size, msg)
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
954
fa95b04e67fd Issue #15467: Move helpers for __sizeof__ tests into test_support.
Martin v. Löwis <martin@v.loewis.de>
parents: 77786
diff changeset
955
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
956 #=======================================================================
38055
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
957 # 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
958 # it afterwards.
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
959
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
960 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
961 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
962 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
963 try:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
964 import locale
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
965 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
966 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
967 except AttributeError:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
968 # 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
969 raise
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
970 except:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
971 # 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
972 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
973 else:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
974 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
975 try:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
976 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
977 break
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
978 except:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
979 pass
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
980
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
981 # 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
982 try:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
983 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
984 finally:
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
985 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
986 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
987 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
988 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
989 return inner
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
990 return decorator
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
991
fcc61835c6ac Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents: 38015
diff changeset
992 #=======================================================================
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
993 # 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
994
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
995 # 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
996 # 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
997 _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
998 _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
999 _2G = 2 * _1G
48113
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1000 _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
1001
47347
343aaaa802e8 remove some __getslice__
Benjamin Peterson <benjamin@python.org>
parents: 47316
diff changeset
1002 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
1003
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
1004 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
1005 global max_memuse
48113
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1006 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
1007 sizes = {
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
1008 '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
1009 '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
1010 '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
1011 '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
1012 }
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
1013 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
1014 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
1015 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
1016 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
1017 memlimit = int(float(m.group(1)) * sizes[m.group(3).lower()])
48113
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1018 real_max_memuse = memlimit
39417
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
1019 if memlimit > MAX_Py_ssize_t:
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
1020 memlimit = MAX_Py_ssize_t
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
1021 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
1022 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
1023 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
1024
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
1025 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
1026 """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
1027
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
1028 '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
1029 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
1030 the test, or a good estimate of it. 'overhead' specifies fixed overhead,
46425
29231a973580 spelling
Skip Montanaro <skip@pobox.com>
parents: 46414
diff changeset
1031 independent of the testsize, and defaults to 5Mb.
37993
358662e2f273 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37992
diff changeset
1032
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
1033 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
1034 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
1035 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
1036 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
1037 """
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
1038 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
1039 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
1040 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
1041 # 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
1042 # 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
1043 # 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
1044 # 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
1045 maxsize = 5147
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54717
diff changeset
1046 self.assertFalse(maxsize * memuse + overhead > 20 * _1M)
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
1047 else:
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
1048 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
1049 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
1050 # 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
1051 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
1052 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
1053 "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
1054 return
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
1055 # 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
1056 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
1057 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
1058 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
1059 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
1060 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
1061 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
1062 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
1063
48113
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1064 def precisionbigmemtest(size, memuse, overhead=5*_1M):
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1065 def decorator(f):
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1066 def wrapper(self):
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1067 if not real_max_memuse:
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1068 maxsize = 5147
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1069 else:
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1070 maxsize = size
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1071
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1072 if real_max_memuse and real_max_memuse < maxsize * memuse:
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1073 if verbose:
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1074 sys.stderr.write("Skipping %s because of memory "
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1075 "constraint\n" % (f.__name__,))
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1076 return
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1077
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1078 return f(self, maxsize)
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1079 wrapper.size = size
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1080 wrapper.memuse = memuse
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1081 wrapper.overhead = overhead
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1082 return wrapper
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1083 return decorator
0aafd1ae5120 Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 44989
diff changeset
1084
39417
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
1085 def bigaddrspacetest(f):
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
1086 """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
1087 def wrapper(self):
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
1088 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
1089 if verbose:
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
1090 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
1091 "constraint\n" % (f.__name__,))
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
1092 else:
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
1093 return f(self)
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
1094 return wrapper
74d23cd04421 Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents: 38961
diff changeset
1095
37992
8dc2a4d8a22e The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents: 36382
diff changeset
1096 #=======================================================================
41539
761ce3946b86 Import and raise statement cleanup.
Collin Winter <collinw@gmail.com>
parents: 41536
diff changeset
1097 # 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
1098
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17109
diff changeset
1099 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
1100 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
1101 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
1102 test(result)
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
1103 return result
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
1104
52350
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
1105 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
1106 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
1107
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
1108 def requires_resource(resource):
65318
07b6bb76ee43 Merged revisions 85482 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65306
diff changeset
1109 if is_resource_enabled(resource):
52350
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
1110 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
1111 else:
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
1112 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
1113
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
1114 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
1115 """
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
1116 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
1117 """
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
1118 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
1119
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
1120 def impl_detail(msg=None, **guards):
52359
f26c1cd6c985 ** is required here
Benjamin Peterson <benjamin@python.org>
parents: 52356
diff changeset
1121 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
1122 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
1123 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
1124 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
1125 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
1126 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
1127 else:
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
1128 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
1129 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
1130 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
1131 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
1132
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
1133 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
1134 # 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
1135 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
1136 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
1137 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
1138 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
1139 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
1140
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
1141 # 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
1142 # 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
1143 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
1144 """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
1145 Examples:
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
1146 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
1147 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
1148 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
1149 """
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
1150 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
1151 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
1152
4fe76249a909 add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents: 52334
diff changeset
1153
17109
7eb8b240dd6c Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents: 17036
diff changeset
1154
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
1155 def _run_suite(suite):
19995
b07cb51d731c run_suite(): Oops, update a docstring.
Barry Warsaw <barry@python.org>
parents: 19994
diff changeset
1156 """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
1157 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
1158 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
1159 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
1160 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
1161
17133
d0fd60c59aef Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents: 17109
diff changeset
1162 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
1163 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
1164 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
1165 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
1166 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
1167 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
1168 else:
56930
044a68bc0d2e Merged revisions 75528 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53383
diff changeset
1169 err = "multiple errors occurred"
044a68bc0d2e Merged revisions 75528 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53383
diff changeset
1170 if not verbose:
044a68bc0d2e Merged revisions 75528 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53383
diff changeset
1171 err += "; run in verbose mode for details"
19807
086ffafaed55 It appears that unittest was changed to stop hoarding raw exception data,
Tim Peters <tim.peters@gmail.com>
parents: 19768
diff changeset
1172 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
1173
19994
ab6c14822de1 run_suite(): Factor this out of run_unittest() for tests that build
Barry Warsaw <barry@python.org>
parents: 19819
diff changeset
1174
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28863
diff changeset
1175 def run_unittest(*classes):
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28863
diff changeset
1176 """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
1177 valid_types = (unittest.TestSuite, unittest.TestCase)
28863
6d852ebff36f Factor out common boilerplate for test_support
Raymond Hettinger <python@rcn.com>
parents: 28799
diff changeset
1178 suite = unittest.TestSuite()
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28863
diff changeset
1179 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
1180 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
1181 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
1182 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
1183 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
1184 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
1185 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
1186 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
1187 else:
3f214b39b4df run_unittest() to support TestCase instances as well as classes. Helps with doctests.
Raymond Hettinger <python@rcn.com>
parents: 29142
diff changeset
1188 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
1189 _run_suite(suite)
28863
6d852ebff36f Factor out common boilerplate for test_support
Raymond Hettinger <python@rcn.com>
parents: 28799
diff changeset
1190
19994
ab6c14822de1 run_suite(): Factor this out of run_unittest() for tests that build
Barry Warsaw <barry@python.org>
parents: 19819
diff changeset
1191
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
1192 #=======================================================================
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
1193 # 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
1194
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
1195 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
1196 """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
1197
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
1198 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
1199 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
1200 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
1201 """
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
1202
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
1203 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
1204
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
1205 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
1206 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
1207 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
1208 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
1209
20088
386fb26e0874 Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents: 20083
diff changeset
1210 # 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
1211 # 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
1212 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
1213 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
1214 try:
386fb26e0874 Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents: 20083
diff changeset
1215 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
1216 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
1217 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
1218 finally:
386fb26e0874 Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents: 20083
diff changeset
1219 sys.stdout = save_stdout
29140
e57e22aa56a4 Provide a clue that the doctests have run.
Raymond Hettinger <python@rcn.com>
parents: 29066
diff changeset
1220 if verbose:
29142
5da198480b6a Include module name in doctest summary.
Raymond Hettinger <python@rcn.com>
parents: 29140
diff changeset
1221 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
1222 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
1223
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
1224 #=======================================================================
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
1225 # 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
1226
56980
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56625
diff changeset
1227 # NOTE: we use thread._count() rather than threading.enumerate() (or the
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56625
diff changeset
1228 # moral equivalent thereof) because a threading.Thread object is still alive
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56625
diff changeset
1229 # until its __bootstrap() method has returned, even after it has been
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56625
diff changeset
1230 # unregistered from the threading module.
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56625
diff changeset
1231 # thread._count(), on the other hand, only gets decremented *after* the
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56625
diff changeset
1232 # __bootstrap() method has returned, which gives us reliable reference counts
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56625
diff changeset
1233 # at the end of a test run.
38867
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
1234
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
1235 def threading_setup():
61027
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
1236 if thread:
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
1237 return thread._count(),
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
1238 else:
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
1239 return 1,
38867
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
1240
56980
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56625
diff changeset
1241 def threading_cleanup(nb_threads):
61027
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
1242 if not thread:
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
1243 return
38867
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
1244
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
1245 _MAX_COUNT = 10
56980
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56625
diff changeset
1246 for count in range(_MAX_COUNT):
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56625
diff changeset
1247 n = thread._count()
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56625
diff changeset
1248 if n == nb_threads:
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56625
diff changeset
1249 break
38867
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
1250 time.sleep(0.1)
56980
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56625
diff changeset
1251 # XXX print a warning in case of failure?
38867
30d2333852d5 Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents: 38767
diff changeset
1252
54085
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
1253 def reap_threads(func):
61027
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
1254 """Use this function when threads are being used. This will
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
1255 ensure that the threads are cleaned up even when the test fails.
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
1256 If threading is unavailable this function does nothing.
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
1257 """
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
1258 if not thread:
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
1259 return func
6d9d8545861e Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents: 60482
diff changeset
1260
54085
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
1261 @functools.wraps(func)
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
1262 def decorator(*args):
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
1263 key = threading_setup()
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
1264 try:
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
1265 return func(*args)
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
1266 finally:
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
1267 threading_cleanup(*key)
e45186edac91 a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents: 53644
diff changeset
1268 return decorator
38961
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
1269
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
1270 def reap_children():
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
1271 """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
1272 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
1273 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
1274 for refleaks.
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
1275 """
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
1276
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
1277 # 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
1278 # 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
1279 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
1280 any_process = -1
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
1281 while True:
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
1282 try:
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
1283 # 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
1284 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
1285 if pid == 0:
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
1286 break
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
1287 except:
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 38875
diff changeset
1288 break
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
1289
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 54466
diff changeset
1290 def py3k_bytes(b):
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 54466
diff changeset
1291 """Emulate the py3k bytes() constructor.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 54466
diff changeset
1292
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 54466
diff changeset
1293 NOTE: This is only a best effort function.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 54466
diff changeset
1294 """
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 54466
diff changeset
1295 try:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 54466
diff changeset
1296 # memoryview?
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 54466
diff changeset
1297 return b.tobytes()
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 54466
diff changeset
1298 except AttributeError:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 54466
diff changeset
1299 try:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 54466
diff changeset
1300 # iterable of ints?
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 54466
diff changeset
1301 return b"".join(chr(x) for x in b)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 54466
diff changeset
1302 except TypeError:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 54466
diff changeset
1303 return bytes(b)
65305
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1304
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1305 def args_from_interpreter_flags():
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1306 """Return a list of command-line arguments reproducing the current
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1307 settings in sys.flags."""
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1308 flag_opt_map = {
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1309 'bytes_warning': 'b',
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1310 'dont_write_bytecode': 'B',
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1311 'ignore_environment': 'E',
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1312 'no_user_site': 's',
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1313 'no_site': 'S',
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1314 'optimize': 'O',
65306
6a8489554019 Add support for "-3" flag (closes #7516)
Antoine Pitrou <solipsis@pitrou.net>
parents: 65305
diff changeset
1315 'py3k_warning': '3',
65305
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1316 'verbose': 'v',
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1317 }
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1318 args = []
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1319 for flag, opt in flag_opt_map.items():
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1320 v = getattr(sys.flags, flag)
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1321 if v > 0:
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1322 args.append('-' + opt * v)
a5eead141cb4 Merged revisions 85469 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64602
diff changeset
1323 return args
67312
a476eb087ee9 Merged revisions 87695 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 65318
diff changeset
1324
75100
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
1325 def strip_python_stderr(stderr):
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
1326 """Strip the stderr of a Python process from potential debug output
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
1327 emitted by the interpreter.
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
1328
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
1329 This will typically be run on the result of the communicate() method
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
1330 of a subprocess.Popen object.
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
1331 """
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
1332 stderr = re.sub(br"\[\d+ refs\]\r?\n?$", b"", stderr).strip()
6b7704fe1be1 - Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents: 68255
diff changeset
1333 return stderr