Skip to content

Commit 51d61a3

Browse files
authored
Merge pull request #2345 from yunline/window-repr
Add `__repr__` for Window class
2 parents 3acd3a5 + e87ceb8 commit 51d61a3

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src_c/window.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,28 @@ window_from_display_module(PyTypeObject *cls, PyObject *_null)
713713
return (PyObject *)self;
714714
}
715715

716+
PyObject *
717+
window_repr(pgWindowObject *self)
718+
{
719+
const char *title;
720+
int win_id;
721+
if (!self->_win) {
722+
return PyUnicode_FromString("<Window(Destroyed)>");
723+
}
724+
725+
if (self->_is_borrowed) {
726+
return PyUnicode_FromString("<Window(From Display)>");
727+
}
728+
729+
title = SDL_GetWindowTitle(self->_win);
730+
win_id = SDL_GetWindowID(self->_win);
731+
if (win_id == 0) {
732+
return RAISE(pgExc_SDLError, SDL_GetError());
733+
}
734+
735+
return PyUnicode_FromFormat("<Window(title='%s', id=%d)>", title, win_id);
736+
}
737+
716738
static PyMethodDef window_methods[] = {
717739
{"destroy", (PyCFunction)window_destroy, METH_NOARGS,
718740
DOC_SDL2_VIDEO_WINDOW_DESTROY},
@@ -775,7 +797,8 @@ static PyTypeObject pgWindow_Type = {
775797
.tp_methods = window_methods,
776798
.tp_init = (initproc)window_init,
777799
.tp_new = PyType_GenericNew,
778-
.tp_getset = _window_getset};
800+
.tp_getset = _window_getset,
801+
.tp_repr = (reprfunc)window_repr};
779802

780803
static PyMethodDef _window_methods[] = {
781804
{"get_grabbed_window", (PyCFunction)get_grabbed_window, METH_NOARGS,

0 commit comments

Comments
 (0)