From bf48d7c75970139a6d02b96dfd0bc9c4ce56e7fb Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 28 Apr 2025 20:16:47 +0300 Subject: [PATCH 1/3] gh-132987: Support __index__() in the select.kqueue_event constructor --- Modules/selectmodule.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c index d701026b50887c..69614efeb723c6 100644 --- a/Modules/selectmodule.c +++ b/Modules/selectmodule.c @@ -1922,14 +1922,27 @@ kqueue_event_init(PyObject *op, PyObject *args, PyObject *kwds) return -1; } - if (PyLong_Check(pfd)) { - self->e.ident = PyLong_AsSize_t(pfd); + if (PyIndex_Check(pfd)) { + Py_ssize_t bytes = PyLong_AsNativeBytes(pfd, + self->e.ident, sizeof(self->e.ident), + Py_ASNATIVEBYTES_NATIVE_ENDIAN | + Py_ASNATIVEBYTES_ALLOW_INDEX | + Py_ASNATIVEBYTES_REJECT_NEGATIVE | + Py_ASNATIVEBYTES_UNSIGNED_BUFFER); + if (bytes < 0) { + return -1; + } + if ((size_t)bytes > sizeof(self->e.ident)) { + PyErr_SetString(PyExc_OverflowError, + "Python int too large for C uintptr_t"); + return -1; + } } else { self->e.ident = PyObject_AsFileDescriptor(pfd); - } - if (PyErr_Occurred()) { - return -1; + if (PyErr_Occurred()) { + return -1; + } } return 0; } From a1b251d5707bddf78d528a6c3d138f3ae8c4b22f Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 28 Apr 2025 21:20:09 +0300 Subject: [PATCH 2/3] Fix typo. --- Modules/selectmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c index 69614efeb723c6..4809c268af6814 100644 --- a/Modules/selectmodule.c +++ b/Modules/selectmodule.c @@ -1924,7 +1924,7 @@ kqueue_event_init(PyObject *op, PyObject *args, PyObject *kwds) if (PyIndex_Check(pfd)) { Py_ssize_t bytes = PyLong_AsNativeBytes(pfd, - self->e.ident, sizeof(self->e.ident), + &self->e.ident, sizeof(self->e.ident), Py_ASNATIVEBYTES_NATIVE_ENDIAN | Py_ASNATIVEBYTES_ALLOW_INDEX | Py_ASNATIVEBYTES_REJECT_NEGATIVE | From 3d7470c8a23f623cc95b34c7d5c5d6abb86f4ead Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 29 Apr 2025 16:29:50 +0300 Subject: [PATCH 3/3] Update Modules/selectmodule.c Co-authored-by: Victor Stinner --- Modules/selectmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c index 4809c268af6814..d234d504cb5167 100644 --- a/Modules/selectmodule.c +++ b/Modules/selectmodule.c @@ -1934,7 +1934,7 @@ kqueue_event_init(PyObject *op, PyObject *args, PyObject *kwds) } if ((size_t)bytes > sizeof(self->e.ident)) { PyErr_SetString(PyExc_OverflowError, - "Python int too large for C uintptr_t"); + "Python int too large for C kqueue event identifier"); return -1; } }