Skip to content

Commit b9052a0

Browse files
bpo-31893: Fixed select.kqueue(). (python#4166)
* Fixed the layout of the kqueue_event structure on OpenBSD and NetBSD. * Fixed the comparison of the kqueue_event objects.
1 parent baac01e commit b9052a0

File tree

2 files changed

+73
-47
lines changed

2 files changed

+73
-47
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed the layout of the kqueue_event structure on OpenBSD and NetBSD. Fixed
2+
the comparison of the kqueue_event objects.

Modules/selectmodule.c

Lines changed: 71 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,40 +1779,73 @@ static PyTypeObject kqueue_queue_Type;
17791779
#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG_LONG)
17801780
# define T_UINTPTRT T_ULONGLONG
17811781
# define T_INTPTRT T_LONGLONG
1782-
# define PyLong_AsUintptr_t PyLong_AsUnsignedLongLong
17831782
# define UINTPTRT_FMT_UNIT "K"
17841783
# define INTPTRT_FMT_UNIT "L"
17851784
#elif (SIZEOF_UINTPTR_T == SIZEOF_LONG)
17861785
# define T_UINTPTRT T_ULONG
17871786
# define T_INTPTRT T_LONG
1788-
# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
17891787
# define UINTPTRT_FMT_UNIT "k"
17901788
# define INTPTRT_FMT_UNIT "l"
17911789
#elif (SIZEOF_UINTPTR_T == SIZEOF_INT)
17921790
# define T_UINTPTRT T_UINT
17931791
# define T_INTPTRT T_INT
1794-
# define PyLong_AsUintptr_t PyLong_AsUnsignedLong
17951792
# define UINTPTRT_FMT_UNIT "I"
17961793
# define INTPTRT_FMT_UNIT "i"
17971794
#else
17981795
# error uintptr_t does not match int, long, or long long!
17991796
#endif
18001797

1798+
#if SIZEOF_LONG_LONG == 8
1799+
# define T_INT64 T_LONGLONG
1800+
# define INT64_FMT_UNIT "L"
1801+
#elif SIZEOF_LONG == 8
1802+
# define T_INT64 T_LONG
1803+
# define INT64_FMT_UNIT "l"
1804+
#elif SIZEOF_INT == 8
1805+
# define T_INT64 T_INT
1806+
# define INT64_FMT_UNIT "i"
1807+
#else
1808+
# define INT64_FMT_UNIT "_"
1809+
#endif
1810+
1811+
#if SIZEOF_LONG_LONG == 4
1812+
# define T_UINT32 T_ULONGLONG
1813+
# define UINT32_FMT_UNIT "K"
1814+
#elif SIZEOF_LONG == 4
1815+
# define T_UINT32 T_ULONG
1816+
# define UINT32_FMT_UNIT "k"
1817+
#elif SIZEOF_INT == 4
1818+
# define T_UINT32 T_UINT
1819+
# define UINT32_FMT_UNIT "I"
1820+
#else
1821+
# define UINT32_FMT_UNIT "_"
1822+
#endif
1823+
18011824
/*
18021825
* kevent is not standard and its members vary across BSDs.
18031826
*/
1804-
#if !defined(__OpenBSD__)
1805-
# define IDENT_TYPE T_UINTPTRT
1806-
# define IDENT_CAST intptr_t
1807-
# define DATA_TYPE T_INTPTRT
1808-
# define DATA_FMT_UNIT INTPTRT_FMT_UNIT
1809-
# define IDENT_AsType PyLong_AsUintptr_t
1827+
#ifdef __NetBSD__
1828+
# define FILTER_TYPE T_UINT32
1829+
# define FILTER_FMT_UNIT UINT32_FMT_UNIT
1830+
# define FLAGS_TYPE T_UINT32
1831+
# define FLAGS_FMT_UNIT UINT32_FMT_UNIT
1832+
# define FFLAGS_TYPE T_UINT32
1833+
# define FFLAGS_FMT_UNIT UINT32_FMT_UNIT
18101834
#else
1811-
# define IDENT_TYPE T_UINT
1812-
# define IDENT_CAST int
1813-
# define DATA_TYPE T_INT
1814-
# define DATA_FMT_UNIT "i"
1815-
# define IDENT_AsType PyLong_AsUnsignedLong
1835+
# define FILTER_TYPE T_SHORT
1836+
# define FILTER_FMT_UNIT "h"
1837+
# define FLAGS_TYPE T_USHORT
1838+
# define FLAGS_FMT_UNIT "H"
1839+
# define FFLAGS_TYPE T_UINT
1840+
# define FFLAGS_FMT_UNIT "I"
1841+
#endif
1842+
1843+
#ifdef __FreeBSD__
1844+
# define DATA_TYPE T_INTPTRT
1845+
# define DATA_FMT_UNIT INTPTR_FMT_UNIT
1846+
#else
1847+
# define DATA_TYPE T_INT64
1848+
# define DATA_FMT_UNIT INT64_FMT_UNIT
18161849
#endif
18171850

18181851
/* Unfortunately, we can't store python objects in udata, because
@@ -1822,9 +1855,9 @@ static PyTypeObject kqueue_queue_Type;
18221855

18231856
#define KQ_OFF(x) offsetof(kqueue_event_Object, x)
18241857
static struct PyMemberDef kqueue_event_members[] = {
1825-
{"ident", IDENT_TYPE, KQ_OFF(e.ident)},
1826-
{"filter", T_SHORT, KQ_OFF(e.filter)},
1827-
{"flags", T_USHORT, KQ_OFF(e.flags)},
1858+
{"ident", T_UINTPTRT, KQ_OFF(e.ident)},
1859+
{"filter", FILTER_TYPE, KQ_OFF(e.filter)},
1860+
{"flags", FLAGS_TYPE, KQ_OFF(e.flags)},
18281861
{"fflags", T_UINT, KQ_OFF(e.fflags)},
18291862
{"data", DATA_TYPE, KQ_OFF(e.data)},
18301863
{"udata", T_UINTPTRT, KQ_OFF(e.udata)},
@@ -1840,9 +1873,9 @@ kqueue_event_repr(kqueue_event_Object *s)
18401873
PyOS_snprintf(
18411874
buf, sizeof(buf),
18421875
"<select.kevent ident=%zu filter=%d flags=0x%x fflags=0x%x "
1843-
"data=0x%zd udata=%p>",
1844-
(size_t)(s->e.ident), s->e.filter, s->e.flags,
1845-
s->e.fflags, (Py_ssize_t)(s->e.data), s->e.udata);
1876+
"data=0x%llx udata=%p>",
1877+
(size_t)(s->e.ident), (int)s->e.filter, (unsigned int)s->e.flags,
1878+
(unsigned int)s->e.fflags, (long long)(s->e.data), (void *)s->e.udata);
18461879
return PyUnicode_FromString(buf);
18471880
}
18481881

@@ -1852,7 +1885,9 @@ kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds)
18521885
PyObject *pfd;
18531886
static char *kwlist[] = {"ident", "filter", "flags", "fflags",
18541887
"data", "udata", NULL};
1855-
static const char fmt[] = "O|hHI" DATA_FMT_UNIT UINTPTRT_FMT_UNIT ":kevent";
1888+
static const char fmt[] = "O|"
1889+
FILTER_FMT_UNIT FLAGS_FMT_UNIT FFLAGS_FMT_UNIT DATA_FMT_UNIT
1890+
UINTPTRT_FMT_UNIT ":kevent";
18561891

18571892
EV_SET(&(self->e), 0, EVFILT_READ, EV_ADD, 0, 0, 0); /* defaults */
18581893

@@ -1862,12 +1897,8 @@ kqueue_event_init(kqueue_event_Object *self, PyObject *args, PyObject *kwds)
18621897
return -1;
18631898
}
18641899

1865-
if (PyLong_Check(pfd)
1866-
#if IDENT_TYPE == T_UINT
1867-
&& PyLong_AsUnsignedLong(pfd) <= UINT_MAX
1868-
#endif
1869-
) {
1870-
self->e.ident = IDENT_AsType(pfd);
1900+
if (PyLong_Check(pfd)) {
1901+
self->e.ident = PyLong_AsSize_t(pfd);
18711902
}
18721903
else {
18731904
self->e.ident = PyObject_AsFileDescriptor(pfd);
@@ -1882,28 +1913,21 @@ static PyObject *
18821913
kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o,
18831914
int op)
18841915
{
1885-
intptr_t result = 0;
1916+
int result;
18861917

18871918
if (!kqueue_event_Check(o)) {
1888-
if (op == Py_EQ || op == Py_NE) {
1889-
PyObject *res = op == Py_EQ ? Py_False : Py_True;
1890-
Py_INCREF(res);
1891-
return res;
1892-
}
1893-
PyErr_Format(PyExc_TypeError,
1894-
"can't compare %.200s to %.200s",
1895-
Py_TYPE(s)->tp_name, Py_TYPE(o)->tp_name);
1896-
return NULL;
1897-
}
1898-
if (((result = (IDENT_CAST)(s->e.ident - o->e.ident)) == 0) &&
1899-
((result = s->e.filter - o->e.filter) == 0) &&
1900-
((result = s->e.flags - o->e.flags) == 0) &&
1901-
((result = (int)(s->e.fflags - o->e.fflags)) == 0) &&
1902-
((result = s->e.data - o->e.data) == 0) &&
1903-
((result = s->e.udata - o->e.udata) == 0)
1904-
) {
1905-
result = 0;
1906-
}
1919+
Py_RETURN_NOTIMPLEMENTED;
1920+
}
1921+
1922+
#define CMP(a, b) ((a) != (b)) ? ((a) < (b) ? -1 : 1)
1923+
result = CMP(s->e.ident, o->e.ident)
1924+
: CMP(s->e.filter, o->e.filter)
1925+
: CMP(s->e.flags, o->e.flags)
1926+
: CMP(s->e.fflags, o->e.fflags)
1927+
: CMP(s->e.data, o->e.data)
1928+
: CMP((intptr_t)s->e.udata, (intptr_t)o->e.udata)
1929+
: 0;
1930+
#undef CMP
19071931

19081932
switch (op) {
19091933
case Py_EQ:

0 commit comments

Comments
 (0)