8000 bpo-37738: Fix curses addch(str, color_pair) (GH-15071) (GH-15273) · python/cpython@7eef81e · GitHub
[go: up one dir, main page]

Skip to content

Commit 7eef81e

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

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
@@ -178,6 +178,18 @@ static char *screen_encoding = NULL;
178178

179179
/* Utility Functions */
180180

181+
static inline int
182+
color_pair_to_attr(short color_number)
183+
{
184+
return ((int)color_number << 8);
185+
}
186+
187+
static inline short
188+
attr_to_color_pair(int attr)
189+
{
190+
return (short)((attr & A_COLOR) >> 8);
191+
}
192+
181193
/*
182194
* Check the return code from a curses function and return None
183195
* or raise an exception as appropriate. These are exported using the
@@ -614,7 +626,7 @@ curses_window_addch_impl(PyCursesWindowObject *self, int group_left_1, int y,
614626
if (type == 2) {
615627
funcname = "add_wch";
616628
wstr[1] = L'\0';
617-
setcchar(&wcval, wstr, attr, 0, NULL);
629+
setcchar(&wcval, wstr, attr, attr_to_color_pair(attr), NULL);
618630
if (coordinates_group)
619631
rtn = mvwadd_wch(cwself->win,y,x, &wcval);
620632
else {
@@ -2204,7 +2216,7 @@ PyCurses_color_pair(PyObject *self, PyObject *args)
22042216
PyCursesInitialisedColor;
22052217

22062218
if (!PyArg_ParseTuple(args, "i:color_pair", &n)) return NULL;
2207-
return PyLong_FromLong((long) (n << 8));
2219+
return PyLong_FromLong(color_pair_to_attr(n));
22082220
}
22092221

22102222
static PyObject *
@@ -2802,7 +2814,7 @@ PyCurses_pair_number(PyObject *self, PyObject *args)
28022814
return NULL;
28032815
}
28042816

2805-
return PyLong_FromLong((long) ((n & A_COLOR) >> 8));
2817+
return PyLong_FromLong(attr_to_color_pair(n));
28062818
}
28072819

28082820
static PyObject *

0 commit comments

Comments
 (0)
0