Skip to content

Commit 0fdfceb

Browse files
author
Victor Stinner
committed
Issue python#12567: The curses module uses Unicode functions for Unicode arguments
when it is linked to the ncurses library. It encodes also Unicode strings to the locale encoding instead of UTF-8.
1 parent c248476 commit 0fdfceb

6 files changed

Lines changed: 520 additions & 121 deletions

File tree

‎Doc/library/curses.rst‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ Window Objects
653653
--------------
654654

655655
Window objects, as returned by :func:`initscr` and :func:`newwin` above, have
656-
the following methods:
656+
the following methods and attributes:
657657

658658

659659
.. method:: window.addch([y, x,] ch[, attr])
@@ -834,6 +834,16 @@ the following methods:
834834
event.
835835

836836

837+
.. attribute:: window.encoding
838+
839+
Encoding used to encode method arguments (Unicode strings and characters).
840+
The encoding attribute is inherited from by parent window when a subwindow
841+
is created, for example with :meth:`window.subwin`. By default, the locale
842+
encoding is used (see :func:`locale.getpreferredencoding`).
843+
844+
.. versionadded:: 3.3
845+
846+
837847
.. method:: window.erase()
838848

839849
Clear the window.

‎Doc/whatsnew/3.3.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,11 @@ function to the :mod:`crypt` module.
333333
curses
334334
------
335335

336+
* If the :mod:`curses` module is linked to the ncursesw library, use Unicode
337+
functions when Unicode strings or characters are passed (e.g.
338+
:c:func:`waddwstr`), and bytes functions otherwise (e.g. :c:func:`waddstr`).
339+
* Use the locale encoding instead of ``utf-8`` to encode Unicode strings.
340+
* :class:`curses.window` has a new :attr:`curses.window.encoding` attribute.
336341
* The :class:`curses.window` class has a new :meth:`~curses.window.get_wch`
337342
method to get a wide character
338343
* The :mod:`curses` module has a new :meth:`~curses.unget_wch` function to

‎Include/py_curses.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ extern "C" {
7676
typedef struct {
7777
PyObject_HEAD
7878
WINDOW *win;
79+
char *encoding;
7980
} PyCursesWindowObject;
8081

8182
#define PyCursesWindow_Check(v) (Py_TYPE(v) == &PyCursesWindow_Type)

‎Lib/test/test_curses.py‎

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -267,24 +267,42 @@ def test_issue6243(stdscr):
267267
def test_unget_wch(stdscr):
268268
if not hasattr(curses, 'unget_wch'):
269269
return
270-
ch = 'a'
271-
curses.unget_wch(ch)
272-
read = stdscr.get_wch()
273-
read = chr(read)
274-
if read != ch:
275-
raise AssertionError("%r != %r" % (read, ch))
276-
277-
ch = ord('a')
278-
curses.unget_wch(ch)
279-
read = stdscr.get_wch()
280-
if read != ch:
281-
raise AssertionError("%r != %r" % (read, ch))
270+
for ch in ('a', '\xe9', '\u20ac', '\U0010FFFF'):
271+
curses.unget_wch(ch)
272+
read = stdscr.get_wch()
273+
read = chr(read)
274+
if read != ch:
275+
raise AssertionError("%r != %r" % (read, ch))
276+
277+
code = ord(ch)
278+
curses.unget_wch(code)
279+
read = stdscr.get_wch()
280+
if read != code:
281+
raise AssertionError("%r != %r" % (read, code))
282282

283283
def test_issue10570():
284284
b = curses.tparm(curses.tigetstr("cup"), 5, 3)
285285
assert type(b) is bytes
286286
curses.putp(b)
287287

288+
def test_encoding(stdscr):
289+
import codecs
290+
encoding = stdscr.encoding
291+
codecs.lookup(encoding)
292+
try:
293+
stdscr.encoding = 10
294+
except TypeError:
295+
pass
296+
else:
297+
raise AssertionError("TypeError not raised")
298+
stdscr.encoding = encoding
299+
try:
300+
del stdscr.encoding
301+
except TypeError:
302+
pass
303+
else:
304+
raise AssertionError("TypeError not raised")
305+
288306
def main(stdscr):
289307
curses.savetty()
290308
try:
@@ -295,6 +313,7 @@ def main(stdscr):
295313
test_issue6243(stdscr)
296314
test_unget_wch(stdscr)
297315
test_issue10570()
316+
test_encoding(stdscr)
298317
finally:
299318
curses.resetty()
300319

‎Misc/NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,10 @@ Core and Builtins
395395
Library
396396
-------
397397

398+
- Issue #12567: The curses module uses Unicode functions for Unicode arguments
399+
when it is linked to the ncurses library. It encodes also Unicode strings to
400+
the locale encoding instead of UTF-8.
401+
398402
- Issue #12856: Ensure child processes do not inherit the parent's random
399403
seed for filename generation in the tempfile module. Patch by Brian
400404
Harring.

0 commit comments

Comments
 (0)