From b00da57561505eb220a107fab0e7fbc322f767ac Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Mon, 1 Feb 2016 21:19:22 -0800 Subject: Issue #26194: Inserting into a full deque to raise an IndexError --- Doc/library/collections.rst | 4 ++-- Lib/test/test_deque.py | 23 ++++++++++++----------- Misc/NEWS | 4 ++-- Modules/_collectionsmodule.c | 7 ++----- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index e89da35..092221f 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -477,8 +477,8 @@ or subtracting from an empty counter. Insert *x* into the deque at position *i*. - If the insertion causes a bounded deque to grow beyond *maxlen*, the - rightmost element is then removed to restore the size limit. + If the insertion would cause a bounded deque to grow beyond *maxlen*, + an :exc:`IndexError` is raised. .. versionadded:: 3.5 diff --git a/Lib/test/test_deque.py b/Lib/test/test_deque.py index d2a4633..2ca508d 100644 --- a/Lib/test/test_deque.py +++ b/Lib/test/test_deque.py @@ -304,19 +304,20 @@ class TestBasic(unittest.TestCase): s.insert(i, 'Z') self.assertEqual(list(d), s) - def test_index_bug_26194(self): + def test_insert_bug_26194(self): data = 'ABC' - for i in range(len(data) + 1): - d = deque(data, len(data)) - d.insert(i, None) - s = list(data) - s.insert(i, None) - s.pop() - self.assertEqual(list(d), s) - if i < len(data): - self.assertIsNone(d[i]) + d = deque(data, maxlen=len(data)) + with self.assertRaises(IndexError): + d.insert(2, None) + + elements = 'ABCDEFGHI' + for i in range(-len(elements), len(elements)): + d = deque(elements, maxlen=len(elements)+1) + d.insert(i, 'Z') + if i >= 0: + self.assertEqual(d[i], 'Z') else: - self.assertTrue(None not in d) + self.assertEqual(d[i-1], 'Z') def test_imul(self): for n in (-10, -1, 0, 1, 2, 10, 1000): diff --git a/Misc/NEWS b/Misc/NEWS index a95d2a0..ba1f4de 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -22,8 +22,8 @@ Core and Builtins compiler issues. - Issue #26194: Deque.insert() gave odd results for bounded deques that had - reached their maximum size. Now, the insert will happen normally and then - any overflowing element will be truncated from the right side. + reached their maximum size. Now an IndexError will be raised when attempting + to insert into a full deque. - Issue #25843: When compiling code, don't merge constants if they are equal but have a different types. For example, ``f1, f2 = lambda: 1, lambda: 1.0`` diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index b9c4f3b..10fbcfe 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -973,16 +973,13 @@ deque_insert(dequeobject *deque, PyObject *args) Py_ssize_t index; Py_ssize_t n = Py_SIZE(deque); PyObject *value; - PyObject *oldvalue; PyObject *rv; if (!PyArg_ParseTuple(args, "nO:insert", &index, &value)) return NULL; if (deque->maxlen == Py_SIZE(deque)) { - if (index >= deque->maxlen || Py_SIZE(deque) == 0) - Py_RETURN_NONE; - oldvalue = deque_pop(deque, NULL); - Py_DECREF(oldvalue); + PyErr_SetString(PyExc_IndexError, "deque already at its maximum size"); + return NULL; } if (index >= n) return deque_append(deque, value); -- cgit v0.12 From 567d513b9bf24ea5f58c4ca0fc3e25c887b85dcf Mon Sep 17 00:00:00 2001 From: Martin Panter Date: Wed, 3 Feb 2016 07:06:33 +0000 Subject: Issue #26244: Clarify default zlib compression level in documentation Based on patch by Aviv Palivoda. --- Doc/library/zlib.rst | 7 ++++--- Misc/ACKS | 1 + Modules/clinic/zlibmodule.c.h | 7 ++++--- Modules/zlibmodule.c | 7 ++++--- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Doc/library/zlib.rst b/Doc/library/zlib.rst index a815d1f..1869bb8 100644 --- a/Doc/library/zlib.rst +++ b/Doc/library/zlib.rst @@ -60,10 +60,11 @@ The available exception and functions in this module are: Returns a compression object, to be used for compressing data streams that won't fit into memory at once. - *level* is the compression level -- an integer from ``0`` to ``9``. A value - of ``1`` is fastest and produces the least compression, while a value of + *level* is the compression level -- an integer from ``0`` to ``9`` or ``-1``. + A value of ``1`` is fastest and produces the least compression, while a value of ``9`` is slowest and produces the most. ``0`` is no compression. The default - value is ``6``. + value is ``-1`` (Z_DEFAULT_COMPRESSION). Z_DEFAULT_COMPRESSION represents a default + compromise between speed and compression (currently equivalent to level 6). *method* is the compression algorithm. Currently, the only supported value is ``DEFLATED``. diff --git a/Misc/ACKS b/Misc/ACKS index 426d19f..b07244a 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1071,6 +1071,7 @@ Joonas Paalasmaa Martin Packman Shriphani Palakodety Julien Palard +Aviv Palivoda Ondrej Palkovsky Mike Pall Todd R. Palmer diff --git a/Modules/clinic/zlibmodule.c.h b/Modules/clinic/zlibmodule.c.h index c5cdf42..2d75bc9 100644 --- a/Modules/clinic/zlibmodule.c.h +++ b/Modules/clinic/zlibmodule.c.h @@ -89,8 +89,9 @@ PyDoc_STRVAR(zlib_compressobj__doc__, "Return a compressor object.\n" "\n" " level\n" -" The compression level (an integer in the range 0-9; default is 6).\n" -" Higher compression levels are slower, but produce smaller results.\n" +" The compression level (an integer in the range 0-9 or -1; default is\n" +" currently equivalent to 6). Higher compression levels are slower,\n" +" but produce smaller results.\n" " method\n" " The compression algorithm. If given, this must be DEFLATED.\n" " wbits\n" @@ -438,4 +439,4 @@ exit: #ifndef ZLIB_COMPRESS_COPY_METHODDEF #define ZLIB_COMPRESS_COPY_METHODDEF #endif /* !defined(ZLIB_COMPRESS_COPY_METHODDEF) */ -/*[clinic end generated code: output=7734aec079550bc8 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=cf81e1deae3af0ce input=a9049054013a1b77]*/ diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c index d5a6e53..11a34bb 100644 --- a/Modules/zlibmodule.c +++ b/Modules/zlibmodule.c @@ -390,8 +390,9 @@ zlib_decompress_impl(PyModuleDef *module, Py_buffer *data, int wbits, zlib.compressobj level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION - The compression level (an integer in the range 0-9; default is 6). - Higher compression levels are slower, but produce smaller results. + The compression level (an integer in the range 0-9 or -1; default is + currently equivalent to 6). Higher compression levels are slower, + but produce smaller results. method: int(c_default="DEFLATED") = DEFLATED The compression algorithm. If given, this must be DEFLATED. wbits: int(c_default="MAX_WBITS") = MAX_WBITS @@ -413,7 +414,7 @@ Return a compressor object. static PyObject * zlib_compressobj_impl(PyModuleDef *module, int level, int method, int wbits, int memLevel, int strategy, Py_buffer *zdict) -/*[clinic end generated code: output=2949bbb9a5723ccd input=b034847f8821f6af]*/ +/*[clinic end generated code: output=2949bbb9a5723ccd input=de2ffab6e910cd8b]*/ { compobject *self = NULL; int err; -- cgit v0.12