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

Delta Between Two Patch Sets: Lib/test/support.py

Issue 7652: Merge C version of decimal into py3k.
Left Patch Set: Created 7 years, 7 months ago
Right Patch Set: Created 7 years, 4 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/decimal.py ('k') | Lib/test/test_decimal.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 fnmatch 24 import fnmatch
25 import logging.handlers 25 import logging.handlers
26 import struct
26 27
27 try: 28 try:
28 import _thread, threading 29 import _thread, threading
29 except ImportError: 30 except ImportError:
30 _thread = None 31 _thread = None
31 threading = None 32 threading = None
32 try: 33 try:
33 import multiprocessing.process 34 import multiprocessing.process
34 except ImportError: 35 except ImportError:
35 multiprocessing = None 36 multiprocessing = None
36 37
38 try:
39 import faulthandler
40 except ImportError:
41 faulthandler = None
37 42
38 try: 43 try:
39 import zlib 44 import zlib
40 except ImportError: 45 except ImportError:
41 zlib = None 46 zlib = None
42 47
48 try:
49 import fcntl
50 except ImportError:
51 fcntl = None
52
43 __all__ = [ 53 __all__ = [
44 "Error", "TestFailed", "ResourceDenied", "import_module", 54 "Error", "TestFailed", "ResourceDenied", "import_module",
45 "verbose", "use_resources", "max_memuse", "record_original_stdout", 55 "verbose", "use_resources", "max_memuse", "record_original_stdout",
46 "get_original_stdout", "unload", "unlink", "rmtree", "forget", 56 "get_original_stdout", "unload", "unlink", "rmtree", "forget",
47 "is_resource_enabled", "requires", "requires_linux_version", 57 "is_resource_enabled", "requires", "requires_freebsd_version",
48 "requires_mac_ver", "find_unused_port", "bind_port", 58 "requires_linux_version", "requires_mac_ver", "find_unused_port", "bind_port ",
49 "IPV6_ENABLED", "is_jython", "TESTFN", "HOST", "SAVEDCWD", "temp_cwd", 59 "IPV6_ENABLED", "is_jython", "TESTFN", "HOST", "SAVEDCWD", "temp_cwd",
50 "findfile", "create_empty_file", "sortdict", "check_syntax_error", "open_url resource", 60 "findfile", "create_empty_file", "sortdict", "check_syntax_error", "open_url resource",
51 "check_warnings", "CleanImport", "EnvironmentVarGuard", "TransientResource", 61 "check_warnings", "CleanImport", "EnvironmentVarGuard", "TransientResource",
52 "captured_stdout", "captured_stdin", "captured_stderr", "time_out", 62 "captured_stdout", "captured_stdin", "captured_stderr", "time_out",
53 "socket_peer_reset", "ioerror_peer_reset", "run_with_locale", 'temp_umask', 63 "socket_peer_reset", "ioerror_peer_reset", "run_with_locale", 'temp_umask',
54 "transient_internet", "set_memlimit", "bigmemtest", "bigaddrspacetest", 64 "transient_internet", "set_memlimit", "bigmemtest", "bigaddrspacetest",
55 "BasicTestRunner", "run_unittest", "run_doctest", "threading_setup", 65 "BasicTestRunner", "run_unittest", "run_doctest", "threading_setup",
56 "threading_cleanup", "reap_children", "cpython_only", "check_impl_detail", 66 "threading_cleanup", "reap_children", "cpython_only", "check_impl_detail",
57 "get_attribute", "swap_item", "swap_attr", "requires_IEEE_754", 67 "get_attribute", "swap_item", "swap_attr", "requires_IEEE_754",
58 "TestHandler", "Matcher", "can_symlink", "skip_unless_symlink", 68 "TestHandler", "Matcher", "can_symlink", "skip_unless_symlink",
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 for name_to_remove in names_to_remove: 185 for name_to_remove in names_to_remove:
176 del sys.modules[name_to_remove] 186 del sys.modules[name_to_remove]
177 return fresh_module 187 return fresh_module
178 188
179 189
180 def get_attribute(obj, name): 190 def get_attribute(obj, name):
181 """Get an attribute, raising SkipTest if AttributeError is raised.""" 191 """Get an attribute, raising SkipTest if AttributeError is raised."""
182 try: 192 try:
183 attribute = getattr(obj, name) 193 attribute = getattr(obj, name)
184 except AttributeError: 194 except AttributeError:
185 raise unittest.SkipTest("module %s has no attribute %s" % ( 195 raise unittest.SkipTest("object %r has no attribute %r" % (obj, name))
186 repr(obj), name))
187 else: 196 else:
188 return attribute 197 return attribute
189 198
190 verbose = 1 # Flag set to 0 by regrtest.py 199 verbose = 1 # Flag set to 0 by regrtest.py
191 use_resources = None # Flag set to [] by regrtest.py 200 use_resources = None # Flag set to [] by regrtest.py
192 max_memuse = 0 # Disable bigmem tests (they will still be run with 201 max_memuse = 0 # Disable bigmem tests (they will still be run with
193 # small sizes, to make sure they work.) 202 # small sizes, to make sure they work.)
194 real_max_memuse = 0 203 real_max_memuse = 0
195 failfast = False 204 failfast = False
196 match_tests = None 205 match_tests = None
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 raise unittest.SkipTest("Cannot use the 'gui' resource") 314 raise unittest.SkipTest("Cannot use the 'gui' resource")
306 # see if the caller's module is __main__ - if so, treat as if 315 # see if the caller's module is __main__ - if so, treat as if
307 # the resource was set 316 # the resource was set
308 if sys._getframe(1).f_globals.get("__name__") == "__main__": 317 if sys._getframe(1).f_globals.get("__name__") == "__main__":
309 return 318 return
310 if not is_resource_enabled(resource): 319 if not is_resource_enabled(resource):
311 if msg is None: 320 if msg is None:
312 msg = "Use of the %r resource not enabled" % resource 321 msg = "Use of the %r resource not enabled" % resource
313 raise ResourceDenied(msg) 322 raise ResourceDenied(msg)
314 323
315 def requires_linux_version(*min_version): 324 def _requires_unix_version(sysname, min_version):
316 """Decorator raising SkipTest if the OS is Linux and the kernel version is 325 """Decorator raising SkipTest if the OS is `sysname` and the version is less
317 less than min_version. 326 than `min_version`.
318 327
319 For example, @requires_linux_version(2, 6, 35) raises SkipTest if the Linux 328 For example, @_requires_unix_version('FreeBSD', (7, 2)) raises SkipTest if
320 kernel version is less than 2.6.35. 329 the FreeBSD version is less than 7.2.
321 """ 330 """
322 def decorator(func): 331 def decorator(func):
323 @functools.wraps(func) 332 @functools.wraps(func)
324 def wrapper(*args, **kw): 333 def wrapper(*args, **kw):
325 if sys.platform == 'linux': 334 if platform.system() == sysname:
326 version_txt = platform.release().split('-', 1)[0] 335 version_txt = platform.release().split('-', 1)[0]
327 try: 336 try:
328 version = tuple(map(int, version_txt.split('.'))) 337 version = tuple(map(int, version_txt.split('.')))
329 except ValueError: 338 except ValueError:
330 pass 339 pass
331 else: 340 else:
332 if version < min_version: 341 if version < min_version:
333 min_version_txt = '.'.join(map(str, min_version)) 342 min_version_txt = '.'.join(map(str, min_version))
334 raise unittest.SkipTest( 343 raise unittest.SkipTest(
335 "Linux kernel %s or higher required, not %s" 344 "%s version %s or higher required, not %s"
336 % (min_version_txt, version_txt)) 345 % (sysname, min_version_txt, version_txt))
337 return func(*args, **kw)
338 wrapper.min_version = min_version
339 return wrapper 346 return wrapper
340 return decorator 347 return decorator
348
349 def requires_freebsd_version(*min_version):
350 """Decorator raising SkipTest if the OS is FreeBSD and the FreeBSD version i s
351 less than `min_version`.
352
353 For example, @requires_freebsd_version(7, 2) raises SkipTest if the FreeBSD
354 version is less than 7.2.
355 """
356 return _requires_unix_version('FreeBSD', min_version)
357
358 def requires_linux_version(*min_version):
359 """Decorator raising SkipTest if the OS is Linux and the Linux version is
360 less than `min_version`.
361
362 For example, @requires_linux_version(2, 6, 32) raises SkipTest if the Linux
363 version is less than 2.6.32.
364 """
365 return _requires_unix_version('Linux', min_version)
341 366
342 def requires_mac_ver(*min_version): 367 def requires_mac_ver(*min_version):
343 """Decorator raising SkipTest if the OS is Mac OS X and the OS X 368 """Decorator raising SkipTest if the OS is Mac OS X and the OS X
344 version if less than min_version. 369 version if less than min_version.
345 370
346 For example, @requires_mac_ver(10, 5) raises SkipTest if the OS X version 371 For example, @requires_mac_ver(10, 5) raises SkipTest if the OS X version
347 is lesser than 10.5. 372 is lesser than 10.5.
348 """ 373 """
349 def decorator(func): 374 def decorator(func):
350 @functools.wraps(func) 375 @functools.wraps(func)
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after
924 with the Internet connection manifest themselves as exceptions.""" 949 with the Internet connection manifest themselves as exceptions."""
925 default_errnos = [ 950 default_errnos = [
926 ('ECONNREFUSED', 111), 951 ('ECONNREFUSED', 111),
927 ('ECONNRESET', 104), 952 ('ECONNRESET', 104),
928 ('EHOSTUNREACH', 113), 953 ('EHOSTUNREACH', 113),
929 ('ENETUNREACH', 101), 954 ('ENETUNREACH', 101),
930 ('ETIMEDOUT', 110), 955 ('ETIMEDOUT', 110),
931 ] 956 ]
932 default_gai_errnos = [ 957 default_gai_errnos = [
933 ('EAI_AGAIN', -3), 958 ('EAI_AGAIN', -3),
959 ('EAI_FAIL', -4),
934 ('EAI_NONAME', -2), 960 ('EAI_NONAME', -2),
935 ('EAI_NODATA', -5), 961 ('EAI_NODATA', -5),
936 # Encountered when trying to resolve IPv6-only hostnames 962 # Encountered when trying to resolve IPv6-only hostnames
937 ('WSANO_DATA', 11004), 963 ('WSANO_DATA', 11004),
938 ] 964 ]
939 965
940 denied = ResourceDenied("Resource %r is not available" % resource_name) 966 denied = ResourceDenied("Resource %r is not available" % resource_name)
941 captured_errnos = errnos 967 captured_errnos = errnos
942 gai_errnos = [] 968 gai_errnos = []
943 if not captured_errnos: 969 if not captured_errnos:
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
1110 if m is None: 1136 if m is None:
1111 raise ValueError('Invalid memory limit %r' % (limit,)) 1137 raise ValueError('Invalid memory limit %r' % (limit,))
1112 memlimit = int(float(m.group(1)) * sizes[m.group(3).lower()]) 1138 memlimit = int(float(m.group(1)) * sizes[m.group(3).lower()])
1113 real_max_memuse = memlimit 1139 real_max_memuse = memlimit
1114 if memlimit > MAX_Py_ssize_t: 1140 if memlimit > MAX_Py_ssize_t:
1115 memlimit = MAX_Py_ssize_t 1141 memlimit = MAX_Py_ssize_t
1116 if memlimit < _2G - 1: 1142 if memlimit < _2G - 1:
1117 raise ValueError('Memory limit %r too low to be useful' % (limit,)) 1143 raise ValueError('Memory limit %r too low to be useful' % (limit,))
1118 max_memuse = memlimit 1144 max_memuse = memlimit
1119 1145
1120 def bigmemtest(minsize, memuse): 1146 class _MemoryWatchdog:
1147 """An object which periodically watches the process' memory consumption
1148 and prints it out.
1149 """
1150
1151 def __init__(self):
1152 self.procfile = '/proc/{pid}/statm'.format(pid=os.getpid())
1153 self.started = False
1154 self.thread = None
1155 try:
1156 self.page_size = os.sysconf('SC_PAGESIZE')
1157 except (ValueError, AttributeError):
1158 try:
1159 self.page_size = os.sysconf('SC_PAGE_SIZE')
1160 except (ValueError, AttributeError):
1161 self.page_size = 4096
1162
1163 def consumer(self, fd):
1164 HEADER = "l"
1165 header_size = struct.calcsize(HEADER)
1166 try:
1167 while True:
1168 header = os.read(fd, header_size)
1169 if len(header) < header_size:
1170 # Pipe closed on other end
1171 break
1172 data_len, = struct.unpack(HEADER, header)
1173 data = os.read(fd, data_len)
1174 statm = data.decode('ascii')
1175 data = int(statm.split()[5])
1176 print(" ... process data size: {data:.1f}G"
1177 .format(data=data * self.page_size / (1024 ** 3)))
1178 finally:
1179 os.close(fd)
1180
1181 def start(self):
1182 if not faulthandler or not hasattr(faulthandler, '_file_watchdog'):
1183 return
1184 try:
1185 rfd = os.open(self.procfile, os.O_RDONLY)
1186 except OSError as e:
1187 warnings.warn('/proc not available for stats: {}'.format(e),
1188 RuntimeWarning)
1189 sys.stderr.flush()
1190 return
1191 pipe_fd, wfd = os.pipe()
1192 # set the write end of the pipe non-blocking to avoid blocking the
1193 # watchdog thread when the consumer doesn't drain the pipe fast enough
1194 if fcntl:
1195 flags = fcntl.fcntl(wfd, fcntl.F_GETFL)
1196 fcntl.fcntl(wfd, fcntl.F_SETFL, flags|os.O_NONBLOCK)
1197 # _file_watchdog() doesn't take the GIL in its child thread, and
1198 # therefore collects statistics timely
1199 faulthandler._file_watchdog(rfd, wfd, 1.0)
1200 self.started = True
1201 self.thread = threading.Thread(target=self.consumer, args=(pipe_fd,))
1202 self.thread.daemon = True
1203 self.thread.start()
1204
1205 def stop(self):
1206 if not self.started:
1207 return
1208 faulthandler._cancel_file_watchdog()
1209 self.thread.join()
1210
1211
1212 def bigmemtest(size, memuse, dry_run=True):
1121 """Decorator for bigmem tests. 1213 """Decorator for bigmem tests.
1122 1214
1123 'minsize' is the minimum useful size for the test (in arbitrary, 1215 'minsize' is the minimum useful size for the test (in arbitrary,
1124 test-interpreted units.) 'memuse' is the number of 'bytes per size' for 1216 test-interpreted units.) 'memuse' is the number of 'bytes per size' for
1125 the test, or a good estimate of it. 1217 the test, or a good estimate of it.
1126 1218
1127 The decorator tries to guess a good value for 'size' and passes it to 1219 if 'dry_run' is False, it means the test doesn't support dummy runs
1128 the decorated test function. If minsize * memuse is more than the 1220 when -M is not specified.
1129 allowed memory use (as defined by max_memuse), the test is skipped.
1130 Otherwise, minsize is adjusted upward to use up to max_memuse.
1131 """
1132 def decorator(f):
1133 def wrapper(self):
1134 # Retrieve values in case someone decided to adjust them
1135 minsize = wrapper.minsize
1136 memuse = wrapper.memuse
1137 if not max_memuse:
1138 # If max_memuse is 0 (the default),
1139 # we still want to run the tests with size set to a few kb,
1140 # to make sure they work. We still want to avoid using
1141 # too much memory, though, but we do that noisily.
1142 maxsize = 5147
1143 self.assertFalse(maxsize * memuse > 20 * _1M)
1144 else:
1145 maxsize = int(max_memuse / memuse)
1146 if maxsize < minsize:
1147 raise unittest.SkipTest(
1148 "not enough memory: %.1fG minimum needed"
1149 % (minsize * memuse / (1024 ** 3)))
1150 return f(self, maxsize)
1151 wrapper.minsize = minsize
1152 wrapper.memuse = memuse
1153 return wrapper
1154 return decorator
1155
1156 def precisionbigmemtest(size, memuse, dry_run=True):
1157 """Decorator for bigmem tests that need exact sizes.
1158
1159 Like bigmemtest, but without the size scaling upward to fill available
1160 memory.
1161 """ 1221 """
1162 def decorator(f): 1222 def decorator(f):
1163 def wrapper(self): 1223 def wrapper(self):
1164 size = wrapper.size 1224 size = wrapper.size
1165 memuse = wrapper.memuse 1225 memuse = wrapper.memuse
1166 if not real_max_memuse: 1226 if not real_max_memuse:
1167 maxsize = 5147 1227 maxsize = 5147
1168 else: 1228 else:
1169 maxsize = size 1229 maxsize = size
1170 1230
1171 if ((real_max_memuse or not dry_run) 1231 if ((real_max_memuse or not dry_run)
1172 and real_max_memuse < maxsize * memuse): 1232 and real_max_memuse < maxsize * memuse):
1173 raise unittest.SkipTest( 1233 raise unittest.SkipTest(
1174 "not enough memory: %.1fG minimum needed" 1234 "not enough memory: %.1fG minimum needed"
1175 % (size * memuse / (1024 ** 3))) 1235 % (size * memuse / (1024 ** 3)))
1176 1236
1177 return f(self, maxsize) 1237 if real_max_memuse and verbose and faulthandler and threading:
1238 print()
1239 print(" ... expected peak memory use: {peak:.1f}G"
1240 .format(peak=size * memuse / (1024 ** 3)))
1241 watchdog = _MemoryWatchdog()
1242 watchdog.start()
1243 else:
1244 watchdog = None
1245
1246 try:
1247 return f(self, maxsize)
1248 finally:
1249 if watchdog:
1250 watchdog.stop()
1251
1178 wrapper.size = size 1252 wrapper.size = size
1179 wrapper.memuse = memuse 1253 wrapper.memuse = memuse
1180 return wrapper 1254 return wrapper
1181 return decorator 1255 return decorator
1182 1256
1183 def bigaddrspacetest(f): 1257 def bigaddrspacetest(f):
1184 """Decorator for tests that fill the address space.""" 1258 """Decorator for tests that fill the address space."""
1185 def wrapper(self): 1259 def wrapper(self):
1186 if max_memuse < MAX_Py_ssize_t: 1260 if max_memuse < MAX_Py_ssize_t:
1187 if MAX_Py_ssize_t >= 2**63 - 1 and max_memuse >= 2**31: 1261 if MAX_Py_ssize_t >= 2**63 - 1 and max_memuse >= 2**31:
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
1517 """ 1591 """
1518 stderr = re.sub(br"\[\d+ refs\]\r?\n?$", b"", stderr).strip() 1592 stderr = re.sub(br"\[\d+ refs\]\r?\n?$", b"", stderr).strip()
1519 return stderr 1593 return stderr
1520 1594
1521 def args_from_interpreter_flags(): 1595 def args_from_interpreter_flags():
1522 """Return a list of command-line arguments reproducing the current 1596 """Return a list of command-line arguments reproducing the current
1523 settings in sys.flags and sys.warnoptions.""" 1597 settings in sys.flags and sys.warnoptions."""
1524 flag_opt_map = { 1598 flag_opt_map = {
1525 'bytes_warning': 'b', 1599 'bytes_warning': 'b',
1526 'dont_write_bytecode': 'B', 1600 'dont_write_bytecode': 'B',
1601 'hash_randomization': 'R',
1527 'ignore_environment': 'E', 1602 'ignore_environment': 'E',
1528 'no_user_site': 's', 1603 'no_user_site': 's',
1529 'no_site': 'S', 1604 'no_site': 'S',
1530 'optimize': 'O', 1605 'optimize': 'O',
1531 'verbose': 'v', 1606 'verbose': 'v',
1532 } 1607 }
1533 args = [] 1608 args = []
1534 for flag, opt in flag_opt_map.items(): 1609 for flag, opt in flag_opt_map.items():
1535 v = getattr(sys.flags, flag) 1610 v = getattr(sys.flags, flag)
1536 if v > 0: 1611 if v > 0:
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
1652 def cleanup(): 1727 def cleanup():
1653 if attr_is_local: 1728 if attr_is_local:
1654 setattr(object_to_patch, attr_name, old_value) 1729 setattr(object_to_patch, attr_name, old_value)
1655 else: 1730 else:
1656 delattr(object_to_patch, attr_name) 1731 delattr(object_to_patch, attr_name)
1657 1732
1658 test_instance.addCleanup(cleanup) 1733 test_instance.addCleanup(cleanup)
1659 1734
1660 # actually override the attribute 1735 # actually override the attribute
1661 setattr(object_to_patch, attr_name, new_value) 1736 setattr(object_to_patch, attr_name, new_value)
LEFTRIGHT

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