Kaydet (Commit) 59406a9d authored tarafından Benjamin Peterson's avatar Benjamin Peterson

officially deprecated max_buffer_size

üst 1ef7c6bc
...@@ -5,6 +5,7 @@ Python implementation of the io module. ...@@ -5,6 +5,7 @@ Python implementation of the io module.
import os import os
import abc import abc
import codecs import codecs
import warnings
# Import _thread instead of threading to reduce startup cost # Import _thread instead of threading to reduce startup cost
try: try:
from _thread import allocate_lock as Lock from _thread import allocate_lock as Lock
...@@ -960,16 +961,20 @@ class BufferedWriter(_BufferedIOMixin): ...@@ -960,16 +961,20 @@ class BufferedWriter(_BufferedIOMixin):
The constructor creates a BufferedWriter for the given writeable raw The constructor creates a BufferedWriter for the given writeable raw
stream. If the buffer_size is not given, it defaults to stream. If the buffer_size is not given, it defaults to
DEFAULT_BUFFER_SIZE. If max_buffer_size is omitted, it defaults to DEFAULT_BUFFER_SIZE.
twice the buffer size.
""" """
_warning_stack_offset = 2
def __init__(self, raw, def __init__(self, raw,
buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None): buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None):
raw._checkWritable() raw._checkWritable()
_BufferedIOMixin.__init__(self, raw) _BufferedIOMixin.__init__(self, raw)
if buffer_size <= 0: if buffer_size <= 0:
raise ValueError("invalid buffer size") raise ValueError("invalid buffer size")
if max_buffer_size is not None:
warnings.warn("max_buffer_size is deprecated", DeprecationWarning,
self._warning_stack_offset)
self.buffer_size = buffer_size self.buffer_size = buffer_size
self._write_buf = bytearray() self._write_buf = bytearray()
self._write_lock = Lock() self._write_lock = Lock()
...@@ -1055,8 +1060,7 @@ class BufferedRWPair(BufferedIOBase): ...@@ -1055,8 +1060,7 @@ class BufferedRWPair(BufferedIOBase):
reader and writer are RawIOBase objects that are readable and reader and writer are RawIOBase objects that are readable and
writeable respectively. If the buffer_size is omitted it defaults to writeable respectively. If the buffer_size is omitted it defaults to
DEFAULT_BUFFER_SIZE. The max_buffer_size (for the buffered writer) DEFAULT_BUFFER_SIZE.
defaults to twice the buffer size.
""" """
# XXX The usefulness of this (compared to having two separate IO # XXX The usefulness of this (compared to having two separate IO
...@@ -1068,10 +1072,12 @@ class BufferedRWPair(BufferedIOBase): ...@@ -1068,10 +1072,12 @@ class BufferedRWPair(BufferedIOBase):
The arguments are two RawIO instances. The arguments are two RawIO instances.
""" """
if max_buffer_size is not None:
warnings.warn("max_buffer_size is deprecated", DeprecationWarning, 2)
reader._checkReadable() reader._checkReadable()
writer._checkWritable() writer._checkWritable()
self.reader = BufferedReader(reader, buffer_size) self.reader = BufferedReader(reader, buffer_size)
self.writer = BufferedWriter(writer, buffer_size, max_buffer_size) self.writer = BufferedWriter(writer, buffer_size)
def read(self, n=None): def read(self, n=None):
if n is None: if n is None:
...@@ -1117,10 +1123,11 @@ class BufferedRandom(BufferedWriter, BufferedReader): ...@@ -1117,10 +1123,11 @@ class BufferedRandom(BufferedWriter, BufferedReader):
The constructor creates a reader and writer for a seekable stream, The constructor creates a reader and writer for a seekable stream,
raw, given in the first argument. If the buffer_size is omitted it raw, given in the first argument. If the buffer_size is omitted it
defaults to DEFAULT_BUFFER_SIZE. The max_buffer_size (for the buffered defaults to DEFAULT_BUFFER_SIZE.
writer) defaults to twice the buffer size.
""" """
_warning_stack_offset = 3
def __init__(self, raw, def __init__(self, raw,
buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None): buffer_size=DEFAULT_BUFFER_SIZE, max_buffer_size=None):
raw._checkSeekable() raw._checkSeekable()
......
...@@ -26,6 +26,7 @@ import array ...@@ -26,6 +26,7 @@ import array
import threading import threading
import random import random
import unittest import unittest
import warnings
import weakref import weakref
import gc import gc
import abc import abc
...@@ -861,7 +862,7 @@ class BufferedWriterTest(unittest.TestCase, CommonBufferedTests): ...@@ -861,7 +862,7 @@ class BufferedWriterTest(unittest.TestCase, CommonBufferedTests):
def test_write_non_blocking(self): def test_write_non_blocking(self):
raw = self.MockNonBlockWriterIO() raw = self.MockNonBlockWriterIO()
bufio = self.tp(raw, 8, 8) bufio = self.tp(raw, 8)
self.assertEquals(bufio.write(b"abcd"), 4) self.assertEquals(bufio.write(b"abcd"), 4)
self.assertEquals(bufio.write(b"efghi"), 5) self.assertEquals(bufio.write(b"efghi"), 5)
...@@ -979,6 +980,17 @@ class BufferedWriterTest(unittest.TestCase, CommonBufferedTests): ...@@ -979,6 +980,17 @@ class BufferedWriterTest(unittest.TestCase, CommonBufferedTests):
self.assertRaises(IOError, bufio.tell) self.assertRaises(IOError, bufio.tell)
self.assertRaises(IOError, bufio.write, b"abcdef") self.assertRaises(IOError, bufio.write, b"abcdef")
def test_max_buffer_size_deprecation(self):
with support.check_warnings() as w:
warnings.simplefilter("always", DeprecationWarning)
self.tp(self.MockRawIO(), 8, 12)
self.assertEqual(len(w.warnings), 1)
warning = w.warnings[0]
self.assertTrue(warning.category is DeprecationWarning)
self.assertEqual(str(warning.message),
"max_buffer_size is deprecated")
class CBufferedWriterTest(BufferedWriterTest): class CBufferedWriterTest(BufferedWriterTest):
tp = io.BufferedWriter tp = io.BufferedWriter
...@@ -1029,6 +1041,16 @@ class BufferedRWPairTest(unittest.TestCase): ...@@ -1029,6 +1041,16 @@ class BufferedRWPairTest(unittest.TestCase):
pair = self.tp(r, w) pair = self.tp(r, w)
self.assertFalse(pair.closed) self.assertFalse(pair.closed)
def test_max_buffer_size_deprecation(self):
with support.check_warnings() as w:
warnings.simplefilter("always", DeprecationWarning)
self.tp(self.MockRawIO(), self.MockRawIO(), 8, 12)
self.assertEqual(len(w.warnings), 1)
warning = w.warnings[0]
self.assertTrue(warning.category is DeprecationWarning)
self.assertEqual(str(warning.message),
"max_buffer_size is deprecated")
# XXX More Tests # XXX More Tests
class CBufferedRWPairTest(BufferedRWPairTest): class CBufferedRWPairTest(BufferedRWPairTest):
...@@ -1048,7 +1070,7 @@ class BufferedRandomTest(BufferedReaderTest, BufferedWriterTest): ...@@ -1048,7 +1070,7 @@ class BufferedRandomTest(BufferedReaderTest, BufferedWriterTest):
def test_read_and_write(self): def test_read_and_write(self):
raw = self.MockRawIO((b"asdf", b"ghjk")) raw = self.MockRawIO((b"asdf", b"ghjk"))
rw = self.tp(raw, 8, 12) rw = self.tp(raw, 8)
self.assertEqual(b"as", rw.read(2)) self.assertEqual(b"as", rw.read(2))
rw.write(b"ddd") rw.write(b"ddd")
......
...@@ -41,6 +41,9 @@ Core and Builtins ...@@ -41,6 +41,9 @@ Core and Builtins
Library Library
------- -------
- The max_buffer_size arguments of io.BufferedWriter, io.BufferedRWPair, and
io.BufferedRandom have been deprecated for removal in Python 3.2.
- Issue #5068: Fixed the tarfile._BZ2Proxy.read() method that would loop - Issue #5068: Fixed the tarfile._BZ2Proxy.read() method that would loop
forever on incomplete input. That caused tarfile.open() to hang when used forever on incomplete input. That caused tarfile.open() to hang when used
with mode 'r' or 'r:bz2' and a fileobj argument that contained no data or with mode 'r' or 'r:bz2' and a fileobj argument that contained no data or
......
...@@ -1415,6 +1415,16 @@ PyTypeObject PyBufferedReader_Type = { ...@@ -1415,6 +1415,16 @@ PyTypeObject PyBufferedReader_Type = {
}; };
static int
complain_about_max_buffer_size(void)
{
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"max_buffer_size is deprecated", 1) < 0)
return 0;
return 1;
}
/* /*
* class BufferedWriter * class BufferedWriter
*/ */
...@@ -1439,7 +1449,7 @@ BufferedWriter_init(BufferedObject *self, PyObject *args, PyObject *kwds) ...@@ -1439,7 +1449,7 @@ BufferedWriter_init(BufferedObject *self, PyObject *args, PyObject *kwds)
/* TODO: properly deprecate max_buffer_size */ /* TODO: properly deprecate max_buffer_size */
char *kwlist[] = {"raw", "buffer_size", "max_buffer_size", NULL}; char *kwlist[] = {"raw", "buffer_size", "max_buffer_size", NULL};
Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE; Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
Py_ssize_t max_buffer_size = -1; Py_ssize_t max_buffer_size = -234;
PyObject *raw; PyObject *raw;
self->ok = 0; self->ok = 0;
...@@ -1449,6 +1459,9 @@ BufferedWriter_init(BufferedObject *self, PyObject *args, PyObject *kwds) ...@@ -1449,6 +1459,9 @@ BufferedWriter_init(BufferedObject *self, PyObject *args, PyObject *kwds)
return -1; return -1;
} }
if (max_buffer_size != -234 && !complain_about_max_buffer_size())
return -1;
if (_PyIOBase_checkWritable(raw, Py_True) == NULL) if (_PyIOBase_checkWritable(raw, Py_True) == NULL)
return -1; return -1;
...@@ -1767,8 +1780,7 @@ PyDoc_STRVAR(BufferedRWPair_doc, ...@@ -1767,8 +1780,7 @@ PyDoc_STRVAR(BufferedRWPair_doc,
"\n" "\n"
"reader and writer are RawIOBase objects that are readable and\n" "reader and writer are RawIOBase objects that are readable and\n"
"writeable respectively. If the buffer_size is omitted it defaults to\n" "writeable respectively. If the buffer_size is omitted it defaults to\n"
"DEFAULT_BUFFER_SIZE. The max_buffer_size (for the buffered writer)\n" "DEFAULT_BUFFER_SIZE.\n"
"defaults to twice the buffer size.\n"
); );
/* XXX The usefulness of this (compared to having two separate IO objects) is /* XXX The usefulness of this (compared to having two separate IO objects) is
...@@ -1789,13 +1801,16 @@ BufferedRWPair_init(BufferedRWPairObject *self, PyObject *args, ...@@ -1789,13 +1801,16 @@ BufferedRWPair_init(BufferedRWPairObject *self, PyObject *args,
{ {
PyObject *reader, *writer; PyObject *reader, *writer;
Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE; Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
Py_ssize_t max_buffer_size = -1; Py_ssize_t max_buffer_size = -234;
if (!PyArg_ParseTuple(args, "OO|nn:BufferedRWPair", &reader, &writer, if (!PyArg_ParseTuple(args, "OO|nn:BufferedRWPair", &reader, &writer,
&buffer_size, &max_buffer_size)) { &buffer_size, &max_buffer_size)) {
return -1; return -1;
} }
if (max_buffer_size != -234 && !complain_about_max_buffer_size())
return -1;
if (_PyIOBase_checkReadable(reader, Py_True) == NULL) if (_PyIOBase_checkReadable(reader, Py_True) == NULL)
return -1; return -1;
if (_PyIOBase_checkWritable(writer, Py_True) == NULL) if (_PyIOBase_checkWritable(writer, Py_True) == NULL)
...@@ -1812,7 +1827,7 @@ BufferedRWPair_init(BufferedRWPairObject *self, PyObject *args, ...@@ -1812,7 +1827,7 @@ BufferedRWPair_init(BufferedRWPairObject *self, PyObject *args,
if (self->reader == NULL) if (self->reader == NULL)
return -1; return -1;
args = Py_BuildValue("(nn)", buffer_size, max_buffer_size); args = Py_BuildValue("(n)", buffer_size);
if (args == NULL) { if (args == NULL) {
Py_CLEAR(self->reader); Py_CLEAR(self->reader);
return -1; return -1;
...@@ -2016,7 +2031,7 @@ BufferedRandom_init(BufferedObject *self, PyObject *args, PyObject *kwds) ...@@ -2016,7 +2031,7 @@ BufferedRandom_init(BufferedObject *self, PyObject *args, PyObject *kwds)
{ {
char *kwlist[] = {"raw", "buffer_size", "max_buffer_size", NULL}; char *kwlist[] = {"raw", "buffer_size", "max_buffer_size", NULL};
Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE; Py_ssize_t buffer_size = DEFAULT_BUFFER_SIZE;
Py_ssize_t max_buffer_size = -1; Py_ssize_t max_buffer_size = -234;
PyObject *raw; PyObject *raw;
self->ok = 0; self->ok = 0;
...@@ -2026,6 +2041,9 @@ BufferedRandom_init(BufferedObject *self, PyObject *args, PyObject *kwds) ...@@ -2026,6 +2041,9 @@ BufferedRandom_init(BufferedObject *self, PyObject *args, PyObject *kwds)
return -1; return -1;
} }
if (max_buffer_size != -234 && !complain_about_max_buffer_size())
return -1;
if (_PyIOBase_checkSeekable(raw, Py_True) == NULL) if (_PyIOBase_checkSeekable(raw, Py_True) == NULL)
return -1; return -1;
if (_PyIOBase_checkReadable(raw, Py_True) == NULL) if (_PyIOBase_checkReadable(raw, Py_True) == NULL)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment