Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_support.py @ 65035:87f4b32905df 2.7
Cosmetic fix to use print function.
| author | Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp> |
|---|---|
| date | Fri, 01 Oct 2010 10:48:47 +0000 |
| parents | 457aa8da490a |
| children | a5eead141cb4 |
| 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 |
|
59432
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
19 import re |
|
60482
0592e7841777
adjust gc_collect for Jython
Philip Jenvey <pjenvey@underboss.org>
parents:
59683
diff
changeset
|
20 import time |
|
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
|
21 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
|
22 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
|
23 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
|
24 thread = None |
|
17109
7eb8b240dd6c
Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents:
17036
diff
changeset
|
25 |
|
52353
e6009b9cd908
remove test_support.TestSkipped and just use unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents:
52350
diff
changeset
|
26 __all__ = ["Error", "TestFailed", "ResourceDenied", "import_module", |
|
47316
c5604be7acdf
add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents:
46863
diff
changeset
|
27 "verbose", "use_resources", "max_memuse", "record_original_stdout", |
|
c5604be7acdf
add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents:
46863
diff
changeset
|
28 "get_original_stdout", "unload", "unlink", "rmtree", "forget", |
|
c5604be7acdf
add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents:
46863
diff
changeset
|
29 "is_resource_enabled", "requires", "find_unused_port", "bind_port", |
|
c5604be7acdf
add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents:
46863
diff
changeset
|
30 "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
|
31 "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
|
32 "open_urlresource", "check_warnings", "check_py3k_warnings", |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
33 "CleanImport", "EnvironmentVarGuard", "captured_output", |
|
47316
c5604be7acdf
add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents:
46863
diff
changeset
|
34 "captured_stdout", "TransientResource", "transient_internet", |
|
c5604be7acdf
add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents:
46863
diff
changeset
|
35 "run_with_locale", "set_memlimit", "bigmemtest", "bigaddrspacetest", |
|
c5604be7acdf
add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents:
46863
diff
changeset
|
36 "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
|
37 "threading_cleanup", "reap_children", "cpython_only", |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
54466
diff
changeset
|
38 "check_impl_detail", "get_attribute", "py3k_bytes"] |
|
47316
c5604be7acdf
add __all__ to test_support
Benjamin Peterson <benjamin@python.org>
parents:
46863
diff
changeset
|
39 |
|
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
|
40 |
|
13682
7de18f04f85a
Restore TestSkipped exception; appears to have disappeared in last checkin.
Fred Drake <fdrake@acm.org>
parents:
13647
diff
changeset
|
41 class Error(Exception): |
|
15305
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
13682
diff
changeset
|
42 """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
|
43 |
|
7de18f04f85a
Restore TestSkipped exception; appears to have disappeared in last checkin.
Fred Drake <fdrake@acm.org>
parents:
13647
diff
changeset
|
44 class TestFailed(Error): |
|
15305
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
13682
diff
changeset
|
45 """Test failed.""" |
| 653 | 46 |
| 52354 | 47 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
|
48 """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
|
49 |
|
974f196abb87
test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents:
26527
diff
changeset
|
50 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
|
51 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
|
52 and unexpected skips. |
|
974f196abb87
test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents:
26527
diff
changeset
|
53 """ |
|
974f196abb87
test_support.requires(): Instead of raising TestSkipped, raise a new
Fred Drake <fdrake@acm.org>
parents:
26527
diff
changeset
|
54 |
|
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
|
55 @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
|
56 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
|
57 """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
|
58 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
|
59 |
|
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 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
|
61 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
|
62 with warnings.catch_warnings(): |
|
fb41fe67e061
Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents:
52573
diff
changeset
|
63 warnings.filterwarnings("ignore", ".+ (module|package)", |
|
fb41fe67e061
Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents:
52573
diff
changeset
|
64 DeprecationWarning) |
|
fb41fe67e061
Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents:
52573
diff
changeset
|
65 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
|
66 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
|
67 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
|
68 |
|
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 |
|
46752
9fbc2da54e29
The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents:
46611
diff
changeset
|
70 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
|
71 """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
|
72 it is not available. |
|
68b7e8217c7e
Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents:
52555
diff
changeset
|
73 |
|
68b7e8217c7e
Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents:
52555
diff
changeset
|
74 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
|
75 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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 |
|
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
|
82 |
|
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
|
83 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
|
84 """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
|
85 |
|
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 Return value is True if the module was in sys.modules and |
|
e9595cb5c786
Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents:
53048
diff
changeset
|
87 False otherwise.""" |
|
e9595cb5c786
Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents:
53048
diff
changeset
|
88 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
|
89 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
|
90 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
|
91 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
|
92 saved = False |
|
e9595cb5c786
Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents:
53048
diff
changeset
|
93 else: |
|
e9595cb5c786
Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents:
53048
diff
changeset
|
94 del sys.modules[name] |
|
e9595cb5c786
Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents:
53048
diff
changeset
|
95 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
|
96 |
|
e9595cb5c786
Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents:
53048
diff
changeset
|
97 |
|
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 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
|
99 """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
|
100 |
|
e9595cb5c786
Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents:
53048
diff
changeset
|
101 Return value is True if the module was in sys.modules and |
|
e9595cb5c786
Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents:
53048
diff
changeset
|
102 False otherwise.""" |
|
e9595cb5c786
Issue 5354: Change API for import_fresh_module() to better support test_warnings use case (also fixes some bugs in the original implementation)
Nick Coghlan <ncoghlan@gmail.com>
parents:
53048
diff
changeset
|
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. |
|
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
|
118 |
|
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 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
|
120 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
|
121 |
|
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 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
|
123 will be suppressed.""" |
|
fb41fe67e061
Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents:
52573
diff
changeset
|
124 # NOTE: test_heapq and test_warnings include extra sanity checks to make |
|
fb41fe67e061
Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents:
52573
diff
changeset
|
125 # sure that this utility function is working as expected |
|
fb41fe67e061
Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents:
52573
diff
changeset
|
126 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
|
127 # 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
|
128 # 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
|
129 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
|
130 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
|
131 _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
|
132 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
|
133 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
|
134 _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
|
135 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
|
136 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
|
137 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
|
138 fresh_module = importlib.import_module(name) |
|
53048
fb41fe67e061
Issue 5354: Provide a standardised testing mechanism for doing fresh imports of modules, including the ability to block extension modules in order to test the pure Python fallbacks
Nick Coghlan <ncoghlan@gmail.com>
parents:
52573
diff
changeset
|
139 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
|
140 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
|
141 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
|
142 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
|
143 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
|
144 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
|
145 |
|
46752
9fbc2da54e29
The mutex module has been deprecated for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents:
46611
diff
changeset
|
146 |
|
52573
68b7e8217c7e
Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents:
52555
diff
changeset
|
147 def get_attribute(obj, name): |
|
68b7e8217c7e
Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents:
52555
diff
changeset
|
148 """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
|
149 try: |
|
68b7e8217c7e
Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents:
52555
diff
changeset
|
150 attribute = getattr(obj, name) |
|
68b7e8217c7e
Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents:
52555
diff
changeset
|
151 except AttributeError: |
|
68b7e8217c7e
Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents:
52555
diff
changeset
|
152 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
|
153 obj.__name__, name)) |
|
68b7e8217c7e
Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents:
52555
diff
changeset
|
154 else: |
|
68b7e8217c7e
Improve test_support.import_module docstring, remove
R. David Murray <rdmurray@bitdance.com>
parents:
52555
diff
changeset
|
155 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
|
156 |
|
f2c90fd05d66
Add import_function method to test.test_support, and modify a number of
R. David Murray <rdmurray@bitdance.com>
parents:
52359
diff
changeset
|
157 |
|
19416
a6ce2d16d739
use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
158 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
|
159 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
|
160 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
|
161 # small sizes, to make sure they work.) |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
162 real_max_memuse = 0 |
|
4791
a2fd80eed27f
Add verbose flag for regression test to clear.
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
163 |
|
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
|
164 # _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
|
165 # 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
|
166 # 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
|
167 _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
|
168 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
|
169 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
|
170 _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
|
171 |
|
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 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
|
173 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
|
174 |
| 653 | 175 def unload(name): |
|
15305
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
13682
diff
changeset
|
176 try: |
|
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
13682
diff
changeset
|
177 del sys.modules[name] |
|
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
13682
diff
changeset
|
178 except KeyError: |
|
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
13682
diff
changeset
|
179 pass |
| 653 | 180 |
|
36382
99d491faa65c
Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents:
35998
diff
changeset
|
181 def unlink(filename): |
|
99d491faa65c
Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents:
35998
diff
changeset
|
182 try: |
|
99d491faa65c
Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents:
35998
diff
changeset
|
183 os.unlink(filename) |
|
99d491faa65c
Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents:
35998
diff
changeset
|
184 except OSError: |
|
99d491faa65c
Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents:
35998
diff
changeset
|
185 pass |
|
99d491faa65c
Convenience function to remove a possibly non-existant file
Neal Norwitz <nnorwitz@gmail.com>
parents:
35998
diff
changeset
|
186 |
|
45506
55eefb43ef6f
Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents:
45468
diff
changeset
|
187 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
|
188 try: |
|
55eefb43ef6f
Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents:
45468
diff
changeset
|
189 shutil.rmtree(path) |
|
55eefb43ef6f
Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents:
45468
diff
changeset
|
190 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
|
191 # 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
|
192 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
|
193 raise |
|
55eefb43ef6f
Create a db_home directory with a unique name so multiple users can
Neal Norwitz <nnorwitz@gmail.com>
parents:
45468
diff
changeset
|
194 |
| 653 | 195 def forget(modname): |
|
28968
b174d0927f0e
'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents:
28917
diff
changeset
|
196 '''"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
|
197 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
|
198 unload(modname) |
|
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
13682
diff
changeset
|
199 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
|
200 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
|
201 # 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
|
202 # 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
|
203 # 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
|
204 unlink(os.path.join(dirname, modname + os.extsep + 'pyo')) |
| 653 | 205 |
|
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
|
206 def is_resource_enabled(resource): |
|
28968
b174d0927f0e
'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents:
28917
diff
changeset
|
207 """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
|
208 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
|
209 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
|
210 |
|
19416
a6ce2d16d739
use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
211 def requires(resource, msg=None): |
|
28968
b174d0927f0e
'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents:
28917
diff
changeset
|
212 """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
|
213 |
|
b174d0927f0e
'forget' now also deletes any proper .pyo files.
Brett Cannon <bcannon@gmail.com>
parents:
28917
diff
changeset
|
214 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
|
215 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
|
216 # 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
|
217 # the resource was set |
|
52334
1fbec29f89e8
this can be slightly less ugly
Benjamin Peterson <benjamin@python.org>
parents:
52320
diff
changeset
|
218 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
|
219 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
|
220 if not is_resource_enabled(resource): |
|
19416
a6ce2d16d739
use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
221 if msg is None: |
|
a6ce2d16d739
use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
222 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
|
223 raise ResourceDenied(msg) |
|
19416
a6ce2d16d739
use_large_resources => use_resources
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
224 |
|
46362
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
46241
diff
changeset
|
225 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
|
226 |
|
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
46241
diff
changeset
|
227 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
|
228 """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
|
229 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
|
230 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
|
231 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
|
232 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
|
233 then closed and deleted, and the ephemeral port is returned. |
|
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
46241
diff
changeset
|
234 |
|
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
46241
diff
changeset
|
235 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
|
236 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
|
237 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
|
238 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
|
239 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
|
240 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
|
241 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
|
242 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
|
243 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
|
244 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
|
245 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
|
246 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
|
247 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
|
248 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
|
249 |
|
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
46241
diff
changeset
|
250 (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
|
251 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
|
252 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
|
253 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
|
254 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
|
255 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
|
256 |
|
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
46241
diff
changeset
|
257 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
|
258 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
|
259 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
|
260 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
|
261 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
|
262 must be forcibly killed via OpenProcess()/TerminateProcess(). |
|
38767
46f3eb474c84
Fix the socket tests so they can be run concurrently. Backport candidate
Neal Norwitz <nnorwitz@gmail.com>
parents:
38055
diff
changeset
|
263 |
|
46362
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
46241
diff
changeset
|
264 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
|
265 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
|
266 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
|
267 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
|
268 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
|
269 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
|
270 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
|
271 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
|
272 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
|
273 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
|
274 |
|
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
46241
diff
changeset
|
275 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
|
276 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
|
277 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
|
278 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
|
279 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
|
280 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
|
281 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
|
282 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
|
283 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
|
284 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
|
285 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
|
286 |
|
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
46241
diff
changeset
|
287 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
|
288 """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
|
289 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
|
290 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
|
291 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
|
292 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
|
293 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
|
294 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
|
295 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
|
296 |
|
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
46241
diff
changeset
|
297 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
|
298 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
|
299 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
|
300 """ |
|
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
46241
diff
changeset
|
301 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
|
302 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
|
303 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
|
304 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
|
305 "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
|
306 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
|
307 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
|
308 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
|
309 "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
|
310 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
|
311 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
|
312 |
|
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
46241
diff
changeset
|
313 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
|
314 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
|
315 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
|
316 |
|
1197
675a02820d66
* os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents:
653
diff
changeset
|
317 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
|
318 |
|
675a02820d66
* os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents:
653
diff
changeset
|
319 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
|
320 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
|
321 try: |
|
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
13682
diff
changeset
|
322 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
|
323 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
|
324 return 0 |
|
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
13682
diff
changeset
|
325 except: |
|
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
13682
diff
changeset
|
326 pass |
|
48109
6f232fcfb657
Backport test.support.fcmp() from 3.0 to silence -3 warnings.
Brett Cannon <bcannon@gmail.com>
parents:
48053
diff
changeset
|
327 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
|
328 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
|
329 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
|
330 if outcome != 0: |
|
15305
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
13682
diff
changeset
|
331 return outcome |
|
48109
6f232fcfb657
Backport test.support.fcmp() from 3.0 to silence -3 warnings.
Brett Cannon <bcannon@gmail.com>
parents:
48053
diff
changeset
|
332 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
|
333 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
|
334 |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
18635
diff
changeset
|
335 try: |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
18635
diff
changeset
|
336 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
|
337 have_unicode = True |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
18635
diff
changeset
|
338 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
|
339 have_unicode = False |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
18635
diff
changeset
|
340 |
|
25982
e1bf4604c0a5
Patch #631972: Adds an is_jython flag.
Finn Bock <bckfnn@worldonline.dk>
parents:
24442
diff
changeset
|
341 is_jython = sys.platform.startswith('java') |
|
e1bf4604c0a5
Patch #631972: Adds an is_jython flag.
Finn Bock <bckfnn@worldonline.dk>
parents:
24442
diff
changeset
|
342 |
|
17213
4d6ee7f2bee6
In Jython, `@' is not allowed in module names. Extend the TESTFN test
Barry Warsaw <barry@python.org>
parents:
17171
diff
changeset
|
343 # 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
|
344 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
|
345 # 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
|
346 TESTFN = '$test' |
|
29066
90342f395721
Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents:
28968
diff
changeset
|
347 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
|
348 TESTFN = 'testfile' |
|
90342f395721
Patch #734231: Update RiscOS support. In particular, correct
Martin v. Löwis <martin@v.loewis.de>
parents:
28968
diff
changeset
|
349 else: |
|
17213
4d6ee7f2bee6
In Jython, `@' is not allowed in module names. Extend the TESTFN test
Barry Warsaw <barry@python.org>
parents:
17171
diff
changeset
|
350 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
|
351 # 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
|
352 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
|
353 # 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
|
354 # 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
|
355 # 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
|
356 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
|
357 # 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
|
358 # 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
|
359 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
|
360 else: |
|
31092
de391e9d70ea
Typo repair; added some comments and horizontal whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
31087
diff
changeset
|
361 # 2 latin characters. |
|
de391e9d70ea
Typo repair; added some comments and horizontal whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
31087
diff
changeset
|
362 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
|
363 TESTFN_ENCODING = sys.getfilesystemencoding() |
|
64602
457aa8da490a
Recorded merge of revisions 83987 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64542
diff
changeset
|
364 # 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
|
365 # 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
|
366 # 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
|
367 # 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
|
368 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
|
369 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
|
370 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
|
371 else: |
|
31087
8dd159279576
Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents:
31078
diff
changeset
|
372 # 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
|
373 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
|
374 try: |
|
8dd159279576
Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents:
31078
diff
changeset
|
375 # 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
|
376 # 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
|
377 # 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
|
378 # 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
|
379 # 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
|
380 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
|
381 except UnicodeEncodeError: |
|
8dd159279576
Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents:
31078
diff
changeset
|
382 pass |
|
8dd159279576
Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents:
31078
diff
changeset
|
383 else: |
|
8dd159279576
Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents:
31078
diff
changeset
|
384 print \ |
|
8dd159279576
Fix test_unicode_file errors on platforms without Unicode file support,
Mark Hammond <mhammond@skippinet.com.au>
parents:
31078
diff
changeset
|
385 '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
|
386 '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
|
387 % TESTFN_UNENCODABLE |
|
26000
83d21ea9457d
Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents:
25982
diff
changeset
|
388 |
|
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
|
389 |
|
54465
d74b8edd7a8c
Issue #6152: New option '-j'/'--multiprocess' for regrtest allows running
Antoine Pitrou <solipsis@pitrou.net>
parents:
54085
diff
changeset
|
390 # 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
|
391 # 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
|
392 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
|
393 |
|
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
|
394 # 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
|
395 SAVEDCWD = os.getcwd() |
|
54465
d74b8edd7a8c
Issue #6152: New option '-j'/'--multiprocess' for regrtest allows running
Antoine Pitrou <solipsis@pitrou.net>
parents:
54085
diff
changeset
|
396 |
|
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
|
397 @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
|
398 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
|
399 """ |
|
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
|
400 Context manager that creates a temporary directory and set it as 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
|
401 |
|
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
|
402 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
|
403 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
|
404 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
|
405 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
|
406 """ |
|
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
|
407 if isinstance(name, unicode): |
|
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
|
408 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
|
409 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
|
410 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
|
411 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
|
412 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
|
413 '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
|
414 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
|
415 is_temporary = False |
|
26000
83d21ea9457d
Fix SF # 631066, running regrtest in user mode fails
Neal Norwitz <nnorwitz@gmail.com>
parents:
25982
diff
changeset
|
416 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
|
417 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
|
418 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
|
419 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
|
420 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
|
421 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
|
422 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
|
423 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
|
424 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
|
425 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
|
426 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
|
427 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
|
428 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
|
429 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
|
430 rmtree(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
|
431 |
|
17036
ad741745cb6c
Oops. A RISCOS patch I forgot to check in.
Guido van Rossum <guido@python.org>
parents:
16723
diff
changeset
|
432 |
|
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
|
433 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
|
434 """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
|
435 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
|
436 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
|
437 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
|
438 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
|
439 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
|
440 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
|
441 path = sys.path |
|
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
13682
diff
changeset
|
442 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
|
443 for dn in path: |
|
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
13682
diff
changeset
|
444 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
|
445 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
|
446 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
|
447 |
|
17815
d6df8d371b67
Get rid of the superstitious "~" in dict hashing's "i = (~hash) & mask".
Tim Peters <tim.peters@gmail.com>
parents:
17213
diff
changeset
|
448 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
|
449 "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
|
450 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
|
451 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
|
452 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
|
453 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
|
454 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
|
455 |
|
50914
caa79d238917
Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49146
diff
changeset
|
456 def make_bad_fd(): |
|
caa79d238917
Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49146
diff
changeset
|
457 """ |
|
caa79d238917
Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49146
diff
changeset
|
458 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
|
459 its fd. |
|
caa79d238917
Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49146
diff
changeset
|
460 """ |
|
caa79d238917
Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49146
diff
changeset
|
461 file = open(TESTFN, "wb") |
|
caa79d238917
Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49146
diff
changeset
|
462 try: |
|
caa79d238917
Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49146
diff
changeset
|
463 return file.fileno() |
|
caa79d238917
Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49146
diff
changeset
|
464 finally: |
|
caa79d238917
Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49146
diff
changeset
|
465 file.close() |
|
caa79d238917
Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49146
diff
changeset
|
466 unlink(TESTFN) |
|
caa79d238917
Merged revisions 68779 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49146
diff
changeset
|
467 |
|
40217
13b8ad679def
Convert test_global, test_scope and test_grammar to unittest.
Georg Brandl <georg@python.org>
parents:
39417
diff
changeset
|
468 def check_syntax_error(testcase, statement): |
|
52320
a9515f29b38c
this is better written using assertRaises
Benjamin Peterson <benjamin@python.org>
parents:
50914
diff
changeset
|
469 testcase.assertRaises(SyntaxError, compile, statement, |
|
a9515f29b38c
this is better written using assertRaises
Benjamin Peterson <benjamin@python.org>
parents:
50914
diff
changeset
|
470 '<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
|
471 |
|
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
|
472 def open_urlresource(url, check=None): |
|
57052
a01e6cef1256
Use a custom timeout in test_support.open_urlresource.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56980
diff
changeset
|
473 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
|
474 |
|
35998
96d1fa36964d
Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
34707
diff
changeset
|
475 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
|
476 |
|
55220
2630c075b57c
put downloaded test support files in Lib/test/data instead of the cwd
Benjamin Peterson <benjamin@python.org>
parents:
55002
diff
changeset
|
477 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
|
478 |
|
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
|
479 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
|
480 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
|
481 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
|
482 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
|
483 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
|
484 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
|
485 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
|
486 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
|
487 |
|
55220
2630c075b57c
put downloaded test support files in Lib/test/data instead of the cwd
Benjamin Peterson <benjamin@python.org>
parents:
55002
diff
changeset
|
488 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
|
489 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
|
490 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
|
491 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
|
492 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
|
493 |
|
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
|
494 # 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
|
495 requires('urlfetch') |
|
35998
96d1fa36964d
Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
34707
diff
changeset
|
496 |
|
96d1fa36964d
Patch #1276356: Implement new resource "urlfetch" for regrtest.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
34707
diff
changeset
|
497 print >> get_original_stdout(), '\tfetching %s ...' % url |
|
57052
a01e6cef1256
Use a custom timeout in test_support.open_urlresource.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56980
diff
changeset
|
498 f = urllib2.urlopen(url, timeout=15) |
|
a01e6cef1256
Use a custom timeout in test_support.open_urlresource.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56980
diff
changeset
|
499 try: |
|
a01e6cef1256
Use a custom timeout in test_support.open_urlresource.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56980
diff
changeset
|
500 with open(fn, "wb") as out: |
|
a01e6cef1256
Use a custom timeout in test_support.open_urlresource.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56980
diff
changeset
|
501 s = f.read() |
|
a01e6cef1256
Use a custom timeout in test_support.open_urlresource.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56980
diff
changeset
|
502 while s: |
|
a01e6cef1256
Use a custom timeout in test_support.open_urlresource.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56980
diff
changeset
|
503 out.write(s) |
|
a01e6cef1256
Use a custom timeout in test_support.open_urlresource.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56980
diff
changeset
|
504 s = f.read() |
|
a01e6cef1256
Use a custom timeout in test_support.open_urlresource.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56980
diff
changeset
|
505 finally: |
|
a01e6cef1256
Use a custom timeout in test_support.open_urlresource.
Antoine Pitrou <solipsis@pitrou.net>
parents:
56980
diff
changeset
|
506 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
|
507 |
|
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
|
508 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
|
509 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
|
510 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
|
511 raise TestFailed('invalid resource "%s"' % fn) |
|
40732
5dae0d7f683a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
40537
diff
changeset
|
512 |
|
40537
be8240ce52ff
Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40455
diff
changeset
|
513 |
|
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
|
514 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
|
515 """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
|
516 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
|
517 """ |
|
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
|
518 def __init__(self, warnings_list): |
|
59432
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
519 self._warnings = warnings_list |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
520 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
|
521 |
|
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
|
522 def __getattr__(self, attr): |
|
59432
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
523 if len(self._warnings) > self._last: |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
524 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
|
525 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
|
526 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
|
527 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
|
528 |
|
59432
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
529 @property |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
530 def warnings(self): |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
531 return self._warnings[self._last:] |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
532 |
|
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
|
533 def reset(self): |
|
59432
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
534 self._last = len(self._warnings) |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
535 |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
536 |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
537 def _filterwarnings(filters, quiet=False): |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
538 """Catch the warnings, then check if all the expected |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
539 warnings have been raised and re-raise unexpected warnings. |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
540 If 'quiet' is True, only re-raise the unexpected warnings. |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
541 """ |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
542 # Clear the warning registry of the calling module |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
543 # in order to re-raise the warnings. |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
544 frame = sys._getframe(2) |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
545 registry = frame.f_globals.get('__warningregistry__') |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
546 if registry: |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
547 registry.clear() |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
548 with warnings.catch_warnings(record=True) as w: |
|
59489
269a12a50123
#7772: Fix test_py3kwarn. Now the test suite could pass with "-3" flag.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59443
diff
changeset
|
549 # Set filter "always" to record all warnings. Because |
|
269a12a50123
#7772: Fix test_py3kwarn. Now the test suite could pass with "-3" flag.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59443
diff
changeset
|
550 # test_warnings swap the module, we need to look up in |
|
269a12a50123
#7772: Fix test_py3kwarn. Now the test suite could pass with "-3" flag.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59443
diff
changeset
|
551 # the sys.modules dictionary. |
|
269a12a50123
#7772: Fix test_py3kwarn. Now the test suite could pass with "-3" flag.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59443
diff
changeset
|
552 sys.modules['warnings'].simplefilter("always") |
|
59432
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
553 yield WarningsRecorder(w) |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
554 # Filter the recorded warnings |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
555 reraise = [warning.message for warning in w] |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
556 missing = [] |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
557 for msg, cat in filters: |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
558 seen = False |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
559 for exc in reraise[:]: |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
560 message = str(exc) |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
561 # Filter out the matching messages |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
562 if (re.match(msg, message, re.I) and |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
563 issubclass(exc.__class__, cat)): |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
564 seen = True |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
565 reraise.remove(exc) |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
566 if not seen and not quiet: |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
567 # This filter caught nothing |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
568 missing.append((msg, cat.__name__)) |
|
59443
e45ff027482f
Refresh the documentation for the test.test_support module.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59432
diff
changeset
|
569 if reraise: |
|
e45ff027482f
Refresh the documentation for the test.test_support module.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59432
diff
changeset
|
570 raise AssertionError("unhandled warning %r" % reraise[0]) |
|
e45ff027482f
Refresh the documentation for the test.test_support module.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59432
diff
changeset
|
571 if missing: |
|
e45ff027482f
Refresh the documentation for the test.test_support module.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59432
diff
changeset
|
572 raise AssertionError("filter (%r, %s) did not catch any warning" % |
|
e45ff027482f
Refresh the documentation for the test.test_support module.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59432
diff
changeset
|
573 missing[0]) |
|
59432
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
574 |
|
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
|
575 |
|
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
|
576 @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
|
577 def check_warnings(*filters, **kwargs): |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
578 """Context manager to silence warnings. |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
579 |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
580 Accept 2-tuples as positional arguments: |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
581 ("message regexp", WarningCategory) |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
582 |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
583 Optional argument: |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
584 - if 'quiet' is True, it does not fail if a filter catches nothing |
|
59683
76f9e3b6c584
#8155: Preserve backward compatibility for test_support.check_warnings(). Add regression tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59643
diff
changeset
|
585 (default True without argument, |
|
76f9e3b6c584
#8155: Preserve backward compatibility for test_support.check_warnings(). Add regression tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59643
diff
changeset
|
586 default False if some filters are defined) |
|
59432
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
587 |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
588 Without argument, it defaults to: |
|
59683
76f9e3b6c584
#8155: Preserve backward compatibility for test_support.check_warnings(). Add regression tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59643
diff
changeset
|
589 check_warnings(("", Warning), quiet=True) |
|
59432
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
590 """ |
|
59683
76f9e3b6c584
#8155: Preserve backward compatibility for test_support.check_warnings(). Add regression tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59643
diff
changeset
|
591 quiet = kwargs.get('quiet') |
|
59432
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
592 if not filters: |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
593 filters = (("", Warning),) |
|
59683
76f9e3b6c584
#8155: Preserve backward compatibility for test_support.check_warnings(). Add regression tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59643
diff
changeset
|
594 # Preserve backward compatibility |
|
76f9e3b6c584
#8155: Preserve backward compatibility for test_support.check_warnings(). Add regression tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59643
diff
changeset
|
595 if quiet is None: |
|
76f9e3b6c584
#8155: Preserve backward compatibility for test_support.check_warnings(). Add regression tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59643
diff
changeset
|
596 quiet = True |
|
76f9e3b6c584
#8155: Preserve backward compatibility for test_support.check_warnings(). Add regression tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59643
diff
changeset
|
597 return _filterwarnings(filters, quiet) |
|
59432
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
598 |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
599 |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
600 @contextlib.contextmanager |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
601 def check_py3k_warnings(*filters, **kwargs): |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
602 """Context manager to silence py3k warnings. |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
603 |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
604 Accept 2-tuples as positional arguments: |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
605 ("message regexp", WarningCategory) |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
606 |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
607 Optional argument: |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
608 - if 'quiet' is True, it does not fail if a filter catches nothing |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
609 (default False) |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
610 |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
611 Without argument, it defaults to: |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
612 check_py3k_warnings(("", DeprecationWarning), quiet=False) |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
613 """ |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
614 if sys.py3kwarning: |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
615 if not filters: |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
616 filters = (("", DeprecationWarning),) |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
617 else: |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
618 # It should not raise any py3k warning |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
619 filters = () |
|
e23cba03cc4b
Issue #7849: Now the utility ``check_warnings`` verifies if the warnings are
Florent Xicluna <florent.xicluna@gmail.com>
parents:
58971
diff
changeset
|
620 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
|
621 |
|
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
|
622 |
|
46861
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
623 class CleanImport(object): |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
624 """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
|
625 |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
626 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
|
627 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
|
628 |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
629 Use like this: |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
630 |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
631 with CleanImport("foo"): |
|
58683
30e4246ee803
Fix a typo in a docstring introduced in r77956.
Brett Cannon <bcannon@gmail.com>
parents:
58682
diff
changeset
|
632 importlib.import_module("foo") # new reference |
|
46861
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
633 """ |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
634 |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
635 def __init__(self, *module_names): |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
636 self.original_modules = sys.modules.copy() |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
637 for module_name in module_names: |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
638 if module_name in sys.modules: |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
639 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
|
640 # 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
|
641 # 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
|
642 # 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
|
643 # the import cache. |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
644 if module.__name__ != module_name: |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
645 del sys.modules[module.__name__] |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
646 del sys.modules[module_name] |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
647 |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
648 def __enter__(self): |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
649 return self |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
650 |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
651 def __exit__(self, *ignore_exc): |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
652 sys.modules.update(self.original_modules) |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
653 |
|
16f16fce5302
Added test framework for handling module renames.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46816
diff
changeset
|
654 |
|
53644
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
53382
diff
changeset
|
655 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
|
656 |
|
be8240ce52ff
Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40455
diff
changeset
|
657 """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
|
658 a context manager.""" |
|
be8240ce52ff
Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40455
diff
changeset
|
659 |
|
be8240ce52ff
Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40455
diff
changeset
|
660 def __init__(self): |
|
53644
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
53382
diff
changeset
|
661 self._environ = os.environ |
|
53382
5a46a82206e7
Issue #5837: Certain sequences of calls to set() and unset() for
Walter Dörwald <walter@livinglogic.de>
parents:
53315
diff
changeset
|
662 self._changed = {} |
|
40537
be8240ce52ff
Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40455
diff
changeset
|
663 |
|
53644
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
53382
diff
changeset
|
664 def __getitem__(self, envvar): |
|
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
53382
diff
changeset
|
665 return self._environ[envvar] |
|
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
53382
diff
changeset
|
666 |
|
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
53382
diff
changeset
|
667 def __setitem__(self, envvar, value): |
|
53382
5a46a82206e7
Issue #5837: Certain sequences of calls to set() and unset() for
Walter Dörwald <walter@livinglogic.de>
parents:
53315
diff
changeset
|
668 # Remember the initial value on the first access |
|
5a46a82206e7
Issue #5837: Certain sequences of calls to set() and unset() for
Walter Dörwald <walter@livinglogic.de>
parents:
53315
diff
changeset
|
669 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
|
670 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
|
671 self._environ[envvar] = value |
|
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
53382
diff
changeset
|
672 |
|
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
53382
diff
changeset
|
673 def __delitem__(self, envvar): |
|
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
53382
diff
changeset
|
674 # 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
|
675 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
|
676 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
|
677 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
|
678 del self._environ[envvar] |
|
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
53382
diff
changeset
|
679 |
|
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
53382
diff
changeset
|
680 def keys(self): |
|
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
53382
diff
changeset
|
681 return self._environ.keys() |
|
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
53382
diff
changeset
|
682 |
|
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
53382
diff
changeset
|
683 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
|
684 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
|
685 |
|
be8240ce52ff
Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40455
diff
changeset
|
686 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
|
687 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
|
688 |
|
be8240ce52ff
Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40455
diff
changeset
|
689 def __enter__(self): |
|
be8240ce52ff
Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40455
diff
changeset
|
690 return self |
|
be8240ce52ff
Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40455
diff
changeset
|
691 |
|
be8240ce52ff
Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40455
diff
changeset
|
692 def __exit__(self, *ignore_exc): |
|
53382
5a46a82206e7
Issue #5837: Certain sequences of calls to set() and unset() for
Walter Dörwald <walter@livinglogic.de>
parents:
53315
diff
changeset
|
693 for (k, v) in self._changed.items(): |
|
5a46a82206e7
Issue #5837: Certain sequences of calls to set() and unset() for
Walter Dörwald <walter@livinglogic.de>
parents:
53315
diff
changeset
|
694 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
|
695 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
|
696 del self._environ[k] |
|
53382
5a46a82206e7
Issue #5837: Certain sequences of calls to set() and unset() for
Walter Dörwald <walter@livinglogic.de>
parents:
53315
diff
changeset
|
697 else: |
|
53644
85542715a249
Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents:
53382
diff
changeset
|
698 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
|
699 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
|
700 |
|
40537
be8240ce52ff
Add EnvironmentVarGuard to test.test_support. Provides a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40455
diff
changeset
|
701 |
|
56562
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
702 class DirsOnSysPath(object): |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
703 """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
|
704 |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
705 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
|
706 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
|
707 settings when the context ends. |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
708 |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
709 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
|
710 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
|
711 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
|
712 """ |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
713 |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
714 def __init__(self, *paths): |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
715 self.original_value = sys.path[:] |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
716 self.original_object = sys.path |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
717 sys.path.extend(paths) |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
718 |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
719 def __enter__(self): |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
720 return self |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
721 |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
722 def __exit__(self, *ignore_exc): |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
723 sys.path = self.original_object |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
724 sys.path[:] = self.original_value |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
725 |
|
a1088bb4fcf8
Restore original sys.path when running TTK tests
Nick Coghlan <ncoghlan@gmail.com>
parents:
55220
diff
changeset
|
726 |
|
41081
be1e2f745669
Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40732
diff
changeset
|
727 class TransientResource(object): |
|
be1e2f745669
Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40732
diff
changeset
|
728 |
|
be1e2f745669
Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40732
diff
changeset
|
729 """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
|
730 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
|
731 |
|
be1e2f745669
Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40732
diff
changeset
|
732 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
|
733 self.exc = exc |
|
be1e2f745669
Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40732
diff
changeset
|
734 self.attrs = kwargs |
|
be1e2f745669
Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40732
diff
changeset
|
735 |
|
be1e2f745669
Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40732
diff
changeset
|
736 def __enter__(self): |
|
be1e2f745669
Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40732
diff
changeset
|
737 return self |
|
be1e2f745669
Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40732
diff
changeset
|
738 |
|
be1e2f745669
Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40732
diff
changeset
|
739 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
|
740 """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
|
741 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
|
742 propagate (if any).""" |
|
be1e2f745669
Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40732
diff
changeset
|
743 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
|
744 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
|
745 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
|
746 break |
|
be1e2f745669
Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40732
diff
changeset
|
747 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
|
748 break |
|
be1e2f745669
Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40732
diff
changeset
|
749 else: |
|
be1e2f745669
Introduce test.test_support.TransientResource. It's a context manager to
Brett Cannon <bcannon@gmail.com>
parents:
40732
diff
changeset
|
750 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
|
751 |
|
17109
7eb8b240dd6c
Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents:
17036
diff
changeset
|
752 |
|
56585
77ecc5e4f664
Silence a deprecation warning by using the appropriate replacement construct
Nick Coghlan <ncoghlan@gmail.com>
parents:
56565
diff
changeset
|
753 @contextlib.contextmanager |
|
64539
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
754 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
|
755 """Return a context manager that raises ResourceDenied when various issues |
|
64539
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
756 with the Internet connection manifest themselves as exceptions.""" |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
757 default_errnos = [ |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
758 ('ECONNREFUSED', 111), |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
759 ('ECONNRESET', 104), |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
760 ('ENETUNREACH', 101), |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
761 ('ETIMEDOUT', 110), |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
762 ] |
|
64542
d008f406dc98
Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64539
diff
changeset
|
763 default_gai_errnos = [ |
|
d008f406dc98
Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64539
diff
changeset
|
764 ('EAI_NONAME', -2), |
|
d008f406dc98
Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64539
diff
changeset
|
765 ('EAI_NODATA', -5), |
|
d008f406dc98
Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64539
diff
changeset
|
766 ] |
|
64539
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
767 |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
768 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
|
769 captured_errnos = errnos |
|
64542
d008f406dc98
Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64539
diff
changeset
|
770 gai_errnos = [] |
|
64539
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
771 if not captured_errnos: |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
772 captured_errnos = [getattr(errno, name, num) |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
773 for (name, num) in default_errnos] |
|
64542
d008f406dc98
Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64539
diff
changeset
|
774 gai_errnos = [getattr(socket, name, num) |
|
d008f406dc98
Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64539
diff
changeset
|
775 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
|
776 |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
777 def filter_error(err): |
|
64542
d008f406dc98
Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64539
diff
changeset
|
778 n = getattr(err, 'errno', None) |
|
64539
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
779 if (isinstance(err, socket.timeout) or |
|
64542
d008f406dc98
Merged revisions 84604 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
64539
diff
changeset
|
780 (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
|
781 n in captured_errnos): |
|
64539
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
782 if not verbose: |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
783 sys.stderr.write(denied.args[0] + "\n") |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
784 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
|
785 |
|
64539
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
786 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
|
787 try: |
|
64539
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
788 if timeout is not None: |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
789 socket.setdefaulttimeout(timeout) |
|
56585
77ecc5e4f664
Silence a deprecation warning by using the appropriate replacement construct
Nick Coghlan <ncoghlan@gmail.com>
parents:
56565
diff
changeset
|
790 yield |
|
64539
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
791 except IOError as err: |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
792 # 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
|
793 # unwrap to get at the original error. |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
794 while True: |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
795 a = err.args |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
796 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
|
797 err = a[0] |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
798 # 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
|
799 # except socket.error as msg: |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
800 # 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
|
801 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
|
802 err = a[1] |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
803 else: |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
804 break |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
805 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
|
806 raise |
|
64539
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
807 # 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
|
808 # __cause__ or __context__? |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
809 finally: |
|
af414b29d467
Merged revisions 84597-84599 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
63008
diff
changeset
|
810 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
|
811 |
|
91bd738a4206
Add test.test_support.transient_internet . Returns a context manager that
Brett Cannon <bcannon@gmail.com>
parents:
41081
diff
changeset
|
812 |
|
42783
f798c366270c
Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents:
42584
diff
changeset
|
813 @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
|
814 def captured_output(stream_name): |
|
7dff5e41fa7a
Generalize test.test_support.test_stdout() with a base context manager so that
Brett Cannon <bcannon@gmail.com>
parents:
45513
diff
changeset
|
815 """Run the 'with' statement body using a StringIO object in place of a |
|
7dff5e41fa7a
Generalize test.test_support.test_stdout() with a base context manager so that
Brett Cannon <bcannon@gmail.com>
parents:
45513
diff
changeset
|
816 specific attribute on the sys module. |
|
7dff5e41fa7a
Generalize test.test_support.test_stdout() with a base context manager so that
Brett Cannon <bcannon@gmail.com>
parents:
45513
diff
changeset
|
817 Example use (with 'stream_name=stdout'):: |
|
42783
f798c366270c
Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents:
42584
diff
changeset
|
818 |
|
f798c366270c
Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents:
42584
diff
changeset
|
819 with captured_stdout() as s: |
|
f798c366270c
Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents:
42584
diff
changeset
|
820 print "hello" |
|
f798c366270c
Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents:
42584
diff
changeset
|
821 assert s.getvalue() == "hello" |
|
f798c366270c
Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents:
42584
diff
changeset
|
822 """ |
|
f798c366270c
Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents:
42584
diff
changeset
|
823 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
|
824 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
|
825 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
|
826 try: |
|
3036e21ac661
make test_support's captured_output a bit more robust when exceptions happen
Benjamin Peterson <benjamin@python.org>
parents:
46425
diff
changeset
|
827 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
|
828 finally: |
|
3036e21ac661
make test_support's captured_output a bit more robust when exceptions happen
Benjamin Peterson <benjamin@python.org>
parents:
46425
diff
changeset
|
829 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
|
830 |
|
7dff5e41fa7a
Generalize test.test_support.test_stdout() with a base context manager so that
Brett Cannon <bcannon@gmail.com>
parents:
45513
diff
changeset
|
831 def captured_stdout(): |
|
7dff5e41fa7a
Generalize test.test_support.test_stdout() with a base context manager so that
Brett Cannon <bcannon@gmail.com>
parents:
45513
diff
changeset
|
832 return captured_output("stdout") |
|
42783
f798c366270c
Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents:
42584
diff
changeset
|
833 |
|
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
|
834 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
|
835 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
|
836 |
|
52350
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
837 def 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
|
838 """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
|
839 |
|
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
840 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
|
841 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
|
842 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
|
843 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
|
844 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
|
845 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
|
846 """ |
|
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
847 gc.collect() |
|
60482
0592e7841777
adjust gc_collect for Jython
Philip Jenvey <pjenvey@underboss.org>
parents:
59683
diff
changeset
|
848 if is_jython: |
|
0592e7841777
adjust gc_collect for Jython
Philip Jenvey <pjenvey@underboss.org>
parents:
59683
diff
changeset
|
849 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
|
850 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
|
851 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
|
852 |
|
42783
f798c366270c
Port test_frozen to unittest.
Georg Brandl <georg@python.org>
parents:
42584
diff
changeset
|
853 |
|
17109
7eb8b240dd6c
Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents:
17036
diff
changeset
|
854 #======================================================================= |
|
38055
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
855 # 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
|
856 # it afterwards. |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
857 |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
858 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
|
859 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
|
860 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
|
861 try: |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
862 import locale |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
863 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
|
864 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
|
865 except AttributeError: |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
866 # 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
|
867 raise |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
868 except: |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
869 # 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
|
870 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
|
871 else: |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
872 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
|
873 try: |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
874 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
|
875 break |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
876 except: |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
877 pass |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
878 |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
879 # 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
|
880 try: |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
881 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
|
882 finally: |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
883 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
|
884 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
|
885 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
|
886 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
|
887 return inner |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
888 return decorator |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
889 |
|
fcc61835c6ac
Bug #1473625: stop cPickle making float dumps locale dependent in protocol 0.
Georg Brandl <georg@python.org>
parents:
38015
diff
changeset
|
890 #======================================================================= |
|
37992
8dc2a4d8a22e
The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents:
36382
diff
changeset
|
891 # 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
|
892 |
|
8dc2a4d8a22e
The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents:
36382
diff
changeset
|
893 # 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
|
894 # 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
|
895 _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
|
896 _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
|
897 _2G = 2 * _1G |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
898 _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
|
899 |
|
47347
343aaaa802e8
remove some __getslice__
Benjamin Peterson <benjamin@python.org>
parents:
47316
diff
changeset
|
900 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
|
901 |
|
37992
8dc2a4d8a22e
The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents:
36382
diff
changeset
|
902 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
|
903 global max_memuse |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
904 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
|
905 sizes = { |
|
8dc2a4d8a22e
The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents:
36382
diff
changeset
|
906 '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
|
907 '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
|
908 '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
|
909 '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
|
910 } |
|
8dc2a4d8a22e
The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents:
36382
diff
changeset
|
911 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
|
912 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
|
913 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
|
914 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
|
915 memlimit = int(float(m.group(1)) * sizes[m.group(3).lower()]) |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
916 real_max_memuse = memlimit |
|
39417
74d23cd04421
Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents:
38961
diff
changeset
|
917 if memlimit > MAX_Py_ssize_t: |
|
74d23cd04421
Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents:
38961
diff
changeset
|
918 memlimit = MAX_Py_ssize_t |
|
74d23cd04421
Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents:
38961
diff
changeset
|
919 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
|
920 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
|
921 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
|
922 |
|
8dc2a4d8a22e
The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents:
36382
diff
changeset
|
923 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
|
924 """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
|
925 |
|
8dc2a4d8a22e
The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents:
36382
diff
changeset
|
926 '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
|
927 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
|
928 the test, or a good estimate of it. 'overhead' specifies fixed overhead, |
| 46425 | 929 independent of the testsize, and defaults to 5Mb. |
|
37993
358662e2f273
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
37992
diff
changeset
|
930 |
|
37992
8dc2a4d8a22e
The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents:
36382
diff
changeset
|
931 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
|
932 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
|
933 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
|
934 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
|
935 """ |
|
8dc2a4d8a22e
The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents:
36382
diff
changeset
|
936 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
|
937 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
|
938 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
|
939 # 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
|
940 # 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
|
941 # 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
|
942 # 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
|
943 maxsize = 5147 |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54717
diff
changeset
|
944 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
|
945 else: |
|
8dc2a4d8a22e
The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents:
36382
diff
changeset
|
946 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
|
947 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
|
948 # 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
|
949 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
|
950 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
|
951 "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
|
952 return |
|
8dc2a4d8a22e
The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents:
36382
diff
changeset
|
953 # 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
|
954 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
|
955 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
|
956 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
|
957 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
|
958 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
|
959 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
|
960 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
|
961 |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
962 def precisionbigmemtest(size, memuse, overhead=5*_1M): |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
963 def decorator(f): |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
964 def wrapper(self): |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
965 if not real_max_memuse: |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
966 maxsize = 5147 |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
967 else: |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
968 maxsize = size |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
969 |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
970 if real_max_memuse and real_max_memuse < maxsize * memuse: |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
971 if verbose: |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
972 sys.stderr.write("Skipping %s because of memory " |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
973 "constraint\n" % (f.__name__,)) |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
974 return |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
975 |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
976 return f(self, maxsize) |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
977 wrapper.size = size |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
978 wrapper.memuse = memuse |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
979 wrapper.overhead = overhead |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
980 return wrapper |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
981 return decorator |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48109
diff
changeset
|
982 |
|
39417
74d23cd04421
Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents:
38961
diff
changeset
|
983 def bigaddrspacetest(f): |
|
74d23cd04421
Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents:
38961
diff
changeset
|
984 """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
|
985 def wrapper(self): |
|
74d23cd04421
Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents:
38961
diff
changeset
|
986 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
|
987 if verbose: |
|
74d23cd04421
Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents:
38961
diff
changeset
|
988 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
|
989 "constraint\n" % (f.__name__,)) |
|
74d23cd04421
Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents:
38961
diff
changeset
|
990 else: |
|
74d23cd04421
Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents:
38961
diff
changeset
|
991 return f(self) |
|
74d23cd04421
Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents:
38961
diff
changeset
|
992 return wrapper |
|
74d23cd04421
Concatenation on a long string breaks (SF #1526585).
Armin Rigo <arigo@tunes.org>
parents:
38961
diff
changeset
|
993 |
|
37992
8dc2a4d8a22e
The result of SF patch #1471578: big-memory tests for strings, lists and
Thomas Wouters <thomas@python.org>
parents:
36382
diff
changeset
|
994 #======================================================================= |
|
41539
761ce3946b86
Import and raise statement cleanup.
Collin Winter <collinw@gmail.com>
parents:
41536
diff
changeset
|
995 # 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
|
996 |
|
17133
d0fd60c59aef
Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents:
17109
diff
changeset
|
997 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
|
998 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
|
999 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
|
1000 test(result) |
|
7eb8b240dd6c
Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents:
17036
diff
changeset
|
1001 return result |
|
7eb8b240dd6c
Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents:
17036
diff
changeset
|
1002 |
|
52350
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
1003 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
|
1004 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
|
1005 |
|
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
1006 def requires_resource(resource): |
|
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
1007 if resource_is_enabled(resource): |
|
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
1008 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
|
1009 else: |
|
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
1010 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
|
1011 |
|
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
1012 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
|
1013 """ |
|
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
1014 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
|
1015 """ |
|
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
1016 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
|
1017 |
|
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
1018 def impl_detail(msg=None, **guards): |
|
52359
f26c1cd6c985
** is required here
Benjamin Peterson <benjamin@python.org>
parents:
52356
diff
changeset
|
1019 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
|
1020 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
|
1021 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
|
1022 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
|
1023 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
|
1024 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
|
1025 else: |
|
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
1026 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
|
1027 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
|
1028 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
|
1029 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
|
1030 |
|
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
1031 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
|
1032 # 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
|
1033 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
|
1034 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
|
1035 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
|
1036 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
|
1037 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
|
1038 |
|
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
1039 # 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
|
1040 # 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
|
1041 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
|
1042 """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
|
1043 Examples: |
|
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
1044 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
|
1045 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
|
1046 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
|
1047 """ |
|
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
1048 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
|
1049 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
|
1050 |
|
4fe76249a909
add some useful utilities for skipping tests with unittest's new skipping ability
Benjamin Peterson <benjamin@python.org>
parents:
52334
diff
changeset
|
1051 |
|
17109
7eb8b240dd6c
Just import sys at the top instead of inside lots of functions.
Fred Drake <fdrake@acm.org>
parents:
17036
diff
changeset
|
1052 |
|
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
|
1053 def _run_suite(suite): |
|
19995
b07cb51d731c
run_suite(): Oops, update a docstring.
Barry Warsaw <barry@python.org>
parents:
19994
diff
changeset
|
1054 """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
|
1055 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
|
1056 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
|
1057 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
|
1058 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
|
1059 |
|
17133
d0fd60c59aef
Updated to latest PyUnit version (1.31 in PyUnit CVS); test_support.py
Steve Purcell <steve@pythonconsulting.com>
parents:
17109
diff
changeset
|
1060 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
|
1061 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
|
1062 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
|
1063 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
|
1064 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
|
1065 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
|
1066 else: |
|
56625
90c10dcb2913
Clarify error report message, and don't recommend running in verbose
R. David Murray <rdmurray@bitdance.com>
parents:
56585
diff
changeset
|
1067 err = "multiple errors occurred" |
|
90c10dcb2913
Clarify error report message, and don't recommend running in verbose
R. David Murray <rdmurray@bitdance.com>
parents:
56585
diff
changeset
|
1068 if not verbose: |
|
90c10dcb2913
Clarify error report message, and don't recommend running in verbose
R. David Murray <rdmurray@bitdance.com>
parents:
56585
diff
changeset
|
1069 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
|
1070 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
|
1071 |
|
19994
ab6c14822de1
run_suite(): Factor this out of run_unittest() for tests that build
Barry Warsaw <barry@python.org>
parents:
19819
diff
changeset
|
1072 |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28863
diff
changeset
|
1073 def run_unittest(*classes): |
|
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28863
diff
changeset
|
1074 """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
|
1075 valid_types = (unittest.TestSuite, unittest.TestCase) |
|
28863
6d852ebff36f
Factor out common boilerplate for test_support
Raymond Hettinger <python@rcn.com>
parents:
28799
diff
changeset
|
1076 suite = unittest.TestSuite() |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
28863
diff
changeset
|
1077 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
|
1078 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
|
1079 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
|
1080 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
|
1081 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
|
1082 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
|
1083 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
|
1084 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
|
1085 else: |
|
3f214b39b4df
run_unittest() to support TestCase instances as well as classes. Helps with doctests.
Raymond Hettinger <python@rcn.com>
parents:
29142
diff
changeset
|
1086 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
|
1087 _run_suite(suite) |
|
28863
6d852ebff36f
Factor out common boilerplate for test_support
Raymond Hettinger <python@rcn.com>
parents:
28799
diff
changeset
|
1088 |
|
19994
ab6c14822de1
run_suite(): Factor this out of run_unittest() for tests that build
Barry Warsaw <barry@python.org>
parents:
19819
diff
changeset
|
1089 |
|
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
|
1090 #======================================================================= |
|
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
|
1091 # 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
|
1092 |
|
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
|
1093 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
|
1094 """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
|
1095 |
|
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
|
1096 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
|
1097 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
|
1098 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
|
1099 """ |
|
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
|
1100 |
|
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
|
1101 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
|
1102 |
|
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
|
1103 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
|
1104 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
|
1105 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
|
1106 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
|
1107 |
|
20088
386fb26e0874
Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents:
20083
diff
changeset
|
1108 # 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
|
1109 # 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
|
1110 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
|
1111 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
|
1112 try: |
|
386fb26e0874
Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents:
20083
diff
changeset
|
1113 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
|
1114 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
|
1115 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
|
1116 finally: |
|
386fb26e0874
Get rid of the increasingly convoluted global tricks w/ sys.stdout, in
Tim Peters <tim.peters@gmail.com>
parents:
20083
diff
changeset
|
1117 sys.stdout = save_stdout |
|
29140
e57e22aa56a4
Provide a clue that the doctests have run.
Raymond Hettinger <python@rcn.com>
parents:
29066
diff
changeset
|
1118 if verbose: |
|
29142
5da198480b6a
Include module name in doctest summary.
Raymond Hettinger <python@rcn.com>
parents:
29140
diff
changeset
|
1119 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
|
1120 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
|
1121 |
|
30d2333852d5
Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents:
38767
diff
changeset
|
1122 #======================================================================= |
|
30d2333852d5
Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents:
38767
diff
changeset
|
1123 # 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
|
1124 |
|
56980
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56625
diff
changeset
|
1125 # 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
|
1126 # 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
|
1127 # 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
|
1128 # 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
|
1129 # 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
|
1130 # __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
|
1131 # at the end of a test run. |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56625
diff
changeset
|
1132 |
|
38867
30d2333852d5
Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents:
38767
diff
changeset
|
1133 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
|
1134 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
|
1135 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
|
1136 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
|
1137 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
|
1138 |
|
56980
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56625
diff
changeset
|
1139 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
|
1140 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
|
1141 return |
|
38867
30d2333852d5
Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents:
38767
diff
changeset
|
1142 |
|
30d2333852d5
Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents:
38767
diff
changeset
|
1143 _MAX_COUNT = 10 |
|
56980
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56625
diff
changeset
|
1144 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
|
1145 n = thread._count() |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56625
diff
changeset
|
1146 if n == nb_threads: |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56625
diff
changeset
|
1147 break |
|
38867
30d2333852d5
Prevent spurious leaks when running regrtest.py -R. There may be more
Neal Norwitz <nnorwitz@gmail.com>
parents:
38767
diff
changeset
|
1148 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
|
1149 # XXX print a warning in case of failure? |
|
38961
0a83dc3529d1
Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents:
38875
diff
changeset
|
1150 |
|
54085
e45186edac91
a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents:
53644
diff
changeset
|
1151 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
|
1152 """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
|
1153 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
|
1154 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
|
1155 """ |
|
6d9d8545861e
Issue #7449, part 1: fix test_support.py for Python compiled without thread
Victor Stinner <victor.stinner@haypocalc.com>
parents:
60482
diff
changeset
|
1156 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
|
1157 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
|
1158 |
|
54085
e45186edac91
a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents:
53644
diff
changeset
|
1159 @functools.wraps(func) |
|
e45186edac91
a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents:
53644
diff
changeset
|
1160 def decorator(*args): |
|
e45186edac91
a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents:
53644
diff
changeset
|
1161 key = threading_setup() |
|
e45186edac91
a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents:
53644
diff
changeset
|
1162 try: |
|
e45186edac91
a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents:
53644
diff
changeset
|
1163 return func(*args) |
|
e45186edac91
a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents:
53644
diff
changeset
|
1164 finally: |
|
e45186edac91
a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents:
53644
diff
changeset
|
1165 threading_cleanup(*key) |
|
e45186edac91
a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents:
53644
diff
changeset
|
1166 return decorator |
|
e45186edac91
a useful decorator for cleaning up threads
Benjamin Peterson <benjamin@python.org>
parents:
53644
diff
changeset
|
1167 |
|
38961
0a83dc3529d1
Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents:
38875
diff
changeset
|
1168 def reap_children(): |
|
0a83dc3529d1
Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents:
38875
diff
changeset
|
1169 """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
|
1170 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
|
1171 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
|
1172 for refleaks. |
|
0a83dc3529d1
Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents:
38875
diff
changeset
|
1173 """ |
|
0a83dc3529d1
Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents:
38875
diff
changeset
|
1174 |
|
0a83dc3529d1
Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents:
38875
diff
changeset
|
1175 # 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
|
1176 # 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
|
1177 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
|
1178 any_process = -1 |
|
0a83dc3529d1
Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents:
38875
diff
changeset
|
1179 while True: |
|
0a83dc3529d1
Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents:
38875
diff
changeset
|
1180 try: |
|
0a83dc3529d1
Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents:
38875
diff
changeset
|
1181 # 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
|
1182 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
|
1183 if pid == 0: |
|
0a83dc3529d1
Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents:
38875
diff
changeset
|
1184 break |
|
0a83dc3529d1
Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents:
38875
diff
changeset
|
1185 except: |
|
0a83dc3529d1
Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents:
38875
diff
changeset
|
1186 break |
|
54717
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
54466
diff
changeset
|
1187 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
54466
diff
changeset
|
1188 def py3k_bytes(b): |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
54466
diff
changeset
|
1189 """Emulate the py3k bytes() constructor. |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
54466
diff
changeset
|
1190 |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
54466
diff
changeset
|
1191 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
|
1192 """ |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
54466
diff
changeset
|
1193 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
54466
diff
changeset
|
1194 # memoryview? |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
54466
diff
changeset
|
1195 return b.tobytes() |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
54466
diff
changeset
|
1196 except AttributeError: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
54466
diff
changeset
|
1197 try: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
54466
diff
changeset
|
1198 # iterable of ints? |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
54466
diff
changeset
|
1199 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
|
1200 except TypeError: |
|
760a710eb6c1
Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
54466
diff
changeset
|
1201 return bytes(b) |
