Skip to content

Commit e9e3976

Browse files
ZackerySpytzserhiy-storchaka
authored andcommitted
bpo-33767: Fix improper use of SystemError by mmap.mmap objects (GH-7381)
Raise TypeError instead of SystemError for unsupported operations.
1 parent af1ec97 commit e9e3976

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
@@ -812,24 +812,6 @@ mmap_subscript(mmap_object *self, PyObject *item)
812812
}
813813
}
814814

815-
static PyObject *
816-
mmap_concat(mmap_object *self, PyObject *bb)
817-
{
818-
CHECK_VALID(NULL);
819-
PyErr_SetString(PyExc_SystemError,
820-
"mmaps don't support concatenation");
821-
return NULL;
822-
}
823-
824-
static PyObject *
825-
mmap_repeat(mmap_object *self, Py_ssize_t n)
826-
{
827-
CHECK_VALID(NULL);
828-
PyErr_SetString(PyExc_SystemError,
829-
"mmaps don't support repeat operation");
830-
return NULL;
831-
}
832-
833815
static int
834816
mmap_ass_item(mmap_object *self, Py_ssize_t i, PyObject *v)
835817
{
@@ -949,8 +931,8 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
949931

950932
static PySequenceMethods mmap_as_sequence = {
951933
(lenfunc)mmap_length, /*sq_length*/
952-
(binaryfunc)mmap_concat, /*sq_concat*/
953-
(ssizeargfunc)mmap_repeat, /*sq_repeat*/
934+
0, /*sq_concat*/
935+
0, /*sq_repeat*/
954936
(ssizeargfunc)mmap_item, /*sq_item*/
955937
0, /*sq_slice*/
956938
(ssizeobjargproc)mmap_ass_item, /*sq_ass_item*/

0 commit comments

Comments
 (0)