gh-123961: Remove global variable ModDict in _cursesmodule.c (#123962)

This commit is contained in:
Bénédikt Tran 2024-09-13 12:33:13 +02:00 committed by GitHub
parent e5b0185e43
commit 403f3ddedc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 44 additions and 38 deletions

View file

@ -3255,8 +3255,6 @@ _curses_init_pair_impl(PyObject *module, int pair_number, int fg, int bg)
Py_RETURN_NONE; Py_RETURN_NONE;
} }
static PyObject *ModDict;
/*[clinic input] /*[clinic input]
_curses.initscr _curses.initscr
@ -3285,19 +3283,23 @@ _curses_initscr_impl(PyObject *module)
initialised = initialised_setupterm = TRUE; initialised = initialised_setupterm = TRUE;
/* This was moved from initcurses() because it core dumped on SGI, PyObject *module_dict = PyModule_GetDict(module); // borrowed
where they're not defined until you've called initscr() */ if (module_dict == NULL) {
#define SetDictInt(NAME, VALUE) \ return NULL;
do { \ }
PyObject *value = PyLong_FromLong((long)(VALUE)); \ /* This was moved from initcurses() because it core dumped on SGI,
if (value == NULL) { \ where they're not defined until you've called initscr() */
return NULL; \ #define SetDictInt(NAME, VALUE) \
} \ do { \
int rc = PyDict_SetItemString(ModDict, (NAME), value); \ PyObject *value = PyLong_FromLong((long)(VALUE)); \
Py_DECREF(value); \ if (value == NULL) { \
if (rc < 0) { \ return NULL; \
return NULL; \ } \
} \ int rc = PyDict_SetItemString(module_dict, (NAME), value); \
Py_DECREF(value); \
if (rc < 0) { \
return NULL; \
} \
} while (0) } while (0)
/* Here are some graphic symbols you can use */ /* Here are some graphic symbols you can use */
@ -3976,11 +3978,11 @@ _curses_qiflush_impl(PyObject *module, int flag)
Py_RETURN_NONE; Py_RETURN_NONE;
} }
/* Internal helper used for updating curses.LINES, curses.COLS, _curses.LINES
* and _curses.COLS */
#if defined(HAVE_CURSES_RESIZETERM) || defined(HAVE_CURSES_RESIZE_TERM) #if defined(HAVE_CURSES_RESIZETERM) || defined(HAVE_CURSES_RESIZE_TERM)
/* Internal helper used for updating curses.LINES, curses.COLS, _curses.LINES
* and _curses.COLS. Returns 1 on success and 0 on failure. */
static int static int
update_lines_cols(void) update_lines_cols(PyObject *private_module)
{ {
PyObject *exposed_module = NULL, *o = NULL; PyObject *exposed_module = NULL, *o = NULL;
@ -3992,6 +3994,10 @@ update_lines_cols(void)
if (exposed_module_dict == NULL) { if (exposed_module_dict == NULL) {
goto error; goto error;
} }
PyObject *private_module_dict = PyModule_GetDict(private_module); // borrowed
if (private_module_dict == NULL) {
goto error;
}
o = PyLong_FromLong(LINES); o = PyLong_FromLong(LINES);
if (o == NULL) { if (o == NULL) {
@ -4000,7 +4006,7 @@ update_lines_cols(void)
if (PyDict_SetItemString(exposed_module_dict, "LINES", o) < 0) { if (PyDict_SetItemString(exposed_module_dict, "LINES", o) < 0) {
goto error; goto error;
} }
if (PyDict_SetItemString(ModDict, "LINES", o) < 0) { if (PyDict_SetItemString(private_module_dict, "LINES", o) < 0) {
goto error; goto error;
} }
Py_DECREF(o); Py_DECREF(o);
@ -4012,7 +4018,7 @@ update_lines_cols(void)
if (PyDict_SetItemString(exposed_module_dict, "COLS", o) < 0) { if (PyDict_SetItemString(exposed_module_dict, "COLS", o) < 0) {
goto error; goto error;
} }
if (PyDict_SetItemString(ModDict, "COLS", o) < 0) { if (PyDict_SetItemString(private_module_dict, "COLS", o) < 0) {
goto error; goto error;
} }
Py_DECREF(o); Py_DECREF(o);
@ -4034,7 +4040,7 @@ static PyObject *
_curses_update_lines_cols_impl(PyObject *module) _curses_update_lines_cols_impl(PyObject *module)
/*[clinic end generated code: output=423f2b1e63ed0f75 input=5f065ab7a28a5d90]*/ /*[clinic end generated code: output=423f2b1e63ed0f75 input=5f065ab7a28a5d90]*/
{ {
if (!update_lines_cols()) { if (!update_lines_cols(module)) {
return NULL; return NULL;
} }
Py_RETURN_NONE; Py_RETURN_NONE;
@ -4121,7 +4127,7 @@ _curses_resizeterm_impl(PyObject *module, int nlines, int ncols)
result = PyCursesCheckERR(resizeterm(nlines, ncols), "resizeterm"); result = PyCursesCheckERR(resizeterm(nlines, ncols), "resizeterm");
if (!result) if (!result)
return NULL; return NULL;
if (!update_lines_cols()) { if (!update_lines_cols(module)) {
Py_DECREF(result); Py_DECREF(result);
return NULL; return NULL;
} }
@ -4160,7 +4166,7 @@ _curses_resize_term_impl(PyObject *module, int nlines, int ncols)
result = PyCursesCheckERR(resize_term(nlines, ncols), "resize_term"); result = PyCursesCheckERR(resize_term(nlines, ncols), "resize_term");
if (!result) if (!result)
return NULL; return NULL;
if (!update_lines_cols()) { if (!update_lines_cols(module)) {
Py_DECREF(result); Py_DECREF(result);
return NULL; return NULL;
} }
@ -4232,17 +4238,21 @@ _curses_start_color_impl(PyObject *module)
initialisedcolors = TRUE; initialisedcolors = TRUE;
#define DICT_ADD_INT_VALUE(NAME, VALUE) \ PyObject *module_dict = PyModule_GetDict(module); // borrowed
do { \ if (module_dict == NULL) {
PyObject *value = PyLong_FromLong((long)(VALUE)); \ return NULL;
if (value == NULL) { \ }
return NULL; \ #define DICT_ADD_INT_VALUE(NAME, VALUE) \
} \ do { \
int rc = PyDict_SetItemString(ModDict, (NAME), value); \ PyObject *value = PyLong_FromLong((long)(VALUE)); \
Py_DECREF(value); \ if (value == NULL) { \
if (rc < 0) { \ return NULL; \
return NULL; \ } \
} \ int rc = PyDict_SetItemString(module_dict, (NAME), value); \
Py_DECREF(value); \
if (rc < 0) { \
return NULL; \
} \
} while (0) } while (0)
DICT_ADD_INT_VALUE("COLORS", COLORS); DICT_ADD_INT_VALUE("COLORS", COLORS);
@ -4779,7 +4789,6 @@ cursesmodule_exec(PyObject *module)
if (module_dict == NULL) { if (module_dict == NULL) {
return -1; return -1;
} }
ModDict = module_dict; /* For PyCurses_InitScr to use later */
void **PyCurses_API = PyMem_Calloc(PyCurses_API_pointers, sizeof(void *)); void **PyCurses_API = PyMem_Calloc(PyCurses_API_pointers, sizeof(void *));
if (PyCurses_API == NULL) { if (PyCurses_API == NULL) {

View file

@ -391,9 +391,6 @@ Modules/xxmodule.c - ErrorObject -
##----------------------- ##-----------------------
## other ## other
## initialized once
Modules/_cursesmodule.c - ModDict -
## state ## state
Modules/_datetimemodule.c - _datetime_global_state - Modules/_datetimemodule.c - _datetime_global_state -
Modules/_tkinter.c - tcl_lock - Modules/_tkinter.c - tcl_lock -

Can't render this file because it has a wrong number of fields in line 4.