@@ -527,6 +527,11 @@ PyCurses_ConvertToWideCell(PyObject *obj, wchar_t *wch)
527527 (int )CCHARW_MAX , PyUnicode_GET_LENGTH (obj ));
528528 return -1 ;
529529 }
530+ #ifdef _Py_NON_UNICODE_WCHAR_T
531+ if (_Py_UnicodeToLocaleWchar_InPlace (wch , nch ) < 0 ) {
532+ return -1 ;
533+ }
534+ #endif
530535 /* A lone control character is allowed (like addch(ord('\n'))), but in a
531536 multi-character cell the base must be a printable spacing character and
532537 the rest zero-width combining characters. Check explicitly: otherwise
@@ -622,6 +627,13 @@ PyCurses_ConvertToString(PyCursesWindowObject *win, PyObject *obj,
622627 * wstr = PyUnicode_AsWideCharString (obj , NULL );
623628 if (* wstr == NULL )
624629 return 0 ;
630+ #ifdef _Py_NON_UNICODE_WCHAR_T
631+ if (_Py_UnicodeToLocaleWchar_InPlace (* wstr , wcslen (* wstr )) < 0 ) {
632+ PyMem_Free (* wstr );
633+ * wstr = NULL ;
634+ return 0 ;
635+ }
636+ #endif
625637 return 2 ;
626638#else
627639 assert (wstr == NULL );
@@ -880,6 +892,9 @@ curses_cell_text(cursesmodule_state *state, const curses_cell_t *cell)
880892 PyErr_SetString (state -> error , "getcchar() returned ERR" );
881893 return NULL ;
882894 }
895+ #ifdef _Py_NON_UNICODE_WCHAR_T
896+ _Py_LocaleWcharToUnicode_InPlace (wstr , wcslen (wstr ));
897+ #endif
883898 return PyUnicode_FromWideChar (wstr , -1 );
884899#else
885900 char ch = (char )(* cell & A_CHARTEXT );
@@ -1296,6 +1311,12 @@ complexstr_from_string(cursesmodule_state *state, PyObject *str,
12961311 if (wbuf == NULL ) {
12971312 return NULL ;
12981313 }
1314+ #ifdef _Py_NON_UNICODE_WCHAR_T
1315+ if (_Py_UnicodeToLocaleWchar_InPlace (wbuf , n ) < 0 ) {
1316+ PyMem_Free (wbuf );
1317+ return NULL ;
1318+ }
1319+ #endif
12991320 curses_cell_t * cells = n > 0 ? PyMem_New (curses_cell_t , n ) : NULL ;
13001321 if (n > 0 && cells == NULL ) {
13011322 PyMem_Free (wbuf );
@@ -1585,6 +1606,9 @@ complexstr_str(PyObject *self)
15851606 }
15861607 pos += wcslen (buf + pos );
15871608 }
1609+ #ifdef _Py_NON_UNICODE_WCHAR_T
1610+ _Py_LocaleWcharToUnicode_InPlace (buf , pos );
1611+ #endif
15881612 PyObject * res = PyUnicode_FromWideChar (buf , pos );
15891613 PyMem_Free (buf );
15901614 return res ;
@@ -3275,6 +3299,13 @@ _curses_window_getkey_impl(PyCursesWindowObject *self, int group_right_1,
32753299 rtn += 256 ;
32763300 }
32773301#endif
3302+ #endif
3303+ #ifdef _Py_NON_UNICODE_WCHAR_T
3304+ {
3305+ wchar_t wch = (wchar_t )rtn ;
3306+ _Py_LocaleWcharToUnicode_InPlace (& wch , 1 );
3307+ return PyUnicode_FromOrdinal (wch );
3308+ }
32783309#endif
32793310 return PyUnicode_FromOrdinal (rtn );
32803311 } else {
@@ -3325,8 +3356,16 @@ _curses_window_get_wch_impl(PyCursesWindowObject *self, int group_right_1,
33253356 }
33263357 if (ct == KEY_CODE_YES )
33273358 return PyLong_FromLong (rtn );
3359+ #ifdef _Py_NON_UNICODE_WCHAR_T
3360+ else {
3361+ wchar_t wch = (wchar_t )rtn ;
3362+ _Py_LocaleWcharToUnicode_InPlace (& wch , 1 );
3363+ return PyUnicode_FromOrdinal (wch );
3364+ }
3365+ #else
33283366 else
33293367 return PyUnicode_FromOrdinal (rtn );
3368+ #endif
33303369#else
33313370 /* Without the wide library, read one key with wgetch(): a value above 255
33323371 is a function key (returned as an int); a byte is decoded with the
@@ -3801,6 +3840,9 @@ PyCursesWindow_get_wstr(PyObject *op, PyObject *args)
38013840 for (Py_ssize_t i = 0 ; i < len ; i ++ ) {
38023841 wbuf [i ] = (wchar_t )buf [i ];
38033842 }
3843+ #ifdef _Py_NON_UNICODE_WCHAR_T
3844+ _Py_LocaleWcharToUnicode_InPlace (wbuf , len );
3845+ #endif
38043846 PyObject * res = PyUnicode_FromWideChar (wbuf , len );
38053847 PyMem_Free (wbuf );
38063848 PyMem_Free (buf );
@@ -3866,6 +3908,9 @@ PyCursesWindow_in_wstr(PyObject *op, PyObject *args)
38663908 PyMem_Free (buf );
38673909 return Py_GetConstant (Py_CONSTANT_EMPTY_STR );
38683910 }
3911+ #ifdef _Py_NON_UNICODE_WCHAR_T
3912+ _Py_LocaleWcharToUnicode_InPlace (buf , wcslen (buf ));
3913+ #endif
38693914 PyObject * res = PyUnicode_FromWideChar (buf , -1 );
38703915 PyMem_Free (buf );
38713916 return res ;
@@ -5854,6 +5899,9 @@ _curses_erasewchar_impl(PyObject *module)
58545899 curses_set_error (module , "erasewchar" , NULL );
58555900 return NULL ;
58565901 }
5902+ #ifdef _Py_NON_UNICODE_WCHAR_T
5903+ _Py_LocaleWcharToUnicode_InPlace (& ch , 1 );
5904+ #endif
58575905 return PyUnicode_FromWideChar (& ch , 1 );
58585906#else
58595907 /* Without the wide library, decode the single-byte erase character
@@ -7076,6 +7124,9 @@ _curses_killwchar_impl(PyObject *module)
70767124 curses_set_error (module , "killwchar" , NULL );
70777125 return NULL ;
70787126 }
7127+ #ifdef _Py_NON_UNICODE_WCHAR_T
7128+ _Py_LocaleWcharToUnicode_InPlace (& ch , 1 );
7129+ #endif
70797130 return PyUnicode_FromWideChar (& ch , 1 );
70807131#else
70817132 /* Without the wide library, decode the single-byte kill character
@@ -8018,6 +8069,19 @@ _curses_wunctrl(PyObject *module, PyObject *ch)
80188069 curses_set_null_error (module , "wunctrl" , NULL );
80198070 return NULL ;
80208071 }
8072+ #ifdef _Py_NON_UNICODE_WCHAR_T
8073+ {
8074+ /* wunctrl() returns a pointer to a static buffer; make a copy
8075+ before converting in place. */
8076+ wchar_t wbuf [8 ];
8077+ size_t len = wcslen (res );
8078+ if (len < Py_ARRAY_LENGTH (wbuf )) {
8079+ wcscpy (wbuf , res );
8080+ _Py_LocaleWcharToUnicode_InPlace (wbuf , len );
8081+ return PyUnicode_FromWideChar (wbuf , len );
8082+ }
8083+ }
8084+ #endif
80218085 return PyUnicode_FromWideChar (res , -1 );
80228086#else
80238087 /* Without the wide library, fall back to the single-byte unctrl() and
@@ -8107,6 +8171,10 @@ _curses_ungetch(PyObject *module, PyObject *ch)
81078171 wchar_t wch ;
81088172 if (!PyCurses_ConvertToWchar_t (ch , & wch ))
81098173 return NULL ;
8174+ #ifdef _Py_NON_UNICODE_WCHAR_T
8175+ if (_Py_UnicodeToLocaleWchar_InPlace (& wch , 1 ) < 0 )
8176+ return NULL ;
8177+ #endif
81108178 return curses_check_err (module , unget_wch (wch ), "unget_wch" , "ungetch" );
81118179 }
81128180#endif
@@ -8138,6 +8206,10 @@ _curses_unget_wch(PyObject *module, PyObject *ch)
81388206 if (!PyCurses_ConvertToWchar_t (ch , & wch ))
81398207 return NULL ;
81408208#ifdef HAVE_NCURSESW
8209+ #ifdef _Py_NON_UNICODE_WCHAR_T
8210+ if (_Py_UnicodeToLocaleWchar_InPlace (& wch , 1 ) < 0 )
8211+ return NULL ;
8212+ #endif
81418213 return curses_check_err (module , unget_wch (wch ), "unget_wch" , NULL );
81428214#else
81438215 /* Without the wide library there is no unget_wch(): encode the character as
@@ -8226,6 +8298,12 @@ _curses_slk_set_impl(PyObject *module, int labnum, PyObject *label,
82268298 if (wstr == NULL ) {
82278299 return NULL ;
82288300 }
8301+ #ifdef _Py_NON_UNICODE_WCHAR_T
8302+ if (_Py_UnicodeToLocaleWchar_InPlace (wstr , wcslen (wstr )) < 0 ) {
8303+ PyMem_Free (wstr );
8304+ return NULL ;
8305+ }
8306+ #endif
82298307 rtn = slk_wset (labnum , wstr , justify );
82308308 PyMem_Free (wstr );
82318309#else
0 commit comments