Commit 0fdfceb7 authored by Victor Stinner's avatar Victor Stinner

Issue #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.
parent c2484765
...@@ -653,7 +653,7 @@ Window Objects ...@@ -653,7 +653,7 @@ Window Objects
-------------- --------------
Window objects, as returned by :func:`initscr` and :func:`newwin` above, have Window objects, as returned by :func:`initscr` and :func:`newwin` above, have
the following methods: the following methods and attributes:
.. method:: window.addch([y, x,] ch[, attr]) .. method:: window.addch([y, x,] ch[, attr])
...@@ -834,6 +834,16 @@ the following methods: ...@@ -834,6 +834,16 @@ the following methods:
event. event.
.. attribute:: window.encoding
Encoding used to encode method arguments (Unicode strings and characters).
The encoding attribute is inherited from by parent window when a subwindow
is created, for example with :meth:`window.subwin`. By default, the locale
encoding is used (see :func:`locale.getpreferredencoding`).
.. versionadded:: 3.3
.. method:: window.erase() .. method:: window.erase()
Clear the window. Clear the window.
......
...@@ -333,6 +333,11 @@ function to the :mod:`crypt` module. ...@@ -333,6 +333,11 @@ function to the :mod:`crypt` module.
curses curses
------ ------
* If the :mod:`curses` module is linked to the ncursesw library, use Unicode
functions when Unicode strings or characters are passed (e.g.
:c:func:`waddwstr`), and bytes functions otherwise (e.g. :c:func:`waddstr`).
* Use the locale encoding instead of ``utf-8`` to encode Unicode strings.
* :class:`curses.window` has a new :attr:`curses.window.encoding` attribute.
* The :class:`curses.window` class has a new :meth:`~curses.window.get_wch` * The :class:`curses.window` class has a new :meth:`~curses.window.get_wch`
method to get a wide character method to get a wide character
* The :mod:`curses` module has a new :meth:`~curses.unget_wch` function to * The :mod:`curses` module has a new :meth:`~curses.unget_wch` function to
......
...@@ -76,6 +76,7 @@ extern "C" { ...@@ -76,6 +76,7 @@ extern "C" {
typedef struct { typedef struct {
PyObject_HEAD PyObject_HEAD
WINDOW *win; WINDOW *win;
char *encoding;
} PyCursesWindowObject; } PyCursesWindowObject;
#define PyCursesWindow_Check(v) (Py_TYPE(v) == &PyCursesWindow_Type) #define PyCursesWindow_Check(v) (Py_TYPE(v) == &PyCursesWindow_Type)
......
...@@ -267,24 +267,42 @@ def test_issue6243(stdscr): ...@@ -267,24 +267,42 @@ def test_issue6243(stdscr):
def test_unget_wch(stdscr): def test_unget_wch(stdscr):
if not hasattr(curses, 'unget_wch'): if not hasattr(curses, 'unget_wch'):
return return
ch = 'a' for ch in ('a', '\xe9', '\u20ac', '\U0010FFFF'):
curses.unget_wch(ch) curses.unget_wch(ch)
read = stdscr.get_wch() read = stdscr.get_wch()
read = chr(read) read = chr(read)
if read != ch: if read != ch:
raise AssertionError("%r != %r" % (read, ch)) raise AssertionError("%r != %r" % (read, ch))
ch = ord('a') code = ord(ch)
curses.unget_wch(ch) curses.unget_wch(code)
read = stdscr.get_wch() read = stdscr.get_wch()
if read != ch: if read != code:
raise AssertionError("%r != %r" % (read, ch)) raise AssertionError("%r != %r" % (read, code))
def test_issue10570(): def test_issue10570():
b = curses.tparm(curses.tigetstr("cup"), 5, 3) b = curses.tparm(curses.tigetstr("cup"), 5, 3)
assert type(b) is bytes assert type(b) is bytes
curses.putp(b) curses.putp(b)
def test_encoding(stdscr):
import codecs
encoding = stdscr.encoding
codecs.lookup(encoding)
try:
stdscr.encoding = 10
except TypeError:
pass
else:
raise AssertionError("TypeError not raised")
stdscr.encoding = encoding
try:
del stdscr.encoding
except TypeError:
pass
else:
raise AssertionError("TypeError not raised")
def main(stdscr): def main(stdscr):
curses.savetty() curses.savetty()
try: try:
...@@ -295,6 +313,7 @@ def main(stdscr): ...@@ -295,6 +313,7 @@ def main(stdscr):
test_issue6243(stdscr) test_issue6243(stdscr)
test_unget_wch(stdscr) test_unget_wch(stdscr)
test_issue10570() test_issue10570()
test_encoding(stdscr)
finally: finally:
curses.resetty() curses.resetty()
......
...@@ -395,6 +395,10 @@ Core and Builtins ...@@ -395,6 +395,10 @@ Core and Builtins
Library Library
------- -------
- Issue #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.
- Issue #12856: Ensure child processes do not inherit the parent's random - Issue #12856: Ensure child processes do not inherit the parent's random
seed for filename generation in the tempfile module. Patch by Brian seed for filename generation in the tempfile module. Patch by Brian
Harring. Harring.
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment