From 9aaf854bd0e9f208a6774b4c57c96debf4077153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josip=20Komljenovi=C4=87?= Date: Sat, 7 Jun 2025 23:02:25 +0200 Subject: [PATCH 1/8] Use macro for exception raising --- src_c/_camera.c | 10 +- src_c/_freetype.c | 124 +++++++++--------- src_c/_pygame.h | 3 +- src_c/_sdl2/controller.c | 3 +- src_c/base.c | 144 +++++++++------------ src_c/bufferproxy.c | 24 ++-- src_c/camera_windows.c | 38 +++--- src_c/circle.c | 88 ++++++------- src_c/color.c | 154 +++++++++-------------- src_c/display.c | 76 +++++------ src_c/draw.c | 8 +- src_c/event.c | 33 +++-- src_c/font.c | 40 +++--- src_c/freetype/ft_layout.c | 6 +- src_c/freetype/ft_render.c | 60 ++++----- src_c/freetype/ft_wrap.c | 57 +++------ src_c/freetype/ft_wrap.h | 11 +- src_c/imageext.c | 3 +- src_c/key.c | 5 +- src_c/line.c | 25 ++-- src_c/mask.c | 43 +++---- src_c/math.c | 223 ++++++++++++--------------------- src_c/mixer.c | 71 +++++------ src_c/newbuffer.c | 12 +- src_c/pixelarray.c | 94 ++++++-------- src_c/pixelarray_methods.c | 18 ++- src_c/pixelcopy.c | 45 +++---- src_c/rect.c | 60 ++++----- src_c/rect_impl.h | 145 ++++++++------------- src_c/render.c | 3 +- src_c/rwobject.c | 21 ++-- src_c/scrap.h | 9 +- src_c/scrap_sdl2.c | 7 +- src_c/scrap_win.c | 7 +- src_c/surface.c | 250 ++++++++++++++----------------------- src_c/surflock.c | 3 +- src_c/time.c | 3 +- src_c/transform.c | 38 ++---- src_c/window.c | 103 ++++++--------- 39 files changed, 808 insertions(+), 1259 deletions(-) diff --git a/src_c/_camera.c b/src_c/_camera.c index 5c1977ad39..a4a98f39b3 100644 --- a/src_c/_camera.c +++ b/src_c/_camera.c @@ -1890,9 +1890,8 @@ camera_init(pgCameraObject *self, PyObject *arg, PyObject *kwargs) p = windows_device_from_name(dev_name); if (!p) { - PyErr_SetString(PyExc_ValueError, - "Couldn't find a camera with that name"); - return -1; + RAISERETURN(PyExc_ValueError, "Couldn't find a camera with that name", + -1); } if (color) { @@ -1928,9 +1927,8 @@ camera_init(pgCameraObject *self, PyObject *arg, PyObject *kwargs) return 0; #else - PyErr_SetString(PyExc_RuntimeError, - "_camera backend not available on your platform"); - return -1; + RAISERETURN(PyExc_RuntimeError, + "_camera backend not available on your platform", -1); #endif } diff --git a/src_c/_freetype.c b/src_c/_freetype.c index 512693dfb6..8725708c9e 100644 --- a/src_c/_freetype.c +++ b/src_c/_freetype.c @@ -201,15 +201,13 @@ free_string(PGFT_String *); "." FONT_TYPE_NAME " instance is not initialized"); \ } -#define PGFT_CHECK_BOOL(_pyobj, _var) \ - if (_pyobj) { \ - if (!PyBool_Check(_pyobj)) { \ - PyErr_SetString(PyExc_TypeError, \ - #_var " must be a boolean value"); \ - return 0; \ - } \ - \ - _var = PyObject_IsTrue(_pyobj); \ +#define PGFT_CHECK_BOOL(_pyobj, _var) \ + if (_pyobj) { \ + if (!PyBool_Check(_pyobj)) { \ + RAISERETURN(PyExc_TypeError, #_var " must be a boolean value", \ + 0); \ + } \ + _var = PyObject_IsTrue(_pyobj); \ } #define DEFAULT_FONT_NAME "freesansbold.ttf" @@ -307,8 +305,7 @@ parse_dest(PyObject *dest, int *x, int *y) if (!pg_IntFromObj(oi, &i) || !pg_IntFromObj(oj, &j)) { Py_DECREF(oi); Py_DECREF(oj); - PyErr_SetString(PyExc_TypeError, "dest expects a pair of numbers"); - return -1; + RAISERETURN(PyExc_TypeError, "dest expects a pair of numbers", -1); } Py_DECREF(oi); Py_DECREF(oj); @@ -427,9 +424,8 @@ build_scale(PyObject *x, PyObject *y, Scale_t *size) } } if (sz_x == 0 && sz_y != 0) { - PyErr_SetString(PyExc_ValueError, - "expected zero size height when width is zero"); - return 0; + RAISERETURN(PyExc_ValueError, + "expected zero size height when width is zero", 0); } size->x = sz_x; size->y = sz_y; @@ -749,7 +745,7 @@ _ftfont_init(pgFontObject *self, PyObject *args, PyObject *kwds) file = load_font_res(DEFAULT_FONT_NAME); if (!file) { - PyErr_SetString(PyExc_RuntimeError, "Failed to find default font"); + RAISE(PyExc_RuntimeError, "Failed to find default font"); goto end; } } @@ -879,20 +875,20 @@ _ftfont_setstyle_flag(pgFontObject *self, PyObject *value, void *closure) const intptr_t style_flag = (intptr_t)closure; if (!PyBool_Check(value)) { - PyErr_SetString(PyExc_TypeError, "The style value must be a boolean"); - return -1; + RAISERETURN(PyExc_TypeError, "The style value must be a boolean", -1); } if ((style_flag & FT_STYLES_SCALABLE_ONLY) && !self->is_scalable) { if (pgFont_IS_ALIVE(self)) { - PyErr_SetString(PyExc_AttributeError, - "this style is unsupported for a bitmap font"); + RAISERETURN(PyExc_AttributeError, + "this style is unsupported for a bitmap font", -1); } else { - PyErr_SetString(PyExc_RuntimeError, MODULE_NAME - "." FONT_TYPE_NAME " instance is not initialized"); + RAISERETURN(PyExc_RuntimeError, + MODULE_NAME "." FONT_TYPE_NAME + " instance is not initialized", + -1); } - return -1; } if (PyObject_IsTrue(value)) { self->style |= (FT_UInt16)style_flag; @@ -925,10 +921,10 @@ _ftfont_setstyle(pgFontObject *self, PyObject *value, void *closure) FT_UInt32 style; if (!PyLong_Check(value)) { - PyErr_SetString(PyExc_TypeError, - "The style value must be an integer" - " from the FT constants module"); - return -1; + RAISERETURN( + PyExc_TypeError, + "The style value must be an integer from the FT constants module", + -1); } style = (FT_UInt32)PyLong_AsLong(value); @@ -945,14 +941,15 @@ _ftfont_setstyle(pgFontObject *self, PyObject *value, void *closure) } if ((style & FT_STYLES_SCALABLE_ONLY) && !self->is_scalable) { if (pgFont_IS_ALIVE(self)) { - PyErr_SetString(PyExc_AttributeError, - "this style is unsupported for a bitmap font"); + RAISERETURN(PyExc_AttributeError, + "this style is unsupported for a bitmap font", -1); } else { - PyErr_SetString(PyExc_RuntimeError, MODULE_NAME - "." FONT_TYPE_NAME " instance is not initialized"); + RAISERETURN(PyExc_RuntimeError, + MODULE_NAME "." FONT_TYPE_NAME + " instance is not initialized", + -1); } - return -1; } self->style = (FT_UInt16)style; @@ -1132,8 +1129,7 @@ _ftfont_getpath(pgFontObject *self, void *closure) PyObject *path = ((pgFontObject *)self)->path; if (!path) { - PyErr_SetString(PyExc_AttributeError, "path unavailable"); - return 0; + RAISERETURN(PyExc_AttributeError, "path unavailable", 0); } Py_INCREF(path); return path; @@ -1205,8 +1201,7 @@ _ftfont_setrender_flag(pgFontObject *self, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!PyBool_Check(value)) { - PyErr_SetString(PyExc_TypeError, "The style value must be a boolean"); - return -1; + RAISERETURN(PyExc_TypeError, "The style value must be a boolean", -1); } if (PyObject_IsTrue(value)) { @@ -1252,14 +1247,15 @@ _ftfont_setrotation(pgFontObject *self, PyObject *value, void *closure) if (!self->is_scalable) { if (pgFont_IS_ALIVE(self)) { - PyErr_SetString(PyExc_AttributeError, - "rotation is unsupported for a bitmap font"); + RAISERETURN(PyExc_AttributeError, + "rotation is unsupported for a bitmap font", -1); } else { - PyErr_SetString(PyExc_RuntimeError, MODULE_NAME - "." FONT_TYPE_NAME " instance is not initialized"); + RAISERETURN(PyExc_RuntimeError, + MODULE_NAME "." FONT_TYPE_NAME + " instance is not initialized", + -1); } - return -1; } return obj_to_rotation(value, &self->rotation) ? 0 : -1; } @@ -1418,8 +1414,7 @@ get_metrics(FontRenderMode *render, pgFontObject *font, PGFT_String *text) Py_ssize_t i; if (!_PGFT_GetFontSized(font->freetype, font, render->face_size)) { - PyErr_SetString(pgExc_SDLError, _PGFT_GetError(font->freetype)); - return 0; + RAISERETURN(pgExc_SDLError, _PGFT_GetError(font->freetype), 0); } list = PyList_New(length); if (!list) { @@ -1524,12 +1519,11 @@ _ftfont_getsizedascender(pgFontObject *self, PyObject *args) if (face_size.x == 0) { if (self->face_size.x == 0) { - PyErr_SetString(PyExc_ValueError, - "No font point size specified" - " and no default font size in typefont"); - return 0; + RAISERETURN(PyExc_ValueError, + "No font point size specified and no default font " + "size in typefont", + 0); } - face_size = self->face_size; } value = (long)_PGFT_Font_GetAscenderSized(self->freetype, self, face_size); @@ -1555,12 +1549,11 @@ _ftfont_getsizeddescender(pgFontObject *self, PyObject *args) if (face_size.x == 0) { if (self->face_size.x == 0) { - PyErr_SetString(PyExc_ValueError, - "No font point size specified" - " and no default font size in typefont"); - return 0; + RAISERETURN(PyExc_ValueError, + "No font point size specified and no default font " + "size in typefont", + 0); } - face_size = self->face_size; } value = @@ -1587,12 +1580,11 @@ _ftfont_getsizedheight(pgFontObject *self, PyObject *args) if (face_size.x == 0) { if (self->face_size.x == 0) { - PyErr_SetString(PyExc_ValueError, - "No font point size specified" - " and no default font size in typeface"); - return 0; + RAISERETURN(PyExc_ValueError, + "No font point size specified and no default font " + "size in typeface", + 0); } - face_size = self->face_size; } value = _PGFT_Font_GetHeightSized(self->freetype, self, face_size); @@ -1618,12 +1610,11 @@ _ftfont_getsizedglyphheight(pgFontObject *self, PyObject *args) if (face_size.x == 0) { if (self->face_size.x == 0) { - PyErr_SetString(PyExc_ValueError, - "No font point size specified" - " and no default font size in typeface"); - return 0; + RAISERETURN(PyExc_ValueError, + "No font point size specified and no default font " + "size in typeface", + 0); } - face_size = self->face_size; } value = @@ -2062,7 +2053,7 @@ _ftfont_render_to(pgFontObject *self, PyObject *args, PyObject *kwds) surface = surface_obj ? pgSurface_AsSurface(surface_obj) : NULL; if (!surface) { - PyErr_SetString(pgExc_SDLError, "display Surface quit"); + RAISE(pgExc_SDLError, "display Surface quit"); goto error; } if (_PGFT_Render_ExistingSurface( @@ -2226,12 +2217,9 @@ _ft_get_version(PyObject *self, PyObject *args, PyObject *kwargs) FT_Library lib; int err = FT_Init_FreeType(&lib); if (err) { - PyErr_SetString(PyExc_RuntimeError, - "FreeType could not be initialized"); - FT_Done_FreeType(lib); - - return NULL; + return RAISE(PyExc_RuntimeError, + "FreeType could not be initialized"); } FT_Int major, minor, patch; FT_Library_Version(lib, &major, &minor, &patch); diff --git a/src_c/_pygame.h b/src_c/_pygame.h index d2d747dc23..ea9c45205a 100644 --- a/src_c/_pygame.h +++ b/src_c/_pygame.h @@ -580,8 +580,7 @@ typedef enum { #define DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value) \ do { \ if (!value) { \ - PyErr_SetString(PyExc_AttributeError, "Cannot delete attribute"); \ - return -1; \ + RAISERETURN(PyExc_AttributeError, "Cannot delete attribute", -1); \ } \ } while (0) diff --git a/src_c/_sdl2/controller.c b/src_c/_sdl2/controller.c index 648af64a09..7f503e402f 100644 --- a/src_c/_sdl2/controller.c +++ b/src_c/_sdl2/controller.c @@ -485,8 +485,7 @@ controller_init(pgControllerObject *self, PyObject *args, PyObject *kwargs) if (self->controller == NULL) { controller = SDL_GameControllerOpen(self->id); if (controller == NULL) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } self->controller = controller; } diff --git a/src_c/base.c b/src_c/base.c index 654e73404a..ecff14b408 100644 --- a/src_c/base.c +++ b/src_c/base.c @@ -909,7 +909,7 @@ pgGetArrayStruct(PyObject *obj, PyObject **cobj_p, PyArrayInterface **inter_p) if (cobj == NULL) { if (PyErr_ExceptionMatches(PyExc_AttributeError)) { PyErr_Clear(); - PyErr_SetString(PyExc_ValueError, "no C-struct array interface"); + RAISERETURN(PyExc_ValueError, "no C-struct array interface", -1); } return -1; } @@ -920,8 +920,7 @@ pgGetArrayStruct(PyObject *obj, PyObject **cobj_p, PyArrayInterface **inter_p) if (inter == NULL || inter->two != 2 /* conditional or */) { Py_DECREF(cobj); - PyErr_SetString(PyExc_ValueError, "invalid array interface"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid array interface", -1); } *cobj_p = cobj; @@ -944,10 +943,9 @@ pgArrayStruct_AsDict(PyArrayInterface *inter_p) if (inter_p->flags & PAI_ARR_HAS_DESCR) { if (!inter_p->descr) { Py_DECREF(dictobj); - PyErr_SetString(PyExc_ValueError, - "Array struct has descr flag set" - " but no descriptor"); - return 0; + RAISERETURN(PyExc_ValueError, + "Array struct has descr flag set but no descriptor", + 0); } if (PyDict_SetItemString(dictobj, "descr", inter_p->descr)) { Py_DECREF(dictobj); @@ -1332,15 +1330,13 @@ pgObject_GetBuffer(PyObject *obj, pg_buffer *pg_view_p, int flags) break; default: pgBuffer_Release(pg_view_p); - PyErr_SetString(PyExc_ValueError, - "Unsupported array element type"); - return -1; + RAISERETURN(PyExc_ValueError, "Unsupported array element type", + -1); } if (*fchar_p != '\0') { pgBuffer_Release(pg_view_p); - PyErr_SetString(PyExc_ValueError, - "Arrays of records are unsupported"); - return -1; + RAISERETURN(PyExc_ValueError, "Arrays of records are unsupported", + -1); } success = 1; } @@ -1476,7 +1472,7 @@ pgGetArrayInterface(PyObject **dict, PyObject *obj) if (inter == NULL) { if (PyErr_ExceptionMatches(PyExc_AttributeError)) { PyErr_Clear(); - PyErr_SetString(PyExc_ValueError, "no array interface"); + RAISERETURN(PyExc_ValueError, "no array interface", -1) } return -1; } @@ -1517,29 +1513,25 @@ _pg_arraystruct_as_buffer(Py_buffer *view_p, PyObject *cobj, view_p->obj = 0; view_p->internal = 0; if (PyBUF_HAS_FLAG(flags, PyBUF_WRITABLE) && readonly) { - PyErr_SetString(pgExc_BufferError, - "require writable buffer, but it is read-only"); - return -1; + RAISERETURN(pgExc_BufferError, + "require writable buffer, but it is read-only", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_ANY_CONTIGUOUS)) { if (!(inter_p->flags & (PAI_CONTIGUOUS | PAI_FORTRAN))) { - PyErr_SetString(pgExc_BufferError, - "buffer data is not contiguous"); - return -1; + RAISERETURN(pgExc_BufferError, "buffer data is not contiguous", + -1); } } else if (PyBUF_HAS_FLAG(flags, PyBUF_C_CONTIGUOUS)) { if (!(inter_p->flags & PAI_CONTIGUOUS)) { - PyErr_SetString(pgExc_BufferError, - "buffer data is not C contiguous"); - return -1; + RAISERETURN(pgExc_BufferError, "buffer data is not C contiguous", + -1); } } else if (PyBUF_HAS_FLAG(flags, PyBUF_F_CONTIGUOUS)) { if (!(inter_p->flags & PAI_FORTRAN)) { - PyErr_SetString(pgExc_BufferError, - "buffer data is not F contiguous"); - return -1; + RAISERETURN(pgExc_BufferError, "buffer data is not F contiguous", + -1); } } internal_p = (pgViewInternals *)PyMem_Malloc(sz); @@ -1573,9 +1565,8 @@ _pg_arraystruct_as_buffer(Py_buffer *view_p, PyObject *cobj, view_p->shape = 0; } else { - PyErr_SetString(pgExc_BufferError, - "buffer data is not C contiguous, shape needed"); - return -1; + RAISERETURN(pgExc_BufferError, + "buffer data is not C contiguous, shape needed", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_STRIDES)) { view_p->strides = view_p->shape + inter_p->nd; @@ -1587,9 +1578,8 @@ _pg_arraystruct_as_buffer(Py_buffer *view_p, PyObject *cobj, view_p->strides = 0; } else { - PyErr_SetString(pgExc_BufferError, - "buffer is not contiguous, strides needed"); - return -1; + RAISERETURN(pgExc_BufferError, + "buffer is not contiguous, strides needed", -1); } view_p->suboffsets = 0; view_p->len = view_p->itemsize; @@ -1757,18 +1747,15 @@ static int _pg_shape_check(PyObject *op) { if (!op) { - PyErr_SetString(PyExc_ValueError, "required 'shape' item is missing"); - return -1; + RAISERETURN(PyExc_ValueError, "required 'shape' item is missing", -1); } if (!_pg_is_int_tuple(op)) { - PyErr_SetString(PyExc_ValueError, - "expected a tuple of ints for 'shape'"); - return -1; + RAISERETURN(PyExc_ValueError, "expected a tuple of ints for 'shape'", + -1); } if (PyTuple_GET_SIZE(op) == 0) { - PyErr_SetString(PyExc_ValueError, - "expected 'shape' to be at least one-dimensional"); - return -1; + RAISERETURN(PyExc_ValueError, + "expected 'shape' to be at least one-dimensional", -1); } return 0; } @@ -1777,28 +1764,23 @@ static int _pg_typestr_check(PyObject *op) { if (!op) { - PyErr_SetString(PyExc_ValueError, - "required 'typestr' item is missing"); - return -1; + RAISERETURN(PyExc_ValueError, "required 'typestr' item is missing", + -1); } if (PyUnicode_Check(op)) { - Py_ssize_t len = PyUnicode_GET_LENGTH(op); - if (len != 3) { - PyErr_SetString(PyExc_ValueError, - "expected 'typestr' to be length 3"); - return -1; + if (PyUnicode_GET_LENGTH(op) != 3) { + RAISERETURN(PyExc_ValueError, "expected 'typestr' to be length 3", + -1); } } else if (PyBytes_Check(op)) { if (PyBytes_GET_SIZE(op) != 3) { - PyErr_SetString(PyExc_ValueError, - "expected 'typestr' to be length 3"); - return -1; + RAISERETURN(PyExc_ValueError, "expected 'typestr' to be length 3", + -1); } } else { - PyErr_SetString(PyExc_ValueError, "expected a string for 'typestr'"); - return -1; + RAISERETURN(PyExc_ValueError, "expected a string for 'typestr'", -1); } return 0; } @@ -1809,23 +1791,19 @@ _pg_data_check(PyObject *op) PyObject *item; if (!op) { - PyErr_SetString(PyExc_ValueError, "required 'data' item is missing"); - return -1; + RAISERETURN(PyExc_ValueError, "required 'data' item is missing", -1); } if (!PyTuple_Check(op)) { - PyErr_SetString(PyExc_ValueError, "expected a tuple for 'data'"); - return -1; + RAISERETURN(PyExc_ValueError, "expected a tuple for 'data'", -1); } if (PyTuple_GET_SIZE(op) != 2) { - PyErr_SetString(PyExc_ValueError, - "expected a length 2 tuple for 'data'"); - return -1; + RAISERETURN(PyExc_ValueError, "expected a length 2 tuple for 'data'", + -1); } item = PyTuple_GET_ITEM(op, 0); if (!PyLong_Check(item)) { - PyErr_SetString(PyExc_ValueError, - "expected an int for item 0 of 'data'"); - return -1; + RAISERETURN(PyExc_ValueError, "expected an int for item 0 of 'data'", + -1); } return 0; } @@ -1834,9 +1812,8 @@ static int _pg_strides_check(PyObject *op) { if (op && !_pg_is_int_tuple(op) /* Conditional && */) { - PyErr_SetString(PyExc_ValueError, - "expected a tuple of ints for 'strides'"); - return -1; + RAISERETURN(PyExc_ValueError, "expected a tuple of ints for 'strides'", + -1); } return 0; } @@ -1873,9 +1850,8 @@ _pg_values_as_buffer(Py_buffer *view_p, int flags, PyObject *typestr, view_p->obj = 0; view_p->internal = 0; if (strides && PyTuple_GET_SIZE(strides) != ndim /* Cond. && */) { - PyErr_SetString(PyExc_ValueError, - "'shape' and 'strides' are not the same length"); - return -1; + RAISERETURN(PyExc_ValueError, + "'shape' and 'strides' are not the same length", -1); } view_p->ndim = (int)ndim; view_p->buf = PyLong_AsVoidPtr(PyTuple_GET_ITEM(data, 0)); @@ -1887,9 +1863,8 @@ _pg_values_as_buffer(Py_buffer *view_p, int flags, PyObject *typestr, return -1; } if (PyBUF_HAS_FLAG(flags, PyBUF_WRITABLE) && view_p->readonly) { - PyErr_SetString(pgExc_BufferError, - "require writable buffer, but it is read-only"); - return -1; + RAISERETURN(pgExc_BufferError, + "require writable buffer, but it is read-only", -1); } sz = sizeof(pgViewInternals) + (2 * ndim - 1) * sizeof(Py_ssize_t); internal_p = (pgViewInternals *)PyMem_Malloc(sz); @@ -1928,23 +1903,20 @@ _pg_values_as_buffer(Py_buffer *view_p, int flags, PyObject *typestr, } if (PyBUF_HAS_FLAG(flags, PyBUF_ANY_CONTIGUOUS)) { if (!PyBuffer_IsContiguous(view_p, 'A')) { - PyErr_SetString(pgExc_BufferError, - "buffer data is not contiguous"); - return -1; + RAISERETURN(pgExc_BufferError, "buffer data is not contiguous", + -1); } } else if (PyBUF_HAS_FLAG(flags, PyBUF_C_CONTIGUOUS)) { if (!PyBuffer_IsContiguous(view_p, 'C')) { - PyErr_SetString(pgExc_BufferError, - "buffer data is not C contiguous"); - return -1; + RAISERETURN(pgExc_BufferError, "buffer data is not C contiguous", + -1); } } else if (PyBUF_HAS_FLAG(flags, PyBUF_F_CONTIGUOUS)) { if (!PyBuffer_IsContiguous(view_p, 'F')) { - PyErr_SetString(pgExc_BufferError, - "buffer data is not F contiguous"); - return -1; + RAISERETURN(pgExc_BufferError, "buffer data is not F contiguous", + -1); } } if (!PyBUF_HAS_FLAG(flags, PyBUF_STRIDES)) { @@ -1952,9 +1924,8 @@ _pg_values_as_buffer(Py_buffer *view_p, int flags, PyObject *typestr, view_p->strides = 0; } else { - PyErr_SetString(pgExc_BufferError, - "buffer data is not C contiguous, strides needed"); - return -1; + RAISERETURN(pgExc_BufferError, + "buffer data is not C contiguous, strides needed", -1); } } if (!PyBUF_HAS_FLAG(flags, PyBUF_ND)) { @@ -1962,9 +1933,8 @@ _pg_values_as_buffer(Py_buffer *view_p, int flags, PyObject *typestr, view_p->shape = 0; } else { - PyErr_SetString(pgExc_BufferError, - "buffer data is not C contiguous, shape needed"); - return -1; + RAISERETURN(pgExc_BufferError, + "buffer data is not C contiguous, shape needed", -1); } } if (!PyBUF_HAS_FLAG(flags, PyBUF_FORMAT)) { diff --git a/src_c/bufferproxy.c b/src_c/bufferproxy.c index 7e06e9b939..a8ce9d2550 100644 --- a/src_c/bufferproxy.c +++ b/src_c/bufferproxy.c @@ -349,8 +349,7 @@ proxy_get_raw(pgBufferProxyObject *self, PyObject *closure) } if (!PyBuffer_IsContiguous(view_p, 'A')) { _proxy_release_view(self); - PyErr_SetString(PyExc_ValueError, "the bytes are not contiguous"); - return 0; + RAISERETURN(PyExc_ValueError, "the bytes are not contiguous", 0); } py_raw = PyBytes_FromStringAndSize((char *)view_p->buf, view_p->len); if (!py_raw) { @@ -414,22 +413,19 @@ proxy_write(pgBufferProxyObject *self, PyObject *args, PyObject *kwds) if (!PyBuffer_IsContiguous(&view, 'A')) { proxy_releasebuffer(self, &view); Py_DECREF(self); - PyErr_SetString(PyExc_ValueError, - "the BufferProxy bytes are not contiguous"); - return 0; + RAISERETURN(PyExc_ValueError, + "the BufferProxy bytes are not contiguous", 0); } if (buflen > view.len) { proxy_releasebuffer(self, &view); Py_DECREF(self); - PyErr_SetString(PyExc_ValueError, - "'buffer' object length is too large"); - return 0; + RAISERETURN(PyExc_ValueError, "'buffer' object length is too large", + 0); } if (offset < 0 || buflen + offset > view.len) { proxy_releasebuffer(self, &view); Py_DECREF(self); - PyErr_SetString(PyExc_IndexError, "'offset' is out of range"); - return 0; + RAISERETURN(PyExc_IndexError, "'offset' is out of range", 0); } memcpy((char *)view.buf + offset, buf, (size_t)buflen); proxy_releasebuffer(self, &view); @@ -534,10 +530,10 @@ pgBufferProxy_New(PyObject *obj, getbufferproc get_buffer) { if (!get_buffer) { if (!obj) { - PyErr_SetString(PyExc_ValueError, - "One of arguments 'obj' or 'get_buffer' is " - "required: both NULL instead"); - return 0; + RAISERETURN(PyExc_ValueError, + "One of arguments 'obj' or 'get_buffer' is required: " + "both NULL instead", + 0); } get_buffer = (getbufferproc)pgObject_GetBuffer; } diff --git a/src_c/camera_windows.c b/src_c/camera_windows.c index 4a44975741..5787e1bbb6 100644 --- a/src_c/camera_windows.c +++ b/src_c/camera_windows.c @@ -97,13 +97,13 @@ _check_integrity(pgCameraObject *self) if (FAILED(self->t_error)) { /* MF_E_HW_MFT_FAILED_START_STREAMING */ if (self->t_error == (HRESULT)-1072875772) { - PyErr_SetString(PyExc_SystemError, - "Camera already in use (Capture Thread Quit)"); + RAISERETURN(PyExc_SystemError, + "Camera already in use (Capture Thread Quit)", 0); } /* MF_E_VIDEO_RECORDING_DEVICE_INVALIDATED */ else if (self->t_error == (HRESULT)-1072873822) { - PyErr_SetString(PyExc_SystemError, - "Camera disconnected (Capture Thread Quit)"); + RAISERETURN(PyExc_SystemError, + "Camera disconnected (Capture Thread Quit)", 0); } else { T_FORMATHR(self->t_error, self->t_error_line) @@ -321,9 +321,8 @@ _select_source_type(pgCameraObject *self, IMFMediaType **mp) } if (!type_count) { - PyErr_SetString(pgExc_SDLError, - "Could not find any video types on this camera"); - return 0; + RAISERETURN(pgExc_SDLError, + "Could not find any video types on this camera", 0); } native_types = malloc(sizeof(IMFMediaType *) * type_count); @@ -382,9 +381,8 @@ _select_source_type(pgCameraObject *self, IMFMediaType **mp) } if (index == -1) { - PyErr_SetString(pgExc_SDLError, - "Could not find a valid video type in any supported" - " format"); + RAISE(pgExc_SDLError, + "Could not find a valid video type in any supported format"); hr = HR_UPSTREAM_FAILURE; goto cleanup; } @@ -803,9 +801,8 @@ windows_read_frame(pgCameraObject *self, SDL_Surface *surf) HRESULT hr; if (!self->open) { - PyErr_SetString(pgExc_SDLError, - "Camera needs to be started to read data"); - return 0; + RAISERETURN(pgExc_SDLError, "Camera needs to be started to read data", + 0); } if (!_check_integrity(self)) { @@ -839,9 +836,8 @@ windows_frame_ready(pgCameraObject *self, int *result) *result = self->buffer_ready; if (!self->open) { - PyErr_SetString(pgExc_SDLError, - "Camera needs to be started to read data"); - return 0; + RAISERETURN(pgExc_SDLError, "Camera needs to be started to read data", + 0); } if (!_check_integrity(self)) { @@ -858,9 +854,8 @@ windows_read_raw(pgCameraObject *self) HRESULT hr; if (!self->open) { - PyErr_SetString(pgExc_SDLError, - "Camera needs to be started to read data"); - return 0; + RAISERETURN(pgExc_SDLError, "Camera needs to be started to read data", + 0); } if (!_check_integrity(self)) { @@ -877,9 +872,8 @@ windows_read_raw(pgCameraObject *self) data = PyBytes_FromStringAndSize(buf_data, buf_length); if (!data) { - PyErr_SetString(pgExc_SDLError, - "Error constructing bytes from data"); - return 0; + RAISERETURN(pgExc_SDLError, "Error constructing bytes from data", + 0); } hr = self->raw_buf->lpVtbl->Unlock(self->raw_buf); diff --git a/src_c/circle.c b/src_c/circle.c index b236da023a..f160490d40 100644 --- a/src_c/circle.c +++ b/src_c/circle.c @@ -44,12 +44,11 @@ static int pg_circle_init(pgCircleObject *self, PyObject *args, PyObject *kwds) { if (!pgCircle_FromObject(args, &self->circle)) { - PyErr_SetString( - PyExc_TypeError, - "Arguments must be a Circle, a sequence of length 3 or 2, or an " - "object with an attribute called 'circle', all with corresponding " - "nonnegative radius argument"); - return -1; + RAISERETURN(PyExc_TypeError, + "Arguments must be a Circle, a sequence of length 3 or 2, " + "or an object with an attribute called 'circle', all with " + "corresponding nonnegative radius argument", + -1); } return 0; } @@ -139,10 +138,8 @@ static PyObject * pg_circle_update(pgCircleObject *self, PyObject *const *args, Py_ssize_t nargs) { if (!pgCircle_FromObjectFastcall(args, nargs, &self->circle)) { - PyErr_SetString( - PyExc_TypeError, - "Circle.update requires a circle or CircleLike object"); - return NULL; + return RAISE(PyExc_TypeError, + "Circle.update requires a circle or CircleLike object"); } Py_RETURN_NONE; } @@ -334,18 +331,17 @@ _pg_circle_collideswith(pgCircleBase *scirc, PyObject *arg) else if (PySequence_Check(arg)) { double x, y; if (!pg_TwoDoublesFromObj(arg, &x, &y)) { - PyErr_SetString( + RAISERETURN( PyExc_TypeError, - "Invalid point argument, must be a sequence of two numbers"); - return -1; + "Invalid point argument, must be a sequence of two numbers", + -1); } result = pgCollision_CirclePoint(scirc, x, y); } else { - PyErr_SetString( - PyExc_TypeError, - "Invalid point argument, must be a sequence of 2 numbers"); - return -1; + RAISERETURN(PyExc_TypeError, + "Invalid point argument, must be a sequence of 2 numbers", + -1); } return result; @@ -672,8 +668,7 @@ pg_circle_setr(pgCircleObject *self, PyObject *value, void *closure) } if (radius < 0) { - PyErr_SetString(PyExc_ValueError, "Radius must be nonnegative"); - return -1; + RAISERETURN(PyExc_ValueError, "Radius must be nonnegative", -1); } self->circle.r = radius; @@ -695,15 +690,13 @@ pg_circle_setr_sqr(pgCircleObject *self, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_DoubleFromObj(value, &radius_squared)) { - PyErr_SetString(PyExc_TypeError, - "Invalid type for radius squared, must be numeric"); - return -1; + RAISERETURN(PyExc_TypeError, + "Invalid type for radius squared, must be numeric", -1); } if (radius_squared < 0) { - PyErr_SetString(PyExc_ValueError, - "Invalid radius squared value, must be nonnegative"); - return -1; + RAISERETURN(PyExc_ValueError, + "Invalid radius squared value, must be nonnegative", -1); } self->circle.r = sqrt(radius_squared); @@ -722,8 +715,7 @@ pg_circle_setcenter(pgCircleObject *self, PyObject *value, void *closure) { DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_TwoDoublesFromObj(value, &self->circle.x, &self->circle.y)) { - PyErr_SetString(PyExc_TypeError, "Expected a sequence of 2 numbers"); - return -1; + RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", -1); } return 0; } @@ -742,15 +734,13 @@ pg_circle_setarea(pgCircleObject *self, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_DoubleFromObj(value, &area)) { - PyErr_SetString(PyExc_TypeError, - "Invalid type for area, must be numeric"); - return -1; + RAISERETURN(PyExc_TypeError, "Invalid type for area, must be numeric", + -1); } if (area < 0) { - PyErr_SetString(PyExc_ValueError, - "Invalid area value, must be nonnegative"); - return -1; + RAISERETURN(PyExc_ValueError, + "Invalid area value, must be nonnegative", -1); } self->circle.r = sqrt(area / M_PI); @@ -773,15 +763,13 @@ pg_circle_setcircumference(pgCircleObject *self, PyObject *value, DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_DoubleFromObj(value, &circumference)) { - PyErr_SetString(PyExc_TypeError, - "Invalid type for circumference, must be numeric"); - return -1; + RAISERETURN(PyExc_TypeError, + "Invalid type for circumference, must be numeric", -1); } if (circumference < 0) { - PyErr_SetString(PyExc_ValueError, - "Invalid circumference value, must be nonnegative"); - return -1; + RAISERETURN(PyExc_ValueError, + "Invalid circumference value, must be nonnegative", -1); } self->circle.r = circumference / M_TWOPI; @@ -803,15 +791,13 @@ pg_circle_setdiameter(pgCircleObject *self, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_DoubleFromObj(value, &diameter)) { - PyErr_SetString(PyExc_TypeError, - "Invalid type for diameter, must be numeric"); - return -1; + RAISERETURN(PyExc_TypeError, + "Invalid type for diameter, must be numeric", -1); } if (diameter < 0) { - PyErr_SetString(PyExc_ValueError, - "Invalid diameter value, must be nonnegative"); - return -1; + RAISERETURN(PyExc_ValueError, + "Invalid diameter value, must be nonnegative", -1); } self->circle.r = diameter / 2; @@ -834,8 +820,7 @@ pg_circle_settop(pgCircleObject *self, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_TwoDoublesFromObj(value, &x, &y)) { - PyErr_SetString(PyExc_TypeError, "Expected a sequence of 2 numbers"); - return -1; + RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", -1); } self->circle.y = y + self->circle.r; @@ -859,8 +844,7 @@ pg_circle_setleft(pgCircleObject *self, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_TwoDoublesFromObj(value, &x, &y)) { - PyErr_SetString(PyExc_TypeError, "Expected a sequence of 2 numbers"); - return -1; + RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", -1); } self->circle.x = x + self->circle.r; @@ -884,8 +868,7 @@ pg_circle_setbottom(pgCircleObject *self, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_TwoDoublesFromObj(value, &x, &y)) { - PyErr_SetString(PyExc_TypeError, "Expected a sequence of 2 numbers"); - return -1; + RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", -1); } self->circle.y = y - self->circle.r; @@ -909,8 +892,7 @@ pg_circle_setright(pgCircleObject *self, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_TwoDoublesFromObj(value, &x, &y)) { - PyErr_SetString(PyExc_TypeError, "Expected a sequence of 2 numbers"); - return -1; + RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", -1); } self->circle.x = x - self->circle.r; diff --git a/src_c/color.c b/src_c/color.c index 1ea417ce1e..272dd31452 100644 --- a/src_c/color.c +++ b/src_c/color.c @@ -356,10 +356,9 @@ _get_color_int_component(PyObject *val, Uint8 *color) if (PyLong_Check(val)) { unsigned long longval = PyLong_AsUnsignedLong(val); if (PyErr_Occurred() || (longval > 255)) { - PyErr_SetString( - PyExc_ValueError, - "invalid color component (must be in range [0, 255])"); - return 0; + RAISERETURN(PyExc_ValueError, + "invalid color component (must be in range [0, 255])", + 0); } *color = (Uint8)longval; return 1; @@ -624,8 +623,7 @@ _parse_color_from_text(PyObject *str_obj, Uint8 *rgba) color = PyDict_GetItem(_COLORDICT, name2); Py_DECREF(name2); if (!color) { - PyErr_SetString(PyExc_ValueError, "invalid color name"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid color name", -1); } } @@ -1035,8 +1033,7 @@ _color_set_hsva(pgColorObject *color, PyObject *value, void *closure) if (!PySequence_Check(value) || PySequence_Size(value) < 3 || PySequence_Size(value) > 4) { - PyErr_SetString(PyExc_ValueError, "invalid HSVA value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid HSVA value", -1); } /* H */ @@ -1044,8 +1041,7 @@ _color_set_hsva(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(hsva[0])) || hsva[0] < 0 || hsva[0] > 360) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid HSVA value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid HSVA value", -1); } Py_DECREF(item); @@ -1054,8 +1050,7 @@ _color_set_hsva(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(hsva[1])) || hsva[1] < 0 || hsva[1] > 100) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid HSVA value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid HSVA value", -1); } Py_DECREF(item); @@ -1064,8 +1059,7 @@ _color_set_hsva(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(hsva[2])) || hsva[2] < 0 || hsva[2] > 100) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid HSVA value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid HSVA value", -1); } Py_DECREF(item); @@ -1075,8 +1069,7 @@ _color_set_hsva(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(hsva[3])) || hsva[3] < 0 || hsva[3] > 100) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid HSVA value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid HSVA value", -1); } Py_DECREF(item); } @@ -1201,8 +1194,7 @@ _color_set_hsla(pgColorObject *color, PyObject *value, void *closure) if (!PySequence_Check(value) || PySequence_Size(value) < 3 || PySequence_Size(value) > 4) { - PyErr_SetString(PyExc_ValueError, "invalid HSLA value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid HSLA value", -1); } /* H */ @@ -1210,8 +1202,7 @@ _color_set_hsla(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(hsla[0])) || hsla[0] < 0 || hsla[0] > 360) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid HSLA value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid HSLA value", -1); } Py_DECREF(item); @@ -1220,8 +1211,7 @@ _color_set_hsla(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(hsla[1])) || hsla[1] < 0 || hsla[1] > 100) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid HSLA value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid HSLA value", -1); } Py_DECREF(item); @@ -1230,8 +1220,7 @@ _color_set_hsla(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(hsla[2])) || hsla[2] < 0 || hsla[2] > 100) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid HSLA value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid HSLA value", -1); } Py_DECREF(item); @@ -1241,8 +1230,7 @@ _color_set_hsla(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(hsla[3])) || hsla[3] < 0 || hsla[3] > 100) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid HSLA value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid HSLA value", -1); } Py_DECREF(item); } @@ -1366,8 +1354,7 @@ _color_set_i1i2i3(pgColorObject *color, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK("i1i2i3", value); if (!PySequence_Check(value) || PySequence_Size(value) != 3) { - PyErr_SetString(PyExc_ValueError, "invalid I1I2I3 value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid I1I2I3 value", -1); } /* I1 */ @@ -1375,8 +1362,7 @@ _color_set_i1i2i3(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(i1i2i3[0])) || i1i2i3[0] < 0 || i1i2i3[0] > 1) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid I1I2I3 value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid I1I2I3 value", -1); } Py_DECREF(item); @@ -1385,8 +1371,7 @@ _color_set_i1i2i3(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(i1i2i3[1])) || i1i2i3[1] < -0.5f || i1i2i3[1] > 0.5f) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid I1I2I3 value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid I1I2I3 value", -1); } Py_DECREF(item); @@ -1395,8 +1380,7 @@ _color_set_i1i2i3(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(i1i2i3[2])) || i1i2i3[2] < -0.5f || i1i2i3[2] > 0.5f) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid I1I2I3 value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid I1I2I3 value", -1); } Py_DECREF(item); @@ -1438,16 +1422,14 @@ _color_set_cmy(pgColorObject *color, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK("cmy", value); if (!PySequence_Check(value) || PySequence_Size(value) != 3) { - PyErr_SetString(PyExc_ValueError, "invalid CMY value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid CMY value", -1); } /* I1 */ item = PySequence_GetItem(value, 0); if (!item || !_get_double(item, &(cmy[0])) || cmy[0] < 0 || cmy[0] > 1) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid CMY value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid CMY value", -1); } Py_DECREF(item); @@ -1455,8 +1437,7 @@ _color_set_cmy(pgColorObject *color, PyObject *value, void *closure) item = PySequence_GetItem(value, 1); if (!item || !_get_double(item, &(cmy[1])) || cmy[1] < 0 || cmy[1] > 1) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid CMY value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid CMY value", -1); } Py_DECREF(item); @@ -1464,8 +1445,7 @@ _color_set_cmy(pgColorObject *color, PyObject *value, void *closure) item = PySequence_GetItem(value, 2); if (!item || !_get_double(item, &(cmy[2])) || cmy[2] < 0 || cmy[2] > 1) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid CMY value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid CMY value", -1); } Py_DECREF(item); @@ -1499,16 +1479,14 @@ _color_set_normalized(pgColorObject *color, PyObject *value, void *closure) if (!PySequence_Check(value) || PySequence_Size(value) < 3 || PySequence_Size(value) > 4) { - PyErr_SetString(PyExc_ValueError, "invalid normalized value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid normalized value", -1); } item = PySequence_GetItem(value, 0); if (!item || !_get_double(item, &(frgba[0])) || frgba[0] < 0.0 || frgba[0] > 1.0) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid normalized value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid normalized value", -1); } Py_DECREF(item); @@ -1516,8 +1494,7 @@ _color_set_normalized(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(frgba[1])) || frgba[1] < 0.0 || frgba[1] > 1.0) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid normalized value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid normalized value", -1); } Py_DECREF(item); @@ -1525,8 +1502,7 @@ _color_set_normalized(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(frgba[2])) || frgba[2] < 0.0 || frgba[2] > 1.0) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid normalized value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid normalized value", -1); } Py_DECREF(item); @@ -1535,8 +1511,7 @@ _color_set_normalized(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(frgba[3])) || frgba[3] < 0.0 || frgba[3] > 1.0) { Py_XDECREF(item); - PyErr_SetString(PyExc_ValueError, "invalid normalized value"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid normalized value", -1); } Py_DECREF(item); } @@ -1563,14 +1538,12 @@ _color_set_hex(pgColorObject *color, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK("hex", value); if (!PyUnicode_Check(value)) { - PyErr_SetString(PyExc_TypeError, "hex color must be a string"); - return -1; + RAISERETURN(PyExc_TypeError, "hex color must be a string", -1); } switch (_hexcolor(value, color->data)) { case TRISTATE_FAIL: - PyErr_SetString(PyExc_ValueError, "invalid hex string"); - return -1; + RAISERETURN(PyExc_ValueError, "invalid hex string", -1); case TRISTATE_ERROR: return -1; /* forward python error */ default: @@ -1874,8 +1847,7 @@ _color_ass_item(pgColorObject *color, Py_ssize_t _index, PyObject *value) case 3: return _color_set_a(color, value, NULL); default: - PyErr_SetString(PyExc_IndexError, "invalid index"); - break; + RAISERETURN(PyExc_IndexError, "invalid index", -1); } return -1; } @@ -1946,9 +1918,8 @@ static int _color_contains(pgColorObject *self, PyObject *arg) { if (!PyLong_Check(arg)) { - PyErr_SetString(PyExc_TypeError, - "'in ' requires integer object"); - return -1; + RAISERETURN(PyExc_TypeError, + "'in ' requires integer object", -1); } long comp = PyLong_AsLong(arg); @@ -1970,9 +1941,8 @@ static int _color_set_slice(pgColorObject *color, PyObject *idx, PyObject *val) { if (val == NULL) { - PyErr_SetString(PyExc_TypeError, - "Color object doesn't support item deletion"); - return -1; + RAISERETURN(PyExc_TypeError, + "Color object doesn't support item deletion", -1); } if (PyLong_Check(idx)) { return _color_ass_item(color, PyLong_AsLong(idx), val); @@ -2009,16 +1979,14 @@ _color_set_slice(pgColorObject *color, PyObject *idx, PyObject *val) c = PyLong_AsLong(obj); } else { - PyErr_SetString(PyExc_TypeError, - "color components must be integers"); Py_DECREF(fastitems); - return -1; + RAISERETURN(PyExc_TypeError, + "color components must be integers", -1); } if (c < 0 || c > 255) { - PyErr_SetString(PyExc_ValueError, - "color component must be 0-255"); Py_DECREF(fastitems); - return -1; + RAISERETURN(PyExc_TypeError, "color component must be 0-255", + -1); } color->data[cur] = (Uint8)c; } @@ -2026,8 +1994,7 @@ _color_set_slice(pgColorObject *color, PyObject *idx, PyObject *val) Py_DECREF(fastitems); return 0; } - PyErr_SetString(PyExc_IndexError, "Index must be an integer or slice"); - return -1; + RAISERETURN(PyExc_IndexError, "Index must be an integer or slice", -1); } /* @@ -2080,8 +2047,7 @@ _color_getbuffer(pgColorObject *color, Py_buffer *view, int flags) static char format[] = "B"; if (PyBUF_HAS_FLAG(flags, PyBUF_WRITABLE)) { - PyErr_SetString(pgExc_BufferError, "color buffer is read-only"); - return -1; + RAISERETURN(pgExc_BufferError, "color buffer is read-only", -1); } view->buf = color->data; view->ndim = 1; @@ -2275,17 +2241,16 @@ _color_setAttr_swizzle(pgColorObject *self, PyObject *attr_name, PyObject *val) } if (entry_was_set[idx]) { - PyErr_SetString(PyExc_AttributeError, - "Attribute assignment conflicts with swizzling"); + RAISE(PyExc_AttributeError, + "Attribute assignment conflicts with swizzling"); goto swizzle_error; } entry_was_set[idx] = 1; entry_obj = PySequence_GetItem(val, i); if (entry_obj == NULL) { - PyErr_SetString( - PyExc_TypeError, - "A sequence of the corresponding elements is expected"); + RAISE(PyExc_TypeError, + "A sequence of the corresponding elements is expected"); goto swizzle_error; } @@ -2299,9 +2264,8 @@ _color_setAttr_swizzle(pgColorObject *self, PyObject *attr_name, PyObject *val) entry[idx] = (Uint8)entry_long; } else { - PyErr_SetString( - PyExc_TypeError, - "Color element is outside of the range from 0 to 255"); + RAISE(PyExc_TypeError, + "Color element is outside of the range from 0 to 255"); goto swizzle_error; } } @@ -2383,10 +2347,8 @@ _pg_pylong_to_uint32(PyObject *val, Uint32 *color, int handle_negative) return 1; error: - PyErr_SetString( - PyExc_ValueError, - "invalid color argument (integer out of acceptable range)"); - return 0; + RAISERETURN(PyExc_ValueError, + "invalid color argument (integer out of acceptable range)", 0); } static int @@ -2418,11 +2380,10 @@ pg_RGBAFromObjEx(PyObject *obj, Uint8 *rgba, pgColorHandleFlags handle_flags) if ((handle_flags & PG_COLOR_HANDLE_RESTRICT_SEQ) && !PyTuple_Check(obj)) { /* ValueError (and not TypeError) for backcompat reasons */ - PyErr_SetString( - PyExc_ValueError, - "invalid color (here, generic sequences are " - "restricted, but pygame.Color and RGB[A] tuples are allowed)"); - return 0; + RAISERETURN(PyExc_ValueError, + "invalid color (here, generic sequences are restricted, " + "but pygame.Color and RGB[A] tuples are allowed)", + 0); } if (pg_RGBAFromObj(obj, rgba)) { @@ -2433,18 +2394,17 @@ pg_RGBAFromObjEx(PyObject *obj, Uint8 *rgba, pgColorHandleFlags handle_flags) /* Error */ if (PySequence_Check(obj)) { /* It was a tuple-like; raise a ValueError */ - PyErr_SetString( - PyExc_ValueError, - "invalid color (color sequence must have size 3 or " - "4, and each element must be an integer in the range [0, " - "255])"); + RAISERETURN(PyExc_ValueError, + "invalid color (color sequence must have size 3 or 4, and " + "each element must be an integer in the range [0, 255])", + 0); } else { PyErr_Format(PyExc_TypeError, "unable to interpret object of type '%128s' as a color", Py_TYPE(obj)->tp_name); + return 0; } - return 0; } static int diff --git a/src_c/display.c b/src_c/display.c index 8f72dcbcc6..bf188c62cc 100644 --- a/src_c/display.c +++ b/src_c/display.c @@ -448,7 +448,7 @@ pg_GetVideoInfo(pg_VideoInfo *info) SDL_FreeFormat(tempformat); } else { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); + RAISE(pgExc_SDLError, SDL_GetError()); return (pg_VideoInfo *)NULL; } } @@ -1218,7 +1218,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds) state->gl_context = SDL_GL_CreateContext(win); if (!state->gl_context) { _display_state_cleanup(state); - PyErr_SetString(pgExc_SDLError, SDL_GetError()); + RAISE(pgExc_SDLError, SDL_GetError()); goto DESTROY_WINDOW; } /* SDL_GetWindowSurface can not be used when using GL. @@ -1246,19 +1246,16 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds) for the whole desktop because of wayland GL compositing. */ if (vsync == -1) { if (SDL_GL_SetSwapInterval(-1) != 0) { - PyErr_SetString(pgExc_SDLError, - "adaptive vsync for OpenGL not " - "available"); - + RAISE(pgExc_SDLError, + "adaptive vsync for OpenGL not available"); _display_state_cleanup(state); goto DESTROY_WINDOW; } } else if (vsync == 1) { if (SDL_GL_SetSwapInterval(1) != 0) { - PyErr_SetString(pgExc_SDLError, - "regular vsync for OpenGL not " - "available"); + RAISE(pgExc_SDLError, + "regular vsync for OpenGL not available"); _display_state_cleanup(state); goto DESTROY_WINDOW; } @@ -1317,8 +1314,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds) SDL_GetRendererInfo(pg_renderer, &info); if (vsync && !(info.flags & SDL_RENDERER_PRESENTVSYNC)) { - PyErr_SetString(pgExc_SDLError, - "could not enable vsync"); + RAISE(pgExc_SDLError, "could not enable vsync"); _display_state_cleanup(state); goto DESTROY_WINDOW; } @@ -1352,22 +1348,21 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds) _display_state_cleanup(state); /* Recover error, then destroy the window */ - PyErr_SetString(pgExc_SDLError, SDL_GetError()); + RAISE(pgExc_SDLError, SDL_GetError()); goto DESTROY_WINDOW; } } if (state->using_gl && pg_renderer != NULL) { _display_state_cleanup(state); - PyErr_SetString( - pgExc_SDLError, - "GL context and SDL_Renderer created at the same time"); + RAISE(pgExc_SDLError, + "GL context and SDL_Renderer created at the same time"); goto DESTROY_WINDOW; } if (!surf) { _display_state_cleanup(state); - PyErr_SetString(pgExc_SDLError, SDL_GetError()); + RAISE(pgExc_SDLError, SDL_GetError()); goto DESTROY_WINDOW; } if (!surface) { @@ -1497,8 +1492,7 @@ _pg_get_default_display_masks(int bpp, Uint32 *Rmask, Uint32 *Gmask, *Bmask = 0xFF; break; default: - PyErr_SetString(PyExc_ValueError, "nonstandard bit depth given"); - return -1; + RAISERETURN(PyExc_ValueError, "nonstandard bit depth given", -1); } return 0; } @@ -1696,13 +1690,11 @@ pg_flip_internal(_DisplayState *state) /* Same check as VIDEO_INIT_CHECK() but returns -1 instead of NULL on * fail. */ if (!SDL_WasInit(SDL_INIT_VIDEO)) { - PyErr_SetString(pgExc_SDLError, "video system not initialized"); - return -1; + RAISERETURN(pgExc_SDLError, "video system not initialized", -1); } if (!win) { - PyErr_SetString(pgExc_SDLError, "Display mode not set"); - return -1; + RAISERETURN(pgExc_SDLError, "Display mode not set", -1); } Py_BEGIN_ALLOW_THREADS; @@ -1735,8 +1727,7 @@ pg_flip_internal(_DisplayState *state) Py_END_ALLOW_THREADS; if (status < 0) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } return 0; @@ -2066,18 +2057,15 @@ pg_convert_to_uint16(PyObject *python_array, Uint16 *c_uint16_array) PyObject *item; if (!c_uint16_array) { - PyErr_SetString(PyExc_RuntimeError, - "Memory not allocated for c_uint16_array."); - return 0; + RAISERETURN(PyExc_RuntimeError, + "Memory not allocated for c_uint16_array.", 0); } if (!PySequence_Check(python_array)) { - PyErr_SetString(PyExc_TypeError, "Array must be sequence type"); - return 0; + RAISERETURN(PyExc_TypeError, "Array must be sequence type", 0); } if (PySequence_Size(python_array) != 256) { - PyErr_SetString(PyExc_ValueError, - "gamma ramp must be 256 elements long"); - return 0; + RAISERETURN(PyExc_ValueError, "gamma ramp must be 256 elements long", + 0); } for (i = 0; i < 256; i++) { long ret; @@ -2086,9 +2074,8 @@ pg_convert_to_uint16(PyObject *python_array, Uint16 *c_uint16_array) return 0; } if (!PyLong_Check(item)) { - PyErr_SetString(PyExc_ValueError, - "gamma ramp must contain integer elements"); - return 0; + RAISERETURN(PyExc_ValueError, + "gamma ramp must contain integer elements", 0); } ret = PyLong_AsLong(item); Py_XDECREF(item); @@ -2097,10 +2084,9 @@ pg_convert_to_uint16(PyObject *python_array, Uint16 *c_uint16_array) /* Happens when PyLong_AsLong overflows */ return 0; } - PyErr_SetString( - PyExc_ValueError, - "integers in gamma ramp must be between 0 and 0xFFFF"); - return 0; + RAISERETURN(PyExc_ValueError, + "integers in gamma ramp must be between 0 and 0xFFFF", + 0); } c_uint16_array[i] = (Uint16)ret; } @@ -2973,16 +2959,14 @@ pg_message_box(PyObject *self, PyObject *arg, PyObject *kwargs) msgbox_data.numbuttons = 1; if (-1 > return_button_index || return_button_index >= 1) { - PyErr_SetString(PyExc_IndexError, - "return_button index out of range"); + RAISE(PyExc_IndexError, "return_button index out of range"); goto error; } buttons_data->flags |= SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT; if (escape_button_used) { if (-1 > escape_button_index || escape_button_index >= 1) { - PyErr_SetString(PyExc_IndexError, - "escape_button index out of range"); + RAISE(PyExc_IndexError, "escape_button index out of range"); goto error; } buttons_data->flags |= SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT; @@ -3030,8 +3014,8 @@ pg_message_box(PyObject *self, PyObject *arg, PyObject *kwargs) } if (!PyUnicode_Check(btn_name_obj)) { - PyErr_SetString(PyExc_TypeError, - "'buttons' should be a sequence of string"); + RAISE(PyExc_TypeError, + "'buttons' should be a sequence of string"); goto error; } @@ -3060,7 +3044,7 @@ pg_message_box(PyObject *self, PyObject *arg, PyObject *kwargs) int clicked_button_id; if (SDL_ShowMessageBox(&msgbox_data, &clicked_button_id)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); + RAISE(pgExc_SDLError, SDL_GetError()); goto error; } diff --git a/src_c/draw.c b/src_c/draw.c index a63aea97cc..1d69932c76 100644 --- a/src_c/draw.c +++ b/src_c/draw.c @@ -833,14 +833,12 @@ circle(PyObject *self, PyObject *args, PyObject *kwargs) } if (!pg_TwoIntsFromObj(posobj, &posx, &posy)) { - PyErr_SetString(PyExc_TypeError, - "center argument must be a pair of numbers"); - return 0; + RAISERETURN(PyExc_TypeError, + "center argument must be a pair of numbers", 0) } if (!pg_IntFromObj(radiusobj, &radius)) { - PyErr_SetString(PyExc_TypeError, "radius argument must be a number"); - return 0; + RAISERETURN(PyExc_TypeError, "radius argument must be a number", 0); } surf = pgSurface_AsSurface(surfobj); diff --git a/src_c/event.c b/src_c/event.c index 35ac437d4e..c11d0d6df3 100644 --- a/src_c/event.c +++ b/src_c/event.c @@ -718,9 +718,8 @@ static int pg_EnableKeyRepeat(int delay, int interval) { if (delay < 0 || interval < 0) { - PyErr_SetString(PyExc_ValueError, - "delay and interval must equal at least 0"); - return -1; + RAISERETURN(PyExc_ValueError, + "delay and interval must equal at least 0", -1); } PG_LOCK_EVFILTER_MUTEX pg_key_repeat_delay = delay; @@ -1677,8 +1676,7 @@ pg_event_init(pgEventObject *self, PyObject *args, PyObject *kwargs) } if (type < 0 || type >= PG_NUMEVENTS) { - PyErr_SetString(PyExc_ValueError, "event type out of range"); - return -1; + RAISERETURN(PyExc_ValueError, "event type out of range", -1); } if (!dict) { @@ -1705,10 +1703,9 @@ pg_event_init(pgEventObject *self, PyObject *args, PyObject *kwargs) } if (PyDict_GetItemString(dict, "type")) { - PyErr_SetString(PyExc_ValueError, - "redundant type field in event dict"); Py_DECREF(dict); - return -1; + RAISERETURN(PyExc_ValueError, "redundant type field in event dict", + -1); } self->type = _pg_pgevent_deproxify(type); @@ -1939,13 +1936,11 @@ _pg_eventtype_from_seq(PyObject *seq, int ind) { int val = 0; if (!pg_IntFromObjIndex(seq, ind, &val)) { - PyErr_SetString(PyExc_TypeError, - "type sequence must contain valid event types"); - return -1; + RAISERETURN(PyExc_TypeError, + "type sequence must contain valid event types", -1); } if (val < 0 || val >= PG_NUMEVENTS) { - PyErr_SetString(PyExc_ValueError, "event type out of range"); - return -1; + RAISERETURN(PyExc_ValueError, "event type out of range", -1); } return val; } @@ -2101,7 +2096,7 @@ _pg_get_all_events_except(PyObject *obj) do { ret = PG_PEEP_EVENT(&event, 1, SDL_GETEVENT, type); if (ret < 0) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); + RAISE(pgExc_SDLError, SDL_GetError()); goto error; } else if (ret > 0) { @@ -2125,7 +2120,7 @@ _pg_get_all_events_except(PyObject *obj) ret = PG_PEEP_EVENT(&event, 1, SDL_GETEVENT, _pg_pgevent_proxify(type)); if (ret < 0) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); + RAISE(pgExc_SDLError, SDL_GetError()); goto error; } else if (ret > 0) { @@ -2151,7 +2146,7 @@ _pg_get_all_events_except(PyObject *obj) do { len = PG_PEEP_EVENT_ALL(eventbuf, PG_GET_LIST_LEN, SDL_GETEVENT); if (len == -1) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); + RAISE(pgExc_SDLError, SDL_GetError()); goto error; } @@ -2191,7 +2186,7 @@ _pg_get_all_events(void) while (len == PG_GET_LIST_LEN) { len = PG_PEEP_EVENT_ALL(eventbuf, PG_GET_LIST_LEN, SDL_GETEVENT); if (len == -1) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); + RAISE(pgExc_SDLError, SDL_GetError()); goto error; } @@ -2235,7 +2230,7 @@ _pg_get_seq_events(PyObject *obj) do { ret = PG_PEEP_EVENT(&event, 1, SDL_GETEVENT, type); if (ret < 0) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); + RAISE(pgExc_SDLError, SDL_GetError()); goto error; } else if (ret > 0) { @@ -2248,7 +2243,7 @@ _pg_get_seq_events(PyObject *obj) ret = PG_PEEP_EVENT(&event, 1, SDL_GETEVENT, _pg_pgevent_proxify(type)); if (ret < 0) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); + RAISE(pgExc_SDLError, SDL_GetError()); goto error; } else if (ret > 0) { diff --git a/src_c/font.c b/src_c/font.c index 3f46279511..07a1984bca 100644 --- a/src_c/font.c +++ b/src_c/font.c @@ -536,18 +536,16 @@ font_setter_align(PyObject *self, PyObject *value, void *closure) long val = PyLong_AsLong(value); if (val == -1 && PyErr_Occurred()) { - PyErr_SetString( - PyExc_TypeError, - "font.align must be an integer. " - "Must correspond with FONT_LEFT, FONT_CENTER, or FONT_RIGHT."); - return -1; + RAISERETURN(PyExc_TypeError, + "font.align must be an integer. Must correspond with " + "FONT_LEFT, FONT_CENTER, or FONT_RIGHT.", + -1); } if (val < 0 || val > 2) { - PyErr_SetString(pgExc_SDLError, - "font.align must be FONT_LEFT, FONT_CENTER, or " - "FONT_RIGHT."); - return -1; + RAISERETURN( + pgExc_SDLError, + "font.align must be FONT_LEFT, FONT_CENTER, or FONT_RIGHT.", -1); } #if SDL_TTF_VERSION_ATLEAST(3, 0, 0) @@ -557,10 +555,10 @@ font_setter_align(PyObject *self, PyObject *value, void *closure) #endif return 0; #else - PyErr_SetString(pgExc_SDLError, - "pygame.font not compiled with a new enough SDL_ttf " - "version. Needs SDL_ttf 2.20.0 or above."); - return -1; + RAISERETURN(pgExc_SDLError, + "pygame.font not compiled with a new enough SDL_ttf version. " + "Needs SDL_ttf 2.20.0 or above.", + -1); #endif } @@ -823,9 +821,8 @@ font_setter_point_size(PyFontObject *self, PyObject *value, void *closure) } if (val <= 0) { - PyErr_SetString(PyExc_ValueError, - "point_size cannot be equal to, or less than 0"); - return -1; + RAISERETURN(PyExc_ValueError, + "point_size cannot be equal to, or less than 0", -1); } #if SDL_TTF_VERSION_ATLEAST(3, 0, 0) @@ -835,16 +832,14 @@ font_setter_point_size(PyFontObject *self, PyObject *value, void *closure) if (TTF_SetFontSize(font, val) == -1) #endif { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } self->ptsize = val; return 0; #else - PyErr_SetString(pgExc_SDLError, - "Incorrect SDL_TTF version (requires 2.0.18)"); - return -1; + RAISERETURN(pgExc_SDLError, "Incorrect SDL_TTF version (requires 2.0.18)", + -1); #endif } @@ -1243,8 +1238,7 @@ font_init(PyFontObject *self, PyObject *args, PyObject *kwds) } if (!font_initialized) { - PyErr_SetString(pgExc_SDLError, "font not initialized"); - return -1; + RAISERETURN(pgExc_SDLError, "font not initialized", -1); } /* Incref obj, needs to be decref'd later */ diff --git a/src_c/freetype/ft_layout.c b/src_c/freetype/ft_layout.c index 8d4cfd917f..a4a5db930d 100644 --- a/src_c/freetype/ft_layout.c +++ b/src_c/freetype/ft_layout.c @@ -139,8 +139,7 @@ _PGFT_LoadLayout(FreeTypeInstance *ft, pgFontObject *fontobj, copy_mode(&ftext->mode, mode); font = _PGFT_GetFontSized(ft, fontobj, mode->face_size); if (!font) { - PyErr_SetString(pgExc_SDLError, _PGFT_GetError(ft)); - return 0; + RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } } @@ -213,8 +212,7 @@ size_text(Layout *ftext, FreeTypeInstance *ft, TextContext *context, &slots[length].kerning); if (error) { _PGFT_SetError(ft, "Loading glyphs", error); - PyErr_SetString(pgExc_SDLError, _PGFT_GetError(ft)); - return -1; + RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), -1); } } prev_id = id; diff --git a/src_c/freetype/ft_render.c b/src_c/freetype/ft_render.c index a7cb54c28d..62bd97413d 100644 --- a/src_c/freetype/ft_render.c +++ b/src_c/freetype/ft_render.c @@ -126,10 +126,10 @@ _PGFT_BuildRenderMode(FreeTypeInstance *ft, pgFontObject *fontobj, { if (face_size.x == 0) { if (fontobj->face_size.x == 0) { - PyErr_SetString(PyExc_ValueError, - "No font point size specified" - " and no default font size in typeface"); - return -1; + RAISERETURN(PyExc_ValueError, + "No font point size specified and no default font " + "size in typeface", + -1); } face_size = fontobj->face_size; @@ -141,16 +141,14 @@ _PGFT_BuildRenderMode(FreeTypeInstance *ft, pgFontObject *fontobj, } else { if (_PGFT_CheckStyle((FT_UInt32)style)) { - PyErr_SetString(PyExc_ValueError, "Invalid style value"); - return -1; + RAISERETURN(PyExc_ValueError, "Invalid style value", -1); } mode->style = (FT_UInt16)style; } if ((mode->style & FT_STYLES_SCALABLE_ONLY) && !fontobj->is_scalable) { - PyErr_SetString(PyExc_ValueError, - "Unsupported style(s) for a bitmap font"); - return -1; + RAISERETURN(PyExc_ValueError, "Unsupported style(s) for a bitmap font", + -1); } mode->strength = DBL_TO_FX16(fontobj->strength); mode->underline_adjustment = DBL_TO_FX16(fontobj->underline_adjustment); @@ -160,34 +158,29 @@ _PGFT_BuildRenderMode(FreeTypeInstance *ft, pgFontObject *fontobj, if (mode->rotation_angle != 0) { if (!fontobj->is_scalable) { - PyErr_SetString(PyExc_ValueError, - "rotated text is unsupported for a bitmap font"); - return -1; + RAISERETURN(PyExc_ValueError, + "rotated text is unsupported for a bitmap font", -1); } if (mode->style & FT_STYLE_WIDE) { - PyErr_SetString(PyExc_ValueError, - "the wide style is unsupported for rotated text"); - return -1; + RAISERETURN(PyExc_ValueError, + "the wide style is unsupported for rotated text", -1); } if (mode->style & FT_STYLE_UNDERLINE) { - PyErr_SetString( - PyExc_ValueError, - "the underline style is unsupported for rotated text"); - return -1; + RAISERETURN(PyExc_ValueError, + "the underline style is unsupported for rotated text", + -1); } if (mode->render_flags & FT_RFLAG_PAD) { - PyErr_SetString(PyExc_ValueError, - "padding is unsupported for rotated text"); - return -1; + RAISERETURN(PyExc_ValueError, + "padding is unsupported for rotated text", -1); } } if (mode->render_flags & FT_RFLAG_VERTICAL) { if (mode->style & FT_STYLE_UNDERLINE) { - PyErr_SetString( - PyExc_ValueError, - "the underline style is unsupported for vertical text"); - return -1; + RAISERETURN(PyExc_ValueError, + "the underline style is unsupported for vertical text", + -1); } } @@ -195,8 +188,7 @@ _PGFT_BuildRenderMode(FreeTypeInstance *ft, pgFontObject *fontobj, FT_Face font = _PGFT_GetFontSized(ft, fontobj, mode->face_size); if (!font) { - PyErr_SetString(pgExc_SDLError, _PGFT_GetError(ft)); - return -1; + RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), -1); } if (!FT_HAS_KERNING(font)) { @@ -288,8 +280,7 @@ _PGFT_Render_ExistingSurface(FreeTypeInstance *ft, pgFontObject *fontobj, if (SDL_MUSTLOCK(surface)) { if (!PG_LockSurface(surface)) { SDL_FreeSurface(surface); - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } locked = 1; } @@ -337,8 +328,7 @@ _PGFT_Render_ExistingSurface(FreeTypeInstance *ft, pgFontObject *fontobj, if (!PG_SURF_BytesPerPixel(surface)) { // This should never happen, error to make static analyzer happy - PyErr_SetString(pgExc_SDLError, "Got surface of invalid format"); - return -1; + RAISERETURN(pgExc_SDLError, "Got surface of invalid format", -1); } /* @@ -440,15 +430,13 @@ _PGFT_Render_NewSurface(FreeTypeInstance *ft, pgFontObject *fontobj, } surface = PG_CreateSurface(width, height, pixelformat); if (!surface) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return 0; + RAISERETURN(pgExc_SDLError, SDL_GetError(), 0); } if (SDL_MUSTLOCK(surface)) { if (!PG_LockSurface(surface)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(surface); - return 0; + RAISERETURN(pgExc_SDLError, SDL_GetError(), 0); } locked = 1; } diff --git a/src_c/freetype/ft_wrap.c b/src_c/freetype/ft_wrap.c index 708e9832d5..8451d8566f 100644 --- a/src_c/freetype/ft_wrap.c +++ b/src_c/freetype/ft_wrap.c @@ -99,8 +99,7 @@ _PGFT_Font_IsFixedWidth(FreeTypeInstance *ft, pgFontObject *fontobj) FT_Face font = _PGFT_GetFont(ft, fontobj); if (!font) { - PyErr_SetString(pgExc_SDLError, _PGFT_GetError(ft)); - return -1; + RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), -1); } return FT_IS_FIXED_WIDTH(font) ? 1 : 0; } @@ -111,8 +110,7 @@ _PGFT_Font_NumFixedSizes(FreeTypeInstance *ft, pgFontObject *fontobj) FT_Face font = _PGFT_GetFont(ft, fontobj); if (!font) { - PyErr_SetString(pgExc_SDLError, _PGFT_GetError(ft)); - return -1; + RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), -1); } return FT_HAS_FIXED_SIZES(font) ? font->num_fixed_sizes : 0; } @@ -126,8 +124,7 @@ _PGFT_Font_GetAvailableSize(FreeTypeInstance *ft, pgFontObject *fontobj, FT_Bitmap_Size *bitmap_size_p; if (!font) { - PyErr_SetString(pgExc_SDLError, _PGFT_GetError(ft)); - return -1; + RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), -1); } if (!FT_HAS_FIXED_SIZES(font) || n > font->num_fixed_sizes) /* cond. or */ { @@ -149,8 +146,7 @@ _PGFT_Font_GetName(FreeTypeInstance *ft, pgFontObject *fontobj) font = _PGFT_GetFont(ft, fontobj); if (!font) { - PyErr_SetString(pgExc_SDLError, _PGFT_GetError(ft)); - return 0; + RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } return font->family_name ? font->family_name : ""; } @@ -162,8 +158,7 @@ _PGFT_Font_GetStyleName(FreeTypeInstance *ft, pgFontObject *fontobj) font = _PGFT_GetFont(ft, fontobj); if (!font) { - PyErr_SetString(pgExc_SDLError, _PGFT_GetError(ft)); - return 0; + RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } return font->style_name ? font->style_name : ""; } @@ -177,8 +172,7 @@ _PGFT_Font_GetHeight(FreeTypeInstance *ft, pgFontObject *fontobj) FT_Face font = _PGFT_GetFont(ft, fontobj); if (!font) { - PyErr_SetString(pgExc_SDLError, _PGFT_GetError(ft)); - return 0; + RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } return (long)font->height; } @@ -190,8 +184,7 @@ _PGFT_Font_GetHeightSized(FreeTypeInstance *ft, pgFontObject *fontobj, FT_Face font = _PGFT_GetFontSized(ft, fontobj, face_size); if (!font) { - PyErr_SetString(pgExc_SDLError, _PGFT_GetError(ft)); - return 0; + RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } return (long)FX6_TRUNC(FX6_CEIL(font->size->metrics.height)); } @@ -202,8 +195,7 @@ _PGFT_Font_GetAscender(FreeTypeInstance *ft, pgFontObject *fontobj) FT_Face font = _PGFT_GetFont(ft, fontobj); if (!font) { - PyErr_SetString(pgExc_SDLError, _PGFT_GetError(ft)); - return 0; + RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } return (long)font->ascender; } @@ -215,8 +207,7 @@ _PGFT_Font_GetAscenderSized(FreeTypeInstance *ft, pgFontObject *fontobj, FT_Face font = _PGFT_GetFontSized(ft, fontobj, face_size); if (!font) { - PyErr_SetString(pgExc_SDLError, _PGFT_GetError(ft)); - return 0; + RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } return (long)FX6_TRUNC(FX6_CEIL(font->size->metrics.ascender)); } @@ -227,8 +218,7 @@ _PGFT_Font_GetDescender(FreeTypeInstance *ft, pgFontObject *fontobj) FT_Face font = _PGFT_GetFont(ft, fontobj); if (!font) { - PyErr_SetString(pgExc_SDLError, _PGFT_GetError(ft)); - return 0; + RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } return (long)font->descender; } @@ -240,8 +230,7 @@ _PGFT_Font_GetDescenderSized(FreeTypeInstance *ft, pgFontObject *fontobj, FT_Face font = _PGFT_GetFontSized(ft, fontobj, face_size); if (!font) { - PyErr_SetString(pgExc_SDLError, _PGFT_GetError(ft)); - return 0; + RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } return (long)FX6_TRUNC(FX6_FLOOR(font->size->metrics.descender)); } @@ -257,8 +246,7 @@ _PGFT_Font_GetGlyphHeightSized(FreeTypeInstance *ft, pgFontObject *fontobj, FT_Size_Metrics *metrics; if (!font) { - PyErr_SetString(pgExc_SDLError, _PGFT_GetError(ft)); - return 0; + RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } metrics = &font->size->metrics; return (long)FX6_TRUNC(FX6_CEIL(metrics->ascender) - @@ -398,8 +386,7 @@ ft_wrap_init(FreeTypeInstance *ft, pgFontObject *fontobj) font = _PGFT_GetFont(ft, fontobj); if (!font) { - PyErr_SetString(PyExc_FileNotFoundError, _PGFT_GetError(ft)); - return -1; + RAISERETURN(PyExc_FileNotFoundError, _PGFT_GetError(ft), -1); } fontobj->is_scalable = FT_IS_SCALABLE(font) ? ~0 : 0; @@ -491,8 +478,7 @@ _PGFT_TryLoadFont_RWops(FreeTypeInstance *ft, pgFontObject *fontobj, position = SDL_RWtell(src); if (position < 0) { - PyErr_SetString(pgExc_SDLError, "Failed to seek in font stream"); - return -1; + RAISERETURN(pgExc_SDLError, "Failed to seek in font stream", -1); } stream = _PGFT_calloc(1, sizeof(*stream)); @@ -569,24 +555,21 @@ _PGFT_Init(FreeTypeInstance **_instance, int cache_size) error = FT_Init_FreeType(&inst->library); if (error) { - PyErr_SetString( - PyExc_RuntimeError, - "pygame (_PGFT_Init): failed to initialize FreeType library"); + RAISE(PyExc_RuntimeError, + "pygame (_PGFT_Init): failed to initialize FreeType library"); goto error_cleanup; } if (FTC_Manager_New(inst->library, 0, 0, 0, &_PGFT_font_request, 0, &inst->cache_manager) != 0) { - PyErr_SetString( - PyExc_RuntimeError, - "pygame (_PGFT_Init): failed to create new FreeType manager"); + RAISE(PyExc_RuntimeError, + "pygame (_PGFT_Init): failed to create new FreeType manager"); goto error_cleanup; } if (FTC_CMapCache_New(inst->cache_manager, &inst->cache_charmap) != 0) { - PyErr_SetString( - PyExc_RuntimeError, - "pygame (_PGFT_Init): failed to create new FreeType cache"); + RAISE(PyExc_RuntimeError, + "pygame (_PGFT_Init): failed to create new FreeType cache"); goto error_cleanup; } diff --git a/src_c/freetype/ft_wrap.h b/src_c/freetype/ft_wrap.h index 0bba50fc91..14d9dd31ac 100644 --- a/src_c/freetype/ft_wrap.h +++ b/src_c/freetype/ft_wrap.h @@ -241,12 +241,11 @@ extern _FreeTypeState _modstate; #define FREETYPE_STATE FREETYPE_MOD_STATE(0) #endif /* defined(PYPY_VERSION) */ -#define ASSERT_GRAB_FREETYPE(ft_ptr, rvalue) \ - ft_ptr = FREETYPE_STATE->freetype; \ - if (!ft_ptr) { \ - PyErr_SetString(PyExc_RuntimeError, \ - "The FreeType 2 library hasn't been initialized"); \ - return (rvalue); \ +#define ASSERT_GRAB_FREETYPE(ft_ptr, rvalue) \ + ft_ptr = FREETYPE_STATE->freetype; \ + if (!ft_ptr) { \ + RAISERETURN(PyExc_RuntimeError, \ + "The FreeType 2 library hasn't been initialized", rvalue) \ } /********************************************************** diff --git a/src_c/imageext.c b/src_c/imageext.c index b13a6859c7..9b68620009 100644 --- a/src_c/imageext.c +++ b/src_c/imageext.c @@ -471,8 +471,7 @@ MODINIT_DEFINE(imageext) #ifdef WITH_THREAD _pg_img_mutex = SDL_CreateMutex(); if (!_pg_img_mutex) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } #endif */ diff --git a/src_c/key.c b/src_c/key.c index a4bd1cf1d0..3792c8fa41 100644 --- a/src_c/key.c +++ b/src_c/key.c @@ -99,9 +99,8 @@ pg_scancodewrapper_subscript(pgScancodeWrapper *self, PyObject *item) static PyObject * pg_iter_raise(PyObject *self) { - PyErr_SetString(PyExc_TypeError, - "Iterating over key states is not supported"); - return NULL; + return RAISE(PyExc_TypeError, + "Iterating over key states is not supported"); } /** diff --git a/src_c/line.c b/src_c/line.c index ad25ed4866..f8838613ff 100644 --- a/src_c/line.c +++ b/src_c/line.c @@ -50,10 +50,10 @@ static int pg_line_init(pgLineObject *self, PyObject *args, PyObject *kwds) { if (!pgLine_FromObject(args, &self->line)) { - PyErr_SetString(PyExc_TypeError, - "Invalid line end points, expected 4 " - "numbers or 2 sequences of 2 numbers"); - return -1; + RAISERETURN(PyExc_TypeError, + "Invalid line end points, expected 4 numbers or 2 " + "sequences of 2 numbers", + -1); } return 0; } @@ -147,14 +147,12 @@ _line_scale_helper(pgLineBase *line, double factor, double origin) return 1; } else if (factor <= 0.0) { - PyErr_SetString(PyExc_ValueError, - "Can only scale by a positive non zero number"); - return 0; + RAISERETURN(PyExc_ValueError, + "Can only scale by a positive non zero number", 0); } if (origin < 0.0 || origin > 1.0) { - PyErr_SetString(PyExc_ValueError, "Origin must be between 0 and 1"); - return 0; + RAISERETURN(PyExc_ValueError, "Origin must be between 0 and 1", 0); } double ax = line->ax; @@ -291,8 +289,7 @@ pg_line_str(pgLineObject *self) self->line.name = val; \ return 0; \ } \ - PyErr_SetString(PyExc_TypeError, "Expected a number"); \ - return -1; \ + RAISERETURN(PyExc_TypeError, "Expected a number", -1); \ } __LINE_GETSET_NAME(ax) @@ -317,8 +314,7 @@ pg_line_seta(pgLineObject *self, PyObject *value, void *closure) self->line.ay = y; return 0; } - PyErr_SetString(PyExc_TypeError, "Expected a sequence of 2 numbers"); - return -1; + RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", -1); } static PyObject * @@ -337,8 +333,7 @@ pg_line_setb(pgLineObject *self, PyObject *value, void *closure) self->line.by = y; return 0; } - PyErr_SetString(PyExc_TypeError, "Expected a sequence of 2 numbers"); - return -1; + RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", -1); } static PyObject * diff --git a/src_c/mask.c b/src_c/mask.c index b7fef4d874..8131070fb3 100644 --- a/src_c/mask.c +++ b/src_c/mask.c @@ -2201,7 +2201,7 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs) if (Py_None != setsurfobj) { if (!pgSurface_Check(setsurfobj)) { - PyErr_SetString(PyExc_TypeError, "invalid setsurface argument"); + RAISE(PyExc_TypeError, "invalid setsurface argument"); goto to_surface_error; } @@ -2209,9 +2209,9 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs) if (0 == check_surface_pixel_format(surf, setsurf)) { /* Needs to have the same format settings as surface. */ - PyErr_SetString(PyExc_ValueError, - "setsurface needs to have same " - "bytesize/bitsize/alpha format as surface"); + RAISE(PyExc_ValueError, + "setsurface needs to have same bytesize/bitsize/alpha " + "format as surface"); goto to_surface_error; } else if ((setsurf->h <= 0) || (setsurf->w <= 0)) { @@ -2225,7 +2225,7 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs) if (Py_None != unsetsurfobj) { if (!pgSurface_Check(unsetsurfobj)) { - PyErr_SetString(PyExc_TypeError, "invalid unsetsurface argument"); + RAISE(PyExc_TypeError, "invalid unsetsurface argument"); goto to_surface_error; } @@ -2233,9 +2233,9 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs) if (0 == check_surface_pixel_format(surf, unsetsurf)) { /* Needs to have the same format settings as surface. */ - PyErr_SetString(PyExc_ValueError, - "unsetsurface needs to have same " - "bytesize/bitsize/alpha format as surface"); + RAISE(PyExc_ValueError, + "unsetsurface needs to have same bytesize/bitsize/alpha " + "format as surface"); goto to_surface_error; } else if ((unsetsurf->h <= 0) || (unsetsurf->w <= 0)) { @@ -2285,21 +2285,21 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs) y_dest = dest_rect->y; } else { - PyErr_SetString(PyExc_TypeError, "invalid dest argument"); + RAISE(PyExc_TypeError, "invalid dest argument"); goto to_surface_error; } } } if (!pgSurface_Lock((pgSurfaceObject *)surfobj)) { - PyErr_SetString(PyExc_RuntimeError, "cannot lock surface"); + RAISE(PyExc_RuntimeError, "cannot lock surface"); goto to_surface_error; } /* Only lock the setsurface if it is being used. * i.e. setsurf is non-NULL */ if (NULL != setsurf && !pgSurface_Lock((pgSurfaceObject *)setsurfobj)) { - PyErr_SetString(PyExc_RuntimeError, "cannot lock setsurface"); + RAISE(PyExc_RuntimeError, "cannot lock setsurface"); goto to_surface_error; } @@ -2307,7 +2307,7 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs) * i.e.. unsetsurf is non-NULL. */ if (NULL != unsetsurf && !pgSurface_Lock((pgSurfaceObject *)unsetsurfobj)) { - PyErr_SetString(PyExc_RuntimeError, "cannot lock unsetsurface"); + RAISE(PyExc_RuntimeError, "cannot lock unsetsurface"); goto to_surface_error; } @@ -2321,17 +2321,17 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs) if (NULL != unsetsurf && !pgSurface_Unlock((pgSurfaceObject *)unsetsurfobj)) { - PyErr_SetString(PyExc_RuntimeError, "cannot unlock unsetsurface"); + RAISE(PyExc_RuntimeError, "cannot unlock unsetsurface"); goto to_surface_error; } if (NULL != setsurf && !pgSurface_Unlock((pgSurfaceObject *)setsurfobj)) { - PyErr_SetString(PyExc_RuntimeError, "cannot unlock setsurface"); + RAISE(PyExc_RuntimeError, "cannot unlock setsurface"); goto to_surface_error; } if (!pgSurface_Unlock((pgSurfaceObject *)surfobj)) { - PyErr_SetString(PyExc_RuntimeError, "cannot unlock surface"); + RAISE(PyExc_RuntimeError, "cannot unlock surface"); goto to_surface_error; } @@ -2488,22 +2488,19 @@ mask_init(PyObject *self, PyObject *args, PyObject *kwargs) } if (!pg_TwoIntsFromObj(size, &w, &h)) { - PyErr_SetString(PyExc_TypeError, "size must be two numbers"); - return -1; + RAISERETURN(PyExc_TypeError, "size must be two numbers", -1); } if (w < 0 || h < 0) { - PyErr_SetString(PyExc_ValueError, - "cannot create mask with negative size"); - return -1; + RAISERETURN(PyExc_ValueError, "cannot create mask with negative size", + -1); } bitmask = bitmask_create(w, h); if (NULL == bitmask) { - PyErr_SetString(PyExc_MemoryError, - "cannot allocate memory for bitmask"); - return -1; + RAISERETURN(PyExc_MemoryError, "cannot allocate memory for bitmask", + -1); } if (fill) { diff --git a/src_c/math.c b/src_c/math.c index d4ebf9cbbb..825ea728df 100644 --- a/src_c/math.c +++ b/src_c/math.c @@ -386,8 +386,7 @@ PySequence_GetItem_AsDouble(PyObject *seq, Py_ssize_t index) item = PySequence_GetItem(seq, index); if (item == NULL) { - PyErr_SetString(PyExc_TypeError, "a sequence is expected"); - return -1; + RAISERETURN(PyExc_TypeError, "a sequence is expected", -1); } value = PyFloat_AsDouble(item); Py_DECREF(item); @@ -408,8 +407,7 @@ PySequence_AsVectorCoords(PyObject *seq, double *const coords, return 1; } if (!PySequence_Check(seq) || PySequence_Length(seq) != size) { - PyErr_SetString(PyExc_ValueError, "Sequence has the wrong length."); - return 0; + RAISERETURN(PyExc_ValueError, "Sequence has the wrong length.", 0); } for (i = 0; i < size; ++i) { @@ -439,10 +437,8 @@ pgVectorCompatible_Check(PyObject *obj, Py_ssize_t dim) } break; default: - PyErr_SetString( - PyExc_SystemError, - "Wrong internal call to pgVectorCompatible_Check."); - return 0; + RAISERETURN(PyExc_SystemError, + "Wrong internal call to pgVectorCompatible_Check.", 0); } if (!PySequence_Check(obj) || (PySequence_Length(obj) != dim)) { @@ -486,9 +482,8 @@ pg_VectorCoordsFromObj(PyObject *obj, Py_ssize_t dim, double *const coords) } break; default: - PyErr_SetString(PyExc_SystemError, - "Wrong internal call to pg_VectorCoordsFromObj."); - return 0; + RAISERETURN(PyExc_SystemError, + "Wrong internal call to pg_VectorCoordsFromObj.", 0); } if (!PySequence_Check(obj) || (PySequence_Length(obj) != dim)) { @@ -527,9 +522,8 @@ get_double_from_unicode_slice(PyObject *unicode_obj, Py_ssize_t idx1, PyObject *float_obj; PyObject *slice = PySequence_GetSlice(unicode_obj, idx1, idx2); if (slice == NULL) { - PyErr_SetString(PyExc_SystemError, - "internal error while converting str slice to float"); - return -1; + RAISERETURN(PyExc_SystemError, + "internal error while converting str slice to float", -1); } float_obj = PyFloat_FromString(slice); Py_DECREF(slice); @@ -772,9 +766,8 @@ vector_generic_math(PyObject *o1, PyObject *o2, int op) case OP_DIV | OP_ARG_NUMBER: case OP_DIV | OP_ARG_NUMBER | OP_INPLACE: if (tmp == 0.) { - PyErr_SetString(PyExc_ZeroDivisionError, "division by zero"); Py_DECREF(ret); - return NULL; + return RAISE(PyExc_ZeroDivisionError, "division by zero"); } tmp = 1. / tmp; for (i = 0; i < dim; i++) { @@ -784,9 +777,8 @@ vector_generic_math(PyObject *o1, PyObject *o2, int op) case OP_FLOOR_DIV | OP_ARG_NUMBER: case OP_FLOOR_DIV | OP_ARG_NUMBER | OP_INPLACE: if (tmp == 0.) { - PyErr_SetString(PyExc_ZeroDivisionError, "division by zero"); Py_DECREF(ret); - return NULL; + return RAISE(PyExc_ZeroDivisionError, "division by zero"); } tmp = 1. / tmp; for (i = 0; i < dim; i++) { @@ -1032,12 +1024,10 @@ static int vector_SetItem(pgVector *self, Py_ssize_t index, PyObject *value) { if (index < 0 || index >= self->dim) { - PyErr_SetString(PyExc_IndexError, "subscript out of range."); - return -1; + RAISERETURN(PyExc_IndexError, "subscript out of range.", -1); } if (value == NULL) { - PyErr_SetString(PyExc_TypeError, "item deletion is not supported"); - return -1; + RAISERETURN(PyExc_TypeError, "item deletion is not supported", -1); } self->coords[index] = PyFloat_AsDouble(value); @@ -1110,9 +1100,8 @@ vector_SetSlice(pgVector *self, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) double new_coords[VECTOR_MAX_SIZE]; if (v == NULL) { - PyErr_SetString(PyExc_TypeError, - "Vector object doesn't support item deletion"); - return -1; + RAISERETURN(PyExc_TypeError, + "Vector object doesn't support item deletion", -1); } if (ilow < 0) { @@ -1241,9 +1230,8 @@ vector_ass_subscript(pgVector *self, PyObject *key, PyObject *value) if (value == NULL) { /* delete slice not supported */ - PyErr_SetString(PyExc_TypeError, - "Deletion of vector components is not supported."); - return -1; + RAISERETURN(PyExc_TypeError, + "Deletion of vector components is not supported.", -1); } else { /* assign slice */ @@ -1283,19 +1271,16 @@ vector_set_component(pgVector *self, PyObject *value, int component) if (value == NULL) { switch (component) { case 0: { - PyErr_SetString(PyExc_TypeError, - "Cannot delete the x attribute"); - break; + RAISERETURN(PyExc_TypeError, "Cannot delete the x attribute", + -1); } case 1: { - PyErr_SetString(PyExc_TypeError, - "Cannot delete the y attribute"); - break; + RAISERETURN(PyExc_TypeError, "Cannot delete the y attribute", + -1); } case 2: { - PyErr_SetString(PyExc_TypeError, - "Cannot delete the z attribute"); - break; + RAISERETURN(PyExc_TypeError, "Cannot delete the z attribute", + -1); } default: { PyErr_BadInternalCall(); @@ -1672,10 +1657,8 @@ vector_slerp(pgVector *self, PyObject *args) } /* special case angle==180 and angle==-180 */ else if (fabs(fabs(angle) - M_PI) < self->epsilon) { - PyErr_SetString(PyExc_ValueError, - "SLERP with 180 degrees is undefined."); Py_DECREF(ret); - return NULL; + return RAISE(PyExc_ValueError, "SLERP with 180 degrees is undefined."); } else { f0 = ((length2 - length1) * t + length1) / sin(angle); @@ -1772,9 +1755,7 @@ _vector_reflect_helper(double *dst_coords, const double *src_coords, norm_length = _scalar_product(norm_coords, norm_coords, dim); if (norm_length < epsilon) { - PyErr_SetString(PyExc_ValueError, - "Normal must not be of length zero."); - return 0; + RAISERETURN(PyExc_ValueError, "Normal must not be of length zero.", 0); } if (norm_length != 1) { norm_length = sqrt(norm_length); @@ -1834,8 +1815,7 @@ _vector_distance_helper(pgVector *self, PyObject *other) double dx, dy; if (dim != otherv->dim) { - PyErr_SetString(PyExc_ValueError, "Vectors must be the same size"); - return -1; + RAISERETURN(PyExc_ValueError, "Vectors must be the same size", -1); } dx = otherv->coords[0] - self->coords[0]; @@ -1859,9 +1839,8 @@ _vector_distance_helper(pgVector *self, PyObject *other) if (PySequence_Fast_GET_SIZE(fast_seq) != dim) { Py_DECREF(fast_seq); - PyErr_SetString(PyExc_ValueError, - "Vector and sequence must be the same size"); - return -1; + RAISERETURN(PyExc_ValueError, + "Vector and sequence must be the same size", -1); } for (i = 0; i < dim; ++i) { @@ -1903,18 +1882,16 @@ static int _vector_check_snprintf_success(int return_code, int max_size) { if (return_code < 0) { - PyErr_SetString( - PyExc_SystemError, - "internal snprintf call went wrong! Please report " - "this to github.com/pygame-community/pygame-ce/issues"); - return 0; + RAISERETURN(PyExc_SystemError, + "internal snprintf call went wrong! Please report this to " + "github.com/pygame-community/pygame-ce/issues", + 0); } if (return_code >= max_size) { - PyErr_SetString( - PyExc_SystemError, - "Internal buffer too small for snprintf! Please report " - "this to github.com/pygame-community/pygame-ce/issues"); - return 0; + RAISERETURN(PyExc_SystemError, + "Internal buffer too small for snprintf! Please report " + "this to github.com/pygame-community/pygame-ce/issues", + 0); } return 1; } @@ -2005,10 +1982,9 @@ vector_project_onto(pgVector *self, PyObject *other) b_dot_b = _scalar_product(other_coords, other_coords, self->dim); if (b_dot_b < self->epsilon) { - PyErr_SetString(PyExc_ValueError, - "Cannot project onto a vector with zero length"); Py_DECREF(ret); - return NULL; + return RAISE(PyExc_ValueError, + "Cannot project onto a vector with zero length"); } factor = a_dot_b / b_dot_b; @@ -2193,19 +2169,17 @@ vector_setAttr_swizzle(pgVector *self, PyObject *attr_name, PyObject *val) } return 0; case SWIZZLE_ERR_DOUBLE_IDX: - PyErr_SetString(PyExc_AttributeError, - "Attribute assignment conflicts with swizzling."); - return -1; + RAISERETURN(PyExc_AttributeError, + "Attribute assignment conflicts with swizzling.", -1); case SWIZZLE_ERR_EXTRACTION_ERR: /* exception was set by PySequence_GetItem_AsDouble */ return -1; default: /* this should NEVER happen and means a bug in the code */ - PyErr_SetString( - PyExc_RuntimeError, - "Unhandled error in swizzle code. Please report " - "this bug to github.com/pygame-community/pygame-ce/issues"); - return -1; + RAISERETURN(PyExc_RuntimeError, + "Unhandled error in swizzle code. Please report this " + "bug to github.com/pygame-community/pygame-ce/issues", + -1); } } @@ -2214,9 +2188,7 @@ static Py_ssize_t vector_readbuffer(pgVector *self, Py_ssize_t segment, void **ptrptr) { if (segment != 0) { - PyErr_SetString(PyExc_SystemError, - "accessing non-existent vector segment"); - return -1; + RAISERETURN(PyExc_SystemError, "accessing non-existent vector segment", -1); } *ptrptr = self->coords; return self->dim; @@ -2226,9 +2198,7 @@ static Py_ssize_t vector_writebuffer(pgVector *self, Py_ssize_t segment, void **ptrptr) { if (segment != 0) { - PyErr_SetString(PyExc_SystemError, - "accessing non-existent vector segment"); - return -1; + RAISERETURN(PyExc_SystemError, "accessing non-existent vector segment", -1); } *ptrptr = self->coords; return self->dim; @@ -2312,9 +2282,8 @@ vector___round__(pgVector *self, PyObject *args) } } else { - PyErr_SetString(PyExc_TypeError, "Argument must be an integer"); Py_DECREF(ret); - return NULL; + return RAISE(PyExc_TypeError, "Argument must be an integer"); } return (PyObject *)ret; @@ -2399,11 +2368,10 @@ _vector2_set(pgVector *self, PyObject *xOrSequence, PyObject *y) /* success initialization */ return 0; error: - PyErr_SetString(PyExc_ValueError, - "Vector2 must be set with 2 real numbers, a " - "sequence of 2 real numbers, or " - "another Vector2 instance"); - return -1; + RAISERETURN(PyExc_ValueError, + "Vector2 must be set with 2 real numbers, a sequence of 2 " + "real numbers, or another Vector2 instance", + -1); } static int @@ -2469,12 +2437,11 @@ _vector2_rotate_helper(double *dst_coords, const double *src_coords, break; default: /* this should NEVER happen and means a bug in the code */ - PyErr_SetString( - PyExc_RuntimeError, - "Please report this bug in vector2_rotate_helper to " - "the developers at " - "github.com/pygame-community/pygame-ce/issues"); - return 0; + RAISERETURN(PyExc_RuntimeError, + "Please report this bug in vector2_rotate_helper " + "to the developers at " + "github.com/pygame-community/pygame-ce/issues", + 0); } } else { @@ -2594,10 +2561,9 @@ vector2_cross(pgVector *self, PyObject *other) } if (!pg_VectorCoordsFromObj(other, 2, other_coords)) { - PyErr_SetString( + return RAISE( PyExc_TypeError, "Incompatible vector argument: cannot calculate cross product"); - return NULL; } return PyFloat_FromDouble((self->coords[0] * other_coords[1]) - @@ -2617,10 +2583,9 @@ vector2_angle_to(pgVector *self, PyObject *other) double other_coords[2]; if (!pg_VectorCoordsFromObj(other, 2, other_coords)) { - PyErr_SetString( + return RAISE( PyExc_TypeError, "Incompatible vector argument: cannot calculate angle to"); - return NULL; } angle = (atan2(other_coords[1], other_coords[0]) - @@ -2855,11 +2820,10 @@ _vector3_set(pgVector *self, PyObject *xOrSequence, PyObject *y, PyObject *z) /* success initialization */ return 0; error: - PyErr_SetString(PyExc_ValueError, - "Vector3 must be set with 3 real numbers, a " - "sequence of 3 real numbers, or " - "another Vector3 instance"); - return -1; + RAISERETURN(PyExc_ValueError, + "Vector3 must be set with 3 real numbers, a sequence of 3 " + "real numbers, or another Vector3 instance", + -1); } static int @@ -2915,8 +2879,7 @@ _vector3_rotate_helper(double *dst_coords, const double *src_coords, /* Rotation axis may not be Zero */ if (axisLength2 < epsilon) { - PyErr_SetString(PyExc_ValueError, "Rotation Axis is to close to Zero"); - return 0; + RAISERETURN(PyExc_ValueError, "Rotation Axis is to close to Zero", 0); } /* normalize the axis */ @@ -2975,12 +2938,11 @@ _vector3_rotate_helper(double *dst_coords, const double *src_coords, break; default: /* this should NEVER happen and means a bug in the code */ - PyErr_SetString( - PyExc_RuntimeError, - "Please report this bug in vector3_rotate_helper to " - "the developers at " - "github.com/pygame-community/pygame-ce/issues"); - return 0; + RAISERETURN(PyExc_RuntimeError, + "Please report this bug in vector3_rotate_helper " + "to the developers at " + "github.com/pygame-community/pygame-ce/issues", + 0); } } else { @@ -3454,10 +3416,9 @@ vector3_cross(pgVector *self, PyObject *other) double other_coords[3]; if (!pg_VectorCoordsFromObj(other, 3, other_coords)) { - PyErr_SetString( + return RAISE( PyExc_TypeError, "Incompatible vector argument: cannot calculate cross product"); - return NULL; } self_coords = self->coords; @@ -3484,10 +3445,9 @@ vector3_angle_to(pgVector *self, PyObject *other) double other_coords[3]; if (!pg_VectorCoordsFromObj(other, 3, other_coords)) { - PyErr_SetString( + return RAISE( PyExc_TypeError, "Incompatible vector argument: cannot calculate angle to"); - return NULL; } squared_length1 = _scalar_product(self->coords, self->coords, self->dim); @@ -4016,10 +3976,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) case OP_DIV | OP_ARG_VECTOR: for (i = 0; i < vec->dim; i++) { if (other_coords[i] == 0) { - PyErr_SetString(PyExc_ZeroDivisionError, - "division by zero"); Py_DECREF(ret); - return NULL; + return RAISE(PyExc_ZeroDivisionError, "division by zero"); } ret->coords[i] = vec->coords[i] / other_coords[i]; } @@ -4027,10 +3985,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) case OP_DIV | OP_ARG_VECTOR | OP_ARG_REVERSE: for (i = 0; i < vec->dim; i++) { if (vec->coords[i] == 0) { - PyErr_SetString(PyExc_ZeroDivisionError, - "division by zero"); Py_DECREF(ret); - return NULL; + return RAISE(PyExc_ZeroDivisionError, "division by zero"); } ret->coords[i] = other_coords[i] / vec->coords[i]; } @@ -4038,10 +3994,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) case OP_DIV | OP_ARG_NUMBER | OP_ARG_REVERSE: for (i = 0; i < vec->dim; i++) { if (vec->coords[i] == 0) { - PyErr_SetString(PyExc_ZeroDivisionError, - "division by zero"); Py_DECREF(ret); - return NULL; + return RAISE(PyExc_ZeroDivisionError, "division by zero"); } ret->coords[i] = other_value / vec->coords[i]; } @@ -4049,10 +4003,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) case OP_FLOOR_DIV | OP_ARG_VECTOR: for (i = 0; i < vec->dim; i++) { if (other_coords[i] == 0) { - PyErr_SetString(PyExc_ZeroDivisionError, - "division by zero"); Py_DECREF(ret); - return NULL; + return RAISE(PyExc_ZeroDivisionError, "division by zero"); } ret->coords[i] = floor(vec->coords[i] / other_coords[i]); } @@ -4060,10 +4012,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) case OP_FLOOR_DIV | OP_ARG_VECTOR | OP_ARG_REVERSE: for (i = 0; i < vec->dim; i++) { if (vec->coords[i] == 0) { - PyErr_SetString(PyExc_ZeroDivisionError, - "division by zero"); Py_DECREF(ret); - return NULL; + return RAISE(PyExc_ZeroDivisionError, "division by zero"); } ret->coords[i] = floor(other_coords[i] / vec->coords[i]); } @@ -4071,10 +4021,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) case OP_FLOOR_DIV | OP_ARG_NUMBER | OP_ARG_REVERSE: for (i = 0; i < vec->dim; i++) { if (vec->coords[i] == 0) { - PyErr_SetString(PyExc_ZeroDivisionError, - "division by zero"); Py_DECREF(ret); - return NULL; + return RAISE(PyExc_ZeroDivisionError, "division by zero"); } ret->coords[i] = floor(other_value / vec->coords[i]); } @@ -4082,10 +4030,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) case OP_MOD | OP_ARG_VECTOR: for (i = 0; i < vec->dim; i++) { if (other_coords[i] == 0) { - PyErr_SetString(PyExc_ZeroDivisionError, - "division by zero"); Py_DECREF(ret); - return NULL; + return RAISE(PyExc_ZeroDivisionError, "division by zero"); } mod = fmod(vec->coords[i], other_coords[i]); /* note: checking mod*value < 0 is incorrect -- underflows @@ -4099,10 +4045,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) case OP_MOD | OP_ARG_VECTOR | OP_ARG_REVERSE: for (i = 0; i < vec->dim; i++) { if (vec->coords[i] == 0) { - PyErr_SetString(PyExc_ZeroDivisionError, - "division by zero"); Py_DECREF(ret); - return NULL; + return RAISE(PyExc_ZeroDivisionError, "division by zero"); } mod = fmod(other_coords[i], vec->coords[i]); /* note: see above */ @@ -4114,9 +4058,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) break; case OP_MOD | OP_ARG_NUMBER: if (other_value == 0) { - PyErr_SetString(PyExc_ZeroDivisionError, "division by zero"); Py_DECREF(ret); - return NULL; + return RAISE(PyExc_ZeroDivisionError, "division by zero"); } for (i = 0; i < vec->dim; i++) { mod = fmod(vec->coords[i], other_value); @@ -4130,10 +4073,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) case OP_MOD | OP_ARG_NUMBER | OP_ARG_REVERSE: for (i = 0; i < vec->dim; i++) { if (vec->coords[i] == 0) { - PyErr_SetString(PyExc_ZeroDivisionError, - "division by zero"); Py_DECREF(ret); - return NULL; + return RAISE(PyExc_ZeroDivisionError, "division by zero"); } mod = fmod(other_value, vec->coords[i]); /* note: see above */ @@ -4269,9 +4210,9 @@ vector_elementwiseproxy_pow(PyObject *baseObj, PyObject *expoObj, /* raising a negative number to a fractional power returns a * complex in python-3.x. we do not allow this. */ if (!PyErr_Occurred()) { - PyErr_SetString(PyExc_ValueError, - "negative number " - "cannot be raised to a fractional power"); + RAISE( + PyExc_ValueError, + "negative number cannot be raised to a fractional power"); } Py_XDECREF(result); Py_DECREF(ret); diff --git a/src_c/mixer.c b/src_c/mixer.c index 19872ec9ea..38df6d2900 100644 --- a/src_c/mixer.c +++ b/src_c/mixer.c @@ -34,12 +34,12 @@ #define PyBUF_HAS_FLAG(f, F) (((f) & (F)) == (F)) -#define CHECK_CHUNK_VALID(CHUNK, RET) \ - if ((CHUNK) == NULL) { \ - PyErr_SetString(PyExc_RuntimeError, \ - "__init__() was not called on Sound object so it " \ - "failed to setup correctly."); \ - return (RET); \ +#define CHECK_CHUNK_VALID(CHUNK, RET) \ + if ((CHUNK) == NULL) { \ + RAISERETURN(PyExc_RuntimeError, \ + "__init__() was not called on Sound object so it failed " \ + "to setup correctly.", \ + RET); \ } /* The SDL audio format constants are not defined for anything larger @@ -156,8 +156,7 @@ _format_view_to_audio(Py_buffer *view) } fstr_len = strlen(view->format); if (fstr_len < 1 || fstr_len > 2) { - PyErr_SetString(PyExc_ValueError, "Array has unsupported item format"); - return 0; + RAISERETURN(PyExc_ValueError, "Array has unsupported item format", 0); } if (fstr_len == 1) { format |= PG_SAMPLE_NATIVE_ENDIAN; @@ -184,9 +183,8 @@ _format_view_to_audio(Py_buffer *view) break; default: - PyErr_SetString(PyExc_ValueError, - "Array has unsupported item format"); - return 0; + RAISERETURN(PyExc_ValueError, + "Array has unsupported item format", 0); } ++index; } @@ -965,9 +963,8 @@ snd_getbuffer(PyObject *obj, Py_buffer *view, int flags) return -1; } if (channels != 1 && PyBUF_HAS_FLAG(flags, PyBUF_F_CONTIGUOUS)) { - PyErr_SetString(pgExc_BufferError, - "polyphonic sound is not Fortran contiguous"); - return -1; + RAISERETURN(pgExc_BufferError, + "polyphonic sound is not Fortran contiguous", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_ND)) { ndim = channels > 1 ? 2 : 1; @@ -1380,12 +1377,10 @@ static int _channel_init(pgChannelObject *self, int channelnum) { if (!SDL_WasInit(SDL_INIT_AUDIO)) { - PyErr_SetString(pgExc_SDLError, "mixer not initialized"); - return -1; + RAISERETURN(pgExc_SDLError, "mixer not initialized", -1); } if (channelnum < 0 || channelnum >= Mix_GroupCount(-1)) { - PyErr_SetString(PyExc_IndexError, "invalid channel index"); - return -1; + RAISERETURN(PyExc_IndexError, "invalid channel index", -1); } self->chan = channelnum; return 0; @@ -1677,29 +1672,25 @@ _chunk_from_array(void *buf, PG_sample_format_t view_format, int ndim, Py_ssize_t loop1, loop2, step1, step2, length, length2 = 0; if (!Mix_QuerySpec(&freq, &format, &channels)) { - PyErr_SetString(pgExc_SDLError, "mixer not initialized"); - return -1; + RAISERETURN(pgExc_SDLError, "mixer not initialized", -1); } /* Check for compatible values. */ if (channels == 1) { if (ndim != 1) { - PyErr_SetString(PyExc_ValueError, - "Array must be 1-dimensional for mono mixer"); - return -1; + RAISERETURN(PyExc_ValueError, + "Array must be 1-dimensional for mono mixer", -1); } } else { if (ndim != 2) { - PyErr_SetString(PyExc_ValueError, - "Array must be 2-dimensional for stereo mixer"); - return -1; + RAISERETURN(PyExc_ValueError, + "Array must be 2-dimensional for stereo mixer", -1); } if (shape[1] != channels) { - PyErr_SetString(PyExc_ValueError, - "Array depth must match number of mixer channels"); - return -1; + RAISERETURN(PyExc_ValueError, + "Array depth must match number of mixer channels", -1); } } itemsize = _format_itemsize(format); @@ -1829,8 +1820,7 @@ sound_init(PyObject *self, PyObject *arg, PyObject *kwarg) /* Similar to MIXER_INIT_CHECK(), but different return value. */ if (!SDL_WasInit(SDL_INIT_AUDIO)) { - PyErr_SetString(pgExc_SDLError, "mixer not initialized"); - return -1; + RAISERETURN(pgExc_SDLError, "mixer not initialized", -1); } /* Process arguments, returning cleaner error messages than @@ -1839,8 +1829,7 @@ sound_init(PyObject *self, PyObject *arg, PyObject *kwarg) if (arg != NULL && PyTuple_GET_SIZE(arg)) { if ((kwarg != NULL && PyDict_Size(kwarg)) || /* conditional and */ PyTuple_GET_SIZE(arg) != 1) { - PyErr_SetString(PyExc_TypeError, arg_cnt_err_msg); - return -1; + RAISERETURN(PyExc_TypeError, arg_cnt_err_msg, -1); } obj = PyTuple_GET_ITEM(arg, 0); @@ -1856,8 +1845,7 @@ sound_init(PyObject *self, PyObject *arg, PyObject *kwarg) } else if (kwarg != NULL) { if (PyDict_Size(kwarg) != 1) { - PyErr_SetString(PyExc_TypeError, arg_cnt_err_msg); - return -1; + RAISERETURN(PyExc_TypeError, arg_cnt_err_msg, -1); } if ((file = PyDict_GetItemString(kwarg, "file")) == NULL && (buffer = PyDict_GetItemString(kwarg, "buffer")) == NULL && @@ -1879,14 +1867,12 @@ sound_init(PyObject *self, PyObject *arg, PyObject *kwarg) return -1; } if (buffer != NULL && PyUnicode_Check(buffer)) { /* conditional and */ - PyErr_SetString(PyExc_TypeError, - "Unicode object not allowed as buffer object"); - return -1; + RAISERETURN(PyExc_TypeError, + "Unicode object not allowed as buffer object", -1); } } else { - PyErr_SetString(PyExc_TypeError, arg_cnt_err_msg); - return -1; + RAISERETURN(PyExc_TypeError, arg_cnt_err_msg, -1); } if (file != NULL) { @@ -1908,8 +1894,7 @@ sound_init(PyObject *self, PyObject *arg, PyObject *kwarg) #endif Py_END_ALLOW_THREADS; if (chunk == NULL) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } } @@ -1968,7 +1953,7 @@ sound_init(PyObject *self, PyObject *arg, PyObject *kwarg) if (chunk == NULL) { if (obj == NULL) { - PyErr_SetString(PyExc_TypeError, "Unrecognized argument"); + RAISERETURN(PyExc_TypeError, "Unrecognized argument", -1); } else { PyErr_Format(PyExc_TypeError, "Unrecognized argument (type %s)", diff --git a/src_c/newbuffer.c b/src_c/newbuffer.c index fdba603c88..4ec283660a 100644 --- a/src_c/newbuffer.c +++ b/src_c/newbuffer.c @@ -307,9 +307,8 @@ buffer_get_buffer(BufferObject *self, PyObject *args, PyObject *kwds) return 0; } if (bufobj_flags & BUFOBJ_FILLED) { - PyErr_SetString(PyExc_ValueError, - "The Py_buffer struct is already filled in"); - return 0; + RAISERETURN(PyExc_ValueError, + "The Py_buffer struct is already filled in", 0); } self->flags = BUFOBJ_MUTABLE & bufobj_flags; if (!self->view_p) { @@ -751,8 +750,7 @@ Buffer_New(Py_buffer *view_p, int filled, int preserve) static PyObject * mixin__get_buffer(PyObject *self, PyObject *args) { - PyErr_SetString(PyExc_NotImplementedError, "abstract method"); - return 0; + RAISERETURN(PyExc_NotImplementedError, "abstract method", 0); } static PyObject * @@ -788,9 +786,9 @@ mixin_getbuffer(PyObject *self, Py_buffer *view_p, int flags) Py_DECREF(py_rval); } else if (py_rval) { - PyErr_SetString(PyExc_ValueError, - "_get_buffer method return value was not None"); Py_DECREF(py_rval); + RAISE(PyExc_ValueError, + "_get_buffer method return value was not None"); } } return rval; diff --git a/src_c/pixelarray.c b/src_c/pixelarray.c index 9523b05e36..c57a6ce51a 100644 --- a/src_c/pixelarray.c +++ b/src_c/pixelarray.c @@ -301,10 +301,10 @@ _pxarray_new_internal(PyTypeObject *type, pgSurfaceObject *surface, if (!parent) { if (!surface) { Py_TYPE(self)->tp_free((PyObject *)self); - PyErr_SetString(PyExc_SystemError, - "Pygame internal error in _pxarray_new_internal: " - "no parent or surface."); - return 0; + RAISERETURN(PyExc_SystemError, + "Pygame internal error in _pxarray_new_internal: no " + "parent or surface.", + 0); } self->parent = 0; self->surface = surface; @@ -540,8 +540,7 @@ _pxarray_getbuffer(pgPixelArrayObject *self, Py_buffer *view_p, int flags) Py_ssize_t len; if (self->surface == NULL) { - PyErr_SetString(PyExc_ValueError, "Operation on closed PixelArray."); - return -1; + RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", -1); } itemsize = PG_SURF_BytesPerPixel(pgSurface_AsSurface(self->surface)); @@ -550,21 +549,18 @@ _pxarray_getbuffer(pgPixelArrayObject *self, Py_buffer *view_p, int flags) view_p->obj = 0; if (PyBUF_HAS_FLAG(flags, PyBUF_C_CONTIGUOUS) && !array_is_contiguous(self, 'C')) { - PyErr_SetString(pgExc_BufferError, - "this pixel array is not C contiguous"); - return -1; + RAISERETURN(pgExc_BufferError, "this pixel array is not C contiguous", + -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_F_CONTIGUOUS) && !array_is_contiguous(self, 'F')) { - PyErr_SetString(pgExc_BufferError, - "this pixel array is not F contiguous"); - return -1; + RAISERETURN(pgExc_BufferError, "this pixel array is not F contiguous", + -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_ANY_CONTIGUOUS) && !array_is_contiguous(self, 'A')) { - PyErr_SetString(pgExc_BufferError, - "this pixel array is not contiguous"); - return -1; + RAISERETURN(pgExc_BufferError, "this pixel array is not contiguous", + -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_ND)) { shape = self->shape; @@ -572,19 +568,17 @@ _pxarray_getbuffer(pgPixelArrayObject *self, Py_buffer *view_p, int flags) strides = self->strides; } else if (!array_is_contiguous(self, 'C')) { - PyErr_SetString( - pgExc_BufferError, - "this pixel array is not contiguous: need strides"); - return -1; + RAISERETURN(pgExc_BufferError, + "this pixel array is not contiguous: need strides", + -1); } } else if (array_is_contiguous(self, 'F')) { ndim = 0; } else { - PyErr_SetString(pgExc_BufferError, - "this pixel array is not C contiguous: need strides"); - return -1; + RAISERETURN(pgExc_BufferError, + "this pixel array is not C contiguous: need strides", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_FORMAT)) { /* Find the appropriate format for given pixel size */ @@ -931,8 +925,7 @@ _array_assign_array(pgPixelArrayObject *array, Py_ssize_t low, Py_ssize_t high, int sizes_match = 0; if (array->surface == NULL) { - PyErr_SetString(PyExc_ValueError, "Operation on closed PixelArray."); - return -1; + RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", -1); } surf = pgSurface_AsSurface(array->surface); @@ -959,8 +952,7 @@ _array_assign_array(pgPixelArrayObject *array, Py_ssize_t low, Py_ssize_t high, } if (!sizes_match) { /* Bounds do not match. */ - PyErr_SetString(PyExc_ValueError, "array sizes do not match"); - return -1; + RAISERETURN(PyExc_ValueError, "array sizes do not match", -1); } bpp = PG_SURF_BytesPerPixel(surf); @@ -968,15 +960,13 @@ _array_assign_array(pgPixelArrayObject *array, Py_ssize_t low, Py_ssize_t high, if (val_bpp != bpp) { /* bpp do not match. We cannot guarantee that the padding and columns * would be set correctly. */ - PyErr_SetString(PyExc_ValueError, "bit depths do not match"); - return -1; + RAISERETURN(PyExc_ValueError, "bit depths do not match", -1); } PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf); PG_PixelFormat *val_surf_format = PG_GetSurfaceFormat(val_surf); if (surf_format == NULL || val_surf_format == NULL) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } /* If we reassign the same array, we need to copy the pixels @@ -1103,14 +1093,12 @@ _array_assign_sequence(pgPixelArrayObject *array, Py_ssize_t low, PyObject *item; if (val_dim0 != dim0) { - PyErr_SetString(PyExc_ValueError, "sequence size mismatch"); - return -1; + RAISERETURN(PyExc_ValueError, "sequence size mismatch", -1); } PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf); if (surf_format == NULL) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } bpp = PG_FORMAT_BytesPerPixel(surf_format); @@ -1223,8 +1211,7 @@ _array_assign_slice(pgPixelArrayObject *array, Py_ssize_t low, Py_ssize_t high, PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf); if (surf_format == NULL) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } bpp = PG_FORMAT_BytesPerPixel(surf_format); @@ -1340,10 +1327,9 @@ _pxarray_ass_item(pgPixelArrayObject *array, Py_ssize_t index, PyObject *value) return -1; } if (!pgPixelArrayObject_Check(tmparray)) { - PyErr_SetString( - PyExc_ValueError, - "cannot assign a pixel sequence to a single pixel"); - return -1; + RAISERETURN(PyExc_ValueError, + "cannot assign a pixel sequence to a single pixel", + -1); } retval = _array_assign_sequence(tmparray, 0, tmparray->shape[0], value); @@ -1358,13 +1344,11 @@ _pxarray_ass_item(pgPixelArrayObject *array, Py_ssize_t index, PyObject *value) if (index < 0) { index += dim0; if (index < 0) { - PyErr_SetString(PyExc_IndexError, "array index out of range"); - return -1; + RAISERETURN(PyExc_IndexError, "array index out of range", -1); } } if (index >= dim0) { - PyErr_SetString(PyExc_IndexError, "array index out of range"); - return -1; + RAISERETURN(PyExc_IndexError, "array index out of range", -1); } pixels += index * stride0; @@ -1375,8 +1359,7 @@ _pxarray_ass_item(pgPixelArrayObject *array, Py_ssize_t index, PyObject *value) PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf); if (surf_format == NULL) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } Py_BEGIN_ALLOW_THREADS; @@ -1588,8 +1571,7 @@ _get_subslice(PyObject *op, Py_ssize_t length, Py_ssize_t *start, *start += length; } if (*start >= length || *start < 0) { - PyErr_SetString(PyExc_IndexError, "invalid index"); - return -1; + RAISERETURN(PyExc_IndexError, "invalid index", -1); } *stop = (*start) + 1; *step = 0; @@ -1735,9 +1717,8 @@ _pxarray_ass_subscript(pgPixelArrayObject *array, PyObject *op, int retval; if (size > 2 || (size == 2 && !dim1)) { - PyErr_SetString(PyExc_IndexError, - "too many indices for the array"); - return -1; + RAISERETURN(PyExc_IndexError, "too many indices for the array", + -1); } obj = PyTuple_GET_ITEM(op, 0); @@ -1832,9 +1813,8 @@ _pxarray_ass_subscript(pgPixelArrayObject *array, PyObject *op, return -1; } if (slicelen < 0) { - PyErr_SetString(PyExc_IndexError, - "Unable to handle negative slice"); - return -1; + RAISERETURN(PyExc_IndexError, "Unable to handle negative slice", + -1); } if (slicelen == 0) { return 0; @@ -1857,10 +1837,8 @@ _pxarray_ass_subscript(pgPixelArrayObject *array, PyObject *op, } return _pxarray_ass_item(array, i, value); } - - PyErr_SetString(PyExc_TypeError, - "index must be an integer, sequence or slice"); - return -1; + RAISERETURN(PyExc_TypeError, "index must be an integer, sequence or slice", + -1); } /**** C API interfaces ****/ diff --git a/src_c/pixelarray_methods.c b/src_c/pixelarray_methods.c index 6eb031d637..cea13f552d 100644 --- a/src_c/pixelarray_methods.c +++ b/src_c/pixelarray_methods.c @@ -257,12 +257,11 @@ _get_weights(PyObject *weights, float *wr, float *wg, float *wb) } if (!PySequence_Check(weights)) { - PyErr_SetString(PyExc_TypeError, "weights must be a sequence"); + RAISE(PyExc_TypeError, "weights must be a sequence"); success = 0; } else if (PySequence_Size(weights) < 3) { - PyErr_SetString(PyExc_TypeError, - "weights must contain at least 3 values"); + RAISE(PyExc_TypeError, "weights must contain at least 3 values"); success = 0; } else { @@ -302,7 +301,7 @@ _get_weights(PyObject *weights, float *wr, float *wg, float *wb) } } else { - PyErr_SetString(PyExc_TypeError, "invalid weights"); + RAISE(PyExc_TypeError, "invalid weights"); success = 0; } Py_XDECREF(item); @@ -320,9 +319,8 @@ _get_weights(PyObject *weights, float *wr, float *wg, float *wb) *wb = rgb[2]; if ((*wr < 0 || *wg < 0 || *wb < 0) || (*wr == 0 && *wg == 0 && *wb == 0)) { - PyErr_SetString(PyExc_ValueError, - "weights must be positive and greater than 0"); - return 0; + RAISERETURN(PyExc_ValueError, + "weights must be positive and greater than 0", 0); } /* Build the average weight values. */ sum = *wr + *wg + *wb; @@ -810,8 +808,7 @@ _compare(pgPixelArrayObject *array, PyObject *args, PyObject *kwds) if (other_array->shape[0] != dim0 || other_array->shape[1] != dim1) { /* Bounds do not match. */ - PyErr_SetString(PyExc_ValueError, "array sizes do not match"); - return 0; + RAISERETURN(PyExc_ValueError, "array sizes do not match", 0); } other_surf = pgSurface_AsSurface(other_array->surface); @@ -830,8 +827,7 @@ _compare(pgPixelArrayObject *array, PyObject *args, PyObject *kwds) if (PG_FORMAT_BytesPerPixel(other_format) != bpp) { /* bpp do not match. We cannot guarantee that the padding and co * would be set correctly. */ - PyErr_SetString(PyExc_ValueError, "bit depths do not match"); - return 0; + RAISERETURN(PyExc_ValueError, "bit depths do not match", 0); } other_stride0 = other_array->strides[0]; diff --git a/src_c/pixelcopy.c b/src_c/pixelcopy.c index 0304d7ebce..b119630f75 100644 --- a/src_c/pixelcopy.c +++ b/src_c/pixelcopy.c @@ -93,8 +93,7 @@ _validate_view_format(const char *format) /* default: unrecognized character; raise error later */ } if (format[i] != '\0') { - PyErr_SetString(PyExc_ValueError, "Unsupported array item type"); - return -1; + RAISERETURN(PyExc_ValueError, "Unsupported array item type", -1); } return 0; @@ -125,17 +124,15 @@ _view_kind(PyObject *obj, void *view_kind_vptr) if (PyUnicode_Check(obj)) { if (PyUnicode_GET_LENGTH(obj) != 1) { - PyErr_SetString(PyExc_TypeError, - "expected a length 1 string for argument 3"); - return 0; + RAISERETURN(PyExc_TypeError, + "expected a length 1 string for argument 3", 0); } ch = PyUnicode_READ_CHAR(obj, 0); } else if (PyBytes_Check(obj)) { if (PyBytes_GET_SIZE(obj) != 1) { - PyErr_SetString(PyExc_TypeError, - "expected a length 1 string for argument 3"); - return 0; + RAISERETURN(PyExc_TypeError, + "expected a length 1 string for argument 3", 0); } ch = *PyBytes_AS_STRING(obj); } @@ -288,13 +285,11 @@ _copy_colorplane(Py_buffer *view_p, SDL_Surface *surf, return -1; } if (!PG_GetSurfaceBlendMode(surf, &mode)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } if (!PG_GetSurfaceDetails(surf, &format, &palette)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } /* Select appropriate color plane element within the pixel */ @@ -423,8 +418,7 @@ _copy_unmapped(Py_buffer *view_p, SDL_Surface *surf) SDL_Palette *palette; if (!PG_GetSurfaceDetails(surf, &format, &palette)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } for (x = 0; x < w; ++x) { @@ -826,11 +820,10 @@ surface_to_array(PyObject *self, PyObject *args, PyObject *kwds) } else if (view_p->ndim == 3) { if (view_kind != VIEWKIND_RGB) { - PyErr_SetString(PyExc_ValueError, - "color planes only supported for 2d targets"); pgBuffer_Release(&pg_view); pgSurface_Unlock(surfobj); - return 0; + RAISERETURN(PyExc_ValueError, + "color planes only supported for 2d targets", 0); } if (_copy_unmapped(view_p, surf)) { pgBuffer_Release(&pg_view); @@ -911,7 +904,7 @@ map_array(PyObject *self, PyObject *args) tar_view_p = (Py_buffer *)&tar_pg_view; tar = (Uint8 *)tar_view_p->buf; if (_validate_view_format(tar_view_p->format)) { - PyErr_SetString(PyExc_ValueError, "expected an integer target array"); + RAISE(PyExc_ValueError, "expected an integer target array"); goto fail; } ndim = tar_view_p->ndim; @@ -919,7 +912,7 @@ map_array(PyObject *self, PyObject *args) shape = tar_view_p->shape; tar_strides = tar_view_p->strides; if (ndim < 1) { - PyErr_SetString(PyExc_ValueError, "target array must be at least 1D"); + RAISE(PyExc_ValueError, "target array must be at least 1D"); goto fail; } if (ndim > PIXELCOPY_MAX_DIM) { @@ -938,7 +931,7 @@ map_array(PyObject *self, PyObject *args) src = (Uint8 *)src_view_p->buf; src_ndim = src_view_p->ndim; if (src_ndim < 1) { - PyErr_SetString(PyExc_ValueError, "source array must be at least 1D"); + RAISE(PyExc_ValueError, "source array must be at least 1D"); goto fail; } if (src_view_p->shape[src_ndim - 1] != 3) { @@ -992,8 +985,8 @@ map_array(PyObject *self, PyObject *args) pix_bytesize = PG_FORMAT_BytesPerPixel(format); if (tar_itemsize < pix_bytesize) { - PyErr_SetString(PyExc_ValueError, - "target array itemsize is too small for pixel format"); + RAISE(PyExc_ValueError, + "target array itemsize is too small for pixel format"); goto fail; } src_green = (int)src_view_p->strides[src_ndim - 1]; @@ -1093,9 +1086,8 @@ map_array(PyObject *self, PyObject *args) --dim; if (dim < 0) { /* Should not happen, but handle case for extra safety */ - PyErr_SetString( - PyExc_RuntimeError, - "internal pygame error in pixelcopy map_array"); + RAISE(PyExc_RuntimeError, + "internal pygame error in pixelcopy map_array"); goto fail; } tar += tar_advances[dim]; @@ -1207,9 +1199,8 @@ make_surface(PyObject *self, PyObject *arg) * */ if (!PG_SetPaletteColors(palette, default_palette_colors, 0, default_palette_size - 1)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(surf); - return 0; + RAISERETURN(pgExc_SDLError, SDL_GetError(), 0); } } surfobj = pgSurface_New(surf); diff --git a/src_c/rect.c b/src_c/rect.c index 2817fc20f3..15496f2aaf 100644 --- a/src_c/rect.c +++ b/src_c/rect.c @@ -323,9 +323,8 @@ four_ints_from_obj(PyObject *obj, int *val1, int *val2, int *val3, int *val4) Py_DECREF(item); if (!result) { - PyErr_SetString(PyExc_TypeError, - "number pair expected for first argument"); - return 0; + RAISERETURN(PyExc_TypeError, + "number pair expected for first argument", 0); } /* Get the other end of the line. */ @@ -339,34 +338,29 @@ four_ints_from_obj(PyObject *obj, int *val1, int *val2, int *val3, int *val4) Py_DECREF(item); if (!result) { - PyErr_SetString(PyExc_TypeError, - "number pair expected for second argument"); - return 0; + RAISERETURN(PyExc_TypeError, + "number pair expected for second argument", 0); } } else if (length == 4) { if (!pg_IntFromObjIndex(obj, 0, val1)) { - PyErr_SetString(PyExc_TypeError, - "number expected for first argument"); - return 0; + RAISERETURN(PyExc_TypeError, "number expected for first argument", + 0); } if (!pg_IntFromObjIndex(obj, 1, val2)) { - PyErr_SetString(PyExc_TypeError, - "number expected for second argument"); - return 0; + RAISERETURN(PyExc_TypeError, "number expected for second argument", + 0); } if (!pg_IntFromObjIndex(obj, 2, val3)) { - PyErr_SetString(PyExc_TypeError, - "number expected for third argument"); - return 0; + RAISERETURN(PyExc_TypeError, "number expected for third argument", + 0); } if (!pg_IntFromObjIndex(obj, 3, val4)) { - PyErr_SetString(PyExc_TypeError, - "number expected for fourth argument"); - return 0; + RAISERETURN(PyExc_TypeError, "number expected for fourth argument", + 0); } } else { @@ -402,9 +396,8 @@ four_floats_from_obj(PyObject *obj, float *val1, float *val2, float *val3, Py_DECREF(item); if (!result) { - PyErr_SetString(PyExc_TypeError, - "number pair expected for first argument"); - return 0; + RAISERETURN(PyExc_TypeError, + "number pair expected for first argument", 0); } /* Get the other end of the line. */ @@ -418,34 +411,29 @@ four_floats_from_obj(PyObject *obj, float *val1, float *val2, float *val3, Py_DECREF(item); if (!result) { - PyErr_SetString(PyExc_TypeError, - "number pair expected for second argument"); - return 0; + RAISERETURN(PyExc_TypeError, + "number pair expected for second argument", 0); } } else if (length == 4) { if (!pg_FloatFromObjIndex(obj, 0, val1)) { - PyErr_SetString(PyExc_TypeError, - "number expected for first argument"); - return 0; + RAISERETURN(PyExc_TypeError, "number expected for first argument", + 0); } if (!pg_FloatFromObjIndex(obj, 1, val2)) { - PyErr_SetString(PyExc_TypeError, - "number expected for second argument"); - return 0; + RAISERETURN(PyExc_TypeError, "number expected for second argument", + 0); } if (!pg_FloatFromObjIndex(obj, 2, val3)) { - PyErr_SetString(PyExc_TypeError, - "number expected for third argument"); - return 0; + RAISERETURN(PyExc_TypeError, "number expected for third argument", + 0); } if (!pg_FloatFromObjIndex(obj, 3, val4)) { - PyErr_SetString(PyExc_TypeError, - "number expected for fourth argument"); - return 0; + RAISERETURN(PyExc_TypeError, "number expected for fourth argument", + 0); } } else { diff --git a/src_c/rect_impl.h b/src_c/rect_impl.h index cc611f949f..09bb3e1db1 100644 --- a/src_c/rect_impl.h +++ b/src_c/rect_impl.h @@ -878,8 +878,7 @@ RectExport_init(RectObject *self, PyObject *args, PyObject *kwds) InnerRect *argrect, temp; if (!(argrect = RectFromObject(args, &temp))) { - PyErr_SetString(PyExc_TypeError, "Argument must be rect style object"); - return -1; + RAISERETURN(PyExc_TypeError, "Argument must be rect style object", -1); } self->r = *argrect; @@ -1995,9 +1994,9 @@ RectExport_containsSeq(RectObject *self, PyObject *arg) } int ret = RectExport_contains_internal(self, (PyObject *const *)&arg, 1); if (ret < 0) { - PyErr_SetString(PyExc_TypeError, "'in <" ObjectName - ">' requires rect style object" - " or int as left operand"); + RAISE(PyExc_TypeError, + "'in <" ObjectName + ">' requires rect style object or int as left operand"); } return ret; } @@ -2148,8 +2147,7 @@ RectExport_assItem(RectObject *self, Py_ssize_t i, PyObject *v) PrimitiveType *data = (PrimitiveType *)&self->r; if (!v) { - PyErr_SetString(PyExc_TypeError, "item deletion is not supported"); - return -1; + RAISERETURN(PyExc_TypeError, "item deletion is not supported", -1); } if (i < 0 || i > 3) { @@ -2157,13 +2155,11 @@ RectExport_assItem(RectObject *self, Py_ssize_t i, PyObject *v) i += 4; } else { - PyErr_SetString(PyExc_IndexError, "Invalid rect Index"); - return -1; + RAISERETURN(PyExc_IndexError, "Invalid rect Index", -1); } } if (!PrimitiveFromObj(v, &val)) { - PyErr_SetString(PyExc_TypeError, "Must assign numeric values"); - return -1; + RAISERETURN(PyExc_TypeError, "Must assign numeric values", -1); } data[i] = val; return 0; @@ -2220,8 +2216,7 @@ static int RectExport_assSubscript(RectObject *self, PyObject *op, PyObject *value) { if (!value) { - PyErr_SetString(PyExc_TypeError, "item deletion is not supported"); - return -1; + RAISERETURN(PyExc_TypeError, "item deletion is not supported", -1); } if (PyIndex_Check(op)) { Py_ssize_t i = PyNumber_AsSsize_t(op, NULL); @@ -2253,8 +2248,7 @@ RectExport_assSubscript(RectObject *self, PyObject *op, PyObject *value) Py_ssize_t i; if (PySequence_Size(value) != 4) { - PyErr_SetString(PyExc_TypeError, "Expect a length 4 sequence"); - return -1; + RAISERETURN(PyExc_TypeError, "Expect a length 4 sequence", -1); } for (i = 0; i < 4; ++i) { item = PySequence_ITEM(value, i); @@ -2270,9 +2264,8 @@ RectExport_assSubscript(RectObject *self, PyObject *op, PyObject *value) self->r.h = values[3]; } else { - PyErr_SetString(PyExc_TypeError, - "Expected an integer or sequence"); - return -1; + RAISERETURN(PyExc_TypeError, "Expected an integer or sequence", + -1); } } else if (PySlice_Check(op)) { @@ -2316,14 +2309,12 @@ RectExport_assSubscript(RectObject *self, PyObject *op, PyObject *value) } } else { - PyErr_SetString(PyExc_TypeError, - "Expected an integer or sequence"); - return -1; + RAISERETURN(PyExc_TypeError, "Expected an integer or sequence", + -1); } } else { - PyErr_SetString(PyExc_TypeError, "Invalid Rect slice"); - return -1; + RAISERETURN(PyExc_TypeError, "Invalid Rect slice", -1); } return 0; } @@ -2402,13 +2393,11 @@ RectExport_setwidth(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!PrimitiveFromObj(value, &val1)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.w = val1; return 0; @@ -2428,13 +2417,11 @@ RectExport_setheight(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!PrimitiveFromObj(value, &val1)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.h = val1; return 0; @@ -2454,13 +2441,11 @@ RectExport_settop(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!PrimitiveFromObj(value, &val1)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.y = val1; return 0; @@ -2480,13 +2465,11 @@ RectExport_setleft(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!PrimitiveFromObj(value, &val1)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1; return 0; @@ -2506,13 +2489,11 @@ RectExport_setright(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!PrimitiveFromObj(value, &val1)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1 - self->r.w; return 0; @@ -2532,13 +2513,11 @@ RectExport_setbottom(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!PrimitiveFromObj(value, &val1)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.y = val1 - self->r.h; return 0; @@ -2558,13 +2537,11 @@ RectExport_setcenterx(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!PrimitiveFromObj(value, &val1)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1 - (self->r.w / 2); return 0; @@ -2584,13 +2561,11 @@ RectExport_setcentery(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!PrimitiveFromObj(value, &val1)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.y = val1 - (self->r.h / 2); return 0; @@ -2610,13 +2585,11 @@ RectExport_settopleft(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1; self->r.y = val2; @@ -2637,13 +2610,11 @@ RectExport_settopright(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1 - self->r.w; self->r.y = val2; @@ -2664,13 +2635,11 @@ RectExport_setbottomleft(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1; self->r.y = val2 - self->r.h; @@ -2692,13 +2661,11 @@ RectExport_setbottomright(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1 - self->r.w; self->r.y = val2 - self->r.h; @@ -2719,13 +2686,11 @@ RectExport_setmidtop(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x += val1 - (self->r.x + (self->r.w / 2)); self->r.y = val2; @@ -2746,13 +2711,11 @@ RectExport_setmidleft(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1; self->r.y += val2 - (self->r.y + (self->r.h / 2)); @@ -2774,13 +2737,11 @@ RectExport_setmidbottom(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x += val1 - (self->r.x + (self->r.w / 2)); self->r.y = val2 - self->r.h; @@ -2802,13 +2763,11 @@ RectExport_setmidright(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1 - self->r.w; self->r.y += val2 - (self->r.y + (self->r.h / 2)); @@ -2830,13 +2789,11 @@ RectExport_setcenter(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x += val1 - (self->r.x + (self->r.w / 2)); self->r.y += val2 - (self->r.y + (self->r.h / 2)); @@ -2857,13 +2814,11 @@ RectExport_setsize(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - PyErr_SetString(PyExc_AttributeError, "can't delete attribute"); - return -1; + RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - PyErr_SetString(PyExc_TypeError, "invalid rect assignment"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.w = val1; self->r.h = val2; diff --git a/src_c/render.c b/src_c/render.c index d237ced3e7..7985d35b00 100644 --- a/src_c/render.c +++ b/src_c/render.c @@ -523,8 +523,7 @@ renderer_init(pgRendererObject *self, PyObject *args, PyObject *kwargs) } renderer = SDL_CreateRenderer(window->_win, index, flags); if (!renderer) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } self->renderer = renderer; self->window = window; diff --git a/src_c/rwobject.c b/src_c/rwobject.c index 61e05b9428..94def6f205 100644 --- a/src_c/rwobject.c +++ b/src_c/rwobject.c @@ -86,17 +86,15 @@ _pg_is_exception_class(PyObject *obj, void **optr) !PyObject_IsSubclass(obj, PyExc_BaseException)) { oname = PyObject_Str(obj); if (oname == NULL) { - PyErr_SetString(PyExc_TypeError, - "invalid exception class argument"); - return 0; + RAISERETURN(PyExc_TypeError, "invalid exception class argument", + 0); } tmp = PyUnicode_AsEncodedString(oname, "ascii", "replace"); Py_DECREF(oname); if (tmp == NULL) { - PyErr_SetString(PyExc_TypeError, - "invalid exception class argument"); - return 0; + RAISERETURN(PyExc_TypeError, "invalid exception class argument", + 0); } oname = tmp; @@ -131,8 +129,7 @@ fetch_object_methods(pgRWHelper *helper, PyObject *obj) } } if (!helper->read && !helper->write) { - PyErr_SetString(PyExc_TypeError, "not a file object"); - return -1; + RAISERETURN(PyExc_TypeError, "not a file object", -1); } if (PyObject_HasAttrString(obj, "seek")) { helper->seek = PyObject_GetAttrString(obj, "seek"); @@ -212,7 +209,7 @@ pg_EncodeString(PyObject *obj, const char *encoding, const char *errors, Py_DECREF(exc_type); Py_XDECREF(exc_trace); if (exc_value == NULL) { - PyErr_SetString(eclass, "Unicode encoding error"); + RAISE(eclass, "Unicode encoding error"); } else { str = PyObject_Str(exc_value); @@ -272,7 +269,7 @@ pg_EncodeFilePath(PyObject *obj, PyObject *eclass) /* End code replacement section */ if (obj == NULL) { - PyErr_SetString(PyExc_SyntaxError, "Forwarded exception"); + RAISE(PyExc_SyntaxError, "Forwarded exception"); } PyObject *result = @@ -699,7 +696,7 @@ _rwops_from_pystr(PyObject *obj, char **extptr) #else if (SDL_RWclose(rw) < 0) { #endif - PyErr_SetString(PyExc_IOError, SDL_GetError()); + RAISE(PyExc_IOError, SDL_GetError()); } return (SDL_RWops *)PyErr_NoMemory(); } @@ -816,7 +813,7 @@ pg_encode_string(PyObject *self, PyObject *args, PyObject *keywds) } if (obj == NULL) { - PyErr_SetString(PyExc_SyntaxError, "Forwarded exception"); + RAISE(PyExc_SyntaxError, "Forwarded exception"); } return pg_EncodeString(obj, encoding, errors, eclass); } diff --git a/src_c/scrap.h b/src_c/scrap.h index 25d979a70c..39537cbbf5 100644 --- a/src_c/scrap.h +++ b/src_c/scrap.h @@ -27,6 +27,7 @@ #undef _POSIX_C_SOURCE #endif +#include "pygame.h" #include /* Handle clipboard text and data in arbitrary formats */ @@ -54,10 +55,10 @@ typedef enum { /** * Macro for initialization checks. */ -#define PYGAME_SCRAP_INIT_CHECK() \ - if (!pygame_scrap_initialized()) \ - return (PyErr_SetString(pgExc_SDLError, "scrap system not initialized."), \ - NULL) +#define PYGAME_SCRAP_INIT_CHECK() \ + if (!pygame_scrap_initialized()) { \ + return RAISE(pgExc_SDLError, "scrap system not initialized."); \ + } /** * \brief Checks, whether the pygame scrap module was initialized. diff --git a/src_c/scrap_sdl2.c b/src_c/scrap_sdl2.c index 5de1f4cdbc..bbb6f3bdb3 100644 --- a/src_c/scrap_sdl2.c +++ b/src_c/scrap_sdl2.c @@ -1,3 +1,4 @@ +#include "pygame.h" #ifdef PG_SDL3 #include #include @@ -44,8 +45,7 @@ char ** pygame_scrap_get_types(void) { if (!pygame_scrap_initialized()) { - PyErr_SetString(pgExc_SDLError, "scrap system not initialized."); - return NULL; + return RAISE(pgExc_SDLError, "scrap system not initialized."); } return pygame_scrap_types; @@ -78,8 +78,7 @@ int pygame_scrap_put(char *type, Py_ssize_t srclen, char *src) { if (!pygame_scrap_initialized()) { - PyErr_SetString(pgExc_SDLError, "scrap system not initialized."); - return 0; + RAISERETURN(pgExc_SDLError, "scrap system not initialized.", 0); } if (strcmp(type, pygame_scrap_plaintext_type) == 0) { diff --git a/src_c/scrap_win.c b/src_c/scrap_win.c index 96391c3d97..971c51e6b0 100644 --- a/src_c/scrap_win.c +++ b/src_c/scrap_win.c @@ -18,6 +18,7 @@ License along with this library; if not, write to the Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include "scrap.h" #include #if !defined(CF_DIBV5) @@ -201,8 +202,7 @@ int pygame_scrap_lost(void) { if (!pygame_scrap_initialized()) { - PyErr_SetString(pgExc_SDLError, "scrap system not initialized."); - return 0; + RAISERETURN(pgExc_SDLError, "scrap system not initialized.", 0); } return (GetClipboardOwner() != window_handle); } @@ -215,8 +215,7 @@ pygame_scrap_put(char *type, Py_ssize_t srclen, char *src) HANDLE hMem; if (!pygame_scrap_initialized()) { - PyErr_SetString(pgExc_SDLError, "scrap system not initialized."); - return 0; + RAISERETURN(pgExc_SDLError, "scrap system not initialized.", 0); } format = _convert_internal_type(type); diff --git a/src_c/surface.c b/src_c/surface.c index 80cc21119f..bd15755f75 100644 --- a/src_c/surface.c +++ b/src_c/surface.c @@ -348,8 +348,7 @@ static int pgSurface_SetSurface(pgSurfaceObject *self, SDL_Surface *s, int owner) { if (!s) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } if (s == self->surf) { self->owner = owner; @@ -500,20 +499,17 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) if (PySequence_Check(size) && PySequence_Length(size) == 2) { if ((!pg_IntFromObjIndex(size, 0, &width)) || (!pg_IntFromObjIndex(size, 1, &height))) { - PyErr_SetString(PyExc_ValueError, - "size needs to be (number width, number height)"); - return -1; + RAISERETURN(PyExc_ValueError, + "size needs to be (number width, number height)", -1); } } else { - PyErr_SetString(PyExc_ValueError, - "size needs to be (number width, number height)"); - return -1; + RAISERETURN(PyExc_ValueError, + "size needs to be (number width, number height)", -1); } if (width < 0 || height < 0) { - PyErr_SetString(pgExc_SDLError, "Invalid resolution for Surface"); - return -1; + RAISERETURN(pgExc_SDLError, "Invalid resolution for Surface", -1); } surface_cleanup(self); @@ -522,36 +518,31 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) if (depth && masks) { /* all info supplied, most errorchecking * needed */ if (pgSurface_Check(depth)) { - PyErr_SetString(PyExc_ValueError, - "cannot pass surface for depth and color masks"); - return -1; + RAISERETURN(PyExc_ValueError, + "cannot pass surface for depth and color masks", -1); } if (!pg_IntFromObj(depth, &bpp)) { - PyErr_SetString(PyExc_ValueError, - "invalid bits per pixel depth argument"); - return -1; + RAISERETURN(PyExc_ValueError, + "invalid bits per pixel depth argument", -1); } if (!PySequence_Check(masks) || PySequence_Length(masks) != 4) { - PyErr_SetString(PyExc_ValueError, - "masks argument must be sequence of four numbers"); - return -1; + RAISERETURN(PyExc_ValueError, + "masks argument must be sequence of four numbers", -1); } if (!pg_UintFromObjIndex(masks, 0, &Rmask) || !pg_UintFromObjIndex(masks, 1, &Gmask) || !pg_UintFromObjIndex(masks, 2, &Bmask) || !pg_UintFromObjIndex(masks, 3, &Amask)) { - PyErr_SetString(PyExc_ValueError, - "invalid mask values in masks sequence"); - return -1; + RAISERETURN(PyExc_ValueError, + "invalid mask values in masks sequence", -1); } format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask); } else if (depth && PyNumber_Check(depth)) { /* use default masks */ if (!pg_IntFromObj(depth, &bpp)) { - PyErr_SetString(PyExc_ValueError, - "invalid bits per pixel depth argument"); - return -1; + RAISERETURN(PyExc_ValueError, + "invalid bits per pixel depth argument", -1); } if (flags & PGS_SRCALPHA) { switch (bpp) { @@ -562,11 +553,10 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) format = SDL_PIXELFORMAT_ARGB8888; break; default: - PyErr_SetString( - PyExc_ValueError, - "no standard masks exist for given bitdepth with " - "alpha"); - return -1; + RAISERETURN(PyExc_ValueError, + "no standard masks exist for given bitdepth " + "with alpha", + -1); } } else { @@ -594,9 +584,8 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) format = SDL_PIXELFORMAT_XRGB8888; break; default: - PyErr_SetString(PyExc_ValueError, - "nonstandard bit depth given"); - return -1; + RAISERETURN(PyExc_ValueError, + "nonstandard bit depth given", -1); } } } @@ -624,24 +613,21 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) format = SDL_PIXELFORMAT_ARGB8888; break; default: - PyErr_SetString( - PyExc_ValueError, - "no standard masks exist for given bitdepth with " - "alpha"); - return -1; + RAISERETURN(PyExc_ValueError, + "no standard masks exist for given bitdepth " + "with alpha", + -1); } } } if (format == SDL_PIXELFORMAT_UNKNOWN) { - PyErr_SetString(PyExc_ValueError, "Invalid mask values"); - return -1; + RAISERETURN(PyExc_ValueError, "Invalid mask values", -1); } surface = PG_CreateSurface(width, height, format); if (!surface) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } if (!(flags & PGS_SRCALPHA)) { @@ -659,9 +645,8 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) PG_PixelFormat *surf_format; SDL_Palette *surf_palette; if (!PG_GetSurfaceDetails(surface, &surf_format, &surf_palette)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(surface); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } if (surf_format->Amask != 0) { @@ -676,43 +661,37 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) SDL_Palette *surf_palette = SDL_CreateSurfacePalette(surface); if (!PG_SetPaletteColors(surf_palette, default_palette_colors, 0, default_palette_size - 1)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(surface); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } } #else if (depth && masks) { /* all info supplied, most errorchecking * needed */ if (pgSurface_Check(depth)) { - PyErr_SetString(PyExc_ValueError, - "cannot pass surface for depth and color masks"); - return -1; + RAISERETURN(PyExc_ValueError, + "cannot pass surface for depth and color masks", -1); } if (!pg_IntFromObj(depth, &bpp)) { - PyErr_SetString(PyExc_ValueError, - "invalid bits per pixel depth argument"); - return -1; + RAISERETURN(PyExc_ValueError, + "invalid bits per pixel depth argument", -1); } if (!PySequence_Check(masks) || PySequence_Length(masks) != 4) { - PyErr_SetString(PyExc_ValueError, - "masks argument must be sequence of four numbers"); - return -1; + RAISERETURN(PyExc_ValueError, + "masks argument must be sequence of four numbers", -1); } if (!pg_UintFromObjIndex(masks, 0, &Rmask) || !pg_UintFromObjIndex(masks, 1, &Gmask) || !pg_UintFromObjIndex(masks, 2, &Bmask) || !pg_UintFromObjIndex(masks, 3, &Amask)) { - PyErr_SetString(PyExc_ValueError, - "invalid mask values in masks sequence"); - return -1; + RAISERETURN(PyExc_ValueError, + "invalid mask values in masks sequence", -1); } } else if (depth && PyNumber_Check(depth)) { /* use default masks */ if (!pg_IntFromObj(depth, &bpp)) { - PyErr_SetString(PyExc_ValueError, - "invalid bits per pixel depth argument"); - return -1; + RAISERETURN(PyExc_ValueError, + "invalid bits per pixel depth argument", -1); } if (flags & PGS_SRCALPHA) { switch (bpp) { @@ -729,11 +708,10 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) Amask = 0xFF << 24; break; default: - PyErr_SetString( - PyExc_ValueError, - "no standard masks exist for given bitdepth with " - "alpha"); - return -1; + RAISERETURN(PyExc_ValueError, + "no standard masks exist for given bitdepth " + "with alpha", + -1); } } else { @@ -766,9 +744,8 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) Bmask = 0xFF; break; default: - PyErr_SetString(PyExc_ValueError, - "nonstandard bit depth given"); - return -1; + RAISERETURN(PyExc_ValueError, + "nonstandard bit depth given", -1); } } } @@ -809,11 +786,10 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) Amask = 0xFF << 24; break; default: - PyErr_SetString( - PyExc_ValueError, - "no standard masks exist for given bitdepth with " - "alpha"); - return -1; + RAISERETURN(PyExc_ValueError, + "no standard masks exist for given bitdepth " + "with alpha", + -1); } } else { @@ -827,14 +803,12 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) Uint32 pxformat = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask); if (pxformat == SDL_PIXELFORMAT_UNKNOWN) { - PyErr_SetString(PyExc_ValueError, "Invalid mask values"); - return -1; + RAISERETURN(PyExc_ValueError, "Invalid mask values", -1); } surface = PG_CreateSurface(width, height, pxformat); if (!surface) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } if (!(flags & PGS_SRCALPHA)) { @@ -861,9 +835,8 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) if (!PG_SetPaletteColors(surface->format->palette, default_palette_colors, 0, default_palette_size - 1)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(surface); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } } #endif @@ -1815,9 +1788,8 @@ surf_convert(pgSurfaceObject *self, PyObject *args) if (has_colorkey) { colorkey = SDL_MapSurfaceRGBA(newsurf, key_r, key_g, key_b, key_a); if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(newsurf); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } } #else @@ -1978,9 +1950,8 @@ surf_convert(pgSurfaceObject *self, PyObject *args) if (has_colorkey) { colorkey = SDL_MapRGBA(newsurf->format, key_r, key_g, key_b, key_a); if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(newsurf); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } } #endif @@ -2939,8 +2910,7 @@ surf_scroll(PyObject *self, PyObject *args, PyObject *keywds) if (dx >= w || dx <= -w || dy >= h || dy <= -h) { if (erase) { if (!PG_FillSurfaceRect(surf, NULL, 0)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } } Py_RETURN_NONE; @@ -2971,8 +2941,7 @@ _PgSurface_SrcAlpha(SDL_Surface *surf) { SDL_BlendMode mode; if (!PG_GetSurfaceBlendMode(surf, &mode)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } return (mode != SDL_BLENDMODE_NONE); } @@ -3227,42 +3196,36 @@ surf_subsurface(PyObject *self, PyObject *args) SDL_Palette *pal = SDL_AllocPalette(ncolors); if (!pal) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(sub); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } if (!PG_SetPaletteColors(pal, colors, 0, ncolors)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreePalette(pal); SDL_FreeSurface(sub); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } if (SDL_SetSurfacePalette(sub, pal) != 0) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreePalette(pal); SDL_FreeSurface(sub); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } SDL_FreePalette(pal); } if (!PG_GetSurfaceAlphaMod(surf, &alpha)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(sub); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } if (alpha != 255) { if (!PG_SetSurfaceAlphaMod(sub, alpha)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(sub); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } } if (SDL_HasColorKey(surf)) { SDL_GetColorKey(surf, &colorkey); if (SDL_SetColorKey(sub, SDL_TRUE, colorkey) != 0) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(sub); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } } @@ -3645,17 +3608,15 @@ surf_get_view(PyObject *self, PyObject *args) case VIEWKIND_0D: if (surface->pitch != bpp * surface->w) { - PyErr_SetString(PyExc_ValueError, - "Surface data is not contiguous"); - return 0; + RAISERETURN(PyExc_ValueError, "Surface data is not contiguous", + 0); } get_buffer = _get_buffer_0D; break; case VIEWKIND_1D: if (surface->pitch != bpp * surface->w) { - PyErr_SetString(PyExc_ValueError, - "Surface data is not contiguous"); - return 0; + RAISERETURN(PyExc_ValueError, "Surface data is not contiguous", + 0); } get_buffer = _get_buffer_1D; break; @@ -3905,34 +3866,28 @@ _get_buffer_2D(PyObject *obj, Py_buffer *view_p, int flags) view_p->obj = 0; if (!PyBUF_HAS_FLAG(flags, PyBUF_ND)) { if (surface->pitch != surface->w * itemsize) { - PyErr_SetString(pgExc_BufferError, - "A 2D surface view is not C contiguous"); - return -1; + RAISERETURN(pgExc_BufferError, + "A 2D surface view is not C contiguous", -1); } return _get_buffer_1D(obj, view_p, flags); } if (!PyBUF_HAS_FLAG(flags, PyBUF_STRIDES)) { - PyErr_SetString(pgExc_BufferError, - "A 2D surface view is not C contiguous: " - "need strides"); - return -1; + RAISERETURN(pgExc_BufferError, + "A 2D surface view is not C contiguous: need strides", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_C_CONTIGUOUS)) { - PyErr_SetString(pgExc_BufferError, - "A 2D surface view is not C contiguous"); - return -1; + RAISERETURN(pgExc_BufferError, "A 2D surface view is not C contiguous", + -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_F_CONTIGUOUS) && surface->pitch != surface->w * itemsize) { - PyErr_SetString(pgExc_BufferError, - "This 2D surface view is not F contiguous"); - return -1; + RAISERETURN(pgExc_BufferError, + "This 2D surface view is not F contiguous", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_ANY_CONTIGUOUS) && surface->pitch != surface->w * itemsize) { - PyErr_SetString(pgExc_BufferError, - "This 2D surface view is not contiguous"); - return -1; + RAISERETURN(pgExc_BufferError, + "This 2D surface view is not contiguous", -1); } if (_init_buffer(obj, view_p, flags)) { return -1; @@ -3991,21 +3946,18 @@ _get_buffer_3D(PyObject *obj, Py_buffer *view_p, int flags) view_p->obj = 0; if (!PyBUF_HAS_FLAG(flags, PyBUF_STRIDES)) { - PyErr_SetString(pgExc_BufferError, - "A 3D surface view is not contiguous: needs strides"); - return -1; + RAISERETURN(pgExc_BufferError, + "A 3D surface view is not contiguous: needs strides", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_C_CONTIGUOUS) || PyBUF_HAS_FLAG(flags, PyBUF_F_CONTIGUOUS) || PyBUF_HAS_FLAG(flags, PyBUF_ANY_CONTIGUOUS)) { - PyErr_SetString(pgExc_BufferError, - "A 3D surface view is not contiguous"); - return -1; + RAISERETURN(pgExc_BufferError, "A 3D surface view is not contiguous", + -1); } PG_PixelFormat *surface_format = PG_GetSurfaceFormat(surface); if (surface_format == NULL) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } if (_init_buffer(obj, view_p, flags)) { @@ -4072,8 +4024,7 @@ _get_buffer_red(PyObject *obj, Py_buffer *view_p, int flags) PG_PixelFormat *surface_format = PG_GetSurfaceFormat(pgSurface_AsSurface(obj)); if (surface_format == NULL) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } return _get_buffer_colorplane(obj, view_p, flags, "red", @@ -4086,8 +4037,7 @@ _get_buffer_green(PyObject *obj, Py_buffer *view_p, int flags) PG_PixelFormat *surface_format = PG_GetSurfaceFormat(pgSurface_AsSurface(obj)); if (surface_format == NULL) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } return _get_buffer_colorplane(obj, view_p, flags, "green", @@ -4100,8 +4050,7 @@ _get_buffer_blue(PyObject *obj, Py_buffer *view_p, int flags) PG_PixelFormat *surface_format = PG_GetSurfaceFormat(pgSurface_AsSurface(obj)); if (surface_format == NULL) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } return _get_buffer_colorplane(obj, view_p, flags, "blue", @@ -4114,8 +4063,7 @@ _get_buffer_alpha(PyObject *obj, Py_buffer *view_p, int flags) PG_PixelFormat *surface_format = PG_GetSurfaceFormat(pgSurface_AsSurface(obj)); if (surface_format == NULL) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } return _get_buffer_colorplane(obj, view_p, flags, "alpha", @@ -4132,17 +4080,15 @@ _get_buffer_colorplane(PyObject *obj, Py_buffer *view_p, int flags, char *name, view_p->obj = 0; if (!PyBUF_HAS_FLAG(flags, PyBUF_STRIDES)) { - PyErr_SetString(pgExc_BufferError, - "A surface color plane view is not contiguous: " - "need strides"); - return -1; + RAISERETURN( + pgExc_BufferError, + "A surface color plane view is not contiguous: need strides", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_C_CONTIGUOUS) || PyBUF_HAS_FLAG(flags, PyBUF_F_CONTIGUOUS) || PyBUF_HAS_FLAG(flags, PyBUF_ANY_CONTIGUOUS)) { - PyErr_SetString(pgExc_BufferError, - "A surface color plane view is not contiguous"); - return -1; + RAISERETURN(pgExc_BufferError, + "A surface color plane view is not contiguous", -1); } switch (mask) { /* This switch statement is exhaustive over possible mask value, @@ -4292,17 +4238,15 @@ _view_kind(PyObject *obj, void *view_kind_vptr) if (PyUnicode_Check(obj)) { if (PyUnicode_GET_LENGTH(obj) != 1) { - PyErr_SetString(PyExc_TypeError, - "expected a length 1 string for argument 1"); - return 0; + RAISERETURN(PyExc_TypeError, + "expected a length 1 string for argument 1", 0); } ch = PyUnicode_READ_CHAR(obj, 0); } else if (PyBytes_Check(obj)) { if (PyBytes_GET_SIZE(obj) != 1) { - PyErr_SetString(PyExc_TypeError, - "expected a length 1 string for argument 1"); - return 0; + RAISERETURN(PyExc_TypeError, + "expected a length 1 string for argument 1", 0); } ch = *PyBytes_AS_STRING(obj); } @@ -4585,10 +4529,10 @@ pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj, pgSurface_Unprep(srcobj); if (result == -1) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); + RAISE(pgExc_SDLError, SDL_GetError()); } if (result == -2) { - PyErr_SetString(pgExc_SDLError, "Surface was lost"); + RAISE(pgExc_SDLError, "Surface was lost"); } return result != 0; diff --git a/src_c/surflock.c b/src_c/surflock.c index 862c9a9303..81e6540973 100644 --- a/src_c/surflock.c +++ b/src_c/surflock.c @@ -100,8 +100,7 @@ pgSurface_LockBy(pgSurfaceObject *surfobj, PyObject *lockobj) pgSurface_Prep(surfobj); } if (!PG_LockSurface(surf->surf)) { - PyErr_SetString(PyExc_RuntimeError, "error locking surface"); - return 0; + RAISERETURN(PyExc_RuntimeError, "error locking surface", 0); } return 1; } diff --git a/src_c/time.c b/src_c/time.c index e0b8d955c8..4c55520181 100644 --- a/src_c/time.c +++ b/src_c/time.c @@ -326,8 +326,7 @@ accurate_delay(Sint64 ticks) #if !SDL_VERSION_ATLEAST(3, 0, 0) if (!SDL_WasInit(SDL_INIT_TIMER)) { if (SDL_InitSubSystem(SDL_INIT_TIMER)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } } #endif diff --git a/src_c/transform.c b/src_c/transform.c index 810c32865b..f19e996317 100644 --- a/src_c/transform.c +++ b/src_c/transform.c @@ -139,74 +139,63 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height) SDL_Palette *surf_palette = PG_GetSurfacePalette(surf); if (newsurf_palette == NULL) { - PyErr_SetString( - pgExc_SDLError, - "Palette expected (newsurf) but no palette found."); SDL_FreeSurface(newsurf); - return NULL; + return RAISE(pgExc_SDLError, + "Palette expected (newsurf) but no palette found."); } if (surf_palette == NULL) { - PyErr_SetString(pgExc_SDLError, - "Palette expected (surf) but no palette found."); SDL_FreeSurface(newsurf); - return NULL; + return RAISE(pgExc_SDLError, + "Palette expected (surf) but no palette found."); } if (!PG_SetPaletteColors(newsurf_palette, surf_palette->colors, 0, surf_palette->ncolors)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(newsurf); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } } if (!PG_GetSurfaceAlphaMod(surf, &alpha)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(newsurf); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } if (alpha != 255) { if (!PG_SetSurfaceAlphaMod(newsurf, alpha)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(newsurf); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } } isalpha = _PgSurface_SrcAlpha(surf); if (isalpha == 1) { if (!PG_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_BLEND)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(newsurf); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } } else if (isalpha == -1) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(newsurf); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } else { if (!PG_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_NONE)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(newsurf); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } } if (SDL_HasColorKey(surf)) { SDL_GetColorKey(surf, &colorkey); if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(newsurf); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } if (PG_SurfaceHasRLE(surf) && SDL_SetSurfaceRLE(newsurf, SDL_TRUE) != 0) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); SDL_FreeSurface(newsurf); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } } @@ -966,8 +955,7 @@ surf_rotozoom(PyObject *self, PyObject *args, PyObject *kwargs) newsurf = rotozoomSurface(surf32, angle, scale, 1); Py_END_ALLOW_THREADS; if (newsurf == NULL) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return NULL; + return RAISE(pgExc_SDLError, SDL_GetError()); } if (surf32 == surf) { diff --git a/src_c/window.c b/src_c/window.c index f2976df502..6e519c7353 100644 --- a/src_c/window.c +++ b/src_c/window.c @@ -236,12 +236,10 @@ _window_opengl_set_viewport(SDL_Window *window, SDL_GLContext context, if (SDL_GL_MakeCurrent(window, context) < 0) #endif { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } if (p_glViewport == NULL) { - PyErr_SetString(pgExc_SDLError, "glViewport function is unavailable"); - return -1; + RAISERETURN(pgExc_SDLError, "glViewport function is unavailable", -1); } p_glViewport(0, 0, wnew, hnew); return 0; @@ -578,9 +576,8 @@ window_set_title(pgWindowObject *self, PyObject *arg, void *v) { const char *title; if (!PyUnicode_Check(arg)) { - PyErr_SetString(PyExc_TypeError, - "Argument to set_title must be a str."); - return -1; + RAISERETURN(PyExc_TypeError, "Argument to set_title must be a str.", + -1); } title = PyUnicode_AsUTF8(arg); SDL_SetWindowTitle(self->_win, title); @@ -678,17 +675,15 @@ window_set_mouse_rect(pgWindowObject *self, PyObject *arg, void *v) SDL_Rect tmp_rect; SDL_Rect *mouse_rect_p = pgRect_FromObject(arg, &tmp_rect); if (mouse_rect_p == NULL && arg != Py_None) { - PyErr_SetString(PyExc_TypeError, - "mouse_rect should be a Rect-like object or None"); - return -1; + RAISERETURN(PyExc_TypeError, + "mouse_rect should be a Rect-like object or None", -1); } #if SDL_VERSION_ATLEAST(3, 0, 0) if (!SDL_SetWindowMouseRect(self->_win, mouse_rect_p)) { #else if (SDL_SetWindowMouseRect(self->_win, mouse_rect_p) < 0) { #endif - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } #else if (PyErr_WarnEx(PyExc_Warning, @@ -723,15 +718,13 @@ window_set_size(pgWindowObject *self, PyObject *arg, void *v) int w, h; if (!pg_TwoIntsFromObj(arg, &w, &h)) { - PyErr_SetString(PyExc_TypeError, "invalid size argument"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid size argument", -1); } if (w <= 0 || h <= 0) { - PyErr_SetString( - PyExc_ValueError, - "width or height should not be less than or equal to zero"); - return -1; + RAISERETURN(PyExc_ValueError, + "width or height should not be less than or equal to zero", + -1); } SDL_SetWindowSize(self->_win, w, h); @@ -767,23 +760,21 @@ window_set_minimum_size(pgWindowObject *self, PyObject *arg, void *v) int max_w, max_h; if (!pg_TwoIntsFromObj(arg, &w, &h)) { - PyErr_SetString(PyExc_TypeError, "invalid size argument"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid size argument", -1); } if (w < 0 || h < 0) { - PyErr_SetString( - PyExc_ValueError, - "minimum width or height should not be less than zero"); - return -1; + RAISERETURN(PyExc_ValueError, + "minimum width or height should not be less than zero", + -1); } SDL_GetWindowMaximumSize(self->_win, &max_w, &max_h); if ((max_w > 0 && max_h > 0) && (w > max_w || h > max_h)) { - PyErr_SetString(PyExc_ValueError, - "minimum width or height should not be greater than " - "maximum width or height respectively"); - return -1; + RAISERETURN(PyExc_ValueError, + "minimum width or height should not be greater than " + "maximum width or height respectively", + -1); } SDL_SetWindowMinimumSize(self->_win, w, h); @@ -807,23 +798,21 @@ window_set_maximum_size(pgWindowObject *self, PyObject *arg, void *v) int min_w, min_h; if (!pg_TwoIntsFromObj(arg, &w, &h)) { - PyErr_SetString(PyExc_TypeError, "invalid size argument"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid size argument", -1); } if (w < 0 || h < 0) { - PyErr_SetString( - PyExc_ValueError, - "maximum width or height should not be less than zero"); - return -1; + RAISERETURN(PyExc_ValueError, + "maximum width or height should not be less than zero", + -1); } SDL_GetWindowMinimumSize(self->_win, &min_w, &min_h); if (w < min_w || h < min_h) { - PyErr_SetString(PyExc_ValueError, - "maximum width or height should not be less than " - "minimum width or height respectively"); - return -1; + RAISERETURN(PyExc_ValueError, + "maximum width or height should not be less than minimum " + "width or height respectively", + -1); } SDL_SetWindowMaximumSize(self->_win, w, h); @@ -848,13 +837,11 @@ window_set_position(pgWindowObject *self, PyObject *arg, void *v) if (PyLong_Check(arg)) { x = y = PyLong_AsLong(arg); if (x != SDL_WINDOWPOS_CENTERED && x != SDL_WINDOWPOS_UNDEFINED) { - PyErr_SetString(PyExc_TypeError, "invalid position argument"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid position argument", -1); } } else if (!pg_TwoIntsFromObj(arg, &x, &y)) { - PyErr_SetString(PyExc_TypeError, "invalid position argument"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid position argument", -1); } SDL_SetWindowPosition(self->_win, x, y); @@ -880,8 +867,7 @@ window_set_opacity(pgWindowObject *self, PyObject *arg, void *v) return -1; } if (SDL_SetWindowOpacity(self->_win, opacity)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } return 0; } @@ -1016,8 +1002,7 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs) if (kwargs) { while (PyDict_Next(kwargs, &dict_pos, &_key, &_value)) { if (!PyUnicode_Check(_key)) { - PyErr_SetString(PyExc_TypeError, "keywords must be strings"); - return -1; + RAISERETURN(PyExc_TypeError, "keywords must be strings", -1); } _key_str = PyUnicode_AsUTF8(_key); @@ -1183,16 +1168,14 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs) if (size) { if (!pg_TwoIntsFromObj(size, &size_w, &size_h)) { - PyErr_SetString(PyExc_TypeError, "invalid size argument"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid size argument", -1); } } if (size_w <= 0 || size_h <= 0) { - PyErr_SetString( + RAISERETURN( PyExc_ValueError, - "width or height should not be less than or equal to zero."); - return -1; + "width or height should not be less than or equal to zero.", -1); } if (position) { @@ -1200,26 +1183,22 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs) pos_x = pos_y = PyLong_AsLong(position); if (pos_x != SDL_WINDOWPOS_CENTERED && pos_x != SDL_WINDOWPOS_UNDEFINED) { - PyErr_SetString(PyExc_TypeError, "invalid position argument"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid position argument", -1); } } else if (!pg_TwoIntsFromObj(position, &pos_x, &pos_y)) { - PyErr_SetString(PyExc_TypeError, "invalid position argument"); - return -1; + RAISERETURN(PyExc_TypeError, "invalid position argument", -1); } } _win = PG_CreateWindow(title, pos_x, pos_y, size_w, size_h, flags); if (!_win) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } #if SDL_VERSION_ATLEAST(3, 0, 0) if (fullscreen_non_desktop) { if (!pg_window_set_fullscreen(_win, 0)) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } } #endif @@ -1230,8 +1209,7 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs) if (flags & SDL_WINDOW_OPENGL) { SDL_GLContext context = SDL_GL_CreateContext(self->_win); if (context == NULL) { - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } /* As stated in the 'Remarks' of the docs * (https://wiki.libsdl.org/SDL2/SDL_GL_GetProcAddress) on Windows @@ -1258,8 +1236,7 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs) if (SDL_SetColorKey(pgSurface_AsSurface(icon), SDL_TRUE, icon_colorkey) < 0) { #endif - PyErr_SetString(pgExc_SDLError, SDL_GetError()); - return -1; + RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } } SDL_SetWindowIcon(self->_win, pgSurface_AsSurface(icon)); From 1d26b16992ffca6919dcdc46b92dbe40981ef880 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josip=20Komljenovi=C4=87?= Date: Sat, 7 Jun 2025 23:12:40 +0200 Subject: [PATCH 2/8] Use macro for exception raising --- src_c/mask.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src_c/mask.c b/src_c/mask.c index cfeb61e03b..c8b0684a7c 100644 --- a/src_c/mask.c +++ b/src_c/mask.c @@ -2205,7 +2205,7 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs) y_dest = dest_rect->y; } else { - PyErr_SetString(PyExc_TypeError, "invalid dest argument"); + RAISE(PyExc_TypeError, "invalid dest argument"); goto to_surface_error; } } @@ -2213,7 +2213,7 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs) if (areaobj && areaobj != Py_None) { if (!(area_rect = pgRect_FromObject(areaobj, &temp_rect))) { - PyErr_SetString(PyExc_TypeError, "invalid rectstyle argument"); + RAISE(PyExc_TypeError, "invalid rectstyle argument"); goto to_surface_error; } From e624ea6257078766146d931366479049f87846d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josip=20Komljenovi=C4=87?= Date: Sun, 8 Jun 2025 13:11:53 +0200 Subject: [PATCH 3/8] Use macro for exception raising --- src_c/_camera.c | 8 +- src_c/_freetype.c | 110 ++++++++++++---------- src_c/_pygame.h | 15 ++- src_c/_sdl2/controller.c | 2 +- src_c/base.c | 122 +++++++++++++------------ src_c/bufferproxy.c | 22 +++-- src_c/camera_windows.c | 29 +++--- src_c/circle.c | 72 ++++++++------- src_c/color.c | 105 +++++++++++---------- src_c/display.c | 29 +++--- src_c/draw.c | 7 +- src_c/event.c | 16 ++-- src_c/font.c | 40 ++++---- src_c/freetype/ft_layout.c | 4 +- src_c/freetype/ft_render.c | 52 ++++++----- src_c/freetype/ft_wrap.c | 29 +++--- src_c/freetype/ft_wrap.h | 11 ++- src_c/line.c | 23 +++-- src_c/mask.c | 10 +- src_c/math.c | 136 ++++++++++++++++------------ src_c/mixer.c | 62 +++++++------ src_c/newbuffer.c | 6 +- src_c/pixelarray.c | 77 ++++++++-------- src_c/pixelarray_methods.c | 9 +- src_c/pixelcopy.c | 24 ++--- src_c/rect.c | 48 +++++----- src_c/rect_impl.h | 98 ++++++++++---------- src_c/render.c | 18 ++-- src_c/rwobject.c | 10 +- src_c/scrap_sdl2.c | 2 +- src_c/scrap_win.c | 4 +- src_c/surface.c | 181 ++++++++++++++++++++----------------- src_c/surflock.c | 2 +- src_c/time.c | 2 +- src_c/window.c | 86 ++++++++++-------- 35 files changed, 790 insertions(+), 681 deletions(-) diff --git a/src_c/_camera.c b/src_c/_camera.c index a4a98f39b3..9fea073b95 100644 --- a/src_c/_camera.c +++ b/src_c/_camera.c @@ -1890,8 +1890,8 @@ camera_init(pgCameraObject *self, PyObject *arg, PyObject *kwargs) p = windows_device_from_name(dev_name); if (!p) { - RAISERETURN(PyExc_ValueError, "Couldn't find a camera with that name", - -1); + return RAISERETURN(PyExc_ValueError, + "Couldn't find a camera with that name", -1); } if (color) { @@ -1927,8 +1927,8 @@ camera_init(pgCameraObject *self, PyObject *arg, PyObject *kwargs) return 0; #else - RAISERETURN(PyExc_RuntimeError, - "_camera backend not available on your platform", -1); + return RAISERETURN(PyExc_RuntimeError, + "_camera backend not available on your platform", -1); #endif } diff --git a/src_c/_freetype.c b/src_c/_freetype.c index 8725708c9e..01d4190870 100644 --- a/src_c/_freetype.c +++ b/src_c/_freetype.c @@ -201,13 +201,13 @@ free_string(PGFT_String *); "." FONT_TYPE_NAME " instance is not initialized"); \ } -#define PGFT_CHECK_BOOL(_pyobj, _var) \ - if (_pyobj) { \ - if (!PyBool_Check(_pyobj)) { \ - RAISERETURN(PyExc_TypeError, #_var " must be a boolean value", \ - 0); \ - } \ - _var = PyObject_IsTrue(_pyobj); \ +#define PGFT_CHECK_BOOL(_pyobj, _var) \ + if (_pyobj) { \ + if (!PyBool_Check(_pyobj)) { \ + return RAISERETURN(PyExc_TypeError, \ + #_var " must be a boolean value", 0); \ + } \ + _var = PyObject_IsTrue(_pyobj); \ } #define DEFAULT_FONT_NAME "freesansbold.ttf" @@ -220,7 +220,7 @@ static unsigned int current_freetype_generation = 0; (((pgFontObject *)(x))->init_generation == current_freetype_generation) #define RAISE_FREETYPE_QUIT_ERROR(r) \ - RAISERETURN( \ + return RAISERETURN( \ pgExc_SDLError, \ "Invalid freetype font (freetype module quit since freetype font " \ "created)", \ @@ -305,7 +305,8 @@ parse_dest(PyObject *dest, int *x, int *y) if (!pg_IntFromObj(oi, &i) || !pg_IntFromObj(oj, &j)) { Py_DECREF(oi); Py_DECREF(oj); - RAISERETURN(PyExc_TypeError, "dest expects a pair of numbers", -1); + return RAISERETURN(PyExc_TypeError, "dest expects a pair of numbers", + -1); } Py_DECREF(oi); Py_DECREF(oj); @@ -424,8 +425,8 @@ build_scale(PyObject *x, PyObject *y, Scale_t *size) } } if (sz_x == 0 && sz_y != 0) { - RAISERETURN(PyExc_ValueError, - "expected zero size height when width is zero", 0); + return RAISERETURN(PyExc_ValueError, + "expected zero size height when width is zero", 0); } size->x = sz_x; size->y = sz_y; @@ -875,19 +876,21 @@ _ftfont_setstyle_flag(pgFontObject *self, PyObject *value, void *closure) const intptr_t style_flag = (intptr_t)closure; if (!PyBool_Check(value)) { - RAISERETURN(PyExc_TypeError, "The style value must be a boolean", -1); + return RAISERETURN(PyExc_TypeError, + "The style value must be a boolean", -1); } if ((style_flag & FT_STYLES_SCALABLE_ONLY) && !self->is_scalable) { if (pgFont_IS_ALIVE(self)) { - RAISERETURN(PyExc_AttributeError, - "this style is unsupported for a bitmap font", -1); + return RAISERETURN(PyExc_AttributeError, + "this style is unsupported for a bitmap font", + -1); } else { - RAISERETURN(PyExc_RuntimeError, - MODULE_NAME "." FONT_TYPE_NAME - " instance is not initialized", - -1); + return RAISERETURN(PyExc_RuntimeError, + MODULE_NAME "." FONT_TYPE_NAME + " instance is not initialized", + -1); } } if (PyObject_IsTrue(value)) { @@ -921,7 +924,7 @@ _ftfont_setstyle(pgFontObject *self, PyObject *value, void *closure) FT_UInt32 style; if (!PyLong_Check(value)) { - RAISERETURN( + return RAISERETURN( PyExc_TypeError, "The style value must be an integer from the FT constants module", -1); @@ -941,14 +944,15 @@ _ftfont_setstyle(pgFontObject *self, PyObject *value, void *closure) } if ((style & FT_STYLES_SCALABLE_ONLY) && !self->is_scalable) { if (pgFont_IS_ALIVE(self)) { - RAISERETURN(PyExc_AttributeError, - "this style is unsupported for a bitmap font", -1); + return RAISERETURN(PyExc_AttributeError, + "this style is unsupported for a bitmap font", + -1); } else { - RAISERETURN(PyExc_RuntimeError, - MODULE_NAME "." FONT_TYPE_NAME - " instance is not initialized", - -1); + return RAISERETURN(PyExc_RuntimeError, + MODULE_NAME "." FONT_TYPE_NAME + " instance is not initialized", + -1); } } @@ -1129,7 +1133,7 @@ _ftfont_getpath(pgFontObject *self, void *closure) PyObject *path = ((pgFontObject *)self)->path; if (!path) { - RAISERETURN(PyExc_AttributeError, "path unavailable", 0); + return RAISERETURN(PyExc_AttributeError, "path unavailable", 0); } Py_INCREF(path); return path; @@ -1201,7 +1205,8 @@ _ftfont_setrender_flag(pgFontObject *self, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!PyBool_Check(value)) { - RAISERETURN(PyExc_TypeError, "The style value must be a boolean", -1); + return RAISERETURN(PyExc_TypeError, + "The style value must be a boolean", -1); } if (PyObject_IsTrue(value)) { @@ -1247,14 +1252,15 @@ _ftfont_setrotation(pgFontObject *self, PyObject *value, void *closure) if (!self->is_scalable) { if (pgFont_IS_ALIVE(self)) { - RAISERETURN(PyExc_AttributeError, - "rotation is unsupported for a bitmap font", -1); + return RAISERETURN(PyExc_AttributeError, + "rotation is unsupported for a bitmap font", + -1); } else { - RAISERETURN(PyExc_RuntimeError, - MODULE_NAME "." FONT_TYPE_NAME - " instance is not initialized", - -1); + return RAISERETURN(PyExc_RuntimeError, + MODULE_NAME "." FONT_TYPE_NAME + " instance is not initialized", + -1); } } return obj_to_rotation(value, &self->rotation) ? 0 : -1; @@ -1414,7 +1420,7 @@ get_metrics(FontRenderMode *render, pgFontObject *font, PGFT_String *text) Py_ssize_t i; if (!_PGFT_GetFontSized(font->freetype, font, render->face_size)) { - RAISERETURN(pgExc_SDLError, _PGFT_GetError(font->freetype), 0); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(font->freetype), 0); } list = PyList_New(length); if (!list) { @@ -1519,10 +1525,11 @@ _ftfont_getsizedascender(pgFontObject *self, PyObject *args) if (face_size.x == 0) { if (self->face_size.x == 0) { - RAISERETURN(PyExc_ValueError, - "No font point size specified and no default font " - "size in typefont", - 0); + return RAISERETURN( + PyExc_ValueError, + "No font point size specified and no default font " + "size in typefont", + 0); } face_size = self->face_size; } @@ -1549,10 +1556,11 @@ _ftfont_getsizeddescender(pgFontObject *self, PyObject *args) if (face_size.x == 0) { if (self->face_size.x == 0) { - RAISERETURN(PyExc_ValueError, - "No font point size specified and no default font " - "size in typefont", - 0); + return RAISERETURN( + PyExc_ValueError, + "No font point size specified and no default font " + "size in typefont", + 0); } face_size = self->face_size; } @@ -1580,10 +1588,11 @@ _ftfont_getsizedheight(pgFontObject *self, PyObject *args) if (face_size.x == 0) { if (self->face_size.x == 0) { - RAISERETURN(PyExc_ValueError, - "No font point size specified and no default font " - "size in typeface", - 0); + return RAISERETURN( + PyExc_ValueError, + "No font point size specified and no default font " + "size in typeface", + 0); } face_size = self->face_size; } @@ -1610,10 +1619,11 @@ _ftfont_getsizedglyphheight(pgFontObject *self, PyObject *args) if (face_size.x == 0) { if (self->face_size.x == 0) { - RAISERETURN(PyExc_ValueError, - "No font point size specified and no default font " - "size in typeface", - 0); + return RAISERETURN( + PyExc_ValueError, + "No font point size specified and no default font " + "size in typeface", + 0); } face_size = self->face_size; } diff --git a/src_c/_pygame.h b/src_c/_pygame.h index ea9c45205a..625ef1ea9a 100644 --- a/src_c/_pygame.h +++ b/src_c/_pygame.h @@ -565,9 +565,7 @@ typedef enum { } PygameScrollSurfaceFlags; #define RAISE(x, y) (PyErr_SetString((x), (y)), NULL) -#define RAISERETURN(x, y, r) \ - PyErr_SetString((x), (y)); \ - return r; +#define RAISERETURN(x, y, r) (PyErr_SetString((x), (y)), r) #define DEL_ATTR_NOT_SUPPORTED_CHECK(name, value) \ do { \ if (!value) { \ @@ -577,11 +575,12 @@ typedef enum { } \ } while (0) -#define DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value) \ - do { \ - if (!value) { \ - RAISERETURN(PyExc_AttributeError, "Cannot delete attribute", -1); \ - } \ +#define DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value) \ + do { \ + if (!value) { \ + return RAISERETURN(PyExc_AttributeError, \ + "Cannot delete attribute", -1); \ + } \ } while (0) /* diff --git a/src_c/_sdl2/controller.c b/src_c/_sdl2/controller.c index 7f503e402f..f7950649a4 100644 --- a/src_c/_sdl2/controller.c +++ b/src_c/_sdl2/controller.c @@ -485,7 +485,7 @@ controller_init(pgControllerObject *self, PyObject *args, PyObject *kwargs) if (self->controller == NULL) { controller = SDL_GameControllerOpen(self->id); if (controller == NULL) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } self->controller = controller; } diff --git a/src_c/base.c b/src_c/base.c index ecff14b408..5dab6ee2de 100644 --- a/src_c/base.c +++ b/src_c/base.c @@ -909,7 +909,8 @@ pgGetArrayStruct(PyObject *obj, PyObject **cobj_p, PyArrayInterface **inter_p) if (cobj == NULL) { if (PyErr_ExceptionMatches(PyExc_AttributeError)) { PyErr_Clear(); - RAISERETURN(PyExc_ValueError, "no C-struct array interface", -1); + return RAISERETURN(PyExc_ValueError, "no C-struct array interface", + -1); } return -1; } @@ -920,7 +921,7 @@ pgGetArrayStruct(PyObject *obj, PyObject **cobj_p, PyArrayInterface **inter_p) if (inter == NULL || inter->two != 2 /* conditional or */) { Py_DECREF(cobj); - RAISERETURN(PyExc_ValueError, "invalid array interface", -1); + return RAISERETURN(PyExc_ValueError, "invalid array interface", -1); } *cobj_p = cobj; @@ -943,9 +944,9 @@ pgArrayStruct_AsDict(PyArrayInterface *inter_p) if (inter_p->flags & PAI_ARR_HAS_DESCR) { if (!inter_p->descr) { Py_DECREF(dictobj); - RAISERETURN(PyExc_ValueError, - "Array struct has descr flag set but no descriptor", - 0); + return RAISERETURN( + PyExc_ValueError, + "Array struct has descr flag set but no descriptor", 0); } if (PyDict_SetItemString(dictobj, "descr", inter_p->descr)) { Py_DECREF(dictobj); @@ -1330,13 +1331,13 @@ pgObject_GetBuffer(PyObject *obj, pg_buffer *pg_view_p, int flags) break; default: pgBuffer_Release(pg_view_p); - RAISERETURN(PyExc_ValueError, "Unsupported array element type", - -1); + return RAISERETURN(PyExc_ValueError, + "Unsupported array element type", -1); } if (*fchar_p != '\0') { pgBuffer_Release(pg_view_p); - RAISERETURN(PyExc_ValueError, "Arrays of records are unsupported", - -1); + return RAISERETURN(PyExc_ValueError, + "Arrays of records are unsupported", -1); } success = 1; } @@ -1472,7 +1473,7 @@ pgGetArrayInterface(PyObject **dict, PyObject *obj) if (inter == NULL) { if (PyErr_ExceptionMatches(PyExc_AttributeError)) { PyErr_Clear(); - RAISERETURN(PyExc_ValueError, "no array interface", -1) + return RAISERETURN(PyExc_ValueError, "no array interface", -1); } return -1; } @@ -1513,25 +1514,25 @@ _pg_arraystruct_as_buffer(Py_buffer *view_p, PyObject *cobj, view_p->obj = 0; view_p->internal = 0; if (PyBUF_HAS_FLAG(flags, PyBUF_WRITABLE) && readonly) { - RAISERETURN(pgExc_BufferError, - "require writable buffer, but it is read-only", -1); + return RAISERETURN(pgExc_BufferError, + "require writable buffer, but it is read-only", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_ANY_CONTIGUOUS)) { if (!(inter_p->flags & (PAI_CONTIGUOUS | PAI_FORTRAN))) { - RAISERETURN(pgExc_BufferError, "buffer data is not contiguous", - -1); + return RAISERETURN(pgExc_BufferError, + "buffer data is not contiguous", -1); } } else if (PyBUF_HAS_FLAG(flags, PyBUF_C_CONTIGUOUS)) { if (!(inter_p->flags & PAI_CONTIGUOUS)) { - RAISERETURN(pgExc_BufferError, "buffer data is not C contiguous", - -1); + return RAISERETURN(pgExc_BufferError, + "buffer data is not C contiguous", -1); } } else if (PyBUF_HAS_FLAG(flags, PyBUF_F_CONTIGUOUS)) { if (!(inter_p->flags & PAI_FORTRAN)) { - RAISERETURN(pgExc_BufferError, "buffer data is not F contiguous", - -1); + return RAISERETURN(pgExc_BufferError, + "buffer data is not F contiguous", -1); } } internal_p = (pgViewInternals *)PyMem_Malloc(sz); @@ -1565,8 +1566,9 @@ _pg_arraystruct_as_buffer(Py_buffer *view_p, PyObject *cobj, view_p->shape = 0; } else { - RAISERETURN(pgExc_BufferError, - "buffer data is not C contiguous, shape needed", -1); + return RAISERETURN(pgExc_BufferError, + "buffer data is not C contiguous, shape needed", + -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_STRIDES)) { view_p->strides = view_p->shape + inter_p->nd; @@ -1578,8 +1580,8 @@ _pg_arraystruct_as_buffer(Py_buffer *view_p, PyObject *cobj, view_p->strides = 0; } else { - RAISERETURN(pgExc_BufferError, - "buffer is not contiguous, strides needed", -1); + return RAISERETURN(pgExc_BufferError, + "buffer is not contiguous, strides needed", -1); } view_p->suboffsets = 0; view_p->len = view_p->itemsize; @@ -1747,15 +1749,17 @@ static int _pg_shape_check(PyObject *op) { if (!op) { - RAISERETURN(PyExc_ValueError, "required 'shape' item is missing", -1); + return RAISERETURN(PyExc_ValueError, + "required 'shape' item is missing", -1); } if (!_pg_is_int_tuple(op)) { - RAISERETURN(PyExc_ValueError, "expected a tuple of ints for 'shape'", - -1); + return RAISERETURN(PyExc_ValueError, + "expected a tuple of ints for 'shape'", -1); } if (PyTuple_GET_SIZE(op) == 0) { - RAISERETURN(PyExc_ValueError, - "expected 'shape' to be at least one-dimensional", -1); + return RAISERETURN(PyExc_ValueError, + "expected 'shape' to be at least one-dimensional", + -1); } return 0; } @@ -1764,23 +1768,24 @@ static int _pg_typestr_check(PyObject *op) { if (!op) { - RAISERETURN(PyExc_ValueError, "required 'typestr' item is missing", - -1); + return RAISERETURN(PyExc_ValueError, + "required 'typestr' item is missing", -1); } if (PyUnicode_Check(op)) { if (PyUnicode_GET_LENGTH(op) != 3) { - RAISERETURN(PyExc_ValueError, "expected 'typestr' to be length 3", - -1); + return RAISERETURN(PyExc_ValueError, + "expected 'typestr' to be length 3", -1); } } else if (PyBytes_Check(op)) { if (PyBytes_GET_SIZE(op) != 3) { - RAISERETURN(PyExc_ValueError, "expected 'typestr' to be length 3", - -1); + return RAISERETURN(PyExc_ValueError, + "expected 'typestr' to be length 3", -1); } } else { - RAISERETURN(PyExc_ValueError, "expected a string for 'typestr'", -1); + return RAISERETURN(PyExc_ValueError, "expected a string for 'typestr'", + -1); } return 0; } @@ -1791,19 +1796,21 @@ _pg_data_check(PyObject *op) PyObject *item; if (!op) { - RAISERETURN(PyExc_ValueError, "required 'data' item is missing", -1); + return RAISERETURN(PyExc_ValueError, "required 'data' item is missing", + -1); } if (!PyTuple_Check(op)) { - RAISERETURN(PyExc_ValueError, "expected a tuple for 'data'", -1); + return RAISERETURN(PyExc_ValueError, "expected a tuple for 'data'", + -1); } if (PyTuple_GET_SIZE(op) != 2) { - RAISERETURN(PyExc_ValueError, "expected a length 2 tuple for 'data'", - -1); + return RAISERETURN(PyExc_ValueError, + "expected a length 2 tuple for 'data'", -1); } item = PyTuple_GET_ITEM(op, 0); if (!PyLong_Check(item)) { - RAISERETURN(PyExc_ValueError, "expected an int for item 0 of 'data'", - -1); + return RAISERETURN(PyExc_ValueError, + "expected an int for item 0 of 'data'", -1); } return 0; } @@ -1812,8 +1819,8 @@ static int _pg_strides_check(PyObject *op) { if (op && !_pg_is_int_tuple(op) /* Conditional && */) { - RAISERETURN(PyExc_ValueError, "expected a tuple of ints for 'strides'", - -1); + return RAISERETURN(PyExc_ValueError, + "expected a tuple of ints for 'strides'", -1); } return 0; } @@ -1850,8 +1857,9 @@ _pg_values_as_buffer(Py_buffer *view_p, int flags, PyObject *typestr, view_p->obj = 0; view_p->internal = 0; if (strides && PyTuple_GET_SIZE(strides) != ndim /* Cond. && */) { - RAISERETURN(PyExc_ValueError, - "'shape' and 'strides' are not the same length", -1); + return RAISERETURN(PyExc_ValueError, + "'shape' and 'strides' are not the same length", + -1); } view_p->ndim = (int)ndim; view_p->buf = PyLong_AsVoidPtr(PyTuple_GET_ITEM(data, 0)); @@ -1863,8 +1871,8 @@ _pg_values_as_buffer(Py_buffer *view_p, int flags, PyObject *typestr, return -1; } if (PyBUF_HAS_FLAG(flags, PyBUF_WRITABLE) && view_p->readonly) { - RAISERETURN(pgExc_BufferError, - "require writable buffer, but it is read-only", -1); + return RAISERETURN(pgExc_BufferError, + "require writable buffer, but it is read-only", -1); } sz = sizeof(pgViewInternals) + (2 * ndim - 1) * sizeof(Py_ssize_t); internal_p = (pgViewInternals *)PyMem_Malloc(sz); @@ -1903,20 +1911,20 @@ _pg_values_as_buffer(Py_buffer *view_p, int flags, PyObject *typestr, } if (PyBUF_HAS_FLAG(flags, PyBUF_ANY_CONTIGUOUS)) { if (!PyBuffer_IsContiguous(view_p, 'A')) { - RAISERETURN(pgExc_BufferError, "buffer data is not contiguous", - -1); + return RAISERETURN(pgExc_BufferError, + "buffer data is not contiguous", -1); } } else if (PyBUF_HAS_FLAG(flags, PyBUF_C_CONTIGUOUS)) { if (!PyBuffer_IsContiguous(view_p, 'C')) { - RAISERETURN(pgExc_BufferError, "buffer data is not C contiguous", - -1); + return RAISERETURN(pgExc_BufferError, + "buffer data is not C contiguous", -1); } } else if (PyBUF_HAS_FLAG(flags, PyBUF_F_CONTIGUOUS)) { if (!PyBuffer_IsContiguous(view_p, 'F')) { - RAISERETURN(pgExc_BufferError, "buffer data is not F contiguous", - -1); + return RAISERETURN(pgExc_BufferError, + "buffer data is not F contiguous", -1); } } if (!PyBUF_HAS_FLAG(flags, PyBUF_STRIDES)) { @@ -1924,8 +1932,9 @@ _pg_values_as_buffer(Py_buffer *view_p, int flags, PyObject *typestr, view_p->strides = 0; } else { - RAISERETURN(pgExc_BufferError, - "buffer data is not C contiguous, strides needed", -1); + return RAISERETURN( + pgExc_BufferError, + "buffer data is not C contiguous, strides needed", -1); } } if (!PyBUF_HAS_FLAG(flags, PyBUF_ND)) { @@ -1933,8 +1942,9 @@ _pg_values_as_buffer(Py_buffer *view_p, int flags, PyObject *typestr, view_p->shape = 0; } else { - RAISERETURN(pgExc_BufferError, - "buffer data is not C contiguous, shape needed", -1); + return RAISERETURN(pgExc_BufferError, + "buffer data is not C contiguous, shape needed", + -1); } } if (!PyBUF_HAS_FLAG(flags, PyBUF_FORMAT)) { diff --git a/src_c/bufferproxy.c b/src_c/bufferproxy.c index a8ce9d2550..1a4b976937 100644 --- a/src_c/bufferproxy.c +++ b/src_c/bufferproxy.c @@ -349,7 +349,8 @@ proxy_get_raw(pgBufferProxyObject *self, PyObject *closure) } if (!PyBuffer_IsContiguous(view_p, 'A')) { _proxy_release_view(self); - RAISERETURN(PyExc_ValueError, "the bytes are not contiguous", 0); + return RAISERETURN(PyExc_ValueError, "the bytes are not contiguous", + 0); } py_raw = PyBytes_FromStringAndSize((char *)view_p->buf, view_p->len); if (!py_raw) { @@ -413,19 +414,19 @@ proxy_write(pgBufferProxyObject *self, PyObject *args, PyObject *kwds) if (!PyBuffer_IsContiguous(&view, 'A')) { proxy_releasebuffer(self, &view); Py_DECREF(self); - RAISERETURN(PyExc_ValueError, - "the BufferProxy bytes are not contiguous", 0); + return RAISERETURN(PyExc_ValueError, + "the BufferProxy bytes are not contiguous", 0); } if (buflen > view.len) { proxy_releasebuffer(self, &view); Py_DECREF(self); - RAISERETURN(PyExc_ValueError, "'buffer' object length is too large", - 0); + return RAISERETURN(PyExc_ValueError, + "'buffer' object length is too large", 0); } if (offset < 0 || buflen + offset > view.len) { proxy_releasebuffer(self, &view); Py_DECREF(self); - RAISERETURN(PyExc_IndexError, "'offset' is out of range", 0); + return RAISERETURN(PyExc_IndexError, "'offset' is out of range", 0); } memcpy((char *)view.buf + offset, buf, (size_t)buflen); proxy_releasebuffer(self, &view); @@ -530,10 +531,11 @@ pgBufferProxy_New(PyObject *obj, getbufferproc get_buffer) { if (!get_buffer) { if (!obj) { - RAISERETURN(PyExc_ValueError, - "One of arguments 'obj' or 'get_buffer' is required: " - "both NULL instead", - 0); + return RAISERETURN( + PyExc_ValueError, + "One of arguments 'obj' or 'get_buffer' is required: " + "both NULL instead", + 0); } get_buffer = (getbufferproc)pgObject_GetBuffer; } diff --git a/src_c/camera_windows.c b/src_c/camera_windows.c index 5787e1bbb6..e780e9e914 100644 --- a/src_c/camera_windows.c +++ b/src_c/camera_windows.c @@ -97,13 +97,14 @@ _check_integrity(pgCameraObject *self) if (FAILED(self->t_error)) { /* MF_E_HW_MFT_FAILED_START_STREAMING */ if (self->t_error == (HRESULT)-1072875772) { - RAISERETURN(PyExc_SystemError, - "Camera already in use (Capture Thread Quit)", 0); + return RAISERETURN(PyExc_SystemError, + "Camera already in use (Capture Thread Quit)", + 0); } /* MF_E_VIDEO_RECORDING_DEVICE_INVALIDATED */ else if (self->t_error == (HRESULT)-1072873822) { - RAISERETURN(PyExc_SystemError, - "Camera disconnected (Capture Thread Quit)", 0); + return RAISERETURN(PyExc_SystemError, + "Camera disconnected (Capture Thread Quit)", 0); } else { T_FORMATHR(self->t_error, self->t_error_line) @@ -321,8 +322,8 @@ _select_source_type(pgCameraObject *self, IMFMediaType **mp) } if (!type_count) { - RAISERETURN(pgExc_SDLError, - "Could not find any video types on this camera", 0); + return RAISERETURN(pgExc_SDLError, + "Could not find any video types on this camera", 0); } native_types = malloc(sizeof(IMFMediaType *) * type_count); @@ -801,8 +802,8 @@ windows_read_frame(pgCameraObject *self, SDL_Surface *surf) HRESULT hr; if (!self->open) { - RAISERETURN(pgExc_SDLError, "Camera needs to be started to read data", - 0); + return RAISERETURN(pgExc_SDLError, + "Camera needs to be started to read data", 0); } if (!_check_integrity(self)) { @@ -836,8 +837,8 @@ windows_frame_ready(pgCameraObject *self, int *result) *result = self->buffer_ready; if (!self->open) { - RAISERETURN(pgExc_SDLError, "Camera needs to be started to read data", - 0); + return RAISERETURN(pgExc_SDLError, + "Camera needs to be started to read data", 0); } if (!_check_integrity(self)) { @@ -854,8 +855,8 @@ windows_read_raw(pgCameraObject *self) HRESULT hr; if (!self->open) { - RAISERETURN(pgExc_SDLError, "Camera needs to be started to read data", - 0); + return RAISERETURN(pgExc_SDLError, + "Camera needs to be started to read data", 0); } if (!_check_integrity(self)) { @@ -872,8 +873,8 @@ windows_read_raw(pgCameraObject *self) data = PyBytes_FromStringAndSize(buf_data, buf_length); if (!data) { - RAISERETURN(pgExc_SDLError, "Error constructing bytes from data", - 0); + return RAISERETURN(pgExc_SDLError, + "Error constructing bytes from data", 0); } hr = self->raw_buf->lpVtbl->Unlock(self->raw_buf); diff --git a/src_c/circle.c b/src_c/circle.c index f160490d40..70422c3f67 100644 --- a/src_c/circle.c +++ b/src_c/circle.c @@ -44,11 +44,12 @@ static int pg_circle_init(pgCircleObject *self, PyObject *args, PyObject *kwds) { if (!pgCircle_FromObject(args, &self->circle)) { - RAISERETURN(PyExc_TypeError, - "Arguments must be a Circle, a sequence of length 3 or 2, " - "or an object with an attribute called 'circle', all with " - "corresponding nonnegative radius argument", - -1); + return RAISERETURN( + PyExc_TypeError, + "Arguments must be a Circle, a sequence of length 3 or 2, " + "or an object with an attribute called 'circle', all with " + "corresponding nonnegative radius argument", + -1); } return 0; } @@ -331,7 +332,7 @@ _pg_circle_collideswith(pgCircleBase *scirc, PyObject *arg) else if (PySequence_Check(arg)) { double x, y; if (!pg_TwoDoublesFromObj(arg, &x, &y)) { - RAISERETURN( + return RAISERETURN( PyExc_TypeError, "Invalid point argument, must be a sequence of two numbers", -1); @@ -339,9 +340,9 @@ _pg_circle_collideswith(pgCircleBase *scirc, PyObject *arg) result = pgCollision_CirclePoint(scirc, x, y); } else { - RAISERETURN(PyExc_TypeError, - "Invalid point argument, must be a sequence of 2 numbers", - -1); + return RAISERETURN( + PyExc_TypeError, + "Invalid point argument, must be a sequence of 2 numbers", -1); } return result; @@ -668,7 +669,7 @@ pg_circle_setr(pgCircleObject *self, PyObject *value, void *closure) } if (radius < 0) { - RAISERETURN(PyExc_ValueError, "Radius must be nonnegative", -1); + return RAISERETURN(PyExc_ValueError, "Radius must be nonnegative", -1); } self->circle.r = radius; @@ -690,13 +691,15 @@ pg_circle_setr_sqr(pgCircleObject *self, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_DoubleFromObj(value, &radius_squared)) { - RAISERETURN(PyExc_TypeError, - "Invalid type for radius squared, must be numeric", -1); + return RAISERETURN(PyExc_TypeError, + "Invalid type for radius squared, must be numeric", + -1); } if (radius_squared < 0) { - RAISERETURN(PyExc_ValueError, - "Invalid radius squared value, must be nonnegative", -1); + return RAISERETURN(PyExc_ValueError, + "Invalid radius squared value, must be nonnegative", + -1); } self->circle.r = sqrt(radius_squared); @@ -715,7 +718,8 @@ pg_circle_setcenter(pgCircleObject *self, PyObject *value, void *closure) { DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_TwoDoublesFromObj(value, &self->circle.x, &self->circle.y)) { - RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", -1); + return RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", + -1); } return 0; } @@ -734,13 +738,13 @@ pg_circle_setarea(pgCircleObject *self, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_DoubleFromObj(value, &area)) { - RAISERETURN(PyExc_TypeError, "Invalid type for area, must be numeric", - -1); + return RAISERETURN(PyExc_TypeError, + "Invalid type for area, must be numeric", -1); } if (area < 0) { - RAISERETURN(PyExc_ValueError, - "Invalid area value, must be nonnegative", -1); + return RAISERETURN(PyExc_ValueError, + "Invalid area value, must be nonnegative", -1); } self->circle.r = sqrt(area / M_PI); @@ -763,13 +767,15 @@ pg_circle_setcircumference(pgCircleObject *self, PyObject *value, DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_DoubleFromObj(value, &circumference)) { - RAISERETURN(PyExc_TypeError, - "Invalid type for circumference, must be numeric", -1); + return RAISERETURN(PyExc_TypeError, + "Invalid type for circumference, must be numeric", + -1); } if (circumference < 0) { - RAISERETURN(PyExc_ValueError, - "Invalid circumference value, must be nonnegative", -1); + return RAISERETURN(PyExc_ValueError, + "Invalid circumference value, must be nonnegative", + -1); } self->circle.r = circumference / M_TWOPI; @@ -791,13 +797,13 @@ pg_circle_setdiameter(pgCircleObject *self, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_DoubleFromObj(value, &diameter)) { - RAISERETURN(PyExc_TypeError, - "Invalid type for diameter, must be numeric", -1); + return RAISERETURN(PyExc_TypeError, + "Invalid type for diameter, must be numeric", -1); } if (diameter < 0) { - RAISERETURN(PyExc_ValueError, - "Invalid diameter value, must be nonnegative", -1); + return RAISERETURN(PyExc_ValueError, + "Invalid diameter value, must be nonnegative", -1); } self->circle.r = diameter / 2; @@ -820,7 +826,8 @@ pg_circle_settop(pgCircleObject *self, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_TwoDoublesFromObj(value, &x, &y)) { - RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", -1); + return RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", + -1); } self->circle.y = y + self->circle.r; @@ -844,7 +851,8 @@ pg_circle_setleft(pgCircleObject *self, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_TwoDoublesFromObj(value, &x, &y)) { - RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", -1); + return RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", + -1); } self->circle.x = x + self->circle.r; @@ -868,7 +876,8 @@ pg_circle_setbottom(pgCircleObject *self, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_TwoDoublesFromObj(value, &x, &y)) { - RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", -1); + return RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", + -1); } self->circle.y = y - self->circle.r; @@ -892,7 +901,8 @@ pg_circle_setright(pgCircleObject *self, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); if (!pg_TwoDoublesFromObj(value, &x, &y)) { - RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", -1); + return RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", + -1); } self->circle.x = x - self->circle.r; diff --git a/src_c/color.c b/src_c/color.c index 272dd31452..0f541efe46 100644 --- a/src_c/color.c +++ b/src_c/color.c @@ -356,9 +356,9 @@ _get_color_int_component(PyObject *val, Uint8 *color) if (PyLong_Check(val)) { unsigned long longval = PyLong_AsUnsignedLong(val); if (PyErr_Occurred() || (longval > 255)) { - RAISERETURN(PyExc_ValueError, - "invalid color component (must be in range [0, 255])", - 0); + return RAISERETURN( + PyExc_ValueError, + "invalid color component (must be in range [0, 255])", 0); } *color = (Uint8)longval; return 1; @@ -623,7 +623,7 @@ _parse_color_from_text(PyObject *str_obj, Uint8 *rgba) color = PyDict_GetItem(_COLORDICT, name2); Py_DECREF(name2); if (!color) { - RAISERETURN(PyExc_ValueError, "invalid color name", -1); + return RAISERETURN(PyExc_ValueError, "invalid color name", -1); } } @@ -1033,7 +1033,7 @@ _color_set_hsva(pgColorObject *color, PyObject *value, void *closure) if (!PySequence_Check(value) || PySequence_Size(value) < 3 || PySequence_Size(value) > 4) { - RAISERETURN(PyExc_ValueError, "invalid HSVA value", -1); + return RAISERETURN(PyExc_ValueError, "invalid HSVA value", -1); } /* H */ @@ -1041,7 +1041,7 @@ _color_set_hsva(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(hsva[0])) || hsva[0] < 0 || hsva[0] > 360) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid HSVA value", -1); + return RAISERETURN(PyExc_ValueError, "invalid HSVA value", -1); } Py_DECREF(item); @@ -1050,7 +1050,7 @@ _color_set_hsva(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(hsva[1])) || hsva[1] < 0 || hsva[1] > 100) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid HSVA value", -1); + return RAISERETURN(PyExc_ValueError, "invalid HSVA value", -1); } Py_DECREF(item); @@ -1059,7 +1059,7 @@ _color_set_hsva(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(hsva[2])) || hsva[2] < 0 || hsva[2] > 100) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid HSVA value", -1); + return RAISERETURN(PyExc_ValueError, "invalid HSVA value", -1); } Py_DECREF(item); @@ -1069,7 +1069,7 @@ _color_set_hsva(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(hsva[3])) || hsva[3] < 0 || hsva[3] > 100) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid HSVA value", -1); + return RAISERETURN(PyExc_ValueError, "invalid HSVA value", -1); } Py_DECREF(item); } @@ -1194,7 +1194,7 @@ _color_set_hsla(pgColorObject *color, PyObject *value, void *closure) if (!PySequence_Check(value) || PySequence_Size(value) < 3 || PySequence_Size(value) > 4) { - RAISERETURN(PyExc_ValueError, "invalid HSLA value", -1); + return RAISERETURN(PyExc_ValueError, "invalid HSLA value", -1); } /* H */ @@ -1202,7 +1202,7 @@ _color_set_hsla(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(hsla[0])) || hsla[0] < 0 || hsla[0] > 360) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid HSLA value", -1); + return RAISERETURN(PyExc_ValueError, "invalid HSLA value", -1); } Py_DECREF(item); @@ -1211,7 +1211,7 @@ _color_set_hsla(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(hsla[1])) || hsla[1] < 0 || hsla[1] > 100) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid HSLA value", -1); + return RAISERETURN(PyExc_ValueError, "invalid HSLA value", -1); } Py_DECREF(item); @@ -1220,7 +1220,7 @@ _color_set_hsla(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(hsla[2])) || hsla[2] < 0 || hsla[2] > 100) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid HSLA value", -1); + return RAISERETURN(PyExc_ValueError, "invalid HSLA value", -1); } Py_DECREF(item); @@ -1230,7 +1230,7 @@ _color_set_hsla(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(hsla[3])) || hsla[3] < 0 || hsla[3] > 100) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid HSLA value", -1); + return RAISERETURN(PyExc_ValueError, "invalid HSLA value", -1); } Py_DECREF(item); } @@ -1354,7 +1354,7 @@ _color_set_i1i2i3(pgColorObject *color, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK("i1i2i3", value); if (!PySequence_Check(value) || PySequence_Size(value) != 3) { - RAISERETURN(PyExc_ValueError, "invalid I1I2I3 value", -1); + return RAISERETURN(PyExc_ValueError, "invalid I1I2I3 value", -1); } /* I1 */ @@ -1362,7 +1362,7 @@ _color_set_i1i2i3(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(i1i2i3[0])) || i1i2i3[0] < 0 || i1i2i3[0] > 1) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid I1I2I3 value", -1); + return RAISERETURN(PyExc_ValueError, "invalid I1I2I3 value", -1); } Py_DECREF(item); @@ -1371,7 +1371,7 @@ _color_set_i1i2i3(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(i1i2i3[1])) || i1i2i3[1] < -0.5f || i1i2i3[1] > 0.5f) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid I1I2I3 value", -1); + return RAISERETURN(PyExc_ValueError, "invalid I1I2I3 value", -1); } Py_DECREF(item); @@ -1380,7 +1380,7 @@ _color_set_i1i2i3(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(i1i2i3[2])) || i1i2i3[2] < -0.5f || i1i2i3[2] > 0.5f) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid I1I2I3 value", -1); + return RAISERETURN(PyExc_ValueError, "invalid I1I2I3 value", -1); } Py_DECREF(item); @@ -1422,14 +1422,14 @@ _color_set_cmy(pgColorObject *color, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK("cmy", value); if (!PySequence_Check(value) || PySequence_Size(value) != 3) { - RAISERETURN(PyExc_ValueError, "invalid CMY value", -1); + return RAISERETURN(PyExc_ValueError, "invalid CMY value", -1); } /* I1 */ item = PySequence_GetItem(value, 0); if (!item || !_get_double(item, &(cmy[0])) || cmy[0] < 0 || cmy[0] > 1) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid CMY value", -1); + return RAISERETURN(PyExc_ValueError, "invalid CMY value", -1); } Py_DECREF(item); @@ -1437,7 +1437,7 @@ _color_set_cmy(pgColorObject *color, PyObject *value, void *closure) item = PySequence_GetItem(value, 1); if (!item || !_get_double(item, &(cmy[1])) || cmy[1] < 0 || cmy[1] > 1) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid CMY value", -1); + return RAISERETURN(PyExc_ValueError, "invalid CMY value", -1); } Py_DECREF(item); @@ -1445,7 +1445,7 @@ _color_set_cmy(pgColorObject *color, PyObject *value, void *closure) item = PySequence_GetItem(value, 2); if (!item || !_get_double(item, &(cmy[2])) || cmy[2] < 0 || cmy[2] > 1) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid CMY value", -1); + return RAISERETURN(PyExc_ValueError, "invalid CMY value", -1); } Py_DECREF(item); @@ -1479,14 +1479,14 @@ _color_set_normalized(pgColorObject *color, PyObject *value, void *closure) if (!PySequence_Check(value) || PySequence_Size(value) < 3 || PySequence_Size(value) > 4) { - RAISERETURN(PyExc_ValueError, "invalid normalized value", -1); + return RAISERETURN(PyExc_ValueError, "invalid normalized value", -1); } item = PySequence_GetItem(value, 0); if (!item || !_get_double(item, &(frgba[0])) || frgba[0] < 0.0 || frgba[0] > 1.0) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid normalized value", -1); + return RAISERETURN(PyExc_ValueError, "invalid normalized value", -1); } Py_DECREF(item); @@ -1494,7 +1494,7 @@ _color_set_normalized(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(frgba[1])) || frgba[1] < 0.0 || frgba[1] > 1.0) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid normalized value", -1); + return RAISERETURN(PyExc_ValueError, "invalid normalized value", -1); } Py_DECREF(item); @@ -1502,7 +1502,7 @@ _color_set_normalized(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(frgba[2])) || frgba[2] < 0.0 || frgba[2] > 1.0) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid normalized value", -1); + return RAISERETURN(PyExc_ValueError, "invalid normalized value", -1); } Py_DECREF(item); @@ -1511,7 +1511,8 @@ _color_set_normalized(pgColorObject *color, PyObject *value, void *closure) if (!item || !_get_double(item, &(frgba[3])) || frgba[3] < 0.0 || frgba[3] > 1.0) { Py_XDECREF(item); - RAISERETURN(PyExc_ValueError, "invalid normalized value", -1); + return RAISERETURN(PyExc_ValueError, "invalid normalized value", + -1); } Py_DECREF(item); } @@ -1538,12 +1539,12 @@ _color_set_hex(pgColorObject *color, PyObject *value, void *closure) DEL_ATTR_NOT_SUPPORTED_CHECK("hex", value); if (!PyUnicode_Check(value)) { - RAISERETURN(PyExc_TypeError, "hex color must be a string", -1); + return RAISERETURN(PyExc_TypeError, "hex color must be a string", -1); } switch (_hexcolor(value, color->data)) { case TRISTATE_FAIL: - RAISERETURN(PyExc_ValueError, "invalid hex string", -1); + return RAISERETURN(PyExc_ValueError, "invalid hex string", -1); case TRISTATE_ERROR: return -1; /* forward python error */ default: @@ -1847,7 +1848,7 @@ _color_ass_item(pgColorObject *color, Py_ssize_t _index, PyObject *value) case 3: return _color_set_a(color, value, NULL); default: - RAISERETURN(PyExc_IndexError, "invalid index", -1); + return RAISERETURN(PyExc_IndexError, "invalid index", -1); } return -1; } @@ -1918,8 +1919,8 @@ static int _color_contains(pgColorObject *self, PyObject *arg) { if (!PyLong_Check(arg)) { - RAISERETURN(PyExc_TypeError, - "'in ' requires integer object", -1); + return RAISERETURN(PyExc_TypeError, + "'in ' requires integer object", -1); } long comp = PyLong_AsLong(arg); @@ -1941,8 +1942,8 @@ static int _color_set_slice(pgColorObject *color, PyObject *idx, PyObject *val) { if (val == NULL) { - RAISERETURN(PyExc_TypeError, - "Color object doesn't support item deletion", -1); + return RAISERETURN(PyExc_TypeError, + "Color object doesn't support item deletion", -1); } if (PyLong_Check(idx)) { return _color_ass_item(color, PyLong_AsLong(idx), val); @@ -1980,13 +1981,13 @@ _color_set_slice(pgColorObject *color, PyObject *idx, PyObject *val) } else { Py_DECREF(fastitems); - RAISERETURN(PyExc_TypeError, - "color components must be integers", -1); + return RAISERETURN(PyExc_TypeError, + "color components must be integers", -1); } if (c < 0 || c > 255) { Py_DECREF(fastitems); - RAISERETURN(PyExc_TypeError, "color component must be 0-255", - -1); + return RAISERETURN(PyExc_TypeError, + "color component must be 0-255", -1); } color->data[cur] = (Uint8)c; } @@ -1994,7 +1995,8 @@ _color_set_slice(pgColorObject *color, PyObject *idx, PyObject *val) Py_DECREF(fastitems); return 0; } - RAISERETURN(PyExc_IndexError, "Index must be an integer or slice", -1); + return RAISERETURN(PyExc_IndexError, "Index must be an integer or slice", + -1); } /* @@ -2047,7 +2049,7 @@ _color_getbuffer(pgColorObject *color, Py_buffer *view, int flags) static char format[] = "B"; if (PyBUF_HAS_FLAG(flags, PyBUF_WRITABLE)) { - RAISERETURN(pgExc_BufferError, "color buffer is read-only", -1); + return RAISERETURN(pgExc_BufferError, "color buffer is read-only", -1); } view->buf = color->data; view->ndim = 1; @@ -2347,8 +2349,9 @@ _pg_pylong_to_uint32(PyObject *val, Uint32 *color, int handle_negative) return 1; error: - RAISERETURN(PyExc_ValueError, - "invalid color argument (integer out of acceptable range)", 0); + return RAISERETURN( + PyExc_ValueError, + "invalid color argument (integer out of acceptable range)", 0); } static int @@ -2380,10 +2383,11 @@ pg_RGBAFromObjEx(PyObject *obj, Uint8 *rgba, pgColorHandleFlags handle_flags) if ((handle_flags & PG_COLOR_HANDLE_RESTRICT_SEQ) && !PyTuple_Check(obj)) { /* ValueError (and not TypeError) for backcompat reasons */ - RAISERETURN(PyExc_ValueError, - "invalid color (here, generic sequences are restricted, " - "but pygame.Color and RGB[A] tuples are allowed)", - 0); + return RAISERETURN( + PyExc_ValueError, + "invalid color (here, generic sequences are restricted, " + "but pygame.Color and RGB[A] tuples are allowed)", + 0); } if (pg_RGBAFromObj(obj, rgba)) { @@ -2394,10 +2398,11 @@ pg_RGBAFromObjEx(PyObject *obj, Uint8 *rgba, pgColorHandleFlags handle_flags) /* Error */ if (PySequence_Check(obj)) { /* It was a tuple-like; raise a ValueError */ - RAISERETURN(PyExc_ValueError, - "invalid color (color sequence must have size 3 or 4, and " - "each element must be an integer in the range [0, 255])", - 0); + return RAISERETURN( + PyExc_ValueError, + "invalid color (color sequence must have size 3 or 4, and " + "each element must be an integer in the range [0, 255])", + 0); } else { PyErr_Format(PyExc_TypeError, diff --git a/src_c/display.c b/src_c/display.c index bf188c62cc..81e891d4f3 100644 --- a/src_c/display.c +++ b/src_c/display.c @@ -1492,7 +1492,8 @@ _pg_get_default_display_masks(int bpp, Uint32 *Rmask, Uint32 *Gmask, *Bmask = 0xFF; break; default: - RAISERETURN(PyExc_ValueError, "nonstandard bit depth given", -1); + return RAISERETURN(PyExc_ValueError, "nonstandard bit depth given", + -1); } return 0; } @@ -1690,11 +1691,11 @@ pg_flip_internal(_DisplayState *state) /* Same check as VIDEO_INIT_CHECK() but returns -1 instead of NULL on * fail. */ if (!SDL_WasInit(SDL_INIT_VIDEO)) { - RAISERETURN(pgExc_SDLError, "video system not initialized", -1); + return RAISERETURN(pgExc_SDLError, "video system not initialized", -1); } if (!win) { - RAISERETURN(pgExc_SDLError, "Display mode not set", -1); + return RAISERETURN(pgExc_SDLError, "Display mode not set", -1); } Py_BEGIN_ALLOW_THREADS; @@ -1727,7 +1728,7 @@ pg_flip_internal(_DisplayState *state) Py_END_ALLOW_THREADS; if (status < 0) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } return 0; @@ -2057,15 +2058,15 @@ pg_convert_to_uint16(PyObject *python_array, Uint16 *c_uint16_array) PyObject *item; if (!c_uint16_array) { - RAISERETURN(PyExc_RuntimeError, - "Memory not allocated for c_uint16_array.", 0); + return RAISERETURN(PyExc_RuntimeError, + "Memory not allocated for c_uint16_array.", 0); } if (!PySequence_Check(python_array)) { - RAISERETURN(PyExc_TypeError, "Array must be sequence type", 0); + return RAISERETURN(PyExc_TypeError, "Array must be sequence type", 0); } if (PySequence_Size(python_array) != 256) { - RAISERETURN(PyExc_ValueError, "gamma ramp must be 256 elements long", - 0); + return RAISERETURN(PyExc_ValueError, + "gamma ramp must be 256 elements long", 0); } for (i = 0; i < 256; i++) { long ret; @@ -2074,8 +2075,8 @@ pg_convert_to_uint16(PyObject *python_array, Uint16 *c_uint16_array) return 0; } if (!PyLong_Check(item)) { - RAISERETURN(PyExc_ValueError, - "gamma ramp must contain integer elements", 0); + return RAISERETURN(PyExc_ValueError, + "gamma ramp must contain integer elements", 0); } ret = PyLong_AsLong(item); Py_XDECREF(item); @@ -2084,9 +2085,9 @@ pg_convert_to_uint16(PyObject *python_array, Uint16 *c_uint16_array) /* Happens when PyLong_AsLong overflows */ return 0; } - RAISERETURN(PyExc_ValueError, - "integers in gamma ramp must be between 0 and 0xFFFF", - 0); + return RAISERETURN( + PyExc_ValueError, + "integers in gamma ramp must be between 0 and 0xFFFF", 0); } c_uint16_array[i] = (Uint16)ret; } diff --git a/src_c/draw.c b/src_c/draw.c index 1d69932c76..3098174c76 100644 --- a/src_c/draw.c +++ b/src_c/draw.c @@ -833,12 +833,13 @@ circle(PyObject *self, PyObject *args, PyObject *kwargs) } if (!pg_TwoIntsFromObj(posobj, &posx, &posy)) { - RAISERETURN(PyExc_TypeError, - "center argument must be a pair of numbers", 0) + return RAISERETURN(PyExc_TypeError, + "center argument must be a pair of numbers", 0); } if (!pg_IntFromObj(radiusobj, &radius)) { - RAISERETURN(PyExc_TypeError, "radius argument must be a number", 0); + return RAISERETURN(PyExc_TypeError, "radius argument must be a number", + 0); } surf = pgSurface_AsSurface(surfobj); diff --git a/src_c/event.c b/src_c/event.c index c11d0d6df3..e79d7f0de0 100644 --- a/src_c/event.c +++ b/src_c/event.c @@ -718,8 +718,8 @@ static int pg_EnableKeyRepeat(int delay, int interval) { if (delay < 0 || interval < 0) { - RAISERETURN(PyExc_ValueError, - "delay and interval must equal at least 0", -1); + return RAISERETURN(PyExc_ValueError, + "delay and interval must equal at least 0", -1); } PG_LOCK_EVFILTER_MUTEX pg_key_repeat_delay = delay; @@ -1676,7 +1676,7 @@ pg_event_init(pgEventObject *self, PyObject *args, PyObject *kwargs) } if (type < 0 || type >= PG_NUMEVENTS) { - RAISERETURN(PyExc_ValueError, "event type out of range", -1); + return RAISERETURN(PyExc_ValueError, "event type out of range", -1); } if (!dict) { @@ -1704,8 +1704,8 @@ pg_event_init(pgEventObject *self, PyObject *args, PyObject *kwargs) if (PyDict_GetItemString(dict, "type")) { Py_DECREF(dict); - RAISERETURN(PyExc_ValueError, "redundant type field in event dict", - -1); + return RAISERETURN(PyExc_ValueError, + "redundant type field in event dict", -1); } self->type = _pg_pgevent_deproxify(type); @@ -1936,11 +1936,11 @@ _pg_eventtype_from_seq(PyObject *seq, int ind) { int val = 0; if (!pg_IntFromObjIndex(seq, ind, &val)) { - RAISERETURN(PyExc_TypeError, - "type sequence must contain valid event types", -1); + return RAISERETURN(PyExc_TypeError, + "type sequence must contain valid event types", -1); } if (val < 0 || val >= PG_NUMEVENTS) { - RAISERETURN(PyExc_ValueError, "event type out of range", -1); + return RAISERETURN(PyExc_ValueError, "event type out of range", -1); } return val; } diff --git a/src_c/font.c b/src_c/font.c index 07a1984bca..99b6f5dce7 100644 --- a/src_c/font.c +++ b/src_c/font.c @@ -40,9 +40,10 @@ #define RAISE_TEXT_TYPE_ERROR() \ RAISE(PyExc_TypeError, "text must be a unicode or bytes"); -#define RAISE_FONT_QUIT_ERROR_RETURN(r) \ - RAISERETURN(pgExc_SDLError, \ - "Invalid font (font module quit since font created)", r) +#define RAISE_FONT_QUIT_ERROR_RETURN(r) \ + return RAISERETURN(pgExc_SDLError, \ + "Invalid font (font module quit since font created)", \ + r) #define RAISE_FONT_QUIT_ERROR() \ RAISE(pgExc_SDLError, \ @@ -536,14 +537,15 @@ font_setter_align(PyObject *self, PyObject *value, void *closure) long val = PyLong_AsLong(value); if (val == -1 && PyErr_Occurred()) { - RAISERETURN(PyExc_TypeError, - "font.align must be an integer. Must correspond with " - "FONT_LEFT, FONT_CENTER, or FONT_RIGHT.", - -1); + return RAISERETURN( + PyExc_TypeError, + "font.align must be an integer. Must correspond with " + "FONT_LEFT, FONT_CENTER, or FONT_RIGHT.", + -1); } if (val < 0 || val > 2) { - RAISERETURN( + return RAISERETURN( pgExc_SDLError, "font.align must be FONT_LEFT, FONT_CENTER, or FONT_RIGHT.", -1); } @@ -555,10 +557,11 @@ font_setter_align(PyObject *self, PyObject *value, void *closure) #endif return 0; #else - RAISERETURN(pgExc_SDLError, - "pygame.font not compiled with a new enough SDL_ttf version. " - "Needs SDL_ttf 2.20.0 or above.", - -1); + return RAISERETURN( + pgExc_SDLError, + "pygame.font not compiled with a new enough SDL_ttf version. " + "Needs SDL_ttf 2.20.0 or above.", + -1); #endif } @@ -821,8 +824,9 @@ font_setter_point_size(PyFontObject *self, PyObject *value, void *closure) } if (val <= 0) { - RAISERETURN(PyExc_ValueError, - "point_size cannot be equal to, or less than 0", -1); + return RAISERETURN(PyExc_ValueError, + "point_size cannot be equal to, or less than 0", + -1); } #if SDL_TTF_VERSION_ATLEAST(3, 0, 0) @@ -832,14 +836,14 @@ font_setter_point_size(PyFontObject *self, PyObject *value, void *closure) if (TTF_SetFontSize(font, val) == -1) #endif { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } self->ptsize = val; return 0; #else - RAISERETURN(pgExc_SDLError, "Incorrect SDL_TTF version (requires 2.0.18)", - -1); + return RAISERETURN(pgExc_SDLError, + "Incorrect SDL_TTF version (requires 2.0.18)", -1); #endif } @@ -1238,7 +1242,7 @@ font_init(PyFontObject *self, PyObject *args, PyObject *kwds) } if (!font_initialized) { - RAISERETURN(pgExc_SDLError, "font not initialized", -1); + return RAISERETURN(pgExc_SDLError, "font not initialized", -1); } /* Incref obj, needs to be decref'd later */ diff --git a/src_c/freetype/ft_layout.c b/src_c/freetype/ft_layout.c index a4a5db930d..65a6d69007 100644 --- a/src_c/freetype/ft_layout.c +++ b/src_c/freetype/ft_layout.c @@ -139,7 +139,7 @@ _PGFT_LoadLayout(FreeTypeInstance *ft, pgFontObject *fontobj, copy_mode(&ftext->mode, mode); font = _PGFT_GetFontSized(ft, fontobj, mode->face_size); if (!font) { - RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } } @@ -212,7 +212,7 @@ size_text(Layout *ftext, FreeTypeInstance *ft, TextContext *context, &slots[length].kerning); if (error) { _PGFT_SetError(ft, "Loading glyphs", error); - RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), -1); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), -1); } } prev_id = id; diff --git a/src_c/freetype/ft_render.c b/src_c/freetype/ft_render.c index 62bd97413d..c300655b76 100644 --- a/src_c/freetype/ft_render.c +++ b/src_c/freetype/ft_render.c @@ -126,10 +126,11 @@ _PGFT_BuildRenderMode(FreeTypeInstance *ft, pgFontObject *fontobj, { if (face_size.x == 0) { if (fontobj->face_size.x == 0) { - RAISERETURN(PyExc_ValueError, - "No font point size specified and no default font " - "size in typeface", - -1); + return RAISERETURN( + PyExc_ValueError, + "No font point size specified and no default font " + "size in typeface", + -1); } face_size = fontobj->face_size; @@ -141,14 +142,14 @@ _PGFT_BuildRenderMode(FreeTypeInstance *ft, pgFontObject *fontobj, } else { if (_PGFT_CheckStyle((FT_UInt32)style)) { - RAISERETURN(PyExc_ValueError, "Invalid style value", -1); + return RAISERETURN(PyExc_ValueError, "Invalid style value", -1); } mode->style = (FT_UInt16)style; } if ((mode->style & FT_STYLES_SCALABLE_ONLY) && !fontobj->is_scalable) { - RAISERETURN(PyExc_ValueError, "Unsupported style(s) for a bitmap font", - -1); + return RAISERETURN(PyExc_ValueError, + "Unsupported style(s) for a bitmap font", -1); } mode->strength = DBL_TO_FX16(fontobj->strength); mode->underline_adjustment = DBL_TO_FX16(fontobj->underline_adjustment); @@ -158,29 +159,31 @@ _PGFT_BuildRenderMode(FreeTypeInstance *ft, pgFontObject *fontobj, if (mode->rotation_angle != 0) { if (!fontobj->is_scalable) { - RAISERETURN(PyExc_ValueError, - "rotated text is unsupported for a bitmap font", -1); + return RAISERETURN(PyExc_ValueError, + "rotated text is unsupported for a bitmap font", + -1); } if (mode->style & FT_STYLE_WIDE) { - RAISERETURN(PyExc_ValueError, - "the wide style is unsupported for rotated text", -1); + return RAISERETURN( + PyExc_ValueError, + "the wide style is unsupported for rotated text", -1); } if (mode->style & FT_STYLE_UNDERLINE) { - RAISERETURN(PyExc_ValueError, - "the underline style is unsupported for rotated text", - -1); + return RAISERETURN( + PyExc_ValueError, + "the underline style is unsupported for rotated text", -1); } if (mode->render_flags & FT_RFLAG_PAD) { - RAISERETURN(PyExc_ValueError, - "padding is unsupported for rotated text", -1); + return RAISERETURN(PyExc_ValueError, + "padding is unsupported for rotated text", -1); } } if (mode->render_flags & FT_RFLAG_VERTICAL) { if (mode->style & FT_STYLE_UNDERLINE) { - RAISERETURN(PyExc_ValueError, - "the underline style is unsupported for vertical text", - -1); + return RAISERETURN( + PyExc_ValueError, + "the underline style is unsupported for vertical text", -1); } } @@ -188,7 +191,7 @@ _PGFT_BuildRenderMode(FreeTypeInstance *ft, pgFontObject *fontobj, FT_Face font = _PGFT_GetFontSized(ft, fontobj, mode->face_size); if (!font) { - RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), -1); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), -1); } if (!FT_HAS_KERNING(font)) { @@ -280,7 +283,7 @@ _PGFT_Render_ExistingSurface(FreeTypeInstance *ft, pgFontObject *fontobj, if (SDL_MUSTLOCK(surface)) { if (!PG_LockSurface(surface)) { SDL_FreeSurface(surface); - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } locked = 1; } @@ -328,7 +331,8 @@ _PGFT_Render_ExistingSurface(FreeTypeInstance *ft, pgFontObject *fontobj, if (!PG_SURF_BytesPerPixel(surface)) { // This should never happen, error to make static analyzer happy - RAISERETURN(pgExc_SDLError, "Got surface of invalid format", -1); + return RAISERETURN(pgExc_SDLError, "Got surface of invalid format", + -1); } /* @@ -430,13 +434,13 @@ _PGFT_Render_NewSurface(FreeTypeInstance *ft, pgFontObject *fontobj, } surface = PG_CreateSurface(width, height, pixelformat); if (!surface) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), 0); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), 0); } if (SDL_MUSTLOCK(surface)) { if (!PG_LockSurface(surface)) { SDL_FreeSurface(surface); - RAISERETURN(pgExc_SDLError, SDL_GetError(), 0); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), 0); } locked = 1; } diff --git a/src_c/freetype/ft_wrap.c b/src_c/freetype/ft_wrap.c index 8451d8566f..3f2f3964b3 100644 --- a/src_c/freetype/ft_wrap.c +++ b/src_c/freetype/ft_wrap.c @@ -99,7 +99,7 @@ _PGFT_Font_IsFixedWidth(FreeTypeInstance *ft, pgFontObject *fontobj) FT_Face font = _PGFT_GetFont(ft, fontobj); if (!font) { - RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), -1); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), -1); } return FT_IS_FIXED_WIDTH(font) ? 1 : 0; } @@ -110,7 +110,7 @@ _PGFT_Font_NumFixedSizes(FreeTypeInstance *ft, pgFontObject *fontobj) FT_Face font = _PGFT_GetFont(ft, fontobj); if (!font) { - RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), -1); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), -1); } return FT_HAS_FIXED_SIZES(font) ? font->num_fixed_sizes : 0; } @@ -124,7 +124,7 @@ _PGFT_Font_GetAvailableSize(FreeTypeInstance *ft, pgFontObject *fontobj, FT_Bitmap_Size *bitmap_size_p; if (!font) { - RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), -1); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), -1); } if (!FT_HAS_FIXED_SIZES(font) || n > font->num_fixed_sizes) /* cond. or */ { @@ -146,7 +146,7 @@ _PGFT_Font_GetName(FreeTypeInstance *ft, pgFontObject *fontobj) font = _PGFT_GetFont(ft, fontobj); if (!font) { - RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } return font->family_name ? font->family_name : ""; } @@ -158,7 +158,7 @@ _PGFT_Font_GetStyleName(FreeTypeInstance *ft, pgFontObject *fontobj) font = _PGFT_GetFont(ft, fontobj); if (!font) { - RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } return font->style_name ? font->style_name : ""; } @@ -172,7 +172,7 @@ _PGFT_Font_GetHeight(FreeTypeInstance *ft, pgFontObject *fontobj) FT_Face font = _PGFT_GetFont(ft, fontobj); if (!font) { - RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } return (long)font->height; } @@ -184,7 +184,7 @@ _PGFT_Font_GetHeightSized(FreeTypeInstance *ft, pgFontObject *fontobj, FT_Face font = _PGFT_GetFontSized(ft, fontobj, face_size); if (!font) { - RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } return (long)FX6_TRUNC(FX6_CEIL(font->size->metrics.height)); } @@ -195,7 +195,7 @@ _PGFT_Font_GetAscender(FreeTypeInstance *ft, pgFontObject *fontobj) FT_Face font = _PGFT_GetFont(ft, fontobj); if (!font) { - RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } return (long)font->ascender; } @@ -207,7 +207,7 @@ _PGFT_Font_GetAscenderSized(FreeTypeInstance *ft, pgFontObject *fontobj, FT_Face font = _PGFT_GetFontSized(ft, fontobj, face_size); if (!font) { - RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } return (long)FX6_TRUNC(FX6_CEIL(font->size->metrics.ascender)); } @@ -218,7 +218,7 @@ _PGFT_Font_GetDescender(FreeTypeInstance *ft, pgFontObject *fontobj) FT_Face font = _PGFT_GetFont(ft, fontobj); if (!font) { - RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } return (long)font->descender; } @@ -230,7 +230,7 @@ _PGFT_Font_GetDescenderSized(FreeTypeInstance *ft, pgFontObject *fontobj, FT_Face font = _PGFT_GetFontSized(ft, fontobj, face_size); if (!font) { - RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } return (long)FX6_TRUNC(FX6_FLOOR(font->size->metrics.descender)); } @@ -246,7 +246,7 @@ _PGFT_Font_GetGlyphHeightSized(FreeTypeInstance *ft, pgFontObject *fontobj, FT_Size_Metrics *metrics; if (!font) { - RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); } metrics = &font->size->metrics; return (long)FX6_TRUNC(FX6_CEIL(metrics->ascender) - @@ -386,7 +386,7 @@ ft_wrap_init(FreeTypeInstance *ft, pgFontObject *fontobj) font = _PGFT_GetFont(ft, fontobj); if (!font) { - RAISERETURN(PyExc_FileNotFoundError, _PGFT_GetError(ft), -1); + return RAISERETURN(PyExc_FileNotFoundError, _PGFT_GetError(ft), -1); } fontobj->is_scalable = FT_IS_SCALABLE(font) ? ~0 : 0; @@ -478,7 +478,8 @@ _PGFT_TryLoadFont_RWops(FreeTypeInstance *ft, pgFontObject *fontobj, position = SDL_RWtell(src); if (position < 0) { - RAISERETURN(pgExc_SDLError, "Failed to seek in font stream", -1); + return RAISERETURN(pgExc_SDLError, "Failed to seek in font stream", + -1); } stream = _PGFT_calloc(1, sizeof(*stream)); diff --git a/src_c/freetype/ft_wrap.h b/src_c/freetype/ft_wrap.h index 14d9dd31ac..df86967f2f 100644 --- a/src_c/freetype/ft_wrap.h +++ b/src_c/freetype/ft_wrap.h @@ -241,11 +241,12 @@ extern _FreeTypeState _modstate; #define FREETYPE_STATE FREETYPE_MOD_STATE(0) #endif /* defined(PYPY_VERSION) */ -#define ASSERT_GRAB_FREETYPE(ft_ptr, rvalue) \ - ft_ptr = FREETYPE_STATE->freetype; \ - if (!ft_ptr) { \ - RAISERETURN(PyExc_RuntimeError, \ - "The FreeType 2 library hasn't been initialized", rvalue) \ +#define ASSERT_GRAB_FREETYPE(ft_ptr, rvalue) \ + ft_ptr = FREETYPE_STATE->freetype; \ + if (!ft_ptr) { \ + return RAISERETURN(PyExc_RuntimeError, \ + "The FreeType 2 library hasn't been initialized", \ + rvalue); \ } /********************************************************** diff --git a/src_c/line.c b/src_c/line.c index f8838613ff..15fb0f443b 100644 --- a/src_c/line.c +++ b/src_c/line.c @@ -50,10 +50,10 @@ static int pg_line_init(pgLineObject *self, PyObject *args, PyObject *kwds) { if (!pgLine_FromObject(args, &self->line)) { - RAISERETURN(PyExc_TypeError, - "Invalid line end points, expected 4 numbers or 2 " - "sequences of 2 numbers", - -1); + return RAISERETURN(PyExc_TypeError, + "Invalid line end points, expected 4 numbers or 2 " + "sequences of 2 numbers", + -1); } return 0; } @@ -147,12 +147,13 @@ _line_scale_helper(pgLineBase *line, double factor, double origin) return 1; } else if (factor <= 0.0) { - RAISERETURN(PyExc_ValueError, - "Can only scale by a positive non zero number", 0); + return RAISERETURN(PyExc_ValueError, + "Can only scale by a positive non zero number", 0); } if (origin < 0.0 || origin > 1.0) { - RAISERETURN(PyExc_ValueError, "Origin must be between 0 and 1", 0); + return RAISERETURN(PyExc_ValueError, "Origin must be between 0 and 1", + 0); } double ax = line->ax; @@ -289,7 +290,7 @@ pg_line_str(pgLineObject *self) self->line.name = val; \ return 0; \ } \ - RAISERETURN(PyExc_TypeError, "Expected a number", -1); \ + return RAISERETURN(PyExc_TypeError, "Expected a number", -1); \ } __LINE_GETSET_NAME(ax) @@ -314,7 +315,8 @@ pg_line_seta(pgLineObject *self, PyObject *value, void *closure) self->line.ay = y; return 0; } - RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", -1); + return RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", + -1); } static PyObject * @@ -333,7 +335,8 @@ pg_line_setb(pgLineObject *self, PyObject *value, void *closure) self->line.by = y; return 0; } - RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", -1); + return RAISERETURN(PyExc_TypeError, "Expected a sequence of 2 numbers", + -1); } static PyObject * diff --git a/src_c/mask.c b/src_c/mask.c index c8b0684a7c..5002dedaf5 100644 --- a/src_c/mask.c +++ b/src_c/mask.c @@ -2548,19 +2548,19 @@ mask_init(PyObject *self, PyObject *args, PyObject *kwargs) } if (!pg_TwoIntsFromObj(size, &w, &h)) { - RAISERETURN(PyExc_TypeError, "size must be two numbers", -1); + return RAISERETURN(PyExc_TypeError, "size must be two numbers", -1); } if (w < 0 || h < 0) { - RAISERETURN(PyExc_ValueError, "cannot create mask with negative size", - -1); + return RAISERETURN(PyExc_ValueError, + "cannot create mask with negative size", -1); } bitmask = bitmask_create(w, h); if (NULL == bitmask) { - RAISERETURN(PyExc_MemoryError, "cannot allocate memory for bitmask", - -1); + return RAISERETURN(PyExc_MemoryError, + "cannot allocate memory for bitmask", -1); } if (fill) { diff --git a/src_c/math.c b/src_c/math.c index dcbd91173d..7292d6f55a 100644 --- a/src_c/math.c +++ b/src_c/math.c @@ -386,7 +386,7 @@ PySequence_GetItem_AsDouble(PyObject *seq, Py_ssize_t index) item = PySequence_GetItem(seq, index); if (item == NULL) { - RAISERETURN(PyExc_TypeError, "a sequence is expected", -1); + return RAISERETURN(PyExc_TypeError, "a sequence is expected", -1); } value = PyFloat_AsDouble(item); Py_DECREF(item); @@ -407,7 +407,8 @@ PySequence_AsVectorCoords(PyObject *seq, double *const coords, return 1; } if (!PySequence_Check(seq) || PySequence_Length(seq) != size) { - RAISERETURN(PyExc_ValueError, "Sequence has the wrong length.", 0); + return RAISERETURN(PyExc_ValueError, "Sequence has the wrong length.", + 0); } for (i = 0; i < size; ++i) { @@ -437,8 +438,9 @@ pgVectorCompatible_Check(PyObject *obj, Py_ssize_t dim) } break; default: - RAISERETURN(PyExc_SystemError, - "Wrong internal call to pgVectorCompatible_Check.", 0); + return RAISERETURN( + PyExc_SystemError, + "Wrong internal call to pgVectorCompatible_Check.", 0); } if (!PySequence_Check(obj) || (PySequence_Length(obj) != dim)) { @@ -482,8 +484,9 @@ pg_VectorCoordsFromObj(PyObject *obj, Py_ssize_t dim, double *const coords) } break; default: - RAISERETURN(PyExc_SystemError, - "Wrong internal call to pg_VectorCoordsFromObj.", 0); + return RAISERETURN( + PyExc_SystemError, + "Wrong internal call to pg_VectorCoordsFromObj.", 0); } if (!PySequence_Check(obj) || (PySequence_Length(obj) != dim)) { @@ -522,8 +525,9 @@ get_double_from_unicode_slice(PyObject *unicode_obj, Py_ssize_t idx1, PyObject *float_obj; PyObject *slice = PySequence_GetSlice(unicode_obj, idx1, idx2); if (slice == NULL) { - RAISERETURN(PyExc_SystemError, - "internal error while converting str slice to float", -1); + return RAISERETURN( + PyExc_SystemError, + "internal error while converting str slice to float", -1); } float_obj = PyFloat_FromString(slice); Py_DECREF(slice); @@ -1024,10 +1028,11 @@ static int vector_SetItem(pgVector *self, Py_ssize_t index, PyObject *value) { if (index < 0 || index >= self->dim) { - RAISERETURN(PyExc_IndexError, "subscript out of range.", -1); + return RAISERETURN(PyExc_IndexError, "subscript out of range.", -1); } if (value == NULL) { - RAISERETURN(PyExc_TypeError, "item deletion is not supported", -1); + return RAISERETURN(PyExc_TypeError, "item deletion is not supported", + -1); } self->coords[index] = PyFloat_AsDouble(value); @@ -1100,8 +1105,8 @@ vector_SetSlice(pgVector *self, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v) double new_coords[VECTOR_MAX_SIZE]; if (v == NULL) { - RAISERETURN(PyExc_TypeError, - "Vector object doesn't support item deletion", -1); + return RAISERETURN(PyExc_TypeError, + "Vector object doesn't support item deletion", -1); } if (ilow < 0) { @@ -1230,8 +1235,9 @@ vector_ass_subscript(pgVector *self, PyObject *key, PyObject *value) if (value == NULL) { /* delete slice not supported */ - RAISERETURN(PyExc_TypeError, - "Deletion of vector components is not supported.", -1); + return RAISERETURN( + PyExc_TypeError, + "Deletion of vector components is not supported.", -1); } else { /* assign slice */ @@ -1271,16 +1277,16 @@ vector_set_component(pgVector *self, PyObject *value, int component) if (value == NULL) { switch (component) { case 0: { - RAISERETURN(PyExc_TypeError, "Cannot delete the x attribute", - -1); + return RAISERETURN(PyExc_TypeError, + "Cannot delete the x attribute", -1); } case 1: { - RAISERETURN(PyExc_TypeError, "Cannot delete the y attribute", - -1); + return RAISERETURN(PyExc_TypeError, + "Cannot delete the y attribute", -1); } case 2: { - RAISERETURN(PyExc_TypeError, "Cannot delete the z attribute", - -1); + return RAISERETURN(PyExc_TypeError, + "Cannot delete the z attribute", -1); } default: { PyErr_BadInternalCall(); @@ -1755,7 +1761,8 @@ _vector_reflect_helper(double *dst_coords, const double *src_coords, norm_length = _scalar_product(norm_coords, norm_coords, dim); if (norm_length < epsilon) { - RAISERETURN(PyExc_ValueError, "Normal must not be of length zero.", 0); + return RAISERETURN(PyExc_ValueError, + "Normal must not be of length zero.", 0); } if (norm_length != 1) { norm_length = sqrt(norm_length); @@ -1815,7 +1822,8 @@ _vector_distance_helper(pgVector *self, PyObject *other) double dx, dy; if (dim != otherv->dim) { - RAISERETURN(PyExc_ValueError, "Vectors must be the same size", -1); + return RAISERETURN(PyExc_ValueError, + "Vectors must be the same size", -1); } dx = otherv->coords[0] - self->coords[0]; @@ -1839,8 +1847,9 @@ _vector_distance_helper(pgVector *self, PyObject *other) if (PySequence_Fast_GET_SIZE(fast_seq) != dim) { Py_DECREF(fast_seq); - RAISERETURN(PyExc_ValueError, - "Vector and sequence must be the same size", -1); + return RAISERETURN(PyExc_ValueError, + "Vector and sequence must be the same size", + -1); } for (i = 0; i < dim; ++i) { @@ -1882,16 +1891,18 @@ static int _vector_check_snprintf_success(int return_code, int max_size) { if (return_code < 0) { - RAISERETURN(PyExc_SystemError, - "internal snprintf call went wrong! Please report this to " - "github.com/pygame-community/pygame-ce/issues", - 0); + return RAISERETURN( + PyExc_SystemError, + "internal snprintf call went wrong! Please report this to " + "github.com/pygame-community/pygame-ce/issues", + 0); } if (return_code >= max_size) { - RAISERETURN(PyExc_SystemError, - "Internal buffer too small for snprintf! Please report " - "this to github.com/pygame-community/pygame-ce/issues", - 0); + return RAISERETURN( + PyExc_SystemError, + "Internal buffer too small for snprintf! Please report " + "this to github.com/pygame-community/pygame-ce/issues", + 0); } return 1; } @@ -2169,17 +2180,19 @@ vector_setAttr_swizzle(pgVector *self, PyObject *attr_name, PyObject *val) } return 0; case SWIZZLE_ERR_DOUBLE_IDX: - RAISERETURN(PyExc_AttributeError, - "Attribute assignment conflicts with swizzling.", -1); + return RAISERETURN( + PyExc_AttributeError, + "Attribute assignment conflicts with swizzling.", -1); case SWIZZLE_ERR_EXTRACTION_ERR: /* exception was set by PySequence_GetItem_AsDouble */ return -1; default: /* this should NEVER happen and means a bug in the code */ - RAISERETURN(PyExc_RuntimeError, - "Unhandled error in swizzle code. Please report this " - "bug to github.com/pygame-community/pygame-ce/issues", - -1); + return RAISERETURN( + PyExc_RuntimeError, + "Unhandled error in swizzle code. Please report this " + "bug to github.com/pygame-community/pygame-ce/issues", + -1); } } @@ -2188,7 +2201,7 @@ static Py_ssize_t vector_readbuffer(pgVector *self, Py_ssize_t segment, void **ptrptr) { if (segment != 0) { - RAISERETURN(PyExc_SystemError, "accessing non-existent vector segment", -1); + return RAISERETURN(PyExc_SystemError, "accessing non-existent vector segment", -1); } *ptrptr = self->coords; return self->dim; @@ -2198,7 +2211,7 @@ static Py_ssize_t vector_writebuffer(pgVector *self, Py_ssize_t segment, void **ptrptr) { if (segment != 0) { - RAISERETURN(PyExc_SystemError, "accessing non-existent vector segment", -1); + return RAISERETURN(PyExc_SystemError, "accessing non-existent vector segment", -1); } *ptrptr = self->coords; return self->dim; @@ -2363,10 +2376,11 @@ _vector2_set(pgVector *self, PyObject *xOrSequence, PyObject *y) /* success initialization */ return 0; error: - RAISERETURN(PyExc_ValueError, - "Vector2 must be set with 2 real numbers, a sequence of 2 " - "real numbers, or another Vector2 instance", - -1); + return RAISERETURN( + PyExc_ValueError, + "Vector2 must be set with 2 real numbers, a sequence of 2 " + "real numbers, or another Vector2 instance", + -1); } static int @@ -2432,11 +2446,12 @@ _vector2_rotate_helper(double *dst_coords, const double *src_coords, break; default: /* this should NEVER happen and means a bug in the code */ - RAISERETURN(PyExc_RuntimeError, - "Please report this bug in vector2_rotate_helper " - "to the developers at " - "github.com/pygame-community/pygame-ce/issues", - 0); + return RAISERETURN( + PyExc_RuntimeError, + "Please report this bug in vector2_rotate_helper " + "to the developers at " + "github.com/pygame-community/pygame-ce/issues", + 0); } } else { @@ -2810,10 +2825,11 @@ _vector3_set(pgVector *self, PyObject *xOrSequence, PyObject *y, PyObject *z) /* success initialization */ return 0; error: - RAISERETURN(PyExc_ValueError, - "Vector3 must be set with 3 real numbers, a sequence of 3 " - "real numbers, or another Vector3 instance", - -1); + return RAISERETURN( + PyExc_ValueError, + "Vector3 must be set with 3 real numbers, a sequence of 3 " + "real numbers, or another Vector3 instance", + -1); } static int @@ -2869,7 +2885,8 @@ _vector3_rotate_helper(double *dst_coords, const double *src_coords, /* Rotation axis may not be Zero */ if (axisLength2 < epsilon) { - RAISERETURN(PyExc_ValueError, "Rotation Axis is to close to Zero", 0); + return RAISERETURN(PyExc_ValueError, + "Rotation Axis is to close to Zero", 0); } /* normalize the axis */ @@ -2928,11 +2945,12 @@ _vector3_rotate_helper(double *dst_coords, const double *src_coords, break; default: /* this should NEVER happen and means a bug in the code */ - RAISERETURN(PyExc_RuntimeError, - "Please report this bug in vector3_rotate_helper " - "to the developers at " - "github.com/pygame-community/pygame-ce/issues", - 0); + return RAISERETURN( + PyExc_RuntimeError, + "Please report this bug in vector3_rotate_helper " + "to the developers at " + "github.com/pygame-community/pygame-ce/issues", + 0); } } else { diff --git a/src_c/mixer.c b/src_c/mixer.c index 38df6d2900..10b04131b0 100644 --- a/src_c/mixer.c +++ b/src_c/mixer.c @@ -34,12 +34,13 @@ #define PyBUF_HAS_FLAG(f, F) (((f) & (F)) == (F)) -#define CHECK_CHUNK_VALID(CHUNK, RET) \ - if ((CHUNK) == NULL) { \ - RAISERETURN(PyExc_RuntimeError, \ - "__init__() was not called on Sound object so it failed " \ - "to setup correctly.", \ - RET); \ +#define CHECK_CHUNK_VALID(CHUNK, RET) \ + if ((CHUNK) == NULL) { \ + return RAISERETURN( \ + PyExc_RuntimeError, \ + "__init__() was not called on Sound object so it failed " \ + "to setup correctly.", \ + RET); \ } /* The SDL audio format constants are not defined for anything larger @@ -156,7 +157,8 @@ _format_view_to_audio(Py_buffer *view) } fstr_len = strlen(view->format); if (fstr_len < 1 || fstr_len > 2) { - RAISERETURN(PyExc_ValueError, "Array has unsupported item format", 0); + return RAISERETURN(PyExc_ValueError, + "Array has unsupported item format", 0); } if (fstr_len == 1) { format |= PG_SAMPLE_NATIVE_ENDIAN; @@ -183,8 +185,8 @@ _format_view_to_audio(Py_buffer *view) break; default: - RAISERETURN(PyExc_ValueError, - "Array has unsupported item format", 0); + return RAISERETURN(PyExc_ValueError, + "Array has unsupported item format", 0); } ++index; } @@ -963,8 +965,8 @@ snd_getbuffer(PyObject *obj, Py_buffer *view, int flags) return -1; } if (channels != 1 && PyBUF_HAS_FLAG(flags, PyBUF_F_CONTIGUOUS)) { - RAISERETURN(pgExc_BufferError, - "polyphonic sound is not Fortran contiguous", -1); + return RAISERETURN(pgExc_BufferError, + "polyphonic sound is not Fortran contiguous", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_ND)) { ndim = channels > 1 ? 2 : 1; @@ -1377,10 +1379,10 @@ static int _channel_init(pgChannelObject *self, int channelnum) { if (!SDL_WasInit(SDL_INIT_AUDIO)) { - RAISERETURN(pgExc_SDLError, "mixer not initialized", -1); + return RAISERETURN(pgExc_SDLError, "mixer not initialized", -1); } if (channelnum < 0 || channelnum >= Mix_GroupCount(-1)) { - RAISERETURN(PyExc_IndexError, "invalid channel index", -1); + return RAISERETURN(PyExc_IndexError, "invalid channel index", -1); } self->chan = channelnum; return 0; @@ -1672,25 +1674,28 @@ _chunk_from_array(void *buf, PG_sample_format_t view_format, int ndim, Py_ssize_t loop1, loop2, step1, step2, length, length2 = 0; if (!Mix_QuerySpec(&freq, &format, &channels)) { - RAISERETURN(pgExc_SDLError, "mixer not initialized", -1); + return RAISERETURN(pgExc_SDLError, "mixer not initialized", -1); } /* Check for compatible values. */ if (channels == 1) { if (ndim != 1) { - RAISERETURN(PyExc_ValueError, - "Array must be 1-dimensional for mono mixer", -1); + return RAISERETURN(PyExc_ValueError, + "Array must be 1-dimensional for mono mixer", + -1); } } else { if (ndim != 2) { - RAISERETURN(PyExc_ValueError, - "Array must be 2-dimensional for stereo mixer", -1); + return RAISERETURN(PyExc_ValueError, + "Array must be 2-dimensional for stereo mixer", + -1); } if (shape[1] != channels) { - RAISERETURN(PyExc_ValueError, - "Array depth must match number of mixer channels", -1); + return RAISERETURN( + PyExc_ValueError, + "Array depth must match number of mixer channels", -1); } } itemsize = _format_itemsize(format); @@ -1820,7 +1825,7 @@ sound_init(PyObject *self, PyObject *arg, PyObject *kwarg) /* Similar to MIXER_INIT_CHECK(), but different return value. */ if (!SDL_WasInit(SDL_INIT_AUDIO)) { - RAISERETURN(pgExc_SDLError, "mixer not initialized", -1); + return RAISERETURN(pgExc_SDLError, "mixer not initialized", -1); } /* Process arguments, returning cleaner error messages than @@ -1829,7 +1834,7 @@ sound_init(PyObject *self, PyObject *arg, PyObject *kwarg) if (arg != NULL && PyTuple_GET_SIZE(arg)) { if ((kwarg != NULL && PyDict_Size(kwarg)) || /* conditional and */ PyTuple_GET_SIZE(arg) != 1) { - RAISERETURN(PyExc_TypeError, arg_cnt_err_msg, -1); + return RAISERETURN(PyExc_TypeError, arg_cnt_err_msg, -1); } obj = PyTuple_GET_ITEM(arg, 0); @@ -1845,7 +1850,7 @@ sound_init(PyObject *self, PyObject *arg, PyObject *kwarg) } else if (kwarg != NULL) { if (PyDict_Size(kwarg) != 1) { - RAISERETURN(PyExc_TypeError, arg_cnt_err_msg, -1); + return RAISERETURN(PyExc_TypeError, arg_cnt_err_msg, -1); } if ((file = PyDict_GetItemString(kwarg, "file")) == NULL && (buffer = PyDict_GetItemString(kwarg, "buffer")) == NULL && @@ -1867,12 +1872,13 @@ sound_init(PyObject *self, PyObject *arg, PyObject *kwarg) return -1; } if (buffer != NULL && PyUnicode_Check(buffer)) { /* conditional and */ - RAISERETURN(PyExc_TypeError, - "Unicode object not allowed as buffer object", -1); + return RAISERETURN(PyExc_TypeError, + "Unicode object not allowed as buffer object", + -1); } } else { - RAISERETURN(PyExc_TypeError, arg_cnt_err_msg, -1); + return RAISERETURN(PyExc_TypeError, arg_cnt_err_msg, -1); } if (file != NULL) { @@ -1894,7 +1900,7 @@ sound_init(PyObject *self, PyObject *arg, PyObject *kwarg) #endif Py_END_ALLOW_THREADS; if (chunk == NULL) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } } @@ -1953,7 +1959,7 @@ sound_init(PyObject *self, PyObject *arg, PyObject *kwarg) if (chunk == NULL) { if (obj == NULL) { - RAISERETURN(PyExc_TypeError, "Unrecognized argument", -1); + return RAISERETURN(PyExc_TypeError, "Unrecognized argument", -1); } else { PyErr_Format(PyExc_TypeError, "Unrecognized argument (type %s)", diff --git a/src_c/newbuffer.c b/src_c/newbuffer.c index 4ec283660a..f84c741c4d 100644 --- a/src_c/newbuffer.c +++ b/src_c/newbuffer.c @@ -307,8 +307,8 @@ buffer_get_buffer(BufferObject *self, PyObject *args, PyObject *kwds) return 0; } if (bufobj_flags & BUFOBJ_FILLED) { - RAISERETURN(PyExc_ValueError, - "The Py_buffer struct is already filled in", 0); + return RAISERETURN(PyExc_ValueError, + "The Py_buffer struct is already filled in", 0); } self->flags = BUFOBJ_MUTABLE & bufobj_flags; if (!self->view_p) { @@ -750,7 +750,7 @@ Buffer_New(Py_buffer *view_p, int filled, int preserve) static PyObject * mixin__get_buffer(PyObject *self, PyObject *args) { - RAISERETURN(PyExc_NotImplementedError, "abstract method", 0); + return RAISERETURN(PyExc_NotImplementedError, "abstract method", 0); } static PyObject * diff --git a/src_c/pixelarray.c b/src_c/pixelarray.c index c57a6ce51a..1ccc9e5125 100644 --- a/src_c/pixelarray.c +++ b/src_c/pixelarray.c @@ -301,10 +301,11 @@ _pxarray_new_internal(PyTypeObject *type, pgSurfaceObject *surface, if (!parent) { if (!surface) { Py_TYPE(self)->tp_free((PyObject *)self); - RAISERETURN(PyExc_SystemError, - "Pygame internal error in _pxarray_new_internal: no " - "parent or surface.", - 0); + return RAISERETURN( + PyExc_SystemError, + "Pygame internal error in _pxarray_new_internal: no " + "parent or surface.", + 0); } self->parent = 0; self->surface = surface; @@ -540,7 +541,8 @@ _pxarray_getbuffer(pgPixelArrayObject *self, Py_buffer *view_p, int flags) Py_ssize_t len; if (self->surface == NULL) { - RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", -1); + return RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", + -1); } itemsize = PG_SURF_BytesPerPixel(pgSurface_AsSurface(self->surface)); @@ -549,18 +551,18 @@ _pxarray_getbuffer(pgPixelArrayObject *self, Py_buffer *view_p, int flags) view_p->obj = 0; if (PyBUF_HAS_FLAG(flags, PyBUF_C_CONTIGUOUS) && !array_is_contiguous(self, 'C')) { - RAISERETURN(pgExc_BufferError, "this pixel array is not C contiguous", - -1); + return RAISERETURN(pgExc_BufferError, + "this pixel array is not C contiguous", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_F_CONTIGUOUS) && !array_is_contiguous(self, 'F')) { - RAISERETURN(pgExc_BufferError, "this pixel array is not F contiguous", - -1); + return RAISERETURN(pgExc_BufferError, + "this pixel array is not F contiguous", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_ANY_CONTIGUOUS) && !array_is_contiguous(self, 'A')) { - RAISERETURN(pgExc_BufferError, "this pixel array is not contiguous", - -1); + return RAISERETURN(pgExc_BufferError, + "this pixel array is not contiguous", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_ND)) { shape = self->shape; @@ -568,17 +570,18 @@ _pxarray_getbuffer(pgPixelArrayObject *self, Py_buffer *view_p, int flags) strides = self->strides; } else if (!array_is_contiguous(self, 'C')) { - RAISERETURN(pgExc_BufferError, - "this pixel array is not contiguous: need strides", - -1); + return RAISERETURN( + pgExc_BufferError, + "this pixel array is not contiguous: need strides", -1); } } else if (array_is_contiguous(self, 'F')) { ndim = 0; } else { - RAISERETURN(pgExc_BufferError, - "this pixel array is not C contiguous: need strides", -1); + return RAISERETURN( + pgExc_BufferError, + "this pixel array is not C contiguous: need strides", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_FORMAT)) { /* Find the appropriate format for given pixel size */ @@ -925,7 +928,8 @@ _array_assign_array(pgPixelArrayObject *array, Py_ssize_t low, Py_ssize_t high, int sizes_match = 0; if (array->surface == NULL) { - RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", -1); + return RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", + -1); } surf = pgSurface_AsSurface(array->surface); @@ -952,7 +956,7 @@ _array_assign_array(pgPixelArrayObject *array, Py_ssize_t low, Py_ssize_t high, } if (!sizes_match) { /* Bounds do not match. */ - RAISERETURN(PyExc_ValueError, "array sizes do not match", -1); + return RAISERETURN(PyExc_ValueError, "array sizes do not match", -1); } bpp = PG_SURF_BytesPerPixel(surf); @@ -960,13 +964,13 @@ _array_assign_array(pgPixelArrayObject *array, Py_ssize_t low, Py_ssize_t high, if (val_bpp != bpp) { /* bpp do not match. We cannot guarantee that the padding and columns * would be set correctly. */ - RAISERETURN(PyExc_ValueError, "bit depths do not match", -1); + return RAISERETURN(PyExc_ValueError, "bit depths do not match", -1); } PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf); PG_PixelFormat *val_surf_format = PG_GetSurfaceFormat(val_surf); if (surf_format == NULL || val_surf_format == NULL) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } /* If we reassign the same array, we need to copy the pixels @@ -1093,12 +1097,12 @@ _array_assign_sequence(pgPixelArrayObject *array, Py_ssize_t low, PyObject *item; if (val_dim0 != dim0) { - RAISERETURN(PyExc_ValueError, "sequence size mismatch", -1); + return RAISERETURN(PyExc_ValueError, "sequence size mismatch", -1); } PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf); if (surf_format == NULL) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } bpp = PG_FORMAT_BytesPerPixel(surf_format); @@ -1211,7 +1215,7 @@ _array_assign_slice(pgPixelArrayObject *array, Py_ssize_t low, Py_ssize_t high, PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf); if (surf_format == NULL) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } bpp = PG_FORMAT_BytesPerPixel(surf_format); @@ -1327,9 +1331,9 @@ _pxarray_ass_item(pgPixelArrayObject *array, Py_ssize_t index, PyObject *value) return -1; } if (!pgPixelArrayObject_Check(tmparray)) { - RAISERETURN(PyExc_ValueError, - "cannot assign a pixel sequence to a single pixel", - -1); + return RAISERETURN( + PyExc_ValueError, + "cannot assign a pixel sequence to a single pixel", -1); } retval = _array_assign_sequence(tmparray, 0, tmparray->shape[0], value); @@ -1344,11 +1348,12 @@ _pxarray_ass_item(pgPixelArrayObject *array, Py_ssize_t index, PyObject *value) if (index < 0) { index += dim0; if (index < 0) { - RAISERETURN(PyExc_IndexError, "array index out of range", -1); + return RAISERETURN(PyExc_IndexError, "array index out of range", + -1); } } if (index >= dim0) { - RAISERETURN(PyExc_IndexError, "array index out of range", -1); + return RAISERETURN(PyExc_IndexError, "array index out of range", -1); } pixels += index * stride0; @@ -1359,7 +1364,7 @@ _pxarray_ass_item(pgPixelArrayObject *array, Py_ssize_t index, PyObject *value) PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf); if (surf_format == NULL) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } Py_BEGIN_ALLOW_THREADS; @@ -1571,7 +1576,7 @@ _get_subslice(PyObject *op, Py_ssize_t length, Py_ssize_t *start, *start += length; } if (*start >= length || *start < 0) { - RAISERETURN(PyExc_IndexError, "invalid index", -1); + return RAISERETURN(PyExc_IndexError, "invalid index", -1); } *stop = (*start) + 1; *step = 0; @@ -1717,8 +1722,8 @@ _pxarray_ass_subscript(pgPixelArrayObject *array, PyObject *op, int retval; if (size > 2 || (size == 2 && !dim1)) { - RAISERETURN(PyExc_IndexError, "too many indices for the array", - -1); + return RAISERETURN(PyExc_IndexError, + "too many indices for the array", -1); } obj = PyTuple_GET_ITEM(op, 0); @@ -1813,8 +1818,8 @@ _pxarray_ass_subscript(pgPixelArrayObject *array, PyObject *op, return -1; } if (slicelen < 0) { - RAISERETURN(PyExc_IndexError, "Unable to handle negative slice", - -1); + return RAISERETURN(PyExc_IndexError, + "Unable to handle negative slice", -1); } if (slicelen == 0) { return 0; @@ -1837,8 +1842,8 @@ _pxarray_ass_subscript(pgPixelArrayObject *array, PyObject *op, } return _pxarray_ass_item(array, i, value); } - RAISERETURN(PyExc_TypeError, "index must be an integer, sequence or slice", - -1); + return RAISERETURN(PyExc_TypeError, + "index must be an integer, sequence or slice", -1); } /**** C API interfaces ****/ diff --git a/src_c/pixelarray_methods.c b/src_c/pixelarray_methods.c index cea13f552d..65a98edc26 100644 --- a/src_c/pixelarray_methods.c +++ b/src_c/pixelarray_methods.c @@ -319,8 +319,9 @@ _get_weights(PyObject *weights, float *wr, float *wg, float *wb) *wb = rgb[2]; if ((*wr < 0 || *wg < 0 || *wb < 0) || (*wr == 0 && *wg == 0 && *wb == 0)) { - RAISERETURN(PyExc_ValueError, - "weights must be positive and greater than 0", 0); + return RAISERETURN(PyExc_ValueError, + "weights must be positive and greater than 0", + 0); } /* Build the average weight values. */ sum = *wr + *wg + *wb; @@ -808,7 +809,7 @@ _compare(pgPixelArrayObject *array, PyObject *args, PyObject *kwds) if (other_array->shape[0] != dim0 || other_array->shape[1] != dim1) { /* Bounds do not match. */ - RAISERETURN(PyExc_ValueError, "array sizes do not match", 0); + return RAISERETURN(PyExc_ValueError, "array sizes do not match", 0); } other_surf = pgSurface_AsSurface(other_array->surface); @@ -827,7 +828,7 @@ _compare(pgPixelArrayObject *array, PyObject *args, PyObject *kwds) if (PG_FORMAT_BytesPerPixel(other_format) != bpp) { /* bpp do not match. We cannot guarantee that the padding and co * would be set correctly. */ - RAISERETURN(PyExc_ValueError, "bit depths do not match", 0); + return RAISERETURN(PyExc_ValueError, "bit depths do not match", 0); } other_stride0 = other_array->strides[0]; diff --git a/src_c/pixelcopy.c b/src_c/pixelcopy.c index b119630f75..3ae7b344fa 100644 --- a/src_c/pixelcopy.c +++ b/src_c/pixelcopy.c @@ -93,7 +93,8 @@ _validate_view_format(const char *format) /* default: unrecognized character; raise error later */ } if (format[i] != '\0') { - RAISERETURN(PyExc_ValueError, "Unsupported array item type", -1); + return RAISERETURN(PyExc_ValueError, "Unsupported array item type", + -1); } return 0; @@ -124,15 +125,15 @@ _view_kind(PyObject *obj, void *view_kind_vptr) if (PyUnicode_Check(obj)) { if (PyUnicode_GET_LENGTH(obj) != 1) { - RAISERETURN(PyExc_TypeError, - "expected a length 1 string for argument 3", 0); + return RAISERETURN(PyExc_TypeError, + "expected a length 1 string for argument 3", 0); } ch = PyUnicode_READ_CHAR(obj, 0); } else if (PyBytes_Check(obj)) { if (PyBytes_GET_SIZE(obj) != 1) { - RAISERETURN(PyExc_TypeError, - "expected a length 1 string for argument 3", 0); + return RAISERETURN(PyExc_TypeError, + "expected a length 1 string for argument 3", 0); } ch = *PyBytes_AS_STRING(obj); } @@ -285,11 +286,11 @@ _copy_colorplane(Py_buffer *view_p, SDL_Surface *surf, return -1; } if (!PG_GetSurfaceBlendMode(surf, &mode)) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } if (!PG_GetSurfaceDetails(surf, &format, &palette)) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } /* Select appropriate color plane element within the pixel */ @@ -418,7 +419,7 @@ _copy_unmapped(Py_buffer *view_p, SDL_Surface *surf) SDL_Palette *palette; if (!PG_GetSurfaceDetails(surf, &format, &palette)) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } for (x = 0; x < w; ++x) { @@ -822,8 +823,9 @@ surface_to_array(PyObject *self, PyObject *args, PyObject *kwds) if (view_kind != VIEWKIND_RGB) { pgBuffer_Release(&pg_view); pgSurface_Unlock(surfobj); - RAISERETURN(PyExc_ValueError, - "color planes only supported for 2d targets", 0); + return RAISERETURN(PyExc_ValueError, + "color planes only supported for 2d targets", + 0); } if (_copy_unmapped(view_p, surf)) { pgBuffer_Release(&pg_view); @@ -1200,7 +1202,7 @@ make_surface(PyObject *self, PyObject *arg) if (!PG_SetPaletteColors(palette, default_palette_colors, 0, default_palette_size - 1)) { SDL_FreeSurface(surf); - RAISERETURN(pgExc_SDLError, SDL_GetError(), 0); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), 0); } } surfobj = pgSurface_New(surf); diff --git a/src_c/rect.c b/src_c/rect.c index 15496f2aaf..52fe58a3c7 100644 --- a/src_c/rect.c +++ b/src_c/rect.c @@ -323,8 +323,8 @@ four_ints_from_obj(PyObject *obj, int *val1, int *val2, int *val3, int *val4) Py_DECREF(item); if (!result) { - RAISERETURN(PyExc_TypeError, - "number pair expected for first argument", 0); + return RAISERETURN(PyExc_TypeError, + "number pair expected for first argument", 0); } /* Get the other end of the line. */ @@ -338,29 +338,29 @@ four_ints_from_obj(PyObject *obj, int *val1, int *val2, int *val3, int *val4) Py_DECREF(item); if (!result) { - RAISERETURN(PyExc_TypeError, - "number pair expected for second argument", 0); + return RAISERETURN(PyExc_TypeError, + "number pair expected for second argument", 0); } } else if (length == 4) { if (!pg_IntFromObjIndex(obj, 0, val1)) { - RAISERETURN(PyExc_TypeError, "number expected for first argument", - 0); + return RAISERETURN(PyExc_TypeError, + "number expected for first argument", 0); } if (!pg_IntFromObjIndex(obj, 1, val2)) { - RAISERETURN(PyExc_TypeError, "number expected for second argument", - 0); + return RAISERETURN(PyExc_TypeError, + "number expected for second argument", 0); } if (!pg_IntFromObjIndex(obj, 2, val3)) { - RAISERETURN(PyExc_TypeError, "number expected for third argument", - 0); + return RAISERETURN(PyExc_TypeError, + "number expected for third argument", 0); } if (!pg_IntFromObjIndex(obj, 3, val4)) { - RAISERETURN(PyExc_TypeError, "number expected for fourth argument", - 0); + return RAISERETURN(PyExc_TypeError, + "number expected for fourth argument", 0); } } else { @@ -396,8 +396,8 @@ four_floats_from_obj(PyObject *obj, float *val1, float *val2, float *val3, Py_DECREF(item); if (!result) { - RAISERETURN(PyExc_TypeError, - "number pair expected for first argument", 0); + return RAISERETURN(PyExc_TypeError, + "number pair expected for first argument", 0); } /* Get the other end of the line. */ @@ -411,29 +411,29 @@ four_floats_from_obj(PyObject *obj, float *val1, float *val2, float *val3, Py_DECREF(item); if (!result) { - RAISERETURN(PyExc_TypeError, - "number pair expected for second argument", 0); + return RAISERETURN(PyExc_TypeError, + "number pair expected for second argument", 0); } } else if (length == 4) { if (!pg_FloatFromObjIndex(obj, 0, val1)) { - RAISERETURN(PyExc_TypeError, "number expected for first argument", - 0); + return RAISERETURN(PyExc_TypeError, + "number expected for first argument", 0); } if (!pg_FloatFromObjIndex(obj, 1, val2)) { - RAISERETURN(PyExc_TypeError, "number expected for second argument", - 0); + return RAISERETURN(PyExc_TypeError, + "number expected for second argument", 0); } if (!pg_FloatFromObjIndex(obj, 2, val3)) { - RAISERETURN(PyExc_TypeError, "number expected for third argument", - 0); + return RAISERETURN(PyExc_TypeError, + "number expected for third argument", 0); } if (!pg_FloatFromObjIndex(obj, 3, val4)) { - RAISERETURN(PyExc_TypeError, "number expected for fourth argument", - 0); + return RAISERETURN(PyExc_TypeError, + "number expected for fourth argument", 0); } } else { diff --git a/src_c/rect_impl.h b/src_c/rect_impl.h index 09bb3e1db1..a36836431c 100644 --- a/src_c/rect_impl.h +++ b/src_c/rect_impl.h @@ -878,7 +878,8 @@ RectExport_init(RectObject *self, PyObject *args, PyObject *kwds) InnerRect *argrect, temp; if (!(argrect = RectFromObject(args, &temp))) { - RAISERETURN(PyExc_TypeError, "Argument must be rect style object", -1); + return RAISERETURN(PyExc_TypeError, + "Argument must be rect style object", -1); } self->r = *argrect; @@ -2147,7 +2148,8 @@ RectExport_assItem(RectObject *self, Py_ssize_t i, PyObject *v) PrimitiveType *data = (PrimitiveType *)&self->r; if (!v) { - RAISERETURN(PyExc_TypeError, "item deletion is not supported", -1); + return RAISERETURN(PyExc_TypeError, "item deletion is not supported", + -1); } if (i < 0 || i > 3) { @@ -2155,11 +2157,11 @@ RectExport_assItem(RectObject *self, Py_ssize_t i, PyObject *v) i += 4; } else { - RAISERETURN(PyExc_IndexError, "Invalid rect Index", -1); + return RAISERETURN(PyExc_IndexError, "Invalid rect Index", -1); } } if (!PrimitiveFromObj(v, &val)) { - RAISERETURN(PyExc_TypeError, "Must assign numeric values", -1); + return RAISERETURN(PyExc_TypeError, "Must assign numeric values", -1); } data[i] = val; return 0; @@ -2216,7 +2218,8 @@ static int RectExport_assSubscript(RectObject *self, PyObject *op, PyObject *value) { if (!value) { - RAISERETURN(PyExc_TypeError, "item deletion is not supported", -1); + return RAISERETURN(PyExc_TypeError, "item deletion is not supported", + -1); } if (PyIndex_Check(op)) { Py_ssize_t i = PyNumber_AsSsize_t(op, NULL); @@ -2248,7 +2251,8 @@ RectExport_assSubscript(RectObject *self, PyObject *op, PyObject *value) Py_ssize_t i; if (PySequence_Size(value) != 4) { - RAISERETURN(PyExc_TypeError, "Expect a length 4 sequence", -1); + return RAISERETURN(PyExc_TypeError, + "Expect a length 4 sequence", -1); } for (i = 0; i < 4; ++i) { item = PySequence_ITEM(value, i); @@ -2264,8 +2268,8 @@ RectExport_assSubscript(RectObject *self, PyObject *op, PyObject *value) self->r.h = values[3]; } else { - RAISERETURN(PyExc_TypeError, "Expected an integer or sequence", - -1); + return RAISERETURN(PyExc_TypeError, + "Expected an integer or sequence", -1); } } else if (PySlice_Check(op)) { @@ -2309,12 +2313,12 @@ RectExport_assSubscript(RectObject *self, PyObject *op, PyObject *value) } } else { - RAISERETURN(PyExc_TypeError, "Expected an integer or sequence", - -1); + return RAISERETURN(PyExc_TypeError, + "Expected an integer or sequence", -1); } } else { - RAISERETURN(PyExc_TypeError, "Invalid Rect slice", -1); + return RAISERETURN(PyExc_TypeError, "Invalid Rect slice", -1); } return 0; } @@ -2393,11 +2397,11 @@ RectExport_setwidth(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!PrimitiveFromObj(value, &val1)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.w = val1; return 0; @@ -2417,11 +2421,11 @@ RectExport_setheight(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!PrimitiveFromObj(value, &val1)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.h = val1; return 0; @@ -2441,11 +2445,11 @@ RectExport_settop(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!PrimitiveFromObj(value, &val1)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.y = val1; return 0; @@ -2465,11 +2469,11 @@ RectExport_setleft(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!PrimitiveFromObj(value, &val1)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1; return 0; @@ -2489,11 +2493,11 @@ RectExport_setright(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!PrimitiveFromObj(value, &val1)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1 - self->r.w; return 0; @@ -2513,11 +2517,11 @@ RectExport_setbottom(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!PrimitiveFromObj(value, &val1)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.y = val1 - self->r.h; return 0; @@ -2537,11 +2541,11 @@ RectExport_setcenterx(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!PrimitiveFromObj(value, &val1)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1 - (self->r.w / 2); return 0; @@ -2561,11 +2565,11 @@ RectExport_setcentery(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!PrimitiveFromObj(value, &val1)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.y = val1 - (self->r.h / 2); return 0; @@ -2585,11 +2589,11 @@ RectExport_settopleft(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1; self->r.y = val2; @@ -2610,11 +2614,11 @@ RectExport_settopright(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1 - self->r.w; self->r.y = val2; @@ -2635,11 +2639,11 @@ RectExport_setbottomleft(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1; self->r.y = val2 - self->r.h; @@ -2661,11 +2665,11 @@ RectExport_setbottomright(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1 - self->r.w; self->r.y = val2 - self->r.h; @@ -2686,11 +2690,11 @@ RectExport_setmidtop(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x += val1 - (self->r.x + (self->r.w / 2)); self->r.y = val2; @@ -2711,11 +2715,11 @@ RectExport_setmidleft(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1; self->r.y += val2 - (self->r.y + (self->r.h / 2)); @@ -2737,11 +2741,11 @@ RectExport_setmidbottom(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x += val1 - (self->r.x + (self->r.w / 2)); self->r.y = val2 - self->r.h; @@ -2763,11 +2767,11 @@ RectExport_setmidright(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x = val1 - self->r.w; self->r.y += val2 - (self->r.y + (self->r.h / 2)); @@ -2789,11 +2793,11 @@ RectExport_setcenter(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.x += val1 - (self->r.x + (self->r.w / 2)); self->r.y += val2 - (self->r.y + (self->r.h / 2)); @@ -2814,11 +2818,11 @@ RectExport_setsize(RectObject *self, PyObject *value, void *closure) if (NULL == value) { /* Attribute deletion not supported. */ - RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); + return RAISERETURN(PyExc_AttributeError, "can't delete attribute", -1); } if (!twoPrimitivesFromObj(value, &val1, &val2)) { - RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); + return RAISERETURN(PyExc_TypeError, "invalid rect assignment", -1); } self->r.w = val1; self->r.h = val2; diff --git a/src_c/render.c b/src_c/render.c index 7985d35b00..b8e6ef5106 100644 --- a/src_c/render.c +++ b/src_c/render.c @@ -25,9 +25,9 @@ static PyTypeObject pgImage_Type; return RAISE(pgExc_SDLError, SDL_GetError()); \ } -#define RENDERER_PROPERTY_ERROR_CHECK(x) \ - if (x < 0) { \ - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); \ +#define RENDERER_PROPERTY_ERROR_CHECK(x) \ + if (x < 0) { \ + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); \ } #define PARSE_POINT(obj, x, y, name) \ @@ -416,7 +416,7 @@ renderer_set_draw_blend_mode(pgRendererObject *self, PyObject *arg, void *closure) { if (!PyLong_Check(arg)) { - RAISERETURN(PyExc_TypeError, "Draw blend mode must be int", -1); + return RAISERETURN(PyExc_TypeError, "Draw blend mode must be int", -1); } RENDERER_PROPERTY_ERROR_CHECK( SDL_SetRenderDrawBlendMode(self->renderer, (int)PyLong_AsLong(arg))) @@ -436,7 +436,7 @@ renderer_set_logical_size(pgRendererObject *self, PyObject *arg, void *closure) { int w, h; if (!pg_TwoIntsFromObj(arg, &w, &h)) { - RAISERETURN(PyExc_TypeError, "invalid logical size", -1); + return RAISERETURN(PyExc_TypeError, "invalid logical size", -1); } RENDERER_PROPERTY_ERROR_CHECK( SDL_RenderSetLogicalSize(self->renderer, w, h)) @@ -456,7 +456,7 @@ renderer_set_scale(pgRendererObject *self, PyObject *arg, void *closure) { float x, y; if (!pg_TwoFloatsFromObj(arg, &x, &y)) { - RAISERETURN(PyExc_TypeError, "invalid scale", -1); + return RAISERETURN(PyExc_TypeError, "invalid scale", -1); } RENDERER_PROPERTY_ERROR_CHECK(SDL_RenderSetScale(self->renderer, x, y)) return 0; @@ -488,8 +488,8 @@ renderer_set_target(pgRendererObject *self, PyObject *arg, void *closure) return 0; } else { - RAISERETURN(PyExc_TypeError, "target must be Texture object or None", - -1); + return RAISERETURN(PyExc_TypeError, + "target must be Texture object or None", -1); } } @@ -523,7 +523,7 @@ renderer_init(pgRendererObject *self, PyObject *args, PyObject *kwargs) } renderer = SDL_CreateRenderer(window->_win, index, flags); if (!renderer) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } self->renderer = renderer; self->window = window; diff --git a/src_c/rwobject.c b/src_c/rwobject.c index 94def6f205..7509ca17c9 100644 --- a/src_c/rwobject.c +++ b/src_c/rwobject.c @@ -86,15 +86,15 @@ _pg_is_exception_class(PyObject *obj, void **optr) !PyObject_IsSubclass(obj, PyExc_BaseException)) { oname = PyObject_Str(obj); if (oname == NULL) { - RAISERETURN(PyExc_TypeError, "invalid exception class argument", - 0); + return RAISERETURN(PyExc_TypeError, + "invalid exception class argument", 0); } tmp = PyUnicode_AsEncodedString(oname, "ascii", "replace"); Py_DECREF(oname); if (tmp == NULL) { - RAISERETURN(PyExc_TypeError, "invalid exception class argument", - 0); + return RAISERETURN(PyExc_TypeError, + "invalid exception class argument", 0); } oname = tmp; @@ -129,7 +129,7 @@ fetch_object_methods(pgRWHelper *helper, PyObject *obj) } } if (!helper->read && !helper->write) { - RAISERETURN(PyExc_TypeError, "not a file object", -1); + return RAISERETURN(PyExc_TypeError, "not a file object", -1); } if (PyObject_HasAttrString(obj, "seek")) { helper->seek = PyObject_GetAttrString(obj, "seek"); diff --git a/src_c/scrap_sdl2.c b/src_c/scrap_sdl2.c index bbb6f3bdb3..aebb1c28e9 100644 --- a/src_c/scrap_sdl2.c +++ b/src_c/scrap_sdl2.c @@ -78,7 +78,7 @@ int pygame_scrap_put(char *type, Py_ssize_t srclen, char *src) { if (!pygame_scrap_initialized()) { - RAISERETURN(pgExc_SDLError, "scrap system not initialized.", 0); + return RAISERETURN(pgExc_SDLError, "scrap system not initialized.", 0); } if (strcmp(type, pygame_scrap_plaintext_type) == 0) { diff --git a/src_c/scrap_win.c b/src_c/scrap_win.c index 971c51e6b0..b16204191b 100644 --- a/src_c/scrap_win.c +++ b/src_c/scrap_win.c @@ -202,7 +202,7 @@ int pygame_scrap_lost(void) { if (!pygame_scrap_initialized()) { - RAISERETURN(pgExc_SDLError, "scrap system not initialized.", 0); + return RAISERETURN(pgExc_SDLError, "scrap system not initialized.", 0); } return (GetClipboardOwner() != window_handle); } @@ -215,7 +215,7 @@ pygame_scrap_put(char *type, Py_ssize_t srclen, char *src) HANDLE hMem; if (!pygame_scrap_initialized()) { - RAISERETURN(pgExc_SDLError, "scrap system not initialized.", 0); + return RAISERETURN(pgExc_SDLError, "scrap system not initialized.", 0); } format = _convert_internal_type(type); diff --git a/src_c/surface.c b/src_c/surface.c index bd15755f75..c3af0fa1de 100644 --- a/src_c/surface.c +++ b/src_c/surface.c @@ -348,7 +348,7 @@ static int pgSurface_SetSurface(pgSurfaceObject *self, SDL_Surface *s, int owner) { if (!s) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } if (s == self->surf) { self->owner = owner; @@ -499,17 +499,20 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) if (PySequence_Check(size) && PySequence_Length(size) == 2) { if ((!pg_IntFromObjIndex(size, 0, &width)) || (!pg_IntFromObjIndex(size, 1, &height))) { - RAISERETURN(PyExc_ValueError, - "size needs to be (number width, number height)", -1); + return RAISERETURN( + PyExc_ValueError, + "size needs to be (number width, number height)", -1); } } else { - RAISERETURN(PyExc_ValueError, - "size needs to be (number width, number height)", -1); + return RAISERETURN(PyExc_ValueError, + "size needs to be (number width, number height)", + -1); } if (width < 0 || height < 0) { - RAISERETURN(pgExc_SDLError, "Invalid resolution for Surface", -1); + return RAISERETURN(pgExc_SDLError, "Invalid resolution for Surface", + -1); } surface_cleanup(self); @@ -518,31 +521,33 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) if (depth && masks) { /* all info supplied, most errorchecking * needed */ if (pgSurface_Check(depth)) { - RAISERETURN(PyExc_ValueError, - "cannot pass surface for depth and color masks", -1); + return RAISERETURN(PyExc_ValueError, + "cannot pass surface for depth and color masks", + -1); } if (!pg_IntFromObj(depth, &bpp)) { - RAISERETURN(PyExc_ValueError, - "invalid bits per pixel depth argument", -1); + return RAISERETURN(PyExc_ValueError, + "invalid bits per pixel depth argument", -1); } if (!PySequence_Check(masks) || PySequence_Length(masks) != 4) { - RAISERETURN(PyExc_ValueError, - "masks argument must be sequence of four numbers", -1); + return RAISERETURN( + PyExc_ValueError, + "masks argument must be sequence of four numbers", -1); } if (!pg_UintFromObjIndex(masks, 0, &Rmask) || !pg_UintFromObjIndex(masks, 1, &Gmask) || !pg_UintFromObjIndex(masks, 2, &Bmask) || !pg_UintFromObjIndex(masks, 3, &Amask)) { - RAISERETURN(PyExc_ValueError, - "invalid mask values in masks sequence", -1); + return RAISERETURN(PyExc_ValueError, + "invalid mask values in masks sequence", -1); } format = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask); } else if (depth && PyNumber_Check(depth)) { /* use default masks */ if (!pg_IntFromObj(depth, &bpp)) { - RAISERETURN(PyExc_ValueError, - "invalid bits per pixel depth argument", -1); + return RAISERETURN(PyExc_ValueError, + "invalid bits per pixel depth argument", -1); } if (flags & PGS_SRCALPHA) { switch (bpp) { @@ -553,10 +558,11 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) format = SDL_PIXELFORMAT_ARGB8888; break; default: - RAISERETURN(PyExc_ValueError, - "no standard masks exist for given bitdepth " - "with alpha", - -1); + return RAISERETURN( + PyExc_ValueError, + "no standard masks exist for given bitdepth " + "with alpha", + -1); } } else { @@ -584,8 +590,8 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) format = SDL_PIXELFORMAT_XRGB8888; break; default: - RAISERETURN(PyExc_ValueError, - "nonstandard bit depth given", -1); + return RAISERETURN(PyExc_ValueError, + "nonstandard bit depth given", -1); } } } @@ -613,21 +619,22 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) format = SDL_PIXELFORMAT_ARGB8888; break; default: - RAISERETURN(PyExc_ValueError, - "no standard masks exist for given bitdepth " - "with alpha", - -1); + return RAISERETURN( + PyExc_ValueError, + "no standard masks exist for given bitdepth " + "with alpha", + -1); } } } if (format == SDL_PIXELFORMAT_UNKNOWN) { - RAISERETURN(PyExc_ValueError, "Invalid mask values", -1); + return RAISERETURN(PyExc_ValueError, "Invalid mask values", -1); } surface = PG_CreateSurface(width, height, format); if (!surface) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } if (!(flags & PGS_SRCALPHA)) { @@ -646,7 +653,7 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) SDL_Palette *surf_palette; if (!PG_GetSurfaceDetails(surface, &surf_format, &surf_palette)) { SDL_FreeSurface(surface); - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } if (surf_format->Amask != 0) { @@ -662,36 +669,38 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) if (!PG_SetPaletteColors(surf_palette, default_palette_colors, 0, default_palette_size - 1)) { SDL_FreeSurface(surface); - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } } #else if (depth && masks) { /* all info supplied, most errorchecking * needed */ if (pgSurface_Check(depth)) { - RAISERETURN(PyExc_ValueError, - "cannot pass surface for depth and color masks", -1); + return RAISERETURN(PyExc_ValueError, + "cannot pass surface for depth and color masks", + -1); } if (!pg_IntFromObj(depth, &bpp)) { - RAISERETURN(PyExc_ValueError, - "invalid bits per pixel depth argument", -1); + return RAISERETURN(PyExc_ValueError, + "invalid bits per pixel depth argument", -1); } if (!PySequence_Check(masks) || PySequence_Length(masks) != 4) { - RAISERETURN(PyExc_ValueError, - "masks argument must be sequence of four numbers", -1); + return RAISERETURN( + PyExc_ValueError, + "masks argument must be sequence of four numbers", -1); } if (!pg_UintFromObjIndex(masks, 0, &Rmask) || !pg_UintFromObjIndex(masks, 1, &Gmask) || !pg_UintFromObjIndex(masks, 2, &Bmask) || !pg_UintFromObjIndex(masks, 3, &Amask)) { - RAISERETURN(PyExc_ValueError, - "invalid mask values in masks sequence", -1); + return RAISERETURN(PyExc_ValueError, + "invalid mask values in masks sequence", -1); } } else if (depth && PyNumber_Check(depth)) { /* use default masks */ if (!pg_IntFromObj(depth, &bpp)) { - RAISERETURN(PyExc_ValueError, - "invalid bits per pixel depth argument", -1); + return RAISERETURN(PyExc_ValueError, + "invalid bits per pixel depth argument", -1); } if (flags & PGS_SRCALPHA) { switch (bpp) { @@ -708,10 +717,11 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) Amask = 0xFF << 24; break; default: - RAISERETURN(PyExc_ValueError, - "no standard masks exist for given bitdepth " - "with alpha", - -1); + return RAISERETURN( + PyExc_ValueError, + "no standard masks exist for given bitdepth " + "with alpha", + -1); } } else { @@ -744,8 +754,8 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) Bmask = 0xFF; break; default: - RAISERETURN(PyExc_ValueError, - "nonstandard bit depth given", -1); + return RAISERETURN(PyExc_ValueError, + "nonstandard bit depth given", -1); } } } @@ -786,10 +796,11 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) Amask = 0xFF << 24; break; default: - RAISERETURN(PyExc_ValueError, - "no standard masks exist for given bitdepth " - "with alpha", - -1); + return RAISERETURN( + PyExc_ValueError, + "no standard masks exist for given bitdepth " + "with alpha", + -1); } } else { @@ -803,12 +814,12 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) Uint32 pxformat = SDL_MasksToPixelFormatEnum(bpp, Rmask, Gmask, Bmask, Amask); if (pxformat == SDL_PIXELFORMAT_UNKNOWN) { - RAISERETURN(PyExc_ValueError, "Invalid mask values", -1); + return RAISERETURN(PyExc_ValueError, "Invalid mask values", -1); } surface = PG_CreateSurface(width, height, pxformat); if (!surface) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } if (!(flags & PGS_SRCALPHA)) { @@ -836,7 +847,7 @@ surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds) default_palette_colors, 0, default_palette_size - 1)) { SDL_FreeSurface(surface); - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } } #endif @@ -2941,7 +2952,7 @@ _PgSurface_SrcAlpha(SDL_Surface *surf) { SDL_BlendMode mode; if (!PG_GetSurfaceBlendMode(surf, &mode)) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } return (mode != SDL_BLENDMODE_NONE); } @@ -3608,15 +3619,15 @@ surf_get_view(PyObject *self, PyObject *args) case VIEWKIND_0D: if (surface->pitch != bpp * surface->w) { - RAISERETURN(PyExc_ValueError, "Surface data is not contiguous", - 0); + return RAISERETURN(PyExc_ValueError, + "Surface data is not contiguous", 0); } get_buffer = _get_buffer_0D; break; case VIEWKIND_1D: if (surface->pitch != bpp * surface->w) { - RAISERETURN(PyExc_ValueError, "Surface data is not contiguous", - 0); + return RAISERETURN(PyExc_ValueError, + "Surface data is not contiguous", 0); } get_buffer = _get_buffer_1D; break; @@ -3866,28 +3877,29 @@ _get_buffer_2D(PyObject *obj, Py_buffer *view_p, int flags) view_p->obj = 0; if (!PyBUF_HAS_FLAG(flags, PyBUF_ND)) { if (surface->pitch != surface->w * itemsize) { - RAISERETURN(pgExc_BufferError, - "A 2D surface view is not C contiguous", -1); + return RAISERETURN(pgExc_BufferError, + "A 2D surface view is not C contiguous", -1); } return _get_buffer_1D(obj, view_p, flags); } if (!PyBUF_HAS_FLAG(flags, PyBUF_STRIDES)) { - RAISERETURN(pgExc_BufferError, - "A 2D surface view is not C contiguous: need strides", -1); + return RAISERETURN( + pgExc_BufferError, + "A 2D surface view is not C contiguous: need strides", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_C_CONTIGUOUS)) { - RAISERETURN(pgExc_BufferError, "A 2D surface view is not C contiguous", - -1); + return RAISERETURN(pgExc_BufferError, + "A 2D surface view is not C contiguous", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_F_CONTIGUOUS) && surface->pitch != surface->w * itemsize) { - RAISERETURN(pgExc_BufferError, - "This 2D surface view is not F contiguous", -1); + return RAISERETURN(pgExc_BufferError, + "This 2D surface view is not F contiguous", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_ANY_CONTIGUOUS) && surface->pitch != surface->w * itemsize) { - RAISERETURN(pgExc_BufferError, - "This 2D surface view is not contiguous", -1); + return RAISERETURN(pgExc_BufferError, + "This 2D surface view is not contiguous", -1); } if (_init_buffer(obj, view_p, flags)) { return -1; @@ -3946,18 +3958,19 @@ _get_buffer_3D(PyObject *obj, Py_buffer *view_p, int flags) view_p->obj = 0; if (!PyBUF_HAS_FLAG(flags, PyBUF_STRIDES)) { - RAISERETURN(pgExc_BufferError, - "A 3D surface view is not contiguous: needs strides", -1); + return RAISERETURN( + pgExc_BufferError, + "A 3D surface view is not contiguous: needs strides", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_C_CONTIGUOUS) || PyBUF_HAS_FLAG(flags, PyBUF_F_CONTIGUOUS) || PyBUF_HAS_FLAG(flags, PyBUF_ANY_CONTIGUOUS)) { - RAISERETURN(pgExc_BufferError, "A 3D surface view is not contiguous", - -1); + return RAISERETURN(pgExc_BufferError, + "A 3D surface view is not contiguous", -1); } PG_PixelFormat *surface_format = PG_GetSurfaceFormat(surface); if (surface_format == NULL) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } if (_init_buffer(obj, view_p, flags)) { @@ -4024,7 +4037,7 @@ _get_buffer_red(PyObject *obj, Py_buffer *view_p, int flags) PG_PixelFormat *surface_format = PG_GetSurfaceFormat(pgSurface_AsSurface(obj)); if (surface_format == NULL) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } return _get_buffer_colorplane(obj, view_p, flags, "red", @@ -4037,7 +4050,7 @@ _get_buffer_green(PyObject *obj, Py_buffer *view_p, int flags) PG_PixelFormat *surface_format = PG_GetSurfaceFormat(pgSurface_AsSurface(obj)); if (surface_format == NULL) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } return _get_buffer_colorplane(obj, view_p, flags, "green", @@ -4050,7 +4063,7 @@ _get_buffer_blue(PyObject *obj, Py_buffer *view_p, int flags) PG_PixelFormat *surface_format = PG_GetSurfaceFormat(pgSurface_AsSurface(obj)); if (surface_format == NULL) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } return _get_buffer_colorplane(obj, view_p, flags, "blue", @@ -4063,7 +4076,7 @@ _get_buffer_alpha(PyObject *obj, Py_buffer *view_p, int flags) PG_PixelFormat *surface_format = PG_GetSurfaceFormat(pgSurface_AsSurface(obj)); if (surface_format == NULL) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } return _get_buffer_colorplane(obj, view_p, flags, "alpha", @@ -4080,15 +4093,15 @@ _get_buffer_colorplane(PyObject *obj, Py_buffer *view_p, int flags, char *name, view_p->obj = 0; if (!PyBUF_HAS_FLAG(flags, PyBUF_STRIDES)) { - RAISERETURN( + return RAISERETURN( pgExc_BufferError, "A surface color plane view is not contiguous: need strides", -1); } if (PyBUF_HAS_FLAG(flags, PyBUF_C_CONTIGUOUS) || PyBUF_HAS_FLAG(flags, PyBUF_F_CONTIGUOUS) || PyBUF_HAS_FLAG(flags, PyBUF_ANY_CONTIGUOUS)) { - RAISERETURN(pgExc_BufferError, - "A surface color plane view is not contiguous", -1); + return RAISERETURN(pgExc_BufferError, + "A surface color plane view is not contiguous", -1); } switch (mask) { /* This switch statement is exhaustive over possible mask value, @@ -4238,15 +4251,15 @@ _view_kind(PyObject *obj, void *view_kind_vptr) if (PyUnicode_Check(obj)) { if (PyUnicode_GET_LENGTH(obj) != 1) { - RAISERETURN(PyExc_TypeError, - "expected a length 1 string for argument 1", 0); + return RAISERETURN(PyExc_TypeError, + "expected a length 1 string for argument 1", 0); } ch = PyUnicode_READ_CHAR(obj, 0); } else if (PyBytes_Check(obj)) { if (PyBytes_GET_SIZE(obj) != 1) { - RAISERETURN(PyExc_TypeError, - "expected a length 1 string for argument 1", 0); + return RAISERETURN(PyExc_TypeError, + "expected a length 1 string for argument 1", 0); } ch = *PyBytes_AS_STRING(obj); } diff --git a/src_c/surflock.c b/src_c/surflock.c index 81e6540973..fa17c518b5 100644 --- a/src_c/surflock.c +++ b/src_c/surflock.c @@ -100,7 +100,7 @@ pgSurface_LockBy(pgSurfaceObject *surfobj, PyObject *lockobj) pgSurface_Prep(surfobj); } if (!PG_LockSurface(surf->surf)) { - RAISERETURN(PyExc_RuntimeError, "error locking surface", 0); + return RAISERETURN(PyExc_RuntimeError, "error locking surface", 0); } return 1; } diff --git a/src_c/time.c b/src_c/time.c index 4c55520181..f70904491b 100644 --- a/src_c/time.c +++ b/src_c/time.c @@ -326,7 +326,7 @@ accurate_delay(Sint64 ticks) #if !SDL_VERSION_ATLEAST(3, 0, 0) if (!SDL_WasInit(SDL_INIT_TIMER)) { if (SDL_InitSubSystem(SDL_INIT_TIMER)) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } } #endif diff --git a/src_c/window.c b/src_c/window.c index 6e519c7353..a6bd997a0f 100644 --- a/src_c/window.c +++ b/src_c/window.c @@ -236,10 +236,11 @@ _window_opengl_set_viewport(SDL_Window *window, SDL_GLContext context, if (SDL_GL_MakeCurrent(window, context) < 0) #endif { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } if (p_glViewport == NULL) { - RAISERETURN(pgExc_SDLError, "glViewport function is unavailable", -1); + return RAISERETURN(pgExc_SDLError, + "glViewport function is unavailable", -1); } p_glViewport(0, 0, wnew, hnew); return 0; @@ -576,8 +577,8 @@ window_set_title(pgWindowObject *self, PyObject *arg, void *v) { const char *title; if (!PyUnicode_Check(arg)) { - RAISERETURN(PyExc_TypeError, "Argument to set_title must be a str.", - -1); + return RAISERETURN(PyExc_TypeError, + "Argument to set_title must be a str.", -1); } title = PyUnicode_AsUTF8(arg); SDL_SetWindowTitle(self->_win, title); @@ -675,15 +676,16 @@ window_set_mouse_rect(pgWindowObject *self, PyObject *arg, void *v) SDL_Rect tmp_rect; SDL_Rect *mouse_rect_p = pgRect_FromObject(arg, &tmp_rect); if (mouse_rect_p == NULL && arg != Py_None) { - RAISERETURN(PyExc_TypeError, - "mouse_rect should be a Rect-like object or None", -1); + return RAISERETURN(PyExc_TypeError, + "mouse_rect should be a Rect-like object or None", + -1); } #if SDL_VERSION_ATLEAST(3, 0, 0) if (!SDL_SetWindowMouseRect(self->_win, mouse_rect_p)) { #else if (SDL_SetWindowMouseRect(self->_win, mouse_rect_p) < 0) { #endif - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } #else if (PyErr_WarnEx(PyExc_Warning, @@ -718,13 +720,13 @@ window_set_size(pgWindowObject *self, PyObject *arg, void *v) int w, h; if (!pg_TwoIntsFromObj(arg, &w, &h)) { - RAISERETURN(PyExc_TypeError, "invalid size argument", -1); + return RAISERETURN(PyExc_TypeError, "invalid size argument", -1); } if (w <= 0 || h <= 0) { - RAISERETURN(PyExc_ValueError, - "width or height should not be less than or equal to zero", - -1); + return RAISERETURN( + PyExc_ValueError, + "width or height should not be less than or equal to zero", -1); } SDL_SetWindowSize(self->_win, w, h); @@ -760,21 +762,22 @@ window_set_minimum_size(pgWindowObject *self, PyObject *arg, void *v) int max_w, max_h; if (!pg_TwoIntsFromObj(arg, &w, &h)) { - RAISERETURN(PyExc_TypeError, "invalid size argument", -1); + return RAISERETURN(PyExc_TypeError, "invalid size argument", -1); } if (w < 0 || h < 0) { - RAISERETURN(PyExc_ValueError, - "minimum width or height should not be less than zero", - -1); + return RAISERETURN( + PyExc_ValueError, + "minimum width or height should not be less than zero", -1); } SDL_GetWindowMaximumSize(self->_win, &max_w, &max_h); if ((max_w > 0 && max_h > 0) && (w > max_w || h > max_h)) { - RAISERETURN(PyExc_ValueError, - "minimum width or height should not be greater than " - "maximum width or height respectively", - -1); + return RAISERETURN( + PyExc_ValueError, + "minimum width or height should not be greater than " + "maximum width or height respectively", + -1); } SDL_SetWindowMinimumSize(self->_win, w, h); @@ -798,21 +801,22 @@ window_set_maximum_size(pgWindowObject *self, PyObject *arg, void *v) int min_w, min_h; if (!pg_TwoIntsFromObj(arg, &w, &h)) { - RAISERETURN(PyExc_TypeError, "invalid size argument", -1); + return RAISERETURN(PyExc_TypeError, "invalid size argument", -1); } if (w < 0 || h < 0) { - RAISERETURN(PyExc_ValueError, - "maximum width or height should not be less than zero", - -1); + return RAISERETURN( + PyExc_ValueError, + "maximum width or height should not be less than zero", -1); } SDL_GetWindowMinimumSize(self->_win, &min_w, &min_h); if (w < min_w || h < min_h) { - RAISERETURN(PyExc_ValueError, - "maximum width or height should not be less than minimum " - "width or height respectively", - -1); + return RAISERETURN( + PyExc_ValueError, + "maximum width or height should not be less than minimum " + "width or height respectively", + -1); } SDL_SetWindowMaximumSize(self->_win, w, h); @@ -837,11 +841,12 @@ window_set_position(pgWindowObject *self, PyObject *arg, void *v) if (PyLong_Check(arg)) { x = y = PyLong_AsLong(arg); if (x != SDL_WINDOWPOS_CENTERED && x != SDL_WINDOWPOS_UNDEFINED) { - RAISERETURN(PyExc_TypeError, "invalid position argument", -1); + return RAISERETURN(PyExc_TypeError, "invalid position argument", + -1); } } else if (!pg_TwoIntsFromObj(arg, &x, &y)) { - RAISERETURN(PyExc_TypeError, "invalid position argument", -1); + return RAISERETURN(PyExc_TypeError, "invalid position argument", -1); } SDL_SetWindowPosition(self->_win, x, y); @@ -867,7 +872,7 @@ window_set_opacity(pgWindowObject *self, PyObject *arg, void *v) return -1; } if (SDL_SetWindowOpacity(self->_win, opacity)) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } return 0; } @@ -1002,7 +1007,8 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs) if (kwargs) { while (PyDict_Next(kwargs, &dict_pos, &_key, &_value)) { if (!PyUnicode_Check(_key)) { - RAISERETURN(PyExc_TypeError, "keywords must be strings", -1); + return RAISERETURN(PyExc_TypeError, "keywords must be strings", + -1); } _key_str = PyUnicode_AsUTF8(_key); @@ -1168,12 +1174,12 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs) if (size) { if (!pg_TwoIntsFromObj(size, &size_w, &size_h)) { - RAISERETURN(PyExc_TypeError, "invalid size argument", -1); + return RAISERETURN(PyExc_TypeError, "invalid size argument", -1); } } if (size_w <= 0 || size_h <= 0) { - RAISERETURN( + return RAISERETURN( PyExc_ValueError, "width or height should not be less than or equal to zero.", -1); } @@ -1183,22 +1189,24 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs) pos_x = pos_y = PyLong_AsLong(position); if (pos_x != SDL_WINDOWPOS_CENTERED && pos_x != SDL_WINDOWPOS_UNDEFINED) { - RAISERETURN(PyExc_TypeError, "invalid position argument", -1); + return RAISERETURN(PyExc_TypeError, + "invalid position argument", -1); } } else if (!pg_TwoIntsFromObj(position, &pos_x, &pos_y)) { - RAISERETURN(PyExc_TypeError, "invalid position argument", -1); + return RAISERETURN(PyExc_TypeError, "invalid position argument", + -1); } } _win = PG_CreateWindow(title, pos_x, pos_y, size_w, size_h, flags); if (!_win) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } #if SDL_VERSION_ATLEAST(3, 0, 0) if (fullscreen_non_desktop) { if (!pg_window_set_fullscreen(_win, 0)) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } } #endif @@ -1209,7 +1217,7 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs) if (flags & SDL_WINDOW_OPENGL) { SDL_GLContext context = SDL_GL_CreateContext(self->_win); if (context == NULL) { - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } /* As stated in the 'Remarks' of the docs * (https://wiki.libsdl.org/SDL2/SDL_GL_GetProcAddress) on Windows @@ -1236,7 +1244,7 @@ window_init(pgWindowObject *self, PyObject *args, PyObject *kwargs) if (SDL_SetColorKey(pgSurface_AsSurface(icon), SDL_TRUE, icon_colorkey) < 0) { #endif - RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); } } SDL_SetWindowIcon(self->_win, pgSurface_AsSurface(icon)); From 37c81d905231ee3d611f8e3fc87f48cb5c36c332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josip=20Komljenovi=C4=87?= Date: Sun, 8 Jun 2025 13:17:04 +0200 Subject: [PATCH 4/8] Use macro for exception raising --- src_c/base.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src_c/base.c b/src_c/base.c index 5dab6ee2de..1a9781d709 100644 --- a/src_c/base.c +++ b/src_c/base.c @@ -944,9 +944,8 @@ pgArrayStruct_AsDict(PyArrayInterface *inter_p) if (inter_p->flags & PAI_ARR_HAS_DESCR) { if (!inter_p->descr) { Py_DECREF(dictobj); - return RAISERETURN( - PyExc_ValueError, - "Array struct has descr flag set but no descriptor", 0); + return RAISE(PyExc_ValueError, + "Array struct has descr flag set but no descriptor"); } if (PyDict_SetItemString(dictobj, "descr", inter_p->descr)) { Py_DECREF(dictobj); From 9ac18f2cb13eb844ba12265caebe14a7d47f6415 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josip=20Komljenovi=C4=87?= Date: Sun, 8 Jun 2025 21:26:59 +0200 Subject: [PATCH 5/8] Use macro for exception raising --- src_c/_camera.c | 24 +-- src_c/_freetype.c | 24 +-- src_c/_pygame.h | 20 ++- src_c/_sdl2/controller.c | 43 ++--- src_c/_sdl2/touch.c | 12 +- src_c/base.c | 5 +- src_c/circle.c | 80 +++++---- src_c/color.c | 17 +- src_c/display.c | 189 +++++++++++--------- src_c/draw.c | 153 +++++++++------- src_c/event.c | 29 +-- src_c/font.c | 103 ++++++----- src_c/font.h | 8 +- src_c/gfxdraw.c | 140 ++++++++------- src_c/image.c | 233 ++++++++++++++---------- src_c/imageext.c | 28 +-- src_c/include/_pygame.h | 14 +- src_c/joystick.c | 44 ++--- src_c/key.c | 20 +-- src_c/line.c | 20 ++- src_c/mask.c | 97 +++++----- src_c/math.c | 244 +++++++++++++++---------- src_c/mixer.c | 31 ++-- src_c/mixer.h | 7 +- src_c/mouse.c | 46 ++--- src_c/music.c | 18 +- src_c/pixelarray.c | 55 +++--- src_c/pixelarray_methods.c | 48 +++-- src_c/pixelcopy.c | 55 +++--- src_c/rect.c | 4 +- src_c/rect_impl.h | 201 ++++++++++++--------- src_c/render.c | 49 ++--- src_c/rwobject.c | 19 +- src_c/scrap.c | 14 +- src_c/scrap.h | 7 +- src_c/scrap_sdl2.c | 6 +- src_c/scrap_win.c | 3 +- src_c/surface.c | 356 +++++++++++++++++++++---------------- src_c/system.c | 5 +- src_c/time.c | 39 ++-- src_c/transform.c | 280 ++++++++++++++++------------- src_c/window.c | 66 ++++--- 42 files changed, 1631 insertions(+), 1225 deletions(-) diff --git a/src_c/_camera.c b/src_c/_camera.c index 9fea073b95..d6d817cb88 100644 --- a/src_c/_camera.c +++ b/src_c/_camera.c @@ -104,7 +104,8 @@ surf_colorspace(PyObject *self, PyObject *arg) cspace = HSV_OUT; } else { - return RAISE(PyExc_ValueError, "Incorrect colorspace value"); + return RAISERETURN(PyExc_ValueError, "Incorrect colorspace value", + NULL); } surf = pgSurface_AsSurface(surfobj); @@ -121,18 +122,19 @@ surf_colorspace(PyObject *self, PyObject *arg) /* check to see if the size is the same. */ if (newsurf->w != surf->w || newsurf->h != surf->h) { - return RAISE(PyExc_ValueError, - "Surfaces not the same width and height."); + return RAISERETURN(PyExc_ValueError, + "Surfaces not the same width and height.", NULL); } /* check to see if the format of the surface is the same. */ if (PG_SURF_BitsPerPixel(surf) != PG_SURF_BitsPerPixel(newsurf)) { - return RAISE(PyExc_ValueError, "Surfaces not the same depth"); + return RAISERETURN(PyExc_ValueError, "Surfaces not the same depth", + NULL); } PG_PixelFormat *src_fmt = PG_GetSurfaceFormat(surf); if (!src_fmt) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_LockSurface(newsurf); @@ -413,8 +415,9 @@ camera_get_image(pgCameraObject *self, PyObject *arg) } if (surf->w != self->width || surf->h != self->height) { - return RAISE(PyExc_ValueError, - "Destination surface not the correct width or height."); + return RAISERETURN( + PyExc_ValueError, + "Destination surface not the correct width or height.", NULL); } Py_BEGIN_ALLOW_THREADS; @@ -428,7 +431,7 @@ camera_get_image(pgCameraObject *self, PyObject *arg) strerror(errno_code)); return NULL; } - return RAISE(PyExc_SystemError, "image processing error"); + return RAISERETURN(PyExc_SystemError, "image processing error", NULL); } if (surfobj) { @@ -461,8 +464,9 @@ camera_get_image(pgCameraObject *self, PyObject *arg) } if (surf->w != self->width || surf->h != self->height) { - return RAISE(PyExc_ValueError, - "Destination surface not the correct width or height."); + return RAISERETURN( + PyExc_ValueError, + "Destination surface not the correct width or height.", NULL); } if (!windows_read_frame(self, surf)) { diff --git a/src_c/_freetype.c b/src_c/_freetype.c index 01d4190870..34ed3a9149 100644 --- a/src_c/_freetype.c +++ b/src_c/_freetype.c @@ -195,10 +195,12 @@ free_string(PGFT_String *); /* * Auxiliary defines */ -#define ASSERT_SELF_IS_ALIVE(s) \ - if (!pgFont_IS_ALIVE(s)) { \ - return RAISE(PyExc_RuntimeError, MODULE_NAME \ - "." FONT_TYPE_NAME " instance is not initialized"); \ +#define ASSERT_SELF_IS_ALIVE(s) \ + if (!pgFont_IS_ALIVE(s)) { \ + return RAISERETURN(PyExc_RuntimeError, \ + MODULE_NAME "." FONT_TYPE_NAME \ + " instance is not initialized", \ + NULL); \ } #define PGFT_CHECK_BOOL(_pyobj, _var) \ @@ -1105,7 +1107,8 @@ _ftfont_getname(pgFontObject *self, void *closure) return name ? PyUnicode_FromString(name) : 0; } - return RAISE(PyExc_AttributeError, ""); + return RAISERETURN(PyExc_AttributeError, "", + NULL); } static PyObject * @@ -1120,7 +1123,8 @@ _ftfont_getstylename(pgFontObject *self, void *closure) return stylename ? PyUnicode_FromString(stylename) : 0; } - return RAISE(PyExc_AttributeError, ""); + return RAISERETURN(PyExc_AttributeError, "", + NULL); } static PyObject * @@ -2138,8 +2142,8 @@ _ft_autoinit(PyObject *self, PyObject *_null) } if (_PGFT_Init(&(FREETYPE_MOD_STATE(self)->freetype), cache_size)) { - return RAISE(PyExc_RuntimeError, - "Failed to initialize freetype library"); + return RAISERETURN(PyExc_RuntimeError, + "Failed to initialize freetype library", NULL); } FREETYPE_MOD_STATE(self)->cache_size = cache_size; @@ -2228,8 +2232,8 @@ _ft_get_version(PyObject *self, PyObject *args, PyObject *kwargs) int err = FT_Init_FreeType(&lib); if (err) { FT_Done_FreeType(lib); - return RAISE(PyExc_RuntimeError, - "FreeType could not be initialized"); + return RAISERETURN(PyExc_RuntimeError, + "FreeType could not be initialized", NULL); } FT_Int major, minor, patch; FT_Library_Version(lib, &major, &minor, &patch); diff --git a/src_c/_pygame.h b/src_c/_pygame.h index 625ef1ea9a..8bcefd1b98 100644 --- a/src_c/_pygame.h +++ b/src_c/_pygame.h @@ -564,7 +564,7 @@ typedef enum { PGS_SCROLL_ERASE = 0x00000004 } PygameScrollSurfaceFlags; -#define RAISE(x, y) (PyErr_SetString((x), (y)), NULL) +#define RAISE(x, y) (PyErr_SetString((x), (y))) #define RAISERETURN(x, y, r) (PyErr_SetString((x), (y)), r) #define DEL_ATTR_NOT_SUPPORTED_CHECK(name, value) \ do { \ @@ -587,13 +587,17 @@ typedef enum { * Initialization checks */ -#define VIDEO_INIT_CHECK() \ - if (!SDL_WasInit(SDL_INIT_VIDEO)) \ - return RAISE(pgExc_SDLError, "video system not initialized") - -#define JOYSTICK_INIT_CHECK() \ - if (!SDL_WasInit(SDL_INIT_JOYSTICK)) \ - return RAISE(pgExc_SDLError, "joystick system not initialized") +#define VIDEO_INIT_CHECK() \ + if (!SDL_WasInit(SDL_INIT_VIDEO)) { \ + return RAISERETURN(pgExc_SDLError, "video system not initialized", \ + NULL); \ + } + +#define JOYSTICK_INIT_CHECK() \ + if (!SDL_WasInit(SDL_INIT_JOYSTICK)) { \ + return RAISERETURN(pgExc_SDLError, "joystick system not initialized", \ + NULL); \ + } /* thread check */ #ifdef WITH_THREAD diff --git a/src_c/_sdl2/controller.c b/src_c/_sdl2/controller.c index f7950649a4..26fd973e80 100644 --- a/src_c/_sdl2/controller.c +++ b/src_c/_sdl2/controller.c @@ -4,9 +4,10 @@ #include "structmember.h" #include "../doc/sdl2_controller_doc.h" -#define CONTROLLER_INIT_CHECK() \ - if (!SDL_WasInit(SDL_INIT_GAMECONTROLLER)) \ - return RAISE(pgExc_SDLError, "Controller system not initialized"); +#define CONTROLLER_INIT_CHECK() \ + if (!SDL_WasInit(SDL_INIT_GAMECONTROLLER)) { \ + return RAISERETURN(pgExc_SDLError, "Controller system not initialized", NULL); \ + } typedef struct pgControllerObject { PyObject_HEAD int id; @@ -27,7 +28,7 @@ controller_module_init(PyObject *module, PyObject *_null) { if (!SDL_WasInit(SDL_INIT_GAMECONTROLLER)) { if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } SDL_GameControllerEventState(SDL_ENABLE); @@ -89,7 +90,7 @@ controller_module_get_count(PyObject *module, PyObject *_null) int count = SDL_NumJoysticks(); if (count < 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return PyLong_FromLong(count); @@ -159,7 +160,7 @@ controller_attached(pgControllerObject *self, PyObject *_null) { CONTROLLER_INIT_CHECK(); if (!self->controller) { - return RAISE(pgExc_SDLError, "Controller is not initialized"); + return RAISERETURN(pgExc_SDLError, "Controller is not initialized", NULL); } return PyBool_FromLong(SDL_GameControllerGetAttached(self->controller)); @@ -171,7 +172,7 @@ controller_as_joystick(pgControllerObject *self, PyObject *_null) CONTROLLER_INIT_CHECK(); JOYSTICK_INIT_CHECK(); if (!self->controller) { - return RAISE(pgExc_SDLError, "Controller is not initialized"); + return RAISERETURN(pgExc_SDLError, "Controller is not initialized", NULL); } return pgJoystick_New(self->id); @@ -188,11 +189,11 @@ controller_get_axis(pgControllerObject *self, PyObject *args, PyObject *kwargs) } CONTROLLER_INIT_CHECK(); if (!self->controller) { - return RAISE(pgExc_SDLError, "Controller is not initialized"); + return RAISERETURN(pgExc_SDLError, "Controller is not initialized", NULL); } if (axis < 0 || axis > SDL_CONTROLLER_AXIS_MAX - 1) { - return RAISE(pgExc_SDLError, "Invalid axis"); + return RAISERETURN(pgExc_SDLError, "Invalid axis", NULL); } return PyLong_FromLong(SDL_GameControllerGetAxis(self->controller, axis)); @@ -210,11 +211,11 @@ controller_get_button(pgControllerObject *self, PyObject *args, } CONTROLLER_INIT_CHECK(); if (!self->controller) { - return RAISE(pgExc_SDLError, "Controller is not initialized"); + return RAISERETURN(pgExc_SDLError, "Controller is not initialized", NULL); } if (button < 0 || button > SDL_CONTROLLER_BUTTON_MAX) { - return RAISE(pgExc_SDLError, "Invalid button"); + return RAISERETURN(pgExc_SDLError, "Invalid button", NULL); } return PyBool_FromLong( @@ -230,12 +231,12 @@ controller_get_mapping(pgControllerObject *self, PyObject *_null) CONTROLLER_INIT_CHECK(); if (!self->controller) { - return RAISE(pgExc_SDLError, "Controller is not initialized"); + return RAISERETURN(pgExc_SDLError, "Controller is not initialized", NULL); } mapping = SDL_GameControllerMapping(self->controller); if (!mapping) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } dict = PyDict_New(); @@ -285,7 +286,7 @@ controller_set_mapping(pgControllerObject *self, PyObject *args, CONTROLLER_INIT_CHECK(); if (!self->controller) { - return RAISE(pgExc_SDLError, "Controller is not initialized"); + return RAISERETURN(pgExc_SDLError, "Controller is not initialized", NULL); } char guid_str[64]; @@ -304,7 +305,7 @@ controller_set_mapping(pgControllerObject *self, PyObject *args, while (PyDict_Next(dict, &dict_index, &key, &value)) { if (!PyUnicode_Check(key) || !PyUnicode_Check(value)) { free(mapping); - return RAISE(PyExc_TypeError, "Dict items must be strings"); + return RAISERETURN(PyExc_TypeError, "Dict items must be strings", NULL); } key_str = PyUnicode_AsUTF8(key); @@ -318,7 +319,7 @@ controller_set_mapping(pgControllerObject *self, PyObject *args, key_str, value_str); if (res < 0) { free(mapping); - return RAISE(PyExc_RuntimeError, "Internal snprintf call failed"); + return RAISERETURN(PyExc_RuntimeError, "Internal snprintf call failed", NULL); } else if (res >= size - offset) { // Retry the same key value pair with more memory allocated @@ -343,7 +344,7 @@ controller_set_mapping(pgControllerObject *self, PyObject *args, free(mapping_string); if (res < 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return PyLong_FromLong(res); @@ -364,7 +365,7 @@ controller_rumble(pgControllerObject *self, PyObject *args, PyObject *kwargs) CONTROLLER_INIT_CHECK(); if (!self->controller) { - return RAISE(pgExc_SDLError, "Controller is not initialized"); + return RAISERETURN(pgExc_SDLError, "Controller is not initialized", NULL); } // rumble takes values in range 0 to 0xFFFF (65535) @@ -382,7 +383,7 @@ controller_stop_rumble(pgControllerObject *self, PyObject *_null) { CONTROLLER_INIT_CHECK(); if (!self->controller) { - return RAISE(pgExc_SDLError, "Controller is not initialized"); + return RAISERETURN(pgExc_SDLError, "Controller is not initialized", NULL); } SDL_GameControllerRumble(self->controller, 0, 0, 1); Py_RETURN_NONE; @@ -438,12 +439,12 @@ controller_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) CONTROLLER_INIT_CHECK(); if (id >= SDL_NumJoysticks() || !SDL_IsGameController(id)) { - return RAISE(pgExc_SDLError, "Invalid index"); + return RAISERETURN(pgExc_SDLError, "Invalid index", NULL); } SDL_GameController *controller = SDL_GameControllerOpen(id); if (!controller) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } cur = _first_controller; diff --git a/src_c/_sdl2/touch.c b/src_c/_sdl2/touch.c index 1c7bd83b57..a8fd499d96 100644 --- a/src_c/_sdl2/touch.c +++ b/src_c/_sdl2/touch.c @@ -33,15 +33,15 @@ pg_touch_get_device(PyObject *self, PyObject *index) { SDL_TouchID touchid; if (!PyLong_Check(index)) { - return RAISE(PyExc_TypeError, + return RAISERETURN(PyExc_TypeError, "index must be an integer " - "specifying a device to get the ID for"); + "specifying a device to get the ID for", NULL); } touchid = SDL_GetTouchDevice(PyLong_AsLong(index)); if (touchid == 0) { /* invalid index */ - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return PyLong_FromLongLong(touchid); } @@ -51,16 +51,16 @@ pg_touch_num_fingers(PyObject *self, PyObject *device_id) { int fingercount; if (!PyLong_Check(device_id)) { - return RAISE(PyExc_TypeError, + return RAISERETURN(PyExc_TypeError, "device_id must be an integer " - "specifying a touch device"); + "specifying a touch device", NULL); } VIDEO_INIT_CHECK(); fingercount = SDL_GetNumTouchFingers(PyLong_AsLongLong(device_id)); if (fingercount == 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return PyLong_FromLong(fingercount); } diff --git a/src_c/base.c b/src_c/base.c index 1a9781d709..a30eebadb6 100644 --- a/src_c/base.c +++ b/src_c/base.c @@ -944,8 +944,9 @@ pgArrayStruct_AsDict(PyArrayInterface *inter_p) if (inter_p->flags & PAI_ARR_HAS_DESCR) { if (!inter_p->descr) { Py_DECREF(dictobj); - return RAISE(PyExc_ValueError, - "Array struct has descr flag set but no descriptor"); + return RAISERETURN( + PyExc_ValueError, + "Array struct has descr flag set but no descriptor", NULL); } if (PyDict_SetItemString(dictobj, "descr", inter_p->descr)) { Py_DECREF(dictobj); diff --git a/src_c/circle.c b/src_c/circle.c index 70422c3f67..0903e823de 100644 --- a/src_c/circle.c +++ b/src_c/circle.c @@ -112,7 +112,8 @@ pg_circle_move(pgCircleObject *self, PyObject *const *args, Py_ssize_t nargs) double Dx, Dy; if (!pg_TwoDoublesFromFastcallArgs(args, nargs, &Dx, &Dy)) { - return RAISE(PyExc_TypeError, "move requires a pair of numbers"); + return RAISERETURN(PyExc_TypeError, "move requires a pair of numbers", + NULL); } return _pg_circle_subtype_new3(Py_TYPE(self), self->circle.x + Dx, @@ -126,7 +127,8 @@ pg_circle_move_ip(pgCircleObject *self, PyObject *const *args, double Dx, Dy; if (!pg_TwoDoublesFromFastcallArgs(args, nargs, &Dx, &Dy)) { - return RAISE(PyExc_TypeError, "move_ip requires a pair of numbers"); + return RAISERETURN(PyExc_TypeError, + "move_ip requires a pair of numbers", NULL); } self->circle.x += Dx; @@ -139,8 +141,9 @@ static PyObject * pg_circle_update(pgCircleObject *self, PyObject *const *args, Py_ssize_t nargs) { if (!pgCircle_FromObjectFastcall(args, nargs, &self->circle)) { - return RAISE(PyExc_TypeError, - "Circle.update requires a circle or CircleLike object"); + return RAISERETURN( + PyExc_TypeError, + "Circle.update requires a circle or CircleLike object", NULL); } Py_RETURN_NONE; } @@ -152,9 +155,9 @@ pg_circle_collidepoint(pgCircleObject *self, PyObject *const *args, double px, py; if (!pg_TwoDoublesFromFastcallArgs(args, nargs, &px, &py)) { - return RAISE( + return RAISERETURN( PyExc_TypeError, - "Circle.collidepoint requires a point or PointLike object"); + "Circle.collidepoint requires a point or PointLike object", NULL); } return PyBool_FromLong(pgCollision_CirclePoint(&self->circle, px, py)); @@ -167,7 +170,8 @@ pg_circle_collidecircle(pgCircleObject *self, PyObject *const *args, pgCircleBase other_circle; if (!pgCircle_FromObjectFastcall(args, nargs, &other_circle)) { - return RAISE(PyExc_TypeError, "A CircleType object was expected"); + return RAISERETURN(PyExc_TypeError, "A CircleType object was expected", + NULL); } return PyBool_FromLong( @@ -183,9 +187,11 @@ pg_circle_colliderect(pgCircleObject *self, PyObject *const *args, if (nargs == 1) { SDL_FRect temp, *tmp; if (!(tmp = pgFRect_FromObject(args[0], &temp))) { - return RAISE(PyExc_TypeError, - "Invalid rect, must be RectType or sequence of 4 " - "numbers"); + return RAISERETURN( + PyExc_TypeError, + "Invalid rect, must be RectType or sequence of 4 " + "numbers", + NULL); } x = (double)tmp->x; y = (double)tmp->y; @@ -195,15 +201,17 @@ pg_circle_colliderect(pgCircleObject *self, PyObject *const *args, else if (nargs == 2) { if (!pg_TwoDoublesFromObj(args[0], &x, &y) || !pg_TwoDoublesFromObj(args[1], &w, &h)) { - return RAISE(PyExc_TypeError, - "Invalid rect, all 4 fields must be numeric"); + return RAISERETURN(PyExc_TypeError, + "Invalid rect, all 4 fields must be numeric", + NULL); } } else if (nargs == 4) { if (!pg_DoubleFromObj(args[0], &x) || !pg_DoubleFromObj(args[1], &y) || !pg_DoubleFromObj(args[2], &w) || !pg_DoubleFromObj(args[3], &h)) { - return RAISE(PyExc_TypeError, - "Invalid rect, all 4 fields must be numeric"); + return RAISERETURN(PyExc_TypeError, + "Invalid rect, all 4 fields must be numeric", + NULL); } } else { @@ -241,7 +249,8 @@ static PyObject * pg_circle_rotate(pgCircleObject *self, PyObject *const *args, Py_ssize_t nargs) { if (!nargs || nargs > 2) { - return RAISE(PyExc_TypeError, "rotate requires 1 or 2 arguments"); + return RAISERETURN(PyExc_TypeError, "rotate requires 1 or 2 arguments", + NULL); } pgCircleBase *circle = &self->circle; @@ -251,8 +260,8 @@ pg_circle_rotate(pgCircleObject *self, PyObject *const *args, Py_ssize_t nargs) ry = circle->y; if (!pg_DoubleFromObj(args[0], &angle)) { - return RAISE(PyExc_TypeError, - "Invalid angle argument, must be numeric"); + return RAISERETURN(PyExc_TypeError, + "Invalid angle argument, must be numeric", NULL); } if (nargs != 2) { @@ -260,9 +269,11 @@ pg_circle_rotate(pgCircleObject *self, PyObject *const *args, Py_ssize_t nargs) } if (!pg_TwoDoublesFromObj(args[1], &rx, &ry)) { - return RAISE(PyExc_TypeError, - "Invalid rotation point argument, must be a sequence of " - "2 numbers"); + return RAISERETURN( + PyExc_TypeError, + "Invalid rotation point argument, must be a sequence of " + "2 numbers", + NULL); } PyObject *circle_obj = _pg_circle_subtype_new(Py_TYPE(self), circle); @@ -280,7 +291,8 @@ pg_circle_rotate_ip(pgCircleObject *self, PyObject *const *args, Py_ssize_t nargs) { if (!nargs || nargs > 2) { - return RAISE(PyExc_TypeError, "rotate requires 1 or 2 arguments"); + return RAISERETURN(PyExc_TypeError, "rotate requires 1 or 2 arguments", + NULL); } pgCircleBase *circle = &self->circle; @@ -290,8 +302,8 @@ pg_circle_rotate_ip(pgCircleObject *self, PyObject *const *args, ry = circle->y; if (!pg_DoubleFromObj(args[0], &angle)) { - return RAISE(PyExc_TypeError, - "Invalid angle argument, must be numeric"); + return RAISERETURN(PyExc_TypeError, + "Invalid angle argument, must be numeric", NULL); } if (nargs != 2) { @@ -300,9 +312,11 @@ pg_circle_rotate_ip(pgCircleObject *self, PyObject *const *args, } if (!pg_TwoDoublesFromObj(args[1], &rx, &ry)) { - return RAISE(PyExc_TypeError, - "Invalid rotation point argument, must be a sequence " - "of 2 numbers"); + return RAISERETURN( + PyExc_TypeError, + "Invalid rotation point argument, must be a sequence " + "of 2 numbers", + NULL); } _pg_rotate_circle_helper(circle, angle, rx, ry); @@ -367,7 +381,8 @@ pg_circle_collidelist(pgCircleObject *self, PyObject *arg) int colliding; if (!PySequence_Check(arg)) { - return RAISE(PyExc_TypeError, "colliders argument must be a sequence"); + return RAISERETURN(PyExc_TypeError, + "colliders argument must be a sequence", NULL); } /* fast path */ @@ -416,7 +431,8 @@ pg_circle_collidelistall(pgCircleObject *self, PyObject *arg) int colliding; if (!PySequence_Check(arg)) { - return RAISE(PyExc_TypeError, "Argument must be a sequence"); + return RAISERETURN(PyExc_TypeError, "Argument must be a sequence", + NULL); } ret = PyList_New(0); @@ -562,9 +578,11 @@ pg_circle_contains(pgCircleObject *self, PyObject *arg) result = pgCollision_CirclePoint(scirc, x, y); } else { - return RAISE(PyExc_TypeError, - "Invalid shape argument, must be a Circle, Rect / Frect " - "or a coordinate"); + return RAISERETURN( + PyExc_TypeError, + "Invalid shape argument, must be a Circle, Rect / Frect " + "or a coordinate", + NULL); } return PyBool_FromLong(result); diff --git a/src_c/color.c b/src_c/color.c index 0f541efe46..bb684db6d9 100644 --- a/src_c/color.c +++ b/src_c/color.c @@ -833,7 +833,8 @@ _color_lerp(pgColorObject *self, PyObject *args, PyObject *kw) */ static const double TOLERANCE = 1e-6; if ((amt < -TOLERANCE) || (amt > (1.0 + TOLERANCE))) { - return RAISE(PyExc_ValueError, "Argument 2 must be in range [0, 1]"); + return RAISERETURN(PyExc_ValueError, + "Argument 2 must be in range [0, 1]", NULL); } for (int i = 0; i < 4; i++) { @@ -882,8 +883,8 @@ _color_update(pgColorObject *self, PyObject *const *args, Py_ssize_t nargs) } } else { - return RAISE(PyExc_TypeError, - "update can take only 1, 3 or 4 arguments"); + return RAISERETURN(PyExc_TypeError, + "update can take only 1, 3 or 4 arguments", NULL); } Py_RETURN_NONE; } @@ -1757,7 +1758,8 @@ _color_set_length(pgColorObject *color, PyObject *args) } if (clength > 4 || clength < 1) { - return RAISE(PyExc_ValueError, "Length needs to be 1,2,3, or 4."); + return RAISERETURN(PyExc_ValueError, "Length needs to be 1,2,3, or 4.", + NULL); } color->len = clength; @@ -1772,7 +1774,7 @@ static PyObject * _color_item(pgColorObject *color, Py_ssize_t _index) { if ((_index > (color->len - 1))) { - return RAISE(PyExc_IndexError, "invalid index"); + return RAISERETURN(PyExc_IndexError, "invalid index", NULL); } switch (_index) { @@ -1785,7 +1787,7 @@ _color_item(pgColorObject *color, Py_ssize_t _index) case 3: return PyLong_FromLong(color->data[3]); default: - return RAISE(PyExc_IndexError, "invalid index"); + return RAISERETURN(PyExc_IndexError, "invalid index", NULL); } } @@ -1821,7 +1823,8 @@ _color_subscript(pgColorObject *self, PyObject *item) return _color_slice(self, start, stop); } else { - return RAISE(PyExc_TypeError, "slice steps not supported"); + return RAISERETURN(PyExc_TypeError, "slice steps not supported", + NULL); } } else { diff --git a/src_c/display.c b/src_c/display.c index 81e891d4f3..68e029e2fe 100644 --- a/src_c/display.c +++ b/src_c/display.c @@ -243,7 +243,7 @@ pg_display_init(PyObject *self, PyObject *_null) } if (SDL_InitSubSystem(SDL_INIT_VIDEO)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } @@ -349,7 +349,8 @@ pg_vidinfo_getattr(PyObject *self, char *name) return PyUnicode_FromString(pixel_format_name); } - return RAISE(PyExc_AttributeError, "does not exist in vidinfo"); + return RAISERETURN(PyExc_AttributeError, "does not exist in vidinfo", + NULL); } PyObject * @@ -396,7 +397,7 @@ pgVidInfo_New(const pg_VideoInfo *i) { pgVidInfoObject *info; if (!i) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } info = PyObject_New(pgVidInfoObject, &pgVidInfo_Type); if (!info) { @@ -647,7 +648,7 @@ pg_gl_set_attribute(PyObject *self, PyObject *arg) } result = SDL_GL_SetAttribute(flag, value); if (result == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -662,7 +663,7 @@ pg_gl_get_attribute(PyObject *self, PyObject *arg) } result = SDL_GL_GetAttribute(flag, &value); if (result == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return PyLong_FromLong(value); } @@ -910,11 +911,12 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds) if (size != NULL) { if (!pg_TwoIntsFromObj(size, &w, &h)) { - return RAISE(PyExc_TypeError, "size must be two numbers"); + return RAISERETURN(PyExc_TypeError, "size must be two numbers", + NULL); } if (w < 0 || h < 0) { - return RAISE(pgExc_SDLError, - "Cannot set negative sized display mode"); + return RAISERETURN(pgExc_SDLError, + "Cannot set negative sized display mode", NULL); } } else { @@ -933,8 +935,8 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds) } if ((vsync == -1) && ((flags & PGS_OPENGL) == 0)) { - return RAISE(PyExc_ValueError, - "requested adaptive vsync without OpenGL"); + return RAISERETURN(PyExc_ValueError, + "requested adaptive vsync without OpenGL", NULL); } state->using_gl = (flags & PGS_OPENGL) != 0; @@ -979,7 +981,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds) SDL_DisplayMode display_mode; if (SDL_GetDesktopDisplayMode(display, &display_mode) != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (w == 0 && h == 0 && !(flags & PGS_SCALED)) { @@ -1008,8 +1010,9 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds) if (flags & PGS_SCALED) { if (w == 0 || h == 0) { - return RAISE(pgExc_SDLError, - "Cannot set 0 sized SCALED display mode"); + return RAISERETURN(pgExc_SDLError, + "Cannot set 0 sized SCALED display mode", + NULL); } } @@ -1109,7 +1112,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds) if (0 != SDL_GetDisplayUsableBounds(display, &display_bounds)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (SDL_GetHintBoolean("SDL_HINT_RENDER_SCALE_QUALITY", @@ -1170,7 +1173,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds) h_actual = h_1; } if (!win) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } else { @@ -1194,7 +1197,7 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds) SDL_SetWindowFullscreen( win, sdl_flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP))) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_SetWindowResizable(win, flags & PGS_RESIZABLE); @@ -1295,7 +1298,8 @@ pg_set_mode(PyObject *self, PyObject *arg, PyObject *kwds) } if (pg_renderer == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), + NULL); } if (flags & PGS_SCALED) { @@ -1504,7 +1508,7 @@ pg_window_size(PyObject *self, PyObject *_null) SDL_Window *win = pg_GetDefaultWindow(); int w, h; if (!win) { - return RAISE(pgExc_SDLError, "No open window"); + return RAISERETURN(pgExc_SDLError, "No open window", NULL); } SDL_GetWindowSize(win, &w, &h); return pg_tuple_couple_from_values_int(w, h); @@ -1516,7 +1520,7 @@ pg_get_window_position(PyObject *self, PyObject *_null) SDL_Window *win = pg_GetDefaultWindow(); int x, y = 0; if (!win) { - return RAISE(pgExc_SDLError, "No open window"); + return RAISERETURN(pgExc_SDLError, "No open window", NULL); } SDL_GetWindowPosition(win, &x, &y); return pg_tuple_couple_from_values_int(x, y); @@ -1535,7 +1539,8 @@ pg_set_window_position(PyObject *self, PyObject *arg) if (pos != NULL) { if (!pg_TwoIntsFromObj(pos, &x, &y)) { - return RAISE(PyExc_TypeError, "position must be two numbers"); + return RAISERETURN(PyExc_TypeError, "position must be two numbers", + NULL); } } @@ -1565,9 +1570,10 @@ pg_mode_ok(PyObject *self, PyObject *args, PyObject *kwds) return NULL; } if (display_index < 0 || display_index >= SDL_GetNumVideoDisplays()) { - return RAISE(PyExc_ValueError, - "The display index must be between 0" - " and the number of displays."); + return RAISERETURN(PyExc_ValueError, + "The display index must be between 0" + " and the number of displays.", + NULL); } #pragma PG_WARN(Ignoring most flags) @@ -1621,23 +1627,24 @@ pg_list_modes(PyObject *self, PyObject *args, PyObject *kwds) } if (display_index < 0 || display_index >= SDL_GetNumVideoDisplays()) { - return RAISE(PyExc_ValueError, - "The display index must be between 0" - " and the number of displays."); + return RAISERETURN(PyExc_ValueError, + "The display index must be between 0" + " and the number of displays.", + NULL); } #pragma PG_WARN(Ignoring flags) if (bpp == 0) { SDL_DisplayMode curmode; if (SDL_GetCurrentDisplayMode(display_index, &curmode)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } bpp = SDL_BITSPERPIXEL(curmode.format); } nummodes = SDL_GetNumDisplayModes(display_index); if (nummodes < 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (!(list = PyList_New(0))) { @@ -1647,7 +1654,7 @@ pg_list_modes(PyObject *self, PyObject *args, PyObject *kwds) for (i = 0; i < nummodes; i++) { if (SDL_GetDisplayMode(display_index, i, &mode) < 0) { Py_DECREF(list); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } /* use reasonable defaults (cf. SDL_video.c) */ if (!mode.format) { @@ -1748,7 +1755,7 @@ pg_num_displays(PyObject *self, PyObject *_null) { int ret = SDL_GetNumVideoDisplays(); if (ret < 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return PyLong_FromLong(ret); } @@ -1782,7 +1789,7 @@ pg_update(PyObject *self, PyObject *arg) VIDEO_INIT_CHECK(); if (!win) { - return RAISE(pgExc_SDLError, "Display mode not set"); + return RAISERETURN(pgExc_SDLError, "Display mode not set", NULL); } if (pg_renderer != NULL) { @@ -1791,7 +1798,8 @@ pg_update(PyObject *self, PyObject *arg) SDL_GetWindowSize(win, &wide, &high); if (state->using_gl) { - return RAISE(pgExc_SDLError, "Cannot update an OPENGL display"); + return RAISERETURN(pgExc_SDLError, "Cannot update an OPENGL display", + NULL); } /*determine type of argument we got*/ @@ -1819,9 +1827,10 @@ pg_update(PyObject *self, PyObject *arg) int count; SDL_Rect *rects; if (PyTuple_Size(arg) != 1) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "update requires a rectstyle or an iterable of rectstyles"); + "update requires a rectstyle or an iterable of rectstyles", + NULL); } single_arg = PyTuple_GET_ITEM(arg, 0); @@ -1835,9 +1844,10 @@ pg_update(PyObject *self, PyObject *arg) } iterable = PyObject_GetIter(single_arg); if (!iterable) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "update requires a rectstyle or an iterable of rectstyles"); + "update requires a rectstyle or an iterable of rectstyles", + NULL); } rects = PyMem_New(SDL_Rect, num); @@ -1867,8 +1877,9 @@ pg_update(PyObject *self, PyObject *arg) if (!gr) { Py_DECREF(iterable); PyMem_Free((void *)rects); - return RAISE(PyExc_ValueError, - "update_rects requires a single list of rects"); + return RAISERETURN( + PyExc_ValueError, + "update_rects requires a single list of rects", NULL); } if (gr->w < 1 && gr->h < 1) { @@ -1923,7 +1934,7 @@ pg_set_palette(PyObject *self, PyObject *args) return NULL; } if (!surface) { - return RAISE(pgExc_SDLError, "No display mode is set"); + return RAISERETURN(pgExc_SDLError, "No display mode is set", NULL); } Py_INCREF(surface); @@ -1931,7 +1942,8 @@ pg_set_palette(PyObject *self, PyObject *args) pal = surf->format->palette; if (PG_SURF_BytesPerPixel(surf) != 1 || !pal) { Py_DECREF(surface); - return RAISE(pgExc_SDLError, "Display mode is not colormapped"); + return RAISERETURN(pgExc_SDLError, "Display mode is not colormapped", + NULL); } if (!list) { @@ -1941,7 +1953,8 @@ pg_set_palette(PyObject *self, PyObject *args) if (!PySequence_Check(list)) { Py_DECREF(surface); - return RAISE(PyExc_ValueError, "Argument must be a sequence type"); + return RAISERETURN(PyExc_ValueError, + "Argument must be a sequence type", NULL); } len = (int)MIN(pal->ncolors, PySequence_Length(list)); @@ -1987,13 +2000,13 @@ pg_set_palette(PyObject *self, PyObject *args) if (!pal) { free((char *)colors); Py_DECREF(surface); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (!SDL_SetPaletteColors(pal, colors, 0, len)) { SDL_FreePalette(pal); free((char *)colors); Py_DECREF(surface); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_SetSurfacePalette(surf, pal); @@ -2195,8 +2208,8 @@ pg_set_icon(PyObject *self, PyObject *surface) _DisplayState *state = DISPLAY_MOD_STATE(self); SDL_Window *win = pg_GetDefaultWindow(); if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, - "Argument to set_icon must be a Surface"); + return RAISERETURN(PyExc_TypeError, + "Argument to set_icon must be a Surface", NULL); } if (!SDL_WasInit(SDL_INIT_VIDEO)) { @@ -2219,7 +2232,7 @@ pg_iconify(PyObject *self, PyObject *_null) SDL_Window *win = pg_GetDefaultWindow(); VIDEO_INIT_CHECK(); if (!win) { - return RAISE(pgExc_SDLError, "No open window"); + return RAISERETURN(pgExc_SDLError, "No open window", NULL); } SDL_MinimizeWindow(win); return PyBool_FromLong(1); @@ -2241,7 +2254,7 @@ pg_get_scaled_renderer_info(PyObject *self, PyObject *_null) } if (SDL_GetRendererInfo(pg_renderer, &r_info)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return Py_BuildValue("(si)", r_info.name, r_info.flags); @@ -2258,7 +2271,7 @@ pg_get_desktop_screen_sizes(PyObject *self, PyObject *_null) display_count = SDL_GetNumVideoDisplays(); if (display_count < 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } result = PyList_New(display_count); @@ -2269,7 +2282,7 @@ pg_get_desktop_screen_sizes(PyObject *self, PyObject *_null) for (i = 0; i < display_count; i++) { if (SDL_GetDesktopDisplayMode(i, &dm)) { Py_DECREF(result); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } size_tuple = pg_tuple_couple_from_values_int(dm.w, dm.h); @@ -2291,7 +2304,7 @@ pg_is_fullscreen(PyObject *self, PyObject *_null) VIDEO_INIT_CHECK(); if (!win) { - return RAISE(pgExc_SDLError, "No open window"); + return RAISERETURN(pgExc_SDLError, "No open window", NULL); } flags = SDL_GetWindowFlags(win) & SDL_WINDOW_FULLSCREEN_DESKTOP; @@ -2312,14 +2325,14 @@ pg_is_vsync(PyObject *self, PyObject *_null) VIDEO_INIT_CHECK(); if (!win) { - return RAISE(pgExc_SDLError, "No open window"); + return RAISERETURN(pgExc_SDLError, "No open window", NULL); } if (pg_renderer != NULL) { SDL_RendererInfo info; if (SDL_GetRendererInfo(pg_renderer, &info) != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (info.flags & SDL_RENDERER_PRESENTVSYNC) { @@ -2351,16 +2364,16 @@ pg_current_refresh_rate(PyObject *self, PyObject *_null) VIDEO_INIT_CHECK(); if (!win) { - return RAISE(pgExc_SDLError, "No open window"); + return RAISERETURN(pgExc_SDLError, "No open window", NULL); } display_index = SDL_GetWindowDisplayIndex(win); if (display_index < 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (SDL_GetCurrentDisplayMode(display_index, &mode) != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return PyLong_FromLong(mode.refresh_rate); @@ -2377,7 +2390,7 @@ pg_desktop_refresh_rates(PyObject *self, PyObject *_null) display_count = SDL_GetNumVideoDisplays(); if (display_count < 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } result = PyList_New(display_count); @@ -2388,7 +2401,7 @@ pg_desktop_refresh_rates(PyObject *self, PyObject *_null) for (i = 0; i < display_count; i++) { if (SDL_GetDesktopDisplayMode(i, &dm)) { Py_DECREF(result); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } refresh_rate = PyLong_FromLong(dm.refresh_rate); @@ -2417,24 +2430,25 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null) VIDEO_INIT_CHECK(); if (!win) { - return RAISE(pgExc_SDLError, "No open window"); + return RAISERETURN(pgExc_SDLError, "No open window", NULL); } flags = SDL_GetWindowFlags(win); SDL_VERSION(&wm_info.version); if (!SDL_GetWindowWMInfo(win, &wm_info)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (state->using_gl && pg_renderer != NULL) { - return RAISE(pgExc_SDLError, - "OPENGL and SDL_Renderer active at the same time"); + return RAISERETURN(pgExc_SDLError, + "OPENGL and SDL_Renderer active at the same time", + NULL); } if (pg_renderer != NULL) { if (SDL_GetRendererInfo(pg_renderer, &r_info) != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } @@ -2475,7 +2489,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null) case SDL_SYSWM_VIVANTE: case SDL_SYSWM_UNKNOWN: default: - return RAISE(pgExc_SDLError, "Unsupported platform"); + return RAISERETURN(pgExc_SDLError, "Unsupported platform", NULL); } display_surface = pg_GetDefaultWindowSurface(); @@ -2485,7 +2499,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null) SDL_GetWindowPosition(win, &x, &y); window_display = SDL_GetWindowDisplayIndex(win); if (SDL_GetDesktopDisplayMode(window_display, &display_mode) != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } /* @@ -2508,7 +2522,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null) if (state->unscaled_render) { result = SDL_SetWindowFullscreen(win, 0); if (result != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } else if (pg_renderer != NULL && !(state->unscaled_render)) { @@ -2523,7 +2537,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null) } result = SDL_SetWindowFullscreen(win, 0); if (result != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_SetWindowSize(win, w * scale, h * scale); @@ -2552,7 +2566,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null) * display surface we can query for dimensions. */ result = SDL_SetWindowFullscreen(win, 0); if (result != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_GL_MakeCurrent(win, state->gl_context); if (state->toggle_windowed_w > 0 && state->toggle_windowed_h > 0) { @@ -2585,7 +2599,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null) SDL_WINDOW_FULLSCREEN_DESKTOP) { result = SDL_SetWindowFullscreen(win, 0); if (result != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } display_surface->surf = SDL_GetWindowSurface(win); } @@ -2607,7 +2621,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null) /* SDL_WINDOW_FULLSCREEN_DESKTOP includes SDL_WINDOW_FULLSCREEN */ win = SDL_CreateWindow(state->title, wx, wy, w, h, flags); if (win == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } else { result = 0; @@ -2618,7 +2632,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null) else { result = SDL_SetWindowFullscreen(win, 0); if (result != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } display_surface->surf = SDL_GetWindowSurface(win); } @@ -2655,14 +2669,14 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null) result = SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP); if (result != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } else if (pg_renderer != NULL) { result = SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP); if (result != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (r_info.flags & SDL_RENDERER_SOFTWARE && wm_info.subsystem == SDL_SYSWM_X11) { @@ -2689,7 +2703,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null) result = SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP); if (result != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_GL_MakeCurrent(win, state->gl_context); if (state->scaled_gl) { @@ -2716,7 +2730,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null) result = SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP); if (result != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } display_surface->surf = SDL_GetWindowSurface(win); } @@ -2734,7 +2748,7 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null) else { result = SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN); if (result != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } display_surface->surf = SDL_GetWindowSurface(win); if (w != display_surface->surf->w || @@ -2743,10 +2757,10 @@ pg_toggle_fullscreen(PyObject *self, PyObject *_null) int wy = SDL_WINDOWPOS_UNDEFINED_DISPLAY(window_display); win = SDL_CreateWindow(state->title, wx, wy, w, h, flags); if (win == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (0 != SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } display_surface->surf = SDL_GetWindowSurface(win); pg_SetDefaultWindow(win); @@ -2782,7 +2796,7 @@ pg_display_resize_event(PyObject *self, PyObject *event) VIDEO_INIT_CHECK(); if (!win) { - return RAISE(pgExc_SDLError, "No open window"); + return RAISERETURN(pgExc_SDLError, "No open window", NULL); } flags = SDL_GetWindowFlags(win) & @@ -2796,7 +2810,7 @@ pg_display_resize_event(PyObject *self, PyObject *event) SDL_GetWindowSize(win, &window_w, &window_h); window_display = SDL_GetWindowDisplayIndex(win); if (SDL_GetDesktopDisplayMode(window_display, &display_mode) != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (state->using_gl) { @@ -2825,7 +2839,7 @@ pg_display_resize_event(PyObject *self, PyObject *event) SDL_SetWindowSize(win, (w > wnew) ? w : wnew, (h > hnew) ? h : hnew); result = SDL_RenderSetLogicalSize(pg_renderer, w, h); if (result != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } else { @@ -2926,7 +2940,8 @@ pg_message_box(PyObject *self, PyObject *arg, PyObject *kwargs) } else { if (!pgWindow_Check(parent_window)) { - return RAISE(PyExc_TypeError, "'parent_window' must be a Window"); + return RAISERETURN(PyExc_TypeError, + "'parent_window' must be a Window", NULL); } msgbox_data.window = ((pgWindowObject *)parent_window)->_win; } @@ -2986,15 +3001,17 @@ pg_message_box(PyObject *self, PyObject *arg, PyObject *kwargs) return NULL; } else if (num_buttons == 0) { - return RAISE(PyExc_TypeError, - "'buttons' should contain at least 1 button"); + return RAISERETURN(PyExc_TypeError, + "'buttons' should contain at least 1 button", + NULL); } if (return_button_index < 0) { return_button_index = (int)num_buttons + return_button_index; } if (0 > return_button_index || return_button_index >= num_buttons) { - return RAISE(PyExc_IndexError, "return_button index out of range"); + return RAISERETURN(PyExc_IndexError, + "return_button index out of range", NULL); } if (escape_button_used) { if (escape_button_index < 0) { @@ -3002,8 +3019,8 @@ pg_message_box(PyObject *self, PyObject *arg, PyObject *kwargs) } if (0 > escape_button_index || escape_button_index >= num_buttons) { - return RAISE(PyExc_IndexError, - "escape_button index out of range"); + return RAISERETURN(PyExc_IndexError, + "escape_button index out of range", NULL); } } diff --git a/src_c/draw.c b/src_c/draw.c index 3098174c76..abdd260454 100644 --- a/src_c/draw.c +++ b/src_c/draw.c @@ -161,22 +161,23 @@ aaline(PyObject *self, PyObject *arg, PyObject *kwargs) SDL_Rect surf_clip_rect; if (!PG_GetSurfaceClipRect(surf, &surf_clip_rect)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf); if (surf_format == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } CHECK_LOAD_COLOR(colorobj) if (!pg_TwoFloatsFromObj(start, &startx, &starty)) { - return RAISE(PyExc_TypeError, "invalid start_pos argument"); + return RAISERETURN(PyExc_TypeError, "invalid start_pos argument", + NULL); } if (!pg_TwoFloatsFromObj(end, &endx, &endy)) { - return RAISE(PyExc_TypeError, "invalid end_pos argument"); + return RAISERETURN(PyExc_TypeError, "invalid end_pos argument", NULL); } if (width < 1) { @@ -184,7 +185,7 @@ aaline(PyObject *self, PyObject *arg, PyObject *kwargs) } if (!pgSurface_Lock(surfobj)) { - return RAISE(PyExc_RuntimeError, "error locking surface"); + return RAISERETURN(PyExc_RuntimeError, "error locking surface", NULL); } if (width > 1) { @@ -204,7 +205,8 @@ aaline(PyObject *self, PyObject *arg, PyObject *kwargs) } if (!pgSurface_Unlock(surfobj)) { - return RAISE(PyExc_RuntimeError, "error unlocking surface"); + return RAISERETURN(PyExc_RuntimeError, "error unlocking surface", + NULL); } if (drawn_area[0] != INT_MAX && drawn_area[1] != INT_MAX && @@ -253,17 +255,18 @@ line(PyObject *self, PyObject *arg, PyObject *kwargs) SDL_Rect surf_clip_rect; if (!PG_GetSurfaceClipRect(surf, &surf_clip_rect)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } CHECK_LOAD_COLOR(colorobj) if (!pg_TwoIntsFromObj(start, &startx, &starty)) { - return RAISE(PyExc_TypeError, "invalid start_pos argument"); + return RAISERETURN(PyExc_TypeError, "invalid start_pos argument", + NULL); } if (!pg_TwoIntsFromObj(end, &endx, &endy)) { - return RAISE(PyExc_TypeError, "invalid end_pos argument"); + return RAISERETURN(PyExc_TypeError, "invalid end_pos argument", NULL); } if (width < 1) { @@ -271,14 +274,15 @@ line(PyObject *self, PyObject *arg, PyObject *kwargs) } if (!pgSurface_Lock(surfobj)) { - return RAISE(PyExc_RuntimeError, "error locking surface"); + return RAISERETURN(PyExc_RuntimeError, "error locking surface", NULL); } draw_line_width(surf, surf_clip_rect, color, startx, starty, endx, endy, width, drawn_area); if (!pgSurface_Unlock(surfobj)) { - return RAISE(PyExc_RuntimeError, "error unlocking surface"); + return RAISERETURN(PyExc_RuntimeError, "error unlocking surface", + NULL); } /* Compute return rect. */ @@ -348,26 +352,28 @@ aalines(PyObject *self, PyObject *arg, PyObject *kwargs) SDL_Rect surf_clip_rect; if (!PG_GetSurfaceClipRect(surf, &surf_clip_rect)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf); if (surf_format == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } CHECK_LOAD_COLOR(colorobj) if (!PySequence_Check(points)) { - return RAISE(PyExc_TypeError, - "points argument must be a sequence of number pairs"); + return RAISERETURN( + PyExc_TypeError, + "points argument must be a sequence of number pairs", NULL); } length = PySequence_Length(points); if (length < 2) { - return RAISE(PyExc_ValueError, - "points argument must contain 2 or more points"); + return RAISERETURN(PyExc_ValueError, + "points argument must contain 2 or more points", + NULL); } // Allocate bytes for the xlist and ylist at once to reduce allocations. @@ -376,8 +382,8 @@ aalines(PyObject *self, PyObject *arg, PyObject *kwargs) float *ylist = points_buf + length; if (points_buf == NULL) { - return RAISE(PyExc_MemoryError, - "cannot allocate memory to draw aalines"); + return RAISERETURN(PyExc_MemoryError, + "cannot allocate memory to draw aalines", NULL); } for (loop = 0; loop < length; ++loop) { @@ -391,7 +397,8 @@ aalines(PyObject *self, PyObject *arg, PyObject *kwargs) if (!result) { PyMem_Free(points_buf); - return RAISE(PyExc_TypeError, "points must be number pairs"); + return RAISERETURN(PyExc_TypeError, "points must be number pairs", + NULL); } xlist[loop] = x; @@ -400,7 +407,7 @@ aalines(PyObject *self, PyObject *arg, PyObject *kwargs) if (!pgSurface_Lock(surfobj)) { PyMem_Free(points_buf); - return RAISE(PyExc_RuntimeError, "error locking surface"); + return RAISERETURN(PyExc_RuntimeError, "error locking surface", NULL); } /* first line - if open, add endpoint pixels.*/ @@ -500,7 +507,8 @@ aalines(PyObject *self, PyObject *arg, PyObject *kwargs) PyMem_Free(points_buf); if (!pgSurface_Unlock(surfobj)) { - return RAISE(PyExc_RuntimeError, "error unlocking surface"); + return RAISERETURN(PyExc_RuntimeError, "error unlocking surface", + NULL); } /* Compute return rect. */ @@ -553,21 +561,23 @@ lines(PyObject *self, PyObject *arg, PyObject *kwargs) SDL_Rect surf_clip_rect; if (!PG_GetSurfaceClipRect(surf, &surf_clip_rect)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } CHECK_LOAD_COLOR(colorobj) if (!PySequence_Check(points)) { - return RAISE(PyExc_TypeError, - "points argument must be a sequence of number pairs"); + return RAISERETURN( + PyExc_TypeError, + "points argument must be a sequence of number pairs", NULL); } length = PySequence_Length(points); if (length < 2) { - return RAISE(PyExc_ValueError, - "points argument must contain 2 or more points"); + return RAISERETURN(PyExc_ValueError, + "points argument must contain 2 or more points", + NULL); } xlist = PyMem_New(int, length); @@ -580,8 +590,8 @@ lines(PyObject *self, PyObject *arg, PyObject *kwargs) if (ylist) { PyMem_Free(ylist); } - return RAISE(PyExc_MemoryError, - "cannot allocate memory to draw lines"); + return RAISERETURN(PyExc_MemoryError, + "cannot allocate memory to draw lines", NULL); } for (loop = 0; loop < length; ++loop) { @@ -592,7 +602,8 @@ lines(PyObject *self, PyObject *arg, PyObject *kwargs) if (!result) { PyMem_Free(xlist); PyMem_Free(ylist); - return RAISE(PyExc_TypeError, "points must be number pairs"); + return RAISERETURN(PyExc_TypeError, "points must be number pairs", + NULL); } xlist[loop] = x; @@ -611,7 +622,7 @@ lines(PyObject *self, PyObject *arg, PyObject *kwargs) if (!pgSurface_Lock(surfobj)) { PyMem_Free(xlist); PyMem_Free(ylist); - return RAISE(PyExc_RuntimeError, "error locking surface"); + return RAISERETURN(PyExc_RuntimeError, "error locking surface", NULL); } for (loop = 1; loop < length; ++loop) { @@ -630,7 +641,8 @@ lines(PyObject *self, PyObject *arg, PyObject *kwargs) PyMem_Free(ylist); if (!pgSurface_Unlock(surfobj)) { - return RAISE(PyExc_RuntimeError, "error unlocking surface"); + return RAISERETURN(PyExc_RuntimeError, "error unlocking surface", + NULL); } /* Compute return rect. */ @@ -669,7 +681,7 @@ arc(PyObject *self, PyObject *arg, PyObject *kwargs) rect = pgRect_FromObject(rectobj, &temp); if (!rect) { - return RAISE(PyExc_TypeError, "rect argument is invalid"); + return RAISERETURN(PyExc_TypeError, "rect argument is invalid", NULL); } surf = pgSurface_AsSurface(surfobj); @@ -683,7 +695,7 @@ arc(PyObject *self, PyObject *arg, PyObject *kwargs) SDL_Rect surf_clip_rect; if (!PG_GetSurfaceClipRect(surf, &surf_clip_rect)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } CHECK_LOAD_COLOR(colorobj) @@ -702,7 +714,7 @@ arc(PyObject *self, PyObject *arg, PyObject *kwargs) } if (!pgSurface_Lock(surfobj)) { - return RAISE(PyExc_RuntimeError, "error locking surface"); + return RAISERETURN(PyExc_RuntimeError, "error locking surface", NULL); } width = MIN(width, MIN(rect->w, rect->h) / 2); @@ -712,7 +724,8 @@ arc(PyObject *self, PyObject *arg, PyObject *kwargs) angle_start, angle_stop, color, drawn_area); if (!pgSurface_Unlock(surfobj)) { - return RAISE(PyExc_RuntimeError, "error unlocking surface"); + return RAISERETURN(PyExc_RuntimeError, "error unlocking surface", + NULL); } /* Compute return rect. */ @@ -749,7 +762,7 @@ ellipse(PyObject *self, PyObject *arg, PyObject *kwargs) rect = pgRect_FromObject(rectobj, &temp); if (!rect) { - return RAISE(PyExc_TypeError, "rect argument is invalid"); + return RAISERETURN(PyExc_TypeError, "rect argument is invalid", NULL); } surf = pgSurface_AsSurface(surfobj); @@ -763,7 +776,7 @@ ellipse(PyObject *self, PyObject *arg, PyObject *kwargs) SDL_Rect surf_clip_rect; if (!PG_GetSurfaceClipRect(surf, &surf_clip_rect)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } CHECK_LOAD_COLOR(colorobj) @@ -773,7 +786,7 @@ ellipse(PyObject *self, PyObject *arg, PyObject *kwargs) } if (!pgSurface_Lock(surfobj)) { - return RAISE(PyExc_RuntimeError, "error locking surface"); + return RAISERETURN(PyExc_RuntimeError, "error locking surface", NULL); } if (!width || @@ -787,7 +800,8 @@ ellipse(PyObject *self, PyObject *arg, PyObject *kwargs) } if (!pgSurface_Unlock(surfobj)) { - return RAISE(PyExc_RuntimeError, "error unlocking surface"); + return RAISERETURN(PyExc_RuntimeError, "error unlocking surface", + NULL); } if (drawn_area[0] != INT_MAX && drawn_area[1] != INT_MAX && @@ -853,7 +867,7 @@ circle(PyObject *self, PyObject *args, PyObject *kwargs) SDL_Rect surf_clip_rect; if (!PG_GetSurfaceClipRect(surf, &surf_clip_rect)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } CHECK_LOAD_COLOR(colorobj) @@ -874,7 +888,7 @@ circle(PyObject *self, PyObject *args, PyObject *kwargs) } if (!pgSurface_Lock(surfobj)) { - return RAISE(PyExc_RuntimeError, "error locking surface"); + return RAISERETURN(PyExc_RuntimeError, "error locking surface", NULL); } if ((top_right == 0 && top_left == 0 && bottom_left == 0 && @@ -899,7 +913,8 @@ circle(PyObject *self, PyObject *args, PyObject *kwargs) } if (!pgSurface_Unlock(surfobj)) { - return RAISE(PyExc_RuntimeError, "error unlocking surface"); + return RAISERETURN(PyExc_RuntimeError, "error unlocking surface", + NULL); } if (drawn_area[0] != INT_MAX && drawn_area[1] != INT_MAX && drawn_area[2] != INT_MIN && drawn_area[3] != INT_MIN) { @@ -944,12 +959,13 @@ aacircle(PyObject *self, PyObject *args, PyObject *kwargs) } if (!pg_TwoIntsFromObj(posobj, &posx, &posy)) { - return RAISE(PyExc_TypeError, - "center argument must be a pair of numbers"); + return RAISERETURN(PyExc_TypeError, + "center argument must be a pair of numbers", NULL); } if (!pg_IntFromObj(radiusobj, &radius)) { - return RAISE(PyExc_TypeError, "radius argument must be a number"); + return RAISERETURN(PyExc_TypeError, "radius argument must be a number", + NULL); } surf = pgSurface_AsSurface(surfobj); @@ -963,12 +979,12 @@ aacircle(PyObject *self, PyObject *args, PyObject *kwargs) SDL_Rect surf_clip_rect; if (!PG_GetSurfaceClipRect(surf, &surf_clip_rect)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf); if (surf_format == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } CHECK_LOAD_COLOR(colorobj) @@ -989,7 +1005,7 @@ aacircle(PyObject *self, PyObject *args, PyObject *kwargs) } if (!pgSurface_Lock(surfobj)) { - return RAISE(PyExc_RuntimeError, "error locking surface"); + return RAISERETURN(PyExc_RuntimeError, "error locking surface", NULL); } if ((top_right == 0 && top_left == 0 && bottom_left == 0 && @@ -1033,7 +1049,8 @@ aacircle(PyObject *self, PyObject *args, PyObject *kwargs) } if (!pgSurface_Unlock(surfobj)) { - return RAISE(PyExc_RuntimeError, "error unlocking surface"); + return RAISERETURN(PyExc_RuntimeError, "error unlocking surface", + NULL); } if (drawn_area[0] != INT_MAX && drawn_area[1] != INT_MAX && drawn_area[2] != INT_MIN && drawn_area[3] != INT_MIN) { @@ -1091,21 +1108,23 @@ polygon(PyObject *self, PyObject *arg, PyObject *kwargs) SDL_Rect surf_clip_rect; if (!PG_GetSurfaceClipRect(surf, &surf_clip_rect)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } CHECK_LOAD_COLOR(colorobj) if (!PySequence_Check(points)) { - return RAISE(PyExc_TypeError, - "points argument must be a sequence of number pairs"); + return RAISERETURN( + PyExc_TypeError, + "points argument must be a sequence of number pairs", NULL); } length = PySequence_Length(points); if (length < 3) { - return RAISE(PyExc_ValueError, - "points argument must contain more than 2 points"); + return RAISERETURN(PyExc_ValueError, + "points argument must contain more than 2 points", + NULL); } // Allocate bytes for the xlist and ylist at once to reduce allocations. @@ -1114,8 +1133,8 @@ polygon(PyObject *self, PyObject *arg, PyObject *kwargs) int *ylist = points_buf + length; if (points_buf == NULL) { - return RAISE(PyExc_MemoryError, - "cannot allocate memory to draw polygon"); + return RAISERETURN(PyExc_MemoryError, + "cannot allocate memory to draw polygon", NULL); } for (loop = 0; loop < length; ++loop) { @@ -1129,7 +1148,8 @@ polygon(PyObject *self, PyObject *arg, PyObject *kwargs) if (!result) { PyMem_Free(points_buf); - return RAISE(PyExc_TypeError, "points must be number pairs"); + return RAISERETURN(PyExc_TypeError, "points must be number pairs", + NULL); } xlist[loop] = x; @@ -1138,7 +1158,7 @@ polygon(PyObject *self, PyObject *arg, PyObject *kwargs) if (!pgSurface_Lock(surfobj)) { PyMem_Free(points_buf); - return RAISE(PyExc_RuntimeError, "error locking surface"); + return RAISERETURN(PyExc_RuntimeError, "error locking surface", NULL); } if (length != 3) { @@ -1151,7 +1171,8 @@ polygon(PyObject *self, PyObject *arg, PyObject *kwargs) PyMem_Free(points_buf); if (!pgSurface_Unlock(surfobj)) { - return RAISE(PyExc_RuntimeError, "error unlocking surface"); + return RAISERETURN(PyExc_RuntimeError, "error unlocking surface", + NULL); } if (drawn_area[0] != INT_MAX && drawn_area[1] != INT_MAX && @@ -1198,7 +1219,7 @@ rect(PyObject *self, PyObject *args, PyObject *kwargs) } if (!(rect = pgRect_FromObject(rectobj, &temp))) { - return RAISE(PyExc_TypeError, "rect argument is invalid"); + return RAISERETURN(PyExc_TypeError, "rect argument is invalid", NULL); } surf = pgSurface_AsSurface(surfobj); @@ -1212,7 +1233,7 @@ rect(PyObject *self, PyObject *args, PyObject *kwargs) SDL_Rect surf_clip_rect; if (!PG_GetSurfaceClipRect(surf, &surf_clip_rect)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } CHECK_LOAD_COLOR(colorobj) @@ -1250,14 +1271,15 @@ rect(PyObject *self, PyObject *args, PyObject *kwargs) pgSurface_Unlock(surfobj); pgSurface_Unprep(surfobj); if (!success) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } return pgRect_New(&clipped); } else { if (!pgSurface_Lock(surfobj)) { - return RAISE(PyExc_RuntimeError, "error locking surface"); + return RAISERETURN(PyExc_RuntimeError, "error locking surface", + NULL); } /* Little bit to normalize the rect: this matters for the rounded @@ -1280,7 +1302,8 @@ rect(PyObject *self, PyObject *args, PyObject *kwargs) width, color, top_left_radius, top_right_radius, bottom_left_radius, bottom_right_radius, drawn_area); if (!pgSurface_Unlock(surfobj)) { - return RAISE(PyExc_RuntimeError, "error unlocking surface"); + return RAISERETURN(PyExc_RuntimeError, "error unlocking surface", + NULL); } } diff --git a/src_c/event.c b/src_c/event.c index e79d7f0de0..b78fe87af7 100644 --- a/src_c/event.c +++ b/src_c/event.c @@ -706,7 +706,7 @@ pg_event_filter(void *_, SDL_Event *event) } /* this doesn't work! This is called by SDL, not Python: if (SDL_PushEvent(&newdownevent) < 0) - return RAISE(pgExc_SDLError, SDL_GetError()), 0; + return RAISERETURN(pgExc_SDLError, SDL_GetError(), 0); */ } return PG_EventEnabled(_pg_pgevent_proxify(event->type)); @@ -768,7 +768,7 @@ pgEvent_AutoInit(PyObject *self, PyObject *_null) /* Create mutex only if it has not been created already */ pg_evfilter_mutex = SDL_CreateMutex(); if (!pg_evfilter_mutex) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } #endif @@ -1959,8 +1959,8 @@ _pg_eventtype_as_seq(PyObject *obj, Py_ssize_t *len) return Py_BuildValue("(O)", obj); } else { - return RAISE(PyExc_TypeError, - "event type must be numeric or a sequence"); + return RAISERETURN(PyExc_TypeError, + "event type must be numeric or a sequence", NULL); } } @@ -2289,9 +2289,10 @@ pg_event_get(PyObject *self, PyObject *args, PyObject *kwargs) } else { if (obj_exclude != NULL && obj_exclude != Py_None) { - return RAISE( + return RAISERETURN( pgExc_SDLError, - "Invalid combination of excluded and included event type"); + "Invalid combination of excluded and included event type", + NULL); } return _pg_get_seq_events(obj_evtype); } @@ -2320,7 +2321,7 @@ pg_event_peek(PyObject *self, PyObject *args, PyObject *kwargs) if (obj == NULL || obj == Py_None) { res = PG_PEEP_EVENT_ALL(&event, 1, SDL_PEEKEVENT); if (res < 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return PyBool_FromLong(res); } @@ -2341,7 +2342,7 @@ pg_event_peek(PyObject *self, PyObject *args, PyObject *kwargs) Py_DECREF(seq); if (res < 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_TRUE; } @@ -2351,7 +2352,7 @@ pg_event_peek(PyObject *self, PyObject *args, PyObject *kwargs) Py_DECREF(seq); if (res < 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_TRUE; } @@ -2371,7 +2372,8 @@ pg_event_post(PyObject *self, PyObject *obj) { VIDEO_INIT_CHECK(); if (!pgEvent_Check(obj)) { - return RAISE(PyExc_TypeError, "argument must be an Event object"); + return RAISERETURN(PyExc_TypeError, "argument must be an Event object", + NULL); } pgEventObject *e = (pgEventObject *)obj; @@ -2381,7 +2383,7 @@ pg_event_post(PyObject *self, PyObject *obj) case 1: Py_RETURN_TRUE; default: - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } @@ -2495,8 +2497,9 @@ pg_event_custom_type(PyObject *self, PyObject *_null) return PyLong_FromLong(_custom_event++); } else { - return RAISE(pgExc_SDLError, - "pygame.event.custom_type made too many event types."); + return RAISERETURN( + pgExc_SDLError, + "pygame.event.custom_type made too many event types.", NULL); } } diff --git a/src_c/font.c b/src_c/font.c index 99b6f5dce7..3d13c0fc76 100644 --- a/src_c/font.c +++ b/src_c/font.c @@ -38,7 +38,7 @@ #include "structmember.h" #define RAISE_TEXT_TYPE_ERROR() \ - RAISE(PyExc_TypeError, "text must be a unicode or bytes"); + RAISERETURN(PyExc_TypeError, "text must be a unicode or bytes", NULL); #define RAISE_FONT_QUIT_ERROR_RETURN(r) \ return RAISERETURN(pgExc_SDLError, \ @@ -46,8 +46,8 @@ r) #define RAISE_FONT_QUIT_ERROR() \ - RAISE(pgExc_SDLError, \ - "Invalid font (font module quit since font created)"); + RAISERETURN(pgExc_SDLError, \ + "Invalid font (font module quit since font created)", NULL); /* For filtering out UCS-4 and larger characters when Python is * built with Py_UNICODE_WIDE. @@ -142,7 +142,7 @@ fontmodule_init(PyObject *self, PyObject *_null) if (TTF_Init()) #endif { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } font_initialized = 1; } @@ -238,7 +238,8 @@ font_set_linesize(PyObject *self, PyObject *arg) TTF_Font *font = PyFont_AsFont(self); if (!PyLong_Check(arg)) { - return RAISE(PyExc_TypeError, "linesize must be an integer"); + return RAISERETURN(PyExc_TypeError, "linesize must be an integer", + NULL); } int linesize = PyLong_AsLong(arg); if (linesize == -1 && PyErr_Occurred()) { @@ -246,16 +247,17 @@ font_set_linesize(PyObject *self, PyObject *arg) } if (linesize < 0) { - return RAISE(PyExc_ValueError, "linesize must be >= 0"); + return RAISERETURN(PyExc_ValueError, "linesize must be >= 0", NULL); } TTF_SetFontLineSkip(font, linesize); Py_RETURN_NONE; #else - return RAISE( + return RAISERETURN( PyExc_NotImplementedError, - "TTF_SetFontLineSkip is not available in this version of SDL_ttf"); + "TTF_SetFontLineSkip is not available in this version of SDL_ttf", + NULL); #endif } @@ -516,9 +518,11 @@ font_getter_align(PyObject *self, void *closure) TTF_Font *font = PyFont_AsFont(self); return PyLong_FromLong(TTF_GetFontWrappedAlign(font)); #else - return RAISE(pgExc_SDLError, - "pygame.font not compiled with a new enough SDL_ttf version. " - "Needs SDL_ttf 2.20.0 or above."); + return RAISERETURN( + pgExc_SDLError, + "pygame.font not compiled with a new enough SDL_ttf version. " + "Needs SDL_ttf 2.20.0 or above.", + NULL); #endif } @@ -646,8 +650,8 @@ font_render(PyObject *self, PyObject *args, PyObject *kwds) } if (wraplength < 0) { - return RAISE(PyExc_ValueError, - "wraplength parameter must be positive"); + return RAISERETURN(PyExc_ValueError, + "wraplength parameter must be positive", NULL); } if (PyUnicode_Check(text)) { @@ -657,8 +661,8 @@ font_render(PyObject *self, PyObject *args, PyObject *kwds) return NULL; } if (strlen(astring) != (size_t)_size) { - return RAISE(PyExc_ValueError, - "A null character was found in the text"); + return RAISERETURN(PyExc_ValueError, + "A null character was found in the text", NULL); } } @@ -733,7 +737,7 @@ font_render(PyObject *self, PyObject *args, PyObject *kwds) } if (surf == NULL) { - return RAISE(pgExc_SDLError, TTF_GetError()); + return RAISERETURN(pgExc_SDLError, TTF_GetError(), NULL); } final = (PyObject *)pgSurface_New(surf); @@ -773,7 +777,7 @@ font_size(PyObject *self, PyObject *text) #endif Py_DECREF(bytes); if (ecode) { - return RAISE(pgExc_SDLError, TTF_GetError()); + return RAISERETURN(pgExc_SDLError, TTF_GetError(), NULL); } } else if (PyBytes_Check(text)) { @@ -784,7 +788,7 @@ font_size(PyObject *self, PyObject *text) if (TTF_SizeText(font, string, &w, &h)) #endif { - return RAISE(pgExc_SDLError, TTF_GetError()); + return RAISERETURN(pgExc_SDLError, TTF_GetError(), NULL); } } else { @@ -803,8 +807,8 @@ font_getter_point_size(PyFontObject *self, void *closure) #if SDL_TTF_VERSION_ATLEAST(2, 0, 18) return PyLong_FromLong(self->ptsize); #else - return RAISE(pgExc_SDLError, - "Incorrect SDL_TTF version (requires 2.0.18)"); + return RAISERETURN(pgExc_SDLError, + "Incorrect SDL_TTF version (requires 2.0.18)", NULL); #endif } @@ -857,8 +861,8 @@ font_get_ptsize(PyObject *self, PyObject *args) #if SDL_TTF_VERSION_ATLEAST(2, 0, 18) return PyLong_FromLong(((PyFontObject *)self)->ptsize); #else - return RAISE(pgExc_SDLError, - "Incorrect SDL_TTF version (requires 2.0.18)"); + return RAISERETURN(pgExc_SDLError, + "Incorrect SDL_TTF version (requires 2.0.18)", NULL); #endif } @@ -878,8 +882,9 @@ font_set_ptsize(PyObject *self, PyObject *arg) } if (val <= 0) { - return RAISE(PyExc_ValueError, - "point_size cannot be equal to, or less than 0"); + return RAISERETURN(PyExc_ValueError, + "point_size cannot be equal to, or less than 0", + NULL); } #if SDL_TTF_VERSION_ATLEAST(3, 0, 0) @@ -889,14 +894,14 @@ font_set_ptsize(PyObject *self, PyObject *arg) if (TTF_SetFontSize(font, val) == -1) #endif { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } ((PyFontObject *)self)->ptsize = val; Py_RETURN_NONE; #else - return RAISE(pgExc_SDLError, - "Incorrect SDL_TTF version (requires 2.0.18)"); + return RAISERETURN(pgExc_SDLError, + "Incorrect SDL_TTF version (requires 2.0.18)", NULL); #endif } @@ -1041,14 +1046,15 @@ font_set_script(PyObject *self, PyObject *arg) const char *script_code; if (!PyUnicode_Check(arg)) { - return RAISE(PyExc_TypeError, "script code must be a string"); + return RAISERETURN(PyExc_TypeError, "script code must be a string", + NULL); } script_code = PyUnicode_AsUTF8AndSize(arg, &size); if (size != 4) { - return RAISE(PyExc_ValueError, - "script code must be exactly 4 characters"); + return RAISERETURN(PyExc_ValueError, + "script code must be exactly 4 characters", NULL); } #if SDL_TTF_VERSION_ATLEAST(3, 0, 0) @@ -1058,12 +1064,14 @@ font_set_script(PyObject *self, PyObject *arg) if (TTF_SetFontScriptName(font, script_code) < 0) #endif { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } #else - return RAISE(pgExc_SDLError, - "pygame.font not compiled with a new enough SDL_ttf version. " - "Needs SDL_ttf 2.20.0 or above."); + return RAISERETURN( + pgExc_SDLError, + "pygame.font not compiled with a new enough SDL_ttf version. " + "Needs SDL_ttf 2.20.0 or above.", + NULL); #endif Py_RETURN_NONE; } @@ -1085,8 +1093,9 @@ font_set_direction(PyObject *self, PyObject *arg, PyObject *kwarg) } if (direction < 0 || direction > 3) { - return RAISE(PyExc_ValueError, - "invalid input parameter for Font.set_direction"); + return RAISERETURN(PyExc_ValueError, + "invalid input parameter for Font.set_direction", + NULL); } TTF_Direction dir; @@ -1129,10 +1138,12 @@ font_set_direction(PyObject *self, PyObject *arg, PyObject *kwarg) default: { // should NEVER reach this point - return RAISE(PyExc_RuntimeError, - "Something went wrong in Font.set_direction. Please " - "report this" - " to https://github.com/pygame-community/pygame-ce"); + return RAISERETURN( + PyExc_RuntimeError, + "Something went wrong in Font.set_direction. Please " + "report this" + " to https://github.com/pygame-community/pygame-ce", + NULL); } } #if SDL_TTF_VERSION_ATLEAST(3, 0, 0) @@ -1141,13 +1152,15 @@ font_set_direction(PyObject *self, PyObject *arg, PyObject *kwarg) if (TTF_SetFontDirection(font, dir)) #endif { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } #else - return RAISE(pgExc_SDLError, - "pygame.font not compiled with a new enough SDL_ttf version. " - "Needs SDL_ttf 2.20.0 or above."); + return RAISERETURN( + pgExc_SDLError, + "pygame.font not compiled with a new enough SDL_ttf version. " + "Needs SDL_ttf 2.20.0 or above.", + NULL); #endif Py_RETURN_NONE; } @@ -1396,7 +1409,7 @@ PyFont_New(TTF_Font *font) PyFontObject *fontobj; if (!font) { - return RAISE(PyExc_RuntimeError, "unable to load font."); + return RAISERETURN(PyExc_RuntimeError, "unable to load font.", NULL); } fontobj = (PyFontObject *)PyFont_Type.tp_new(&PyFont_Type, NULL, NULL); diff --git a/src_c/font.h b/src_c/font.h index 358a66abf8..518cd2a58a 100644 --- a/src_c/font.h +++ b/src_c/font.h @@ -11,9 +11,11 @@ #include #endif /* test font initialization */ -#define FONT_INIT_CHECK() \ - if (!(*(int *)PyFONT_C_API[2])) \ - return RAISE(pgExc_SDLError, "font system not initialized") +#define FONT_INIT_CHECK() \ + if (!(*(int *)PyFONT_C_API[2])) { \ + return RAISERETURN(pgExc_SDLError, "font system not initialized", \ + -1); \ + } #include "include/pygame_font.h" diff --git a/src_c/gfxdraw.c b/src_c/gfxdraw.c index 09cbb050b8..ac11d733bf 100644 --- a/src_c/gfxdraw.c +++ b/src_c/gfxdraw.c @@ -166,7 +166,7 @@ _gfx_pixelcolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; @@ -174,7 +174,7 @@ _gfx_pixelcolor(PyObject *self, PyObject *args) if (pixelRGBA(pgSurface_AsSurface(surface), x, y, rgba[0], rgba[1], rgba[2], rgba[3]) == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -194,7 +194,7 @@ _gfx_hlinecolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; @@ -202,7 +202,7 @@ _gfx_hlinecolor(PyObject *self, PyObject *args) if (hlineRGBA(pgSurface_AsSurface(surface), x1, x2, y, rgba[0], rgba[1], rgba[2], rgba[3]) == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -222,7 +222,7 @@ _gfx_vlinecolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; @@ -230,7 +230,7 @@ _gfx_vlinecolor(PyObject *self, PyObject *args) if (vlineRGBA(pgSurface_AsSurface(surface), x, _y1, y2, rgba[0], rgba[1], rgba[2], rgba[3]) == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -251,11 +251,12 @@ _gfx_rectanglecolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } sdlrect = pgRect_FromObject(rect, &temprect); if (sdlrect == NULL) { - return RAISE(PyExc_TypeError, "invalid rect style argument"); + return RAISERETURN(PyExc_TypeError, "invalid rect style argument", + NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { @@ -269,7 +270,7 @@ _gfx_rectanglecolor(PyObject *self, PyObject *args) if (rectangleRGBA(pgSurface_AsSurface(surface), x1, _y1, x2, y2, rgba[0], rgba[1], rgba[2], rgba[3]) == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -290,11 +291,12 @@ _gfx_boxcolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } sdlrect = pgRect_FromObject(rect, &temprect); if (sdlrect == NULL) { - return RAISE(PyExc_TypeError, "invalid rect style argument"); + return RAISERETURN(PyExc_TypeError, "invalid rect style argument", + NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; @@ -307,7 +309,7 @@ _gfx_boxcolor(PyObject *self, PyObject *args) if (boxRGBA(pgSurface_AsSurface(surface), x1, _y1, x2, y2, rgba[0], rgba[1], rgba[2], rgba[3]) == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -327,7 +329,7 @@ _gfx_linecolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; @@ -335,7 +337,7 @@ _gfx_linecolor(PyObject *self, PyObject *args) if (lineRGBA(pgSurface_AsSurface(surface), x1, _y1, x2, y2, rgba[0], rgba[1], rgba[2], rgba[3]) == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -355,7 +357,7 @@ _gfx_circlecolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; @@ -363,7 +365,7 @@ _gfx_circlecolor(PyObject *self, PyObject *args) if (circleRGBA(pgSurface_AsSurface(surface), x, y, r, rgba[0], rgba[1], rgba[2], rgba[3]) == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -383,7 +385,7 @@ _gfx_arccolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; @@ -391,7 +393,7 @@ _gfx_arccolor(PyObject *self, PyObject *args) if (arcRGBA(pgSurface_AsSurface(surface), x, y, r, start, end, rgba[0], rgba[1], rgba[2], rgba[3]) == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -411,7 +413,7 @@ _gfx_aacirclecolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; @@ -419,7 +421,7 @@ _gfx_aacirclecolor(PyObject *self, PyObject *args) if (aacircleRGBA(pgSurface_AsSurface(surface), x, y, r, rgba[0], rgba[1], rgba[2], rgba[3]) == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -439,7 +441,7 @@ _gfx_filledcirclecolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; @@ -447,7 +449,7 @@ _gfx_filledcirclecolor(PyObject *self, PyObject *args) if (filledCircleRGBA(pgSurface_AsSurface(surface), x, y, r, rgba[0], rgba[1], rgba[2], rgba[3]) == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -467,7 +469,7 @@ _gfx_ellipsecolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; @@ -475,7 +477,7 @@ _gfx_ellipsecolor(PyObject *self, PyObject *args) if (ellipseRGBA(pgSurface_AsSurface(surface), x, y, rx, ry, rgba[0], rgba[1], rgba[2], rgba[3]) == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -495,7 +497,7 @@ _gfx_aaellipsecolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; @@ -503,7 +505,7 @@ _gfx_aaellipsecolor(PyObject *self, PyObject *args) if (aaellipseRGBA(pgSurface_AsSurface(surface), x, y, rx, ry, rgba[0], rgba[1], rgba[2], rgba[3]) == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -523,7 +525,7 @@ _gfx_filledellipsecolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; @@ -531,7 +533,7 @@ _gfx_filledellipsecolor(PyObject *self, PyObject *args) if (filledEllipseRGBA(pgSurface_AsSurface(surface), x, y, rx, ry, rgba[0], rgba[1], rgba[2], rgba[3]) == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -551,7 +553,7 @@ _gfx_piecolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; @@ -559,7 +561,7 @@ _gfx_piecolor(PyObject *self, PyObject *args) if (pieRGBA(pgSurface_AsSurface(surface), x, y, r, start, end, rgba[0], rgba[1], rgba[2], rgba[3]) == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -579,7 +581,7 @@ _gfx_trigoncolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; @@ -587,7 +589,7 @@ _gfx_trigoncolor(PyObject *self, PyObject *args) if (trigonRGBA(pgSurface_AsSurface(surface), x1, _y1, x2, y2, x3, y3, rgba[0], rgba[1], rgba[2], rgba[3]) == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -607,7 +609,7 @@ _gfx_aatrigoncolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; @@ -615,7 +617,7 @@ _gfx_aatrigoncolor(PyObject *self, PyObject *args) if (aatrigonRGBA(pgSurface_AsSurface(surface), x1, _y1, x2, y2, x3, y3, rgba[0], rgba[1], rgba[2], rgba[3]) == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -635,7 +637,7 @@ _gfx_filledtrigoncolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; @@ -643,7 +645,7 @@ _gfx_filledtrigoncolor(PyObject *self, PyObject *args) if (filledTrigonRGBA(pgSurface_AsSurface(surface), x1, _y1, x2, y2, x3, y3, rgba[0], rgba[1], rgba[2], rgba[3]) == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -664,19 +666,19 @@ _gfx_polygoncolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; } if (!PySequence_Check(points)) { - return RAISE(PyExc_TypeError, "points must be a sequence"); + return RAISERETURN(PyExc_TypeError, "points must be a sequence", NULL); } count = PySequence_Size(points); if (count < 3) { - return RAISE(PyExc_ValueError, - "points must contain more than 2 points"); + return RAISERETURN(PyExc_ValueError, + "points must contain more than 2 points", NULL); } vx = PyMem_New(Sint16, (size_t)count); @@ -719,7 +721,7 @@ _gfx_polygoncolor(PyObject *self, PyObject *args) PyMem_Free(vy); if (ret == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -740,19 +742,19 @@ _gfx_aapolygoncolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; } if (!PySequence_Check(points)) { - return RAISE(PyExc_TypeError, "points must be a sequence"); + return RAISERETURN(PyExc_TypeError, "points must be a sequence", NULL); } count = PySequence_Size(points); if (count < 3) { - return RAISE(PyExc_ValueError, - "points must contain more than 2 points"); + return RAISERETURN(PyExc_ValueError, + "points must contain more than 2 points", NULL); } vx = PyMem_New(Sint16, (size_t)count); @@ -795,7 +797,7 @@ _gfx_aapolygoncolor(PyObject *self, PyObject *args) PyMem_Free(vy); if (ret == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -817,19 +819,19 @@ _gfx_filledpolygoncolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; } if (!PySequence_Check(points)) { - return RAISE(PyExc_TypeError, "points must be a sequence"); + return RAISERETURN(PyExc_TypeError, "points must be a sequence", NULL); } count = PySequence_Size(points); if (count < 3) { - return RAISE(PyExc_ValueError, - "points must contain more than 2 points"); + return RAISERETURN(PyExc_ValueError, + "points must contain more than 2 points", NULL); } vx = PyMem_New(Sint16, (size_t)count); @@ -872,7 +874,7 @@ _gfx_filledpolygoncolor(PyObject *self, PyObject *args) PyMem_Free(vy); if (ret == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -894,27 +896,28 @@ _gfx_texturedpolygon(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } s_surface = pgSurface_AsSurface(surface); if (!pgSurface_Check(texture)) { - return RAISE(PyExc_TypeError, "texture must be a Surface"); + return RAISERETURN(PyExc_TypeError, "texture must be a Surface", NULL); } s_texture = pgSurface_AsSurface(texture); if (!PySequence_Check(points)) { - return RAISE(PyExc_TypeError, "points must be a sequence"); + return RAISERETURN(PyExc_TypeError, "points must be a sequence", NULL); } if (PG_SURF_BytesPerPixel(s_surface) == 1 && (s_texture->format->Amask || s_texture->flags & SDL_SRCALPHA)) { - return RAISE(PyExc_ValueError, - "Per-byte alpha texture unsupported " - "for 8 bit surfaces"); + return RAISERETURN(PyExc_ValueError, + "Per-byte alpha texture unsupported " + "for 8 bit surfaces", + NULL); } count = PySequence_Size(points); if (count < 3) { - return RAISE(PyExc_ValueError, - "points must contain more than 2 points"); + return RAISERETURN(PyExc_ValueError, + "points must contain more than 2 points", NULL); } vx = PyMem_New(Sint16, (size_t)count); @@ -956,7 +959,7 @@ _gfx_texturedpolygon(PyObject *self, PyObject *args) PyMem_Free(vy); if (ret == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -978,24 +981,24 @@ _gfx_beziercolor(PyObject *self, PyObject *args) } if (!pgSurface_Check(surface)) { - return RAISE(PyExc_TypeError, "surface must be a Surface"); + return RAISERETURN(PyExc_TypeError, "surface must be a Surface", NULL); } if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_SIMPLE)) { return NULL; } if (!PySequence_Check(points)) { - return RAISE(PyExc_TypeError, "points must be a sequence"); + return RAISERETURN(PyExc_TypeError, "points must be a sequence", NULL); } count = PySequence_Size(points); if (count < 3) { - return RAISE(PyExc_ValueError, - "points must contain more than 2 points"); + return RAISERETURN(PyExc_ValueError, + "points must contain more than 2 points", NULL); } if (steps < 2) { - return RAISE(PyExc_ValueError, - "steps parameter must be greater than 1"); + return RAISERETURN(PyExc_ValueError, + "steps parameter must be greater than 1", NULL); } vx = PyMem_New(Sint16, (size_t)count); @@ -1007,7 +1010,8 @@ _gfx_beziercolor(PyObject *self, PyObject *args) if (vy) { PyMem_Free(vy); } - return RAISE(PyExc_MemoryError, "memory allocation failed"); + return RAISERETURN(PyExc_MemoryError, "memory allocation failed", + NULL); } for (i = 0; i < count; i++) { @@ -1038,7 +1042,7 @@ _gfx_beziercolor(PyObject *self, PyObject *args) PyMem_Free(vy); if (ret == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } diff --git a/src_c/image.c b/src_c/image.c index e4f9cf70f9..d6cb7d6477 100644 --- a/src_c/image.c +++ b/src_c/image.c @@ -91,7 +91,7 @@ image_load_basic(PyObject *self, PyObject *obj) Py_END_ALLOW_THREADS; if (surf == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } final = (PyObject *)pgSurface_New(surf); @@ -105,8 +105,9 @@ static PyObject * image_load_extended(PyObject *self, PyObject *arg, PyObject *kwarg) { if (extloadobj == NULL) { - return RAISE(PyExc_NotImplementedError, - "loading images of extended format is not available"); + return RAISERETURN( + PyExc_NotImplementedError, + "loading images of extended format is not available", NULL); } else { return PyObject_Call(extloadobj, arg, kwarg); @@ -142,8 +143,9 @@ static PyObject * image_save_extended(PyObject *self, PyObject *arg, PyObject *kwarg) { if (extsaveobj == NULL) { - return RAISE(PyExc_NotImplementedError, - "saving images of extended format is not available"); + return RAISERETURN(PyExc_NotImplementedError, + "saving images of extended format is not available", + NULL); } else { return PyObject_Call(extsaveobj, arg, kwarg); @@ -236,11 +238,11 @@ image_save(PyObject *self, PyObject *arg, PyObject *kwarg) } if (result == -1) { /* SDL error: translate to Python error */ - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (result == 1) { /* Should never get here */ - return RAISE(pgExc_SDLError, "Unrecognized image type"); + return RAISERETURN(pgExc_SDLError, "Unrecognized image type", NULL); } Py_RETURN_NONE; @@ -555,7 +557,7 @@ image_tobytes(PyObject *self, PyObject *arg, PyObject *kwarg) const SDL_PixelFormatDetails *format_details = SDL_GetPixelFormatDetails(surf->format); if (!format_details) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_Palette *surf_palette = SDL_GetSurfacePalette(surf); Rloss = format_details->Rbits; @@ -581,9 +583,9 @@ image_tobytes(PyObject *self, PyObject *arg, PyObject *kwarg) if (!strcmp(format, "P")) { if (PG_SURF_BytesPerPixel(surf) != 1) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "Can only create \"P\" format data with 8bit Surfaces"); + "Can only create \"P\" format data with 8bit Surfaces", NULL); } byte_width = surf->w; } @@ -603,14 +605,16 @@ image_tobytes(PyObject *self, PyObject *arg, PyObject *kwarg) else if (!strcmp(format, "RGBA_PREMULT") || !strcmp(format, "ARGB_PREMULT")) { if (PG_SURF_BytesPerPixel(surf) == 1 || Amask == 0) { - return RAISE(PyExc_ValueError, - "Can only create pre-multiplied alpha bytes if " - "the surface has per-pixel alpha"); + return RAISERETURN(PyExc_ValueError, + "Can only create pre-multiplied alpha bytes if " + "the surface has per-pixel alpha", + NULL); } byte_width = surf->w * 4; } else { - return RAISE(PyExc_ValueError, "Unrecognized type of format"); + return RAISERETURN(PyExc_ValueError, "Unrecognized type of format", + NULL); } if (pitch == -1) { @@ -618,9 +622,10 @@ image_tobytes(PyObject *self, PyObject *arg, PyObject *kwarg) padding = 0; } else if (pitch < byte_width) { - return RAISE(PyExc_ValueError, - "Pitch must be greater than or equal to the width " - "as per the format"); + return RAISERETURN(PyExc_ValueError, + "Pitch must be greater than or equal to the width " + "as per the format", + NULL); } else { padding = pitch - byte_width; @@ -1176,8 +1181,8 @@ image_frombytes(PyObject *self, PyObject *arg, PyObject *kwds) } if (w < 1 || h < 1) { - return RAISE(PyExc_ValueError, - "Resolution must be nonzero positive values"); + return RAISERETURN(PyExc_ValueError, + "Resolution must be nonzero positive values", NULL); } PyBytes_AsStringAndSize(bytes, &data, &len); @@ -1187,20 +1192,23 @@ image_frombytes(PyObject *self, PyObject *arg, PyObject *kwds) pitch = w; } else if (pitch < w) { - return RAISE(PyExc_ValueError, - "Pitch must be greater than or equal to the width " - "as per the format"); + return RAISERETURN( + PyExc_ValueError, + "Pitch must be greater than or equal to the width " + "as per the format", + NULL); } if (len != (Py_ssize_t)pitch * h) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "Bytes length does not equal format and resolution size"); + "Bytes length does not equal format and resolution size", + NULL); } surf = PG_CreateSurface(w, h, SDL_PIXELFORMAT_INDEX8); if (!surf) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_LockSurface(surf); for (looph = 0; looph < h; ++looph) { @@ -1214,15 +1222,18 @@ image_frombytes(PyObject *self, PyObject *arg, PyObject *kwds) pitch = w * 3; } else if (pitch < w * 3) { - return RAISE(PyExc_ValueError, - "Pitch must be greater than or equal to the width * " - "3 as per the format"); + return RAISERETURN( + PyExc_ValueError, + "Pitch must be greater than or equal to the width * " + "3 as per the format", + NULL); } if (len != (Py_ssize_t)pitch * h) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "Bytes length does not equal format and resolution size"); + "Bytes length does not equal format and resolution size", + NULL); } #if SDL_BYTEORDER == SDL_LIL_ENDIAN @@ -1231,7 +1242,7 @@ image_frombytes(PyObject *self, PyObject *arg, PyObject *kwds) surf = PG_CreateSurface(w, h, SDL_PIXELFORMAT_RGB24); #endif if (!surf) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_LockSurface(surf); for (looph = 0; looph < h; ++looph) { @@ -1260,22 +1271,25 @@ image_frombytes(PyObject *self, PyObject *arg, PyObject *kwds) pitch = w * 4; } else if (pitch < w * 4) { - return RAISE(PyExc_ValueError, - "Pitch must be greater than or equal to the width * " - "4 as per the format"); + return RAISERETURN( + PyExc_ValueError, + "Pitch must be greater than or equal to the width * " + "4 as per the format", + NULL); } int alphamult = !strcmp(format, "RGBA"); if (len != (Py_ssize_t)pitch * h) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "Bytes length does not equal format and resolution size"); + "Bytes length does not equal format and resolution size", + NULL); } surf = PG_CreateSurface( w, h, (alphamult ? SDL_PIXELFORMAT_RGBA32 : PG_PIXELFORMAT_RGBX32)); if (!surf) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_LockSurface(surf); for (looph = 0; looph < h; ++looph) { @@ -1291,19 +1305,22 @@ image_frombytes(PyObject *self, PyObject *arg, PyObject *kwds) pitch = w * 4; } else if (pitch < w * 4) { - return RAISE(PyExc_ValueError, - "Pitch must be greater than or equal to the width * " - "4 as per the format"); + return RAISERETURN( + PyExc_ValueError, + "Pitch must be greater than or equal to the width * " + "4 as per the format", + NULL); } if (len != (Py_ssize_t)pitch * h) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "Bytes length does not equal format and resolution size"); + "Bytes length does not equal format and resolution size", + NULL); } surf = PG_CreateSurface(w, h, SDL_PIXELFORMAT_BGRA32); if (!surf) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_LockSurface(surf); for (looph = 0; looph < h; ++looph) { @@ -1319,19 +1336,22 @@ image_frombytes(PyObject *self, PyObject *arg, PyObject *kwds) pitch = w * 4; } else if (pitch < w * 4) { - return RAISE(PyExc_ValueError, - "Pitch must be greater than or equal to the width * " - "4 as per the format"); + return RAISERETURN( + PyExc_ValueError, + "Pitch must be greater than or equal to the width * " + "4 as per the format", + NULL); } if (len != (Py_ssize_t)pitch * h) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "Bytes length does not equal format and resolution size"); + "Bytes length does not equal format and resolution size", + NULL); } surf = PG_CreateSurface(w, h, SDL_PIXELFORMAT_ARGB32); if (!surf) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_LockSurface(surf); for (looph = 0; looph < h; ++looph) { @@ -1347,19 +1367,22 @@ image_frombytes(PyObject *self, PyObject *arg, PyObject *kwds) pitch = w * 4; } else if (pitch < w * 4) { - return RAISE(PyExc_ValueError, - "Pitch must be greater than or equal to the width * " - "4 as per the format"); + return RAISERETURN( + PyExc_ValueError, + "Pitch must be greater than or equal to the width * " + "4 as per the format", + NULL); } if (len != (Py_ssize_t)pitch * h) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "Bytes length does not equal format and resolution size"); + "Bytes length does not equal format and resolution size", + NULL); } surf = PG_CreateSurface(w, h, SDL_PIXELFORMAT_ABGR32); if (!surf) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_LockSurface(surf); for (looph = 0; looph < h; ++looph) { @@ -1371,7 +1394,8 @@ image_frombytes(PyObject *self, PyObject *arg, PyObject *kwds) SDL_UnlockSurface(surf); } else { - return RAISE(PyExc_ValueError, "Unrecognized type of format"); + return RAISERETURN(PyExc_ValueError, "Unrecognized type of format", + NULL); } return (PyObject *)pgSurface_New(surf); @@ -1452,8 +1476,8 @@ image_frombuffer(PyObject *self, PyObject *arg, PyObject *kwds) } if (w < 1 || h < 1) { - return RAISE(PyExc_ValueError, - "Resolution must be nonzero positive values"); + return RAISERETURN(PyExc_ValueError, + "Resolution must be nonzero positive values", NULL); } /* breaking constness here, we should really not change this string */ @@ -1466,15 +1490,18 @@ image_frombuffer(PyObject *self, PyObject *arg, PyObject *kwds) pitch = w; } else if (pitch < w) { - return RAISE(PyExc_ValueError, - "Pitch must be greater than or equal to the width " - "as per the format"); + return RAISERETURN( + PyExc_ValueError, + "Pitch must be greater than or equal to the width " + "as per the format", + NULL); } if (len != (Py_ssize_t)pitch * h) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "Buffer length does not equal format and resolution size"); + "Buffer length does not equal format and resolution size", + NULL); } surf = PG_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_INDEX8, data, pitch); @@ -1484,15 +1511,18 @@ image_frombuffer(PyObject *self, PyObject *arg, PyObject *kwds) pitch = w * 3; } else if (pitch < w * 3) { - return RAISE(PyExc_ValueError, - "Pitch must be greater than or equal to the width * " - "3 as per the format"); + return RAISERETURN( + PyExc_ValueError, + "Pitch must be greater than or equal to the width * " + "3 as per the format", + NULL); } if (len != (Py_ssize_t)pitch * h) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "Buffer length does not equal format and resolution size"); + "Buffer length does not equal format and resolution size", + NULL); } surf = PG_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_RGB24, data, pitch); } @@ -1501,15 +1531,18 @@ image_frombuffer(PyObject *self, PyObject *arg, PyObject *kwds) pitch = w * 3; } else if (pitch < w * 3) { - return RAISE(PyExc_ValueError, - "Pitch must be greater than or equal to the width * " - "3 as per the format"); + return RAISERETURN( + PyExc_ValueError, + "Pitch must be greater than or equal to the width * " + "3 as per the format", + NULL); } if (len != (Py_ssize_t)pitch * h) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "Buffer length does not equal format and resolution size"); + "Buffer length does not equal format and resolution size", + NULL); } surf = PG_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_BGR24, data, pitch); } @@ -1518,15 +1551,18 @@ image_frombuffer(PyObject *self, PyObject *arg, PyObject *kwds) pitch = w * 4; } else if (pitch < w * 4) { - return RAISE(PyExc_ValueError, - "Pitch must be greater than or equal to the width * " - "4 as per the format"); + return RAISERETURN( + PyExc_ValueError, + "Pitch must be greater than or equal to the width * " + "4 as per the format", + NULL); } if (len != (Py_ssize_t)pitch * h) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "Buffer length does not equal format and resolution size"); + "Buffer length does not equal format and resolution size", + NULL); } surf = PG_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_BGRA32, data, pitch); } @@ -1535,16 +1571,19 @@ image_frombuffer(PyObject *self, PyObject *arg, PyObject *kwds) pitch = w * 4; } else if (pitch < w * 4) { - return RAISE(PyExc_ValueError, - "Pitch must be greater than or equal to the width * " - "4 as per the format"); + return RAISERETURN( + PyExc_ValueError, + "Pitch must be greater than or equal to the width * " + "4 as per the format", + NULL); } int alphamult = !strcmp(format, "RGBA"); if (len != (Py_ssize_t)pitch * h) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "Buffer length does not equal format and resolution size"); + "Buffer length does not equal format and resolution size", + NULL); } surf = PG_CreateSurfaceFrom( w, h, (alphamult ? SDL_PIXELFORMAT_RGBA32 : PG_PIXELFORMAT_RGBX32), @@ -1555,24 +1594,28 @@ image_frombuffer(PyObject *self, PyObject *arg, PyObject *kwds) pitch = w * 4; } else if (pitch < w * 4) { - return RAISE(PyExc_ValueError, - "Pitch must be greater than or equal to the width * " - "4 as per the format"); + return RAISERETURN( + PyExc_ValueError, + "Pitch must be greater than or equal to the width * " + "4 as per the format", + NULL); } if (len != (Py_ssize_t)pitch * h) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "Buffer length does not equal format and resolution size"); + "Buffer length does not equal format and resolution size", + NULL); } surf = PG_CreateSurfaceFrom(w, h, SDL_PIXELFORMAT_ARGB32, data, pitch); } else { - return RAISE(PyExc_ValueError, "Unrecognized type of format"); + return RAISERETURN(PyExc_ValueError, "Unrecognized type of format", + NULL); } if (!surf) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } surfobj = (pgSurfaceObject *)pgSurface_New(surf); Py_INCREF(buffer); @@ -1882,8 +1925,9 @@ image_load_sized_svg(PyObject *self, PyObject *args, PyObject *kwargs) return PyObject_Call(ext_load_sized_svg, args, kwargs); } - return RAISE(PyExc_NotImplementedError, - "Support for sized svg image loading was not compiled in."); + return RAISERETURN( + PyExc_NotImplementedError, + "Support for sized svg image loading was not compiled in.", NULL); } static PyObject * @@ -1893,8 +1937,9 @@ image_load_animation(PyObject *self, PyObject *args, PyObject *kwargs) return PyObject_Call(ext_load_animation, args, kwargs); } - return RAISE(PyExc_NotImplementedError, - "Support for animation loading was not compiled in."); + return RAISERETURN(PyExc_NotImplementedError, + "Support for animation loading was not compiled in.", + NULL); } static PyMethodDef _image_methods[] = { diff --git a/src_c/imageext.c b/src_c/imageext.c index 9b68620009..cf0b836aa2 100644 --- a/src_c/imageext.c +++ b/src_c/imageext.c @@ -145,7 +145,7 @@ image_load_ext(PyObject *self, PyObject *arg, PyObject *kwarg) } if (surf == NULL) { - return RAISE(pgExc_SDLError, IMG_GetError()); + return RAISERETURN(pgExc_SDLError, IMG_GetError(), NULL); } final = (PyObject *)pgSurface_New(surf); @@ -170,12 +170,12 @@ imageext_load_sized_svg(PyObject *self, PyObject *arg, PyObject *kwargs) } if (!pg_TwoIntsFromObj(size, &width, &height)) { - return RAISE(PyExc_TypeError, "size must be two numbers"); + return RAISERETURN(PyExc_TypeError, "size must be two numbers", NULL); } if (width <= 0 || height <= 0) { - return RAISE(PyExc_ValueError, - "both components of size must be positive"); + return RAISERETURN(PyExc_ValueError, + "both components of size must be positive", NULL); } rw = pgRWops_FromObject(obj, NULL); @@ -192,7 +192,7 @@ imageext_load_sized_svg(PyObject *self, PyObject *arg, PyObject *kwargs) SDL_RWclose(rw); Py_END_ALLOW_THREADS; if (surf == NULL) { - return RAISE(pgExc_SDLError, IMG_GetError()); + return RAISERETURN(pgExc_SDLError, IMG_GetError(), NULL); } final = (PyObject *)pgSurface_New(surf); if (final == NULL) { @@ -200,9 +200,10 @@ imageext_load_sized_svg(PyObject *self, PyObject *arg, PyObject *kwargs) } return final; #else /* ~SDL_IMAGE_VERSION_ATLEAST(2, 6, 0) */ - return RAISE( + return RAISERETURN( pgExc_SDLError, - "pygame must be compiled with SDL_image 2.6.0+ to use this function"); + "pygame must be compiled with SDL_image 2.6.0+ to use this function", + NULL); #endif /* ~SDL_IMAGE_VERSION_ATLEAST(2, 6, 0) */ } @@ -245,7 +246,7 @@ imageext_load_animation(PyObject *self, PyObject *arg, PyObject *kwargs) } if (surfs == NULL) { - return RAISE(pgExc_SDLError, IMG_GetError()); + return RAISERETURN(pgExc_SDLError, IMG_GetError(), NULL); } ret = PyList_New(surfs->count); @@ -279,9 +280,10 @@ imageext_load_animation(PyObject *self, PyObject *arg, PyObject *kwargs) IMG_FreeAnimation(surfs); return NULL; #else /* ~SDL_IMAGE_VERSION_ATLEAST(2, 6, 0) */ - return RAISE( + return RAISERETURN( pgExc_SDLError, - "pygame must be compiled with SDL_image 2.6.0+ to use this function"); + "pygame must be compiled with SDL_image 2.6.0+ to use this function", + NULL); #endif /* ~SDL_IMAGE_VERSION_ATLEAST(2, 6, 0) */ } @@ -367,10 +369,10 @@ image_save_ext(PyObject *self, PyObject *arg, PyObject *kwarg) } if (result == -1) { /* SDL error: translate to Python error */ - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (result == 1) { - return RAISE(pgExc_SDLError, "Unrecognized image type"); + return RAISERETURN(pgExc_SDLError, "Unrecognized image type", NULL); } Py_RETURN_NONE; @@ -471,7 +473,7 @@ MODINIT_DEFINE(imageext) #ifdef WITH_THREAD _pg_img_mutex = SDL_CreateMutex(); if (!_pg_img_mutex) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } #endif */ diff --git a/src_c/include/_pygame.h b/src_c/include/_pygame.h index c3bcc5ec55..6907f7dd6a 100644 --- a/src_c/include/_pygame.h +++ b/src_c/include/_pygame.h @@ -624,11 +624,12 @@ PYGAMEAPI_EXTERN_SLOTS(geometry); * functions in Python 3. */ -#define SURF_INIT_CHECK(surf) \ - { \ - if (!surf) { \ - return RAISE(pgExc_SDLError, "Surface is not initialized"); \ - } \ +#define SURF_INIT_CHECK(surf) \ + { \ + if (!surf) { \ + return RAISERETURN(pgExc_SDLError, "Surface is not initialized", \ + NULL); \ + } \ } static PG_INLINE PyObject * @@ -729,7 +730,8 @@ static PG_INLINE PyObject * pg_PointList_FromArrayDouble(double const *array, int arr_length) { if (arr_length % 2) { - return RAISE(PyExc_ValueError, "array length must be even"); + return RAISERETURN(PyExc_ValueError, "array length must be even", + NULL); } int num_points = arr_length / 2; diff --git a/src_c/joystick.c b/src_c/joystick.c index 518e25756e..5f04e7a8a8 100644 --- a/src_c/joystick.c +++ b/src_c/joystick.c @@ -40,7 +40,7 @@ init(PyObject *self, PyObject *_null) { if (!SDL_WasInit(SDL_INIT_JOYSTICK)) { if (SDL_InitSubSystem(SDL_INIT_JOYSTICK)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } PG_SetJoystickEventsEnabled(SDL_TRUE); } @@ -117,7 +117,7 @@ get_count(PyObject *self, PyObject *_null) int ret; SDL_JoystickID *joysticks = SDL_GetJoysticks(&ret); if (!joysticks) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_free(joysticks); return PyLong_FromLong(ret); @@ -143,7 +143,7 @@ joy_init(PyObject *self, PyObject *_null) if (!jstick->joy) { jstick->joy = SDL_JoystickOpen(jstick->id); if (!jstick->joy) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } @@ -193,7 +193,7 @@ joy_get_instance_id(PyObject *self, PyObject *_null) JOYSTICK_INIT_CHECK(); if (!joy) { - return RAISE(pgExc_SDLError, "Joystick not initialized"); + return RAISERETURN(pgExc_SDLError, "Joystick not initialized", NULL); } return PyLong_FromLong(SDL_JoystickInstanceID(joy)); @@ -212,7 +212,8 @@ joy_get_guid(PyObject *self, PyObject *_null) } else { #if SDL_VERSION_ATLEAST(3, 0, 0) - return RAISE(pgExc_SDLError, "Invalid/closed joystick object"); + return RAISERETURN(pgExc_SDLError, "Invalid/closed joystick object", + NULL); #else guid = SDL_JoystickGetDeviceGUID(pgJoystick_AsID(self)); #endif @@ -284,7 +285,7 @@ joy_get_power_level(PyObject *self, PyObject *_null) JOYSTICK_INIT_CHECK(); if (!joy) { - return RAISE(pgExc_SDLError, "Joystick not initialized"); + return RAISERETURN(pgExc_SDLError, "Joystick not initialized", NULL); } leveltext = _pg_powerlevel_string(joy); @@ -313,7 +314,7 @@ joy_rumble(pgJoystickObject *self, PyObject *args, PyObject *kwargs) JOYSTICK_INIT_CHECK(); if (!joy) { - return RAISE(pgExc_SDLError, "Joystick not initialized"); + return RAISERETURN(pgExc_SDLError, "Joystick not initialized", NULL); } if (lowf < 0) { @@ -363,7 +364,7 @@ joy_get_numaxes(PyObject *self, PyObject *_null) SDL_Joystick *joy = pgJoystick_AsSDL(self); JOYSTICK_INIT_CHECK(); if (!joy) { - return RAISE(pgExc_SDLError, "Joystick not initialized"); + return RAISERETURN(pgExc_SDLError, "Joystick not initialized", NULL); } return PyLong_FromLong(SDL_JoystickNumAxes(joy)); @@ -381,10 +382,10 @@ joy_get_axis(PyObject *self, PyObject *args) JOYSTICK_INIT_CHECK(); if (!joy) { - return RAISE(pgExc_SDLError, "Joystick not initialized"); + return RAISERETURN(pgExc_SDLError, "Joystick not initialized", NULL); } if (axis < 0 || axis >= SDL_JoystickNumAxes(joy)) { - return RAISE(pgExc_SDLError, "Invalid joystick axis"); + return RAISERETURN(pgExc_SDLError, "Invalid joystick axis", NULL); } value = SDL_JoystickGetAxis(joy, axis); @@ -402,7 +403,7 @@ joy_get_numbuttons(PyObject *self, PyObject *_null) JOYSTICK_INIT_CHECK(); if (!joy) { - return RAISE(pgExc_SDLError, "Joystick not initialized"); + return RAISERETURN(pgExc_SDLError, "Joystick not initialized", NULL); } return PyLong_FromLong(SDL_JoystickNumButtons(joy)); @@ -420,10 +421,10 @@ joy_get_button(PyObject *self, PyObject *args) JOYSTICK_INIT_CHECK(); if (!joy) { - return RAISE(pgExc_SDLError, "Joystick not initialized"); + return RAISERETURN(pgExc_SDLError, "Joystick not initialized", NULL); } if (_index < 0 || _index >= SDL_JoystickNumButtons(joy)) { - return RAISE(pgExc_SDLError, "Invalid joystick button"); + return RAISERETURN(pgExc_SDLError, "Invalid joystick button", NULL); } value = SDL_JoystickGetButton(joy, _index); @@ -440,7 +441,7 @@ joy_get_numballs(PyObject *self, PyObject *_null) JOYSTICK_INIT_CHECK(); if (!joy) { - return RAISE(pgExc_SDLError, "Joystick not initialized"); + return RAISERETURN(pgExc_SDLError, "Joystick not initialized", NULL); } return PyLong_FromLong(SDL_JoystickNumBalls(joy)); @@ -459,14 +460,14 @@ joy_get_ball(PyObject *self, PyObject *args) JOYSTICK_INIT_CHECK(); if (!joy) { - return RAISE(pgExc_SDLError, "Joystick not initialized"); + return RAISERETURN(pgExc_SDLError, "Joystick not initialized", NULL); } value = SDL_JoystickNumBalls(joy); #ifdef DEBUG /*printf("SDL_JoystickNumBalls value:%d:\n", value);*/ #endif if (_index < 0 || _index >= value) { - return RAISE(pgExc_SDLError, "Invalid joystick trackball"); + return RAISERETURN(pgExc_SDLError, "Invalid joystick trackball", NULL); } SDL_JoystickGetBall(joy, _index, &dx, &dy); @@ -481,7 +482,7 @@ joy_get_numhats(PyObject *self, PyObject *_null) JOYSTICK_INIT_CHECK(); if (!joy) { - return RAISE(pgExc_SDLError, "Joystick not initialized"); + return RAISERETURN(pgExc_SDLError, "Joystick not initialized", NULL); } value = SDL_JoystickNumHats(joy); @@ -504,10 +505,10 @@ joy_get_hat(PyObject *self, PyObject *args) JOYSTICK_INIT_CHECK(); if (!joy) { - return RAISE(pgExc_SDLError, "Joystick not initialized"); + return RAISERETURN(pgExc_SDLError, "Joystick not initialized", NULL); } if (_index < 0 || _index >= SDL_JoystickNumHats(joy)) { - return RAISE(pgExc_SDLError, "Invalid joystick hat"); + return RAISERETURN(pgExc_SDLError, "Invalid joystick hat", NULL); } px = py = 0; @@ -598,12 +599,13 @@ pgJoystick_New(int id) /* This check should be redundant because SDL_JoystickOpen already checks * and errors if id is out of bounds on SDL3 */ if (id >= SDL_NumJoysticks()) { - return RAISE(pgExc_SDLError, "Invalid joystick device number"); + return RAISERETURN(pgExc_SDLError, "Invalid joystick device number", + NULL); } #endif joy = SDL_JoystickOpen(id); if (!joy) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } /* Search existing joystick objects to see if we already have this stick. diff --git a/src_c/key.c b/src_c/key.c index 3792c8fa41..a3317f6741 100644 --- a/src_c/key.c +++ b/src_c/key.c @@ -99,8 +99,8 @@ pg_scancodewrapper_subscript(pgScancodeWrapper *self, PyObject *item) static PyObject * pg_iter_raise(PyObject *self) { - return RAISE(PyExc_TypeError, - "Iterating over key states is not supported"); + return RAISERETURN(PyExc_TypeError, + "Iterating over key states is not supported", NULL); } /** @@ -490,7 +490,7 @@ key_code(PyObject *self, PyObject *args, PyObject *kwargs) code = _pg_get_key_from_name(name); if (code == SDLK_UNKNOWN) { - return RAISE(PyExc_ValueError, "unknown key name"); + return RAISERETURN(PyExc_ValueError, "unknown key name", NULL); } return PyLong_FromLong(code); } @@ -534,11 +534,11 @@ key_start_text_input(PyObject *self, PyObject *_null) * just does backcompat */ SDL_Window *win = pg_GetDefaultWindow(); if (!win) { - return RAISE(pgExc_SDLError, - "display.set_mode has not been called yet."); + return RAISERETURN(pgExc_SDLError, + "display.set_mode has not been called yet.", NULL); } if (!SDL_StartTextInput(win)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } #else /* https://wiki.libsdl.org/SDL_StartTextInput */ @@ -555,11 +555,11 @@ key_stop_text_input(PyObject *self, PyObject *_null) * just does backcompat */ SDL_Window *win = pg_GetDefaultWindow(); if (!win) { - return RAISE(pgExc_SDLError, - "display.set_mode has not been called yet."); + return RAISERETURN(pgExc_SDLError, + "display.set_mode has not been called yet.", NULL); } if (!SDL_StopTextInput(win)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } #else /* https://wiki.libsdl.org/SDL_StopTextInput */ @@ -581,7 +581,7 @@ key_set_text_input_rect(PyObject *self, PyObject *obj) } rect = pgRect_FromObject(obj, &temp); if (!rect) { - return RAISE(PyExc_TypeError, "Invalid rect argument"); + return RAISERETURN(PyExc_TypeError, "Invalid rect argument", NULL); } if (sdlRenderer != NULL) { diff --git a/src_c/line.c b/src_c/line.c index 15fb0f443b..5a413d9414 100644 --- a/src_c/line.c +++ b/src_c/line.c @@ -75,8 +75,9 @@ static PyObject * pg_line_update(pgLineObject *self, PyObject *const *args, Py_ssize_t nargs) { if (!pgLine_FromObjectFastcall(args, nargs, &self->line)) { - return RAISE(PyExc_TypeError, - "Line.update requires a line or LineLike object"); + return RAISERETURN(PyExc_TypeError, + "Line.update requires a line or LineLike object", + NULL); } Py_RETURN_NONE; } @@ -87,7 +88,8 @@ pg_line_move(pgLineObject *self, PyObject *const *args, Py_ssize_t nargs) double Dx, Dy; if (!pg_TwoDoublesFromFastcallArgs(args, nargs, &Dx, &Dy)) { - return RAISE(PyExc_TypeError, "move requires a pair of numbers"); + return RAISERETURN(PyExc_TypeError, "move requires a pair of numbers", + NULL); } return _pg_line_subtype_new4(Py_TYPE(self), self->line.ax + Dx, @@ -101,7 +103,8 @@ pg_line_move_ip(pgLineObject *self, PyObject *const *args, Py_ssize_t nargs) double Dx, Dy; if (!pg_TwoDoublesFromFastcallArgs(args, nargs, &Dx, &Dy)) { - return RAISE(PyExc_TypeError, "move_ip requires a pair of numbers"); + return RAISERETURN(PyExc_TypeError, + "move_ip requires a pair of numbers", NULL); } self->line.ax += Dx; @@ -184,8 +187,8 @@ pg_line_scale(pgLineObject *self, PyObject *const *args, Py_ssize_t nargs) double factor, origin; if (!pg_TwoDoublesFromFastcallArgs(args, nargs, &factor, &origin)) { - return RAISE(PyExc_TypeError, - "scale requires a sequence of two numbers"); + return RAISERETURN(PyExc_TypeError, + "scale requires a sequence of two numbers", NULL); } PyObject *line; @@ -207,8 +210,9 @@ pg_line_scale_ip(pgLineObject *self, PyObject *const *args, Py_ssize_t nargs) double factor, origin; if (!pg_TwoDoublesFromFastcallArgs(args, nargs, &factor, &origin)) { - return RAISE(PyExc_TypeError, - "scale_ip requires a sequence of two numbers"); + return RAISERETURN(PyExc_TypeError, + "scale_ip requires a sequence of two numbers", + NULL); } if (!_line_scale_helper(&pgLine_AsLine(self), factor, origin)) { diff --git a/src_c/mask.c b/src_c/mask.c index 5002dedaf5..093913897e 100644 --- a/src_c/mask.c +++ b/src_c/mask.c @@ -97,7 +97,8 @@ mask_copy(PyObject *self, PyObject *_null) bitmask_t *new_bitmask = bitmask_copy(pgMask_AsBitmap(self)); if (NULL == new_bitmask) { - return RAISE(PyExc_MemoryError, "cannot allocate memory for bitmask"); + return RAISERETURN(PyExc_MemoryError, + "cannot allocate memory for bitmask", NULL); } return (PyObject *)create_mask_using_bitmask_and_type(new_bitmask, @@ -136,14 +137,15 @@ mask_get_rect(PyObject *self, PyObject *args, PyObject *kwargs) bitmask_t *bitmask = pgMask_AsBitmap(self); if (0 != PyTuple_GET_SIZE(args)) { - return RAISE(PyExc_TypeError, - "get_rect only supports keyword arguments"); + return RAISERETURN(PyExc_TypeError, + "get_rect only supports keyword arguments", NULL); } rect = pgRect_New4(0, 0, bitmask->w, bitmask->h); if (NULL == rect) { - return RAISE(PyExc_MemoryError, "cannot allocate memory for rect"); + return RAISERETURN(PyExc_MemoryError, + "cannot allocate memory for rect", NULL); } if (NULL != kwargs) { @@ -174,7 +176,7 @@ mask_get_at(PyObject *self, PyObject *args, PyObject *kwargs) } if (!pg_TwoIntsFromObj(pos, &x, &y)) { - return RAISE(PyExc_TypeError, "pos must be two numbers"); + return RAISERETURN(PyExc_TypeError, "pos must be two numbers", NULL); } if (x >= 0 && x < mask->w && y >= 0 && y < mask->h) { @@ -202,7 +204,7 @@ mask_set_at(PyObject *self, PyObject *args, PyObject *kwargs) } if (!pg_TwoIntsFromObj(pos, &x, &y)) { - return RAISE(PyExc_TypeError, "pos must be two numbers"); + return RAISERETURN(PyExc_TypeError, "pos must be two numbers", NULL); } if (x >= 0 && x < mask->w && y >= 0 && y < mask->h) { @@ -240,7 +242,8 @@ mask_overlap(PyObject *self, PyObject *args, PyObject *kwargs) othermask = pgMask_AsBitmap(maskobj); if (!pg_TwoIntsFromObj(offset, &x, &y)) { - return RAISE(PyExc_TypeError, "offset must be two numbers"); + return RAISERETURN(PyExc_TypeError, "offset must be two numbers", + NULL); } val = bitmask_overlap_pos(mask, othermask, x, y, &xp, &yp); @@ -271,7 +274,8 @@ mask_overlap_area(PyObject *self, PyObject *args, PyObject *kwargs) othermask = pgMask_AsBitmap(maskobj); if (!pg_TwoIntsFromObj(offset, &x, &y)) { - return RAISE(PyExc_TypeError, "offset must be two numbers"); + return RAISERETURN(PyExc_TypeError, "offset must be two numbers", + NULL); } val = bitmask_overlap_area(mask, othermask, x, y); @@ -296,7 +300,8 @@ mask_overlap_mask(PyObject *self, PyObject *args, PyObject *kwargs) output_maskobj = CREATE_MASK_OBJ(bitmask->w, bitmask->h, 0); if (!pg_TwoIntsFromObj(offset, &x, &y)) { - return RAISE(PyExc_TypeError, "offset must be two numbers"); + return RAISERETURN(PyExc_TypeError, "offset must be two numbers", + NULL); } if (NULL == output_maskobj) { @@ -352,17 +357,19 @@ mask_scale(PyObject *self, PyObject *args, PyObject *kwargs) } if (!pg_TwoIntsFromObj(scale, &x, &y)) { - return RAISE(PyExc_TypeError, "scale must be two numbers"); + return RAISERETURN(PyExc_TypeError, "scale must be two numbers", NULL); } if (x < 0 || y < 0) { - return RAISE(PyExc_ValueError, "cannot scale mask to negative size"); + return RAISERETURN(PyExc_ValueError, + "cannot scale mask to negative size", NULL); } bitmask = bitmask_scale(pgMask_AsBitmap(self), x, y); if (NULL == bitmask) { - return RAISE(PyExc_MemoryError, "cannot allocate memory for bitmask"); + return RAISERETURN(PyExc_MemoryError, + "cannot allocate memory for bitmask", NULL); } return (PyObject *)create_mask_using_bitmask(bitmask); @@ -384,7 +391,8 @@ mask_draw(PyObject *self, PyObject *args, PyObject *kwargs) } if (!pg_TwoIntsFromObj(offset, &x, &y)) { - return RAISE(PyExc_TypeError, "offset must be two numbers"); + return RAISERETURN(PyExc_TypeError, "offset must be two numbers", + NULL); } othermask = pgMask_AsBitmap(maskobj); @@ -410,7 +418,8 @@ mask_erase(PyObject *self, PyObject *args, PyObject *kwargs) } if (!pg_TwoIntsFromObj(offset, &x, &y)) { - return RAISE(PyExc_TypeError, "offset must be two numbers"); + return RAISERETURN(PyExc_TypeError, "offset must be two numbers", + NULL); } othermask = pgMask_AsBitmap(maskobj); @@ -518,8 +527,8 @@ mask_outline(PyObject *self, PyObject *args, PyObject *kwargs) plist = PyList_New(0); if (!plist) { - return RAISE(PyExc_MemoryError, - "outline cannot allocate memory for list"); + return RAISERETURN(PyExc_MemoryError, + "outline cannot allocate memory for list", NULL); } if (!c->w || !c->h) { @@ -530,8 +539,8 @@ mask_outline(PyObject *self, PyObject *args, PyObject *kwargs) m = bitmask_create(c->w + 2, c->h + 2); if (!m) { Py_DECREF(plist); - return RAISE(PyExc_MemoryError, - "outline cannot allocate memory for mask"); + return RAISERETURN(PyExc_MemoryError, + "outline cannot allocate memory for mask", NULL); } bitmask_draw(m, c, 1, 1); @@ -683,7 +692,8 @@ mask_convolve(PyObject *aobj, PyObject *args, PyObject *kwargs) } if (offset && !pg_TwoIntsFromObj(offset, &xoffset, &yoffset)) { - return RAISE(PyExc_TypeError, "offset must be two numbers"); + return RAISERETURN(PyExc_TypeError, "offset must be two numbers", + NULL); } a = pgMask_AsBitmap(aobj); @@ -905,15 +915,15 @@ mask_from_surface(PyObject *self, PyObject *args, PyObject *kwargs) surf = pgSurface_AsSurface(surfobj); if (surf->w < 0 || surf->h < 0) { - return RAISE(PyExc_ValueError, - "cannot create mask with negative size"); + return RAISERETURN(PyExc_ValueError, + "cannot create mask with negative size", NULL); } PG_PixelFormat *surf_format; SDL_Palette *surf_palette; if (!PG_GetSurfaceDetails(surf, &surf_format, &surf_palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } maskobj = CREATE_MASK_OBJ(surf->w, surf->h, 0); @@ -929,7 +939,7 @@ mask_from_surface(PyObject *self, PyObject *args, PyObject *kwargs) if (!pgSurface_Lock(surfobj)) { Py_DECREF((PyObject *)maskobj); - return RAISE(PyExc_RuntimeError, "cannot lock surface"); + return RAISERETURN(PyExc_RuntimeError, "cannot lock surface", NULL); } Py_BEGIN_ALLOW_THREADS; /* Release the GIL. */ @@ -947,7 +957,7 @@ mask_from_surface(PyObject *self, PyObject *args, PyObject *kwargs) if (!pgSurface_Unlock(surfobj)) { Py_DECREF((PyObject *)maskobj); - return RAISE(PyExc_RuntimeError, "cannot unlock surface"); + return RAISERETURN(PyExc_RuntimeError, "cannot unlock surface", NULL); } return (PyObject *)maskobj; @@ -1146,13 +1156,13 @@ mask_from_threshold(PyObject *self, PyObject *args, PyObject *kwargs) } if (!PG_GetSurfaceDetails(surf, &surf_format, &surf_palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (surf2) { surf2_format = PG_GetSurfaceFormat(surf2); if (!surf2_format) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } @@ -1556,8 +1566,9 @@ mask_get_bounding_rects(PyObject *self, PyObject *_null) if (r == -2) { /* memory out failure */ - return RAISE(PyExc_MemoryError, - "Not enough memory to get bounding rects. \n"); + return RAISERETURN(PyExc_MemoryError, + "Not enough memory to get bounding rects. \n", + NULL); } rect_list = PyList_New(0); @@ -1577,8 +1588,9 @@ mask_get_bounding_rects(PyObject *self, PyObject *_null) Py_DECREF(rect_list); free(regions); - return RAISE(PyExc_MemoryError, - "cannot allocate memory for bounding rects"); + return RAISERETURN(PyExc_MemoryError, + "cannot allocate memory for bounding rects", + NULL); } if (0 != PyList_Append(rect_list, rect)) { @@ -1743,8 +1755,9 @@ mask_connected_components(PyObject *self, PyObject *args, PyObject *kwargs) Py_END_ALLOW_THREADS; if (num_components == -2) { - return RAISE(PyExc_MemoryError, - "cannot allocate memory for connected components"); + return RAISERETURN(PyExc_MemoryError, + "cannot allocate memory for connected components", + NULL); } mask_list = PyList_New(0); @@ -1902,7 +1915,8 @@ mask_connected_component(PyObject *self, PyObject *args, PyObject *kwargs) } if (!pg_TwoIntsFromObj(pos, &x, &y)) { - return RAISE(PyExc_TypeError, "pos must be two numbers"); + return RAISERETURN(PyExc_TypeError, "pos must be two numbers", + NULL); } if (x < 0 || x >= input->w || y < 0 || y >= input->h) { @@ -1923,8 +1937,9 @@ mask_connected_component(PyObject *self, PyObject *args, PyObject *kwargs) if (!args_exist || bitmask_getbit(input, x, y)) { if (largest_connected_comp(input, output_maskobj->mask, x, y) == -2) { Py_DECREF(output_maskobj); - return RAISE(PyExc_MemoryError, - "cannot allocate memory for connected component"); + return RAISERETURN( + PyExc_MemoryError, + "cannot allocate memory for connected component", NULL); } } @@ -2253,7 +2268,8 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs) if (NULL == surfobj) { if (!PyErr_Occurred()) { - return RAISE(PyExc_RuntimeError, "unable to create surface"); + return RAISERETURN(PyExc_RuntimeError, + "unable to create surface", NULL); } return NULL; } @@ -2261,7 +2277,7 @@ mask_to_surface(PyObject *self, PyObject *args, PyObject *kwargs) created_surfobj = 1; } else if (!pgSurface_Check(surfobj)) { - return RAISE(PyExc_TypeError, "invalid surface argument"); + return RAISERETURN(PyExc_TypeError, "invalid surface argument", NULL); } surf = pgSurface_AsSurface(surfobj); @@ -2490,8 +2506,8 @@ create_mask_using_bitmask_and_type(bitmask_t *bitmask, PyTypeObject *ob_type) (pgMaskObject *)pgMask_Type.tp_new(ob_type, NULL, NULL); if (NULL == maskobj) { - return (pgMaskObject *)RAISE(PyExc_MemoryError, - "cannot allocate memory for mask"); + return (pgMaskObject *)RAISERETURN( + PyExc_MemoryError, "cannot allocate memory for mask", NULL); } maskobj->mask = bitmask; @@ -2525,7 +2541,8 @@ mask_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) pgMaskObject *maskobj = (pgMaskObject *)subtype->tp_alloc(subtype, 0); if (NULL == maskobj) { - return RAISE(PyExc_MemoryError, "cannot allocate memory for mask"); + return RAISERETURN(PyExc_MemoryError, + "cannot allocate memory for mask", NULL); } maskobj->mask = NULL; diff --git a/src_c/math.c b/src_c/math.c index 7292d6f55a..9d62c5eb95 100644 --- a/src_c/math.c +++ b/src_c/math.c @@ -631,8 +631,8 @@ pgVector_NEW(Py_ssize_t dim) case 3: return vector3_new(&pgVector3_Type, NULL, NULL); default: - return RAISE(PyExc_SystemError, - "Wrong internal call to pgVector_NEW.\n"); + return RAISERETURN(PyExc_SystemError, + "Wrong internal call to pgVector_NEW.\n", NULL); } } @@ -771,7 +771,8 @@ vector_generic_math(PyObject *o1, PyObject *o2, int op) case OP_DIV | OP_ARG_NUMBER | OP_INPLACE: if (tmp == 0.) { Py_DECREF(ret); - return RAISE(PyExc_ZeroDivisionError, "division by zero"); + return RAISERETURN(PyExc_ZeroDivisionError, "division by zero", + NULL); } tmp = 1. / tmp; for (i = 0; i < dim; i++) { @@ -782,7 +783,8 @@ vector_generic_math(PyObject *o1, PyObject *o2, int op) case OP_FLOOR_DIV | OP_ARG_NUMBER | OP_INPLACE: if (tmp == 0.) { Py_DECREF(ret); - return RAISE(PyExc_ZeroDivisionError, "division by zero"); + return RAISERETURN(PyExc_ZeroDivisionError, "division by zero", + NULL); } tmp = 1. / tmp; for (i = 0; i < dim; i++) { @@ -947,26 +949,30 @@ vector_clamp_magnitude_ip(pgVector *self, PyObject *const *args, } break; default: - return RAISE(PyExc_TypeError, - "Vector clamp function must take one or two floats"); + return RAISERETURN( + PyExc_TypeError, + "Vector clamp function must take one or two floats", NULL); } if (min_length > max_length) { - return RAISE(PyExc_ValueError, - "Argument min_length cannot exceed max_length"); + return RAISERETURN(PyExc_ValueError, + "Argument min_length cannot exceed max_length", + NULL); } if (max_length < 0 || min_length < 0) { - return RAISE(PyExc_ValueError, - "Arguments to Vector clamp must be non-negative"); + return RAISERETURN(PyExc_ValueError, + "Arguments to Vector clamp must be non-negative", + NULL); } /* Get magnitude of Vector */ old_length_sq = _scalar_product(self->coords, self->coords, self->dim); if (old_length_sq == 0 && min_length > 0) { - return RAISE(PyExc_ValueError, - "Cannot clamp a vector with zero length with a " - "min_length greater than 0"); + return RAISERETURN(PyExc_ValueError, + "Cannot clamp a vector with zero length with a " + "min_length greater than 0", + NULL); } /* Notes for other contributors reading this code: @@ -1019,7 +1025,7 @@ static PyObject * vector_GetItem(pgVector *self, Py_ssize_t index) { if (index < 0 || index >= self->dim) { - return RAISE(PyExc_IndexError, "subscript out of range."); + return RAISERETURN(PyExc_IndexError, "subscript out of range.", NULL); } return PyFloat_FromDouble(self->coords[index]); } @@ -1408,8 +1414,9 @@ vector_richcompare(PyObject *o1, PyObject *o2, int op) } Py_RETURN_FALSE; default: - return RAISE(PyExc_TypeError, - "This operation is not supported by vectors"); + return RAISERETURN(PyExc_TypeError, + "This operation is not supported by vectors", + NULL); } } @@ -1456,8 +1463,8 @@ vector_normalize_ip(pgVector *self, PyObject *_null) length = sqrt(_scalar_product(self->coords, self->coords, self->dim)); if (length == 0) { - return RAISE(PyExc_ValueError, - "Can't normalize Vector of length zero"); + return RAISERETURN(PyExc_ValueError, + "Can't normalize Vector of length zero", NULL); } for (i = 0; i < self->dim; ++i) { @@ -1485,8 +1492,8 @@ vector_dot(pgVector *self, PyObject *other) { double other_coords[VECTOR_MAX_SIZE]; if (!PySequence_AsVectorCoords(other, other_coords, self->dim)) { - return RAISE(PyExc_TypeError, - "Cannot perform dot product with this type."); + return RAISERETURN(PyExc_TypeError, + "Cannot perform dot product with this type.", NULL); } return PyFloat_FromDouble( _scalar_product(self->coords, other_coords, self->dim)); @@ -1507,8 +1514,8 @@ vector_scale_to_length(pgVector *self, PyObject *length) old_length = sqrt(_scalar_product(self->coords, self->coords, self->dim)); if (old_length < self->epsilon) { - return RAISE(PyExc_ValueError, - "Cannot scale a vector with zero length"); + return RAISERETURN(PyExc_ValueError, + "Cannot scale a vector with zero length", NULL); } fraction = new_length / old_length; @@ -1569,7 +1576,8 @@ vector_move_towards(pgVector *self, PyObject *args) } if (!pg_VectorCoordsFromObj(target, self->dim, target_coords)) { - return RAISE(PyExc_TypeError, "Incompatible vector argument"); + return RAISERETURN(PyExc_TypeError, "Incompatible vector argument", + NULL); } ret = _vector_subtype_new(self); @@ -1600,7 +1608,8 @@ vector_move_towards_ip(pgVector *self, PyObject *args) } if (!pg_VectorCoordsFromObj(target, self->dim, target_coords)) { - return RAISE(PyExc_TypeError, "Incompatible vector argument"); + return RAISERETURN(PyExc_TypeError, "Incompatible vector argument", + NULL); } _vector_move_towards_helper(self->dim, self->coords, target_coords, @@ -1622,16 +1631,19 @@ vector_slerp(pgVector *self, PyObject *args) return NULL; } if (!PySequence_AsVectorCoords(other, other_coords, self->dim)) { - return RAISE(PyExc_TypeError, "Argument 1 must be a vector."); + return RAISERETURN(PyExc_TypeError, "Argument 1 must be a vector.", + NULL); } if (fabs(t) > 1) { - return RAISE(PyExc_ValueError, "Argument 2 must be in range [-1, 1]."); + return RAISERETURN(PyExc_ValueError, + "Argument 2 must be in range [-1, 1].", NULL); } length1 = sqrt(_scalar_product(self->coords, self->coords, self->dim)); length2 = sqrt(_scalar_product(other_coords, other_coords, self->dim)); if ((length1 < self->epsilon) || (length2 < self->epsilon)) { - return RAISE(PyExc_ValueError, "can't use slerp with Zero-Vector"); + return RAISERETURN(PyExc_ValueError, + "can't use slerp with Zero-Vector", NULL); } tmp = (_scalar_product(self->coords, other_coords, self->dim) / (length1 * length2)); @@ -1664,7 +1676,8 @@ vector_slerp(pgVector *self, PyObject *args) /* special case angle==180 and angle==-180 */ else if (fabs(fabs(angle) - M_PI) < self->epsilon) { Py_DECREF(ret); - return RAISE(PyExc_ValueError, "SLERP with 180 degrees is undefined."); + return RAISERETURN(PyExc_ValueError, + "SLERP with 180 degrees is undefined.", NULL); } else { f0 = ((length2 - length1) * t + length1) / sin(angle); @@ -1691,10 +1704,12 @@ vector_lerp(pgVector *self, PyObject *args) return NULL; } if (!PySequence_AsVectorCoords(other, other_coords, self->dim)) { - return RAISE(PyExc_TypeError, "Expected Vector as argument 1"); + return RAISERETURN(PyExc_TypeError, "Expected Vector as argument 1", + NULL); } if (t < 0 || t > 1) { - return RAISE(PyExc_ValueError, "Argument 2 must be in range [0, 1]"); + return RAISERETURN(PyExc_ValueError, + "Argument 2 must be in range [0, 1]", NULL); } ret = _vector_subtype_new(self); @@ -1720,7 +1735,8 @@ vector_smoothstep(pgVector *self, PyObject *args) return NULL; } if (!PySequence_AsVectorCoords(other, other_coords, self->dim)) { - return RAISE(PyExc_TypeError, "Expected Vector as argument 1"); + return RAISERETURN(PyExc_TypeError, "Expected Vector as argument 1", + NULL); } if (t <= 0.0) { @@ -1927,9 +1943,10 @@ vector_repr(pgVector *self) self->coords[0], self->coords[1], self->coords[2]); } else { - return RAISE( + return RAISERETURN( PyExc_NotImplementedError, - "repr() for Vectors of higher dimensions are not implemented yet"); + "repr() for Vectors of higher dimensions are not implemented yet", + NULL); } if (!_vector_check_snprintf_success(tmp, STRING_BUF_SIZE_REPR)) { @@ -1958,9 +1975,10 @@ vector_str(pgVector *self) self->coords[0], self->coords[1], self->coords[2]); } else { - return RAISE( + return RAISERETURN( PyExc_NotImplementedError, - "repr() for Vectors of higher dimensions are not implemented yet"); + "repr() for Vectors of higher dimensions are not implemented yet", + NULL); } if (!_vector_check_snprintf_success(tmp, STRING_BUF_SIZE_STR)) { @@ -1981,7 +1999,8 @@ vector_project_onto(pgVector *self, PyObject *other) double b_dot_b; if (!PySequence_AsVectorCoords(other, other_coords, self->dim)) { - return RAISE(PyExc_TypeError, "Expected Vector as argument 1"); + return RAISERETURN(PyExc_TypeError, "Expected Vector as argument 1", + NULL); } ret = _vector_subtype_new(self); @@ -1994,8 +2013,9 @@ vector_project_onto(pgVector *self, PyObject *other) if (b_dot_b < self->epsilon) { Py_DECREF(ret); - return RAISE(PyExc_ValueError, - "Cannot project onto a vector with zero length"); + return RAISERETURN(PyExc_ValueError, + "Cannot project onto a vector with zero length", + NULL); } factor = a_dot_b / b_dot_b; @@ -2296,7 +2316,8 @@ vector___round__(pgVector *self, PyObject *args) } else { Py_DECREF(ret); - return RAISE(PyExc_TypeError, "Argument must be an integer"); + return RAISERETURN(PyExc_TypeError, "Argument must be an integer", + NULL); } return (PyObject *)ret; @@ -2571,9 +2592,10 @@ vector2_cross(pgVector *self, PyObject *other) } if (!pg_VectorCoordsFromObj(other, 2, other_coords)) { - return RAISE( + return RAISERETURN( PyExc_TypeError, - "Incompatible vector argument: cannot calculate cross product"); + "Incompatible vector argument: cannot calculate cross product", + NULL); } return PyFloat_FromDouble((self->coords[0] * other_coords[1]) - @@ -2593,9 +2615,9 @@ vector2_angle_to(pgVector *self, PyObject *other) double other_coords[2]; if (!pg_VectorCoordsFromObj(other, 2, other_coords)) { - return RAISE( + return RAISERETURN( PyExc_TypeError, - "Incompatible vector argument: cannot calculate angle to"); + "Incompatible vector argument: cannot calculate angle to", NULL); } angle = (atan2(other_coords[1], other_coords[0]) - @@ -2992,8 +3014,9 @@ vector3_rotate_rad(pgVector *self, PyObject *args) return NULL; } if (!pg_VectorCoordsFromObj(axis, 3, axis_coords)) { - return RAISE(PyExc_TypeError, - "Incompatible vector argument: Axis must be a 3D vector"); + return RAISERETURN( + PyExc_TypeError, + "Incompatible vector argument: Axis must be a 3D vector", NULL); } ret = _vector_subtype_new(self); @@ -3020,8 +3043,9 @@ vector3_rotate_rad_ip(pgVector *self, PyObject *args) return NULL; } if (!pg_VectorCoordsFromObj(axis, 3, axis_coords)) { - return RAISE(PyExc_TypeError, - "Incompatible vector argument: Axis must be a 3D vector"); + return RAISERETURN( + PyExc_TypeError, + "Incompatible vector argument: Axis must be a 3D vector", NULL); } memcpy(tmp, self->coords, 3 * sizeof(double)); @@ -3058,8 +3082,9 @@ vector3_rotate(pgVector *self, PyObject *args) } angle = DEG2RAD(angle); if (!pg_VectorCoordsFromObj(axis, 3, axis_coords)) { - return RAISE(PyExc_TypeError, - "Incompatible vector argument: Axis must be a 3D vector"); + return RAISERETURN( + PyExc_TypeError, + "Incompatible vector argument: Axis must be a 3D vector", NULL); } ret = _vector_subtype_new(self); @@ -3087,8 +3112,9 @@ vector3_rotate_ip(pgVector *self, PyObject *args) } angle = DEG2RAD(angle); if (!pg_VectorCoordsFromObj(axis, 3, axis_coords)) { - return RAISE(PyExc_TypeError, - "Incompatible vector argument: Axis must be a 3D vector"); + return RAISERETURN( + PyExc_TypeError, + "Incompatible vector argument: Axis must be a 3D vector", NULL); } memcpy(tmp, self->coords, 3 * sizeof(double)); @@ -3424,9 +3450,10 @@ vector3_cross(pgVector *self, PyObject *other) double other_coords[3]; if (!pg_VectorCoordsFromObj(other, 3, other_coords)) { - return RAISE( + return RAISERETURN( PyExc_TypeError, - "Incompatible vector argument: cannot calculate cross product"); + "Incompatible vector argument: cannot calculate cross product", + NULL); } self_coords = self->coords; @@ -3453,16 +3480,17 @@ vector3_angle_to(pgVector *self, PyObject *other) double other_coords[3]; if (!pg_VectorCoordsFromObj(other, 3, other_coords)) { - return RAISE( + return RAISERETURN( PyExc_TypeError, - "Incompatible vector argument: cannot calculate angle to"); + "Incompatible vector argument: cannot calculate angle to", NULL); } squared_length1 = _scalar_product(self->coords, self->coords, self->dim); squared_length2 = _scalar_product(other_coords, other_coords, self->dim); tmp = sqrt(squared_length1 * squared_length2); if (tmp == 0) { - return RAISE(PyExc_ValueError, "angle to zero vector is undefined."); + return RAISERETURN(PyExc_ValueError, + "angle to zero vector is undefined.", NULL); } angle = acos(_scalar_product(self->coords, other_coords, self->dim) / tmp); return PyFloat_FromDouble(RAD2DEG(angle)); @@ -3985,7 +4013,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) for (i = 0; i < vec->dim; i++) { if (other_coords[i] == 0) { Py_DECREF(ret); - return RAISE(PyExc_ZeroDivisionError, "division by zero"); + return RAISERETURN(PyExc_ZeroDivisionError, + "division by zero", NULL); } ret->coords[i] = vec->coords[i] / other_coords[i]; } @@ -3994,7 +4023,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) for (i = 0; i < vec->dim; i++) { if (vec->coords[i] == 0) { Py_DECREF(ret); - return RAISE(PyExc_ZeroDivisionError, "division by zero"); + return RAISERETURN(PyExc_ZeroDivisionError, + "division by zero", NULL); } ret->coords[i] = other_coords[i] / vec->coords[i]; } @@ -4003,7 +4033,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) for (i = 0; i < vec->dim; i++) { if (vec->coords[i] == 0) { Py_DECREF(ret); - return RAISE(PyExc_ZeroDivisionError, "division by zero"); + return RAISERETURN(PyExc_ZeroDivisionError, + "division by zero", NULL); } ret->coords[i] = other_value / vec->coords[i]; } @@ -4012,7 +4043,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) for (i = 0; i < vec->dim; i++) { if (other_coords[i] == 0) { Py_DECREF(ret); - return RAISE(PyExc_ZeroDivisionError, "division by zero"); + return RAISERETURN(PyExc_ZeroDivisionError, + "division by zero", NULL); } ret->coords[i] = floor(vec->coords[i] / other_coords[i]); } @@ -4021,7 +4053,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) for (i = 0; i < vec->dim; i++) { if (vec->coords[i] == 0) { Py_DECREF(ret); - return RAISE(PyExc_ZeroDivisionError, "division by zero"); + return RAISERETURN(PyExc_ZeroDivisionError, + "division by zero", NULL); } ret->coords[i] = floor(other_coords[i] / vec->coords[i]); } @@ -4030,7 +4063,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) for (i = 0; i < vec->dim; i++) { if (vec->coords[i] == 0) { Py_DECREF(ret); - return RAISE(PyExc_ZeroDivisionError, "division by zero"); + return RAISERETURN(PyExc_ZeroDivisionError, + "division by zero", NULL); } ret->coords[i] = floor(other_value / vec->coords[i]); } @@ -4039,7 +4073,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) for (i = 0; i < vec->dim; i++) { if (other_coords[i] == 0) { Py_DECREF(ret); - return RAISE(PyExc_ZeroDivisionError, "division by zero"); + return RAISERETURN(PyExc_ZeroDivisionError, + "division by zero", NULL); } mod = fmod(vec->coords[i], other_coords[i]); /* note: checking mod*value < 0 is incorrect -- underflows @@ -4054,7 +4089,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) for (i = 0; i < vec->dim; i++) { if (vec->coords[i] == 0) { Py_DECREF(ret); - return RAISE(PyExc_ZeroDivisionError, "division by zero"); + return RAISERETURN(PyExc_ZeroDivisionError, + "division by zero", NULL); } mod = fmod(other_coords[i], vec->coords[i]); /* note: see above */ @@ -4067,7 +4103,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) case OP_MOD | OP_ARG_NUMBER: if (other_value == 0) { Py_DECREF(ret); - return RAISE(PyExc_ZeroDivisionError, "division by zero"); + return RAISERETURN(PyExc_ZeroDivisionError, "division by zero", + NULL); } for (i = 0; i < vec->dim; i++) { mod = fmod(vec->coords[i], other_value); @@ -4082,7 +4119,8 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op) for (i = 0; i < vec->dim; i++) { if (vec->coords[i] == 0) { Py_DECREF(ret); - return RAISE(PyExc_ZeroDivisionError, "division by zero"); + return RAISERETURN(PyExc_ZeroDivisionError, + "division by zero", NULL); } mod = fmod(other_value, vec->coords[i]); /* note: see above */ @@ -4141,9 +4179,10 @@ vector_elementwiseproxy_pow(PyObject *baseObj, PyObject *expoObj, PyObject *expos[VECTOR_MAX_SIZE] = {NULL}; PyObject *ret, *result; if (mod != Py_None) { - return RAISE(PyExc_TypeError, - "pow() 3rd argument not " - "supported for vectors"); + return RAISERETURN(PyExc_TypeError, + "pow() 3rd argument not " + "supported for vectors", + NULL); } if (vector_elementwiseproxy_Check(baseObj)) { @@ -4331,7 +4370,8 @@ static PyObject * math_clamp(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { if (nargs != 3) { - return RAISE(PyExc_TypeError, "clamp requires 3 arguments"); + return RAISERETURN(PyExc_TypeError, "clamp requires 3 arguments", + NULL); } PyObject *value = args[0]; @@ -4340,7 +4380,8 @@ math_clamp(PyObject *self, PyObject *const *args, Py_ssize_t nargs) if (PyNumber_Check(args[0]) != 1 || PyNumber_Check(args[1]) != 1 || PyNumber_Check(args[2]) != 1) { - return RAISE(PyExc_TypeError, "clamp requires 3 numeric arguments"); + return RAISERETURN(PyExc_TypeError, + "clamp requires 3 numeric arguments", NULL); } // Using RichCompare instead of converting to C types for performance @@ -4371,18 +4412,20 @@ math_clamp(PyObject *self, PyObject *const *args, Py_ssize_t nargs) return value; } -#define RAISE_ARG_TYPE_ERROR(var) \ - if (PyErr_Occurred()) { \ - return RAISE(PyExc_TypeError, \ - "The argument '" var "' must be a real number"); \ +#define RAISE_ARG_TYPE_ERROR(var) \ + if (PyErr_Occurred()) { \ + return RAISERETURN(PyExc_TypeError, \ + "The argument '" var "' must be a real number", \ + NULL); \ } static PyObject * math_invlerp(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { if (nargs != 3) { - return RAISE(PyExc_TypeError, - "invlerp requires exactly 3 numeric arguments"); + return RAISERETURN(PyExc_TypeError, + "invlerp requires exactly 3 numeric arguments", + NULL); } double a = PyFloat_AsDouble(args[0]); @@ -4393,14 +4436,17 @@ math_invlerp(PyObject *self, PyObject *const *args, Py_ssize_t nargs) RAISE_ARG_TYPE_ERROR("value") if (PyErr_Occurred()) { - return RAISE(PyExc_ValueError, - "invalid argument values passed to invlerp, numbers " - "might be too small or too big"); + return RAISERETURN( + PyExc_ValueError, + "invalid argument values passed to invlerp, numbers " + "might be too small or too big", + NULL); } if (b - a == 0) { - return RAISE(PyExc_ValueError, - "the result of b - a needs to be different from zero"); + return RAISERETURN( + PyExc_ValueError, + "the result of b - a needs to be different from zero", NULL); } return PyFloat_FromDouble(invlerp(a, b, t)); @@ -4412,8 +4458,8 @@ static PyObject * math_remap(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { if (nargs != 5) { - return RAISE(PyExc_TypeError, - "remap requires exactly 5 numeric arguments"); + return RAISERETURN(PyExc_TypeError, + "remap requires exactly 5 numeric arguments", NULL); } PyObject *i_min = args[0]; @@ -4434,9 +4480,10 @@ math_remap(PyObject *self, PyObject *const *args, Py_ssize_t nargs) RAISE_ARG_TYPE_ERROR("o_max") if (b - a == 0) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "the result of i_max - i_min needs to be different from zero"); + "the result of i_max - i_min needs to be different from zero", + NULL); } return PyFloat_FromDouble(lerp(c, d, invlerp(a, b, v))); @@ -4446,7 +4493,8 @@ static PyObject * math_lerp(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { if (nargs != 3 && nargs != 4) { - return RAISE(PyExc_TypeError, "lerp requires 3 or 4 arguments"); + return RAISERETURN(PyExc_TypeError, "lerp requires 3 or 4 arguments", + NULL); } PyObject *min = args[0]; @@ -4454,8 +4502,9 @@ math_lerp(PyObject *self, PyObject *const *args, Py_ssize_t nargs) PyObject *value = args[2]; if (PyNumber_Check(args[2]) != 1) { - return RAISE(PyExc_TypeError, - "lerp requires the interpolation amount to be number"); + return RAISERETURN( + PyExc_TypeError, + "lerp requires the interpolation amount to be number", NULL); } double t = PyFloat_AsDouble(value); @@ -4477,10 +4526,11 @@ math_lerp(PyObject *self, PyObject *const *args, Py_ssize_t nargs) PyFloat_AsDouble(max) * t); } else { - return RAISE( + return RAISERETURN( PyExc_TypeError, "math.lerp requires all the arguments to be numbers. To lerp " - "between two vectors, please use the Vector class methods."); + "between two vectors, please use the Vector class methods.", + NULL); } } @@ -4488,7 +4538,8 @@ static PyObject * math_smoothstep(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { if (nargs != 3) { - return RAISE(PyExc_TypeError, "smoothstep requires 3 arguments"); + return RAISERETURN(PyExc_TypeError, "smoothstep requires 3 arguments", + NULL); } PyObject *min = args[0]; @@ -4496,9 +4547,9 @@ math_smoothstep(PyObject *self, PyObject *const *args, Py_ssize_t nargs) PyObject *value = args[2]; if (PyNumber_Check(args[2]) != 1) { - return RAISE( + return RAISERETURN( PyExc_TypeError, - "smoothstep requires the interpolation amount to be number"); + "smoothstep requires the interpolation amount to be number", NULL); } double t = PyFloat_AsDouble(value); @@ -4519,11 +4570,12 @@ math_smoothstep(PyObject *self, PyObject *const *args, Py_ssize_t nargs) PyFloat_AsDouble(max) * t); } else { - return RAISE( + return RAISERETURN( PyExc_TypeError, "smoothstep requires all the arguments to be numbers. To " "smoothstep " - "between two vectors, please use the Vector class methods."); + "between two vectors, please use the Vector class methods.", + NULL); } } diff --git a/src_c/mixer.c b/src_c/mixer.c index 10b04131b0..b8da0b2414 100644 --- a/src_c/mixer.c +++ b/src_c/mixer.c @@ -372,8 +372,8 @@ _init(int freq, int size, int channels, int chunk, char *devicename, case 6: break; default: - return RAISE(PyExc_ValueError, - "'channels' must be 1, 2, 4, or 6"); + return RAISERETURN(PyExc_ValueError, + "'channels' must be 1, 2, 4, or 6", NULL); } } @@ -445,13 +445,13 @@ _init(int freq, int size, int channels, int chunk, char *devicename, } if (SDL_InitSubSystem(SDL_INIT_AUDIO)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (Mix_OpenAudioDevice(freq, fmt, channels, chunk, devicename, allowedchanges) == -1) { SDL_QuitSubSystem(SDL_INIT_AUDIO); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Mix_ChannelFinished(endsound_callback); Mix_VolumeMusic(127); @@ -1099,8 +1099,8 @@ chan_queue(PyObject *self, PyObject *sound) Mix_Chunk *chunk; if (!pgSound_Check(sound)) { - return RAISE(PyExc_TypeError, - "The argument must be an instance of Sound"); + return RAISERETURN(PyExc_TypeError, + "The argument must be an instance of Sound", NULL); } chunk = pgSound_AsChunk(sound); @@ -1202,8 +1202,8 @@ chan_set_source_location(PyObject *self, PyObject *args) angle = (Sint16)roundf(fmodf(angle_f, 360)); distance_f = roundf(distance_f); if (0 > distance_f || 256 <= distance_f) { - return RAISE(PyExc_ValueError, - "distance out of range, expected (0, 255)"); + return RAISERETURN(PyExc_ValueError, + "distance out of range, expected (0, 255)", NULL); } distance = (Uint8)distance_f; @@ -1211,7 +1211,7 @@ chan_set_source_location(PyObject *self, PyObject *args) _save = PyEval_SaveThread(); if (!Mix_SetPosition(channelnum, angle, distance)) { PyEval_RestoreThread(_save); - return RAISE(pgExc_SDLError, Mix_GetError()); + return RAISERETURN(pgExc_SDLError, Mix_GetError(), NULL); } PyEval_RestoreThread(_save); Py_RETURN_NONE; @@ -1242,7 +1242,7 @@ chan_set_volume(PyObject *self, PyObject *args) _save = PyEval_SaveThread(); if (!Mix_SetPanning(channelnum, left, right)) { PyEval_RestoreThread(_save); - return RAISE(pgExc_SDLError, Mix_GetError()); + return RAISERETURN(pgExc_SDLError, Mix_GetError(), NULL); } PyEval_RestoreThread(_save); } @@ -1258,7 +1258,7 @@ chan_set_volume(PyObject *self, PyObject *args) _save = PyEval_SaveThread(); if (!Mix_SetPanning(channelnum, left, right)) { PyEval_RestoreThread(_save); - return RAISE(pgExc_SDLError, Mix_GetError()); + return RAISERETURN(pgExc_SDLError, Mix_GetError(), NULL); } PyEval_RestoreThread(_save); @@ -1516,8 +1516,8 @@ mixer_set_soundfont(PyObject *self, PyObject *args) string_path = PyUnicode_AsUTF8(path); } else if (!Py_IsNone(path)) { - return RAISE(PyExc_TypeError, - "Must pass string or None to set_soundfont"); + return RAISERETURN(PyExc_TypeError, + "Must pass string or None to set_soundfont", NULL); } if (strlen(string_path) == 0) { @@ -1528,7 +1528,7 @@ mixer_set_soundfont(PyObject *self, PyObject *args) } if (paths_set == 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; @@ -2014,7 +2014,8 @@ pgSound_New(Mix_Chunk *chunk) pgSoundObject *soundobj; if (!chunk) { - return RAISE(PyExc_RuntimeError, "unable to create sound."); + return RAISERETURN(PyExc_RuntimeError, "unable to create sound.", + NULL); } soundobj = (pgSoundObject *)pgSound_Type.tp_new(&pgSound_Type, NULL, NULL); diff --git a/src_c/mixer.h b/src_c/mixer.h index 97f5a0f1d0..52518b13f1 100644 --- a/src_c/mixer.h +++ b/src_c/mixer.h @@ -4,9 +4,10 @@ #include /* test mixer initializations */ -#define MIXER_INIT_CHECK() \ - if (!SDL_WasInit(SDL_INIT_AUDIO)) \ - return RAISE(pgExc_SDLError, "mixer not initialized") +#define MIXER_INIT_CHECK() \ + if (!SDL_WasInit(SDL_INIT_AUDIO)) { \ + return RAISERETURN(pgExc_SDLError, "mixer not initialized", NULL); \ + } #define PYGAMEAPI_MIXER_NUMSLOTS 5 #include "include/pygame_mixer.h" diff --git a/src_c/mouse.c b/src_c/mouse.c index 62d1ea6b77..9bb61a6163 100644 --- a/src_c/mouse.c +++ b/src_c/mouse.c @@ -36,7 +36,8 @@ mouse_set_pos(PyObject *self, PyObject *args) int x, y; if (!pg_TwoIntsFromObj(args, &x, &y)) { - return RAISE(PyExc_TypeError, "invalid position argument for set_pos"); + return RAISERETURN(PyExc_TypeError, + "invalid position argument for set_pos", NULL); } VIDEO_INIT_CHECK(); @@ -200,8 +201,8 @@ mouse_get_pressed(PyObject *self, PyObject *args, PyObject *kwargs) VIDEO_INIT_CHECK(); if (num_buttons != 3 && num_buttons != 5) { - return RAISE(PyExc_ValueError, - "Number of buttons needs to be 3 or 5."); + return RAISERETURN(PyExc_ValueError, + "Number of buttons needs to be 3 or 5.", NULL); } state = desktop ? SDL_GetGlobalMouseState(NULL, NULL) @@ -331,7 +332,7 @@ mouse_get_visible(PyObject *self, PyObject *_null) #endif if (0 > result) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return PyBool_FromLong(result); @@ -376,11 +377,13 @@ _set_bitmap_cursor(int w, int h, int spotx, int spoty, PyObject *xormask, SDL_Cursor *lastcursor, *cursor = NULL; if (!PySequence_Check(xormask) || !PySequence_Check(andmask)) { - return RAISE(PyExc_TypeError, "xormask and andmask must be sequences"); + return RAISERETURN(PyExc_TypeError, + "xormask and andmask must be sequences", NULL); } if (w % 8) { - return RAISE(PyExc_ValueError, "Cursor width must be divisible by 8."); + return RAISERETURN(PyExc_ValueError, + "Cursor width must be divisible by 8.", NULL); } xorsize = (int)PySequence_Length(xormask); @@ -394,8 +397,8 @@ _set_bitmap_cursor(int w, int h, int spotx, int spoty, PyObject *xormask, } if (xorsize != w * h / 8 || andsize != w * h / 8) { - return RAISE(PyExc_ValueError, - "bitmasks must be sized width*height/8"); + return RAISERETURN(PyExc_ValueError, + "bitmasks must be sized width*height/8", NULL); } #ifdef _MSC_VER @@ -431,7 +434,7 @@ _set_bitmap_cursor(int w, int h, int spotx, int spoty, PyObject *xormask, anddata = NULL; if (!cursor) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } lastcursor = SDL_GetCursor(); @@ -462,7 +465,7 @@ _set_bitmap_cursor(int w, int h, int spotx, int spoty, PyObject *xormask, if (anddata) { free(anddata); } - return RAISE(PyExc_TypeError, "Invalid number in mask array"); + return RAISERETURN(PyExc_TypeError, "Invalid number in mask array", NULL); } static PyObject * @@ -470,15 +473,16 @@ _set_system_cursor(int constant) { SDL_Cursor *lastcursor, *cursor = NULL; if (constant < 0 || constant >= SDL_NUM_SYSTEM_CURSORS) { - return RAISE(pgExc_SDLError, - "System cursor constant value out of range"); + return RAISERETURN(pgExc_SDLError, + "System cursor constant value out of range", NULL); } cursor = SDL_CreateSystemCursor(constant); if (!cursor) { // SDL_GetError() wasn't returning relevant stuff when this function // fails - return RAISE(pgExc_SDLError, "Error while creating system cursor"); + return RAISERETURN(pgExc_SDLError, + "Error while creating system cursor", NULL); } lastcursor = SDL_GetCursor(); @@ -499,7 +503,7 @@ _set_color_cursor(int spotx, int spoty, pgSurfaceObject *surfobj) cursor = SDL_CreateColorCursor(surf, spotx, spoty); if (!cursor) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } lastcursor = SDL_GetCursor(); @@ -575,8 +579,8 @@ mouse_set_cursor(PyObject *self, PyObject *args, PyObject *kwds) else if (surfobj) { return _set_color_cursor(spotx, spoty, surfobj); } - return RAISE(PyExc_ValueError, - "Invalid cursor format: no valid template found"); + return RAISERETURN(PyExc_ValueError, + "Invalid cursor format: no valid template found", NULL); } // mouse.get_cursor goes through a python layer first, see cursors.py @@ -597,7 +601,7 @@ mouse_get_cursor(PyObject *self, PyObject *_null) return Py_BuildValue("(ii)O", cursor_data.spotx, cursor_data.spoty, cursor_data.surfobj); } - return RAISE(pgExc_SDLError, "Cursor not found"); + return RAISERETURN(pgExc_SDLError, "Cursor not found", NULL); } static PyObject * @@ -621,15 +625,15 @@ mouse_set_relative_mode(PyObject *self, PyObject *arg) #if SDL_VERSION_ATLEAST(3, 0, 0) SDL_Window *win = pg_GetDefaultWindow(); if (!win) { - return RAISE(pgExc_SDLError, - "display.set_mode has not been called yet."); + return RAISERETURN(pgExc_SDLError, + "display.set_mode has not been called yet.", NULL); } if (!SDL_SetWindowRelativeMouseMode(win, (bool)mode)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } #else if (SDL_SetRelativeMouseMode((SDL_bool)mode)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } #endif Py_RETURN_NONE; diff --git a/src_c/music.c b/src_c/music.c index 5cc41390bf..13ce489e9f 100644 --- a/src_c/music.c +++ b/src_c/music.c @@ -91,7 +91,7 @@ music_play(PyObject *self, PyObject *args, PyObject *keywds) MIXER_INIT_CHECK(); if (!current_music) { - return RAISE(pgExc_SDLError, "music not loaded"); + return RAISERETURN(pgExc_SDLError, "music not loaded", NULL); } Py_BEGIN_ALLOW_THREADS; @@ -106,7 +106,7 @@ music_play(PyObject *self, PyObject *args, PyObject *keywds) Mix_VolumeMusic(volume); Py_END_ALLOW_THREADS; if (val == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; @@ -236,7 +236,8 @@ music_set_pos(PyObject *self, PyObject *arg) double pos = PyFloat_AsDouble(arg); if (pos == -1 && PyErr_Occurred()) { PyErr_Clear(); - return RAISE(PyExc_TypeError, "set_pos expects 1 float argument"); + return RAISERETURN(PyExc_TypeError, "set_pos expects 1 float argument", + NULL); } MIXER_INIT_CHECK(); @@ -246,7 +247,7 @@ music_set_pos(PyObject *self, PyObject *arg) Py_END_ALLOW_THREADS; if (position_set == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; @@ -409,7 +410,7 @@ _load_music(PyObject *obj, char *namehint) } if (!new_music) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return new_music; @@ -530,9 +531,10 @@ music_get_metadata(PyObject *self, PyObject *args, PyObject *keywds) } } else if (namehint) { - return RAISE( + return RAISERETURN( pgExc_SDLError, - "'namehint' specified without specifying 'filename' or 'fileobj'"); + "'namehint' specified without specifying 'filename' or 'fileobj'", + NULL); } const char *title = ""; @@ -548,7 +550,7 @@ music_get_metadata(PyObject *self, PyObject *args, PyObject *keywds) #endif if (!music) { - return RAISE(pgExc_SDLError, "music not loaded"); + return RAISERETURN(pgExc_SDLError, "music not loaded", NULL); } meta_dict = Py_BuildValue("{ss ss ss ss}", "title", title, "album", album, diff --git a/src_c/pixelarray.c b/src_c/pixelarray.c index 1ccc9e5125..09fd6ab268 100644 --- a/src_c/pixelarray.c +++ b/src_c/pixelarray.c @@ -357,12 +357,13 @@ _pxarray_new(PyTypeObject *type, PyObject *args, PyObject *kwds) stride1 = (Py_ssize_t)surf->pitch; pixels = surf->pixels; if (pixels == NULL) { - return RAISE(PyExc_ValueError, - "Cannot create pixelarray on zero-sized Surface"); + return RAISERETURN(PyExc_ValueError, + "Cannot create pixelarray on zero-sized Surface", + NULL); } if (stride0 < 1 || stride0 > 4) { - return RAISE(PyExc_ValueError, - "unsupported bit depth for reference array"); + return RAISERETURN(PyExc_ValueError, + "unsupported bit depth for reference array", NULL); } return (PyObject *)_pxarray_new_internal(type, surfobj, 0, pixels, dim0, @@ -428,7 +429,8 @@ static pgSurfaceObject * _pxarray_get_surface(pgPixelArrayObject *self, void *closure) { if (self->surface == NULL) { - return RAISE(PyExc_ValueError, "Operation on closed PixelArray."); + return RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", + NULL); } Py_INCREF(self->surface); return self->surface; @@ -442,7 +444,8 @@ static PyObject * _pxarray_get_itemsize(pgPixelArrayObject *self, void *closure) { if (self->surface == NULL) { - return RAISE(PyExc_ValueError, "Operation on closed PixelArray."); + return RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", + NULL); } SDL_Surface *surf = pgSurface_AsSurface(self->surface); @@ -655,7 +658,8 @@ _pxarray_repr(pgPixelArrayObject *array) Uint8 *pixel_p; if (array->surface == NULL) { - return RAISE(PyExc_ValueError, "Operation on closed PixelArray."); + return RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", + NULL); } surf = pgSurface_AsSurface(array->surface); @@ -812,7 +816,8 @@ _pxarray_subscript_internal(pgPixelArrayObject *array, Py_ssize_t xstart, Py_ssize_t dy = ystop - ystart; if (array->surface == NULL) { - return RAISE(PyExc_ValueError, "Operation on closed PixelArray."); + return RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", + NULL); } if (!array->shape[1]) { @@ -854,14 +859,15 @@ _array_slice_internal(pgPixelArrayObject *array, Py_ssize_t start, Py_ssize_t end, Py_ssize_t step) { if (array->surface == NULL) { - return RAISE(PyExc_ValueError, "Operation on closed PixelArray."); + return RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", + NULL); } if (end == start) { - return RAISE(PyExc_IndexError, "array size must not be 0"); + return RAISERETURN(PyExc_IndexError, "array size must not be 0", NULL); } if (start >= array->shape[0]) { - return RAISE(PyExc_IndexError, "array index out of range"); + return RAISERETURN(PyExc_IndexError, "array index out of range", NULL); } return _pxarray_subscript_internal(array, start, end, step, 0, array->shape[1], 1); @@ -885,16 +891,18 @@ static PyObject * _pxarray_item(pgPixelArrayObject *array, Py_ssize_t index) { if (array->surface == NULL) { - return RAISE(PyExc_ValueError, "Operation on closed PixelArray."); + return RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", + NULL); } if (index < 0) { index = array->shape[0] - index; if (index < 0) { - return RAISE(PyExc_IndexError, "array index out of range"); + return RAISERETURN(PyExc_IndexError, "array index out of range", + NULL); } } if (index >= array->shape[0]) { - return RAISE(PyExc_IndexError, "array index out of range"); + return RAISERETURN(PyExc_IndexError, "array index out of range", NULL); } return _pxarray_subscript_internal(array, index, 0, 0, 0, array->shape[1], 1); @@ -1611,7 +1619,8 @@ _pxarray_subscript(pgPixelArrayObject *array, PyObject *op) return (PyObject *)array; } if (size > 2 || (size == 2 && !dim1)) { - return RAISE(PyExc_IndexError, "too many indices for the array"); + return RAISERETURN(PyExc_IndexError, + "too many indices for the array", NULL); } obj = PyTuple_GET_ITEM(op, 0); @@ -1672,7 +1681,8 @@ _pxarray_subscript(pgPixelArrayObject *array, PyObject *op) return 0; } if (slicelen < 0) { - return RAISE(PyExc_IndexError, "Unable to handle negative slice"); + return RAISERETURN(PyExc_IndexError, + "Unable to handle negative slice", NULL); } if (slicelen == 0) { Py_RETURN_NONE; @@ -1690,13 +1700,14 @@ _pxarray_subscript(pgPixelArrayObject *array, PyObject *op) i += dim0; } if (i < 0 || i >= dim0) { - return RAISE(PyExc_IndexError, "array index out of range"); + return RAISERETURN(PyExc_IndexError, "array index out of range", + NULL); } return _pxarray_subscript_internal(array, i, i + 1, 0, 0, dim1, 1); } - return RAISE(PyExc_TypeError, - "index must be an integer, sequence or slice"); + return RAISERETURN(PyExc_TypeError, + "index must be an integer, sequence or slice", NULL); } static int @@ -1858,7 +1869,7 @@ pgPixelArray_New(PyObject *surfobj) Uint8 *pixels; if (!pgSurface_Check(surfobj)) { - return RAISE(PyExc_TypeError, "argument is not a Surface"); + return RAISERETURN(PyExc_TypeError, "argument is not a Surface", NULL); } surf = pgSurface_AsSurface(surfobj); @@ -1868,8 +1879,8 @@ pgPixelArray_New(PyObject *surfobj) stride1 = (Py_ssize_t)surf->pitch; pixels = surf->pixels; if (stride0 < 1 || stride0 > 4) { - return RAISE(PyExc_ValueError, - "unsupported bit depth for reference array"); + return RAISERETURN(PyExc_ValueError, + "unsupported bit depth for reference array", NULL); } return (PyObject *)_pxarray_new_internal( diff --git a/src_c/pixelarray_methods.c b/src_c/pixelarray_methods.c index 65a98edc26..344326b4e2 100644 --- a/src_c/pixelarray_methods.c +++ b/src_c/pixelarray_methods.c @@ -59,7 +59,8 @@ _get_single_pixel(pgPixelArrayObject *array, Py_ssize_t x, Py_ssize_t y) Uint32 pixel; if (array->surface == NULL) { - return RAISE(PyExc_ValueError, "Operation on closed PixelArray."); + return RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", + NULL); } pixel_p = (array->pixels + x * array->strides[0] + y * array->strides[1]); surf = pgSurface_AsSurface(array->surface); @@ -119,7 +120,8 @@ _make_surface(pgPixelArrayObject *array, PyObject *args) Uint8 *new_pixel_p; if (array->surface == NULL) { - return RAISE(PyExc_ValueError, "Operation on closed PixelArray."); + return RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", + NULL); } surf = pgSurface_AsSurface(array->surface); @@ -132,7 +134,7 @@ _make_surface(pgPixelArrayObject *array, PyObject *args) if (!same_dims) { if (!(temp_surf = PG_CreateSurface((int)dim0, (int)dim1, PG_SURF_FORMATENUM(surf)))) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } @@ -143,7 +145,7 @@ _make_surface(pgPixelArrayObject *array, PyObject *args) } if (!new_surf) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } new_surface = pgSurface_New(new_surf); @@ -359,7 +361,8 @@ _replace_color(pgPixelArrayObject *array, PyObject *args, PyObject *kwds) static char *keys[] = {"color", "repcolor", "distance", "weights", NULL}; if (array->surface == NULL) { - return RAISE(PyExc_ValueError, "Operation on closed PixelArray."); + return RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", + NULL); } surf = pgSurface_AsSurface(array->surface); @@ -369,14 +372,15 @@ _replace_color(pgPixelArrayObject *array, PyObject *args, PyObject *kwds) } if (distance < 0 || distance > 1) { - return RAISE(PyExc_ValueError, - "distance must be in the range from 0.0 to 1.0"); + return RAISERETURN(PyExc_ValueError, + "distance must be in the range from 0.0 to 1.0", + NULL); } PG_PixelFormat *format; SDL_Palette *palette; if (!PG_GetSurfaceDetails(surf, &format, &palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } bpp = PG_FORMAT_BytesPerPixel(format); @@ -547,7 +551,8 @@ _extract_color(pgPixelArrayObject *array, PyObject *args, PyObject *kwds) static char *keys[] = {"color", "distance", "weights", NULL}; if (array->surface == NULL) { - return RAISE(PyExc_ValueError, "Operation on closed PixelArray."); + return RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", + NULL); } if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|fO", keys, &excolor, @@ -556,8 +561,9 @@ _extract_color(pgPixelArrayObject *array, PyObject *args, PyObject *kwds) } if (distance < 0 || distance > 1) { - return RAISE(PyExc_ValueError, - "distance must be in the range from 0.0 to 1.0"); + return RAISERETURN(PyExc_ValueError, + "distance must be in the range from 0.0 to 1.0", + NULL); } if (!_get_weights(weights, &wr, &wg, &wb)) { @@ -581,7 +587,7 @@ _extract_color(pgPixelArrayObject *array, PyObject *args, PyObject *kwds) PG_PixelFormat *format; SDL_Palette *palette; if (!PG_GetSurfaceDetails(surf, &format, &palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } bpp = PG_FORMAT_BytesPerPixel(format); @@ -788,7 +794,8 @@ _compare(pgPixelArrayObject *array, PyObject *args, PyObject *kwds) static char *keys[] = {"array", "distance", "weights", NULL}; if (array->surface == NULL) { - return RAISE(PyExc_ValueError, "Operation on closed PixelArray."); + return RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", + NULL); } surf = pgSurface_AsSurface(array->surface); @@ -799,8 +806,9 @@ _compare(pgPixelArrayObject *array, PyObject *args, PyObject *kwds) } if (distance < 0.0 || distance > 1.0) { - return RAISE(PyExc_ValueError, - "distance must be in the range from 0.0 to 1.0"); + return RAISERETURN(PyExc_ValueError, + "distance must be in the range from 0.0 to 1.0", + NULL); } if (!_get_weights(weights, &wr, &wg, &wb)) { @@ -817,10 +825,10 @@ _compare(pgPixelArrayObject *array, PyObject *args, PyObject *kwds) PG_PixelFormat *format, *other_format; SDL_Palette *palette, *other_palette; if (!PG_GetSurfaceDetails(surf, &format, &palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (!PG_GetSurfaceDetails(other_surf, &other_format, &other_palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } bpp = PG_SURF_BytesPerPixel(surf); @@ -1045,7 +1053,8 @@ static PyObject * _transpose(pgPixelArrayObject *array, PyObject *args) { if (array->surface == NULL) { - return RAISE(PyExc_ValueError, "Operation on closed PixelArray."); + return RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", + NULL); } SDL_Surface *surf = pgSurface_AsSurface(array->surface); @@ -1070,7 +1079,8 @@ static PyObject * _close_array(pgPixelArrayObject *array, PyObject *args) { if (array->surface == NULL) { - return RAISE(PyExc_ValueError, "Operation on closed PixelArray."); + return RAISERETURN(PyExc_ValueError, "Operation on closed PixelArray.", + NULL); } _cleanup_array(array); Py_RETURN_NONE; diff --git a/src_c/pixelcopy.c b/src_c/pixelcopy.c index 3ae7b344fa..7be5af0d81 100644 --- a/src_c/pixelcopy.c +++ b/src_c/pixelcopy.c @@ -495,11 +495,13 @@ array_to_surface(PyObject *self, PyObject *arg) } if (!(view_p->ndim == 2 || (view_p->ndim == 3 && view_p->shape[2] == 3))) { - return RAISE(PyExc_ValueError, "must be a valid 2d or 3d array\n"); + return RAISERETURN(PyExc_ValueError, + "must be a valid 2d or 3d array\n", NULL); } if (PG_SURF_BytesPerPixel(surf) == 0 || PG_SURF_BytesPerPixel(surf) > 4) { - return RAISE(PyExc_ValueError, "unsupported bit depth for surface"); + return RAISERETURN(PyExc_ValueError, + "unsupported bit depth for surface", NULL); } stridex = view_p->strides[0]; @@ -518,7 +520,7 @@ array_to_surface(PyObject *self, PyObject *arg) PG_PixelFormat *format; SDL_Palette *palette; if (!PG_GetSurfaceDetails(surf, &format, &palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Rshift = format->Rshift; @@ -542,7 +544,8 @@ array_to_surface(PyObject *self, PyObject *arg) if (sizex != surf->w || sizey != surf->h) { pgBuffer_Release(&pg_view); - return RAISE(PyExc_ValueError, "array must match surface dimensions"); + return RAISERETURN(PyExc_ValueError, + "array must match surface dimensions", NULL); } if (!pgSurface_LockBy(surfobj, arrayobj)) { pgBuffer_Release(&pg_view); @@ -572,8 +575,9 @@ array_to_surface(PyObject *self, PyObject *arg) if (!pgSurface_UnlockBy(surfobj, arrayobj)) { return NULL; } - return RAISE(PyExc_ValueError, - "unsupported datatype for array\n"); + return RAISERETURN(PyExc_ValueError, + "unsupported datatype for array\n", + NULL); } } else { @@ -581,8 +585,8 @@ array_to_surface(PyObject *self, PyObject *arg) if (!pgSurface_UnlockBy(surfobj, arrayobj)) { return NULL; } - return RAISE(PyExc_ValueError, - "unsupported datatype for array\n"); + return RAISERETURN(PyExc_ValueError, + "unsupported datatype for array\n", NULL); } break; case 2: @@ -602,8 +606,9 @@ array_to_surface(PyObject *self, PyObject *arg) if (!pgSurface_UnlockBy(surfobj, arrayobj)) { return NULL; } - return RAISE(PyExc_ValueError, - "unsupported datatype for array\n"); + return RAISERETURN(PyExc_ValueError, + "unsupported datatype for array\n", + NULL); } } else { @@ -629,8 +634,9 @@ array_to_surface(PyObject *self, PyObject *arg) if (!pgSurface_UnlockBy(surfobj, arrayobj)) { return NULL; } - return RAISE(PyExc_ValueError, - "unsupported datatype for array\n"); + return RAISERETURN(PyExc_ValueError, + "unsupported datatype for array\n", + NULL); } } break; @@ -704,8 +710,8 @@ array_to_surface(PyObject *self, PyObject *arg) if (!pgSurface_UnlockBy(surfobj, arrayobj)) { return NULL; } - return RAISE(PyExc_ValueError, - "unsupported datatype for array\n"); + return RAISERETURN(PyExc_ValueError, + "unsupported datatype for array\n", NULL); } break; case 4: @@ -722,8 +728,9 @@ array_to_surface(PyObject *self, PyObject *arg) if (!pgSurface_UnlockBy(surfobj, arrayobj)) { return NULL; } - return RAISE(PyExc_ValueError, - "unsupported datatype for array\n"); + return RAISERETURN(PyExc_ValueError, + "unsupported datatype for array\n", + NULL); } } else { @@ -749,8 +756,9 @@ array_to_surface(PyObject *self, PyObject *arg) if (!pgSurface_UnlockBy(surfobj, arrayobj)) { return NULL; } - return RAISE(PyExc_ValueError, - "unsupported datatype for array\n"); + return RAISERETURN(PyExc_ValueError, + "unsupported datatype for array\n", + NULL); } } break; @@ -759,8 +767,8 @@ array_to_surface(PyObject *self, PyObject *arg) if (!pgSurface_UnlockBy(surfobj, arrayobj)) { return NULL; } - return RAISE(PyExc_RuntimeError, - "unsupported bit depth for image"); + return RAISERETURN(PyExc_RuntimeError, + "unsupported bit depth for image", NULL); } pgBuffer_Release(&pg_view); @@ -982,7 +990,7 @@ map_array(PyObject *self, PyObject *args) SDL_Palette *palette; if (!PG_GetSurfaceDetails(pgSurface_AsSurface(format_surf), &format, &palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } pix_bytesize = PG_FORMAT_BytesPerPixel(format); @@ -1170,7 +1178,8 @@ make_surface(PyObject *self, PyObject *arg) if (!(view_p->ndim == 2 || (view_p->ndim == 3 && view_p->shape[2] == 3))) { pgBuffer_Release(&pg_view); - return RAISE(PyExc_ValueError, "must be a valid 2d or 3d array\n"); + return RAISERETURN(PyExc_ValueError, + "must be a valid 2d or 3d array\n", NULL); } if (_validate_view_format(view_p->format)) { pgBuffer_Release(&pg_view); @@ -1189,7 +1198,7 @@ make_surface(PyObject *self, PyObject *arg) surf = PG_CreateSurface(sizex, sizey, pixelformat); if (!surf) { pgBuffer_Release(&pg_view); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } #if SDL_VERSION_ATLEAST(3, 0, 0) SDL_Palette *palette = SDL_GetSurfacePalette(surf); diff --git a/src_c/rect.c b/src_c/rect.c index 52fe58a3c7..d6182a3ca7 100644 --- a/src_c/rect.c +++ b/src_c/rect.c @@ -608,8 +608,8 @@ pg_frect_repr(pgFRectObject *self) int ret = PyOS_snprintf(str, 256, "FRect(%f, %f, %f, %f)", self->r.x, self->r.y, self->r.w, self->r.h); if (ret < 0 || ret >= 256) { - return RAISE(PyExc_RuntimeError, - "Internal PyOS_snprintf call failed!"); + return RAISERETURN(PyExc_RuntimeError, + "Internal PyOS_snprintf call failed!", NULL); } return PyUnicode_FromString(str); diff --git a/src_c/rect_impl.h b/src_c/rect_impl.h index a36836431c..c0ae9e03a5 100644 --- a/src_c/rect_impl.h +++ b/src_c/rect_impl.h @@ -1027,13 +1027,15 @@ RectExport_moveTo(RectObject *self, PyObject *const *args, Py_ssize_t nargs, if (nargs > 0) { Py_DECREF(rect); - return RAISE(PyExc_TypeError, "move_to takes no positional arguments"); + return RAISERETURN(PyExc_TypeError, + "move_to takes no positional arguments", NULL); } if (!kwnames) { Py_DECREF(rect); - return RAISE(PyExc_TypeError, - "move_to cannot be called without keyword arguments"); + return RAISERETURN( + PyExc_TypeError, + "move_to cannot be called without keyword arguments", NULL); } Py_ssize_t i, sequence_len; @@ -1057,7 +1059,8 @@ RectExport_inflate(RectObject *self, PyObject *args) PrimitiveType x, y; if (!twoPrimitivesFromObj(args, &x, &y)) { - return RAISE(PyExc_TypeError, "argument must contain two numbers"); + return RAISERETURN(PyExc_TypeError, + "argument must contain two numbers", NULL); } return RectExport_subtypeNew4(Py_TYPE(self), self->r.x - x / 2, @@ -1071,7 +1074,8 @@ RectExport_inflateIp(RectObject *self, PyObject *args) PrimitiveType x, y; if (!twoPrimitivesFromObj(args, &x, &y)) { - return RAISE(PyExc_TypeError, "argument must contain two numbers"); + return RAISERETURN(PyExc_TypeError, + "argument must contain two numbers", NULL); } self->r.x -= x / 2; self->r.y -= y / 2; @@ -1088,13 +1092,16 @@ RectExport_scalebyIp(RectObject *self, PyObject *args, PyObject *kwargs) kwargs ? PyDict_GetItemString(kwargs, "scale_by") : NULL; if (scale_by) { if (PyDict_Size(kwargs) > 1) { - return RAISE(PyExc_TypeError, - "The 'scale_by' keyword cannot be combined with " - "other arguments."); + return RAISERETURN( + PyExc_TypeError, + "The 'scale_by' keyword cannot be combined with " + "other arguments.", + NULL); } if (!pg_TwoFloatsFromObj(scale_by, &factor_x, &factor_y)) { - return RAISE(PyExc_TypeError, - "The 'scale_by' argument must be a number pair"); + return RAISERETURN(PyExc_TypeError, + "The 'scale_by' argument must be a number pair", + NULL); } } else { @@ -1107,16 +1114,19 @@ RectExport_scalebyIp(RectObject *self, PyObject *args, PyObject *kwargs) if (!pg_TwoFloatsFromObj(arg_x, &factor_x, &factor_y)) { /* Not a sequence, so try handling int separately */ if (!pg_FloatFromObj(arg_x, &factor_x)) { - return RAISE(PyExc_TypeError, "Argument 'x' must be a number"); + return RAISERETURN(PyExc_TypeError, + "Argument 'x' must be a number", NULL); } if (arg_y && !pg_FloatFromObj(arg_y, &factor_y)) { - return RAISE(PyExc_TypeError, "Argument 'y' must be a number"); + return RAISERETURN(PyExc_TypeError, + "Argument 'y' must be a number", NULL); } } else if (arg_y) { - return RAISE( + return RAISERETURN( PyExc_TypeError, - "Cannot pass argument 'y' after passing a sequence scale"); + "Cannot pass argument 'y' after passing a sequence scale", + NULL); } } @@ -1153,7 +1163,8 @@ RectExport_update(RectObject *self, PyObject *const *args, Py_ssize_t nargs) InnerRect *argrect, temp; if (!(argrect = RectFromFastcallArgs(args, nargs, &temp))) { - return RAISE(PyExc_TypeError, "Argument must be rect style object"); + return RAISERETURN(PyExc_TypeError, + "Argument must be rect style object", NULL); } self->r = *argrect; @@ -1168,7 +1179,8 @@ RectExport_union(RectObject *self, PyObject *const *args, Py_ssize_t nargs) PrimitiveType x, y, w, h; if (!(argrect = RectFromFastcallArgs(args, nargs, &temp))) { - return RAISE(PyExc_TypeError, "Argument must be rect style object"); + return RAISERETURN(PyExc_TypeError, + "Argument must be rect style object", NULL); } x = MIN(self->r.x, argrect->x); y = MIN(self->r.y, argrect->y); @@ -1184,7 +1196,8 @@ RectExport_unionIp(RectObject *self, PyObject *const *args, Py_ssize_t nargs) PrimitiveType x, y, w, h; if (!(argrect = RectFromFastcallArgs(args, nargs, &temp))) { - return RAISE(PyExc_TypeError, "Argument must be rect style object"); + return RAISERETURN(PyExc_TypeError, + "Argument must be rect style object", NULL); } x = MIN(self->r.x, argrect->x); @@ -1207,8 +1220,9 @@ RectExport_unionall(RectObject *self, PyObject *arg) PrimitiveType t, l, b, r; if (!PySequence_Check(arg)) { - return RAISE(PyExc_TypeError, - "Argument must be a sequence of rectstyle objects."); + return RAISERETURN(PyExc_TypeError, + "Argument must be a sequence of rectstyle objects.", + NULL); } l = self->r.x; @@ -1227,9 +1241,9 @@ RectExport_unionall(RectObject *self, PyObject *arg) for (loop = 0; loop < size; ++loop) { if (!(argrect = RectFromObject(items[loop], &temp))) { - return RAISE( + return RAISERETURN( PyExc_TypeError, - "Argument must be a sequence of rectstyle objects."); + "Argument must be a sequence of rectstyle objects.", NULL); } l = MIN(l, argrect->x); t = MIN(t, argrect->y); @@ -1252,9 +1266,9 @@ RectExport_unionall(RectObject *self, PyObject *arg) obj = PySequence_ITEM(arg, loop); if (!obj || !(argrect = RectFromObject(obj, &temp))) { Py_XDECREF(obj); - return RAISE( + return RAISERETURN( PyExc_TypeError, - "Argument must be a sequence of rectstyle objects."); + "Argument must be a sequence of rectstyle objects.", NULL); } l = MIN(l, argrect->x); t = MIN(t, argrect->y); @@ -1276,8 +1290,9 @@ RectExport_unionallIp(RectObject *self, PyObject *arg) PrimitiveType t, l, b, r; if (!PySequence_Check(arg)) { - return RAISE(PyExc_TypeError, - "Argument must be a sequence of rectstyle objects."); + return RAISERETURN(PyExc_TypeError, + "Argument must be a sequence of rectstyle objects.", + NULL); } l = self->r.x; @@ -1296,9 +1311,9 @@ RectExport_unionallIp(RectObject *self, PyObject *arg) for (loop = 0; loop < size; ++loop) { if (!(argrect = RectFromObject(items[loop], &temp))) { - return RAISE( + return RAISERETURN( PyExc_TypeError, - "Argument must be a sequence of rectstyle objects."); + "Argument must be a sequence of rectstyle objects.", NULL); } l = MIN(l, argrect->x); t = MIN(t, argrect->y); @@ -1321,9 +1336,9 @@ RectExport_unionallIp(RectObject *self, PyObject *arg) obj = PySequence_ITEM(arg, loop); if (!obj || !(argrect = RectFromObject(obj, &temp))) { Py_XDECREF(obj); - return RAISE( + return RAISERETURN( PyExc_TypeError, - "Argument must be a sequence of rectstyle objects."); + "Argument must be a sequence of rectstyle objects.", NULL); } l = MIN(l, argrect->x); t = MIN(t, argrect->y); @@ -1366,7 +1381,8 @@ RectExport_colliderect(RectObject *self, PyObject *const *args, InnerRect *argrect, temp; if (!(argrect = RectFromFastcallArgs(args, nargs, &temp))) { - return RAISE(PyExc_TypeError, "Argument must be rect style object"); + return RAISERETURN(PyExc_TypeError, + "Argument must be rect style object", NULL); } return PyBool_FromLong(_pg_do_rects_intersect(&self->r, argrect)); @@ -1407,8 +1423,9 @@ RectExport_collidelist(RectObject *self, PyObject *arg) } if (!PySequence_Check(arg)) { - return RAISE(PyExc_TypeError, - "Argument must be a sequence of rectstyle objects."); + return RAISERETURN(PyExc_TypeError, + "Argument must be a sequence of rectstyle objects.", + NULL); } OPTIMIZED_COLLIDERECT_SETUP; @@ -1419,9 +1436,9 @@ RectExport_collidelist(RectObject *self, PyObject *arg) PyObject **items = PySequence_Fast_ITEMS(arg); for (loop = 0; loop < PySequence_Fast_GET_SIZE(arg); loop++) { if (!(argrect = RectFromObject(items[loop], &temp))) { - return RAISE( + return RAISERETURN( PyExc_TypeError, - "Argument must be a sequence of rectstyle objects."); + "Argument must be a sequence of rectstyle objects.", NULL); } if (OPTIMIZED_COLLIDERECT(argrect)) { @@ -1437,9 +1454,9 @@ RectExport_collidelist(RectObject *self, PyObject *arg) if (!obj || !(argrect = RectFromObject(obj, &temp))) { Py_XDECREF(obj); - return RAISE( + return RAISERETURN( PyExc_TypeError, - "Argument must be a sequence of rectstyle objects."); + "Argument must be a sequence of rectstyle objects.", NULL); } Py_DECREF(obj); @@ -1461,8 +1478,9 @@ RectExport_collidelistall(RectObject *self, PyObject *arg) PyObject *ret = NULL; if (!PySequence_Check(arg)) { - return RAISE(PyExc_TypeError, - "Argument must be a sequence of rectstyle objects."); + return RAISERETURN(PyExc_TypeError, + "Argument must be a sequence of rectstyle objects.", + NULL); } if (!(ret = PyList_New(0))) { @@ -1484,9 +1502,9 @@ RectExport_collidelistall(RectObject *self, PyObject *arg) for (loop = 0; loop < PySequence_Fast_GET_SIZE(arg); loop++) { if (!(argrect = RectFromObject(items[loop], &temp))) { Py_DECREF(ret); - return RAISE( + return RAISERETURN( PyExc_TypeError, - "Argument must be a sequence of rectstyle objects."); + "Argument must be a sequence of rectstyle objects.", NULL); } if (OPTIMIZED_COLLIDERECT(argrect)) { @@ -1512,9 +1530,9 @@ RectExport_collidelistall(RectObject *self, PyObject *arg) if (!obj || !(argrect = RectFromObject(obj, &temp))) { Py_XDECREF(obj); Py_DECREF(ret); - return RAISE( + return RAISERETURN( PyExc_TypeError, - "Argument must be a sequence of rectstyle objects."); + "Argument must be a sequence of rectstyle objects.", NULL); } Py_DECREF(obj); @@ -1551,16 +1569,18 @@ RectExport_RectFromObjectAndKeyFunc(PyObject *obj, PyObject *keyfunc, InnerRect *ret = RectFromObject(obj_with_rect, temp); Py_DECREF(obj_with_rect); if (!ret) { - return RAISE(PyExc_TypeError, - "Key function must return rect or rect-like objects"); + return RAISERETURN( + PyExc_TypeError, + "Key function must return rect or rect-like objects", NULL); } return ret; } else { InnerRect *ret = RectFromObject(obj, temp); if (!ret) { - return RAISE(PyExc_TypeError, - "Sequence must contain rect or rect-like objects"); + return RAISERETURN( + PyExc_TypeError, + "Sequence must contain rect or rect-like objects", NULL); } return ret; } @@ -1585,8 +1605,8 @@ RectExport_collideobjectsall(RectObject *self, PyObject *args, } if (!PySequence_Check(list)) { - return RAISE(PyExc_TypeError, - "Argument must be a sequence of objects."); + return RAISERETURN(PyExc_TypeError, + "Argument must be a sequence of objects.", NULL); } if (keyfunc == Py_None) { @@ -1594,8 +1614,9 @@ RectExport_collideobjectsall(RectObject *self, PyObject *args, } if (keyfunc && !PyCallable_Check(keyfunc)) { - return RAISE(PyExc_TypeError, - "Key function must be callable with one argument."); + return RAISERETURN(PyExc_TypeError, + "Key function must be callable with one argument.", + NULL); } ret = PyList_New(0); @@ -1662,8 +1683,8 @@ RectExport_collideobjects(RectObject *self, PyObject *args, PyObject *kwargs) } if (!PySequence_Check(list)) { - return RAISE(PyExc_TypeError, - "Argument must be a sequence of objects."); + return RAISERETURN(PyExc_TypeError, + "Argument must be a sequence of objects.", NULL); } if (keyfunc == Py_None) { @@ -1671,8 +1692,9 @@ RectExport_collideobjects(RectObject *self, PyObject *args, PyObject *kwargs) } if (keyfunc && !PyCallable_Check(keyfunc)) { - return RAISE(PyExc_TypeError, - "Key function must be callable with one argument."); + return RAISERETURN(PyExc_TypeError, + "Key function must be callable with one argument.", + NULL); } /* If the calling rect has 0 width or height, it cannot collide with @@ -1727,7 +1749,8 @@ RectExport_collidedict(RectObject *self, PyObject *args, PyObject *kwargs) } if (!PyDict_Check(dict)) { - return RAISE(PyExc_TypeError, "first argument must be a dict"); + return RAISERETURN(PyExc_TypeError, "first argument must be a dict", + NULL); } /* If the calling rect has 0 width or height, it cannot collide with @@ -1741,13 +1764,14 @@ RectExport_collidedict(RectObject *self, PyObject *args, PyObject *kwargs) while (PyDict_Next(dict, &loop, &key, &val)) { if (values) { if (!(argrect = RectFromObject(val, &temp))) { - return RAISE(PyExc_TypeError, - "dict must have rectstyle values"); + return RAISERETURN(PyExc_TypeError, + "dict must have rectstyle values", NULL); } } else { if (!(argrect = RectFromObject(key, &temp))) { - return RAISE(PyExc_TypeError, "dict must have rectstyle keys"); + return RAISERETURN(PyExc_TypeError, + "dict must have rectstyle keys", NULL); } } @@ -1780,7 +1804,8 @@ RectExport_collidedictall(RectObject *self, PyObject *args, PyObject *kwargs) } if (!PyDict_Check(dict)) { - return RAISE(PyExc_TypeError, "first argument must be a dict"); + return RAISERETURN(PyExc_TypeError, "first argument must be a dict", + NULL); } ret = PyList_New(0); @@ -1800,14 +1825,15 @@ RectExport_collidedictall(RectObject *self, PyObject *args, PyObject *kwargs) if (values) { if (!(argrect = RectFromObject(val, &temp))) { Py_DECREF(ret); - return RAISE(PyExc_TypeError, - "dict must have rectstyle values"); + return RAISERETURN(PyExc_TypeError, + "dict must have rectstyle values", NULL); } } else { if (!(argrect = RectFromObject(key, &temp))) { Py_DECREF(ret); - return RAISE(PyExc_TypeError, "dict must have rectstyle keys"); + return RAISERETURN(PyExc_TypeError, + "dict must have rectstyle keys", NULL); } } @@ -1837,7 +1863,8 @@ RectExport_clip(RectObject *self, PyObject *const *args, Py_ssize_t nargs) A = &self->r; if (!(B = RectFromFastcallArgs(args, nargs, &temp))) { - return RAISE(PyExc_TypeError, "Argument must be rect style object"); + return RAISERETURN(PyExc_TypeError, + "Argument must be rect style object", NULL); } x = MAX(A->x, B->x); @@ -1883,36 +1910,39 @@ RectExport_clipline(RectObject *self, PyObject *const *args, Py_ssize_t nargs) else if (nargs == 2) { /* Handles format: clipline((x1, y1), (x2, y2)) */ if (!twoPrimitivesFromObj(args[0], &x1, &y1)) { - return RAISE(PyExc_TypeError, - "number pair expected for first argument"); + return RAISERETURN(PyExc_TypeError, + "number pair expected for first argument", + NULL); } /* Get the other end of the line. */ if (!twoPrimitivesFromObj(args[1], &x2, &y2)) { - return RAISE(PyExc_TypeError, - "number pair expected for second argument"); + return RAISERETURN(PyExc_TypeError, + "number pair expected for second argument", + NULL); } } else if (nargs == 4) { /* Handles format: clipline(x1, y1, x2, y2) */ if (!PrimitiveFromObj(args[0], &x1)) { - return RAISE(PyExc_TypeError, - "number expected for first argument"); + return RAISERETURN(PyExc_TypeError, + "number expected for first argument", NULL); } if (!PrimitiveFromObj(args[1], &y1)) { - return RAISE(PyExc_TypeError, - "number expected for second argument"); + return RAISERETURN(PyExc_TypeError, + "number expected for second argument", NULL); } if (!PrimitiveFromObj(args[2], &x2)) { - return RAISE(PyExc_TypeError, - "number expected for third argument"); + return RAISERETURN(PyExc_TypeError, + "number expected for third argument", NULL); } if (!PrimitiveFromObj(args[3], &y2)) { - return RAISE(PyExc_TypeError, - "number expected for fourth argument"); + return RAISERETURN(PyExc_TypeError, + "number expected for fourth argument", NULL); } } else { - return RAISE(PyExc_TypeError, "clipline() takes 1, 2, or 4 arguments"); + return RAISERETURN(PyExc_TypeError, + "clipline() takes 1, 2, or 4 arguments", NULL); } if ((self->r.w < 0) || (self->r.h < 0)) { @@ -1920,7 +1950,8 @@ RectExport_clipline(RectObject *self, PyObject *const *args, Py_ssize_t nargs) rect_copy = &pgRectAsRect(RectExport_RectNew(&self->r)); if (rect_copy == NULL) { - return RAISE(PyExc_MemoryError, "cannot allocate memory for rect"); + return RAISERETURN(PyExc_MemoryError, + "cannot allocate memory for rect", NULL); } RectExport_Normalize(rect_copy); @@ -1980,7 +2011,8 @@ RectExport_contains(RectObject *self, PyObject *const *args, Py_ssize_t nargs) { int result = RectExport_contains_internal(self, args, nargs); if (result == -1) { - return RAISE(PyExc_TypeError, "Argument must be rect style object"); + return RAISERETURN(PyExc_TypeError, + "Argument must be rect style object", NULL); } return PyBool_FromLong(result); } @@ -2009,7 +2041,8 @@ RectExport_clamp(RectObject *self, PyObject *const *args, Py_ssize_t nargs) PrimitiveType x, y; if (!(argrect = RectFromFastcallArgs(args, nargs, &temp))) { - return RAISE(PyExc_TypeError, "Argument must be rect style object"); + return RAISERETURN(PyExc_TypeError, + "Argument must be rect style object", NULL); } if (self->r.w >= argrect->w) { @@ -2049,7 +2082,8 @@ RectExport_fit(RectObject *self, PyObject *const *args, Py_ssize_t nargs) float xratio, yratio, maxratio; if (!(argrect = RectFromFastcallArgs(args, nargs, &temp))) { - return RAISE(PyExc_TypeError, "Argument must be rect style object"); + return RAISERETURN(PyExc_TypeError, + "Argument must be rect style object", NULL); } xratio = (float)self->r.w / (float)argrect->w; @@ -2072,7 +2106,8 @@ RectExport_clampIp(RectObject *self, PyObject *const *args, Py_ssize_t nargs) PrimitiveType x, y; if (!(argrect = RectFromFastcallArgs(args, nargs, &temp))) { - return RAISE(PyExc_TypeError, "Argument must be rect style object"); + return RAISERETURN(PyExc_TypeError, + "Argument must be rect style object", NULL); } if (self->r.w >= argrect->w) { @@ -2135,7 +2170,7 @@ RectExport_item(RectObject *self, Py_ssize_t i) i += 4; } else { - return RAISE(PyExc_IndexError, "Invalid rect Index"); + return RAISERETURN(PyExc_IndexError, "Invalid rect Index", NULL); } } return PythonNumberFromPrimitiveType(data[i]); @@ -2211,7 +2246,7 @@ RectExport_subscript(RectObject *self, PyObject *op) return slice; } - return RAISE(PyExc_TypeError, "Invalid Rect slice"); + return RAISERETURN(PyExc_TypeError, "Invalid Rect slice", NULL); } static int diff --git a/src_c/render.c b/src_c/render.c index b8e6ef5106..979c1cfce0 100644 --- a/src_c/render.c +++ b/src_c/render.c @@ -20,9 +20,9 @@ static PyTypeObject pgImage_Type; #define pgImage_Check(x) (PyObject_IsInstance((x), (PyObject *)&pgImage_Type)) -#define RENDERER_ERROR_CHECK(x) \ - if (x < 0) { \ - return RAISE(pgExc_SDLError, SDL_GetError()); \ +#define RENDERER_ERROR_CHECK(x) \ + if (x < 0) { \ + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); \ } #define RENDERER_PROPERTY_ERROR_CHECK(x) \ @@ -30,9 +30,10 @@ static PyTypeObject pgImage_Type; return RAISERETURN(pgExc_SDLError, SDL_GetError(), -1); \ } -#define PARSE_POINT(obj, x, y, name) \ - if (!pg_TwoFloatsFromObj(obj, &x, &y)) { \ - return RAISE(PyExc_TypeError, "invalid " #name " argument"); \ +#define PARSE_POINT(obj, x, y, name) \ + if (!pg_TwoFloatsFromObj(obj, &x, &y)) { \ + return RAISERETURN(PyExc_TypeError, "invalid " #name " argument", \ + NULL); \ } static void @@ -58,12 +59,12 @@ renderer_from_window(PyTypeObject *cls, PyObject *args, PyObject *kwargs) self->_is_borrowed = SDL_TRUE; } else { - return RAISE(pgExc_SDLError, - "Window is not created from display module"); + return RAISERETURN(pgExc_SDLError, + "Window is not created from display module", NULL); } self->renderer = SDL_GetRenderer(self->window->_win); if (!self->renderer) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } self->target = NULL; Py_INCREF(self); @@ -80,7 +81,7 @@ renderer_draw_point(pgRendererObject *self, PyObject *args, PyObject *kwargs) return NULL; } if (!pg_TwoFloatsFromObj(point, &pos.x, &pos.y)) { - return RAISE(PyExc_TypeError, "invalid argument"); + return RAISERETURN(PyExc_TypeError, "invalid argument", NULL); } RENDERER_ERROR_CHECK(SDL_RenderDrawPointF(self->renderer, pos.x, pos.y)) Py_RETURN_NONE; @@ -113,7 +114,7 @@ renderer_draw_rect(pgRendererObject *self, PyObject *args, PyObject *kwargs) return NULL; } if (!(rect = pgFRect_FromObject(rectobj, &temp))) { - return RAISE(PyExc_TypeError, "rect argument is invalid"); + return RAISERETURN(PyExc_TypeError, "rect argument is invalid", NULL); } RENDERER_ERROR_CHECK(SDL_RenderDrawRectF(self->renderer, rect)) Py_RETURN_NONE; @@ -129,7 +130,7 @@ renderer_fill_rect(pgRendererObject *self, PyObject *args, PyObject *kwargs) return NULL; } if (!(rect = pgFRect_FromObject(rectobj, &temp))) { - return RAISE(PyExc_TypeError, "rect argument is invalid"); + return RAISERETURN(PyExc_TypeError, "rect argument is invalid", NULL); } RENDERER_ERROR_CHECK(SDL_RenderFillRectF(self->renderer, rect)) Py_RETURN_NONE; @@ -178,8 +179,8 @@ renderer_fill_triangle(pgRendererObject *self, PyObject *args, SDL_RenderGeometry(self->renderer, NULL, vertices, 3, NULL, 0)) Py_RETURN_NONE; #else - RAISE(PyExc_TypeError, "fill_triangle() requires SDL 2.0.18 or newer"); - Py_RETURN_NONE; + return RAISERETURN(PyExc_TypeError, + "fill_triangle() requires SDL 2.0.18 or newer", NULL); #endif } @@ -228,8 +229,8 @@ renderer_fill_quad(pgRendererObject *self, PyObject *args, PyObject *kwargs) SDL_RenderGeometry(self->renderer, NULL, vertices, 4, indices, 6)) Py_RETURN_NONE; #else - RAISE(PyExc_TypeError, "fill_quad() requires SDL 2.0.18 or newer"); - Py_RETURN_NONE; + return RAISERETURN(PyExc_TypeError, + "fill_quad() requires SDL 2.0.18 or newer", NULL); #endif } @@ -269,7 +270,8 @@ renderer_set_viewport(pgRendererObject *self, PyObject *args, PyObject *kwargs) } else { if (!(rect = pgRect_FromObject(rectobj, &temp))) { - return RAISE(PyExc_TypeError, "area must be rectangle or None"); + return RAISERETURN(PyExc_TypeError, + "area must be rectangle or None", NULL); } RENDERER_ERROR_CHECK(SDL_RenderSetViewport(self->renderer, rect)) } @@ -308,7 +310,8 @@ renderer_to_surface(pgRendererObject *self, PyObject *args, PyObject *kwargs) } if (!Py_IsNone(rectobj)) { if (!(rect = pgRect_FromObject(rectobj, &temp))) { - return RAISE(PyExc_TypeError, "area must be None or a rect"); + return RAISERETURN(PyExc_TypeError, "area must be None or a rect", + NULL); } SDL_RenderGetViewport(self->renderer, &viewport); SDL_IntersectRect(rect, &viewport, rect); @@ -320,25 +323,27 @@ renderer_to_surface(pgRendererObject *self, PyObject *args, PyObject *kwargs) } if (!Py_IsNone(surfobj)) { if (!(pgSurface_Check(surfobj))) { - return RAISE(PyExc_TypeError, "surface must be None or a Surface"); + return RAISERETURN(PyExc_TypeError, + "surface must be None or a Surface", NULL); } surface = (pgSurfaceObject *)surfobj; Py_INCREF(surface); surf = surface->surf; if (surf->w < rect->w || surf->h < rect->h) { - return RAISE(PyExc_ValueError, "the surface is too small"); + return RAISERETURN(PyExc_ValueError, "the surface is too small", + NULL); } format = surf->format->format; } else { format = SDL_GetWindowPixelFormat(self->window->_win); if (format == SDL_PIXELFORMAT_UNKNOWN) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } surf = SDL_CreateRGBSurfaceWithFormat( 0, rect->w, rect->h, SDL_BITSPERPIXEL(format), format); if (surf == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } surface = pgSurface_New(surf); } diff --git a/src_c/rwobject.c b/src_c/rwobject.c index 7509ca17c9..2e6d387730 100644 --- a/src_c/rwobject.c +++ b/src_c/rwobject.c @@ -224,9 +224,10 @@ pg_EncodeString(PyObject *obj, const char *encoding, const char *errors, else if (encoding == pg_default_encoding && errors == pg_default_errors) { /* The default encoding and error handling should not fail */ - return RAISE(PyExc_SystemError, - "Pygame bug (in pg_EncodeString):" - " unexpected encoding error"); + return RAISERETURN(PyExc_SystemError, + "Pygame bug (in pg_EncodeString):" + " unexpected encoding error", + NULL); } PyErr_Clear(); Py_RETURN_NONE; @@ -483,7 +484,8 @@ pgRWops_FromFileObject(PyObject *obj) pgRWHelper *helper; if (obj == NULL) { - return (SDL_RWops *)RAISE(PyExc_TypeError, "Invalid filetype object"); + return (SDL_RWops *)RAISERETURN(PyExc_TypeError, + "Invalid filetype object", NULL); } helper = PyMem_New(pgRWHelper, 1); @@ -511,18 +513,18 @@ pgRWops_FromFileObject(PyObject *obj) if (rw == NULL) { iface.close(helper); PyMem_Free(helper); - return (SDL_RWops *)RAISE(PyExc_IOError, SDL_GetError()); + return (SDL_RWops *)RAISERETURN(PyExc_IOError, SDL_GetError(), NULL); } SDL_PropertiesID props = SDL_GetIOProperties(rw); if (!props) { PyMem_Free(helper); - return (SDL_RWops *)RAISE(PyExc_IOError, SDL_GetError()); + return (SDL_RWops *)RAISERETURN(PyExc_IOError, SDL_GetError(), NULL); } if (!SDL_SetBooleanProperty(props, "_pygame_is_file_object", 1)) { PyMem_Free(helper); - return (SDL_RWops *)RAISE(PyExc_IOError, SDL_GetError()); + return (SDL_RWops *)RAISERETURN(PyExc_IOError, SDL_GetError(), NULL); } #else @@ -772,7 +774,8 @@ pgRWops_FromObject(PyObject *obj, char **extptr) fail: if (retry) { - return RAISE(PyExc_RuntimeError, "can't access resource on platform"); + return RAISERETURN(PyExc_RuntimeError, + "can't access resource on platform", NULL); } retry = 1; diff --git a/src_c/scrap.c b/src_c/scrap.c index 43fd96b915..505b01c59a 100644 --- a/src_c/scrap.c +++ b/src_c/scrap.c @@ -119,10 +119,10 @@ _scrap_init(PyObject *self, PyObject *args) * anything. * Here is old SDL1 code for future reference * if (!SDL_GetVideoSurface()) - * return RAISE(pgExc_SDLError, "No display mode is set"); + * return RAISERETURN(pgExc_SDLError, "No display mode is set", NULL); */ if (!pygame_scrap_init()) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; @@ -327,8 +327,8 @@ _scrap_put_scrap(PyObject *self, PyObject *args) /* Set it in the clipboard. */ if (!pygame_scrap_put(scrap_type, scraplen, scrap)) { - return RAISE(pgExc_SDLError, - "content could not be placed in clipboard."); + return RAISERETURN(pgExc_SDLError, + "content could not be placed in clipboard.", NULL); } /* Add or replace the set value. */ @@ -390,7 +390,7 @@ _scrap_set_mode(PyObject *self, PyObject *args) } if (_currentmode != SCRAP_CLIPBOARD && _currentmode != SCRAP_SELECTION) { - return RAISE(PyExc_ValueError, "invalid clipboard mode"); + return RAISERETURN(PyExc_ValueError, "invalid clipboard mode", NULL); } /* Force the clipboard, if not in a X11 environment. */ @@ -416,7 +416,7 @@ _scrap_get_text(PyObject *self, PyObject *args) // vs just an empty string in the clipboard if (*text == '\0' && hasText == SDL_TRUE) { SDL_free(text); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } PyObject *returnValue = PyUnicode_FromString(text); @@ -442,7 +442,7 @@ _scrap_put_text(PyObject *self, PyObject *args) } if (SDL_SetClipboardText(text)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; diff --git a/src_c/scrap.h b/src_c/scrap.h index 39537cbbf5..74d092f29e 100644 --- a/src_c/scrap.h +++ b/src_c/scrap.h @@ -55,9 +55,10 @@ typedef enum { /** * Macro for initialization checks. */ -#define PYGAME_SCRAP_INIT_CHECK() \ - if (!pygame_scrap_initialized()) { \ - return RAISE(pgExc_SDLError, "scrap system not initialized."); \ +#define PYGAME_SCRAP_INIT_CHECK() \ + if (!pygame_scrap_initialized()) { \ + return RAISERETURN(pgExc_SDLError, "scrap system not initialized.", \ + NULL); \ } /** diff --git a/src_c/scrap_sdl2.c b/src_c/scrap_sdl2.c index aebb1c28e9..cbc5ae82f7 100644 --- a/src_c/scrap_sdl2.c +++ b/src_c/scrap_sdl2.c @@ -26,7 +26,8 @@ pygame_scrap_get(char *type, size_t *count) char *clipboard = NULL; if (!pygame_scrap_initialized()) { - return RAISE(pgExc_SDLError, "scrap system not initialized."); + return RAISERETURN(pgExc_SDLError, "scrap system not initialized.", + NULL); } if (strcmp(type, pygame_scrap_plaintext_type) == 0) { @@ -45,7 +46,8 @@ char ** pygame_scrap_get_types(void) { if (!pygame_scrap_initialized()) { - return RAISE(pgExc_SDLError, "scrap system not initialized."); + return RAISERETURN(pgExc_SDLError, "scrap system not initialized.", + NULL); } return pygame_scrap_types; diff --git a/src_c/scrap_win.c b/src_c/scrap_win.c index b16204191b..ceda67f8ee 100644 --- a/src_c/scrap_win.c +++ b/src_c/scrap_win.c @@ -270,7 +270,8 @@ pygame_scrap_get(char *type, size_t *count) char *retval = NULL; if (!pygame_scrap_initialized()) { - return RAISE(pgExc_SDLError, "scrap system not initialized."); + return RAISERETURN(pgExc_SDLError, "scrap system not initialized.", + NULL); } if (!pygame_scrap_lost()) { diff --git a/src_c/surface.c b/src_c/surface.c index c3af0fa1de..82ba3c7ff0 100644 --- a/src_c/surface.c +++ b/src_c/surface.c @@ -367,7 +367,7 @@ surf_subtype_new(PyTypeObject *type, SDL_Surface *s, int owner) pgSurfaceObject *self; if (!s) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } self = (pgSurfaceObject *)pgSurface_Type.tp_new(type, NULL, NULL); @@ -875,23 +875,24 @@ surf_get_at(PyObject *self, PyObject *position) SURF_INIT_CHECK(surf) if (!pg_TwoIntsFromObj(position, &x, &y)) { - return RAISE(PyExc_TypeError, - "position must be a sequence of two numbers"); + return RAISERETURN(PyExc_TypeError, + "position must be a sequence of two numbers", NULL); } if (x < 0 || x >= surf->w || y < 0 || y >= surf->h) { - return RAISE(PyExc_IndexError, "pixel index out of range"); + return RAISERETURN(PyExc_IndexError, "pixel index out of range", NULL); } PG_PixelFormat *format; SDL_Palette *palette; if (!PG_GetSurfaceDetails(surf, &format, &palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } int bpp = PG_FORMAT_BytesPerPixel(format); if (bpp < 1 || bpp > 4) { - return RAISE(PyExc_RuntimeError, "invalid color depth for surface"); + return RAISERETURN(PyExc_RuntimeError, + "invalid color depth for surface", NULL); } if (!pgSurface_Lock((pgSurfaceObject *)self)) { @@ -951,25 +952,26 @@ surf_set_at(PyObject *self, PyObject *const *args, Py_ssize_t nargs) } if (!pg_TwoIntsFromObj(args[0], &x, &y)) { - return RAISE(PyExc_TypeError, - "position must be a sequence of two numbers"); + return RAISERETURN(PyExc_TypeError, + "position must be a sequence of two numbers", NULL); } rgba_obj = args[1]; PG_PixelFormat *format = PG_GetSurfaceFormat(surf); if (format == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } int bpp = PG_FORMAT_BytesPerPixel(format); if (bpp < 1 || bpp > 4) { - return RAISE(PyExc_RuntimeError, "invalid color depth for surface"); + return RAISERETURN(PyExc_RuntimeError, + "invalid color depth for surface", NULL); } SDL_Rect clip_rect; if (!PG_GetSurfaceClipRect(surf, &clip_rect)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (x < clip_rect.x || x >= clip_rect.x + clip_rect.w || y < clip_rect.y || @@ -1038,18 +1040,19 @@ surf_get_at_mapped(PyObject *self, PyObject *position) SURF_INIT_CHECK(surf) if (!pg_TwoIntsFromObj(position, &x, &y)) { - return RAISE(PyExc_TypeError, - "position must be a sequence of two numbers"); + return RAISERETURN(PyExc_TypeError, + "position must be a sequence of two numbers", NULL); } if (x < 0 || x >= surf->w || y < 0 || y >= surf->h) { - return RAISE(PyExc_IndexError, "pixel index out of range"); + return RAISERETURN(PyExc_IndexError, "pixel index out of range", NULL); } int bpp = PG_SURF_BytesPerPixel(surf); if (bpp < 1 || bpp > 4) { - return RAISE(PyExc_RuntimeError, "invalid color depth for surface"); + return RAISERETURN(PyExc_RuntimeError, + "invalid color depth for surface", NULL); } if (!pgSurface_Lock((pgSurfaceObject *)self)) { @@ -1100,7 +1103,7 @@ surf_map_rgb(PyObject *self, PyObject *args) PG_PixelFormat *format; SDL_Palette *palette; if (!PG_GetSurfaceDetails(surf, &format, &palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } color = PG_MapRGBA(format, palette, rgba[0], rgba[1], rgba[2], rgba[3]); @@ -1117,14 +1120,15 @@ surf_unmap_rgb(PyObject *self, PyObject *arg) col = (Uint32)PyLong_AsLong(arg); if (col == (Uint32)-1 && PyErr_Occurred()) { PyErr_Clear(); - return RAISE(PyExc_TypeError, "unmap_rgb expects 1 number argument"); + return RAISERETURN(PyExc_TypeError, + "unmap_rgb expects 1 number argument", NULL); } SURF_INIT_CHECK(surf) PG_PixelFormat *format; SDL_Palette *palette; if (!PG_GetSurfaceDetails(surf, &format, &palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (SDL_ISPIXELFORMAT_ALPHA(format->format)) { @@ -1228,7 +1232,8 @@ surf_get_palette(PyObject *self, PyObject *_null) pal = PG_GetSurfacePalette(surf); if (!pal) { - return RAISE(pgExc_SDLError, "Surface has no palette to get\n"); + return RAISERETURN(pgExc_SDLError, "Surface has no palette to get\n", + NULL); } list = PyTuple_New(pal->ncolors); @@ -1270,10 +1275,11 @@ surf_get_palette_at(PyObject *self, PyObject *args) pal = PG_GetSurfacePalette(surf); if (!pal) { - return RAISE(pgExc_SDLError, "Surface has no palette to set\n"); + return RAISERETURN(pgExc_SDLError, "Surface has no palette to set\n", + NULL); } if (_index >= pal->ncolors || _index < 0) { - return RAISE(PyExc_IndexError, "index out of bounds"); + return RAISERETURN(PyExc_IndexError, "index out of bounds", NULL); } c = &pal->colors[_index]; @@ -1306,17 +1312,20 @@ surf_set_palette(PyObject *self, PyObject *seq) SURF_INIT_CHECK(surf) if (!PySequence_Check(seq)) { - return RAISE(PyExc_ValueError, "Argument must be a sequence type"); + return RAISERETURN(PyExc_ValueError, + "Argument must be a sequence type", NULL); } pal = PG_GetSurfacePalette(surf); if (!SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surf))) { - return RAISE(pgExc_SDLError, "Surface colors are not indexed\n"); + return RAISERETURN(pgExc_SDLError, "Surface colors are not indexed\n", + NULL); } if (!pal) { - return RAISE(pgExc_SDLError, "Surface is not palettized\n"); + return RAISERETURN(pgExc_SDLError, "Surface is not palettized\n", + NULL); } old_colors = pal->colors; @@ -1328,11 +1337,12 @@ surf_set_palette(PyObject *self, PyObject *seq) ecode = pg_RGBAFromObjEx(item, rgba, PG_COLOR_HANDLE_SIMPLE); Py_DECREF(item); if (!ecode) { - return RAISE(PyExc_ValueError, - "takes a sequence of integers of RGB"); + return RAISERETURN(PyExc_ValueError, + "takes a sequence of integers of RGB", NULL); } if (rgba[3] != 255) { - return RAISE(PyExc_ValueError, "takes an alpha value of 255"); + return RAISERETURN(PyExc_ValueError, "takes an alpha value of 255", + NULL); } colors[i].r = (unsigned char)rgba[0]; colors[i].g = (unsigned char)rgba[1]; @@ -1345,7 +1355,7 @@ surf_set_palette(PyObject *self, PyObject *seq) } if (!PG_SetPaletteColors(pal, colors, 0, len)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -1370,17 +1380,19 @@ surf_set_palette_at(PyObject *self, PyObject *args) } if (!SDL_ISPIXELFORMAT_INDEXED(PG_SURF_FORMATENUM(surf))) { - return RAISE(pgExc_SDLError, "Surface colors are not indexed\n"); + return RAISERETURN(pgExc_SDLError, "Surface colors are not indexed\n", + NULL); } pal = PG_GetSurfacePalette(surf); if (!pal) { - return RAISE(pgExc_SDLError, "Surface is not palettized\n"); + return RAISERETURN(pgExc_SDLError, "Surface is not palettized\n", + NULL); } if (_index >= pal->ncolors || _index < 0) { - return RAISE(PyExc_IndexError, "index out of bounds"); + return RAISERETURN(PyExc_IndexError, "index out of bounds", NULL); } color.r = rgba[0]; @@ -1389,7 +1401,7 @@ surf_set_palette_at(PyObject *self, PyObject *args) color.a = pal->colors[_index].a; /* May be a colorkey color. */ if (!PG_SetPaletteColors(pal, &color, _index, 1)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; @@ -1436,7 +1448,7 @@ surf_set_colorkey(pgSurfaceObject *self, PyObject *args) pgSurface_Unprep(self); if (result == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; @@ -1460,7 +1472,7 @@ surf_get_colorkey(pgSurfaceObject *self, PyObject *_null) PG_PixelFormat *format; SDL_Palette *palette; if (!PG_GetSurfaceDetails(surf, &format, &palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (SDL_ISPIXELFORMAT_ALPHA(format->format)) { @@ -1494,20 +1506,22 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args) alphaval = (int)PyLong_AsLong(intobj); Py_DECREF(intobj); if (alphaval == -1 && PyErr_Occurred()) { - return RAISE(PyExc_TypeError, "invalid alpha argument"); + return RAISERETURN(PyExc_TypeError, "invalid alpha argument", + NULL); } } else { - return RAISE(PyExc_TypeError, "invalid alpha argument"); + return RAISERETURN(PyExc_TypeError, "invalid alpha argument", + NULL); } if (!PG_SetSurfaceBlendMode(surf, SDL_BLENDMODE_BLEND)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } else { if (!PG_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } @@ -1524,7 +1538,7 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args) if (alpha == 255 && (PG_SURF_BytesPerPixel(surf) == 1)) { /* Can't blend with a surface alpha of 255 and 8bit surfaces */ if (!PG_SetSurfaceBlendMode(surf, SDL_BLENDMODE_NONE)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } pgSurface_Prep(self); @@ -1551,7 +1565,7 @@ surf_set_alpha(pgSurfaceObject *self, PyObject *args) pgSurface_Unprep(self); if (result != 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; @@ -1567,7 +1581,7 @@ surf_get_alpha(pgSurfaceObject *self, PyObject *_null) SURF_INIT_CHECK(surf) if (!PG_GetSurfaceBlendMode(surf, &mode)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (mode != SDL_BLENDMODE_BLEND) { @@ -1575,7 +1589,7 @@ surf_get_alpha(pgSurfaceObject *self, PyObject *_null) } if (!PG_GetSurfaceAlphaMod(surf, &alpha)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return PyLong_FromLong(alpha); @@ -1590,7 +1604,7 @@ surf_get_blendmode(PyObject *self, PyObject *_null) SURF_INIT_CHECK(surf) if (!PG_GetSurfaceBlendMode(surf, &mode)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return PyLong_FromLong((long)mode); } @@ -1634,9 +1648,10 @@ surf_convert(pgSurfaceObject *self, PyObject *args) } if (!argobject && !SDL_WasInit(SDL_INIT_VIDEO)) { - return RAISE(pgExc_SDLError, - "cannot convert without format " - "when pygame.display is not initialized"); + return RAISERETURN(pgExc_SDLError, + "cannot convert without format " + "when pygame.display is not initialized", + NULL); } SURF_INIT_CHECK(surf) @@ -1648,7 +1663,7 @@ surf_convert(pgSurfaceObject *self, PyObject *args) PG_PixelFormat *surf_format; SDL_Palette *surf_palette; if (!PG_GetSurfaceDetails(surf, &surf_format, &surf_palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_GetColorKey(surf, &colorkey); @@ -1685,9 +1700,11 @@ surf_convert(pgSurfaceObject *self, PyObject *args) format_enum = SDL_PIXELFORMAT_ARGB8888; break; default: - return RAISE(PyExc_ValueError, - "no standard masks exist for given " - "bitdepth with alpha"); + return RAISERETURN( + PyExc_ValueError, + "no standard masks exist for given " + "bitdepth with alpha", + NULL); } } else { @@ -1715,8 +1732,9 @@ surf_convert(pgSurfaceObject *self, PyObject *args) format_enum = SDL_PIXELFORMAT_XRGB8888; break; default: - return RAISE(PyExc_ValueError, - "nonstandard bit depth given"); + return RAISERETURN(PyExc_ValueError, + "nonstandard bit depth given", + NULL); } } } @@ -1730,8 +1748,8 @@ surf_convert(pgSurfaceObject *self, PyObject *args) !pg_UintFromObjIndex(argobject, 2, &Bmask) || !pg_UintFromObjIndex(argobject, 3, &Amask)) { pgSurface_Unprep(self); - return RAISE(PyExc_ValueError, - "invalid color masks given"); + return RAISERETURN(PyExc_ValueError, + "invalid color masks given", NULL); } Uint32 mask = Rmask | Gmask | Bmask | Amask; @@ -1751,9 +1769,10 @@ surf_convert(pgSurfaceObject *self, PyObject *args) } else { pgSurface_Unprep(self); - return RAISE( + return RAISERETURN( PyExc_ValueError, - "invalid argument specifying new format to convert to"); + "invalid argument specifying new format to convert to", + NULL); } // If the destination format is indexed, provide a new palette or @@ -1793,14 +1812,14 @@ surf_convert(pgSurfaceObject *self, PyObject *args) } if (newsurf == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (has_colorkey) { colorkey = SDL_MapSurfaceRGBA(newsurf, key_r, key_g, key_b, key_a); if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0) { SDL_FreeSurface(newsurf); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } #else @@ -1846,9 +1865,11 @@ surf_convert(pgSurfaceObject *self, PyObject *args) Amask = 0xFF << 24; break; default: - return RAISE(PyExc_ValueError, - "no standard masks exist for given " - "bitdepth with alpha"); + return RAISERETURN( + PyExc_ValueError, + "no standard masks exist for given " + "bitdepth with alpha", + NULL); } } else { @@ -1881,8 +1902,9 @@ surf_convert(pgSurfaceObject *self, PyObject *args) Bmask = 0xFF; break; default: - return RAISE(PyExc_ValueError, - "nonstandard bit depth given"); + return RAISERETURN(PyExc_ValueError, + "nonstandard bit depth given", + NULL); } } format.Rmask = Rmask; @@ -1899,8 +1921,8 @@ surf_convert(pgSurfaceObject *self, PyObject *args) !pg_UintFromObjIndex(argobject, 2, &format.Bmask) || !pg_UintFromObjIndex(argobject, 3, &format.Amask)) { pgSurface_Unprep(self); - return RAISE(PyExc_ValueError, - "invalid color masks given"); + return RAISERETURN(PyExc_ValueError, + "invalid color masks given", NULL); } mask = format.Rmask | format.Gmask | format.Bmask | format.Amask; @@ -1912,9 +1934,10 @@ surf_convert(pgSurfaceObject *self, PyObject *args) } else { pgSurface_Unprep(self); - return RAISE( + return RAISERETURN( PyExc_ValueError, - "invalid argument specifying new format to convert to"); + "invalid argument specifying new format to convert to", + NULL); } format.BitsPerPixel = (Uint8)bpp; format.BytesPerPixel = (bpp + 7) / 8; @@ -1955,14 +1978,14 @@ surf_convert(pgSurfaceObject *self, PyObject *args) } if (newsurf == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (has_colorkey) { colorkey = SDL_MapRGBA(newsurf->format, key_r, key_g, key_b, key_a); if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0) { SDL_FreeSurface(newsurf); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } #endif @@ -2041,8 +2064,9 @@ surf_convert_alpha(pgSurfaceObject *self, PyObject *args) SURF_INIT_CHECK(surf) if (!SDL_WasInit(SDL_INIT_VIDEO)) { - return RAISE(pgExc_SDLError, - "cannot convert without pygame.display initialized"); + return RAISERETURN(pgExc_SDLError, + "cannot convert without pygame.display initialized", + NULL); } if (!PyArg_ParseTuple(args, "|O!", &pgSurface_Type, &srcsurf)) { @@ -2087,7 +2111,8 @@ surf_set_clip(PyObject *self, PyObject *args) else { rect = pgRect_FromObject(args, &temp); if (!rect) { - return RAISE(PyExc_ValueError, "invalid rectstyle object"); + return RAISERETURN(PyExc_ValueError, + "invalid rectstyle object", NULL); } sdlrect.x = rect->x; sdlrect.y = rect->y; @@ -2101,7 +2126,7 @@ surf_set_clip(PyObject *self, PyObject *args) } if (result == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; @@ -2116,7 +2141,7 @@ surf_get_clip(PyObject *self, PyObject *_null) SDL_Rect clip_rect; if (!PG_GetSurfaceClipRect(surf, &clip_rect)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return pgRect_New(&clip_rect); @@ -2152,7 +2177,7 @@ surf_fill(pgSurfaceObject *self, PyObject *args, PyObject *keywds) temp.h = surf->h; } else if (!(rect = pgRect_FromObject(r, &temp))) { - return RAISE(PyExc_ValueError, "invalid rectstyle object"); + return RAISERETURN(PyExc_ValueError, "invalid rectstyle object", NULL); } /* we need a fresh copy so our Rect values don't get munged */ @@ -2185,7 +2210,7 @@ surf_fill(pgSurfaceObject *self, PyObject *args, PyObject *keywds) pgSurface_Unprep(self); } if (result == -1) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return pgRect_New(&sdlrect); @@ -2227,12 +2252,14 @@ surf_blit(pgSurfaceObject *self, PyObject *args, PyObject *keywds) dy = sy; } else { - return RAISE(PyExc_TypeError, "invalid destination position for blit"); + return RAISERETURN(PyExc_TypeError, + "invalid destination position for blit", NULL); } if (argrect && argrect != Py_None) { if (!(src_rect = pgRect_FromObject(argrect, &temp))) { - return RAISE(PyExc_TypeError, "Invalid rectstyle argument"); + return RAISERETURN(PyExc_TypeError, "Invalid rectstyle argument", + NULL); } } else { @@ -2488,29 +2515,33 @@ surf_blits(pgSurfaceObject *self, PyObject *args, PyObject *keywds) switch (bliterrornum) { case BLITS_ERR_SEQUENCE_REQUIRED: - return RAISE( + return RAISERETURN( PyExc_ValueError, - "blit_sequence should be iterator of (Surface, dest)"); + "blit_sequence should be iterator of (Surface, dest)", NULL); case BLITS_ERR_DISPLAY_SURF_QUIT: - return RAISE(pgExc_SDLError, "display Surface quit"); + return RAISERETURN(pgExc_SDLError, "display Surface quit", NULL); case BLITS_ERR_SEQUENCE_SURF: - return RAISE(PyExc_TypeError, - "First element of blit_list needs to be Surface."); + return RAISERETURN( + PyExc_TypeError, + "First element of blit_list needs to be Surface.", NULL); case BLITS_ERR_INVALID_DESTINATION: - return RAISE(PyExc_TypeError, - "invalid destination position for blit"); + return RAISERETURN(PyExc_TypeError, + "invalid destination position for blit", NULL); case BLITS_ERR_INVALID_RECT_STYLE: - return RAISE(PyExc_TypeError, "Invalid rectstyle argument"); + return RAISERETURN(PyExc_TypeError, "Invalid rectstyle argument", + NULL); case BLITS_ERR_MUST_ASSIGN_NUMERIC: - return RAISE(PyExc_TypeError, "Must assign numeric values"); + return RAISERETURN(PyExc_TypeError, "Must assign numeric values", + NULL); case BLITS_ERR_BLIT_FAIL: - return RAISE(PyExc_TypeError, "Blit failed"); + return RAISERETURN(PyExc_TypeError, "Blit failed", NULL); case BLITS_ERR_PY_EXCEPTION_RAISED: return NULL; /* Raising a previously set exception */ case BLITS_ERR_SOURCE_NOT_SURFACE: - return RAISE(PyExc_TypeError, "Source objects must be a surface"); + return RAISERETURN(PyExc_TypeError, + "Source objects must be a surface", NULL); } - return RAISE(PyExc_TypeError, "Unknown error"); + return RAISERETURN(PyExc_TypeError, "Unknown error", NULL); } #define FBLITS_ERR_TUPLE_REQUIRED 11 @@ -2636,42 +2667,50 @@ surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs) } switch (error) { case BLITS_ERR_SEQUENCE_REQUIRED: - return RAISE( + return RAISERETURN( PyExc_ValueError, - "blit_sequence should be iterator of (Surface, dest)"); + "blit_sequence should be iterator of (Surface, dest)", NULL); case BLITS_ERR_SEQUENCE_SURF: - return RAISE( + return RAISERETURN( PyExc_TypeError, "First element of pairs (Surface, dest) in blit_sequence " - "must be a Surface."); + "must be a Surface.", + NULL); case BLITS_ERR_INVALID_DESTINATION: - return RAISE(PyExc_TypeError, - "invalid destination position for blit"); + return RAISERETURN(PyExc_TypeError, + "invalid destination position for blit", NULL); case BLITS_ERR_INVALID_RECT_STYLE: - return RAISE(PyExc_TypeError, "Invalid rectstyle argument"); + return RAISERETURN(PyExc_TypeError, "Invalid rectstyle argument", + NULL); case BLITS_ERR_MUST_ASSIGN_NUMERIC: - return RAISE(PyExc_TypeError, "Must assign numeric values"); + return RAISERETURN(PyExc_TypeError, "Must assign numeric values", + NULL); case BLITS_ERR_BLIT_FAIL: - return RAISE( + return RAISERETURN( PyExc_TypeError, - "Blit failed (probably the flag used does not exist)"); + "Blit failed (probably the flag used does not exist)", NULL); case BLITS_ERR_PY_EXCEPTION_RAISED: return NULL; /* Raising a previously set exception */ case BLITS_ERR_SOURCE_NOT_SURFACE: - return RAISE(PyExc_TypeError, "Source objects must be a Surface"); + return RAISERETURN(PyExc_TypeError, + "Source objects must be a Surface", NULL); case FBLITS_ERR_TUPLE_REQUIRED: - return RAISE( + return RAISERETURN( PyExc_ValueError, - "Blit_sequence item should be a tuple of (Surface, dest)"); + "Blit_sequence item should be a tuple of (Surface, dest)", + NULL); case FBLITS_ERR_INCORRECT_ARGS_NUM: - return RAISE(PyExc_ValueError, - "Incorrect number of parameters passed: need at " - "least one, 2 at max"); + return RAISERETURN( + PyExc_ValueError, + "Incorrect number of parameters passed: need at " + "least one, 2 at max", + NULL); case FBLITS_ERR_FLAG_NOT_NUMERIC: - return RAISE(PyExc_TypeError, - "The special_flags parameter must be an int"); + return RAISERETURN(PyExc_TypeError, + "The special_flags parameter must be an int", + NULL); } - return RAISE(PyExc_TypeError, "Unknown error"); + return RAISERETURN(PyExc_TypeError, "Unknown error", NULL); } static int @@ -2893,14 +2932,15 @@ surf_scroll(PyObject *self, PyObject *args, PyObject *keywds) } default: { if (scroll_flag != PGS_SCROLL_DEFAULT) { - return RAISE(PyExc_ValueError, "Invalid scroll flag"); + return RAISERETURN(PyExc_ValueError, "Invalid scroll flag", + NULL); } } } SDL_Rect clip_rect; if (!PG_GetSurfaceClipRect(surf, &clip_rect)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_Rect surf_rect = {0, 0, surf->w, surf->h}; @@ -2921,7 +2961,7 @@ surf_scroll(PyObject *self, PyObject *args, PyObject *keywds) if (dx >= w || dx <= -w || dy >= h || dy <= -h) { if (erase) { if (!PG_FillSurfaceRect(surf, NULL, 0)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } Py_RETURN_NONE; @@ -3105,7 +3145,7 @@ surf_get_masks(PyObject *self, PyObject *_null) PG_PixelFormat *format = PG_GetSurfaceFormat(surf); if (format == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return Py_BuildValue("(IIII)", format->Rmask, format->Gmask, format->Bmask, @@ -3115,7 +3155,8 @@ surf_get_masks(PyObject *self, PyObject *_null) static PyObject * surf_set_masks(PyObject *self, PyObject *args) { - return RAISE(PyExc_TypeError, "The surface masks are read-only in SDL2"); + return RAISERETURN(PyExc_TypeError, + "The surface masks are read-only in SDL2", NULL); } static PyObject * @@ -3127,7 +3168,7 @@ surf_get_shifts(PyObject *self, PyObject *_null) PG_PixelFormat *format = PG_GetSurfaceFormat(surf); if (format == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return Py_BuildValue("(iiii)", format->Rshift, format->Gshift, @@ -3137,7 +3178,8 @@ surf_get_shifts(PyObject *self, PyObject *_null) static PyObject * surf_set_shifts(PyObject *self, PyObject *args) { - return RAISE(PyExc_TypeError, "The surface shifts are read-only in SDL2"); + return RAISERETURN(PyExc_TypeError, + "The surface shifts are read-only in SDL2", NULL); } static PyObject * @@ -3149,7 +3191,7 @@ surf_get_losses(PyObject *self, PyObject *_null) PG_PixelFormat *format = PG_GetSurfaceFormat(surf); if (format == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return Py_BuildValue("(iiii)", PG_FORMAT_R_LOSS(format), @@ -3173,16 +3215,17 @@ surf_subsurface(PyObject *self, PyObject *args) PG_PixelFormat *format; SDL_Palette *palette; if (!PG_GetSurfaceDetails(surf, &format, &palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (!(rect = pgRect_FromObject(args, &temp))) { - return RAISE(PyExc_ValueError, "invalid rectstyle argument"); + return RAISERETURN(PyExc_ValueError, "invalid rectstyle argument", + NULL); } if (rect->x < 0 || rect->y < 0 || rect->x + rect->w > surf->w || rect->y + rect->h > surf->h) { - return RAISE(PyExc_ValueError, - "subsurface rectangle outside surface area"); + return RAISERETURN(PyExc_ValueError, + "subsurface rectangle outside surface area", NULL); } pgSurface_Lock((pgSurfaceObject *)self); @@ -3197,7 +3240,7 @@ surf_subsurface(PyObject *self, PyObject *args) pgSurface_Unlock((pgSurfaceObject *)self); if (!sub) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } /* copy the colormap if we need it */ @@ -3208,35 +3251,35 @@ surf_subsurface(PyObject *self, PyObject *args) if (!pal) { SDL_FreeSurface(sub); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (!PG_SetPaletteColors(pal, colors, 0, ncolors)) { SDL_FreePalette(pal); SDL_FreeSurface(sub); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (SDL_SetSurfacePalette(sub, pal) != 0) { SDL_FreePalette(pal); SDL_FreeSurface(sub); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_FreePalette(pal); } if (!PG_GetSurfaceAlphaMod(surf, &alpha)) { SDL_FreeSurface(sub); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (alpha != 255) { if (!PG_SetSurfaceAlphaMod(sub, alpha)) { SDL_FreeSurface(sub); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } if (SDL_HasColorKey(surf)) { SDL_GetColorKey(surf, &colorkey); if (SDL_SetColorKey(sub, SDL_TRUE, colorkey) != 0) { SDL_FreeSurface(sub); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } @@ -3375,21 +3418,22 @@ surf_get_bounding_rect(PyObject *self, PyObject *args, PyObject *kwargs) char *kwids[] = {"min_alpha", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwids, &min_alpha)) { - return RAISE(PyExc_ValueError, - "get_bounding_rect only accepts a single optional " - "min_alpha argument"); + return RAISERETURN(PyExc_ValueError, + "get_bounding_rect only accepts a single optional " + "min_alpha argument", + NULL); } SURF_INIT_CHECK(surf) if (!pgSurface_Lock((pgSurfaceObject *)self)) { - return RAISE(pgExc_SDLError, "could not lock surface"); + return RAISERETURN(pgExc_SDLError, "could not lock surface", NULL); } PG_PixelFormat *format; SDL_Palette *palette; if (!PG_GetSurfaceDetails(surf, &format, &palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } int bpp = PG_FORMAT_BytesPerPixel(format); @@ -3533,7 +3577,7 @@ surf_get_bounding_rect(PyObject *self, PyObject *args, PyObject *kwargs) } } if (!pgSurface_Unlock((pgSurfaceObject *)self)) { - return RAISE(pgExc_SDLError, "could not unlock surface"); + return RAISERETURN(pgExc_SDLError, "could not unlock surface", NULL); } rect = pgRect_New4(min_x, min_y, max_x - min_x, max_y - min_y); @@ -3609,7 +3653,7 @@ surf_get_view(PyObject *self, PyObject *args) PG_PixelFormat *format; SDL_Palette *palette; if (!PG_GetSurfaceDetails(surface, &format, &palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } int bpp = PG_FORMAT_BytesPerPixel(format); @@ -3640,8 +3684,9 @@ surf_get_view(PyObject *self, PyObject *args) } if (format->Gmask != 0x00ff00 && (bpp != 4 || format->Gmask != 0xff0000)) { - return RAISE(PyExc_ValueError, - "unsupported colormasks for 3D reference array"); + return RAISERETURN( + PyExc_ValueError, + "unsupported colormasks for 3D reference array", NULL); } get_buffer = _get_buffer_3D; break; @@ -3649,8 +3694,9 @@ surf_get_view(PyObject *self, PyObject *args) mask = format->Rmask; if (mask != 0x000000ffU && mask != 0x0000ff00U && mask != 0x00ff0000U && mask != 0xff000000U) { - return RAISE(PyExc_ValueError, - "unsupported colormasks for red reference array"); + return RAISERETURN( + PyExc_ValueError, + "unsupported colormasks for red reference array", NULL); } get_buffer = _get_buffer_red; break; @@ -3658,9 +3704,9 @@ surf_get_view(PyObject *self, PyObject *args) mask = format->Gmask; if (mask != 0x000000ffU && mask != 0x0000ff00U && mask != 0x00ff0000U && mask != 0xff000000U) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "unsupported colormasks for green reference array"); + "unsupported colormasks for green reference array", NULL); } get_buffer = _get_buffer_green; break; @@ -3668,9 +3714,9 @@ surf_get_view(PyObject *self, PyObject *args) mask = format->Bmask; if (mask != 0x000000ffU && mask != 0x0000ff00U && mask != 0x00ff0000U && mask != 0xff000000U) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "unsupported colormasks for blue reference array"); + "unsupported colormasks for blue reference array", NULL); } get_buffer = _get_buffer_blue; break; @@ -3678,9 +3724,9 @@ surf_get_view(PyObject *self, PyObject *args) mask = format->Amask; if (mask != 0x000000ffU && mask != 0x0000ff00U && mask != 0x00ff0000U && mask != 0xff000000U) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "unsupported colormasks for alpha reference array"); + "unsupported colormasks for alpha reference array", NULL); } get_buffer = _get_buffer_alpha; break; @@ -3736,12 +3782,14 @@ surf_premul_alpha(pgSurfaceObject *self, PyObject *_null) // just return the copy. int result = premul_surf_color_by_alpha(surf, newsurf); if (result == -1) { - return RAISE(PyExc_ValueError, - "source surface to be alpha pre-multiplied must have " - "alpha channel"); + return RAISERETURN( + PyExc_ValueError, + "source surface to be alpha pre-multiplied must have " + "alpha channel", + NULL); } else if (result == -2) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } pgSurface_Unprep(self); @@ -3768,12 +3816,14 @@ surf_premul_alpha_ip(pgSurfaceObject *self, PyObject *_null) int result = premul_surf_color_by_alpha(surf, surf); if (result == -1) { - return RAISE(PyExc_ValueError, - "source surface to be alpha pre-multiplied must have " - "alpha channel"); + return RAISERETURN( + PyExc_ValueError, + "source surface to be alpha pre-multiplied must have " + "alpha channel", + NULL); } else if (result == -2) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } pgSurface_Unprep(self); diff --git a/src_c/system.c b/src_c/system.c index 2c9e2b4e5f..85a8cfcc08 100644 --- a/src_c/system.c +++ b/src_c/system.c @@ -76,7 +76,7 @@ pg_system_get_pref_path(PyObject *self, PyObject *args, PyObject *kwargs) char *path = SDL_GetPrefPath(org, project); if (path == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } PyObject *ret = PyUnicode_FromString(path); @@ -245,7 +245,8 @@ pg_system_get_power_state(PyObject *self, PyObject *_null) } if (!PowerState_class) { - return RAISE(PyExc_SystemError, "PowerState class is not imported."); + return RAISERETURN(PyExc_SystemError, + "PowerState class is not imported.", NULL); } return PyObject_Call(PowerState_class, return_args, return_kwargs); diff --git a/src_c/time.c b/src_c/time.c index f70904491b..b85d487d5e 100644 --- a/src_c/time.c +++ b/src_c/time.c @@ -186,7 +186,7 @@ pg_time_autoinit(PyObject *self, PyObject *_null) if (!pg_timer_mutex) { pg_timer_mutex = SDL_CreateMutex(); if (!pg_timer_mutex) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } #endif @@ -363,7 +363,8 @@ time_delay(PyObject *self, PyObject *arg) { Sint64 ticks; if (!PyLong_Check(arg)) { - return RAISE(PyExc_TypeError, "delay requires one integer argument"); + return RAISERETURN(PyExc_TypeError, + "delay requires one integer argument", NULL); } ticks = PyLong_AsLongLong(arg); @@ -383,13 +384,14 @@ time_wait(PyObject *self, PyObject *arg) { Sint64 ticks, start; if (!PyLong_Check(arg)) { - return RAISE(PyExc_TypeError, "wait requires one integer argument"); + return RAISERETURN(PyExc_TypeError, + "wait requires one integer argument", NULL); } #if !SDL_VERSION_ATLEAST(3, 0, 0) if (!SDL_WasInit(SDL_INIT_TIMER)) { if (SDL_InitSubSystem(SDL_INIT_TIMER)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } #endif @@ -421,8 +423,8 @@ time_set_timer(PyObject *self, PyObject *args, PyObject *kwargs) /* do not allow set_timer to work on WASM for now... this needs some more * testing and fixes that are WIP on other PRs */ #if defined(__EMSCRIPTEN__) || defined(__wasi__) - return RAISE(PyExc_NotImplementedError, - "set_timer is not implemented on WASM yet"); + return RAISERETURN(PyExc_NotImplementedError, + "set_timer is not implemented on WASM yet", NULL); #endif if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|i", kwids, &obj, &ticks, @@ -437,7 +439,8 @@ time_set_timer(PyObject *self, PyObject *args, PyObject *kwargs) /* happens if PyLong_AsLong overflows, error is already set */ return NULL; } - return RAISE(PyExc_ValueError, "event type out of range"); + return RAISERETURN(PyExc_ValueError, "event type out of range", + NULL); } } else if (pgEvent_Check(obj)) { @@ -446,13 +449,14 @@ time_set_timer(PyObject *self, PyObject *args, PyObject *kwargs) ev_dict = e->dict; } else { - return RAISE(PyExc_TypeError, - "first argument must be an event type or event object"); + return RAISERETURN( + PyExc_TypeError, + "first argument must be an event type or event object", NULL); } #ifndef __EMSCRIPTEN__ if (!pg_timer_mutex) { - return RAISE(pgExc_SDLError, "pygame is not initialized"); + return RAISERETURN(pgExc_SDLError, "pygame is not initialized", NULL); } #endif /* emscripten */ @@ -510,15 +514,16 @@ time_set_timer(PyObject *self, PyObject *args, PyObject *kwargs) case PG_TIMER_NO_ERROR: Py_RETURN_NONE; case PG_TIMER_SDL_ERROR: - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); case PG_TIMER_MEMORY_ERROR: return PyErr_NoMemory(); default: - return RAISE( + return RAISERETURN( pgExc_SDLError, "Unknown and unhandled internal error occurred while handling " "errors in time_set_timer! If you are seeing this message " - "report this to pygame devs"); + "report this to pygame devs", + NULL); } } @@ -549,7 +554,7 @@ clock_tick_base(pgClockObject *self, PyObject *arg, int use_accurate_delay) /*just doublecheck that timer is initialized*/ if (!SDL_WasInit(SDL_INIT_TIMER)) { if (SDL_InitSubSystem(SDL_INIT_TIMER)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } #endif @@ -650,8 +655,8 @@ clock_str(pgClockObject *self) int ret = PyOS_snprintf(str, 64, "", self->fps); if (ret < 0 || ret >= 64) { - return RAISE(PyExc_RuntimeError, - "Internal PyOS_snprintf call failed!"); + return RAISERETURN(PyExc_RuntimeError, + "Internal PyOS_snprintf call failed!", NULL); } return PyUnicode_FromString(str); @@ -671,7 +676,7 @@ clock_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) #if !SDL_VERSION_ATLEAST(3, 0, 0) if (!SDL_WasInit(SDL_INIT_TIMER)) { if (SDL_InitSubSystem(SDL_INIT_TIMER)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } #endif diff --git a/src_c/transform.c b/src_c/transform.c index f19e996317..a9d2182f5c 100644 --- a/src_c/transform.c +++ b/src_c/transform.c @@ -124,13 +124,15 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height) int isalpha; if (PG_SURF_BytesPerPixel(surf) == 0 || PG_SURF_BytesPerPixel(surf) > 4) { - return (SDL_Surface *)(RAISE( - PyExc_ValueError, "unsupported Surface bit depth for transform")); + return (SDL_Surface *)(RAISERETURN( + PyExc_ValueError, "unsupported Surface bit depth for transform", + NULL)); } newsurf = PG_CreateSurface(width, height, PG_SURF_FORMATENUM(surf)); if (!newsurf) { - return (SDL_Surface *)(RAISE(pgExc_SDLError, SDL_GetError())); + return ( + SDL_Surface *)(RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL)); } /* Copy palette, colorkey, etc info */ @@ -140,31 +142,33 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height) if (newsurf_palette == NULL) { SDL_FreeSurface(newsurf); - return RAISE(pgExc_SDLError, - "Palette expected (newsurf) but no palette found."); + return RAISERETURN( + pgExc_SDLError, + "Palette expected (newsurf) but no palette found.", NULL); } if (surf_palette == NULL) { SDL_FreeSurface(newsurf); - return RAISE(pgExc_SDLError, - "Palette expected (surf) but no palette found."); + return RAISERETURN(pgExc_SDLError, + "Palette expected (surf) but no palette found.", + NULL); } if (!PG_SetPaletteColors(newsurf_palette, surf_palette->colors, 0, surf_palette->ncolors)) { SDL_FreeSurface(newsurf); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } if (!PG_GetSurfaceAlphaMod(surf, &alpha)) { SDL_FreeSurface(newsurf); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (alpha != 255) { if (!PG_SetSurfaceAlphaMod(newsurf, alpha)) { SDL_FreeSurface(newsurf); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } @@ -172,17 +176,17 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height) if (isalpha == 1) { if (!PG_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_BLEND)) { SDL_FreeSurface(newsurf); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } else if (isalpha == -1) { SDL_FreeSurface(newsurf); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } else { if (!PG_SetSurfaceBlendMode(newsurf, SDL_BLENDMODE_NONE)) { SDL_FreeSurface(newsurf); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } @@ -190,12 +194,12 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height) SDL_GetColorKey(surf, &colorkey); if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0) { SDL_FreeSurface(newsurf); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (PG_SurfaceHasRLE(surf) && SDL_SetSurfaceRLE(newsurf, SDL_TRUE) != 0) { SDL_FreeSurface(newsurf); - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } @@ -438,7 +442,8 @@ scale_to(pgSurfaceObject *srcobj, pgSurfaceObject *dstobj, int width, int stretch_result_num = 0; if (width < 0 || height < 0) { - return RAISE(PyExc_ValueError, "Cannot scale to negative size"); + return RAISERETURN(PyExc_ValueError, "Cannot scale to negative size", + NULL); } src = pgSurface_AsSurface(srcobj); @@ -455,7 +460,7 @@ scale_to(pgSurfaceObject *srcobj, pgSurfaceObject *dstobj, int width, PG_PixelFormat *ret_format = PG_GetSurfaceFormat(retsurf); PG_PixelFormat *src_format = PG_GetSurfaceFormat(src); if (ret_format == NULL || src_format == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (PG_FORMAT_BytesPerPixel(ret_format) != @@ -463,9 +468,10 @@ scale_to(pgSurfaceObject *srcobj, pgSurfaceObject *dstobj, int width, ret_format->Rmask != src_format->Rmask || ret_format->Gmask != src_format->Gmask || ret_format->Bmask != src_format->Bmask) { - return RAISE(PyExc_ValueError, - "Source and destination surfaces need to be " - "compatible formats."); + return RAISERETURN(PyExc_ValueError, + "Source and destination surfaces need to be " + "compatible formats.", + NULL); } /* If the surface formats are otherwise compatible but the alpha is @@ -481,9 +487,9 @@ scale_to(pgSurfaceObject *srcobj, pgSurfaceObject *dstobj, int width, } if (retsurf->w != width || retsurf->h != height) { - return (SDL_Surface *)(RAISE( + return (SDL_Surface *)(RAISERETURN( PyExc_ValueError, - "Destination surface not the given width or height.")); + "Destination surface not the given width or height.", NULL)); } /* Testing width and height of src and dest, because pygame supports @@ -503,7 +509,8 @@ scale_to(pgSurfaceObject *srcobj, pgSurfaceObject *dstobj, int width, } if (stretch_result_num < 0) { - return (SDL_Surface *)(RAISE(pgExc_SDLError, SDL_GetError())); + return (SDL_Surface *)(RAISERETURN(pgExc_SDLError, SDL_GetError(), + NULL)); } } @@ -530,7 +537,7 @@ surf_scale(PyObject *self, PyObject *args, PyObject *kwargs) SURF_INIT_CHECK(surf) if (!pg_TwoIntsFromObj(size, &width, &height)) { - return RAISE(PyExc_TypeError, "size must be two numbers"); + return RAISERETURN(PyExc_TypeError, "size must be two numbers", NULL); } newsurf = scale_to(surfobj, surfobj2, width, height); @@ -620,13 +627,15 @@ surf_scale2x(PyObject *self, PyObject *args, PyObject *kwargs) /* check to see if the size is twice as big. */ if (newsurf->w != (surf->w * 2) || newsurf->h != (surf->h * 2)) { - return RAISE(PyExc_ValueError, "Destination surface not 2x bigger."); + return RAISERETURN(PyExc_ValueError, + "Destination surface not 2x bigger.", NULL); } /* check to see if the format of the surface is the same. */ if (PG_SURF_BytesPerPixel(surf) != PG_SURF_BytesPerPixel(newsurf)) { - return RAISE(PyExc_ValueError, - "Source and destination surfaces need the same format."); + return RAISERETURN( + PyExc_ValueError, + "Source and destination surfaces need the same format.", NULL); } SDL_LockSurface(newsurf); @@ -674,8 +683,9 @@ surf_rotate(PyObject *self, PyObject *args, PyObject *kwargs) } if (PG_SURF_BytesPerPixel(surf) == 0 || PG_SURF_BytesPerPixel(surf) > 4) { - return RAISE(PyExc_ValueError, - "unsupported Surface bit depth for transform"); + return RAISERETURN(PyExc_ValueError, + "unsupported Surface bit depth for transform", + NULL); } if (!(fmod((double)angle, (double)90.0f))) { @@ -738,7 +748,7 @@ surf_rotate(PyObject *self, PyObject *args, PyObject *kwargs) SDL_UnlockSurface(surf); PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf); if (surf_format == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } bgcolor &= ~surf_format->Amask; } @@ -955,7 +965,7 @@ surf_rotozoom(PyObject *self, PyObject *args, PyObject *kwargs) newsurf = rotozoomSurface(surf32, angle, scale, 1); Py_END_ALLOW_THREADS; if (newsurf == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (surf32 == surf) { @@ -1055,7 +1065,7 @@ surf_chop(PyObject *self, PyObject *args, PyObject *kwargs) } if (!(rect = pgRect_FromObject(rectobj, &temp))) { - return RAISE(PyExc_TypeError, "Rect argument is invalid"); + return RAISERETURN(PyExc_TypeError, "Rect argument is invalid", NULL); } surf = pgSurface_AsSurface(surfobj); @@ -1505,17 +1515,17 @@ smoothscale_to(PyObject *self, pgSurfaceObject *srcobj, SDL_Surface *retsurf = NULL; int bpp; if (width < 0 || height < 0) { - return (SDL_Surface *)(RAISE(PyExc_ValueError, - "Cannot scale to negative size")); + return (SDL_Surface *)(RAISERETURN( + PyExc_ValueError, "Cannot scale to negative size", NULL)); } src = pgSurface_AsSurface(srcobj); bpp = PG_SURF_BytesPerPixel(src); if (bpp < 3 || bpp > 4) { - return (SDL_Surface *)(RAISE( + return (SDL_Surface *)(RAISERETURN( PyExc_ValueError, - "Only 24-bit or 32-bit surfaces can be smoothly scaled")); + "Only 24-bit or 32-bit surfaces can be smoothly scaled", NULL)); } if (!dstobj) { @@ -1529,15 +1539,15 @@ smoothscale_to(PyObject *self, pgSurfaceObject *srcobj, } if (retsurf->w != width || retsurf->h != height) { - return (SDL_Surface *)(RAISE( + return (SDL_Surface *)(RAISERETURN( PyExc_ValueError, - "Destination surface not the given width or height.")); + "Destination surface not the given width or height.", NULL)); } if (((width * bpp + 3) >> 2) > retsurf->pitch) { - return (SDL_Surface *)(RAISE( + return (SDL_Surface *)(RAISERETURN( PyExc_ValueError, - "SDL Error: destination surface pitch not 4-byte aligned.")); + "SDL Error: destination surface pitch not 4-byte aligned.", NULL)); } if (width && height) { @@ -1589,7 +1599,7 @@ surf_scalesmooth(PyObject *self, PyObject *args, PyObject *kwargs) SURF_INIT_CHECK(surf) if (!pg_TwoIntsFromObj(size, &width, &height)) { - return RAISE(PyExc_TypeError, "size must be two numbers"); + return RAISERETURN(PyExc_TypeError, "size must be two numbers", NULL); } newsurf = smoothscale_to(self, surfobj, surfobj2, width, height); @@ -1677,8 +1687,8 @@ surf_set_smoothscale_backend(PyObject *self, PyObject *args, PyObject *kwargs) #if defined(SCALE_MMX_SUPPORT) else if (strcmp(type, "MMX") == 0) { if (!SDL_HasMMX()) { - return RAISE(PyExc_ValueError, - "MMX not supported on this machine"); + return RAISERETURN(PyExc_ValueError, + "MMX not supported on this machine", NULL); } if (PyErr_WarnEx( PyExc_DeprecationWarning, @@ -1694,8 +1704,8 @@ surf_set_smoothscale_backend(PyObject *self, PyObject *args, PyObject *kwargs) } else if (strcmp(type, "SSE") == 0) { if (!SDL_HasSSE()) { - return RAISE(PyExc_ValueError, - "SSE not supported on this machine"); + return RAISERETURN(PyExc_ValueError, + "SSE not supported on this machine", NULL); } if (PyErr_WarnEx( PyExc_DeprecationWarning, @@ -1720,8 +1730,8 @@ surf_set_smoothscale_backend(PyObject *self, PyObject *args, PyObject *kwargs) #if PG_ENABLE_SSE_NEON else if (strcmp(type, "SSE2") == 0) { if (!SDL_HasSSE2()) { - return RAISE(PyExc_ValueError, - "SSE2 not supported on this machine"); + return RAISERETURN(PyExc_ValueError, + "SSE2 not supported on this machine", NULL); } st->filter_type = "SSE2"; st->filter_shrink_X = filter_shrink_X_SSE2; @@ -1732,8 +1742,8 @@ surf_set_smoothscale_backend(PyObject *self, PyObject *args, PyObject *kwargs) else if (strcmp(type, "NEON") == 0) { if (!SDL_HasNEON()) { - return RAISE(PyExc_ValueError, - "NEON not supported on this machine"); + return RAISERETURN(PyExc_ValueError, + "NEON not supported on this machine", NULL); } st->filter_type = "NEON"; st->filter_shrink_X = filter_shrink_X_SSE2; @@ -2021,12 +2031,14 @@ surf_threshold(PyObject *self, PyObject *args, PyObject *kwds) if (set_behavior == 0 && !(set_color_obj == NULL || set_color_obj == Py_None)) { - return RAISE(PyExc_TypeError, - "if set_behavior==0 set_color should be None"); + return RAISERETURN(PyExc_TypeError, + "if set_behavior==0 set_color should be None", + NULL); } if (set_behavior == 0 && dest_surf_obj != Py_None) { - return RAISE(PyExc_TypeError, - "if set_behavior==0 dest_surf_obj should be None"); + return RAISERETURN(PyExc_TypeError, + "if set_behavior==0 dest_surf_obj should be None", + NULL); } if (dest_surf_obj && dest_surf_obj != Py_None && @@ -2034,14 +2046,15 @@ surf_threshold(PyObject *self, PyObject *args, PyObject *kwds) dest_surf = pgSurface_AsSurface(dest_surf_obj); } else if (set_behavior != 0) { - return RAISE( + return RAISERETURN( PyExc_TypeError, - "argument 1 must be pygame.Surface, or None with set_behavior=1"); + "argument 1 must be pygame.Surface, or None with set_behavior=1", + NULL); } surf = pgSurface_AsSurface(surf_obj); if (NULL == surf) { - return RAISE(PyExc_TypeError, "invalid surf argument"); + return RAISERETURN(PyExc_TypeError, "invalid surf argument", NULL); } if (search_surf_obj && pgSurface_Check(search_surf_obj)) { @@ -2049,55 +2062,61 @@ surf_threshold(PyObject *self, PyObject *args, PyObject *kwds) } if (search_surf && search_color_obj != Py_None) { - return RAISE(PyExc_TypeError, - "if search_surf is used, search_color should be None"); + return RAISERETURN( + PyExc_TypeError, + "if search_surf is used, search_color should be None", NULL); } if (set_behavior == 2 && set_color_obj != Py_None) { - return RAISE(PyExc_TypeError, - "if set_behavior==2 set_color should be None"); + return RAISERETURN(PyExc_TypeError, + "if set_behavior==2 set_color should be None", + NULL); } if (search_color_obj != Py_None) { if (_color_from_obj(search_color_obj, surf, NULL, &color_search_color)) { - return RAISE(PyExc_TypeError, "invalid search_color argument"); + return RAISERETURN(PyExc_TypeError, + "invalid search_color argument", NULL); } } if (_color_from_obj(threshold_obj, surf, rgba_threshold_default, &color_threshold)) { - return RAISE(PyExc_TypeError, "invalid threshold argument"); + return RAISERETURN(PyExc_TypeError, "invalid threshold argument", + NULL); } if (set_color_obj != Py_None) { if (_color_from_obj(set_color_obj, surf, rgba_set_color_default, &color_set_color)) { - return RAISE(PyExc_TypeError, "invalid set_color argument"); + return RAISERETURN(PyExc_TypeError, "invalid set_color argument", + NULL); } } if (dest_surf && (surf->h != dest_surf->h || surf->w != dest_surf->w)) { - return RAISE(PyExc_TypeError, "surf and dest_surf not the same size"); + return RAISERETURN(PyExc_TypeError, + "surf and dest_surf not the same size", NULL); } if (search_surf && (surf->h != search_surf->h || surf->w != search_surf->w)) { - return RAISE(PyExc_TypeError, - "surf and search_surf not the same size"); + return RAISERETURN(PyExc_TypeError, + "surf and search_surf not the same size", NULL); } PG_PixelFormat *dest_fmt = NULL; if (dest_surf) { dest_fmt = PG_GetSurfaceFormat(dest_surf); if (dest_fmt == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } PG_PixelFormat *surf_format; SDL_Palette *surf_palette; if (!PG_GetSurfaceDetails(surf, &surf_format, &surf_palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } PG_PixelFormat *search_surf_format = NULL; @@ -2105,7 +2124,7 @@ surf_threshold(PyObject *self, PyObject *args, PyObject *kwds) if (search_surf) { if (!PG_GetSurfaceDetails(search_surf, &search_surf_format, &search_surf_palette)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } @@ -2279,21 +2298,23 @@ grayscale(pgSurfaceObject *srcobj, pgSurfaceObject *dstobj) } if (newsurf->w != src->w || newsurf->h != src->h) { - return (SDL_Surface *)(RAISE( + return (SDL_Surface *)(RAISERETURN( PyExc_ValueError, - "Destination surface must be the same size as source surface.")); + "Destination surface must be the same size as source surface.", + NULL)); } if (PG_SURF_BytesPerPixel(src) != PG_SURF_BytesPerPixel(newsurf)) { - return (SDL_Surface *)(RAISE( + return (SDL_Surface *)(RAISERETURN( PyExc_ValueError, - "Source and destination surfaces need the same format.")); + "Source and destination surfaces need the same format.", NULL)); } PG_PixelFormat *src_format = PG_GetSurfaceFormat(src); PG_PixelFormat *newsurf_format = PG_GetSurfaceFormat(newsurf); if (src_format == NULL || newsurf_format == NULL) { - return (SDL_Surface *)(RAISE(pgExc_SDLError, SDL_GetError())); + return ( + SDL_Surface *)(RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL)); } #if defined(__EMSCRIPTEN__) @@ -2375,22 +2396,24 @@ solid_overlay(pgSurfaceObject *srcobj, Uint32 color, pgSurfaceObject *dstobj, } if (newsurf->w != src->w || newsurf->h != src->h) { - return (SDL_Surface *)(RAISE( + return (SDL_Surface *)(RAISERETURN( PyExc_ValueError, - "Destination surface must be the same size as source surface.")); + "Destination surface must be the same size as source surface.", + NULL)); } PG_PixelFormat *fmt, *newsurf_format; SDL_Palette *src_palette, *newsurf_palette; if (!PG_GetSurfaceDetails(src, &fmt, &src_palette) || !PG_GetSurfaceDetails(newsurf, &newsurf_format, &newsurf_palette)) { - return (SDL_Surface *)(RAISE(pgExc_SDLError, SDL_GetError())); + return ( + SDL_Surface *)(RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL)); } if (fmt->format != newsurf_format->format) { - return (SDL_Surface *)(RAISE( + return (SDL_Surface *)(RAISERETURN( PyExc_ValueError, - "Source and destination surfaces need the same format.")); + "Source and destination surfaces need the same format.", NULL)); } /* If the source surface has no alpha channel, we can't overlay with alpha @@ -2574,7 +2597,7 @@ surf_solid_overlay(PyObject *self, PyObject *args, PyObject *kwargs) surf = pgSurface_AsSurface(surfobj); if (!pg_MappedColorFromObj(colorobj, surf, &color, PG_COLOR_HANDLE_ALL)) { - return RAISE(PyExc_TypeError, "invalid color argument"); + return RAISERETURN(PyExc_TypeError, "invalid color argument", NULL); } newsurf = solid_overlay(surfobj, color, surfobj2, keep_alpha); @@ -2850,19 +2873,21 @@ surf_hsl(PyObject *self, PyObject *args, PyObject *kwargs) } if (dst->w != src->w || dst->h != src->h) { - return RAISE( + return RAISERETURN( PyExc_ValueError, - "Destination surface must be the same size as source surface."); + "Destination surface must be the same size as source surface.", + NULL); } if (PG_SURF_FORMATENUM(src) != PG_SURF_FORMATENUM(dst)) { - return RAISE(PyExc_ValueError, - "Source and destination surfaces need the same format."); + return RAISERETURN( + PyExc_ValueError, + "Source and destination surfaces need the same format.", NULL); } PG_PixelFormat *src_format = PG_GetSurfaceFormat(src); PG_PixelFormat *dst_format = PG_GetSurfaceFormat(dst); if (src_format == NULL || dst_format == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_BEGIN_ALLOW_THREADS; @@ -3110,20 +3135,21 @@ surf_laplacian(PyObject *self, PyObject *args, PyObject *kwargs) /* check to see if the size is the correct size. */ if (newsurf->w != (surf->w) || newsurf->h != (surf->h)) { - return RAISE(PyExc_ValueError, - "Destination surface not the same size."); + return RAISERETURN(PyExc_ValueError, + "Destination surface not the same size.", NULL); } /* check to see if the format of the surface is the same. */ if (PG_SURF_BytesPerPixel(surf) != PG_SURF_BytesPerPixel(newsurf)) { - return RAISE(PyExc_ValueError, - "Source and destination surfaces need the same format."); + return RAISERETURN( + PyExc_ValueError, + "Source and destination surfaces need the same format.", NULL); } PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf); PG_PixelFormat *newsurf_format = PG_GetSurfaceFormat(newsurf); if (surf_format == NULL || newsurf_format == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } SDL_LockSurface(newsurf); @@ -3356,15 +3382,16 @@ surf_average_surfaces(PyObject *self, PyObject *args, PyObject *kwargs) } if (!PySequence_Check(list)) { - return RAISE(PyExc_TypeError, - "Argument must be a sequence of surface objects."); + return RAISERETURN(PyExc_TypeError, + "Argument must be a sequence of surface objects.", + NULL); } size = PySequence_Length(list); /*warning, size could be -1 on error?*/ if (size < 1) { - return RAISE(PyExc_TypeError, - "Needs to be given at least one surface."); + return RAISERETURN(PyExc_TypeError, + "Needs to be given at least one surface.", NULL); } /* Allocate an array of surface pointers. */ @@ -3372,8 +3399,8 @@ surf_average_surfaces(PyObject *self, PyObject *args, PyObject *kwargs) surfaces = (SDL_Surface **)calloc(1, sizeof(SDL_Surface *) * size); if (!surfaces) { - return RAISE(PyExc_MemoryError, - "Not enough memory to store surfaces.\n"); + return RAISERETURN(PyExc_MemoryError, + "Not enough memory to store surfaces.\n", NULL); } /* Iterate over 'surfaces' passed in. */ @@ -3385,14 +3412,16 @@ surf_average_surfaces(PyObject *self, PyObject *args, PyObject *kwargs) if (!obj) { Py_XDECREF(obj); - ret = RAISE(PyExc_TypeError, "Needs to be a surface object."); + RAISE(PyExc_TypeError, "Needs to be a surface object."); + ret = NULL; an_error = 1; break; } if (!pgSurface_Check(obj)) { Py_XDECREF(obj); - ret = RAISE(PyExc_TypeError, "Needs to be a surface object."); + RAISE(PyExc_TypeError, "Needs to be a surface object."); + ret = NULL; an_error = 1; break; } @@ -3401,7 +3430,8 @@ surf_average_surfaces(PyObject *self, PyObject *args, PyObject *kwargs) if (!surf) { Py_XDECREF(obj); - ret = RAISE(PyExc_TypeError, "Needs to be a surface object."); + ret = NULL; + RAISE(PyExc_TypeError, "Needs to be a surface object."); an_error = 1; break; } @@ -3416,8 +3446,8 @@ surf_average_surfaces(PyObject *self, PyObject *args, PyObject *kwargs) if (!newsurf) { Py_XDECREF(obj); - ret = RAISE(PyExc_ValueError, - "Could not create new surface."); + ret = NULL; + RAISE(PyExc_ValueError, "Could not create new surface."); an_error = 1; break; } @@ -3429,8 +3459,9 @@ surf_average_surfaces(PyObject *self, PyObject *args, PyObject *kwargs) /* check to see if the size is the correct size. */ if (newsurf->w != (surf->w) || newsurf->h != (surf->h)) { Py_XDECREF(obj); - ret = RAISE(PyExc_ValueError, - "Destination surface not the same size."); + ret = NULL; + RAISE(PyExc_ValueError, + "Destination surface not the same size."); an_error = 1; break; } @@ -3439,9 +3470,9 @@ surf_average_surfaces(PyObject *self, PyObject *args, PyObject *kwargs) if (PG_SURF_BytesPerPixel(surf) != PG_SURF_BytesPerPixel(newsurf)) { Py_XDECREF(obj); - ret = RAISE( - PyExc_ValueError, - "Source and destination surfaces need the same format."); + ret = NULL; + RAISE(PyExc_ValueError, + "Source and destination surfaces need the same format."); an_error = 1; break; } @@ -3744,7 +3775,8 @@ surf_average_color(PyObject *self, PyObject *args, PyObject *kwargs) } else { if (!(rect = pgRect_FromObject(rectobj, &temp))) { - return RAISE(PyExc_TypeError, "Rect argument is invalid"); + return RAISERETURN(PyExc_TypeError, "Rect argument is invalid", + NULL); } x = rect->x; y = rect->y; @@ -3754,7 +3786,7 @@ surf_average_color(PyObject *self, PyObject *args, PyObject *kwargs) PG_PixelFormat *surf_format = PG_GetSurfaceFormat(surf); if (surf_format == NULL) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_BEGIN_ALLOW_THREADS; @@ -3954,14 +3986,15 @@ blur(pgSurfaceObject *srcobj, pgSurfaceObject *dstobj, int radius, int result = 0; if (radius < 0) { - return RAISE(PyExc_ValueError, - "The radius should not be less than zero."); + return RAISERETURN(PyExc_ValueError, + "The radius should not be less than zero.", NULL); } src = pgSurface_AsSurface(srcobj); if (PG_GetSurfacePalette(src)) { - return RAISE(PyExc_ValueError, "Indexed surfaces cannot be blurred."); + return RAISERETURN(PyExc_ValueError, + "Indexed surfaces cannot be blurred.", NULL); } if (!dstobj) { @@ -3975,8 +4008,8 @@ blur(pgSurfaceObject *srcobj, pgSurfaceObject *dstobj, int radius, } if ((retsurf->w) != (src->w) || (retsurf->h) != (src->h)) { - return RAISE(PyExc_ValueError, - "Destination surface not the same size."); + return RAISERETURN(PyExc_ValueError, + "Destination surface not the same size.", NULL); } if (retsurf->w == 0 || retsurf->h == 0) { @@ -3989,17 +4022,18 @@ blur(pgSurfaceObject *srcobj, pgSurfaceObject *dstobj, int radius, Uint8 *src_end = src_start + src->h * src->pitch; if ((ret_start <= src_start && ret_end >= src_start) || (src_start <= ret_start && src_end >= ret_start)) { - return RAISE( + return RAISERETURN( PyExc_ValueError, "Blur routines do not support dest_surfaces that share pixels " "with the source surface. Likely the surfaces are the same, one " - "of them is a subsurface, or they are sharing the same buffer."); + "of them is a subsurface, or they are sharing the same buffer.", + NULL); } if (PG_SURF_BytesPerPixel(src) != PG_SURF_BytesPerPixel(retsurf)) { - return (SDL_Surface *)(RAISE( + return (SDL_Surface *)(RAISERETURN( PyExc_ValueError, - "Source and destination surfaces need the same format.")); + "Source and destination surfaces need the same format.", NULL)); } if (radius > MIN(src->w, src->h)) { @@ -4138,21 +4172,23 @@ invert(pgSurfaceObject *srcobj, pgSurfaceObject *dstobj) } if (newsurf->w != src->w || newsurf->h != src->h) { - return (SDL_Surface *)(RAISE( + return (SDL_Surface *)(RAISERETURN( PyExc_ValueError, - "Destination surface must be the same size as source surface.")); + "Destination surface must be the same size as source surface.", + NULL)); } if (PG_SURF_BytesPerPixel(src) != PG_SURF_BytesPerPixel(newsurf)) { - return (SDL_Surface *)(RAISE( + return (SDL_Surface *)(RAISERETURN( PyExc_ValueError, - "Source and destination surfaces need the same format.")); + "Source and destination surfaces need the same format.", NULL)); } PG_PixelFormat *src_format = PG_GetSurfaceFormat(src); PG_PixelFormat *newsurf_format = PG_GetSurfaceFormat(newsurf); if (src_format == NULL || newsurf_format == NULL) { - return (SDL_Surface *)(RAISE(pgExc_SDLError, SDL_GetError())); + return ( + SDL_Surface *)(RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL)); } #if defined(__EMSCRIPTEN__) diff --git a/src_c/window.c b/src_c/window.c index a6bd997a0f..cfba116b9d 100644 --- a/src_c/window.c +++ b/src_c/window.c @@ -170,8 +170,9 @@ window_get_surface(pgWindowObject *self, PyObject *_null) if (self->_is_borrowed) { surf = (PyObject *)pg_GetDefaultWindowSurface(); if (!surf) { - return RAISE(pgExc_SDLError, - "display.set_mode has not been called yet."); + return RAISERETURN(pgExc_SDLError, + "display.set_mode has not been called yet.", + NULL); } Py_INCREF(surf); return surf; @@ -179,7 +180,7 @@ window_get_surface(pgWindowObject *self, PyObject *_null) _surf = SDL_GetWindowSurface(self->_win); if (!_surf) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (pg_GetDefaultConvertFormat() == 0) { @@ -205,16 +206,18 @@ window_flip(pgWindowObject *self, PyObject *_null) if (self->context == NULL) { if (!self->surf) { - return RAISE(pgExc_SDLError, - "the Window has no surface associated with it, did " - "you forget to call Window.get_surface()"); + return RAISERETURN( + pgExc_SDLError, + "the Window has no surface associated with it, did " + "you forget to call Window.get_surface()", + NULL); } Py_BEGIN_ALLOW_THREADS; result = SDL_UpdateWindowSurface(self->_win); Py_END_ALLOW_THREADS; if (result) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } else { @@ -301,7 +304,7 @@ static PyObject * window_set_windowed(pgWindowObject *self, PyObject *_null) { if (SDL_SetWindowFullscreen(self->_win, 0)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -351,13 +354,13 @@ window_set_fullscreen(pgWindowObject *self, PyObject *args, PyObject *kwargs) } #if SDL_VERSION_ATLEAST(3, 0, 0) if (!pg_window_set_fullscreen(self->_win, desktop)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } #else if (SDL_SetWindowFullscreen(self->_win, desktop ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_FULLSCREEN)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } #endif Py_RETURN_NONE; @@ -383,7 +386,7 @@ window_focus(pgWindowObject *self, PyObject *args, PyObject *kwargs) return NULL; } if (SDL_SetWindowInputFocus(self->_win)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } else { @@ -465,11 +468,12 @@ static PyObject * window_set_modal_for(pgWindowObject *self, PyObject *arg) { if (!pgWindow_Check(arg)) { - return RAISE(PyExc_TypeError, - "Argument to set_modal_for must be a Window."); + return RAISERETURN(PyExc_TypeError, + "Argument to set_modal_for must be a Window.", + NULL); } if (!PG_SetWindowModalFor(self->_win, ((pgWindowObject *)arg)->_win)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; } @@ -478,8 +482,8 @@ static PyObject * window_set_icon(pgWindowObject *self, PyObject *arg) { if (!pgSurface_Check(arg)) { - return RAISE(PyExc_TypeError, - "Argument to set_icon must be a Surface."); + return RAISERETURN(PyExc_TypeError, + "Argument to set_icon must be a Surface.", NULL); } SDL_SetWindowIcon(self->_win, pgSurface_AsSurface(arg)); Py_RETURN_NONE; @@ -664,7 +668,7 @@ window_get_window_id(pgWindowObject *self, PyObject *_null) { Uint32 window_id = SDL_GetWindowID(self->_win); if (!window_id) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return PyLong_FromLong(window_id); } @@ -883,12 +887,12 @@ window_get_opacity(pgWindowObject *self, void *v) #if SDL_VERSION_ATLEAST(3, 0, 0) float opacity = SDL_GetWindowOpacity(self->_win); if (opacity < 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } #else float opacity; if (SDL_GetWindowOpacity(self->_win, &opacity)) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } #endif return PyFloat_FromDouble((double)opacity); @@ -1271,8 +1275,8 @@ window_from_display_module(PyTypeObject *cls, PyObject *_null) SDL_Window *window = pg_GetDefaultWindow(); if (!window) { - return RAISE(pgExc_SDLError, - "display.set_mode has not been called yet."); + return RAISERETURN(pgExc_SDLError, + "display.set_mode has not been called yet.", NULL); } pgWindowObject *self = (pgWindowObject *)pg_get_pg_window(window); @@ -1294,15 +1298,18 @@ window_flash(pgWindowObject *self, PyObject *arg) #if SDL_VERSION_ATLEAST(2, 0, 16) long operation = PyLong_AsLong(arg); if (operation == -1 && PyErr_Occurred()) { - return RAISE(PyExc_TypeError, - "'operation' must be an integer. " - "Must correspond with FLASH_CANCEL, FLASH_BRIEFLY, or " - "FLASH_UNTIL_FOCUSED."); + return RAISERETURN( + PyExc_TypeError, + "'operation' must be an integer. " + "Must correspond with FLASH_CANCEL, FLASH_BRIEFLY, or " + "FLASH_UNTIL_FOCUSED.", + NULL); } if (operation != SDL_FLASH_CANCEL && operation != SDL_FLASH_BRIEFLY && operation != SDL_FLASH_UNTIL_FOCUSED) { - return RAISE(PyExc_ValueError, "Unsupported window flash operation."); + return RAISERETURN(PyExc_ValueError, + "Unsupported window flash operation.", NULL); } #if SDL_VERSION_ATLEAST(3, 0, 0) @@ -1310,11 +1317,12 @@ window_flash(pgWindowObject *self, PyObject *arg) #else if (SDL_FlashWindow(self->_win, operation) < 0) { #endif - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } Py_RETURN_NONE; #else - return RAISE(pgExc_SDLError, "'Window.flash' requires SDL 2.0.16+"); + return RAISERETURN(pgExc_SDLError, "'Window.flash' requires SDL 2.0.16+", + NULL); #endif /* SDL_VERSION_ATLEAST(2, 0, 16) */ } @@ -1334,7 +1342,7 @@ window_repr(pgWindowObject *self) title = SDL_GetWindowTitle(self->_win); win_id = SDL_GetWindowID(self->_win); if (win_id == 0) { - return RAISE(pgExc_SDLError, SDL_GetError()); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } return PyUnicode_FromFormat("", title, win_id); From 72572690c92ef48fe5643b230a601cd96f5f4ef7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josip=20Komljenovi=C4=87?= Date: Sun, 8 Jun 2025 21:51:04 +0200 Subject: [PATCH 6/8] Use macro for exception raising --- src_c/draw.c | 4 ++-- src_c/surface.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src_c/draw.c b/src_c/draw.c index abdd260454..b018b2666b 100644 --- a/src_c/draw.c +++ b/src_c/draw.c @@ -848,12 +848,12 @@ circle(PyObject *self, PyObject *args, PyObject *kwargs) if (!pg_TwoIntsFromObj(posobj, &posx, &posy)) { return RAISERETURN(PyExc_TypeError, - "center argument must be a pair of numbers", 0); + "center argument must be a pair of numbers", NULL); } if (!pg_IntFromObj(radiusobj, &radius)) { return RAISERETURN(PyExc_TypeError, "radius argument must be a number", - 0); + NULL); } surf = pgSurface_AsSurface(surfobj); diff --git a/src_c/surface.c b/src_c/surface.c index 82ba3c7ff0..116a1d8fbc 100644 --- a/src_c/surface.c +++ b/src_c/surface.c @@ -3664,14 +3664,14 @@ surf_get_view(PyObject *self, PyObject *args) case VIEWKIND_0D: if (surface->pitch != bpp * surface->w) { return RAISERETURN(PyExc_ValueError, - "Surface data is not contiguous", 0); + "Surface data is not contiguous", NULL); } get_buffer = _get_buffer_0D; break; case VIEWKIND_1D: if (surface->pitch != bpp * surface->w) { return RAISERETURN(PyExc_ValueError, - "Surface data is not contiguous", 0); + "Surface data is not contiguous", NULL); } get_buffer = _get_buffer_1D; break; From 76f35426dedbdb820b9e137f14fb32dec9c5adbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josip=20Komljenovi=C4=87?= Date: Sun, 8 Jun 2025 21:58:19 +0200 Subject: [PATCH 7/8] Use macro for exception raising --- src_c/bufferproxy.c | 10 +++++----- src_c/pixelarray.c | 2 +- src_c/pixelarray_methods.c | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src_c/bufferproxy.c b/src_c/bufferproxy.c index 1a4b976937..e8d1be9a2c 100644 --- a/src_c/bufferproxy.c +++ b/src_c/bufferproxy.c @@ -350,7 +350,7 @@ proxy_get_raw(pgBufferProxyObject *self, PyObject *closure) if (!PyBuffer_IsContiguous(view_p, 'A')) { _proxy_release_view(self); return RAISERETURN(PyExc_ValueError, "the bytes are not contiguous", - 0); + NULL); } py_raw = PyBytes_FromStringAndSize((char *)view_p->buf, view_p->len); if (!py_raw) { @@ -415,18 +415,18 @@ proxy_write(pgBufferProxyObject *self, PyObject *args, PyObject *kwds) proxy_releasebuffer(self, &view); Py_DECREF(self); return RAISERETURN(PyExc_ValueError, - "the BufferProxy bytes are not contiguous", 0); + "the BufferProxy bytes are not contiguous", NULL); } if (buflen > view.len) { proxy_releasebuffer(self, &view); Py_DECREF(self); return RAISERETURN(PyExc_ValueError, - "'buffer' object length is too large", 0); + "'buffer' object length is too large", NULL); } if (offset < 0 || buflen + offset > view.len) { proxy_releasebuffer(self, &view); Py_DECREF(self); - return RAISERETURN(PyExc_IndexError, "'offset' is out of range", 0); + return RAISERETURN(PyExc_IndexError, "'offset' is out of range", NULL); } memcpy((char *)view.buf + offset, buf, (size_t)buflen); proxy_releasebuffer(self, &view); @@ -535,7 +535,7 @@ pgBufferProxy_New(PyObject *obj, getbufferproc get_buffer) PyExc_ValueError, "One of arguments 'obj' or 'get_buffer' is required: " "both NULL instead", - 0); + NULL); } get_buffer = (getbufferproc)pgObject_GetBuffer; } diff --git a/src_c/pixelarray.c b/src_c/pixelarray.c index 09fd6ab268..2f67d7021e 100644 --- a/src_c/pixelarray.c +++ b/src_c/pixelarray.c @@ -305,7 +305,7 @@ _pxarray_new_internal(PyTypeObject *type, pgSurfaceObject *surface, PyExc_SystemError, "Pygame internal error in _pxarray_new_internal: no " "parent or surface.", - 0); + (pgPixelArrayObject *)NULL); } self->parent = 0; self->surface = surface; diff --git a/src_c/pixelarray_methods.c b/src_c/pixelarray_methods.c index 344326b4e2..06063e8c6d 100644 --- a/src_c/pixelarray_methods.c +++ b/src_c/pixelarray_methods.c @@ -817,7 +817,7 @@ _compare(pgPixelArrayObject *array, PyObject *args, PyObject *kwds) if (other_array->shape[0] != dim0 || other_array->shape[1] != dim1) { /* Bounds do not match. */ - return RAISERETURN(PyExc_ValueError, "array sizes do not match", 0); + return RAISERETURN(PyExc_ValueError, "array sizes do not match", NULL); } other_surf = pgSurface_AsSurface(other_array->surface); @@ -836,7 +836,7 @@ _compare(pgPixelArrayObject *array, PyObject *args, PyObject *kwds) if (PG_FORMAT_BytesPerPixel(other_format) != bpp) { /* bpp do not match. We cannot guarantee that the padding and co * would be set correctly. */ - return RAISERETURN(PyExc_ValueError, "bit depths do not match", 0); + return RAISERETURN(PyExc_ValueError, "bit depths do not match", NULL); } other_stride0 = other_array->strides[0]; From 07760adf7b6642fa699dc90ae005f235706e62b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josip=20Komljenovi=C4=87?= Date: Sun, 8 Jun 2025 22:06:49 +0200 Subject: [PATCH 8/8] Use macro for exception raising --- src_c/_freetype.c | 17 +++++++++-------- src_c/freetype/ft_layout.c | 2 +- src_c/freetype/ft_render.c | 4 ++-- src_c/freetype/ft_wrap.c | 4 ++-- src_c/newbuffer.c | 4 ++-- src_c/pixelcopy.c | 4 ++-- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src_c/_freetype.c b/src_c/_freetype.c index 34ed3a9149..a168213694 100644 --- a/src_c/_freetype.c +++ b/src_c/_freetype.c @@ -1137,7 +1137,7 @@ _ftfont_getpath(pgFontObject *self, void *closure) PyObject *path = ((pgFontObject *)self)->path; if (!path) { - return RAISERETURN(PyExc_AttributeError, "path unavailable", 0); + return RAISERETURN(PyExc_AttributeError, "path unavailable", NULL); } Py_INCREF(path); return path; @@ -1424,7 +1424,8 @@ get_metrics(FontRenderMode *render, pgFontObject *font, PGFT_String *text) Py_ssize_t i; if (!_PGFT_GetFontSized(font->freetype, font, render->face_size)) { - return RAISERETURN(pgExc_SDLError, _PGFT_GetError(font->freetype), 0); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(font->freetype), + NULL); } list = PyList_New(length); if (!list) { @@ -1533,7 +1534,7 @@ _ftfont_getsizedascender(pgFontObject *self, PyObject *args) PyExc_ValueError, "No font point size specified and no default font " "size in typefont", - 0); + NULL); } face_size = self->face_size; } @@ -1564,7 +1565,7 @@ _ftfont_getsizeddescender(pgFontObject *self, PyObject *args) PyExc_ValueError, "No font point size specified and no default font " "size in typefont", - 0); + NULL); } face_size = self->face_size; } @@ -1596,7 +1597,7 @@ _ftfont_getsizedheight(pgFontObject *self, PyObject *args) PyExc_ValueError, "No font point size specified and no default font " "size in typeface", - 0); + NULL); } face_size = self->face_size; } @@ -1627,7 +1628,7 @@ _ftfont_getsizedglyphheight(pgFontObject *self, PyObject *args) PyExc_ValueError, "No font point size specified and no default font " "size in typeface", - 0); + NULL); } face_size = self->face_size; } @@ -2094,7 +2095,7 @@ pgFont_New(const char *filename, long font_index) pgFontObject *font; FreeTypeInstance *ft; - ASSERT_GRAB_FREETYPE(ft, 0); + ASSERT_GRAB_FREETYPE(ft, NULL); if (!filename) { return 0; @@ -2195,7 +2196,7 @@ static PyObject * _ft_get_error(PyObject *self, PyObject *_null) { FreeTypeInstance *ft; - ASSERT_GRAB_FREETYPE(ft, 0); + ASSERT_GRAB_FREETYPE(ft, NULL); if (ft->_error_msg[0]) { return PyUnicode_FromString(ft->_error_msg); diff --git a/src_c/freetype/ft_layout.c b/src_c/freetype/ft_layout.c index 65a6d69007..95b5ed1da6 100644 --- a/src_c/freetype/ft_layout.c +++ b/src_c/freetype/ft_layout.c @@ -139,7 +139,7 @@ _PGFT_LoadLayout(FreeTypeInstance *ft, pgFontObject *fontobj, copy_mode(&ftext->mode, mode); font = _PGFT_GetFontSized(ft, fontobj, mode->face_size); if (!font) { - return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), NULL); } } diff --git a/src_c/freetype/ft_render.c b/src_c/freetype/ft_render.c index c300655b76..c33f03fdbb 100644 --- a/src_c/freetype/ft_render.c +++ b/src_c/freetype/ft_render.c @@ -434,13 +434,13 @@ _PGFT_Render_NewSurface(FreeTypeInstance *ft, pgFontObject *fontobj, } surface = PG_CreateSurface(width, height, pixelformat); if (!surface) { - return RAISERETURN(pgExc_SDLError, SDL_GetError(), 0); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } if (SDL_MUSTLOCK(surface)) { if (!PG_LockSurface(surface)) { SDL_FreeSurface(surface); - return RAISERETURN(pgExc_SDLError, SDL_GetError(), 0); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } locked = 1; } diff --git a/src_c/freetype/ft_wrap.c b/src_c/freetype/ft_wrap.c index 3f2f3964b3..8fdb809c2c 100644 --- a/src_c/freetype/ft_wrap.c +++ b/src_c/freetype/ft_wrap.c @@ -146,7 +146,7 @@ _PGFT_Font_GetName(FreeTypeInstance *ft, pgFontObject *fontobj) font = _PGFT_GetFont(ft, fontobj); if (!font) { - return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), NULL); } return font->family_name ? font->family_name : ""; } @@ -158,7 +158,7 @@ _PGFT_Font_GetStyleName(FreeTypeInstance *ft, pgFontObject *fontobj) font = _PGFT_GetFont(ft, fontobj); if (!font) { - return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), 0); + return RAISERETURN(pgExc_SDLError, _PGFT_GetError(ft), NULL); } return font->style_name ? font->style_name : ""; } diff --git a/src_c/newbuffer.c b/src_c/newbuffer.c index f84c741c4d..e420682283 100644 --- a/src_c/newbuffer.c +++ b/src_c/newbuffer.c @@ -308,7 +308,7 @@ buffer_get_buffer(BufferObject *self, PyObject *args, PyObject *kwds) } if (bufobj_flags & BUFOBJ_FILLED) { return RAISERETURN(PyExc_ValueError, - "The Py_buffer struct is already filled in", 0); + "The Py_buffer struct is already filled in", NULL); } self->flags = BUFOBJ_MUTABLE & bufobj_flags; if (!self->view_p) { @@ -750,7 +750,7 @@ Buffer_New(Py_buffer *view_p, int filled, int preserve) static PyObject * mixin__get_buffer(PyObject *self, PyObject *args) { - return RAISERETURN(PyExc_NotImplementedError, "abstract method", 0); + return RAISERETURN(PyExc_NotImplementedError, "abstract method", NULL); } static PyObject * diff --git a/src_c/pixelcopy.c b/src_c/pixelcopy.c index 7be5af0d81..aa3fe052ab 100644 --- a/src_c/pixelcopy.c +++ b/src_c/pixelcopy.c @@ -833,7 +833,7 @@ surface_to_array(PyObject *self, PyObject *args, PyObject *kwds) pgSurface_Unlock(surfobj); return RAISERETURN(PyExc_ValueError, "color planes only supported for 2d targets", - 0); + NULL); } if (_copy_unmapped(view_p, surf)) { pgBuffer_Release(&pg_view); @@ -1211,7 +1211,7 @@ make_surface(PyObject *self, PyObject *arg) if (!PG_SetPaletteColors(palette, default_palette_colors, 0, default_palette_size - 1)) { SDL_FreeSurface(surf); - return RAISERETURN(pgExc_SDLError, SDL_GetError(), 0); + return RAISERETURN(pgExc_SDLError, SDL_GetError(), NULL); } } surfobj = pgSurface_New(surf);