Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4)

Side by Side Diff: Lib/test/support.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/regrtest.py ('k') | Lib/test/test_ast.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Supporting definitions for the Python regression tests.""" 1 """Supporting definitions for the Python regression tests."""
2 2
3 if __name__ != 'test.support': 3 if __name__ != 'test.support':
4 raise ImportError('support must be imported from the test package') 4 raise ImportError('support must be imported from the test package')
5 5
6 import contextlib 6 import contextlib
7 import errno 7 import errno
8 import functools 8 import functools
9 import gc 9 import gc
10 import socket 10 import socket
11 import sys 11 import sys
12 import os 12 import os
13 import platform 13 import platform
14 import shutil 14 import shutil
15 import warnings 15 import warnings
16 import unittest 16 import unittest
17 import importlib 17 import importlib
18 import collections.abc 18 import collections.abc
19 import re 19 import re
20 import subprocess 20 import subprocess
21 import imp 21 import imp
22 import time 22 import time
23 import sysconfig 23 import sysconfig
24 import logging.handlers 24 import logging.handlers
25 25
26 try: 26 try:
27 import _thread, threading 27 import _thread
28 except ImportError: 28 except ImportError:
29 _thread = None 29 _thread = None
30 threading = None
31 try:
32 import multiprocessing.process
33 except ImportError:
34 multiprocessing = None
35
36 30
37 try: 31 try:
38 import zlib 32 import zlib
39 except ImportError: 33 except ImportError:
40 zlib = None 34 zlib = None
41 35
42 __all__ = [ 36 __all__ = [
43 "Error", "TestFailed", "ResourceDenied", "import_module", 37 "Error", "TestFailed", "ResourceDenied", "import_module",
44 "verbose", "use_resources", "max_memuse", "record_original_stdout", 38 "verbose", "use_resources", "max_memuse", "record_original_stdout",
45 "get_original_stdout", "unload", "unlink", "rmtree", "forget", 39 "get_original_stdout", "unload", "unlink", "rmtree", "forget",
46 "is_resource_enabled", "requires", "requires_linux_version", 40 "is_resource_enabled", "requires", "requires_linux_version",
47 "requires_mac_ver", "find_unused_port", "bind_port", 41 "requires_mac_ver", "find_unused_port", "bind_port",
48 "IPV6_ENABLED", "is_jython", "TESTFN", "HOST", "SAVEDCWD", "temp_cwd", 42 "IPV6_ENABLED", "is_jython", "TESTFN", "HOST", "SAVEDCWD", "temp_cwd",
49 "findfile", "create_empty_file", "sortdict", "check_syntax_error", "open_url resource", 43 "findfile", "sortdict", "check_syntax_error", "open_urlresource",
50 "check_warnings", "CleanImport", "EnvironmentVarGuard", "TransientResource", 44 "check_warnings", "CleanImport", "EnvironmentVarGuard", "TransientResource",
51 "captured_stdout", "captured_stdin", "captured_stderr", "time_out", 45 "captured_stdout", "captured_stdin", "captured_stderr", "time_out",
52 "socket_peer_reset", "ioerror_peer_reset", "run_with_locale", 'temp_umask', 46 "socket_peer_reset", "ioerror_peer_reset", "run_with_locale", 'temp_umask',
53 "transient_internet", "set_memlimit", "bigmemtest", "bigaddrspacetest", 47 "transient_internet", "set_memlimit", "bigmemtest", "bigaddrspacetest",
54 "BasicTestRunner", "run_unittest", "run_doctest", "threading_setup", 48 "BasicTestRunner", "run_unittest", "run_doctest", "threading_setup",
55 "threading_cleanup", "reap_children", "cpython_only", "check_impl_detail", 49 "threading_cleanup", "reap_children", "cpython_only", "check_impl_detail",
56 "get_attribute", "swap_item", "swap_attr", "requires_IEEE_754", 50 "get_attribute", "swap_item", "swap_attr", "requires_IEEE_754",
57 "TestHandler", "Matcher", "can_symlink", "skip_unless_symlink", 51 "TestHandler", "Matcher", "can_symlink", "skip_unless_symlink",
58 "import_fresh_module", "requires_zlib", "PIPE_MAX_SIZE", "failfast", 52 "import_fresh_module", "requires_zlib", "PIPE_MAX_SIZE"
59 ] 53 ]
60 54
61 class Error(Exception): 55 class Error(Exception):
62 """Base class for regression test exceptions.""" 56 """Base class for regression test exceptions."""
63 57
64 class TestFailed(Error): 58 class TestFailed(Error):
65 """Test failed.""" 59 """Test failed."""
66 60
67 class ResourceDenied(unittest.SkipTest): 61 class ResourceDenied(unittest.SkipTest):
68 """Test skipped because it requested a disallowed resource. 62 """Test skipped because it requested a disallowed resource.
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 del sys.modules[name_to_remove] 157 del sys.modules[name_to_remove]
164 return fresh_module 158 return fresh_module
165 159
166 160
167 def get_attribute(obj, name): 161 def get_attribute(obj, name):
168 """Get an attribute, raising SkipTest if AttributeError is raised.""" 162 """Get an attribute, raising SkipTest if AttributeError is raised."""
169 try: 163 try:
170 attribute = getattr(obj, name) 164 attribute = getattr(obj, name)
171 except AttributeError: 165 except AttributeError:
172 raise unittest.SkipTest("module %s has no attribute %s" % ( 166 raise unittest.SkipTest("module %s has no attribute %s" % (
173 repr(obj), name)) 167 obj.__name__, name))
174 else: 168 else:
175 return attribute 169 return attribute
176 170
177 verbose = 1 # Flag set to 0 by regrtest.py 171 verbose = 1 # Flag set to 0 by regrtest.py
178 use_resources = None # Flag set to [] by regrtest.py 172 use_resources = None # Flag set to [] by regrtest.py
179 max_memuse = 0 # Disable bigmem tests (they will still be run with 173 max_memuse = 0 # Disable bigmem tests (they will still be run with
180 # small sizes, to make sure they work.) 174 # small sizes, to make sure they work.)
181 real_max_memuse = 0 175 real_max_memuse = 0
182 failfast = False
183 176
184 # _original_stdout is meant to hold stdout at the time regrtest began. 177 # _original_stdout is meant to hold stdout at the time regrtest began.
185 # This may be "the real" stdout, or IDLE's emulation of stdout, or whatever. 178 # This may be "the real" stdout, or IDLE's emulation of stdout, or whatever.
186 # The point is to have some flavor of stdout the user can actually see. 179 # The point is to have some flavor of stdout the user can actually see.
187 _original_stdout = None 180 _original_stdout = None
188 def record_original_stdout(stdout): 181 def record_original_stdout(stdout):
189 global _original_stdout 182 global _original_stdout
190 _original_stdout = stdout 183 _original_stdout = stdout
191 184
192 def get_original_stdout(): 185 def get_original_stdout():
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 executing. 282 executing.
290 """ 283 """
291 if resource == 'gui' and not _is_gui_available(): 284 if resource == 'gui' and not _is_gui_available():
292 raise unittest.SkipTest("Cannot use the 'gui' resource") 285 raise unittest.SkipTest("Cannot use the 'gui' resource")
293 # see if the caller's module is __main__ - if so, treat as if 286 # see if the caller's module is __main__ - if so, treat as if
294 # the resource was set 287 # the resource was set
295 if sys._getframe(1).f_globals.get("__name__") == "__main__": 288 if sys._getframe(1).f_globals.get("__name__") == "__main__":
296 return 289 return
297 if not is_resource_enabled(resource): 290 if not is_resource_enabled(resource):
298 if msg is None: 291 if msg is None:
299 msg = "Use of the %r resource not enabled" % resource 292 msg = "Use of the `%s' resource not enabled" % resource
300 raise ResourceDenied(msg) 293 raise ResourceDenied(msg)
301 294
302 def requires_linux_version(*min_version): 295 def requires_linux_version(*min_version):
303 """Decorator raising SkipTest if the OS is Linux and the kernel version is 296 """Decorator raising SkipTest if the OS is Linux and the kernel version is
304 less than min_version. 297 less than min_version.
305 298
306 For example, @requires_linux_version(2, 6, 35) raises SkipTest if the Linux 299 For example, @requires_linux_version(2, 6, 35) raises SkipTest if the Linux
307 kernel version is less than 2.6.35. 300 kernel version is less than 2.6.35.
308 """ 301 """
309 def decorator(func): 302 def decorator(func):
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 warnings.warn('tests may fail, unable to change the CWD to ' + name, 564 warnings.warn('tests may fail, unable to change the CWD to ' + name,
572 RuntimeWarning, stacklevel=3) 565 RuntimeWarning, stacklevel=3)
573 try: 566 try:
574 yield os.getcwd() 567 yield os.getcwd()
575 finally: 568 finally:
576 os.chdir(saved_dir) 569 os.chdir(saved_dir)
577 if is_temporary: 570 if is_temporary:
578 rmtree(name) 571 rmtree(name)
579 572
580 573
581 if hasattr(os, "umask"): 574 @contextlib.contextmanager
582 @contextlib.contextmanager 575 def temp_umask(umask):
583 def temp_umask(umask): 576 """Context manager that temporarily sets the process umask."""
584 """Context manager that temporarily sets the process umask.""" 577 oldmask = os.umask(umask)
585 oldmask = os.umask(umask) 578 try:
586 try: 579 yield
587 yield 580 finally:
588 finally: 581 os.umask(oldmask)
589 os.umask(oldmask)
590 582
591 583
592 def findfile(file, here=__file__, subdir=None): 584 def findfile(file, here=__file__, subdir=None):
593 """Try to find a file on sys.path and the working directory. If it is not 585 """Try to find a file on sys.path and the working directory. If it is not
594 found the argument passed to the function is returned (this does not 586 found the argument passed to the function is returned (this does not
595 necessarily signal failure; could still be the legitimate path).""" 587 necessarily signal failure; could still be the legitimate path)."""
596 if os.path.isabs(file): 588 if os.path.isabs(file):
597 return file 589 return file
598 if subdir is not None: 590 if subdir is not None:
599 file = os.path.join(subdir, file) 591 file = os.path.join(subdir, file)
600 path = sys.path 592 path = sys.path
601 path = [os.path.dirname(here)] + path 593 path = [os.path.dirname(here)] + path
602 for dn in path: 594 for dn in path:
603 fn = os.path.join(dn, file) 595 fn = os.path.join(dn, file)
604 if os.path.exists(fn): return fn 596 if os.path.exists(fn): return fn
605 return file 597 return file
606
607 def create_empty_file(filename):
608 """Create an empty file. If the file already exists, truncate it."""
609 fd = os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_TRUNC)
610 os.close(fd)
611 598
612 def sortdict(dict): 599 def sortdict(dict):
613 "Like repr(dict), but in sorted order." 600 "Like repr(dict), but in sorted order."
614 items = sorted(dict.items()) 601 items = sorted(dict.items())
615 reprpairs = ["%r: %r" % pair for pair in items] 602 reprpairs = ["%r: %r" % pair for pair in items]
616 withcommas = ", ".join(reprpairs) 603 withcommas = ", ".join(reprpairs)
617 return "{%s}" % withcommas 604 return "{%s}" % withcommas
618 605
619 def make_bad_fd(): 606 def make_bad_fd():
620 """ 607 """
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 s = f.read() 653 s = f.read()
667 while s: 654 while s:
668 out.write(s) 655 out.write(s)
669 s = f.read() 656 s = f.read()
670 finally: 657 finally:
671 f.close() 658 f.close()
672 659
673 f = check_valid_file(fn) 660 f = check_valid_file(fn)
674 if f is not None: 661 if f is not None:
675 return f 662 return f
676 raise TestFailed('invalid resource %r' % fn) 663 raise TestFailed('invalid resource "%s"' % fn)
677 664
678 665
679 class WarningsRecorder(object): 666 class WarningsRecorder(object):
680 """Convenience wrapper for the warnings list returned on 667 """Convenience wrapper for the warnings list returned on
681 entry to the warnings.catch_warnings() context manager. 668 entry to the warnings.catch_warnings() context manager.
682 """ 669 """
683 def __init__(self, warnings_list): 670 def __init__(self, warnings_list):
684 self._warnings = warnings_list 671 self._warnings = warnings_list
685 self._last = 0 672 self._last = 0
686 673
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
910 """Return a context manager that raises ResourceDenied when various issues 897 """Return a context manager that raises ResourceDenied when various issues
911 with the Internet connection manifest themselves as exceptions.""" 898 with the Internet connection manifest themselves as exceptions."""
912 default_errnos = [ 899 default_errnos = [
913 ('ECONNREFUSED', 111), 900 ('ECONNREFUSED', 111),
914 ('ECONNRESET', 104), 901 ('ECONNRESET', 104),
915 ('EHOSTUNREACH', 113), 902 ('EHOSTUNREACH', 113),
916 ('ENETUNREACH', 101), 903 ('ENETUNREACH', 101),
917 ('ETIMEDOUT', 110), 904 ('ETIMEDOUT', 110),
918 ] 905 ]
919 default_gai_errnos = [ 906 default_gai_errnos = [
920 ('EAI_AGAIN', -3),
921 ('EAI_NONAME', -2), 907 ('EAI_NONAME', -2),
922 ('EAI_NODATA', -5), 908 ('EAI_NODATA', -5),
923 # Encountered when trying to resolve IPv6-only hostnames 909 # Encountered when trying to resolve IPv6-only hostnames
924 ('WSANO_DATA', 11004), 910 ('WSANO_DATA', 11004),
925 ] 911 ]
926 912
927 denied = ResourceDenied("Resource %r is not available" % resource_name) 913 denied = ResourceDenied("Resource '%s' is not available" % resource_name)
928 captured_errnos = errnos 914 captured_errnos = errnos
929 gai_errnos = [] 915 gai_errnos = []
930 if not captured_errnos: 916 if not captured_errnos:
931 captured_errnos = [getattr(errno, name, num) 917 captured_errnos = [getattr(errno, name, num)
932 for (name, num) in default_errnos] 918 for (name, num) in default_errnos]
933 gai_errnos = [getattr(socket, name, num) 919 gai_errnos = [getattr(socket, name, num)
934 for (name, num) in default_gai_errnos] 920 for (name, num) in default_gai_errnos]
935 921
936 def filter_error(err): 922 def filter_error(err):
937 n = getattr(err, 'errno', None) 923 n = getattr(err, 'errno', None)
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
1005 this can be the case in case of reference cycles.) This means that __del__ 991 this can be the case in case of reference cycles.) This means that __del__
1006 methods may be called later than expected and weakrefs may remain alive for 992 methods may be called later than expected and weakrefs may remain alive for
1007 longer than expected. This function tries its best to force all garbage 993 longer than expected. This function tries its best to force all garbage
1008 objects to disappear. 994 objects to disappear.
1009 """ 995 """
1010 gc.collect() 996 gc.collect()
1011 if is_jython: 997 if is_jython:
1012 time.sleep(0.1) 998 time.sleep(0.1)
1013 gc.collect() 999 gc.collect()
1014 gc.collect() 1000 gc.collect()
1015
1016 @contextlib.contextmanager
1017 def disable_gc():
1018 have_gc = gc.isenabled()
1019 gc.disable()
1020 try:
1021 yield
1022 finally:
1023 if have_gc:
1024 gc.enable()
1025 1001
1026 1002
1027 def python_is_optimized(): 1003 def python_is_optimized():
1028 """Find if Python was built with optimizations.""" 1004 """Find if Python was built with optimizations."""
1029 cflags = sysconfig.get_config_var('PY_CFLAGS') or '' 1005 cflags = sysconfig.get_config_var('PY_CFLAGS') or ''
1030 final_opt = "" 1006 final_opt = ""
1031 for opt in cflags.split(): 1007 for opt in cflags.split():
1032 if opt.startswith('-O'): 1008 if opt.startswith('-O'):
1033 final_opt = opt 1009 final_opt = opt
1034 return final_opt != '' and final_opt != '-O0' 1010 return final_opt and final_opt != '-O0'
1035 1011
1036 1012
1037 #======================================================================= 1013 #=======================================================================
1038 # Decorator for running a function in a different locale, correctly resetting 1014 # Decorator for running a function in a different locale, correctly resetting
1039 # it afterwards. 1015 # it afterwards.
1040 1016
1041 def run_with_locale(catstr, *locales): 1017 def run_with_locale(catstr, *locales):
1042 def decorator(func): 1018 def decorator(func):
1043 def inner(*args, **kwds): 1019 def inner(*args, **kwds):
1044 try: 1020 try:
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
1264 After that, any trace function is unset during the test to prevent 1240 After that, any trace function is unset during the test to prevent
1265 unexpected refcounts caused by the trace function. 1241 unexpected refcounts caused by the trace function.
1266 1242
1267 """ 1243 """
1268 return no_tracing(cpython_only(test)) 1244 return no_tracing(cpython_only(test))
1269 1245
1270 1246
1271 def _run_suite(suite): 1247 def _run_suite(suite):
1272 """Run tests from a unittest.TestSuite-derived class.""" 1248 """Run tests from a unittest.TestSuite-derived class."""
1273 if verbose: 1249 if verbose:
1274 runner = unittest.TextTestRunner(sys.stdout, verbosity=2, 1250 runner = unittest.TextTestRunner(sys.stdout, verbosity=2)
1275 failfast=failfast)
1276 else: 1251 else:
1277 runner = BasicTestRunner() 1252 runner = BasicTestRunner()
1278 1253
1279 result = runner.run(suite) 1254 result = runner.run(suite)
1280 if not result.wasSuccessful(): 1255 if not result.wasSuccessful():
1281 if len(result.errors) == 1 and not result.failures: 1256 if len(result.errors) == 1 and not result.failures:
1282 err = result.errors[0][1] 1257 err = result.errors[0][1]
1283 elif len(result.failures) == 1 and not result.errors: 1258 elif len(result.failures) == 1 and not result.errors:
1284 err = result.failures[0][1] 1259 err = result.failures[0][1]
1285 else: 1260 else:
(...skipping 30 matching lines...) Expand all
1316 usual behavior is used (it searches sys.argv for -v). 1291 usual behavior is used (it searches sys.argv for -v).
1317 """ 1292 """
1318 1293
1319 import doctest 1294 import doctest
1320 1295
1321 if verbosity is None: 1296 if verbosity is None:
1322 verbosity = verbose 1297 verbosity = verbose
1323 else: 1298 else:
1324 verbosity = None 1299 verbosity = None
1325 1300
1326 f, t = doctest.testmod(module, verbose=verbosity) 1301 # Direct doctest output (normally just errors) to real stdout; doctest
1327 if f: 1302 # output shouldn't be compared by regrtest.
1328 raise TestFailed("%d of %d doctests failed" % (f, t)) 1303 save_stdout = sys.stdout
1304 sys.stdout = get_original_stdout()
1305 try:
1306 f, t = doctest.testmod(module, verbose=verbosity)
1307 if f:
1308 raise TestFailed("%d of %d doctests failed" % (f, t))
1309 finally:
1310 sys.stdout = save_stdout
1329 if verbose: 1311 if verbose:
1330 print('doctest (%s) ... %d tests with zero failures' % 1312 print('doctest (%s) ... %d tests with zero failures' %
1331 (module.__name__, t)) 1313 (module.__name__, t))
1332 return f, t 1314 return f, t
1333 1315
1334 1316
1335 #======================================================================= 1317 #=======================================================================
1336 # Support for saving and restoring the imported modules. 1318 # Support for saving and restoring the imported modules.
1337 1319
1338 def modules_setup(): 1320 def modules_setup():
(...skipping 21 matching lines...) Expand all
1360 # NOTE: we use thread._count() rather than threading.enumerate() (or the 1342 # NOTE: we use thread._count() rather than threading.enumerate() (or the
1361 # moral equivalent thereof) because a threading.Thread object is still alive 1343 # moral equivalent thereof) because a threading.Thread object is still alive
1362 # until its __bootstrap() method has returned, even after it has been 1344 # until its __bootstrap() method has returned, even after it has been
1363 # unregistered from the threading module. 1345 # unregistered from the threading module.
1364 # thread._count(), on the other hand, only gets decremented *after* the 1346 # thread._count(), on the other hand, only gets decremented *after* the
1365 # __bootstrap() method has returned, which gives us reliable reference counts 1347 # __bootstrap() method has returned, which gives us reliable reference counts
1366 # at the end of a test run. 1348 # at the end of a test run.
1367 1349
1368 def threading_setup(): 1350 def threading_setup():
1369 if _thread: 1351 if _thread:
1370 return _thread._count(), threading._dangling.copy() 1352 return _thread._count(),
1371 else: 1353 else:
1372 return 1, () 1354 return 1,
1373 1355
1374 def threading_cleanup(*original_values): 1356 def threading_cleanup(nb_threads):
1375 if not _thread: 1357 if not _thread:
1376 return 1358 return
1377 _MAX_COUNT = 10 1359 _MAX_COUNT = 10
1378 for count in range(_MAX_COUNT): 1360 for count in range(_MAX_COUNT):
1379 values = _thread._count(), threading._dangling 1361 n = _thread._count()
1380 if values == original_values: 1362 if n == nb_threads:
1381 break 1363 break
1382 time.sleep(0.1) 1364 time.sleep(0.1)
1383 gc_collect()
1384 # XXX print a warning in case of failure? 1365 # XXX print a warning in case of failure?
1385 1366
1386 def reap_threads(func): 1367 def reap_threads(func):
1387 """Use this function when threads are being used. This will 1368 """Use this function when threads are being used. This will
1388 ensure that the threads are cleaned up even when the test fails. 1369 ensure that the threads are cleaned up even when the test fails.
1389 If threading is unavailable this function does nothing. 1370 If threading is unavailable this function does nothing.
1390 """ 1371 """
1391 if not _thread: 1372 if not _thread:
1392 return func 1373 return func
1393 1374
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
1618 def cleanup(): 1599 def cleanup():
1619 if attr_is_local: 1600 if attr_is_local:
1620 setattr(object_to_patch, attr_name, old_value) 1601 setattr(object_to_patch, attr_name, old_value)
1621 else: 1602 else:
1622 delattr(object_to_patch, attr_name) 1603 delattr(object_to_patch, attr_name)
1623 1604
1624 test_instance.addCleanup(cleanup) 1605 test_instance.addCleanup(cleanup)
1625 1606
1626 # actually override the attribute 1607 # actually override the attribute
1627 setattr(object_to_patch, attr_name, new_value) 1608 setattr(object_to_patch, attr_name, new_value)
OLDNEW
« no previous file with comments | « Lib/test/regrtest.py ('k') | Lib/test/test_ast.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+