From a468845b098a3602abdc3e9f9e6ef4b7a8cfcc16 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Wed, 10 Apr 2019 13:08:30 -0600 Subject: [PATCH 1/2] bpo-36589: Fix the error handling in curses.update_lines_cols() --- Modules/_cursesmodule.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 9a1d2efd256ea5..238a02e85eeb08 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -3705,39 +3705,39 @@ update_lines_cols(void) _Py_IDENTIFIER(COLS); if (!m) - return 0; + return -1; o = PyLong_FromLong(LINES); if (!o) { Py_DECREF(m); - return 0; + return -1; } if (_PyObject_SetAttrId(m, &PyId_LINES, o)) { Py_DECREF(m); Py_DECREF(o); - return 0; + return -1; } /* PyId_LINES.object will be initialized here. */ if (PyDict_SetItem(ModDict, PyId_LINES.object, o)) { Py_DECREF(m); Py_DECREF(o); - return 0; + return -1; } Py_DECREF(o); o = PyLong_FromLong(COLS); if (!o) { Py_DECREF(m); - return 0; + return -1; } if (_PyObject_SetAttrId(m, &PyId_COLS, o)) { Py_DECREF(m); Py_DECREF(o); - return 0; + return -1; } if (PyDict_SetItem(ModDict, PyId_COLS.object, o)) { Py_DECREF(m); Py_DECREF(o); - return 0; + return -1; } Py_DECREF(o); Py_DECREF(m); @@ -3837,8 +3837,10 @@ _curses_resizeterm_impl(PyObject *module, int nlines, int ncols) result = PyCursesCheckERR(resizeterm(nlines, ncols), "resizeterm"); if (!result) return NULL; - if (!update_lines_cols()) + if (update_lines_cols() < 0) { + Py_DECREF(result); return NULL; + } return result; } @@ -3874,8 +3876,10 @@ _curses_resize_term_impl(PyObject *module, int nlines, int ncols) result = PyCursesCheckERR(resize_term(nlines, ncols), "resize_term"); if (!result) return NULL; - if (!update_lines_cols()) + if (update_lines_cols() < 0) { + Py_DECREF(result); return NULL; + } return result; } #endif /* HAVE_CURSES_RESIZE_TERM */ @@ -3946,12 +3950,18 @@ _curses_start_color_impl(PyObject *module) c = PyLong_FromLong((long) COLORS); if (c == NULL) return NULL; - PyDict_SetItemString(ModDict, "COLORS", c); + if (PyDict_SetItemString(ModDict, "COLORS", c) < 0) { + Py_DECREF(c); + return NULL; + } Py_DECREF(c); cp = PyLong_FromLong((long) COLOR_PAIRS); if (cp == NULL) return NULL; - PyDict_SetItemString(ModDict, "COLOR_PAIRS", cp); + if (PyDict_SetItemString(ModDict, "COLOR_PAIRS", cp) < 0) { + Py_DECREF(cp); + return NULL; + } Py_DECREF(cp); Py_RETURN_NONE; } else { From bf5c2287d33593be7390b1cd0e7973b6f3a619c3 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Sat, 16 Nov 2019 22:57:10 -0700 Subject: [PATCH 2/2] Return None instead of 1. --- .../2019-11-16-22-56-51.bpo-36589.0Io76D.rst | 2 ++ Modules/_cursesmodule.c | 29 ++++++++++--------- Modules/clinic/_cursesmodule.c.h | 16 ++-------- 3 files changed, 21 insertions(+), 26 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2019-11-16-22-56-51.bpo-36589.0Io76D.rst diff --git a/Misc/NEWS.d/next/Library/2019-11-16-22-56-51.bpo-36589.0Io76D.rst b/Misc/NEWS.d/next/Library/2019-11-16-22-56-51.bpo-36589.0Io76D.rst new file mode 100644 index 00000000000000..3c1221b2034947 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-11-16-22-56-51.bpo-36589.0Io76D.rst @@ -0,0 +1,2 @@ +The :func:`curses.update_lines_cols` function now returns ``None`` instead +of ``1`` on success. diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 5ac27cea6ace0f..c2ce3a968faee7 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -3797,39 +3797,39 @@ update_lines_cols(void) _Py_IDENTIFIER(COLS); if (!m) - return -1; + return 0; o = PyLong_FromLong(LINES); if (!o) { Py_DECREF(m); - return -1; + return 0; } if (_PyObject_SetAttrId(m, &PyId_LINES, o)) { Py_DECREF(m); Py_DECREF(o); - return -1; + return 0; } /* PyId_LINES.object will be initialized here. */ if (PyDict_SetItem(ModDict, PyId_LINES.object, o)) { Py_DECREF(m); Py_DECREF(o); - return -1; + return 0; } Py_DECREF(o); o = PyLong_FromLong(COLS); if (!o) { Py_DECREF(m); - return -1; + return 0; } if (_PyObject_SetAttrId(m, &PyId_COLS, o)) { Py_DECREF(m); Py_DECREF(o); - return -1; + return 0; } if (PyDict_SetItem(ModDict, PyId_COLS.object, o)) { Py_DECREF(m); Py_DECREF(o); - return -1; + return 0; } Py_DECREF(o); Py_DECREF(m); @@ -3837,15 +3837,18 @@ update_lines_cols(void) } /*[clinic input] -_curses.update_lines_cols -> int +_curses.update_lines_cols [clinic start generated code]*/ -static int +static PyObject * _curses_update_lines_cols_impl(PyObject *module) -/*[clinic end generated code: output=0345e7f072ea711a input=3a87760f7d5197f0]*/ +/*[clinic end generated code: output=423f2b1e63ed0f75 input=5f065ab7a28a5d90]*/ { - return update_lines_cols(); + if (!update_lines_cols()) { + return NULL; + } + Py_RETURN_NONE; } #endif @@ -3929,7 +3932,7 @@ _curses_resizeterm_impl(PyObject *module, int nlines, int ncols) result = PyCursesCheckERR(resizeterm(nlines, ncols), "resizeterm"); if (!result) return NULL; - if (update_lines_cols() < 0) { + if (!update_lines_cols()) { Py_DECREF(result); return NULL; } @@ -3968,7 +3971,7 @@ _curses_resize_term_impl(PyObject *module, int nlines, int ncols) result = PyCursesCheckERR(resize_term(nlines, ncols), "resize_term"); if (!result) return NULL; - if (update_lines_cols() < 0) { + if (!update_lines_cols()) { Py_DECREF(result); return NULL; } diff --git a/Modules/clinic/_cursesmodule.c.h b/Modules/clinic/_cursesmodule.c.h index 7b30a49182f9ec..f3780f8e012f97 100644 --- a/Modules/clinic/_cursesmodule.c.h +++ b/Modules/clinic/_cursesmodule.c.h @@ -3921,23 +3921,13 @@ PyDoc_STRVAR(_curses_update_lines_cols__doc__, #define _CURSES_UPDATE_LINES_COLS_METHODDEF \ {"update_lines_cols", (PyCFunction)_curses_update_lines_cols, METH_NOARGS, _curses_update_lines_cols__doc__}, -static int +static PyObject * _curses_update_lines_cols_impl(PyObject *module); static PyObject * _curses_update_lines_cols(PyObject *module, PyObject *Py_UNUSED(ignored)) { - PyObject *return_value = NULL; - int _return_value; - - _return_value = _curses_update_lines_cols_impl(module); - if ((_return_value == -1) && PyErr_Occurred()) { - goto exit; - } - return_value = PyLong_FromLong((long)_return_value); - -exit: - return return_value; + return _curses_update_lines_cols_impl(module); } #endif /* (defined(HAVE_CURSES_RESIZETERM) || defined(HAVE_CURSES_RESIZE_TERM)) */ @@ -4691,4 +4681,4 @@ _curses_use_default_colors(PyObject *module, PyObject *Py_UNUSED(ignored)) #ifndef _CURSES_USE_DEFAULT_COLORS_METHODDEF #define _CURSES_USE_DEFAULT_COLORS_METHODDEF #endif /* !defined(_CURSES_USE_DEFAULT_COLORS_METHODDEF) */ -/*[clinic end generated code: output=985c0849e841acec input=a9049054013a1b77]*/ +/*[clinic end generated code: output=0ca4f95323c5d585 input=a9049054013a1b77]*/