From 1a094f51d998f38626039014377b730ee7fd3119 Mon Sep 17 00:00:00 2001 From: Sankalp Thakur <31366524+sankalpsthakur@users.noreply.github.com> Date: Tue, 24 Nov 2026 16:20:39 +0400 Subject: [PATCH] [3.15] gh-154874: Fix the sign of curses.termattrs() (GH-154875) (GH-155548) termattrs() returns a chtype mask, but it was routed through an int, so a terminal that advertises A_ITALIC came back negative and the result could no longer be passed to the attribute functions. (cherry picked from commit 0b083c43a455fbdf5378495751e142ea83e294b0) (cherry picked from commit fe1f5a5a22c6168a522c3c36a3b62ab9ed3092be) Co-authored-by: Sankalp Thakur <31366524+sankalpsthakur@users.noreply.github.com> Co-authored-by: Vyron Vasileiadis Signed-off-by: Sankalp Thakur --- Lib/test/test_curses.py | 14 ++++++++++++++ .../2026-07-29-11-58-17.gh-issue-154874.NAPdBp.rst | 3 +++ Modules/_cursesmodule.c | 6 +++++- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-07-29-11-58-17.gh-issue-154874.NAPdBp.rst diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py index 3d1dc5b3c4168f..36c02f0df8f10f 100644 --- a/Lib/test/test_curses.py +++ b/Lib/test/test_curses.py @@ -1039,6 +1039,20 @@ def test_env_queries(self): self.assertIsInstance(c, bytes) self.assertEqual(len(c), 1) + def test_termattrs_is_not_negative(self): + # A_ITALIC is the topmost bit of a 32-bit attribute mask, so termattrs() + # only tells a signed result from an unsigned one on a terminal that + # advertises it. 3.15 lacks the newterm()/pty harness used on main, so + # exercise the current screen: skip when the top bit is not advertised. + attrs = curses.termattrs() + italic = getattr(curses, 'A_ITALIC', 0) + if not italic or not attrs & italic: + self.skipTest('the terminal advertises no attribute in the top bit') + self.assertGreaterEqual(attrs, 0) + # termattrs() exists to be passed back to the attribute functions, + # which reject a negative mask. + self.stdscr.attrset(attrs) + def test_output_options(self): stdscr = self.stdscr diff --git a/Misc/NEWS.d/next/Library/2026-07-29-11-58-17.gh-issue-154874.NAPdBp.rst b/Misc/NEWS.d/next/Library/2026-07-29-11-58-17.gh-issue-154874.NAPdBp.rst new file mode 100644 index 00000000000000..61ba4f7374b318 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-29-11-58-17.gh-issue-154874.NAPdBp.rst @@ -0,0 +1,3 @@ +Fix :func:`curses.termattrs` returning a negative value on a terminal that +supports :const:`curses.A_ITALIC`, which left its result unusable as an +attribute mask. diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index 8d8544b4d9c20f..44216a5030e635 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -4677,7 +4677,11 @@ Return a logical OR of all video attributes supported by the terminal. static PyObject * _curses_termattrs_impl(PyObject *module) /*[clinic end generated code: output=b06f437fce1b6fc4 input=0559882a04f84d1d]*/ -NoArgReturnIntFunctionBody(termattrs) +{ + PyCursesStatefulInitialised(module); + + return PyLong_FromUnsignedLong((unsigned long)(chtype)termattrs()); +} /*[clinic input] _curses.termname