Skip to content

Commit e9c4115

Browse files
authored
Made Surface.set_at() fastcall (#2330)
* made Surface.set_at() fastcall * fixes * fixes * changed PyLong_AsLong for PyLong_AsUnsignedLong * restored old error msg * Revert a fix from this PR that will be applied in the future
1 parent 8b0ec63 commit e9c4115

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src_c/surface.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ surface_move(Uint8 *src, Uint8 *dst, int h, int span, int srcpitch,
124124
static PyObject *
125125
surf_get_at(PyObject *self, PyObject *args);
126126
static PyObject *
127-
surf_set_at(PyObject *self, PyObject *args);
127+
surf_set_at(PyObject *self, PyObject *const *args, Py_ssize_t nargs);
128128
static PyObject *
129129
surf_get_at_mapped(PyObject *self, PyObject *args);
130130
static PyObject *
@@ -305,7 +305,7 @@ static PyGetSetDef surface_getsets[] = {
305305

306306
static struct PyMethodDef surface_methods[] = {
307307
{"get_at", surf_get_at, METH_O, DOC_SURFACE_GETAT},
308-
{"set_at", surf_set_at, METH_VARARGS, DOC_SURFACE_SETAT},
308+
{"set_at", (PyCFunction)surf_set_at, METH_FASTCALL, DOC_SURFACE_SETAT},
309309
{"get_at_mapped", surf_get_at_mapped, METH_O, DOC_SURFACE_GETATMAPPED},
310310
{"map_rgb", surf_map_rgb, METH_VARARGS, DOC_SURFACE_MAPRGB},
311311
{"unmap_rgb", surf_unmap_rgb, METH_O, DOC_SURFACE_UNMAPRGB},
@@ -834,29 +834,32 @@ surf_get_at(PyObject *self, PyObject *position)
834834
}
835835

836836
static PyObject *
837-
surf_set_at(PyObject *self, PyObject *args)
837+
surf_set_at(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
838838
{
839839
SDL_Surface *surf = pgSurface_AsSurface(self);
840+
SURF_INIT_CHECK(surf)
840841
SDL_PixelFormat *format = NULL;
841842
Uint8 *pixels;
842843
int x, y;
843-
PyObject *position;
844844
Uint32 color;
845845
Uint8 rgba[4] = {0, 0, 0, 0};
846846
PyObject *rgba_obj;
847847
Uint8 *byte_buf;
848848

849-
if (!PyArg_ParseTuple(args, "OO", &position, &rgba_obj))
850-
return NULL;
851-
SURF_INIT_CHECK(surf)
852-
853-
format = surf->format;
849+
if (nargs != 2) {
850+
return PyErr_Format(PyExc_TypeError,
851+
"set_at takes exactly 2 arguments (%zd given)",
852+
nargs);
853+
}
854854

855-
if (!pg_TwoIntsFromObj(position, &x, &y)) {
855+
if (!pg_TwoIntsFromObj(args[0], &x, &y)) {
856856
return RAISE(PyExc_TypeError,
857857
"position must be a sequence of two numbers");
858858
}
859859

860+
rgba_obj = args[1];
861+
862+
format = surf->format;
860863
if (format->BytesPerPixel < 1 || format->BytesPerPixel > 4)
861864
return RAISE(PyExc_RuntimeError, "invalid color depth for surface");
862865

@@ -917,6 +920,7 @@ surf_set_at(PyObject *self, PyObject *args)
917920

918921
if (!pgSurface_Unlock((pgSurfaceObject *)self))
919922
return NULL;
923+
920924
Py_RETURN_NONE;
921925
}
922926

0 commit comments

Comments
 (0)