From b60a41384ebc3637388cb2e835df66411e05f7aa Mon Sep 17 00:00:00 2001 From: itzpr3d4t0r <103119829+itzpr3d4t0r@users.noreply.github.com> Date: Fri, 23 Jun 2023 00:17:52 +0200 Subject: [PATCH] refactors and better error messages --- src_c/circle.c | 33 +++++++++++++++++---------------- src_c/geometry.c | 1 - src_c/geometry.h | 1 - 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src_c/circle.c b/src_c/circle.c index d061a570f4..f7d9b41be7 100644 --- a/src_c/circle.c +++ b/src_c/circle.c @@ -174,11 +174,12 @@ static int pg_circle_init(pgCircleObject *self, PyObject *args, PyObject *kwds) { if (!pgCircle_FromObject(args, &self->circle)) { - PyErr_SetString(PyExc_TypeError, - "Argument must be Circle style object"); + PyErr_SetString( + PyExc_TypeError, + "Arguments must be a Circle, a sequence of length 3 or 2, or an " + "object with an attribute called 'circle'"); return -1; } - return 0; } @@ -239,7 +240,7 @@ static struct PyMethodDef pg_circle_methods[] = { {"copy", (PyCFunction)pg_circle_copy, METH_NOARGS, NULL}, {NULL, NULL, 0, NULL}}; -#define GETSET_FOR_SIMPLE(name) \ +#define GETTER_SETTER(name) \ static PyObject *pg_circle_get##name(pgCircleObject *self, void *closure) \ { \ return PyFloat_FromDouble(self->circle.name); \ @@ -249,19 +250,19 @@ static struct PyMethodDef pg_circle_methods[] = { { \ double val; \ DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); \ - if (pg_DoubleFromObj(value, &val)) { \ - self->circle.name = val; \ - return 0; \ + if (!pg_DoubleFromObj(value, &val)) { \ + PyErr_Format(PyExc_TypeError, "Expected a number, got '%s'", \ + Py_TYPE(value)->tp_name); \ + return -1; \ } \ - PyErr_SetString(PyExc_TypeError, "Expected a number"); \ - return -1; \ + self->circle.name = val; \ + return 0; \ } -// they are repetitive enough that we can abstract them like this -GETSET_FOR_SIMPLE(x) -GETSET_FOR_SIMPLE(y) +GETTER_SETTER(x) +GETTER_SETTER(y) -#undef GETSET_FOR_SIMPLE +#undef GETTER_SETTER static PyObject * pg_circle_getr(pgCircleObject *self, void *closure) @@ -277,13 +278,13 @@ pg_circle_setr(pgCircleObject *self, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_DoubleFromObj(value, &radius)) { - PyErr_SetString(PyExc_TypeError, - "Invalid type for radius, must be numeric"); + PyErr_Format(PyExc_TypeError, "Expected a number, got '%s'", + Py_TYPE(value)->tp_name); return -1; } if (radius <= 0) { - PyErr_SetString(PyExc_ValueError, "Invalid radius value, must be > 0"); + PyErr_SetString(PyExc_ValueError, "Radius must be positive"); return -1; } diff --git a/src_c/geometry.c b/src_c/geometry.c index 861e574778..ff4ece8cdf 100644 --- a/src_c/geometry.c +++ b/src_c/geometry.c @@ -25,7 +25,6 @@ MODINIT_DEFINE(geometry) return NULL; } - /* create the module */ module = PyModule_Create(&_module); if (!module) { return NULL; diff --git a/src_c/geometry.h b/src_c/geometry.h index b43eafd2dd..34d7ea0c4a 100644 --- a/src_c/geometry.h +++ b/src_c/geometry.h @@ -22,7 +22,6 @@ typedef struct { static PyTypeObject pgCircle_Type; -// return 1 if success and 0 if failure static int pgCircle_FromObject(PyObject *obj, pgCircleBase *out);