From b74b3167ded445d70a06e31ad8939ac4def0a3ea Mon Sep 17 00:00:00 2001 From: Ondrej Sedlacek Date: Wed, 29 Jan 2025 14:19:31 +0100 Subject: [PATCH] pytrap: raise type error on incorrect type Previous behavior was to return Py_NotImplemented, which ended up as always `False` in python usage. --- pytrap/src/unirecipaddr.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pytrap/src/unirecipaddr.c b/pytrap/src/unirecipaddr.c index 5c7b08ee..35442154 100644 --- a/pytrap/src/unirecipaddr.c +++ b/pytrap/src/unirecipaddr.c @@ -480,7 +480,8 @@ UnirecIPAddrRange_isIn(pytrap_unirecipaddrrange *self, PyObject *args) PyObject *result = Py_False; if (!PyObject_IsInstance(args, (PyObject *) &pytrap_UnirecIPAddr)) { - result = Py_NotImplemented; + PyErr_Format(PyExc_TypeError, "UnirecIPAddr object expected, got '%s'.", Py_TYPE(args)->tp_name); + return NULL; } int cmp_result; @@ -520,7 +521,8 @@ UnirecIPAddrRange_isOverlap(pytrap_unirecipaddrrange *self, PyObject *args) return NULL; if (!PyObject_IsInstance((PyObject*)other, (PyObject *) &pytrap_UnirecIPAddrRange)) { - return Py_NotImplemented; + PyErr_Format(PyExc_TypeError, "UnirecIPAddrRange object expected, got '%s'.", Py_TYPE(other)->tp_name); + return NULL; } tmp = UnirecIPAddrRange_isIn(self, (PyObject *) other->start); @@ -537,6 +539,11 @@ UnirecIPAddrRange_isOverlap(pytrap_unirecipaddrrange *self, PyObject *args) static int UnirecIPAddrRange_contains(pytrap_unirecipaddrrange *o, pytrap_unirecipaddr *ip) { + if (!PyObject_IsInstance((PyObject *) ip, (PyObject *) &pytrap_UnirecIPAddr)) { + PyErr_Format(PyExc_TypeError, "UnirecIPAddr object expected, got '%s'.", Py_TYPE(ip)->tp_name); + return -1; + } + PyObject * tmp = UnirecIPAddrRange_isIn(o, (PyObject *) ip); int cmp_result = PyLong_AsLong(tmp); Py_DECREF(tmp);