Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 077af8c

Browse files
authored
bpo-37738: Fix curses addch(str, color_pair) (GH-15071)
Fix the implementation of curses addch(str, color_pair): pass the color pair to setcchar(), instead of always passing 0 as the color pair.
1 parent 7e479c8 commit 077af8c

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the implementation of curses ``addch(str, color_pair)``: pass the color
2+
pair to ``setcchar()``, instead of always passing 0 as the color pair.

Modules/_cursesmodule.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ static char *screen_encoding = NULL;
176176

177177
/* Utility Functions */
178178

179+
static inline int
180+
color_pair_to_attr(short color_number)
181+
{
182+
return ((int)color_number << 8);
183+
}
184+
185+
static inline short
186+
attr_to_color_pair(int attr)
187+
{
188+
return (short)((attr & A_COLOR) >> 8);
189+
}
190+
179191
/*
180192
* Check the return code from a curses function and return None
181193
* or raise an exception as appropriate. These are exported using the
@@ -606,7 +618,7 @@ _curses_window_addch_impl(PyCursesWindowObject *self, int group_left_1,
606618
if (type == 2) {
607619
funcname = "add_wch";
608620
wstr[1] = L'\0';
609-
setcchar(&wcval, wstr, attr, 0, NULL);
621+
setcchar(&wcval, wstr, attr, attr_to_color_pair(attr), NULL);
610622
if (coordinates_group)
611623
rtn = mvwadd_wch(self->win,y,x, &wcval);
612624
else {
@@ -2621,7 +2633,7 @@ _curses_color_pair_impl(PyObject *module, short color_number)
26212633
PyCursesInitialised;
26222634
PyCursesInitialisedColor;
26232635

2624-
return PyLong_FromLong((long) (color_number << 8));
2636+
return PyLong_FromLong(color_pair_to_attr(color_number));
26252637
}
26262638

26272639
/*[clinic input]
@@ -3644,7 +3656,7 @@ _curses_pair_number_impl(PyObject *module, int attr)
36443656
PyCursesInitialised;
36453657
PyCursesInitialisedColor;
36463658

3647-
return PyLong_FromLong((long) ((attr & A_COLOR) >> 8));
3659+
return PyLong_FromLong(attr_to_color_pair(attr));
36483660
}
36493661

36503662
/*[clinic input]

0 commit comments

Comments
 (0)