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
--------------
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])
......@@ -834,6 +834,16 @@ the following methods:
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()
Clear the window.
......
......@@ -333,6 +333,11 @@ function to the :mod:`crypt` module.
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`
method to get a wide character
* The :mod:`curses` module has a new :meth:`~curses.unget_wch` function to
......
......@@ -76,6 +76,7 @@ extern "C" {
typedef struct {
PyObject_HEAD
WINDOW *win;
char *encoding;
} PyCursesWindowObject;
#define PyCursesWindow_Check(v) (Py_TYPE(v) == &PyCursesWindow_Type)
......
......@@ -267,24 +267,42 @@ def test_issue6243(stdscr):
def test_unget_wch(stdscr):
if not hasattr(curses, 'unget_wch'):
return
ch = 'a'
curses.unget_wch(ch)
read = stdscr.get_wch()
read = chr(read)
if read != ch:
raise AssertionError("%r != %r" % (read, ch))
ch = ord('a')
curses.unget_wch(ch)
read = stdscr.get_wch()
if read != ch:
raise AssertionError("%r != %r" % (read, ch))
for ch in ('a', '\xe9', '\u20ac', '\U0010FFFF'):
curses.unget_wch(ch)
read = stdscr.get_wch()
read = chr(read)
if read != ch:
raise AssertionError("%r != %r" % (read, ch))
code = ord(ch)
curses.unget_wch(code)
read = stdscr.get_wch()
if read != code:
raise AssertionError("%r != %r" % (read, code))
def test_issue10570():
b = curses.tparm(curses.tigetstr("cup"), 5, 3)
assert type(b) is bytes
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):
curses.savetty()
try:
......@@ -295,6 +313,7 @@ def main(stdscr):
test_issue6243(stdscr)
test_unget_wch(stdscr)
test_issue10570()
test_encoding(stdscr)
finally:
curses.resetty()
......
......@@ -395,6 +395,10 @@ Core and Builtins
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
seed for filename generation in the tempfile module. Patch by Brian
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