diff options
| author | Alexey Izbyshev <izbyshev@ispras.ru> | 2018-10-20 03:22:31 +0300 |
|---|---|---|
| committer | Victor Stinner <vstinner@redhat.com> | 2018-10-20 02:22:31 +0200 |
| commit | a2670565d8f5c502388378aba1fe73023fd8c8d4 (patch) | |
| tree | a9f3a5f8e2a123aaff4f27a94c33580f0216dccd /Lib/test/test_file.py | |
| parent | 4acf6c9d4be77b968fa498569d7a1545e5e77344 (diff) | |
| download | cpython-git-a2670565d8f5c502388378aba1fe73023fd8c8d4.tar.gz | |
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/test_file.py')
| -rw-r--r-- | Lib/test/test_file.py | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/Lib/test/test_file.py b/Lib/test/test_file.py index f58d1dae60..cd642e7aaf 100644 --- a/Lib/test/test_file.py +++ b/Lib/test/test_file.py @@ -169,22 +169,33 @@ class OtherFileTests: f.close() self.fail("no error for invalid mode: %s" % bad_mode) + def _checkBufferSize(self, s): + try: + f = self.open(TESTFN, 'wb', s) + f.write(str(s).encode("ascii")) + f.close() + f.close() + f = self.open(TESTFN, 'rb', s) + d = int(f.read().decode("ascii")) + f.close() + f.close() + except OSError as msg: + self.fail('error setting buffer size %d: %s' % (s, str(msg))) + self.assertEqual(d, s) + def testSetBufferSize(self): # make sure that explicitly setting the buffer size doesn't cause # misbehaviour especially with repeated close() calls - for s in (-1, 0, 1, 512): - try: - f = self.open(TESTFN, 'wb', s) - f.write(str(s).encode("ascii")) - f.close() - f.close() - f = self.open(TESTFN, 'rb', s) - d = int(f.read().decode("ascii")) - f.close() - f.close() - except OSError as msg: - self.fail('error setting buffer size %d: %s' % (s, str(msg))) - self.assertEqual(d, s) + for s in (-1, 0, 512): + with support.check_no_warnings(self, + message='line buffering', + category=RuntimeWarning): + self._checkBufferSize(s) + + # test that attempts to use line buffering in binary mode cause + # a warning + with self.assertWarnsRegex(RuntimeWarning, 'line buffering'): + self._checkBufferSize(1) def testTruncateOnWindows(self): # SF bug <http://www.python.org/sf/801631> |
