@@ -176,6 +176,18 @@ static char *screen_encoding = NULL;
176
176
177
177
/* Utility Functions */
178
178
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
+
179
191
/*
180
192
* Check the return code from a curses function and return None
181
193
* 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,
606
618
if (type == 2 ) {
607
619
funcname = "add_wch" ;
608
620
wstr [1 ] = L'\0' ;
609
- setcchar (& wcval , wstr , attr , 0 , NULL );
621
+ setcchar (& wcval , wstr , attr , attr_to_color_pair ( attr ) , NULL );
610
622
if (coordinates_group )
611
623
rtn = mvwadd_wch (self -> win ,y ,x , & wcval );
612
624
else {
@@ -2621,7 +2633,7 @@ _curses_color_pair_impl(PyObject *module, short color_number)
2621
2633
PyCursesInitialised ;
2622
2634
PyCursesInitialisedColor ;
2623
2635
2624
- return PyLong_FromLong (( long ) ( color_number << 8 ));
2636
+ return PyLong_FromLong (color_pair_to_attr ( color_number ));
2625
2637
}
2626
2638
2627
2639
/*[clinic input]
@@ -3644,7 +3656,7 @@ _curses_pair_number_impl(PyObject *module, int attr)
3644
3656
PyCursesInitialised ;
3645
3657
PyCursesInitialisedColor ;
3646
3658
3647
- return PyLong_FromLong (( long ) (( attr & A_COLOR ) >> 8 ));
3659
+ return PyLong_FromLong (attr_to_color_pair ( attr ));
3648
3660
}
3649
3661
3650
3662
/*[clinic input]
0 commit comments