]> granicus.if.org Git - python/commitdiff
bpo-33767: Fix improper use of SystemError by mmap.mmap objects (GH-7381)
authorZackery Spytz <zspytz@gmail.com>
Tue, 5 Jun 2018 12:59:41 +0000 (06:59 -0600)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 5 Jun 2018 12:59:41 +0000 (15:59 +0300)
Raise TypeError instead of SystemError for unsupported operations.

Lib/test/test_mmap.py
Misc/NEWS.d/next/Library/2018-06-03-22-41-59.bpo-33767.2e82g3.rst [new file with mode: 0644]
Modules/mmapmodule.c

index 80835c9519cd879c0eb83144b3d8b23c4de8aa65..355af8cd58935f88478ef1621cd4289eaed6ba92 100644 (file)
@@ -734,6 +734,13 @@ class MmapTests(unittest.TestCase):
         self.assertRaises(ValueError, m.write_byte, 42)
         self.assertRaises(ValueError, m.write, b'abc')
 
+    def test_concat_repeat_exception(self):
+        m = mmap.mmap(-1, 16)
+        with self.assertRaises(TypeError):
+            m + m
+        with self.assertRaises(TypeError):
+            m * 2
+
 
 class LargeMmapTests(unittest.TestCase):
 
diff --git a/Misc/NEWS.d/next/Library/2018-06-03-22-41-59.bpo-33767.2e82g3.rst b/Misc/NEWS.d/next/Library/2018-06-03-22-41-59.bpo-33767.2e82g3.rst
new file mode 100644 (file)
index 0000000..3483301
--- /dev/null
@@ -0,0 +1,3 @@
+The concatenation (``+``) and repetition (``*``) sequence operations now
+raise :exc:`TypeError` instead of :exc:`SystemError` when performed on
+:class:`mmap.mmap` objects.  Patch by Zackery Spytz.
index 9afb79fe2ce05289da74a7711c89ecb8db68a7fc..27030db49b24b96a15cd08a85cdf52f17bda8d3e 100644 (file)
@@ -812,24 +812,6 @@ mmap_subscript(mmap_object *self, PyObject *item)
     }
 }
 
-static PyObject *
-mmap_concat(mmap_object *self, PyObject *bb)
-{
-    CHECK_VALID(NULL);
-    PyErr_SetString(PyExc_SystemError,
-                    "mmaps don't support concatenation");
-    return NULL;
-}
-
-static PyObject *
-mmap_repeat(mmap_object *self, Py_ssize_t n)
-{
-    CHECK_VALID(NULL);
-    PyErr_SetString(PyExc_SystemError,
-                    "mmaps don't support repeat operation");
-    return NULL;
-}
-
 static int
 mmap_ass_item(mmap_object *self, Py_ssize_t i, PyObject *v)
 {
@@ -949,8 +931,8 @@ mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
 
 static PySequenceMethods mmap_as_sequence = {
     (lenfunc)mmap_length,            /*sq_length*/
-    (binaryfunc)mmap_concat,         /*sq_concat*/
-    (ssizeargfunc)mmap_repeat,       /*sq_repeat*/
+    0,                               /*sq_concat*/
+    0,                               /*sq_repeat*/
     (ssizeargfunc)mmap_item,         /*sq_item*/
     0,                               /*sq_slice*/
     (ssizeobjargproc)mmap_ass_item,  /*sq_ass_item*/