Skip to content

Commit 403f3dd

Browse files
authored
gh-123961: Remove global variable ModDict in _cursesmodule.c (#123962)
1 parent e5b0185 commit 403f3dd

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

Modules/_cursesmodule.c

+44-35
Original file line numberDiff line numberDiff line change
@@ -3255,8 +3255,6 @@ _curses_init_pair_impl(PyObject *module, int pair_number, int fg, int bg)
32553255
Py_RETURN_NONE;
32563256
}
32573257

3258-
static PyObject *ModDict;
3259-
32603258
/*[clinic input]
32613259
_curses.initscr
32623260
@@ -3285,19 +3283,23 @@ _curses_initscr_impl(PyObject *module)
32853283

32863284
initialised = initialised_setupterm = TRUE;
32873285

3288-
/* This was moved from initcurses() because it core dumped on SGI,
3289-
where they're not defined until you've called initscr() */
3290-
#define SetDictInt(NAME, VALUE) \
3291-
do { \
3292-
PyObject *value = PyLong_FromLong((long)(VALUE)); \
3293-
if (value == NULL) { \
3294-
return NULL; \
3295-
} \
3296-
int rc = PyDict_SetItemString(ModDict, (NAME), value); \
3297-
Py_DECREF(value); \
3298-
if (rc < 0) { \
3299-
return NULL; \
3300-
} \
3286+
PyObject *module_dict = PyModule_GetDict(module); // borrowed
3287+
if (module_dict == NULL) {
3288+
return NULL;
3289+
}
3290+
/* This was moved from initcurses() because it core dumped on SGI,
3291+
where they're not defined until you've called initscr() */
3292+
#define SetDictInt(NAME, VALUE) \
3293+
do { \
3294+
PyObject *value = PyLong_FromLong((long)(VALUE)); \
3295+
if (value == NULL) { \
3296+
return NULL; \
3297+
} \
3298+
int rc = PyDict_SetItemString(module_dict, (NAME), value); \
3299+
Py_DECREF(value); \
3300+
if (rc < 0) { \
3301+
return NULL; \
3302+
} \
33013303
} while (0)
33023304

33033305
/* Here are some graphic symbols you can use */
@@ -3976,11 +3978,11 @@ _curses_qiflush_impl(PyObject *module, int flag)
39763978
Py_RETURN_NONE;
39773979
}
39783980

3979-
/* Internal helper used for updating curses.LINES, curses.COLS, _curses.LINES
3980-
* and _curses.COLS */
39813981
#if defined(HAVE_CURSES_RESIZETERM) || defined(HAVE_CURSES_RESIZE_TERM)
3982+
/* Internal helper used for updating curses.LINES, curses.COLS, _curses.LINES
3983+
* and _curses.COLS. Returns 1 on success and 0 on failure. */
39823984
static int
3983-
update_lines_cols(void)
3985+
update_lines_cols(PyObject *private_module)
39843986
{
39853987
PyObject *exposed_module = NULL, *o = NULL;
39863988

@@ -3992,6 +3994,10 @@ update_lines_cols(void)
39923994
if (exposed_module_dict == NULL) {
39933995
goto error;
39943996
}
3997+
PyObject *private_module_dict = PyModule_GetDict(private_module); // borrowed
3998+
if (private_module_dict == NULL) {
3999+
goto error;
4000+
}
39954001

39964002
o = PyLong_FromLong(LINES);
39974003
if (o == NULL) {
@@ -4000,7 +4006,7 @@ update_lines_cols(void)
40004006
if (PyDict_SetItemString(exposed_module_dict, "LINES", o) < 0) {
40014007
goto error;
40024008
}
4003-
if (PyDict_SetItemString(ModDict, "LINES", o) < 0) {
4009+
if (PyDict_SetItemString(private_module_dict, "LINES", o) < 0) {
40044010
goto error;
40054011
}
40064012
Py_DECREF(o);
@@ -4012,7 +4018,7 @@ update_lines_cols(void)
40124018
if (PyDict_SetItemString(exposed_module_dict, "COLS", o) < 0) {
40134019
goto error;
40144020
}
4015-
if (PyDict_SetItemString(ModDict, "COLS", o) < 0) {
4021+
if (PyDict_SetItemString(private_module_dict, "COLS", o) < 0) {
40164022
goto error;
40174023
}
40184024
Py_DECREF(o);
@@ -4034,7 +4040,7 @@ static PyObject *
40344040
_curses_update_lines_cols_impl(PyObject *module)
40354041
/*[clinic end generated code: output=423f2b1e63ed0f75 input=5f065ab7a28a5d90]*/
40364042
{
4037-
if (!update_lines_cols()) {
4043+
if (!update_lines_cols(module)) {
40384044
return NULL;
40394045
}
40404046
Py_RETURN_NONE;
@@ -4121,7 +4127,7 @@ _curses_resizeterm_impl(PyObject *module, int nlines, int ncols)
41214127
result = PyCursesCheckERR(resizeterm(nlines, ncols), "resizeterm");
41224128
if (!result)
41234129
return NULL;
4124-
if (!update_lines_cols()) {
4130+
if (!update_lines_cols(module)) {
41254131
Py_DECREF(result);
41264132
return NULL;
41274133
}
@@ -4160,7 +4166,7 @@ _curses_resize_term_impl(PyObject *module, int nlines, int ncols)
41604166
result = PyCursesCheckERR(resize_term(nlines, ncols), "resize_term");
41614167
if (!result)
41624168
return NULL;
4163-
if (!update_lines_cols()) {
4169+
if (!update_lines_cols(module)) {
41644170
Py_DECREF(result);
41654171
return NULL;
41664172
}
@@ -4232,17 +4238,21 @@ _curses_start_color_impl(PyObject *module)
42324238

42334239
initialisedcolors = TRUE;
42344240

4235-
#define DICT_ADD_INT_VALUE(NAME, VALUE) \
4236-
do { \
4237-
PyObject *value = PyLong_FromLong((long)(VALUE)); \
4238-
if (value == NULL) { \
4239-
return NULL; \
4240-
} \
4241-
int rc = PyDict_SetItemString(ModDict, (NAME), value); \
4242-
Py_DECREF(value); \
4243-
if (rc < 0) { \
4244-
return NULL; \
4245-
} \
4241+
PyObject *module_dict = PyModule_GetDict(module); // borrowed
4242+
if (module_dict == NULL) {
4243+
return NULL;
4244+
}
4245+
#define DICT_ADD_INT_VALUE(NAME, VALUE) \
4246+
do { \
4247+
PyObject *value = PyLong_FromLong((long)(VALUE)); \
4248+
if (value == NULL) { \
4249+
return NULL; \
4250+
} \
4251+
int rc = PyDict_SetItemString(module_dict, (NAME), value); \
4252+
Py_DECREF(value); \
4253+
if (rc < 0) { \
4254+
return NULL; \
4255+
} \
42464256
} while (0)
42474257

42484258
DICT_ADD_INT_VALUE("COLORS", COLORS);
@@ -4779,7 +4789,6 @@ cursesmodule_exec(PyObject *module)
47794789
if (module_dict == NULL) {
47804790
return -1;
47814791
}
4782-
ModDict = module_dict; /* For PyCurses_InitScr to use later */
47834792

47844793
void **PyCurses_API = PyMem_Calloc(PyCurses_API_pointers, sizeof(void *));
47854794
if (PyCurses_API == NULL) {

Tools/c-analyzer/cpython/globals-to-fix.tsv

-3
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,6 @@ Modules/xxmodule.c - ErrorObject -
391391
##-----------------------
392392
## other
393393

394-
## initialized once
395-
Modules/_cursesmodule.c - ModDict -
396-
397394
## state
398395
Modules/_datetimemodule.c - _datetime_global_state -
399396
Modules/_tkinter.c - tcl_lock -

0 commit comments

Comments
 (0)