From 1d38c537939a946c736fc664bb0eaf534c8fcefd Mon Sep 17 00:00:00 2001 From: Jeffrey Kintscher Date: Tue, 21 May 2019 13:37:44 -0700 Subject: [PATCH 1/9] bpo-36982, bpo-36630: replumb these curses module functions to support using the extended color functions in ncurses 6.1 when available. color_content color_number init_color init_pair --- Modules/_cursesmodule.c | 103 +++++++++++++++-- Modules/clinic/_cursesmodule.c.h | 186 ++++++++++++++++++++----------- 2 files changed, 216 insertions(+), 73 deletions(-) diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 9a1d2efd256ea5..9356c11b4ed733 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -2581,11 +2581,30 @@ Return the red, green, and blue (RGB) components of the specified color. A 3-tuple is returned, containing the R, G, B values for the given color, which will be between 0 (no component) and 1000 (maximum amount of component). -[clinic start generated code]*/ +*/ + +#ifdef _NCURSES_EXTENDED_COLOR_FUNCS +static PyObject * +_curses_extended_color_content_impl(PyObject *module, int color_number) +{ + int r,g,b; + + PyCursesInitialised; + PyCursesInitialisedColor; + + if (extended_color_content(color_number, &r, &g, &b) != ERR) + return Py_BuildValue("(iii)", r, g, b); + else { + PyErr_SetString(PyCursesError, + "Argument 1 was out of range. Check value of COLORS."); + return NULL; + } +} + +#else /* _NCURSES_EXTENDED_COLOR_FUNCS */ static PyObject * _curses_color_content_impl(PyObject *module, short color_number) -/*[clinic end generated code: output=cb15cf3120d4bfc1 input=5555abb1c11e11b7]*/ { short r,g,b; @@ -2600,6 +2619,7 @@ _curses_color_content_impl(PyObject *module, short color_number) return NULL; } } +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ /*[clinic input] _curses.color_pair @@ -2612,11 +2632,10 @@ Return the attribute value for displaying text in the specified color. This attribute value can be combined with A_STANDOUT, A_REVERSE, and the other A_* attributes. pair_number() is the counterpart to this function. -[clinic start generated code]*/ +*/ static PyObject * -_curses_color_pair_impl(PyObject *module, short color_number) -/*[clinic end generated code: output=6a84cb6b29ecaf9a input=a9d3eb6f50e4dc12]*/ +_curses_color_pair_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number) { PyCursesInitialised; PyCursesInitialisedColor; @@ -3030,18 +3049,31 @@ Change the definition of a color. When init_color() is used, all occurrences of that color on the screen immediately change to the new definition. This function is a no-op on most terminals; it is active only if can_change_color() returns 1. -[clinic start generated code]*/ +*/ + +#ifdef _NCURSES_EXTENDED_COLOR_FUNCS +static PyObject * +_curses_init_extended_color_impl(PyObject *module, int color_number, int r, + int g, int b) +{ + PyCursesInitialised; + PyCursesInitialisedColor; + + return PyCursesCheckERR(init_extended_color(color_number, r, g, b), "init_extended_color"); +} + +#else /* _NCURSES_EXTENDED_COLOR_FUNCS */ static PyObject * _curses_init_color_impl(PyObject *module, short color_number, short r, short g, short b) -/*[clinic end generated code: output=280236f5efe9776a input=f3a05bd38f619175]*/ { PyCursesInitialised; PyCursesInitialisedColor; return PyCursesCheckERR(init_color(color_number, r, g, b), "init_color"); } +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ /*[clinic input] _curses.init_pair @@ -3058,7 +3090,37 @@ Change the definition of a color-pair. If the color-pair was previously initialized, the screen is refreshed and all occurrences of that color-pair are changed to the new definition. -[clinic start generated code]*/ +*/ + +/* +_curses._curses_init_extended_pair_impl + + pair_number: int + The number of the color-pair to be changed (1 - (COLOR_PAIRS-1)). + fg: int + Foreground color number (0 - COLORS). + bg: int + Background color number (0 - COLORS). + / + +Change the definition of a color-pair. + +If the color-pair was previously initialized, the screen is refreshed and +all occurrences of that color-pair are changed to the new definition. +*/ + +#ifdef _NCURSES_EXTENDED_COLOR_FUNCS +static PyObject * +_curses_init_extended_pair_impl(PyObject *module, int pair_number, int fg, + int bg) +{ + PyCursesInitialised; + PyCursesInitialisedColor; + + return PyCursesCheckERR(init_extended_pair(pair_number, fg, bg), "init_extended_pair"); +} + +#else /* _NCURSES_EXTENDED_COLOR_FUNCS */ static PyObject * _curses_init_pair_impl(PyObject *module, short pair_number, short fg, @@ -3070,6 +3132,7 @@ _curses_init_pair_impl(PyObject *module, short pair_number, short fg, return PyCursesCheckERR(init_pair(pair_number, fg, bg), "init_pair"); } +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ static PyObject *ModDict; @@ -3606,11 +3669,30 @@ _curses.pair_content / Return a tuple (fg, bg) containing the colors for the requested color pair. -[clinic start generated code]*/ +*/ + +#ifdef _NCURSES_EXTENDED_COLOR_FUNCS +static PyObject * +_curses_extended_pair_content_impl(PyObject *module, int pair_number) +{ + int f, b; + + PyCursesInitialised; + PyCursesInitialisedColor; + + if (extended_pair_content(pair_number, &f, &b)!=ERR) { + PyErr_SetString(PyCursesError, + "Argument 1 was out of range. (1..COLOR_PAIRS-1)"); + return NULL; + } + + return Py_BuildValue("(ii)", f, b); +} + +#else /* _NCURSES_EXTENDED_COLOR_FUNCS */ static PyObject * _curses_pair_content_impl(PyObject *module, short pair_number) -/*[clinic end generated code: output=5a72aa1a28bbacf3 input=f4d7fec5643b976b]*/ { short f, b; @@ -3625,6 +3707,7 @@ _curses_pair_content_impl(PyObject *module, short pair_number) return Py_BuildValue("(ii)", f, b); } +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ /*[clinic input] _curses.pair_number diff --git a/Modules/clinic/_cursesmodule.c.h b/Modules/clinic/_cursesmodule.c.h index 6837eac3909695..2f2446b375dee7 100644 --- a/Modules/clinic/_cursesmodule.c.h +++ b/Modules/clinic/_cursesmodule.c.h @@ -1,3 +1,22 @@ + +#if defined(NCURSES_EXT_COLORS) && defined(NCURSES_EXT_FUNCS) +#define _NCURSES_EXTENDED_COLOR_FUNCS 1 +#else +#define _NCURSES_EXTENDED_COLOR_FUNCS 0 +#endif /* defined(NCURSES_EXT_COLORS) && defined(NCURSES_EXT_FUNCS) */ + +#if _NCURSES_EXTENDED_COLOR_FUNCS +#define _NCURSES_COLOR_VAL_MAX INT_MAX +#define _NCURSES_COLOR_VAL_MIN INT_MIN +#define _NCURSES_COLOR_VAL_TYPE int +#define _NCURSES_COLOR_VAL_TYPE_STR "integer" +#else +#define _NCURSES_COLOR_VAL_MAX SHRT_MAX +#define _NCURSES_COLOR_VAL_MIN SHRT_MIN +#define _NCURSES_COLOR_VAL_TYPE short +#define _NCURSES_COLOR_VAL_TYPE_STR "short integer" +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ + /*[clinic input] preserve [clinic start generated code]*/ @@ -2036,14 +2055,20 @@ PyDoc_STRVAR(_curses_color_content__doc__, #define _CURSES_COLOR_CONTENT_METHODDEF \ {"color_content", (PyCFunction)_curses_color_content, METH_O, _curses_color_content__doc__}, +#if _NCURSES_EXTENDED_COLOR_FUNCS +#define _CURSES_COLOR_CONTENT_IMPL_FUNC _curses_extended_color_content_impl +#else +#define _CURSES_COLOR_CONTENT_IMPL_FUNC _curses_color_content_impl +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ + static PyObject * -_curses_color_content_impl(PyObject *module, short color_number); +_CURSES_COLOR_CONTENT_IMPL_FUNC(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number); static PyObject * _curses_color_content(PyObject *module, PyObject *arg) { PyObject *return_value = NULL; - short color_number; + _NCURSES_COLOR_VAL_TYPE color_number; if (PyFloat_Check(arg)) { PyErr_SetString(PyExc_TypeError, @@ -2055,26 +2080,30 @@ _curses_color_content(PyObject *module, PyObject *arg) if (ival == -1 && PyErr_Occurred()) { goto exit; } - else if (ival < SHRT_MIN) { + else if (ival < _NCURSES_COLOR_VAL_MIN) { PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); goto exit; } - else if (ival > SHRT_MAX) { + else if (ival > _NCURSES_COLOR_VAL_MAX) { PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); goto exit; } else { - color_number = (short) ival; + color_number = (_NCURSES_COLOR_VAL_TYPE) ival; } } - return_value = _curses_color_content_impl(module, color_number); + return_value = _CURSES_COLOR_CONTENT_IMPL_FUNC(module, color_number); exit: return return_value; } +#ifdef _CURSES_COLOR_CONTENT_IMPL_FUNC +#undef _CURSES_COLOR_CONTENT_IMPL_FUNC +#endif /* _CURSES_COLOR_CONTENT_IMPL_FUNC */ + PyDoc_STRVAR(_curses_color_pair__doc__, "color_pair($module, color_number, /)\n" "--\n" @@ -2091,13 +2120,13 @@ PyDoc_STRVAR(_curses_color_pair__doc__, {"color_pair", (PyCFunction)_curses_color_pair, METH_O, _curses_color_pair__doc__}, static PyObject * -_curses_color_pair_impl(PyObject *module, short color_number); +_curses_color_pair_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number); static PyObject * _curses_color_pair(PyObject *module, PyObject *arg) { PyObject *return_value = NULL; - short color_number; + _NCURSES_COLOR_VAL_TYPE color_number; if (PyFloat_Check(arg)) { PyErr_SetString(PyExc_TypeError, @@ -2109,18 +2138,18 @@ _curses_color_pair(PyObject *module, PyObject *arg) if (ival == -1 && PyErr_Occurred()) { goto exit; } - else if (ival < SHRT_MIN) { + else if (ival < _NCURSES_COLOR_VAL_MIN) { PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); goto exit; } - else if (ival > SHRT_MAX) { + else if (ival > _NCURSES_COLOR_VAL_MAX) { PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); + "signed " _NCURSES_COLOR_VAL_TYPE_STR "is greater than maximum"); goto exit; } else { - color_number = (short) ival; + color_number = (_NCURSES_COLOR_VAL_TYPE) ival; } } return_value = _curses_color_pair_impl(module, color_number); @@ -2714,18 +2743,25 @@ PyDoc_STRVAR(_curses_init_color__doc__, #define _CURSES_INIT_COLOR_METHODDEF \ {"init_color", (PyCFunction)(void(*)(void))_curses_init_color, METH_FASTCALL, _curses_init_color__doc__}, +#ifdef _NCURSES_EXTENDED_COLOR_FUNCS +#define _CURSES_INIT_COLOR_IMPL_FUNC _curses_init_extended_color_impl +#else +#define _CURSES_INIT_COLOR_IMPL_FUNC _curses_init_color_impl +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ + static PyObject * -_curses_init_color_impl(PyObject *module, short color_number, short r, - short g, short b); +_CURSES_INIT_COLOR_IMPL_FUNC(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number, + _NCURSES_COLOR_VAL_TYPE r, _NCURSES_COLOR_VAL_TYPE g, + _NCURSES_COLOR_VAL_TYPE b); static PyObject * _curses_init_color(PyObject *module, PyObject *const *args, Py_ssize_t nargs) { PyObject *return_value = NULL; - short color_number; - short r; - short g; - short b; + _NCURSES_COLOR_VAL_TYPE color_number; + _NCURSES_COLOR_VAL_TYPE r; + _NCURSES_COLOR_VAL_TYPE g; + _NCURSES_COLOR_VAL_TYPE b; if (!_PyArg_CheckPositional("init_color", nargs, 4, 4)) { goto exit; @@ -2740,18 +2776,18 @@ _curses_init_color(PyObject *module, PyObject *const *args, Py_ssize_t nargs) if (ival == -1 && PyErr_Occurred()) { goto exit; } - else if (ival < SHRT_MIN) { + else if (ival < _NCURSES_COLOR_VAL_MIN) { PyErr_SetString(PyExc_OverflowError, "signed short integer is less than minimum"); goto exit; } - else if (ival > SHRT_MAX) { + else if (ival > _NCURSES_COLOR_VAL_MAX) { PyErr_SetString(PyExc_OverflowError, "signed short integer is greater than maximum"); goto exit; } else { - color_number = (short) ival; + color_number = (_NCURSES_COLOR_VAL_TYPE) ival; } } if (PyFloat_Check(args[1])) { @@ -2764,18 +2800,18 @@ _curses_init_color(PyObject *module, PyObject *const *args, Py_ssize_t nargs) if (ival == -1 && PyErr_Occurred()) { goto exit; } - else if (ival < SHRT_MIN) { + else if (ival < _NCURSES_COLOR_VAL_MIN) { PyErr_SetString(PyExc_OverflowError, "signed short integer is less than minimum"); goto exit; } - else if (ival > SHRT_MAX) { + else if (ival > _NCURSES_COLOR_VAL_MAX) { PyErr_SetString(PyExc_OverflowError, "signed short integer is greater than maximum"); goto exit; } else { - r = (short) ival; + r = (_NCURSES_COLOR_VAL_TYPE) ival; } } if (PyFloat_Check(args[2])) { @@ -2788,18 +2824,18 @@ _curses_init_color(PyObject *module, PyObject *const *args, Py_ssize_t nargs) if (ival == -1 && PyErr_Occurred()) { goto exit; } - else if (ival < SHRT_MIN) { + else if (ival < _NCURSES_COLOR_VAL_MIN) { PyErr_SetString(PyExc_OverflowError, "signed short integer is less than minimum"); goto exit; } - else if (ival > SHRT_MAX) { + else if (ival > _NCURSES_COLOR_VAL_MAX) { PyErr_SetString(PyExc_OverflowError, "signed short integer is greater than maximum"); goto exit; } else { - g = (short) ival; + g = (_NCURSES_COLOR_VAL_TYPE) ival; } } if (PyFloat_Check(args[3])) { @@ -2812,26 +2848,30 @@ _curses_init_color(PyObject *module, PyObject *const *args, Py_ssize_t nargs) if (ival == -1 && PyErr_Occurred()) { goto exit; } - else if (ival < SHRT_MIN) { + else if (ival < _NCURSES_COLOR_VAL_MIN) { PyErr_SetString(PyExc_OverflowError, "signed short integer is less than minimum"); goto exit; } - else if (ival > SHRT_MAX) { + else if (ival > _NCURSES_COLOR_VAL_MAX) { PyErr_SetString(PyExc_OverflowError, "signed short integer is greater than maximum"); goto exit; } else { - b = (short) ival; + b = (_NCURSES_COLOR_VAL_TYPE) ival; } } - return_value = _curses_init_color_impl(module, color_number, r, g, b); + return_value = _CURSES_INIT_COLOR_IMPL_FUNC(module, color_number, r, g, b); exit: return return_value; } +#ifdef _CURSES_INIT_COLOR_IMPL_FUNC +#undef _CURSES_INIT_COLOR_IMPL_FUNC +#endif /* _CURSES_INIT_COLOR_IMPL_FUNC */ + PyDoc_STRVAR(_curses_init_pair__doc__, "init_pair($module, pair_number, fg, bg, /)\n" "--\n" @@ -2851,17 +2891,23 @@ PyDoc_STRVAR(_curses_init_pair__doc__, #define _CURSES_INIT_PAIR_METHODDEF \ {"init_pair", (PyCFunction)(void(*)(void))_curses_init_pair, METH_FASTCALL, _curses_init_pair__doc__}, +#if _NCURSES_EXTENDED_COLOR_FUNCS +#define _CURSES_INIT_PAIR_IMPL_FUNC _curses_init_extended_pair_impl +#else +#define _CURSES_INIT_PAIR_IMPL_FUNC _curses_init_pair_impl +#endif + static PyObject * -_curses_init_pair_impl(PyObject *module, short pair_number, short fg, - short bg); +_CURSES_INIT_PAIR_IMPL_FUNC(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number, + _NCURSES_COLOR_VAL_TYPE fg, _NCURSES_COLOR_VAL_TYPE bg); static PyObject * _curses_init_pair(PyObject *module, PyObject *const *args, Py_ssize_t nargs) { PyObject *return_value = NULL; - short pair_number; - short fg; - short bg; + _NCURSES_COLOR_VAL_TYPE pair_number; + _NCURSES_COLOR_VAL_TYPE fg; + _NCURSES_COLOR_VAL_TYPE bg; if (!_PyArg_CheckPositional("init_pair", nargs, 3, 3)) { goto exit; @@ -2876,18 +2922,18 @@ _curses_init_pair(PyObject *module, PyObject *const *args, Py_ssize_t nargs) if (ival == -1 && PyErr_Occurred()) { goto exit; } - else if (ival < SHRT_MIN) { + else if (ival < _NCURSES_COLOR_VAL_MIN) { PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); goto exit; } - else if (ival > SHRT_MAX) { + else if (ival > _NCURSES_COLOR_VAL_MAX) { PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); goto exit; } else { - pair_number = (short) ival; + pair_number = (_NCURSES_COLOR_VAL_TYPE) ival; } } if (PyFloat_Check(args[1])) { @@ -2900,18 +2946,18 @@ _curses_init_pair(PyObject *module, PyObject *const *args, Py_ssize_t nargs) if (ival == -1 && PyErr_Occurred()) { goto exit; } - else if (ival < SHRT_MIN) { + else if (ival < _NCURSES_COLOR_VAL_MIN) { PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); goto exit; } - else if (ival > SHRT_MAX) { + else if (ival > _NCURSES_COLOR_VAL_MAX) { PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); goto exit; } else { - fg = (short) ival; + fg = (_NCURSES_COLOR_VAL_TYPE) ival; } } if (PyFloat_Check(args[2])) { @@ -2924,26 +2970,30 @@ _curses_init_pair(PyObject *module, PyObject *const *args, Py_ssize_t nargs) if (ival == -1 && PyErr_Occurred()) { goto exit; } - else if (ival < SHRT_MIN) { + else if (ival < _NCURSES_COLOR_VAL_MIN) { PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); goto exit; } - else if (ival > SHRT_MAX) { + else if (ival > _NCURSES_COLOR_VAL_MAX) { PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); goto exit; } else { - bg = (short) ival; + bg = (_NCURSES_COLOR_VAL_TYPE) ival; } } - return_value = _curses_init_pair_impl(module, pair_number, fg, bg); + return_value = _CURSES_INIT_PAIR_IMPL_FUNC(module, pair_number, fg, bg); exit: return return_value; } +#ifdef _CURSES_INIT_PAIR_IMPL_FUNC +#undef _CURSES_INIT_PAIR_IMPL_FUNC +#endif /* _CURSES_INIT_PAIR_IMPL_FUNC */ + PyDoc_STRVAR(_curses_initscr__doc__, "initscr($module, /)\n" "--\n" @@ -3640,14 +3690,20 @@ PyDoc_STRVAR(_curses_pair_content__doc__, #define _CURSES_PAIR_CONTENT_METHODDEF \ {"pair_content", (PyCFunction)_curses_pair_content, METH_O, _curses_pair_content__doc__}, +#ifdef _NCURSES_EXTENDED_COLOR_FUNCS +#define _CURSES_PAIR_CONTENT_IMPL_FUNC _curses_extended_pair_content_impl +#else +#define _CURSES_PAIR_CONTENT_IMPL_FUNC _curses_pair_content_impl +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ + static PyObject * -_curses_pair_content_impl(PyObject *module, short pair_number); +_CURSES_PAIR_CONTENT_IMPL_FUNC(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number); static PyObject * _curses_pair_content(PyObject *module, PyObject *arg) { PyObject *return_value = NULL; - short pair_number; + _NCURSES_COLOR_VAL_TYPE pair_number; if (PyFloat_Check(arg)) { PyErr_SetString(PyExc_TypeError, @@ -3659,26 +3715,30 @@ _curses_pair_content(PyObject *module, PyObject *arg) if (ival == -1 && PyErr_Occurred()) { goto exit; } - else if (ival < SHRT_MIN) { + else if (ival < _NCURSES_COLOR_VAL_MIN) { PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); goto exit; } - else if (ival > SHRT_MAX) { + else if (ival > _NCURSES_COLOR_VAL_MAX) { PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); goto exit; } else { - pair_number = (short) ival; + pair_number = (_NCURSES_COLOR_VAL_TYPE) ival; } } - return_value = _curses_pair_content_impl(module, pair_number); + return_value = _CURSES_PAIR_CONTENT_IMPL_FUNC(module, pair_number); exit: return return_value; } +#ifdef _CURSES_PAIR_CONTENT_IMPL_FUNC +#undef _CURSES_PAIR_CONTENT_IMPL_FUNC +#endif /* _CURSES_PAIR_CONTENT_IMPL_FUNC */ + PyDoc_STRVAR(_curses_pair_number__doc__, "pair_number($module, attr, /)\n" "--\n" From 899fc201f6f6b561221335819bcf95158d984403 Mon Sep 17 00:00:00 2001 From: Jeffrey Kintscher Date: Wed, 22 May 2019 00:35:00 -0700 Subject: [PATCH 2/9] bpo-36982, bpo-36630: refactor the curses color implementation functions to make them simpler and more maintainable --- Modules/_cursesmodule.c | 137 ++++++++++--------------------- Modules/clinic/_cursesmodule.c.h | 65 +++++---------- 2 files changed, 65 insertions(+), 137 deletions(-) diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 9356c11b4ed733..27eaf183dd4409 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -396,6 +396,10 @@ static int func_PyCursesInitialisedColor(void) return 1; } +/* _CURSES_FUNC_NAME_STR() is used by some of the */ +/* color functions that have extended color alternatives */ +#define _CURSES_FUNC_NAME_STR(s) #s + /***************************************************************************** The Window Object ******************************************************************************/ @@ -2583,35 +2587,21 @@ A 3-tuple is returned, containing the R, G, B values for the given color, which will be between 0 (no component) and 1000 (maximum amount of component). */ -#ifdef _NCURSES_EXTENDED_COLOR_FUNCS -static PyObject * -_curses_extended_color_content_impl(PyObject *module, int color_number) -{ - int r,g,b; - - PyCursesInitialised; - PyCursesInitialisedColor; - - if (extended_color_content(color_number, &r, &g, &b) != ERR) - return Py_BuildValue("(iii)", r, g, b); - else { - PyErr_SetString(PyCursesError, - "Argument 1 was out of range. Check value of COLORS."); - return NULL; - } -} - -#else /* _NCURSES_EXTENDED_COLOR_FUNCS */ +#if _NCURSES_EXTENDED_COLOR_FUNCS +#define _COLOR_CONTENT_FUNC extended_color_content +#else +#define _COLOR_CONTENT_FUNC color_content +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ static PyObject * -_curses_color_content_impl(PyObject *module, short color_number) +_curses_color_content_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number) { - short r,g,b; + _NCURSES_COLOR_VAL_TYPE r,g,b; PyCursesInitialised; PyCursesInitialisedColor; - if (color_content(color_number, &r, &g, &b) != ERR) + if (_COLOR_CONTENT_FUNC(color_number, &r, &g, &b) != ERR) return Py_BuildValue("(iii)", r, g, b); else { PyErr_SetString(PyCursesError, @@ -2619,7 +2609,8 @@ _curses_color_content_impl(PyObject *module, short color_number) return NULL; } } -#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ + +#undef _COLOR_CONTENT_FUNC /*[clinic input] _curses.color_pair @@ -3052,28 +3043,26 @@ most terminals; it is active only if can_change_color() returns 1. */ #ifdef _NCURSES_EXTENDED_COLOR_FUNCS -static PyObject * -_curses_init_extended_color_impl(PyObject *module, int color_number, int r, - int g, int b) -{ - PyCursesInitialised; - PyCursesInitialisedColor; - - return PyCursesCheckERR(init_extended_color(color_number, r, g, b), "init_extended_color"); -} +#define _CURSES_INIT_COLOR_FUNC init_extended_color +#else +#define _CURSES_INIT_COLOR_FUNC init_color +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ -#else /* _NCURSES_EXTENDED_COLOR_FUNCS */ +#define _CURSES_INIT_COLOR_FUNC_NAME _CURSES_FUNC_NAME_STR(_CURSES_INIT_COLOR_FUNC) static PyObject * -_curses_init_color_impl(PyObject *module, short color_number, short r, - short g, short b) +_curses_init_color_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number, + _NCURSES_COLOR_VAL_TYPE r, _NCURSES_COLOR_VAL_TYPE g, + _NCURSES_COLOR_VAL_TYPE b) { PyCursesInitialised; PyCursesInitialisedColor; - return PyCursesCheckERR(init_color(color_number, r, g, b), "init_color"); + return PyCursesCheckERR(_CURSES_INIT_COLOR_FUNC(color_number, r, g, b), _CURSES_INIT_COLOR_FUNC_NAME); } -#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ + +#undef _CURSES_INIT_COLOR_FUNC +#undef _CURSES_INIT_COLOR_FUNC_NAME /*[clinic input] _curses.init_pair @@ -3092,47 +3081,26 @@ If the color-pair was previously initialized, the screen is refreshed and all occurrences of that color-pair are changed to the new definition. */ -/* -_curses._curses_init_extended_pair_impl - - pair_number: int - The number of the color-pair to be changed (1 - (COLOR_PAIRS-1)). - fg: int - Foreground color number (0 - COLORS). - bg: int - Background color number (0 - COLORS). - / - -Change the definition of a color-pair. - -If the color-pair was previously initialized, the screen is refreshed and -all occurrences of that color-pair are changed to the new definition. -*/ - #ifdef _NCURSES_EXTENDED_COLOR_FUNCS -static PyObject * -_curses_init_extended_pair_impl(PyObject *module, int pair_number, int fg, - int bg) -{ - PyCursesInitialised; - PyCursesInitialisedColor; - - return PyCursesCheckERR(init_extended_pair(pair_number, fg, bg), "init_extended_pair"); -} +#define _CURSES_INIT_PAIR_FUNC init_extended_pair +#else +#define _CURSES_INIT_PAIR_FUNC init_pair +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ -#else /* _NCURSES_EXTENDED_COLOR_FUNCS */ +#define _CURSES_INIT_PAIR_FUNC_NAME _CURSES_FUNC_NAME_STR(_CURSES_INIT_PAIR_FUNC) static PyObject * -_curses_init_pair_impl(PyObject *module, short pair_number, short fg, - short bg) -/*[clinic end generated code: output=9c2ce39c22f376b6 input=c9f0b11b17a2ac6d]*/ +_curses_init_pair_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number, + _NCURSES_COLOR_VAL_TYPE fg, _NCURSES_COLOR_VAL_TYPE bg) { PyCursesInitialised; PyCursesInitialisedColor; - return PyCursesCheckERR(init_pair(pair_number, fg, bg), "init_pair"); + return PyCursesCheckERR(_CURSES_INIT_PAIR_FUNC(pair_number, fg, bg), _CURSES_INIT_PAIR_FUNC_NAME); } -#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ + +#undef _CURSES_INIT_PAIR_FUNC +#undef _CURSES_INIT_PAIR_FUNC_NAME static PyObject *ModDict; @@ -3671,35 +3639,21 @@ _curses.pair_content Return a tuple (fg, bg) containing the colors for the requested color pair. */ -#ifdef _NCURSES_EXTENDED_COLOR_FUNCS -static PyObject * -_curses_extended_pair_content_impl(PyObject *module, int pair_number) -{ - int f, b; - - PyCursesInitialised; - PyCursesInitialisedColor; - - if (extended_pair_content(pair_number, &f, &b)!=ERR) { - PyErr_SetString(PyCursesError, - "Argument 1 was out of range. (1..COLOR_PAIRS-1)"); - return NULL; - } - - return Py_BuildValue("(ii)", f, b); -} - -#else /* _NCURSES_EXTENDED_COLOR_FUNCS */ +#if _NCURSES_EXTENDED_COLOR_FUNCS +#define _CURSES_PAIR_NUMBER_FUNC extended_pair_content +#else +#define _CURSES_PAIR_NUMBER_FUNC pair_content +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ static PyObject * -_curses_pair_content_impl(PyObject *module, short pair_number) +_curses_pair_content_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number) { - short f, b; + _NCURSES_COLOR_VAL_TYPE f, b; PyCursesInitialised; PyCursesInitialisedColor; - if (pair_content(pair_number, &f, &b)==ERR) { + if (_CURSES_PAIR_NUMBER_FUNC(pair_number, &f, &b)==ERR) { PyErr_SetString(PyCursesError, "Argument 1 was out of range. (1..COLOR_PAIRS-1)"); return NULL; @@ -3707,7 +3661,6 @@ _curses_pair_content_impl(PyObject *module, short pair_number) return Py_BuildValue("(ii)", f, b); } -#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ /*[clinic input] _curses.pair_number diff --git a/Modules/clinic/_cursesmodule.c.h b/Modules/clinic/_cursesmodule.c.h index 2f2446b375dee7..81f5ad1afee702 100644 --- a/Modules/clinic/_cursesmodule.c.h +++ b/Modules/clinic/_cursesmodule.c.h @@ -1,20 +1,19 @@ - #if defined(NCURSES_EXT_COLORS) && defined(NCURSES_EXT_FUNCS) #define _NCURSES_EXTENDED_COLOR_FUNCS 1 #else #define _NCURSES_EXTENDED_COLOR_FUNCS 0 -#endif /* defined(NCURSES_EXT_COLORS) && defined(NCURSES_EXT_FUNCS) */ +#endif /* defined(NCURSES_EXT_COLORS) && defined(NCURSES_EXT_FUNCS) */ #if _NCURSES_EXTENDED_COLOR_FUNCS -#define _NCURSES_COLOR_VAL_MAX INT_MAX -#define _NCURSES_COLOR_VAL_MIN INT_MIN -#define _NCURSES_COLOR_VAL_TYPE int -#define _NCURSES_COLOR_VAL_TYPE_STR "integer" +#define _NCURSES_COLOR_VAL_MAX INT_MAX +#define _NCURSES_COLOR_VAL_MIN INT_MIN +#define _NCURSES_COLOR_VAL_TYPE int +#define _NCURSES_COLOR_VAL_TYPE_STR "integer" #else -#define _NCURSES_COLOR_VAL_MAX SHRT_MAX -#define _NCURSES_COLOR_VAL_MIN SHRT_MIN -#define _NCURSES_COLOR_VAL_TYPE short -#define _NCURSES_COLOR_VAL_TYPE_STR "short integer" +#define _NCURSES_COLOR_VAL_MAX SHRT_MAX +#define _NCURSES_COLOR_VAL_MIN SHRT_MIN +#define _NCURSES_COLOR_VAL_TYPE short +#define _NCURSES_COLOR_VAL_TYPE_STR "short integer" #endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ /*[clinic input] @@ -2055,14 +2054,8 @@ PyDoc_STRVAR(_curses_color_content__doc__, #define _CURSES_COLOR_CONTENT_METHODDEF \ {"color_content", (PyCFunction)_curses_color_content, METH_O, _curses_color_content__doc__}, -#if _NCURSES_EXTENDED_COLOR_FUNCS -#define _CURSES_COLOR_CONTENT_IMPL_FUNC _curses_extended_color_content_impl -#else -#define _CURSES_COLOR_CONTENT_IMPL_FUNC _curses_color_content_impl -#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ - static PyObject * -_CURSES_COLOR_CONTENT_IMPL_FUNC(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number); +_curses_color_content_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number); static PyObject * _curses_color_content(PyObject *module, PyObject *arg) @@ -2094,7 +2087,7 @@ _curses_color_content(PyObject *module, PyObject *arg) color_number = (_NCURSES_COLOR_VAL_TYPE) ival; } } - return_value = _CURSES_COLOR_CONTENT_IMPL_FUNC(module, color_number); + return_value = _curses_color_content_impl(module, color_number); exit: return return_value; @@ -2743,16 +2736,10 @@ PyDoc_STRVAR(_curses_init_color__doc__, #define _CURSES_INIT_COLOR_METHODDEF \ {"init_color", (PyCFunction)(void(*)(void))_curses_init_color, METH_FASTCALL, _curses_init_color__doc__}, -#ifdef _NCURSES_EXTENDED_COLOR_FUNCS -#define _CURSES_INIT_COLOR_IMPL_FUNC _curses_init_extended_color_impl -#else -#define _CURSES_INIT_COLOR_IMPL_FUNC _curses_init_color_impl -#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ - static PyObject * -_CURSES_INIT_COLOR_IMPL_FUNC(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number, - _NCURSES_COLOR_VAL_TYPE r, _NCURSES_COLOR_VAL_TYPE g, - _NCURSES_COLOR_VAL_TYPE b); +_curses_init_color_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number, + _NCURSES_COLOR_VAL_TYPE r, _NCURSES_COLOR_VAL_TYPE g, + _NCURSES_COLOR_VAL_TYPE b); static PyObject * _curses_init_color(PyObject *module, PyObject *const *args, Py_ssize_t nargs) @@ -2862,7 +2849,7 @@ _curses_init_color(PyObject *module, PyObject *const *args, Py_ssize_t nargs) b = (_NCURSES_COLOR_VAL_TYPE) ival; } } - return_value = _CURSES_INIT_COLOR_IMPL_FUNC(module, color_number, r, g, b); + return_value = _curses_init_color_impl(module, color_number, r, g, b); exit: return return_value; @@ -2891,15 +2878,9 @@ PyDoc_STRVAR(_curses_init_pair__doc__, #define _CURSES_INIT_PAIR_METHODDEF \ {"init_pair", (PyCFunction)(void(*)(void))_curses_init_pair, METH_FASTCALL, _curses_init_pair__doc__}, -#if _NCURSES_EXTENDED_COLOR_FUNCS -#define _CURSES_INIT_PAIR_IMPL_FUNC _curses_init_extended_pair_impl -#else -#define _CURSES_INIT_PAIR_IMPL_FUNC _curses_init_pair_impl -#endif - static PyObject * -_CURSES_INIT_PAIR_IMPL_FUNC(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number, - _NCURSES_COLOR_VAL_TYPE fg, _NCURSES_COLOR_VAL_TYPE bg); +_curses_init_pair_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number, + _NCURSES_COLOR_VAL_TYPE fg, _NCURSES_COLOR_VAL_TYPE bg); static PyObject * _curses_init_pair(PyObject *module, PyObject *const *args, Py_ssize_t nargs) @@ -2984,7 +2965,7 @@ _curses_init_pair(PyObject *module, PyObject *const *args, Py_ssize_t nargs) bg = (_NCURSES_COLOR_VAL_TYPE) ival; } } - return_value = _CURSES_INIT_PAIR_IMPL_FUNC(module, pair_number, fg, bg); + return_value = _curses_init_pair_impl(module, pair_number, fg, bg); exit: return return_value; @@ -3690,14 +3671,8 @@ PyDoc_STRVAR(_curses_pair_content__doc__, #define _CURSES_PAIR_CONTENT_METHODDEF \ {"pair_content", (PyCFunction)_curses_pair_content, METH_O, _curses_pair_content__doc__}, -#ifdef _NCURSES_EXTENDED_COLOR_FUNCS -#define _CURSES_PAIR_CONTENT_IMPL_FUNC _curses_extended_pair_content_impl -#else -#define _CURSES_PAIR_CONTENT_IMPL_FUNC _curses_pair_content_impl -#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ - static PyObject * -_CURSES_PAIR_CONTENT_IMPL_FUNC(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number); +_curses_pair_content_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number); static PyObject * _curses_pair_content(PyObject *module, PyObject *arg) @@ -3729,7 +3704,7 @@ _curses_pair_content(PyObject *module, PyObject *arg) pair_number = (_NCURSES_COLOR_VAL_TYPE) ival; } } - return_value = _CURSES_PAIR_CONTENT_IMPL_FUNC(module, pair_number); + return_value = _curses_pair_content_impl(module, pair_number); exit: return return_value; From b54ab0d8d3ea376cc3763cd5d24bc27060e45b04 Mon Sep 17 00:00:00 2001 From: Jeffrey Kintscher Date: Wed, 22 May 2019 17:37:41 -0700 Subject: [PATCH 3/9] bpo-36982, bpo-36630: _NCURSES_EXTENDED_COLOR_FUNCS is always defined, so use #if instead of #ifdef. --- Modules/_cursesmodule.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 27eaf183dd4409..31c2b14e6cb07b 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -3042,7 +3042,7 @@ immediately change to the new definition. This function is a no-op on most terminals; it is active only if can_change_color() returns 1. */ -#ifdef _NCURSES_EXTENDED_COLOR_FUNCS +#if _NCURSES_EXTENDED_COLOR_FUNCS #define _CURSES_INIT_COLOR_FUNC init_extended_color #else #define _CURSES_INIT_COLOR_FUNC init_color @@ -3081,7 +3081,7 @@ If the color-pair was previously initialized, the screen is refreshed and all occurrences of that color-pair are changed to the new definition. */ -#ifdef _NCURSES_EXTENDED_COLOR_FUNCS +#if _NCURSES_EXTENDED_COLOR_FUNCS #define _CURSES_INIT_PAIR_FUNC init_extended_pair #else #define _CURSES_INIT_PAIR_FUNC init_pair From 765d44d347a68172dc83e1e21b05e57cd276fdcd Mon Sep 17 00:00:00 2001 From: Jeffrey Kintscher Date: Thu, 23 May 2019 20:11:09 -0700 Subject: [PATCH 4/9] bpo-36982: add new function: curses.has_extended_color_support() to indicate whether extended colors are suported by the underlying ncurses library. --- Doc/library/curses.rst | 10 ++++++++++ Doc/whatsnew/3.8.rst | 7 +++++++ Modules/_cursesmodule.c | 1 + Modules/clinic/_cursesmodule.c.h | 17 +++++++++++++++++ 4 files changed, 35 insertions(+) diff --git a/Doc/library/curses.rst b/Doc/library/curses.rst index 2a4d9ce8a35a4e..78c234dd625f77 100644 --- a/Doc/library/curses.rst +++ b/Doc/library/curses.rst @@ -242,6 +242,16 @@ The module :mod:`curses` defines the following functions: Return ``True`` if the terminal can display colors; otherwise, return ``False``. +.. function:: has_extended_color_support() + + Return ``True`` if the module supports extended colors; otherwise, return + ``False``. Extended color support allows more than 256 color-pairs for + terminals that support more than 16 colors (e.g. xterm-256color). + + Extended color support requires ncurses version 6.1 or later. + + .. versionadded:: 3.8 + .. function:: has_ic() diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index d47993bf1129f8..92e3c58f27c4da 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -375,6 +375,13 @@ Added a new variable holding structured version information for the underlying ncurses library: :data:`~curses.ncurses_version`. (Contributed by Serhiy Storchaka in :issue:`31680`.) +The extended color functions added in ncurses 6.1 will be used transparently +by :func:`curses.color_content`, :func:`curses.init_color`, +:func:`curses.init_pair`, and :func:`curses.pair_content`. A new function, +:func:`curses.has_extended_color_support`, indicates whether extended color +support is provided by the underlying ncurses library. +(Contributed by Jeffrey Kintscher in :issue:`36982`.) + pathlib ------- diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 31c2b14e6cb07b..cadcf86b173175 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -4403,6 +4403,7 @@ static PyMethodDef PyCurses_methods[] = { _CURSES_GETSYX_METHODDEF _CURSES_GETWIN_METHODDEF _CURSES_HAS_COLORS_METHODDEF + _CURSES_HAS_EXTENDED_COLOR_SUPPORT_METHODDEF _CURSES_HAS_IC_METHODDEF _CURSES_HAS_IL_METHODDEF _CURSES_HAS_KEY_METHODDEF diff --git a/Modules/clinic/_cursesmodule.c.h b/Modules/clinic/_cursesmodule.c.h index 81f5ad1afee702..bb2a38e90b3e3f 100644 --- a/Modules/clinic/_cursesmodule.c.h +++ b/Modules/clinic/_cursesmodule.c.h @@ -2638,6 +2638,23 @@ _curses_has_colors(PyObject *module, PyObject *Py_UNUSED(ignored)) return _curses_has_colors_impl(module); } +PyDoc_STRVAR(_curses_has_extended_color_support__doc__, +"has_extended_color_support($module, /)\n" +"--\n" +"\n" +"Return True if the module supports extended colors; otherwise, return\n" +"False. Extended color support allows more than 256 color-pairs for terminals\n" +"that support more than 16 colors (e.g. xterm-256color).\n"); + +#define _CURSES_HAS_EXTENDED_COLOR_SUPPORT_METHODDEF \ + {"has_extended_color_support", (PyCFunction)_curses_has_extended_color_support, METH_NOARGS, _curses_has_extended_color_support__doc__}, + +static PyObject * +_curses_has_extended_color_support(PyObject *module, PyObject *Py_UNUSED(ignored)) +{ + return PyBool_FromLong(_NCURSES_EXTENDED_COLOR_FUNCS); +} + PyDoc_STRVAR(_curses_has_ic__doc__, "has_ic($module, /)\n" "--\n" From a6e3280e40a4312e2fc0b20730aecb1273d086a3 Mon Sep 17 00:00:00 2001 From: Jeffrey Kintscher Date: Thu, 23 May 2019 20:37:53 -0700 Subject: [PATCH 5/9] bpo-36982, bpo-36630: rename the ncurses section to curses (since that is the name of the Python module) --- Doc/whatsnew/3.8.rst | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 56b01b5689e8dc..395463b08d1ab5 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -287,6 +287,21 @@ where the DLL is stored (if a full or partial path is used to load the initial DLL) and paths added by :func:`~os.add_dll_directory`. +curses +------- + +Added a new variable holding structured version information for the +underlying ncurses library: :data:`~curses.ncurses_version`. +(Contributed by Serhiy Storchaka in :issue:`31680`.) + +The extended color functions added in ncurses 6.1 will be used transparently +by :func:`curses.color_content`, :func:`curses.init_color`, +:func:`curses.init_pair`, and :func:`curses.pair_content`. A new function, +:func:`curses.has_extended_color_support`, indicates whether extended color +support is provided by the underlying ncurses library. +(Contributed by Jeffrey Kintscher in :issue:`36982`.) + + datetime -------- @@ -384,6 +399,7 @@ numbers. (Contributed by Pablo Galindo in :issue:`35606`) Added new function :func:`math.isqrt` for computing integer square roots. (Contributed by Mark Dickinson in :issue:`36887`.) + os -- @@ -408,21 +424,6 @@ environment variable and does not use :envvar:`HOME`, which is not normally set for regular user accounts. -ncurses -------- - -Added a new variable holding structured version information for the -underlying ncurses library: :data:`~curses.ncurses_version`. -(Contributed by Serhiy Storchaka in :issue:`31680`.) - -The extended color functions added in ncurses 6.1 will be used transparently -by :func:`curses.color_content`, :func:`curses.init_color`, -:func:`curses.init_pair`, and :func:`curses.pair_content`. A new function, -:func:`curses.has_extended_color_support`, indicates whether extended color -support is provided by the underlying ncurses library. -(Contributed by Jeffrey Kintscher in :issue:`36982`.) - - pathlib ------- From ee3899639b3c6838c4c93ea9521dbc937b776890 Mon Sep 17 00:00:00 2001 From: Jeffrey Kintscher Date: Thu, 23 May 2019 21:13:58 -0700 Subject: [PATCH 6/9] bpo-36982, bpo-36630: add test for curses.has_extended_color_support() --- Lib/test/test_curses.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py index 09738c8a41c942..c4cd87f319fcb5 100644 --- a/Lib/test/test_curses.py +++ b/Lib/test/test_curses.py @@ -231,7 +231,8 @@ def test_module_funcs(self): curses.nocbreak, curses.noecho, curses.nonl, curses.noqiflush, curses.noraw, curses.reset_prog_mode, curses.termattrs, - curses.termname, curses.erasechar]: + curses.termname, curses.erasechar, + curses.has_extended_color_support]: with self.subTest(func=func.__qualname__): func() if hasattr(curses, 'filter'): From a43d22166180d08c869d219e6eb6e7e754781008 Mon Sep 17 00:00:00 2001 From: Jeffrey Kintscher Date: Fri, 24 May 2019 02:22:01 -0700 Subject: [PATCH 7/9] bpo-36982: reorganize new code to keep clinic happy --- Modules/_cursesmodule.c | 526 ++++++++++++++++++++++++++++--- Modules/clinic/_cursesmodule.c.h | 465 +-------------------------- 2 files changed, 487 insertions(+), 504 deletions(-) diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index cadcf86b173175..6489eb24782aa3 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -134,6 +134,24 @@ typedef chtype attr_t; /* No attr_t type is available */ #define STRICT_SYSV_CURSES #endif +#if defined(NCURSES_EXT_COLORS) && defined(NCURSES_EXT_FUNCS) +#define _NCURSES_EXTENDED_COLOR_FUNCS 1 +#else +#define _NCURSES_EXTENDED_COLOR_FUNCS 0 +#endif /* defined(NCURSES_EXT_COLORS) && defined(NCURSES_EXT_FUNCS) */ + +#if _NCURSES_EXTENDED_COLOR_FUNCS +#define _NCURSES_COLOR_VAL_MAX INT_MAX +#define _NCURSES_COLOR_VAL_MIN INT_MIN +#define _NCURSES_COLOR_VAL_TYPE int +#define _NCURSES_COLOR_VAL_TYPE_STR "integer" +#else +#define _NCURSES_COLOR_VAL_MAX SHRT_MAX +#define _NCURSES_COLOR_VAL_MIN SHRT_MIN +#define _NCURSES_COLOR_VAL_TYPE short +#define _NCURSES_COLOR_VAL_TYPE_STR "short integer" +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ + /*[clinic input] module _curses class _curses.window "PyCursesWindowObject *" "&PyCursesWindow_Type" @@ -2574,10 +2592,16 @@ _curses_cbreak_impl(PyObject *module, int flag) /*[clinic end generated code: output=9f9dee9664769751 input=150be619eb1f1458]*/ NoArgOrFlagNoReturnFunctionBody(cbreak, flag) -/*[clinic input] +#if _NCURSES_EXTENDED_COLOR_FUNCS +#define _COLOR_CONTENT_FUNC extended_color_content +#else +#define _COLOR_CONTENT_FUNC color_content +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ + +/* _curses.color_content - color_number: short + color_number: _NCURSES_COLOR_VAL_TYPE The number of the color (0 - COLORS). / @@ -2587,12 +2611,6 @@ A 3-tuple is returned, containing the R, G, B values for the given color, which will be between 0 (no component) and 1000 (maximum amount of component). */ -#if _NCURSES_EXTENDED_COLOR_FUNCS -#define _COLOR_CONTENT_FUNC extended_color_content -#else -#define _COLOR_CONTENT_FUNC color_content -#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ - static PyObject * _curses_color_content_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number) { @@ -2612,10 +2630,64 @@ _curses_color_content_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_numbe #undef _COLOR_CONTENT_FUNC -/*[clinic input] +PyDoc_STRVAR(_curses_color_content__doc__, +"color_content($module, color_number, /)\n" +"--\n" +"\n" +"Return the red, green, and blue (RGB) components of the specified color.\n" +"\n" +" color_number\n" +" The number of the color (0 - COLORS).\n" +"\n" +"A 3-tuple is returned, containing the R, G, B values for the given color,\n" +"which will be between 0 (no component) and 1000 (maximum amount of component)."); + +#define _CURSES_COLOR_CONTENT_METHODDEF \ + {"color_content", (PyCFunction)_curses_color_content, METH_O, _curses_color_content__doc__}, + +static PyObject * +_curses_color_content_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number); + +static PyObject * +_curses_color_content(PyObject *module, PyObject *arg) +{ + PyObject *return_value = NULL; + _NCURSES_COLOR_VAL_TYPE color_number; + + if (PyFloat_Check(arg)) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + { + long ival = PyLong_AsLong(arg); + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + else if (ival < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); + goto exit; + } + else if (ival > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); + goto exit; + } + else { + color_number = (_NCURSES_COLOR_VAL_TYPE) ival; + } + } + return_value = _curses_color_content_impl(module, color_number); + +exit: + return return_value; +} + +/* _curses.color_pair - color_number: short + color_number: _NCURSES_COLOR_VAL_TYPE The number of the color (0 - COLORS). / @@ -2634,6 +2706,60 @@ _curses_color_pair_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number) return PyLong_FromLong((long) (color_number << 8)); } +PyDoc_STRVAR(_curses_color_pair__doc__, +"color_pair($module, color_number, /)\n" +"--\n" +"\n" +"Return the attribute value for displaying text in the specified color.\n" +"\n" +" color_number\n" +" The number of the color (0 - COLORS).\n" +"\n" +"This attribute value can be combined with A_STANDOUT, A_REVERSE, and the\n" +"other A_* attributes. pair_number() is the counterpart to this function."); + +#define _CURSES_COLOR_PAIR_METHODDEF \ + {"color_pair", (PyCFunction)_curses_color_pair, METH_O, _curses_color_pair__doc__}, + +static PyObject * +_curses_color_pair_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number); + +static PyObject * +_curses_color_pair(PyObject *module, PyObject *arg) +{ + PyObject *return_value = NULL; + _NCURSES_COLOR_VAL_TYPE color_number; + + if (PyFloat_Check(arg)) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + { + long ival = PyLong_AsLong(arg); + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + else if (ival < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); + goto exit; + } + else if (ival > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR "is greater than maximum"); + goto exit; + } + else { + color_number = (_NCURSES_COLOR_VAL_TYPE) ival; + } + } + return_value = _curses_color_pair_impl(module, color_number); + +exit: + return return_value; +} + /*[clinic input] _curses.curs_set @@ -3022,16 +3148,24 @@ _curses_has_key_impl(PyObject *module, int key) } #endif -/*[clinic input] +#if _NCURSES_EXTENDED_COLOR_FUNCS +#define _CURSES_INIT_COLOR_FUNC init_extended_color +#else +#define _CURSES_INIT_COLOR_FUNC init_color +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ + +#define _CURSES_INIT_COLOR_FUNC_NAME _CURSES_FUNC_NAME_STR(_CURSES_INIT_COLOR_FUNC) + +/* _curses.init_color - color_number: short + color_number: _NCURSES_COLOR_VAL_TYPE The number of the color to be changed (0 - COLORS). - r: short + r: _NCURSES_COLOR_VAL_TYPE Red component (0 - 1000). - g: short + g: _NCURSES_COLOR_VAL_TYPE Green component (0 - 1000). - b: short + b: _NCURSES_COLOR_VAL_TYPE Blue component (0 - 1000). / @@ -3042,14 +3176,6 @@ immediately change to the new definition. This function is a no-op on most terminals; it is active only if can_change_color() returns 1. */ -#if _NCURSES_EXTENDED_COLOR_FUNCS -#define _CURSES_INIT_COLOR_FUNC init_extended_color -#else -#define _CURSES_INIT_COLOR_FUNC init_color -#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ - -#define _CURSES_INIT_COLOR_FUNC_NAME _CURSES_FUNC_NAME_STR(_CURSES_INIT_COLOR_FUNC) - static PyObject * _curses_init_color_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number, _NCURSES_COLOR_VAL_TYPE r, _NCURSES_COLOR_VAL_TYPE g, @@ -3064,7 +3190,156 @@ _curses_init_color_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number, #undef _CURSES_INIT_COLOR_FUNC #undef _CURSES_INIT_COLOR_FUNC_NAME -/*[clinic input] +PyDoc_STRVAR(_curses_init_color__doc__, +"init_color($module, color_number, r, g, b, /)\n" +"--\n" +"\n" +"Change the definition of a color.\n" +"\n" +" color_number\n" +" The number of the color to be changed (0 - COLORS).\n" +" r\n" +" Red component (0 - 1000).\n" +" g\n" +" Green component (0 - 1000).\n" +" b\n" +" Blue component (0 - 1000).\n" +"\n" +"When init_color() is used, all occurrences of that color on the screen\n" +"immediately change to the new definition. This function is a no-op on\n" +"most terminals; it is active only if can_change_color() returns 1."); + +#define _CURSES_INIT_COLOR_METHODDEF \ + {"init_color", (PyCFunction)(void(*)(void))_curses_init_color, METH_FASTCALL, _curses_init_color__doc__}, + +static PyObject * +_curses_init_color_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number, + _NCURSES_COLOR_VAL_TYPE r, _NCURSES_COLOR_VAL_TYPE g, + _NCURSES_COLOR_VAL_TYPE b); + +static PyObject * +_curses_init_color(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + _NCURSES_COLOR_VAL_TYPE color_number; + _NCURSES_COLOR_VAL_TYPE r; + _NCURSES_COLOR_VAL_TYPE g; + _NCURSES_COLOR_VAL_TYPE b; + + if (!_PyArg_CheckPositional("init_color", nargs, 4, 4)) { + goto exit; + } + if (PyFloat_Check(args[0])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + { + long ival = PyLong_AsLong(args[0]); + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + else if (ival < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is less than minimum"); + goto exit; + } + else if (ival > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is greater than maximum"); + goto exit; + } + else { + color_number = (_NCURSES_COLOR_VAL_TYPE) ival; + } + } + if (PyFloat_Check(args[1])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + { + long ival = PyLong_AsLong(args[1]); + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + else if (ival < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is less than minimum"); + goto exit; + } + else if (ival > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is greater than maximum"); + goto exit; + } + else { + r = (_NCURSES_COLOR_VAL_TYPE) ival; + } + } + if (PyFloat_Check(args[2])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + { + long ival = PyLong_AsLong(args[2]); + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + else if (ival < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is less than minimum"); + goto exit; + } + else if (ival > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is greater than maximum"); + goto exit; + } + else { + g = (_NCURSES_COLOR_VAL_TYPE) ival; + } + } + if (PyFloat_Check(args[3])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + { + long ival = PyLong_AsLong(args[3]); + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + else if (ival < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is less than minimum"); + goto exit; + } + else if (ival > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is greater than maximum"); + goto exit; + } + else { + b = (_NCURSES_COLOR_VAL_TYPE) ival; + } + } + return_value = _curses_init_color_impl(module, color_number, r, g, b); + +exit: + return return_value; +} + +#if _NCURSES_EXTENDED_COLOR_FUNCS +#define _CURSES_INIT_PAIR_FUNC init_extended_pair +#else +#define _CURSES_INIT_PAIR_FUNC init_pair +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ + +#define _CURSES_INIT_PAIR_FUNC_NAME _CURSES_FUNC_NAME_STR(_CURSES_INIT_PAIR_FUNC) + +/* _curses.init_pair pair_number: short @@ -3081,14 +3356,6 @@ If the color-pair was previously initialized, the screen is refreshed and all occurrences of that color-pair are changed to the new definition. */ -#if _NCURSES_EXTENDED_COLOR_FUNCS -#define _CURSES_INIT_PAIR_FUNC init_extended_pair -#else -#define _CURSES_INIT_PAIR_FUNC init_pair -#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ - -#define _CURSES_INIT_PAIR_FUNC_NAME _CURSES_FUNC_NAME_STR(_CURSES_INIT_PAIR_FUNC) - static PyObject * _curses_init_pair_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number, _NCURSES_COLOR_VAL_TYPE fg, _NCURSES_COLOR_VAL_TYPE bg) @@ -3102,6 +3369,118 @@ _curses_init_pair_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number, #undef _CURSES_INIT_PAIR_FUNC #undef _CURSES_INIT_PAIR_FUNC_NAME +PyDoc_STRVAR(_curses_init_pair__doc__, +"init_pair($module, pair_number, fg, bg, /)\n" +"--\n" +"\n" +"Change the definition of a color-pair.\n" +"\n" +" pair_number\n" +" The number of the color-pair to be changed (1 - (COLOR_PAIRS-1)).\n" +" fg\n" +" Foreground color number (0 - COLORS).\n" +" bg\n" +" Background color number (0 - COLORS).\n" +"\n" +"If the color-pair was previously initialized, the screen is refreshed and\n" +"all occurrences of that color-pair are changed to the new definition."); + +#define _CURSES_INIT_PAIR_METHODDEF \ + {"init_pair", (PyCFunction)(void(*)(void))_curses_init_pair, METH_FASTCALL, _curses_init_pair__doc__}, + +static PyObject * +_curses_init_pair_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number, + _NCURSES_COLOR_VAL_TYPE fg, _NCURSES_COLOR_VAL_TYPE bg); + +static PyObject * +_curses_init_pair(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + _NCURSES_COLOR_VAL_TYPE pair_number; + _NCURSES_COLOR_VAL_TYPE fg; + _NCURSES_COLOR_VAL_TYPE bg; + + if (!_PyArg_CheckPositional("init_pair", nargs, 3, 3)) { + goto exit; + } + if (PyFloat_Check(args[0])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + { + long ival = PyLong_AsLong(args[0]); + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + else if (ival < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); + goto exit; + } + else if (ival > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); + goto exit; + } + else { + pair_number = (_NCURSES_COLOR_VAL_TYPE) ival; + } + } + if (PyFloat_Check(args[1])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + { + long ival = PyLong_AsLong(args[1]); + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + else if (ival < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); + goto exit; + } + else if (ival > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); + goto exit; + } + else { + fg = (_NCURSES_COLOR_VAL_TYPE) ival; + } + } + if (PyFloat_Check(args[2])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + { + long ival = PyLong_AsLong(args[2]); + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + else if (ival < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); + goto exit; + } + else if (ival > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); + goto exit; + } + else { + bg = (_NCURSES_COLOR_VAL_TYPE) ival; + } + } + return_value = _curses_init_pair_impl(module, pair_number, fg, bg); + +exit: + return return_value; +} + static PyObject *ModDict; /*[clinic input] @@ -3629,22 +4008,22 @@ _curses_noraw_impl(PyObject *module) /*[clinic end generated code: output=39894e5524c430cc input=6ec86692096dffb5]*/ NoArgNoReturnFunctionBody(noraw) -/*[clinic input] +#if _NCURSES_EXTENDED_COLOR_FUNCS +#define _CURSES_PAIR_NUMBER_FUNC extended_pair_content +#else +#define _CURSES_PAIR_NUMBER_FUNC pair_content +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ + +/* _curses.pair_content - pair_number: short + pair_number: _NCURSES_COLOR_VAL_TYPE The number of the color pair (1 - (COLOR_PAIRS-1)). / Return a tuple (fg, bg) containing the colors for the requested color pair. */ -#if _NCURSES_EXTENDED_COLOR_FUNCS -#define _CURSES_PAIR_NUMBER_FUNC extended_pair_content -#else -#define _CURSES_PAIR_NUMBER_FUNC pair_content -#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ - static PyObject * _curses_pair_content_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number) { @@ -3662,6 +4041,57 @@ _curses_pair_content_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number) return Py_BuildValue("(ii)", f, b); } +PyDoc_STRVAR(_curses_pair_content__doc__, +"pair_content($module, pair_number, /)\n" +"--\n" +"\n" +"Return a tuple (fg, bg) containing the colors for the requested color pair.\n" +"\n" +" pair_number\n" +" The number of the color pair (1 - (COLOR_PAIRS-1))."); + +#define _CURSES_PAIR_CONTENT_METHODDEF \ + {"pair_content", (PyCFunction)_curses_pair_content, METH_O, _curses_pair_content__doc__}, + +static PyObject * +_curses_pair_content_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number); + +static PyObject * +_curses_pair_content(PyObject *module, PyObject *arg) +{ + PyObject *return_value = NULL; + _NCURSES_COLOR_VAL_TYPE pair_number; + + if (PyFloat_Check(arg)) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + { + long ival = PyLong_AsLong(arg); + if (ival == -1 && PyErr_Occurred()) { + goto exit; + } + else if (ival < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); + goto exit; + } + else if (ival > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); + goto exit; + } + else { + pair_number = (_NCURSES_COLOR_VAL_TYPE) ival; + } + } + return_value = _curses_pair_content_impl(module, pair_number); + +exit: + return return_value; +} + /*[clinic input] _curses.pair_number @@ -4377,6 +4807,22 @@ make_ncurses_version(void) #endif /* NCURSES_VERSION */ +PyDoc_STRVAR(_curses_has_extended_color_support__doc__, +"has_extended_color_support($module, /)\n" +"--\n" +"\n" +"Return True if the module supports extended colors; otherwise, return\n" +"False. Extended color support allows more than 256 color-pairs for terminals\n" +"that support more than 16 colors (e.g. xterm-256color).\n"); + +#define _CURSES_HAS_EXTENDED_COLOR_SUPPORT_METHODDEF \ + {"has_extended_color_support", (PyCFunction)_curses_has_extended_color_support, METH_NOARGS, _curses_has_extended_color_support__doc__}, + +static PyObject * +_curses_has_extended_color_support(PyObject *module, PyObject *Py_UNUSED(ignored)) +{ + return PyBool_FromLong(_NCURSES_EXTENDED_COLOR_FUNCS); +} /* List of functions defined in the module */ diff --git a/Modules/clinic/_cursesmodule.c.h b/Modules/clinic/_cursesmodule.c.h index bb2a38e90b3e3f..184fe91bd40c92 100644 --- a/Modules/clinic/_cursesmodule.c.h +++ b/Modules/clinic/_cursesmodule.c.h @@ -1,21 +1,3 @@ -#if defined(NCURSES_EXT_COLORS) && defined(NCURSES_EXT_FUNCS) -#define _NCURSES_EXTENDED_COLOR_FUNCS 1 -#else -#define _NCURSES_EXTENDED_COLOR_FUNCS 0 -#endif /* defined(NCURSES_EXT_COLORS) && defined(NCURSES_EXT_FUNCS) */ - -#if _NCURSES_EXTENDED_COLOR_FUNCS -#define _NCURSES_COLOR_VAL_MAX INT_MAX -#define _NCURSES_COLOR_VAL_MIN INT_MIN -#define _NCURSES_COLOR_VAL_TYPE int -#define _NCURSES_COLOR_VAL_TYPE_STR "integer" -#else -#define _NCURSES_COLOR_VAL_MAX SHRT_MAX -#define _NCURSES_COLOR_VAL_MIN SHRT_MIN -#define _NCURSES_COLOR_VAL_TYPE short -#define _NCURSES_COLOR_VAL_TYPE_STR "short integer" -#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ - /*[clinic input] preserve [clinic start generated code]*/ @@ -2039,118 +2021,6 @@ _curses_cbreak(PyObject *module, PyObject *const *args, Py_ssize_t nargs) return return_value; } -PyDoc_STRVAR(_curses_color_content__doc__, -"color_content($module, color_number, /)\n" -"--\n" -"\n" -"Return the red, green, and blue (RGB) components of the specified color.\n" -"\n" -" color_number\n" -" The number of the color (0 - COLORS).\n" -"\n" -"A 3-tuple is returned, containing the R, G, B values for the given color,\n" -"which will be between 0 (no component) and 1000 (maximum amount of component)."); - -#define _CURSES_COLOR_CONTENT_METHODDEF \ - {"color_content", (PyCFunction)_curses_color_content, METH_O, _curses_color_content__doc__}, - -static PyObject * -_curses_color_content_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number); - -static PyObject * -_curses_color_content(PyObject *module, PyObject *arg) -{ - PyObject *return_value = NULL; - _NCURSES_COLOR_VAL_TYPE color_number; - - if (PyFloat_Check(arg)) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; - } - { - long ival = PyLong_AsLong(arg); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); - goto exit; - } - else { - color_number = (_NCURSES_COLOR_VAL_TYPE) ival; - } - } - return_value = _curses_color_content_impl(module, color_number); - -exit: - return return_value; -} - -#ifdef _CURSES_COLOR_CONTENT_IMPL_FUNC -#undef _CURSES_COLOR_CONTENT_IMPL_FUNC -#endif /* _CURSES_COLOR_CONTENT_IMPL_FUNC */ - -PyDoc_STRVAR(_curses_color_pair__doc__, -"color_pair($module, color_number, /)\n" -"--\n" -"\n" -"Return the attribute value for displaying text in the specified color.\n" -"\n" -" color_number\n" -" The number of the color (0 - COLORS).\n" -"\n" -"This attribute value can be combined with A_STANDOUT, A_REVERSE, and the\n" -"other A_* attributes. pair_number() is the counterpart to this function."); - -#define _CURSES_COLOR_PAIR_METHODDEF \ - {"color_pair", (PyCFunction)_curses_color_pair, METH_O, _curses_color_pair__doc__}, - -static PyObject * -_curses_color_pair_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number); - -static PyObject * -_curses_color_pair(PyObject *module, PyObject *arg) -{ - PyObject *return_value = NULL; - _NCURSES_COLOR_VAL_TYPE color_number; - - if (PyFloat_Check(arg)) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; - } - { - long ival = PyLong_AsLong(arg); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR "is greater than maximum"); - goto exit; - } - else { - color_number = (_NCURSES_COLOR_VAL_TYPE) ival; - } - } - return_value = _curses_color_pair_impl(module, color_number); - -exit: - return return_value; -} - PyDoc_STRVAR(_curses_curs_set__doc__, "curs_set($module, visibility, /)\n" "--\n" @@ -2638,23 +2508,6 @@ _curses_has_colors(PyObject *module, PyObject *Py_UNUSED(ignored)) return _curses_has_colors_impl(module); } -PyDoc_STRVAR(_curses_has_extended_color_support__doc__, -"has_extended_color_support($module, /)\n" -"--\n" -"\n" -"Return True if the module supports extended colors; otherwise, return\n" -"False. Extended color support allows more than 256 color-pairs for terminals\n" -"that support more than 16 colors (e.g. xterm-256color).\n"); - -#define _CURSES_HAS_EXTENDED_COLOR_SUPPORT_METHODDEF \ - {"has_extended_color_support", (PyCFunction)_curses_has_extended_color_support, METH_NOARGS, _curses_has_extended_color_support__doc__}, - -static PyObject * -_curses_has_extended_color_support(PyObject *module, PyObject *Py_UNUSED(ignored)) -{ - return PyBool_FromLong(_NCURSES_EXTENDED_COLOR_FUNCS); -} - PyDoc_STRVAR(_curses_has_ic__doc__, "has_ic($module, /)\n" "--\n" @@ -2731,267 +2584,6 @@ _curses_has_key(PyObject *module, PyObject *arg) #endif /* defined(HAVE_CURSES_HAS_KEY) */ -PyDoc_STRVAR(_curses_init_color__doc__, -"init_color($module, color_number, r, g, b, /)\n" -"--\n" -"\n" -"Change the definition of a color.\n" -"\n" -" color_number\n" -" The number of the color to be changed (0 - COLORS).\n" -" r\n" -" Red component (0 - 1000).\n" -" g\n" -" Green component (0 - 1000).\n" -" b\n" -" Blue component (0 - 1000).\n" -"\n" -"When init_color() is used, all occurrences of that color on the screen\n" -"immediately change to the new definition. This function is a no-op on\n" -"most terminals; it is active only if can_change_color() returns 1."); - -#define _CURSES_INIT_COLOR_METHODDEF \ - {"init_color", (PyCFunction)(void(*)(void))_curses_init_color, METH_FASTCALL, _curses_init_color__doc__}, - -static PyObject * -_curses_init_color_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number, - _NCURSES_COLOR_VAL_TYPE r, _NCURSES_COLOR_VAL_TYPE g, - _NCURSES_COLOR_VAL_TYPE b); - -static PyObject * -_curses_init_color(PyObject *module, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - _NCURSES_COLOR_VAL_TYPE color_number; - _NCURSES_COLOR_VAL_TYPE r; - _NCURSES_COLOR_VAL_TYPE g; - _NCURSES_COLOR_VAL_TYPE b; - - if (!_PyArg_CheckPositional("init_color", nargs, 4, 4)) { - goto exit; - } - if (PyFloat_Check(args[0])) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; - } - { - long ival = PyLong_AsLong(args[0]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - color_number = (_NCURSES_COLOR_VAL_TYPE) ival; - } - } - if (PyFloat_Check(args[1])) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; - } - { - long ival = PyLong_AsLong(args[1]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - r = (_NCURSES_COLOR_VAL_TYPE) ival; - } - } - if (PyFloat_Check(args[2])) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; - } - { - long ival = PyLong_AsLong(args[2]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - g = (_NCURSES_COLOR_VAL_TYPE) ival; - } - } - if (PyFloat_Check(args[3])) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; - } - { - long ival = PyLong_AsLong(args[3]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - b = (_NCURSES_COLOR_VAL_TYPE) ival; - } - } - return_value = _curses_init_color_impl(module, color_number, r, g, b); - -exit: - return return_value; -} - -#ifdef _CURSES_INIT_COLOR_IMPL_FUNC -#undef _CURSES_INIT_COLOR_IMPL_FUNC -#endif /* _CURSES_INIT_COLOR_IMPL_FUNC */ - -PyDoc_STRVAR(_curses_init_pair__doc__, -"init_pair($module, pair_number, fg, bg, /)\n" -"--\n" -"\n" -"Change the definition of a color-pair.\n" -"\n" -" pair_number\n" -" The number of the color-pair to be changed (1 - (COLOR_PAIRS-1)).\n" -" fg\n" -" Foreground color number (0 - COLORS).\n" -" bg\n" -" Background color number (0 - COLORS).\n" -"\n" -"If the color-pair was previously initialized, the screen is refreshed and\n" -"all occurrences of that color-pair are changed to the new definition."); - -#define _CURSES_INIT_PAIR_METHODDEF \ - {"init_pair", (PyCFunction)(void(*)(void))_curses_init_pair, METH_FASTCALL, _curses_init_pair__doc__}, - -static PyObject * -_curses_init_pair_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number, - _NCURSES_COLOR_VAL_TYPE fg, _NCURSES_COLOR_VAL_TYPE bg); - -static PyObject * -_curses_init_pair(PyObject *module, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - _NCURSES_COLOR_VAL_TYPE pair_number; - _NCURSES_COLOR_VAL_TYPE fg; - _NCURSES_COLOR_VAL_TYPE bg; - - if (!_PyArg_CheckPositional("init_pair", nargs, 3, 3)) { - goto exit; - } - if (PyFloat_Check(args[0])) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; - } - { - long ival = PyLong_AsLong(args[0]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); - goto exit; - } - else { - pair_number = (_NCURSES_COLOR_VAL_TYPE) ival; - } - } - if (PyFloat_Check(args[1])) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; - } - { - long ival = PyLong_AsLong(args[1]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); - goto exit; - } - else { - fg = (_NCURSES_COLOR_VAL_TYPE) ival; - } - } - if (PyFloat_Check(args[2])) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; - } - { - long ival = PyLong_AsLong(args[2]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); - goto exit; - } - else { - bg = (_NCURSES_COLOR_VAL_TYPE) ival; - } - } - return_value = _curses_init_pair_impl(module, pair_number, fg, bg); - -exit: - return return_value; -} - -#ifdef _CURSES_INIT_PAIR_IMPL_FUNC -#undef _CURSES_INIT_PAIR_IMPL_FUNC -#endif /* _CURSES_INIT_PAIR_IMPL_FUNC */ - PyDoc_STRVAR(_curses_initscr__doc__, "initscr($module, /)\n" "--\n" @@ -3676,61 +3268,6 @@ _curses_noraw(PyObject *module, PyObject *Py_UNUSED(ignored)) return _curses_noraw_impl(module); } -PyDoc_STRVAR(_curses_pair_content__doc__, -"pair_content($module, pair_number, /)\n" -"--\n" -"\n" -"Return a tuple (fg, bg) containing the colors for the requested color pair.\n" -"\n" -" pair_number\n" -" The number of the color pair (1 - (COLOR_PAIRS-1))."); - -#define _CURSES_PAIR_CONTENT_METHODDEF \ - {"pair_content", (PyCFunction)_curses_pair_content, METH_O, _curses_pair_content__doc__}, - -static PyObject * -_curses_pair_content_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number); - -static PyObject * -_curses_pair_content(PyObject *module, PyObject *arg) -{ - PyObject *return_value = NULL; - _NCURSES_COLOR_VAL_TYPE pair_number; - - if (PyFloat_Check(arg)) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; - } - { - long ival = PyLong_AsLong(arg); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); - goto exit; - } - else { - pair_number = (_NCURSES_COLOR_VAL_TYPE) ival; - } - } - return_value = _curses_pair_content_impl(module, pair_number); - -exit: - return return_value; -} - -#ifdef _CURSES_PAIR_CONTENT_IMPL_FUNC -#undef _CURSES_PAIR_CONTENT_IMPL_FUNC -#endif /* _CURSES_PAIR_CONTENT_IMPL_FUNC */ - PyDoc_STRVAR(_curses_pair_number__doc__, "pair_number($module, attr, /)\n" "--\n" @@ -4621,4 +4158,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=1350eeb0c1e06af6 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=d7307c09c4de7801 input=a9049054013a1b77]*/ From 4568f43f127bb262325fcad85b192bba0b145d6e Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" Date: Sat, 25 May 2019 05:27:39 +0000 Subject: [PATCH 8/9] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Core and Builtins/2019-05-25-05-27-39.bpo-36982.0UHgfB.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2019-05-25-05-27-39.bpo-36982.0UHgfB.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-05-25-05-27-39.bpo-36982.0UHgfB.rst b/Misc/NEWS.d/next/Core and Builtins/2019-05-25-05-27-39.bpo-36982.0UHgfB.rst new file mode 100644 index 00000000000000..f105f1857d487f --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-05-25-05-27-39.bpo-36982.0UHgfB.rst @@ -0,0 +1 @@ +Use ncurses extended color functions when available to support terminals with 256 colors, and add the new function :func:`curses.has_extended_color_support` to indicate whether extended color support is provided by the underlying ncurses library. \ No newline at end of file From 039ffa63a7a864f9456711791d11aab1886ddd84 Mon Sep 17 00:00:00 2001 From: Jeffrey Kintscher Date: Wed, 29 May 2019 13:28:53 -0700 Subject: [PATCH 9/9] bpo-36982: refactor color-pair functions to use clinic --- Modules/_cursesmodule.c | 518 ++++++++----------------------- Modules/clinic/_cursesmodule.c.h | 247 ++++++++++++++- 2 files changed, 375 insertions(+), 390 deletions(-) diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 6489eb24782aa3..e32e48bc220b46 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -2598,10 +2598,10 @@ NoArgOrFlagNoReturnFunctionBody(cbreak, flag) #define _COLOR_CONTENT_FUNC color_content #endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ -/* +/*[clinic input] _curses.color_content - color_number: _NCURSES_COLOR_VAL_TYPE + color_number: int The number of the color (0 - COLORS). / @@ -2609,85 +2609,44 @@ Return the red, green, and blue (RGB) components of the specified color. A 3-tuple is returned, containing the R, G, B values for the given color, which will be between 0 (no component) and 1000 (maximum amount of component). -*/ +[clinic start generated code]*/ static PyObject * -_curses_color_content_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number) +_curses_color_content_impl(PyObject *module, int color_number) +/*[clinic end generated code: output=17b466df7054e0de input=badb7d68ffbb0e93]*/ { + PyObject *return_value = NULL; _NCURSES_COLOR_VAL_TYPE r,g,b; PyCursesInitialised; PyCursesInitialisedColor; + if (color_number < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); + return NULL; + } + else if (color_number > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); + return NULL; + } + if (_COLOR_CONTENT_FUNC(color_number, &r, &g, &b) != ERR) - return Py_BuildValue("(iii)", r, g, b); - else { + return_value = Py_BuildValue("(iii)", r, g, b); + else PyErr_SetString(PyCursesError, "Argument 1 was out of range. Check value of COLORS."); - return NULL; - } -} - -#undef _COLOR_CONTENT_FUNC - -PyDoc_STRVAR(_curses_color_content__doc__, -"color_content($module, color_number, /)\n" -"--\n" -"\n" -"Return the red, green, and blue (RGB) components of the specified color.\n" -"\n" -" color_number\n" -" The number of the color (0 - COLORS).\n" -"\n" -"A 3-tuple is returned, containing the R, G, B values for the given color,\n" -"which will be between 0 (no component) and 1000 (maximum amount of component)."); - -#define _CURSES_COLOR_CONTENT_METHODDEF \ - {"color_content", (PyCFunction)_curses_color_content, METH_O, _curses_color_content__doc__}, - -static PyObject * -_curses_color_content_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number); - -static PyObject * -_curses_color_content(PyObject *module, PyObject *arg) -{ - PyObject *return_value = NULL; - _NCURSES_COLOR_VAL_TYPE color_number; - - if (PyFloat_Check(arg)) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; - } - { - long ival = PyLong_AsLong(arg); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); - goto exit; - } - else { - color_number = (_NCURSES_COLOR_VAL_TYPE) ival; - } - } - return_value = _curses_color_content_impl(module, color_number); -exit: return return_value; } -/* +#undef _COLOR_CONTENT_FUNC + +/*[clinic input] _curses.color_pair - color_number: _NCURSES_COLOR_VAL_TYPE + color_number: int The number of the color (0 - COLORS). / @@ -2695,69 +2654,27 @@ Return the attribute value for displaying text in the specified color. This attribute value can be combined with A_STANDOUT, A_REVERSE, and the other A_* attributes. pair_number() is the counterpart to this function. -*/ +[clinic start generated code]*/ static PyObject * -_curses_color_pair_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number) +_curses_color_pair_impl(PyObject *module, int color_number) +/*[clinic end generated code: output=3fd752e8e24c93fb input=d4ed7238735f1647]*/ { PyCursesInitialised; PyCursesInitialisedColor; - return PyLong_FromLong((long) (color_number << 8)); -} - -PyDoc_STRVAR(_curses_color_pair__doc__, -"color_pair($module, color_number, /)\n" -"--\n" -"\n" -"Return the attribute value for displaying text in the specified color.\n" -"\n" -" color_number\n" -" The number of the color (0 - COLORS).\n" -"\n" -"This attribute value can be combined with A_STANDOUT, A_REVERSE, and the\n" -"other A_* attributes. pair_number() is the counterpart to this function."); - -#define _CURSES_COLOR_PAIR_METHODDEF \ - {"color_pair", (PyCFunction)_curses_color_pair, METH_O, _curses_color_pair__doc__}, - -static PyObject * -_curses_color_pair_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number); - -static PyObject * -_curses_color_pair(PyObject *module, PyObject *arg) -{ - PyObject *return_value = NULL; - _NCURSES_COLOR_VAL_TYPE color_number; - - if (PyFloat_Check(arg)) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; + if (color_number < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); + return NULL; } - { - long ival = PyLong_AsLong(arg); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR "is greater than maximum"); - goto exit; - } - else { - color_number = (_NCURSES_COLOR_VAL_TYPE) ival; - } + else if (color_number > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR "is greater than maximum"); + return NULL; } - return_value = _curses_color_pair_impl(module, color_number); -exit: - return return_value; + return PyLong_FromLong((long) (color_number << 8)); } /*[clinic input] @@ -3156,16 +3073,16 @@ _curses_has_key_impl(PyObject *module, int key) #define _CURSES_INIT_COLOR_FUNC_NAME _CURSES_FUNC_NAME_STR(_CURSES_INIT_COLOR_FUNC) -/* +/*[clinic input] _curses.init_color - color_number: _NCURSES_COLOR_VAL_TYPE + color_number: int The number of the color to be changed (0 - COLORS). - r: _NCURSES_COLOR_VAL_TYPE + r: int Red component (0 - 1000). - g: _NCURSES_COLOR_VAL_TYPE + g: int Green component (0 - 1000). - b: _NCURSES_COLOR_VAL_TYPE + b: int Blue component (0 - 1000). / @@ -3174,163 +3091,66 @@ Change the definition of a color. When init_color() is used, all occurrences of that color on the screen immediately change to the new definition. This function is a no-op on most terminals; it is active only if can_change_color() returns 1. -*/ +[clinic start generated code]*/ static PyObject * -_curses_init_color_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number, - _NCURSES_COLOR_VAL_TYPE r, _NCURSES_COLOR_VAL_TYPE g, - _NCURSES_COLOR_VAL_TYPE b) +_curses_init_color_impl(PyObject *module, int color_number, int r, int g, + int b) +/*[clinic end generated code: output=3824cf242d9174fc input=3ba3d1700bff7a8a]*/ { PyCursesInitialised; PyCursesInitialisedColor; - return PyCursesCheckERR(_CURSES_INIT_COLOR_FUNC(color_number, r, g, b), _CURSES_INIT_COLOR_FUNC_NAME); -} - -#undef _CURSES_INIT_COLOR_FUNC -#undef _CURSES_INIT_COLOR_FUNC_NAME - -PyDoc_STRVAR(_curses_init_color__doc__, -"init_color($module, color_number, r, g, b, /)\n" -"--\n" -"\n" -"Change the definition of a color.\n" -"\n" -" color_number\n" -" The number of the color to be changed (0 - COLORS).\n" -" r\n" -" Red component (0 - 1000).\n" -" g\n" -" Green component (0 - 1000).\n" -" b\n" -" Blue component (0 - 1000).\n" -"\n" -"When init_color() is used, all occurrences of that color on the screen\n" -"immediately change to the new definition. This function is a no-op on\n" -"most terminals; it is active only if can_change_color() returns 1."); - -#define _CURSES_INIT_COLOR_METHODDEF \ - {"init_color", (PyCFunction)(void(*)(void))_curses_init_color, METH_FASTCALL, _curses_init_color__doc__}, - -static PyObject * -_curses_init_color_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE color_number, - _NCURSES_COLOR_VAL_TYPE r, _NCURSES_COLOR_VAL_TYPE g, - _NCURSES_COLOR_VAL_TYPE b); - -static PyObject * -_curses_init_color(PyObject *module, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - _NCURSES_COLOR_VAL_TYPE color_number; - _NCURSES_COLOR_VAL_TYPE r; - _NCURSES_COLOR_VAL_TYPE g; - _NCURSES_COLOR_VAL_TYPE b; - - if (!_PyArg_CheckPositional("init_color", nargs, 4, 4)) { - goto exit; - } - if (PyFloat_Check(args[0])) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; + if (color_number < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is less than minimum"); + return NULL; } - { - long ival = PyLong_AsLong(args[0]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - color_number = (_NCURSES_COLOR_VAL_TYPE) ival; - } + else if (color_number > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is greater than maximum"); + return NULL; } - if (PyFloat_Check(args[1])) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; + + if (r < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is less than minimum"); + return NULL; } - { - long ival = PyLong_AsLong(args[1]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - r = (_NCURSES_COLOR_VAL_TYPE) ival; - } + else if (r > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is greater than maximum"); + return NULL; } - if (PyFloat_Check(args[2])) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; + + if (g < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is less than minimum"); + return NULL; } - { - long ival = PyLong_AsLong(args[2]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - g = (_NCURSES_COLOR_VAL_TYPE) ival; - } + else if (g > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is greater than maximum"); + return NULL; } - if (PyFloat_Check(args[3])) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; + + if (b < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is less than minimum"); + return NULL; } - { - long ival = PyLong_AsLong(args[3]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - b = (_NCURSES_COLOR_VAL_TYPE) ival; - } + else if (b > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is greater than maximum"); + return NULL; } - return_value = _curses_init_color_impl(module, color_number, r, g, b); -exit: - return return_value; + return PyCursesCheckERR(_CURSES_INIT_COLOR_FUNC(color_number, r, g, b), _CURSES_INIT_COLOR_FUNC_NAME); } +#undef _CURSES_INIT_COLOR_FUNC +#undef _CURSES_INIT_COLOR_FUNC_NAME + #if _NCURSES_EXTENDED_COLOR_FUNCS #define _CURSES_INIT_PAIR_FUNC init_extended_pair #else @@ -3339,14 +3159,14 @@ _curses_init_color(PyObject *module, PyObject *const *args, Py_ssize_t nargs) #define _CURSES_INIT_PAIR_FUNC_NAME _CURSES_FUNC_NAME_STR(_CURSES_INIT_PAIR_FUNC) -/* +/*[clinic input] _curses.init_pair - pair_number: short + pair_number: int The number of the color-pair to be changed (1 - (COLOR_PAIRS-1)). - fg: short + fg: int Foreground color number (0 - COLORS). - bg: short + bg: int Background color number (0 - COLORS). / @@ -3354,133 +3174,54 @@ Change the definition of a color-pair. If the color-pair was previously initialized, the screen is refreshed and all occurrences of that color-pair are changed to the new definition. -*/ +[clinic start generated code]*/ static PyObject * -_curses_init_pair_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number, - _NCURSES_COLOR_VAL_TYPE fg, _NCURSES_COLOR_VAL_TYPE bg) +_curses_init_pair_impl(PyObject *module, int pair_number, int fg, int bg) +/*[clinic end generated code: output=a0bba03d2bbc3ee6 input=defd89de917781dc]*/ { PyCursesInitialised; PyCursesInitialisedColor; - return PyCursesCheckERR(_CURSES_INIT_PAIR_FUNC(pair_number, fg, bg), _CURSES_INIT_PAIR_FUNC_NAME); -} - -#undef _CURSES_INIT_PAIR_FUNC -#undef _CURSES_INIT_PAIR_FUNC_NAME - -PyDoc_STRVAR(_curses_init_pair__doc__, -"init_pair($module, pair_number, fg, bg, /)\n" -"--\n" -"\n" -"Change the definition of a color-pair.\n" -"\n" -" pair_number\n" -" The number of the color-pair to be changed (1 - (COLOR_PAIRS-1)).\n" -" fg\n" -" Foreground color number (0 - COLORS).\n" -" bg\n" -" Background color number (0 - COLORS).\n" -"\n" -"If the color-pair was previously initialized, the screen is refreshed and\n" -"all occurrences of that color-pair are changed to the new definition."); - -#define _CURSES_INIT_PAIR_METHODDEF \ - {"init_pair", (PyCFunction)(void(*)(void))_curses_init_pair, METH_FASTCALL, _curses_init_pair__doc__}, - -static PyObject * -_curses_init_pair_impl(PyObject *module, _NCURSES_COLOR_VAL_TYPE pair_number, - _NCURSES_COLOR_VAL_TYPE fg, _NCURSES_COLOR_VAL_TYPE bg); - -static PyObject * -_curses_init_pair(PyObject *module, PyObject *const *args, Py_ssize_t nargs) -{ - PyObject *return_value = NULL; - _NCURSES_COLOR_VAL_TYPE pair_number; - _NCURSES_COLOR_VAL_TYPE fg; - _NCURSES_COLOR_VAL_TYPE bg; - - if (!_PyArg_CheckPositional("init_pair", nargs, 3, 3)) { - goto exit; - } - if (PyFloat_Check(args[0])) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; + if (pair_number < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); + return NULL; } - { - long ival = PyLong_AsLong(args[0]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); - goto exit; - } - else { - pair_number = (_NCURSES_COLOR_VAL_TYPE) ival; - } + else if (pair_number > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); + return NULL; } - if (PyFloat_Check(args[1])) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; + + if (fg < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); + return NULL; } - { - long ival = PyLong_AsLong(args[1]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); - goto exit; - } - else { - fg = (_NCURSES_COLOR_VAL_TYPE) ival; - } + else if (fg > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); + return NULL; } - if (PyFloat_Check(args[2])) { - PyErr_SetString(PyExc_TypeError, - "integer argument expected, got float" ); - goto exit; + + if (bg < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); + return NULL; } - { - long ival = PyLong_AsLong(args[2]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < _NCURSES_COLOR_VAL_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); - goto exit; - } - else if (ival > _NCURSES_COLOR_VAL_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); - goto exit; - } - else { - bg = (_NCURSES_COLOR_VAL_TYPE) ival; - } + else if (bg > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); + return NULL; } - return_value = _curses_init_pair_impl(module, pair_number, fg, bg); -exit: - return return_value; + return PyCursesCheckERR(_CURSES_INIT_PAIR_FUNC(pair_number, fg, bg), _CURSES_INIT_PAIR_FUNC_NAME); } +#undef _CURSES_INIT_PAIR_FUNC +#undef _CURSES_INIT_PAIR_FUNC_NAME + static PyObject *ModDict; /*[clinic input] @@ -4807,19 +4548,18 @@ make_ncurses_version(void) #endif /* NCURSES_VERSION */ -PyDoc_STRVAR(_curses_has_extended_color_support__doc__, -"has_extended_color_support($module, /)\n" -"--\n" -"\n" -"Return True if the module supports extended colors; otherwise, return\n" -"False. Extended color support allows more than 256 color-pairs for terminals\n" -"that support more than 16 colors (e.g. xterm-256color).\n"); +/*[clinic input] +_curses.has_extended_color_support -#define _CURSES_HAS_EXTENDED_COLOR_SUPPORT_METHODDEF \ - {"has_extended_color_support", (PyCFunction)_curses_has_extended_color_support, METH_NOARGS, _curses_has_extended_color_support__doc__}, +Return True if the module supports extended colors; otherwise, return False. + +Extended color support allows more than 256 color-pairs for terminals +that support more than 16 colors (e.g. xterm-256color). +[clinic start generated code]*/ static PyObject * -_curses_has_extended_color_support(PyObject *module, PyObject *Py_UNUSED(ignored)) +_curses_has_extended_color_support_impl(PyObject *module) +/*[clinic end generated code: output=68f1be2b57d92e22 input=4b905f046e35ee9f]*/ { return PyBool_FromLong(_NCURSES_EXTENDED_COLOR_FUNCS); } diff --git a/Modules/clinic/_cursesmodule.c.h b/Modules/clinic/_cursesmodule.c.h index 184fe91bd40c92..1772aa3216e8de 100644 --- a/Modules/clinic/_cursesmodule.c.h +++ b/Modules/clinic/_cursesmodule.c.h @@ -2021,6 +2021,84 @@ _curses_cbreak(PyObject *module, PyObject *const *args, Py_ssize_t nargs) return return_value; } +PyDoc_STRVAR(_curses_color_content__doc__, +"color_content($module, color_number, /)\n" +"--\n" +"\n" +"Return the red, green, and blue (RGB) components of the specified color.\n" +"\n" +" color_number\n" +" The number of the color (0 - COLORS).\n" +"\n" +"A 3-tuple is returned, containing the R, G, B values for the given color,\n" +"which will be between 0 (no component) and 1000 (maximum amount of component)."); + +#define _CURSES_COLOR_CONTENT_METHODDEF \ + {"color_content", (PyCFunction)_curses_color_content, METH_O, _curses_color_content__doc__}, + +static PyObject * +_curses_color_content_impl(PyObject *module, int color_number); + +static PyObject * +_curses_color_content(PyObject *module, PyObject *arg) +{ + PyObject *return_value = NULL; + int color_number; + + if (PyFloat_Check(arg)) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + color_number = _PyLong_AsInt(arg); + if (color_number == -1 && PyErr_Occurred()) { + goto exit; + } + return_value = _curses_color_content_impl(module, color_number); + +exit: + return return_value; +} + +PyDoc_STRVAR(_curses_color_pair__doc__, +"color_pair($module, color_number, /)\n" +"--\n" +"\n" +"Return the attribute value for displaying text in the specified color.\n" +"\n" +" color_number\n" +" The number of the color (0 - COLORS).\n" +"\n" +"This attribute value can be combined with A_STANDOUT, A_REVERSE, and the\n" +"other A_* attributes. pair_number() is the counterpart to this function."); + +#define _CURSES_COLOR_PAIR_METHODDEF \ + {"color_pair", (PyCFunction)_curses_color_pair, METH_O, _curses_color_pair__doc__}, + +static PyObject * +_curses_color_pair_impl(PyObject *module, int color_number); + +static PyObject * +_curses_color_pair(PyObject *module, PyObject *arg) +{ + PyObject *return_value = NULL; + int color_number; + + if (PyFloat_Check(arg)) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + color_number = _PyLong_AsInt(arg); + if (color_number == -1 && PyErr_Occurred()) { + goto exit; + } + return_value = _curses_color_pair_impl(module, color_number); + +exit: + return return_value; +} + PyDoc_STRVAR(_curses_curs_set__doc__, "curs_set($module, visibility, /)\n" "--\n" @@ -2584,6 +2662,152 @@ _curses_has_key(PyObject *module, PyObject *arg) #endif /* defined(HAVE_CURSES_HAS_KEY) */ +PyDoc_STRVAR(_curses_init_color__doc__, +"init_color($module, color_number, r, g, b, /)\n" +"--\n" +"\n" +"Change the definition of a color.\n" +"\n" +" color_number\n" +" The number of the color to be changed (0 - COLORS).\n" +" r\n" +" Red component (0 - 1000).\n" +" g\n" +" Green component (0 - 1000).\n" +" b\n" +" Blue component (0 - 1000).\n" +"\n" +"When init_color() is used, all occurrences of that color on the screen\n" +"immediately change to the new definition. This function is a no-op on\n" +"most terminals; it is active only if can_change_color() returns 1."); + +#define _CURSES_INIT_COLOR_METHODDEF \ + {"init_color", (PyCFunction)(void(*)(void))_curses_init_color, METH_FASTCALL, _curses_init_color__doc__}, + +static PyObject * +_curses_init_color_impl(PyObject *module, int color_number, int r, int g, + int b); + +static PyObject * +_curses_init_color(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + int color_number; + int r; + int g; + int b; + + if (!_PyArg_CheckPositional("init_color", nargs, 4, 4)) { + goto exit; + } + if (PyFloat_Check(args[0])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + color_number = _PyLong_AsInt(args[0]); + if (color_number == -1 && PyErr_Occurred()) { + goto exit; + } + if (PyFloat_Check(args[1])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + r = _PyLong_AsInt(args[1]); + if (r == -1 && PyErr_Occurred()) { + goto exit; + } + if (PyFloat_Check(args[2])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + g = _PyLong_AsInt(args[2]); + if (g == -1 && PyErr_Occurred()) { + goto exit; + } + if (PyFloat_Check(args[3])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + b = _PyLong_AsInt(args[3]); + if (b == -1 && PyErr_Occurred()) { + goto exit; + } + return_value = _curses_init_color_impl(module, color_number, r, g, b); + +exit: + return return_value; +} + +PyDoc_STRVAR(_curses_init_pair__doc__, +"init_pair($module, pair_number, fg, bg, /)\n" +"--\n" +"\n" +"Change the definition of a color-pair.\n" +"\n" +" pair_number\n" +" The number of the color-pair to be changed (1 - (COLOR_PAIRS-1)).\n" +" fg\n" +" Foreground color number (0 - COLORS).\n" +" bg\n" +" Background color number (0 - COLORS).\n" +"\n" +"If the color-pair was previously initialized, the screen is refreshed and\n" +"all occurrences of that color-pair are changed to the new definition."); + +#define _CURSES_INIT_PAIR_METHODDEF \ + {"init_pair", (PyCFunction)(void(*)(void))_curses_init_pair, METH_FASTCALL, _curses_init_pair__doc__}, + +static PyObject * +_curses_init_pair_impl(PyObject *module, int pair_number, int fg, int bg); + +static PyObject * +_curses_init_pair(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + int pair_number; + int fg; + int bg; + + if (!_PyArg_CheckPositional("init_pair", nargs, 3, 3)) { + goto exit; + } + if (PyFloat_Check(args[0])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + pair_number = _PyLong_AsInt(args[0]); + if (pair_number == -1 && PyErr_Occurred()) { + goto exit; + } + if (PyFloat_Check(args[1])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + fg = _PyLong_AsInt(args[1]); + if (fg == -1 && PyErr_Occurred()) { + goto exit; + } + if (PyFloat_Check(args[2])) { + PyErr_SetString(PyExc_TypeError, + "integer argument expected, got float" ); + goto exit; + } + bg = _PyLong_AsInt(args[2]); + if (bg == -1 && PyErr_Occurred()) { + goto exit; + } + return_value = _curses_init_pair_impl(module, pair_number, fg, bg); + +exit: + return return_value; +} + PyDoc_STRVAR(_curses_initscr__doc__, "initscr($module, /)\n" "--\n" @@ -4083,6 +4307,27 @@ _curses_use_default_colors(PyObject *module, PyObject *Py_UNUSED(ignored)) #endif /* !defined(STRICT_SYSV_CURSES) */ +PyDoc_STRVAR(_curses_has_extended_color_support__doc__, +"has_extended_color_support($module, /)\n" +"--\n" +"\n" +"Return True if the module supports extended colors; otherwise, return False.\n" +"\n" +"Extended color support allows more than 256 color-pairs for terminals\n" +"that support more than 16 colors (e.g. xterm-256color)."); + +#define _CURSES_HAS_EXTENDED_COLOR_SUPPORT_METHODDEF \ + {"has_extended_color_support", (PyCFunction)_curses_has_extended_color_support, METH_NOARGS, _curses_has_extended_color_support__doc__}, + +static PyObject * +_curses_has_extended_color_support_impl(PyObject *module); + +static PyObject * +_curses_has_extended_color_support(PyObject *module, PyObject *Py_UNUSED(ignored)) +{ + return _curses_has_extended_color_support_impl(module); +} + #ifndef _CURSES_WINDOW_ENCLOSE_METHODDEF #define _CURSES_WINDOW_ENCLOSE_METHODDEF #endif /* !defined(_CURSES_WINDOW_ENCLOSE_METHODDEF) */ @@ -4158,4 +4403,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=d7307c09c4de7801 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=93d2080b18581c1c input=a9049054013a1b77]*/