From 3edf7b27f61d5cd3bd117773a5d99f9641a8dff0 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Sun, 3 Jun 2018 22:42:45 -0600 Subject: [PATCH 1/3] bpo-33767: Fix improper use of SystemError by mmap.mmap objects --- Lib/test/test_mmap.py | 8 ++++++++ .../next/Library/2018-06-03-22-41-59.bpo-33767.2e82g3.rst | 2 ++ Modules/mmapmodule.c | 4 ++-- 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-06-03-22-41-59.bpo-33767.2e82g3.rst diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index 80835c9519cd879..c3a1172faaef042 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -734,6 +734,14 @@ def test_resize_past_pos(self): self.assertRaises(ValueError, m.write_byte, 42) self.assertRaises(ValueError, m.write, b'abc') + def test_concat_repeat_exception(self): + # A SystemError was raised on two unsupported sequence operations. + 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 index 000000000000000..49997b2e2fe0094 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-06-03-22-41-59.bpo-33767.2e82g3.rst @@ -0,0 +1,2 @@ +Fix improper use of :exc:`SystemError` by :class:`mmap.mmap` objects. Patch +by Zackery Spytz. diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 9afb79fe2ce0528..68c4dcb97090548 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -816,7 +816,7 @@ static PyObject * mmap_concat(mmap_object *self, PyObject *bb) { CHECK_VALID(NULL); - PyErr_SetString(PyExc_SystemError, + PyErr_SetString(PyExc_TypeError, "mmaps don't support concatenation"); return NULL; } @@ -825,7 +825,7 @@ static PyObject * mmap_repeat(mmap_object *self, Py_ssize_t n) { CHECK_VALID(NULL); - PyErr_SetString(PyExc_SystemError, + PyErr_SetString(PyExc_TypeError, "mmaps don't support repeat operation"); return NULL; } From 61d090ca917025f4471321d3f934e35bcab4eb33 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Sun, 3 Jun 2018 23:30:19 -0600 Subject: [PATCH 2/3] Remove mmap_concat() and mmap_repeat() --- Modules/mmapmodule.c | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 68c4dcb97090548..27030db49b24b96 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -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_TypeError, - "mmaps don't support concatenation"); - return NULL; -} - -static PyObject * -mmap_repeat(mmap_object *self, Py_ssize_t n) -{ - CHECK_VALID(NULL); - PyErr_SetString(PyExc_TypeError, - "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*/ From e649f1ca86d69ed9e1ef4eba2e5bfc9455bb4493 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Mon, 4 Jun 2018 22:41:15 -0600 Subject: [PATCH 3/3] Delete comment, reword news entry --- Lib/test/test_mmap.py | 1 - .../next/Library/2018-06-03-22-41-59.bpo-33767.2e82g3.rst | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index c3a1172faaef042..355af8cd58935f8 100644 --- a/Lib/test/test_mmap.py +++ b/Lib/test/test_mmap.py @@ -735,7 +735,6 @@ def test_resize_past_pos(self): self.assertRaises(ValueError, m.write, b'abc') def test_concat_repeat_exception(self): - # A SystemError was raised on two unsupported sequence operations. m = mmap.mmap(-1, 16) with self.assertRaises(TypeError): m + m 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 index 49997b2e2fe0094..3483301890953a2 100644 --- 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 @@ -1,2 +1,3 @@ -Fix improper use of :exc:`SystemError` by :class:`mmap.mmap` objects. Patch -by Zackery Spytz. +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.