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.
This commit is contained in:
Victor Stinner
2011-11-25 22:10:02 +01:00
parent c24847658f
commit 0fdfceb782
6 changed files with 521 additions and 122 deletions

View File

@@ -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.

View File

@@ -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

View File

@@ -76,6 +76,7 @@ extern "C" {
typedef struct {
PyObject_HEAD
WINDOW *win;
char *encoding;
} PyCursesWindowObject;
#define PyCursesWindow_Check(v) (Py_TYPE(v) == &PyCursesWindow_Type)

View File

@@ -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))
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))
ch = ord('a')
curses.unget_wch(ch)
read = stdscr.get_wch()
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()

View File

@@ -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.

File diff suppressed because it is too large Load Diff