Skip to content

Commit fe1f5a5

Browse files
[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 0b083c4) Co-authored-by: Vyron Vasileiadis <[email protected]> Signed-off-by: Sankalp Thakur <[email protected]>
1 parent 08b5898 commit fe1f5a5

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎Lib/test/test_curses.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,20 @@ def test_env_queries(self):
10431043
self.assertIsInstance(c, bytes)
10441044
self.assertEqual(len(c), 1)
10451045

1046+
def test_termattrs_is_not_negative(self):
1047+
# A_ITALIC is the topmost bit of a 32-bit attribute mask, so termattrs()
1048+
# only tells a signed result from an unsigned one on a terminal that
1049+
# advertises it. 3.15 lacks the newterm()/pty harness used on main, so
1050+
# exercise the current screen: skip when the top bit is not advertised.
1051+
attrs = curses.termattrs()
1052+
italic = getattr(curses, 'A_ITALIC', 0)
1053+
if not italic or not attrs & italic:
1054+
self.skipTest('the terminal advertises no attribute in the top bit')
1055+
self.assertGreaterEqual(attrs, 0)
1056+
# termattrs() exists to be passed back to the attribute functions,
1057+
# which reject a negative mask.
1058+
self.stdscr.attrset(attrs)
1059+
10461060
def test_output_options(self):
10471061
stdscr = self.stdscr
10481062

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :func:`curses.termattrs` returning a negative value on a terminal that
2+
supports :const:`curses.A_ITALIC`, which left its result unusable as an
3+
attribute mask.

‎Modules/_cursesmodule.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4981,7 +4981,11 @@ Return a logical OR of all video attributes supported by the terminal.
49814981
static PyObject *
49824982
_curses_termattrs_impl(PyObject *module)
49834983
/*[clinic end generated code: output=b06f437fce1b6fc4 input=0559882a04f84d1d]*/
4984-
NoArgReturnIntFunctionBody(termattrs)
4984+
{
4985+
PyCursesStatefulInitialised(module);
4986+
4987+
return PyLong_FromUnsignedLong((unsigned long)(chtype)termattrs());
4988+
}
49854989

49864990
/*[clinic input]
49874991
@permit_long_summary

0 commit comments

Comments
 (0)