diff --git a/Doc/library/curses.rst b/Doc/library/curses.rst index ac34bf2b8ed3ff..740374ee743478 100644 --- a/Doc/library/curses.rst +++ b/Doc/library/curses.rst @@ -1032,10 +1032,15 @@ Terminfo database Instantiate the bytes object *str* with the supplied parameters, where *str* should be a parameterized byte string obtained from the terminfo database. For example, ``tparm(tigetstr("cup"), 5, 3)`` could result in ``b'\033[6;4H'``, the exact - result depending on terminal type. Up to nine integer parameters may be supplied. + result depending on terminal type. Up to nine parameters may be supplied. + They are integers, except in a few cases, such as ``Ms`` of xterm, + where they must be bytes objects. :func:`setupterm` (or :func:`initscr`) must be called first. + .. versionchanged:: next + Added support of bytes parameters. + .. function:: putp(str) Equivalent to ``tputs(str, 1, putchar)``; emit the value of a specified diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py index 4d39e5014e654b..8579b3eed73a97 100644 --- a/Lib/test/test_curses.py +++ b/Lib/test/test_curses.py @@ -1873,6 +1873,17 @@ def test_terminfo(self): self.assertIsInstance(cud_2, bytes) curses.putp(cud_2) + # The few capabilities that take strings take them as bytes. + ms = curses.tigetstr('Ms') + if ms is not None and b'%p2%s' in ms: + self.assertIn(b'aGk=', curses.tparm(ms, b'c', b'aGk=')) + self.assertRaises(TypeError, curses.tparm, ms, 1, 2) + self.assertRaises(ValueError, curses.tparm, ms, b'c', b'a\0') + # An omitted string is empty. + self.assertNotIn(b'%', curses.tparm(ms, b'c')) + self.assertRaises(TypeError, curses.tparm, b'%p1%d', b'x') + self.assertRaises(TypeError, curses.tparm, b'%p1%d', *range(10)) + curses.putp(b'abc\n') def test_misc_module_funcs(self): diff --git a/Misc/NEWS.d/next/Library/2026-11-07-20-26-52.gh-issue-161571.qQ8OhA.rst b/Misc/NEWS.d/next/Library/2026-11-07-20-26-52.gh-issue-161571.qQ8OhA.rst new file mode 100644 index 00000000000000..804b8a494ca432 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-07-20-26-52.gh-issue-161571.qQ8OhA.rst @@ -0,0 +1,3 @@ +Fix a crash in :func:`curses.tparm` for capabilities with parameters +that they format as strings, such as ``Ms`` of xterm. +Bytes objects are now accepted for these parameters. diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index e82c78691c7f6a..94cad7f02e31bb 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -8374,35 +8374,155 @@ _curses_tigetstr_impl(PyObject *module, const char *capname) return PyBytes_FromString( capname ); } +#ifndef NCURSES_TPARM_ARG +# define NCURSES_TPARM_ARG long +#endif + +/* Return a bit mask of the parameters of a terminfo string that curses takes + as strings: those that %s or %l follows directly after %p, as in ncurses' + _nc_tparm_analyze(). The others are numbers. */ +static unsigned int +curses_tparm_string_params(const char *str) +{ + unsigned int mask = 0; + int last = 0; /* the parameter pushed last, or 0 */ + + while (*str) { + if (*str++ != '%') { + continue; + } + /* Skip the flags, width and precision of a conversion. */ + int allowminus = 0; + while (*str) { + if (*str == ':') { + allowminus = 1; + } + else if (!(*str == '#' || *str == ' ' || *str == '.' + || Py_ISDIGIT(*str) || (*str == '-' && allowminus))) + { + break; + } + str++; + } + switch (*str) { + case 'p': + if (str[1] >= '1' && str[1] <= '9') { + last = str[1] - '0'; + } + str++; + break; + case 's': + case 'l': + if (last > 0) { + mask |= 1u << (last - 1); + } + break; + case 'd': case 'o': case 'x': case 'X': case 'c': + case '+': case '-': case '*': case '/': case 'm': + case '&': case '|': case '^': case '=': case '<': case '>': + case 'A': case 'O': case '!': case '~': + last = 0; + break; + case '\'': + str += 2; + last = 0; + break; + case '\0': + return mask; + default: + /* %P, %g, %{, %i, %?, %t, %e, %; and %% keep it. */ + break; + } + str++; + } + return mask; +} + +/* Convert a tparm() parameter: a string for those that the terminfo string + takes as strings (passing a number there would crash curses), otherwise a + number. An omitted one is 0, or NULL, which curses takes as "". */ +static int +curses_tparm_param(PyObject *arg, int argnum, int is_string, + NCURSES_TPARM_ARG *result) +{ + if (arg == NULL) { + *result = 0; + return 0; + } + if (is_string) { + if (!PyBytes_Check(arg)) { + PyErr_Format(PyExc_TypeError, + "tparm() argument %d must be bytes, not %T", + argnum, arg); + return -1; + } + const char *value = PyBytes_AS_STRING(arg); + if (strlen(value) != (size_t)PyBytes_GET_SIZE(arg)) { + PyErr_SetString(PyExc_ValueError, "embedded null byte"); + return -1; + } + *result = (NCURSES_TPARM_ARG)(intptr_t)value; + return 0; + } + if (PyBytes_Check(arg)) { + PyErr_Format(PyExc_TypeError, + "tparm() argument %d must be int, not bytes", argnum); + return -1; + } + int value = PyLong_AsInt(arg); + if (value == -1 && PyErr_Occurred()) { + return -1; + } + *result = value; + return 0; +} + /*[clinic input] _curses.tparm - str: str(accept={robuffer}) + str as string: object Parameterized byte string obtained from the terminfo database. - i1: int = 0 - i2: int = 0 - i3: int = 0 - i4: int = 0 - i5: int = 0 - i6: int = 0 - i7: int = 0 - i8: int = 0 - i9: int = 0 / + *params: tuple + Up to nine parameters. Instantiate the specified byte string with the supplied parameters. + +The parameters are integers, except in a few cases where they must be +bytes objects. [clinic start generated code]*/ static PyObject * -_curses_tparm_impl(PyObject *module, const char *str, int i1, int i2, int i3, - int i4, int i5, int i6, int i7, int i8, int i9) -/*[clinic end generated code: output=599f62b615c667ff input=5e30b15786f032aa]*/ +_curses_tparm_impl(PyObject *module, PyObject *string, PyObject *params) +/*[clinic end generated code: output=3439bb1b37a13d5b input=4889c0e3a5eab5b5]*/ { char* result = NULL; + NCURSES_TPARM_ARG values[9]; + Py_ssize_t count = PyTuple_GET_SIZE(params); + const char *str; + if (!PyArg_Parse(string, "y:tparm", &str)) { + return NULL; + } + + if (count > 9) { + PyErr_Format(PyExc_TypeError, + "tparm() takes at most 9 parameters (%zd given)", count); + return NULL; + } PyCursesStatefulSetupTermCalled(module); - result = tparm((char *)str,i1,i2,i3,i4,i5,i6,i7,i8,i9); + unsigned int strings = curses_tparm_string_params(str); + for (int i = 0; i < 9; i++) { + PyObject *arg = i < count ? PyTuple_GET_ITEM(params, i) : NULL; + if (curses_tparm_param(arg, i + 2, strings & (1u << i), + &values[i]) < 0) + { + return NULL; + } + } + result = tparm((char *)str, values[0], values[1], values[2], values[3], + values[4], values[5], values[6], values[7], values[8]); if (!result) { curses_set_null_error(module, "tparm", NULL); return NULL; diff --git a/Modules/clinic/_cursesmodule.c.h b/Modules/clinic/_cursesmodule.c.h index 39512bc6cad3d6..ac9a4de4c960c8 100644 --- a/Modules/clinic/_cursesmodule.c.h +++ b/Modules/clinic/_cursesmodule.c.h @@ -5803,44 +5803,46 @@ _curses_tigetstr(PyObject *module, PyObject *arg) } PyDoc_STRVAR(_curses_tparm__doc__, -"tparm($module, str, i1=0, i2=0, i3=0, i4=0, i5=0, i6=0, i7=0, i8=0,\n" -" i9=0, /)\n" +"tparm($module, str, /, *params)\n" "--\n" "\n" "Instantiate the specified byte string with the supplied parameters.\n" "\n" " str\n" -" Parameterized byte string obtained from the terminfo database."); +" Parameterized byte string obtained from the terminfo database.\n" +" params\n" +" Up to nine parameters.\n" +"\n" +"The parameters are integers, except in a few cases where they must be\n" +"bytes objects."); #define _CURSES_TPARM_METHODDEF \ {"tparm", _PyCFunction_CAST(_curses_tparm), METH_FASTCALL, _curses_tparm__doc__}, static PyObject * -_curses_tparm_impl(PyObject *module, const char *str, int i1, int i2, int i3, - int i4, int i5, int i6, int i7, int i8, int i9); +_curses_tparm_impl(PyObject *module, PyObject *string, PyObject *params); static PyObject * _curses_tparm(PyObject *module, PyObject *const *args, Py_ssize_t nargs) { PyObject *return_value = NULL; - const char *str; - int i1 = 0; - int i2 = 0; - int i3 = 0; - int i4 = 0; - int i5 = 0; - int i6 = 0; - int i7 = 0; - int i8 = 0; - int i9 = 0; + PyObject *string; + PyObject *params = NULL; - if (!PyArg_ParseArray(args, nargs, "y|iiiiiiiii:tparm", - &str, &i1, &i2, &i3, &i4, &i5, &i6, &i7, &i8, &i9)) { + if (!_PyArg_CheckPositional("tparm", nargs, 1, PY_SSIZE_T_MAX)) { + goto exit; + } + string = args[0]; + params = PyTuple_FromArray(args + 1, nargs - 1); + if (params == NULL) { goto exit; } - return_value = _curses_tparm_impl(module, str, i1, i2, i3, i4, i5, i6, i7, i8, i9); + return_value = _curses_tparm_impl(module, string, params); exit: + /* Cleanup for params */ + Py_XDECREF(params); + return return_value; } @@ -6715,4 +6717,4 @@ _curses_has_extended_color_support(PyObject *module, PyObject *Py_UNUSED(ignored #ifndef _CURSES_ASSUME_DEFAULT_COLORS_METHODDEF #define _CURSES_ASSUME_DEFAULT_COLORS_METHODDEF #endif /* !defined(_CURSES_ASSUME_DEFAULT_COLORS_METHODDEF) */ -/*[clinic end generated code: output=417ad397dfaa96ce input=a9049054013a1b77]*/ +/*[clinic end generated code: output=2e47ca767f378409 input=a9049054013a1b77]*/