Skip to content

Commit 3014d6e

Browse files
[2.7] bpo-33767: Fix improper use of SystemError by mmap.mmap objects (GH-7381) (GH-7432)
Raise TypeError instead of SystemError for unsupported operations. (cherry picked from commit e9e3976) Co-authored-by: Zackery Spytz <[email protected]>
1 parent bc3df70 commit 3014d6e

File tree

3 files changed

+13
-21
lines changed

3 files changed

+13
-21
lines changed

Lib/test/test_mmap.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,13 @@ def test_resize_past_pos(self):
666666
self.assertRaises(ValueError, m.write_byte, 'b')
667667
self.assertRaises(ValueError, m.write, 'abc')
668668

669+
def test_concat_repeat_exception(self):
670+
m = mmap.mmap(-1, 16)
671+
with self.assertRaises(TypeError):
672+
m + m
673+
with self.assertRaises(TypeError):
674+
m * 2
675+
669676

670677
class LargeMmapTests(unittest.TestCase):
671678

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The concatenation (``+``) and repetition (``*``) sequence operations now
2+
raise :exc:`TypeError` instead of :exc:`SystemError` when performed on
3+
:class:`mmap.mmap` objects. Patch by Zackery Spytz.

Modules/mmapmodule.c

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -818,24 +818,6 @@ mmap_subscript(mmap_object *self, PyObject *item)
818818
}
819819
}
820820

821-
static PyObject *
822-
mmap_concat(mmap_object *self, PyObject *bb)
823-
{
824-
CHECK_VALID(NULL);
825-
PyErr_SetString(PyExc_SystemError,
826-
"mmaps don't support concatenation");
827-
return NULL;
828-
}
829-
830-
static PyObject *
831-
mmap_repeat(mmap_object *self, Py_ssize_t n)
832-
{
833-
CHECK_VALID(NULL);
834-
PyErr_SetString(PyExc_SystemError,
835-
"mmaps don't support repeat operation");
836-
return NULL;
837-
}
838-
839821
static int
840822
mmap_ass_slice(mmap_object *self, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
841823
{
@@ -993,9 +975,9 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
993975

994976
static PySequenceMethods mmap_as_sequence = {
995977
(lenfunc)mmap_length, /*sq_length*/
996-
(binaryfunc)mmap_concat, /*sq_concat*/
997-
(ssizeargfunc)mmap_repeat, /*sq_repeat*/
998-
(ssizeargfunc)mmap_item, /*sq_item*/
978+
0, /*sq_concat*/
979+
0, /*sq_repeat*/
980+
(ssizeargfunc)mmap_item, /*sq_item*/
999981
(ssizessizeargfunc)mmap_slice, /*sq_slice*/
1000982
(ssizeobjargproc)mmap_ass_item, /*sq_ass_item*/
1001983
(ssizessizeobjargproc)mmap_ass_slice, /*sq_ass_slice*/

0 commit comments

Comments
 (0)