Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 77b4604

Browse files
committed
Added close method for menus.
Check for closed objects in getattr (not perfect, but better).
1 parent cc59e94 commit 77b4604

1 file changed

Lines changed: 45 additions & 8 deletions

File tree

Modules/stdwinmodule.c

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ drawing_close(dp)
277277
INCREF(None);
278278
return None;
279279
}
280+
280281
static void
281282
drawing_dealloc(dp)
282283
drawingobject *dp;
@@ -760,11 +761,15 @@ static struct methodlist drawing_methods[] = {
760761
};
761762

762763
static object *
763-
drawing_getattr(wp, name)
764-
drawingobject *wp;
764+
drawing_getattr(dp, name)
765+
drawingobject *dp;
765766
char *name;
766767
{
767-
return findmethod(drawing_methods, (object *)wp, name);
768+
if (dp->d_ref == NULL) {
769+
err_setstr(StdwinError, "drawing object already closed");
770+
return NULL;
771+
}
772+
return findmethod(drawing_methods, (object *)dp, name);
768773
}
769774

770775
typeobject Drawingtype = {
@@ -1062,10 +1067,10 @@ static struct methodlist text_methods[] = {
10621067
{"draw", text_draw},
10631068
{"event", text_event},
10641069
{"getfocus", text_getfocus},
1065-
{"getfocustext", text_getfocustext},
1070+
{"getfocustext",text_getfocustext},
10661071
{"getrect", text_getrect},
10671072
{"gettext", text_gettext},
1068-
{"move", text_move},
1073+
{"move", text_move},
10691074
{"replace", text_replace},
10701075
{"setactive", text_setactive},
10711076
{"setfocus", text_setfocus},
@@ -1080,6 +1085,10 @@ text_getattr(tp, name)
10801085
char *name;
10811086
{
10821087
object *v = NULL;
1088+
if (tp->t_ref == NULL) {
1089+
err_setstr(StdwinError, "text object already closed");
1090+
return NULL;
1091+
}
10831092
if (strcmp(name, "__dict__") == 0) {
10841093
v = tp->t_attr;
10851094
if (v == NULL)
@@ -1175,12 +1184,31 @@ menu_dealloc(mp)
11751184
if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
11761185
menulist[id] = NULL;
11771186
}
1178-
wmenudelete(mp->m_menu);
1179-
if (mp->m_attr != NULL)
1180-
DECREF(mp->m_attr);
1187+
if (mp->m_menu != NULL)
1188+
wmenudelete(mp->m_menu);
1189+
XDECREF(mp->m_attr);
11811190
DEL(mp);
11821191
}
11831192

1193+
static object *
1194+
menu_close(mp, args)
1195+
menuobject *mp;
1196+
object *args;
1197+
{
1198+
int id = mp->m_id - IDOFFSET;
1199+
if (id >= 0 && id < MAXNMENU && menulist[id] == mp) {
1200+
menulist[id] = NULL;
1201+
}
1202+
mp->m_id = -1;
1203+
if (mp->m_menu != NULL)
1204+
wmenudelete(mp->m_menu);
1205+
mp->m_menu = NULL;
1206+
XDECREF(mp->m_attr);
1207+
mp->m_attr = NULL;
1208+
INCREF(None);
1209+
return None;
1210+
}
1211+
11841212
static object *
11851213
menu_additem(self, args)
11861214
menuobject *self;
@@ -1255,6 +1283,7 @@ static struct methodlist menu_methods[] = {
12551283
{"setitem", menu_setitem},
12561284
{"enable", menu_enable},
12571285
{"check", menu_check},
1286+
{"close", menu_close},
12581287
{NULL, NULL} /* sentinel */
12591288
};
12601289

@@ -1264,6 +1293,10 @@ menu_getattr(mp, name)
12641293
char *name;
12651294
{
12661295
object *v = NULL;
1296+
if (mp->m_menu == NULL) {
1297+
err_setstr(StdwinError, "menu object already closed");
1298+
return NULL;
1299+
}
12671300
if (strcmp(name, "__dict__") == 0) {
12681301
v = mp->m_attr;
12691302
if (v == NULL)
@@ -1647,6 +1680,10 @@ window_getattr(wp, name)
16471680
char *name;
16481681
{
16491682
object *v = NULL;
1683+
if (wp->w_win == NULL) {
1684+
err_setstr(StdwinError, "window already closed");
1685+
return NULL;
1686+
}
16501687
if (strcmp(name, "__dict__") == 0) {
16511688
v = wp->w_attr;
16521689
if (v == NULL)

0 commit comments

Comments
 (0)