Skip to content

Commit

Permalink
refactors and better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
itzpr3d4t0r committed Jun 22, 2023
1 parent d1ca70b commit b60a413
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
33 changes: 17 additions & 16 deletions src_c/circle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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); \
Expand All @@ -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)
Expand All @@ -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;
}

Expand Down
1 change: 0 additions & 1 deletion src_c/geometry.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ MODINIT_DEFINE(geometry)
return NULL;
}

/* create the module */
module = PyModule_Create(&_module);
if (!module) {
return NULL;
Expand Down
1 change: 0 additions & 1 deletion src_c/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit b60a413

Please sign in to comment.