diff --git a/Doc/library/curses.rst b/Doc/library/curses.rst index 7d1e7538a292b3..be573fb8125c38 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 f704b47098e647..af011d939825aa 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -346,6 +346,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`.) + + functools --------- @@ -503,14 +518,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`.) - - pathlib ------- 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'): 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 diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 9a1d2efd256ea5..e32e48bc220b46 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" @@ -396,6 +414,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 ******************************************************************************/ @@ -2570,10 +2592,16 @@ _curses_cbreak_impl(PyObject *module, int flag) /*[clinic end generated code: output=9f9dee9664769751 input=150be619eb1f1458]*/ NoArgOrFlagNoReturnFunctionBody(cbreak, flag) +#if _NCURSES_EXTENDED_COLOR_FUNCS +#define _COLOR_CONTENT_FUNC extended_color_content +#else +#define _COLOR_CONTENT_FUNC color_content +#endif /* _NCURSES_EXTENDED_COLOR_FUNCS */ + /*[clinic input] _curses.color_content - color_number: short + color_number: int The number of the color (0 - COLORS). / @@ -2584,27 +2612,41 @@ 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, short color_number) -/*[clinic end generated code: output=cb15cf3120d4bfc1 input=5555abb1c11e11b7]*/ +_curses_color_content_impl(PyObject *module, int color_number) +/*[clinic end generated code: output=17b466df7054e0de input=badb7d68ffbb0e93]*/ { - short r,g,b; + PyObject *return_value = NULL; + _NCURSES_COLOR_VAL_TYPE r,g,b; PyCursesInitialised; PyCursesInitialisedColor; - if (color_content(color_number, &r, &g, &b) != ERR) - return Py_BuildValue("(iii)", r, g, b); - else { + 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_value = Py_BuildValue("(iii)", r, g, b); + else PyErr_SetString(PyCursesError, "Argument 1 was out of range. Check value of COLORS."); - return NULL; - } + + return return_value; } +#undef _COLOR_CONTENT_FUNC + /*[clinic input] _curses.color_pair - color_number: short + color_number: int The number of the color (0 - COLORS). / @@ -2615,13 +2657,24 @@ 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, int color_number) +/*[clinic end generated code: output=3fd752e8e24c93fb input=d4ed7238735f1647]*/ { PyCursesInitialised; PyCursesInitialisedColor; - return PyLong_FromLong((long) (color_number << 8)); + 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; + } + + return PyLong_FromLong((long) (color_number << 8)); } /*[clinic input] @@ -3012,16 +3065,24 @@ _curses_has_key_impl(PyObject *module, int key) } #endif +#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) + /*[clinic input] _curses.init_color - color_number: short + color_number: int The number of the color to be changed (0 - COLORS). - r: short + r: int Red component (0 - 1000). - g: short + g: int Green component (0 - 1000). - b: short + b: int Blue component (0 - 1000). / @@ -3033,24 +3094,79 @@ most terminals; it is active only if can_change_color() returns 1. [clinic start generated code]*/ 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]*/ +_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(init_color(color_number, r, g, b), "init_color"); + if (color_number < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is less than minimum"); + return NULL; + } + else if (color_number > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is greater than maximum"); + return NULL; + } + + if (r < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is less than minimum"); + return NULL; + } + else if (r > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is greater than maximum"); + return NULL; + } + + if (g < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is less than minimum"); + return NULL; + } + else if (g > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is greater than maximum"); + return NULL; + } + + if (b < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is less than minimum"); + return NULL; + } + else if (b > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed short integer is greater than maximum"); + return NULL; + } + + 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 +#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) + /*[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). / @@ -3061,16 +3177,51 @@ all occurrences of that color-pair are changed to the new definition. [clinic start generated code]*/ 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, int pair_number, int fg, int bg) +/*[clinic end generated code: output=a0bba03d2bbc3ee6 input=defd89de917781dc]*/ { PyCursesInitialised; PyCursesInitialisedColor; - return PyCursesCheckERR(init_pair(pair_number, fg, bg), "init_pair"); + if (pair_number < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); + return NULL; + } + 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 (fg < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); + return NULL; + } + else if (fg > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); + return NULL; + } + + if (bg < _NCURSES_COLOR_VAL_MIN) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is less than minimum"); + return NULL; + } + else if (bg > _NCURSES_COLOR_VAL_MAX) { + PyErr_SetString(PyExc_OverflowError, + "signed " _NCURSES_COLOR_VAL_TYPE_STR " is greater than maximum"); + return NULL; + } + + 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] @@ -3598,26 +3749,31 @@ _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. -[clinic start generated code]*/ +*/ static PyObject * -_curses_pair_content_impl(PyObject *module, short pair_number) -/*[clinic end generated code: output=5a72aa1a28bbacf3 input=f4d7fec5643b976b]*/ +_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; @@ -3626,6 +3782,57 @@ _curses_pair_content_impl(PyObject *module, short 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 @@ -4341,6 +4548,21 @@ make_ncurses_version(void) #endif /* NCURSES_VERSION */ +/*[clinic input] +_curses.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). +[clinic start generated code]*/ + +static PyObject * +_curses_has_extended_color_support_impl(PyObject *module) +/*[clinic end generated code: output=68f1be2b57d92e22 input=4b905f046e35ee9f]*/ +{ + return PyBool_FromLong(_NCURSES_EXTENDED_COLOR_FUNCS); +} /* List of functions defined in the module */ @@ -4367,6 +4589,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 6837eac3909695..1772aa3216e8de 100644 --- a/Modules/clinic/_cursesmodule.c.h +++ b/Modules/clinic/_cursesmodule.c.h @@ -2037,37 +2037,22 @@ PyDoc_STRVAR(_curses_color_content__doc__, {"color_content", (PyCFunction)_curses_color_content, METH_O, _curses_color_content__doc__}, static PyObject * -_curses_color_content_impl(PyObject *module, short color_number); +_curses_color_content_impl(PyObject *module, int color_number); static PyObject * _curses_color_content(PyObject *module, PyObject *arg) { PyObject *return_value = NULL; - short color_number; + int 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 < SHRT_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > SHRT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - color_number = (short) ival; - } + color_number = _PyLong_AsInt(arg); + if (color_number == -1 && PyErr_Occurred()) { + goto exit; } return_value = _curses_color_content_impl(module, color_number); @@ -2091,37 +2076,22 @@ 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, int color_number); static PyObject * _curses_color_pair(PyObject *module, PyObject *arg) { PyObject *return_value = NULL; - short color_number; + int 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 < SHRT_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > SHRT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - color_number = (short) ival; - } + color_number = _PyLong_AsInt(arg); + if (color_number == -1 && PyErr_Occurred()) { + goto exit; } return_value = _curses_color_pair_impl(module, color_number); @@ -2715,17 +2685,17 @@ PyDoc_STRVAR(_curses_init_color__doc__, {"init_color", (PyCFunction)(void(*)(void))_curses_init_color, METH_FASTCALL, _curses_init_color__doc__}, static PyObject * -_curses_init_color_impl(PyObject *module, short color_number, short r, - short g, short b); +_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; - short color_number; - short r; - short g; - short b; + int color_number; + int r; + int g; + int b; if (!_PyArg_CheckPositional("init_color", nargs, 4, 4)) { goto exit; @@ -2735,96 +2705,36 @@ _curses_init_color(PyObject *module, PyObject *const *args, Py_ssize_t nargs) "integer argument expected, got float" ); goto exit; } - { - long ival = PyLong_AsLong(args[0]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < SHRT_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > SHRT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - color_number = (short) ival; - } + 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; } - { - long ival = PyLong_AsLong(args[1]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < SHRT_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > SHRT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - r = (short) ival; - } + 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; } - { - long ival = PyLong_AsLong(args[2]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < SHRT_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > SHRT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - g = (short) ival; - } + 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; } - { - long ival = PyLong_AsLong(args[3]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < SHRT_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > SHRT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - b = (short) ival; - } + b = _PyLong_AsInt(args[3]); + if (b == -1 && PyErr_Occurred()) { + goto exit; } return_value = _curses_init_color_impl(module, color_number, r, g, b); @@ -2852,16 +2762,15 @@ PyDoc_STRVAR(_curses_init_pair__doc__, {"init_pair", (PyCFunction)(void(*)(void))_curses_init_pair, METH_FASTCALL, _curses_init_pair__doc__}, static PyObject * -_curses_init_pair_impl(PyObject *module, short pair_number, short fg, - short bg); +_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; - short pair_number; - short fg; - short bg; + int pair_number; + int fg; + int bg; if (!_PyArg_CheckPositional("init_pair", nargs, 3, 3)) { goto exit; @@ -2871,72 +2780,27 @@ _curses_init_pair(PyObject *module, PyObject *const *args, Py_ssize_t nargs) "integer argument expected, got float" ); goto exit; } - { - long ival = PyLong_AsLong(args[0]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < SHRT_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > SHRT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - pair_number = (short) ival; - } + 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; } - { - long ival = PyLong_AsLong(args[1]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < SHRT_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > SHRT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - fg = (short) ival; - } + 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; } - { - long ival = PyLong_AsLong(args[2]); - if (ival == -1 && PyErr_Occurred()) { - goto exit; - } - else if (ival < SHRT_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > SHRT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - bg = (short) ival; - } + bg = _PyLong_AsInt(args[2]); + if (bg == -1 && PyErr_Occurred()) { + goto exit; } return_value = _curses_init_pair_impl(module, pair_number, fg, bg); @@ -3628,57 +3492,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, short pair_number); - -static PyObject * -_curses_pair_content(PyObject *module, PyObject *arg) -{ - PyObject *return_value = NULL; - short 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 < SHRT_MIN) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is less than minimum"); - goto exit; - } - else if (ival > SHRT_MAX) { - PyErr_SetString(PyExc_OverflowError, - "signed short integer is greater than maximum"); - goto exit; - } - else { - pair_number = (short) ival; - } - } - return_value = _curses_pair_content_impl(module, pair_number); - -exit: - return return_value; -} - PyDoc_STRVAR(_curses_pair_number__doc__, "pair_number($module, attr, /)\n" "--\n" @@ -4494,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) */ @@ -4569,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=1350eeb0c1e06af6 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=93d2080b18581c1c input=a9049054013a1b77]*/