Skip to content

Commit

Permalink
Merge pull request #1841 from nthykier/raise-on-del-attributes
Browse files Browse the repository at this point in the history
Reject attempts to delete attributes in that cannot be deleted
  • Loading branch information
illume committed May 26, 2020
2 parents a03f8a7 + 4cc5485 commit c10a68e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 3 deletions.
2 changes: 1 addition & 1 deletion buildconfig/Setup.SDL1.in
Expand Up @@ -69,4 +69,4 @@ bufferproxy src_c/bufferproxy.c $(SDL) $(DEBUG)
pixelarray src_c/pixelarray.c $(SDL) $(DEBUG)
math src_c/math.c $(SDL) $(DEBUG)
pixelcopy src_c/pixelcopy.c $(SDL) $(DEBUG)
newbuffer src_c/newbuffer.c $(DEBUG)
newbuffer src_c/newbuffer.c $(SDL) $(DEBUG)
2 changes: 1 addition & 1 deletion buildconfig/Setup.SDL2.in
Expand Up @@ -72,4 +72,4 @@ bufferproxy src_c/bufferproxy.c $(SDL) $(DEBUG)
pixelarray src_c/pixelarray.c $(SDL) $(DEBUG)
math src_c/math.c $(SDL) $(DEBUG)
pixelcopy src_c/pixelcopy.c $(SDL) $(DEBUG)
newbuffer src_c/newbuffer.c $(DEBUG)
newbuffer src_c/newbuffer.c $(SDL) $(DEBUG)
20 changes: 19 additions & 1 deletion src_c/_freetype.c
Expand Up @@ -1115,6 +1115,8 @@ _ftfont_setsize(pgFontObject *self, PyObject *value, void *closure)
{
Scale_t face_size;

DEL_ATTR_NOT_SUPPORTED_CHECK("size", value);

if (!obj_to_scale(value, &face_size))
goto error;
self->face_size = face_size;
Expand All @@ -1134,9 +1136,13 @@ static int
_ftfont_setunderlineadjustment(pgFontObject *self, PyObject *value,
void *closure)
{
PyObject *adjustmentobj = PyNumber_Float(value);
PyObject *adjustmentobj;
double adjustment;

DEL_ATTR_NOT_SUPPORTED_CHECK("underline_adjustment", value);

adjustmentobj = PyNumber_Float(value);

if (!adjustmentobj) {
return -1;
}
Expand Down Expand Up @@ -1236,6 +1242,9 @@ _ftfont_setrender_flag(pgFontObject *self, PyObject *value, void *closure)
{
const long render_flag = (long)closure;

/* Generic setter; We do not know the name of the attribute */
DEL_ATTR_NOT_SUPPORTED_CHECK(NULL, value);

if (!PyBool_Check(value)) {
PyErr_SetString(PyExc_TypeError, "The style value must be a boolean");
return -1;
Expand Down Expand Up @@ -1268,6 +1277,9 @@ _ftfont_getrotation(pgFontObject *self, void *closure)
static int
_ftfont_setrotation(pgFontObject *self, PyObject *value, void *closure)
{

DEL_ATTR_NOT_SUPPORTED_CHECK("rotation", value);

if (!self->is_scalable) {
if (pgFont_IS_ALIVE(self)) {
PyErr_SetString(PyExc_AttributeError,
Expand All @@ -1292,6 +1304,9 @@ _ftfont_getfgcolor(pgFontObject *self, void *closure)
static int
_ftfont_setfgcolor(pgFontObject *self, PyObject *value, void *closure)
{

DEL_ATTR_NOT_SUPPORTED_CHECK("fgcolor", value);

if (!pg_RGBAFromObj(value, self->fgcolor)) {
PyErr_Format(PyExc_AttributeError,
"unable to convert %128s object to a color",
Expand All @@ -1310,6 +1325,9 @@ _ftfont_getbgcolor(pgFontObject *self, void *closure)
static int
_ftfont_setbgcolor(pgFontObject *self, PyObject *value, void *closure)
{

DEL_ATTR_NOT_SUPPORTED_CHECK("bgcolor", value);

if (!pg_RGBAFromObj(value, self->bgcolor)) {
PyErr_Format(PyExc_AttributeError,
"unable to convert %128s object to a color",
Expand Down
14 changes: 14 additions & 0 deletions src_c/_pygame.h
Expand Up @@ -148,6 +148,20 @@ typedef enum {
#endif /* ~SDL_VERSION_ATLEAST(2, 0, 0) */

#define RAISE(x, y) (PyErr_SetString((x), (y)), (PyObject *)NULL)
#define DEL_ATTR_NOT_SUPPORTED_CHECK(name, value) \
do { \
if (!value) { \
if (name) { \
PyErr_Format(PyExc_AttributeError, \
"Cannot delete attribute %s", \
name); \
} else { \
PyErr_SetString(PyExc_AttributeError, \
"Cannot delete attribute"); \
} \
return -1; \
} \
} while (0)

/*
* Initialization checks
Expand Down
22 changes: 22 additions & 0 deletions src_c/color.c
Expand Up @@ -924,6 +924,9 @@ static int
_color_set_r(pgColorObject *color, PyObject *value, void *closure)
{
Uint32 c;

DEL_ATTR_NOT_SUPPORTED_CHECK("r", value);

if (!_get_color(value, &c)) {
return -1;
}
Expand Down Expand Up @@ -951,6 +954,9 @@ static int
_color_set_g(pgColorObject *color, PyObject *value, void *closure)
{
Uint32 c;

DEL_ATTR_NOT_SUPPORTED_CHECK("g", value);

if (!_get_color(value, &c)) {
return -1;
}
Expand Down Expand Up @@ -978,6 +984,9 @@ static int
_color_set_b(pgColorObject *color, PyObject *value, void *closure)
{
Uint32 c;

DEL_ATTR_NOT_SUPPORTED_CHECK("b", value);

if (!_get_color(value, &c)) {
return -1;
}
Expand Down Expand Up @@ -1005,6 +1014,9 @@ static int
_color_set_a(pgColorObject *color, PyObject *value, void *closure)
{
Uint32 c;

DEL_ATTR_NOT_SUPPORTED_CHECK("a", value);

if (!_get_color(value, &c)) {
return -1;
}
Expand Down Expand Up @@ -1074,6 +1086,9 @@ _color_set_hsva(pgColorObject *color, PyObject *value, void *closure)
double f, p, q, t, v, s;
int hi;


DEL_ATTR_NOT_SUPPORTED_CHECK("hsva", value);

if (!PySequence_Check(value) || PySequence_Size(value) < 3) {
PyErr_SetString(PyExc_ValueError, "invalid HSVA value");
return -1;
Expand Down Expand Up @@ -1237,6 +1252,8 @@ _color_set_hsla(pgColorObject *color, PyObject *value, void *closure)
double ht, h, q, p = 0, s, l = 0;
static double onethird = 1.0 / 3.0f;

DEL_ATTR_NOT_SUPPORTED_CHECK("hsla", value);

if (!PySequence_Check(value) || PySequence_Size(value) < 3) {
PyErr_SetString(PyExc_ValueError, "invalid HSLA value");
return -1;
Expand Down Expand Up @@ -1400,6 +1417,9 @@ _color_set_i1i2i3(pgColorObject *color, PyObject *value, void *closure)
double i1i2i3[3] = {0, 0, 0};
double ar, ag, ab;


DEL_ATTR_NOT_SUPPORTED_CHECK("i1i2i3", value);

/* I1 */
item = PySequence_GetItem(value, 0);
if (!item || !_get_double(item, &(i1i2i3[0])) || i1i2i3[0] < 0 ||
Expand Down Expand Up @@ -1465,6 +1485,8 @@ _color_set_cmy(pgColorObject *color, PyObject *value, void *closure)
PyObject *item;
double cmy[3] = {0, 0, 0};

DEL_ATTR_NOT_SUPPORTED_CHECK("cmy", value);

/* I1 */
item = PySequence_GetItem(value, 0);
if (!item || !_get_double(item, &(cmy[0])) || cmy[0] < 0 || cmy[0] > 1) {
Expand Down
25 changes: 25 additions & 0 deletions src_c/newbuffer.c
Expand Up @@ -18,6 +18,7 @@
*/

#include "pygame.h"
#include <Python.h>

#include "pgcompat.h"
Expand Down Expand Up @@ -393,6 +394,8 @@ buffer_set_obj(BufferObject *self, PyObject *value, void *closure)
{
PyObject *tmp;

DEL_ATTR_NOT_SUPPORTED_CHECK("obj", value);

if (check_view_set(self, (const char *)closure)) {
return -1;
}
Expand Down Expand Up @@ -428,6 +431,9 @@ buffer_get_buf(BufferObject *self, void *closure)
static int
buffer_set_buf(BufferObject *self, PyObject *value, void *closure)
{

DEL_ATTR_NOT_SUPPORTED_CHECK("buf", value);

if (check_view_set(self, (const char *)closure)) {
return -1;
}
Expand All @@ -446,6 +452,9 @@ buffer_get_len(BufferObject *self, void *closure)
static int
buffer_set_len(BufferObject *self, PyObject *value, void *closure)
{

DEL_ATTR_NOT_SUPPORTED_CHECK("len", value);

if (check_view_set(self, (const char *)closure)) {
return -1;
}
Expand All @@ -466,6 +475,8 @@ buffer_set_readonly(BufferObject *self, PyObject *value, void *closure)
{
int readonly = 1;

DEL_ATTR_NOT_SUPPORTED_CHECK("readonly", value);

if (check_view_set(self, (const char *)closure)) {
return -1;
}
Expand Down Expand Up @@ -497,6 +508,8 @@ buffer_set_format(BufferObject *self, PyObject *value, void *closure)
{
void *vp = 0;

DEL_ATTR_NOT_SUPPORTED_CHECK("format", value);

if (check_view_set(self, (const char *)closure)) {
return -1;
}
Expand All @@ -521,6 +534,8 @@ buffer_set_ndim(BufferObject *self, PyObject *value, void *closure)
{
Py_ssize_t ndim = 0;

DEL_ATTR_NOT_SUPPORTED_CHECK("mdim", value);

if (check_view_set(self, (const char *)closure)) {
return -1;
}
Expand Down Expand Up @@ -548,6 +563,8 @@ buffer_set_shape(BufferObject *self, PyObject *value, void *closure)
{
void *vp;

DEL_ATTR_NOT_SUPPORTED_CHECK("shape", value);

if (check_view_set(self, (const char *)closure)) {
return -1;
}
Expand Down Expand Up @@ -575,6 +592,8 @@ buffer_set_strides(BufferObject *self, PyObject *value, void *closure)
{
void *vp;

DEL_ATTR_NOT_SUPPORTED_CHECK("strides", value);

if (check_view_set(self, (const char *)closure)) {
return -1;
}
Expand Down Expand Up @@ -602,6 +621,8 @@ buffer_set_suboffsets(BufferObject *self, PyObject *value, void *closure)
{
void *vp;

DEL_ATTR_NOT_SUPPORTED_CHECK("suboffset", value);

if (check_view_set(self, (const char *)closure)) {
return -1;
}
Expand All @@ -624,6 +645,8 @@ buffer_get_itemsize(BufferObject *self, void *closure)
static int
buffer_set_itemsize(BufferObject *self, PyObject *value, void *closure)
{
DEL_ATTR_NOT_SUPPORTED_CHECK("itemsize", value);

if (check_view_set(self, (const char *)closure)) {
return -1;
}
Expand All @@ -646,6 +669,8 @@ buffer_get_internal(BufferObject *self, void *closure)
static int
buffer_set_internal(BufferObject *self, PyObject *value, void *closure)
{
DEL_ATTR_NOT_SUPPORTED_CHECK("internal", value);

if (check_view_set(self, (const char *)closure)) {
return -1;
}
Expand Down

0 comments on commit c10a68e

Please sign in to comment.