From def0955485e2dd367cfe97d408c544cecd6e5c58 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 30 Apr 2021 15:24:30 +0200 Subject: [PATCH 1/2] Select now uses Py_TPFLAGS_DISALLOW_INSTANTIATION --- Modules/selectmodule.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c index f80da5895401fe..5038c325faa4eb 100644 --- a/Modules/selectmodule.c +++ b/Modules/selectmodule.c @@ -728,13 +728,6 @@ newPollObject(PyObject *module) return self; } -static PyObject * -poll_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) -{ - PyErr_Format(PyExc_TypeError, "Cannot create '%.200s' instances", _PyType_Name(type)); - return NULL; -} - static void poll_dealloc(pollObject *self) { @@ -2275,16 +2268,14 @@ static PyMethodDef poll_methods[] = { static PyType_Slot poll_Type_slots[] = { {Py_tp_dealloc, poll_dealloc}, {Py_tp_methods, poll_methods}, - {Py_tp_new, poll_new}, {0, 0}, }; static PyType_Spec poll_Type_spec = { - "select.poll", - sizeof(pollObject), - 0, - Py_TPFLAGS_DEFAULT, - poll_Type_slots + .name = "select.poll", + .basicsize = sizeof(pollObject), + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION, + .slots = poll_Type_slots, }; #ifdef HAVE_SYS_DEVPOLL_H From 0ea2f04d44db3b6159a5a7b99608052dd02ee1b7 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 30 Apr 2021 15:30:31 +0200 Subject: [PATCH 2/2] Add regression test --- Lib/test/test_select.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_select.py b/Lib/test/test_select.py index f63564e6b0ee6d..1ef56243549874 100644 --- a/Lib/test/test_select.py +++ b/Lib/test/test_select.py @@ -87,6 +87,10 @@ def fileno(self): a[:] = [F()] * 10 self.assertEqual(select.select([], a, []), ([], a[:5], [])) + def test_disallow_instantiation(self): + tp = type(select.poll()) + self.assertRaises(TypeError, tp) + def tearDownModule(): support.reap_children()