diff -r 5b0595339c9d Doc/library/curses.rst --- a/Doc/library/curses.rst Sun Aug 26 07:33:10 2012 +0300 +++ b/Doc/library/curses.rst Sun Aug 26 23:04:09 2012 +0200 @@ -869,8 +869,10 @@ the following methods and attributes: .. method:: window.get_wch([y, x]) - Get a wide character. Like :meth:`getch`, but the integer returned is the - Unicode code point for the key pressed, so it can be passed to :func:`chr`. + Get a wide character as (is_key_code, key). *is_key_code* is True for + function keys, keypad keys and so, in this case, *key* is a multibyte string + containing the key name. Otherwise, *key* is a single character + corresponding to the key. .. versionadded:: 3.3 diff -r 5b0595339c9d Lib/test/test_curses.py --- a/Lib/test/test_curses.py Sun Aug 26 07:33:10 2012 +0300 +++ b/Lib/test/test_curses.py Sun Aug 26 23:04:09 2012 +0200 @@ -267,8 +267,7 @@ def test_issue6243(stdscr): def test_unget_wch(stdscr): if not hasattr(curses, 'unget_wch'): return - import locale - encoding = locale.getpreferredencoding() + encoding = stdscr.encoding for ch in ('a', '\xe9', '\u20ac', '\U0010FFFF'): try: ch.encode(encoding) @@ -277,18 +276,21 @@ def test_unget_wch(stdscr): try: curses.unget_wch(ch) except Exception as err: - raise Exception("unget_wch(%a) failed with locale encoding %s: %s" - % (ch, encoding, err)) - read = stdscr.get_wch() - read = chr(read) + raise Exception("unget_wch(%a) failed with encoding %s: %s" + % (ch, stdscr.encoding, err)) + is_key_code, read = stdscr.get_wch() + if is_key_code: + raise AssertionError("it is not a key code") 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)) + is_key_code, read = stdscr.get_wch() + if is_key_code: + raise AssertionError("it is not a key code") + if read != ch: + raise AssertionError("%r != %r" % (read, ch)) def test_issue10570(): b = curses.tparm(curses.tigetstr("cup"), 5, 3) diff -r 5b0595339c9d Modules/_cursesmodule.c --- a/Modules/_cursesmodule.c Sun Aug 26 07:33:10 2012 +0300 +++ b/Modules/_cursesmodule.c Sun Aug 26 23:04:09 2012 +0200 @@ -1180,6 +1180,8 @@ PyCursesWindow_Get_WCh(PyCursesWindowObj int x, y; int ct; wint_t rtn; + PyObject *obj; + int is_key_code; switch (PyTuple_Size(args)) { case 0: @@ -1203,7 +1205,21 @@ PyCursesWindow_Get_WCh(PyCursesWindowObj PyErr_SetString(PyCursesError, "no input"); return NULL; } - return PyLong_FromLong(rtn); + is_key_code = (ct == KEY_CODE_YES); + if (is_key_code) { + const char *knp; +#if defined(__NetBSD__) + knp = unctrl(rtn); +#else + knp = keyname(rtn); +#endif + obj = PyUnicode_FromString((knp == NULL) ? "" : knp); + } + else + obj = PyUnicode_FromOrdinal(rtn); + if (obj == NULL) + return NULL; + return Py_BuildValue("NN", PyBool_FromLong(is_key_code), obj); } #endif