summaryrefslogtreecommitdiffstats
path: root/Lib/test/support
diff options
context:
space:
mode:
authorAlexey Izbyshev <izbyshev@ispras.ru>2018-10-20 00:22:31 (GMT)
committerVictor Stinner <vstinner@redhat.com>2018-10-20 00:22:31 (GMT)
commita2670565d8f5c502388378aba1fe73023fd8c8d4 (patch)
treea9f3a5f8e2a123aaff4f27a94c33580f0216dccd /Lib/test/support
parent4acf6c9d4be77b968fa498569d7a1545e5e77344 (diff)
downloadcpython-a2670565d8f5c502388378aba1fe73023fd8c8d4.zip
cpython-a2670565d8f5c502388378aba1fe73023fd8c8d4.tar.gz
cpython-a2670565d8f5c502388378aba1fe73023fd8c8d4.tar.bz2
bpo-32236: open() emits RuntimeWarning if buffering=1 for binary mode (GH-4842)
If buffering=1 is specified for open() in binary mode, it is silently treated as buffering=-1 (i.e., the default buffer size). Coupled with the fact that line buffering is always supported in Python 2, such behavior caused several issues (e.g., bpo-10344, bpo-21332). Warn that line buffering is not supported if open() is called with binary mode and buffering=1.
Diffstat (limited to 'Lib/test/support')
-rw-r--r--Lib/test/support/__init__.py32
1 files changed, 27 insertions, 5 deletions
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index ed0d46d..01e8935 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -107,7 +107,8 @@ __all__ = [
# threads
"threading_setup", "threading_cleanup", "reap_threads", "start_threads",
# miscellaneous
- "check_warnings", "check_no_resource_warning", "EnvironmentVarGuard",
+ "check_warnings", "check_no_resource_warning", "check_no_warnings",
+ "EnvironmentVarGuard",
"run_with_locale", "swap_item",
"swap_attr", "Matcher", "set_memlimit", "SuppressCrashReport", "sortdict",
"run_with_tz", "PGO", "missing_compiler_executable", "fd_count",
@@ -1253,6 +1254,30 @@ def check_warnings(*filters, **kwargs):
@contextlib.contextmanager
+def check_no_warnings(testcase, message='', category=Warning, force_gc=False):
+ """Context manager to check that no warnings are emitted.
+
+ This context manager enables a given warning within its scope
+ and checks that no warnings are emitted even with that warning
+ enabled.
+
+ If force_gc is True, a garbage collection is attempted before checking
+ for warnings. This may help to catch warnings emitted when objects
+ are deleted, such as ResourceWarning.
+
+ Other keyword arguments are passed to warnings.filterwarnings().
+ """
+ with warnings.catch_warnings(record=True) as warns:
+ warnings.filterwarnings('always',
+ message=message,
+ category=category)
+ yield
+ if force_gc:
+ gc_collect()
+ testcase.assertEqual(warns, [])
+
+
+@contextlib.contextmanager
def check_no_resource_warning(testcase):
"""Context manager to check that no ResourceWarning is emitted.
@@ -1266,11 +1291,8 @@ def check_no_resource_warning(testcase):
You must remove the object which may emit ResourceWarning before
the end of the context manager.
"""
- with warnings.catch_warnings(record=True) as warns:
- warnings.filterwarnings('always', category=ResourceWarning)
+ with check_no_warnings(testcase, category=ResourceWarning, force_gc=True):
yield
- gc_collect()
- testcase.assertEqual(warns, [])
class CleanImport(object):