Skip to content

Commit ae55d29

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

File tree

3 files changed

+12
-20
lines changed

3 files changed

+12
-20
lines changed

Lib/test/test_mmap.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,13 @@ def test_resize_past_pos(self):
734734
self.assertRaises(ValueError, m.write_byte, 42)
735735
self.assertRaises(ValueError, m.write, b'abc')
736736

737+
def test_concat_repeat_exception(self):
738+
m = mmap.mmap(-1, 16)
739+
with self.assertRaises(TypeError):
740+
m + m
741+
with self.assertRaises(TypeError):
742+
m * 2
743+
737744

738745
class LargeMmapTests(unittest.TestCase):
739746

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: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -840,24 +840,6 @@ mmap_subscript(mmap_object *self, PyObject *item)
840840
}
841841
}
842842

843-
static PyObject *
844-
mmap_concat(mmap_object *self, PyObject *bb)
845-
{
846-
CHECK_VALID(NULL);
847-
PyErr_SetString(PyExc_SystemError,
848-
"mmaps don't support concatenation");
849-
return NULL;
850-
}
851-
852-
static PyObject *
853-
mmap_repeat(mmap_object *self, Py_ssize_t n)
854-
{
855-
CHECK_VALID(NULL);
856-
PyErr_SetString(PyExc_SystemError,
857-
"mmaps don't support repeat operation");
858-
return NULL;
859-
}
860-
861843
static int
862844
mmap_ass_item(mmap_object *self, Py_ssize_t i, PyObject *v)
863845
{
@@ -977,8 +959,8 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
977959

978960
static PySequenceMethods mmap_as_sequence = {
979961
(lenfunc)mmap_length, /*sq_length*/
980-
(binaryfunc)mmap_concat, /*sq_concat*/
981-
(ssizeargfunc)mmap_repeat, /*sq_repeat*/
962+
0, /*sq_concat*/
963+
0, /*sq_repeat*/
982964
(ssizeargfunc)mmap_item, /*sq_item*/
983965
0, /*sq_slice*/
984966
(ssizeobjargproc)mmap_ass_item, /*sq_ass_item*/

0 commit comments

Comments
 (0)