Skip to content

Commit 9b68e96

Browse files
committed
Replace get_surface with surface property
1 parent ff970cd commit 9b68e96

File tree

5 files changed

+23
-28
lines changed

5 files changed

+23
-28
lines changed

buildconfig/stubs/pygame/window.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class Window:
2626
def minimize(self) -> None: ...
2727
def set_modal_for(self, parent: Window, /) -> None: ...
2828
def set_icon(self, icon: Surface, /) -> None: ...
29-
def get_surface(self) -> Surface: ...
29+
@property
30+
def surface(self) -> Surface: ...
3031
def flip(self) -> None: ...
3132

3233
grab_mouse: bool

docs/reST/ref/window.rst

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,16 @@
241241

242242
.. deprecated:: 2.4.0
243243

244-
.. method:: get_surface
244+
.. attribute:: surface
245245

246246
| :sl:`Get the window surface`
247-
| :sg:`get_surface() -> Surface`
247+
| :sg:`surface -> Surface`
248248
249-
Returns a "display surface" for this Window. The surface returned is
249+
Get a "display surface" for this Window. The surface obtained by this is
250250
analogous to the surface returned by :func:`pygame.display.set_mode`.
251251

252-
This method allows software rendering (classic pygame rendering) on top
253-
of the Window API. This method should not be called when using hardware
252+
This property allows software rendering (classic pygame rendering) on top
253+
of the Window API. This attribute should not be used when using hardware
254254
rendering (coming soon).
255255

256256
Similarly to the "display surface" returned by :mod:`pygame.display`,
@@ -273,24 +273,21 @@
273273
of the Window API. This method should not be called when using hardware
274274
rendering (coming soon).
275275

276-
Here is a runnable example of using ``get_surface`` and ``flip``:
276+
Here is a runnable example of using ``surface`` property and ``flip``:
277277

278278
.. code-block:: python
279279
280280
import pygame
281281
282282
win = pygame.Window()
283-
surf = win.get_surface() # get the window surface
284-
285283
while True:
286284
for event in pygame.event.get():
287285
if event.type == pygame.QUIT:
288286
pygame.quit()
289287
raise SystemExit
290288
291289
# draw something on the surface
292-
surf.fill("red")
293-
290+
win.surface.fill("red")
294291
win.flip() # update the surface to the window
295292
296293

src_c/doc/window_doc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#define DOC_WINDOW_POSITION "position -> (int, int) or WINDOWPOS_CENTERED or WINDOWPOS_UNDEFINED\nGet or set the window position in screen coordinates"
1717
#define DOC_WINDOW_OPACITY "opacity -> float\nGet or set the window opacity, between 0.0 (fully transparent) and 1.0 (fully opaque)"
1818
#define DOC_WINDOW_FROMDISPLAYMODULE "from_display_module() -> Window\nCreate a Window object using window data from display module"
19-
#define DOC_WINDOW_GETSURFACE "get_surface() -> Surface\nGet the window surface"
19+
#define DOC_WINDOW_SURFACE "surface -> Surface\nGet the window surface"
2020
#define DOC_WINDOW_FLIP "flip() -> None\nUpdate the display surface to the window."
2121
#define DOC_WINDOW_SETWINDOWED "set_windowed() -> None\nEnable windowed mode (exit fullscreen)"
2222
#define DOC_WINDOW_SETFULLSCREEN "set_fullscreen(desktop=False) -> None\nEnter fullscreen"

src_c/window.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ window_destroy(pgWindowObject *self, PyObject *_null)
139139
}
140140

141141
static PyObject *
142-
window_get_surface(pgWindowObject *self)
142+
window_get_surface(pgWindowObject *self, void *v)
143143
{
144144
PyObject *surf = NULL;
145145
SDL_Surface *_surf;
@@ -1022,13 +1022,13 @@ static PyMethodDef window_methods[] = {
10221022
DOC_WINDOW_SETMODALFOR},
10231023
{"set_icon", (PyCFunction)window_set_icon, METH_O, DOC_WINDOW_SETICON},
10241024
{"flip", (PyCFunction)window_flip, METH_NOARGS, DOC_WINDOW_FLIP},
1025-
{"get_surface", (PyCFunction)window_get_surface, METH_NOARGS,
1026-
DOC_WINDOW_GETSURFACE},
10271025
{"from_display_module", (PyCFunction)window_from_display_module,
10281026
METH_CLASS | METH_NOARGS, DOC_WINDOW_FROMDISPLAYMODULE},
10291027
{NULL, NULL, 0, NULL}};
10301028

10311029
static PyGetSetDef _window_getset[] = {
1030+
{"surface", (getter)window_get_surface, NULL, DOC_WINDOW_SURFACE,
1031+
NULL},
10321032
{"grab_mouse", (getter)window_get_grab_mouse,
10331033
(setter)window_set_grab_mouse, DOC_WINDOW_GRABMOUSE, NULL},
10341034
{"grab_keyboard", (getter)window_get_grab_keyboard,

test/window_test.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -321,40 +321,37 @@ def test_from_display_module(self):
321321

322322
def test_window_surface(self):
323323
win = Window(size=(640, 480))
324-
surf = win.get_surface()
325324

326-
self.assertIsInstance(surf, pygame.Surface)
325+
self.assertIsInstance(win.surface, pygame.Surface)
327326

328327
# test auto resize
329-
self.assertTupleEqual(win.size, surf.get_size())
328+
self.assertTupleEqual(win.size, win.surface.get_size())
330329
win.size = (100, 100)
331-
self.assertTupleEqual(win.size, surf.get_size())
330+
self.assertTupleEqual(win.size, win.surface.get_size())
332331
win.size = (1280, 720)
333-
self.assertTupleEqual(win.size, surf.get_size())
332+
self.assertTupleEqual(win.size, win.surface.get_size())
334333

335334
# window surface should be invalid after the window is destroyed
336335
win.destroy()
337-
self.assertRaises(pygame.error, lambda: surf.fill((0, 0, 0)))
336+
self.assertRaises(pygame.error, lambda: win.surface.fill((0, 0, 0)))
338337

339338
def test_window_surface_with_display_module(self):
340-
# get_surface() should raise an error if the set_mode() is not called.
339+
# surface property should raise an error if the set_mode() is not called.
341340
pygame.display.set_mode((640, 480))
342341
win1 = Window.from_display_module()
343342
pygame.display.quit()
344343
pygame.init()
345-
self.assertRaises(pygame.error, lambda: win1.get_surface())
344+
self.assertRaises(pygame.error, lambda: win1.surface)
346345

347-
# the surface returned by get_surface() should be
346+
# the surface returned by surface property should be
348347
# the surface returned by set_mode()
349348
surf1 = pygame.display.set_mode((640, 480))
350349
win2 = Window.from_display_module()
351-
surf2 = win2.get_surface()
352-
self.assertIs(surf1, surf2)
350+
self.assertIs(surf1, win2.surface)
353351

354352
def test_window_flip(self):
355353
win = Window(size=(640, 480))
356-
surf = win.get_surface()
357-
surf.fill((255, 0, 0))
354+
win.surface.fill((255, 0, 0))
358355

359356
self.assertRaises(TypeError, lambda: win.flip("an argument"))
360357
self.assertIs(win.flip(), None)

0 commit comments

Comments
 (0)