Skip to content

Commit 71e44cb

Browse files
author
Victor Stinner
committed
Issue python#12567: Add curses.unget_wch() function
Push a character so the next get_wch() will return it.
1 parent d4d8ae5 commit 71e44cb

4 files changed

Lines changed: 97 additions & 0 deletions

File tree

‎Doc/library/curses.rst‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,17 @@ The module :mod:`curses` defines the following functions:
598598
Only one *ch* can be pushed before :meth:`getch` is called.
599599

600600

601+
.. function:: unget_wch(ch)
602+
603+
Push *ch* so the next :meth:`get_wch` will return it.
604+
605+
.. note::
606+
607+
Only one *ch* can be pushed before :meth:`get_wch` is called.
608+
609+
.. versionadded:: 3.3
610+
611+
601612
.. function:: ungetmouse(id, x, y, z, bstate)
602613

603614
Push a :const:`KEY_MOUSE` event onto the input queue, associating the given

‎Lib/test/test_curses.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,20 @@ def test_issue6243(stdscr):
264264
curses.ungetch(1025)
265265
stdscr.getkey()
266266

267+
def test_unget_wch(stdscr):
268+
ch = '\xe9'
269+
curses.unget_wch(ch)
270+
read = stdscr.get_wch()
271+
read = chr(read)
272+
if read != ch:
273+
raise AssertionError("%r != %r" % (read, ch))
274+
275+
ch = ord('\xe9')
276+
curses.unget_wch(ch)
277+
read = stdscr.get_wch()
278+
if read != ch:
279+
raise AssertionError("%r != %r" % (read, ch))
280+
267281
def main(stdscr):
268282
curses.savetty()
269283
try:
@@ -272,6 +286,7 @@ def main(stdscr):
272286
test_userptr_without_set(stdscr)
273287
test_resize_term(stdscr)
274288
test_issue6243(stdscr)
289+
test_unget_wch(stdscr)
275290
finally:
276291
curses.resetty()
277292

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ Core and Builtins
271271
Library
272272
-------
273273

274+
- Issue #12567: Add curses.unget_wch() function. Push a character so the next
275+
get_wch() will return it.
276+
274277
- Issue #9561: distutils and packaging now writes egg-info files using UTF-8,
275278
instead of the locale encoding.
276279

‎Modules/_cursesmodule.c‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,6 +2696,71 @@ PyCurses_UngetCh(PyObject *self, PyObject *args)
26962696
return PyCursesCheckERR(ungetch(ch), "ungetch");
26972697
}
26982698

2699+
#ifdef HAVE_NCURSESW
2700+
/* Convert an object to a character (wchar_t):
2701+
2702+
- int
2703+
- str of length 1
2704+
2705+
Return 1 on success, 0 on error. */
2706+
static int
2707+
PyCurses_ConvertToWchar_t(PyObject *obj,
2708+
wchar_t *wch)
2709+
{
2710+
if (PyUnicode_Check(obj)) {
2711+
wchar_t buffer[2];
2712+
if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) {
2713+
PyErr_Format(PyExc_TypeError,
2714+
"expect bytes or str of length 1, or int, "
2715+
"got a str of length %zi",
2716+
PyUnicode_GET_SIZE(obj));
2717+
return 0;
2718+
}
2719+
*wch = buffer[0];
2720+
return 2;
2721+
}
2722+
else if (PyLong_CheckExact(obj)) {
2723+
long value;
2724+
int overflow;
2725+
value = PyLong_AsLongAndOverflow(obj, &overflow);
2726+
if (overflow) {
2727+
PyErr_SetString(PyExc_OverflowError,
2728+
"int doesn't fit in long");
2729+
return 0;
2730+
}
2731+
*wch = (wchar_t)value;
2732+
if ((long)*wch != value) {
2733+
PyErr_Format(PyExc_OverflowError,
2734+
"character doesn't fit in wchar_t");
2735+
return 0;
2736+
}
2737+
return 1;
2738+
}
2739+
else {
2740+
PyErr_Format(PyExc_TypeError,
2741+
"expect bytes or str of length 1, or int, got %s",
2742+
Py_TYPE(obj)->tp_name);
2743+
return 0;
2744+
}
2745+
}
2746+
2747+
static PyObject *
2748+
PyCurses_Unget_Wch(PyObject *self, PyObject *args)
2749+
{
2750+
PyObject *obj;
2751+
wchar_t wch;
2752+
2753+
PyCursesInitialised;
2754+
2755+
if (!PyArg_ParseTuple(args,"O", &obj))
2756+
return NULL;
2757+
2758+
if (!PyCurses_ConvertToWchar_t(obj, &wch))
2759+
return NULL;
2760+
return PyCursesCheckERR(unget_wch(wch), "unget_wch");
2761+
}
2762+
#endif
2763+
26992764
static PyObject *
27002765
PyCurses_Use_Env(PyObject *self, PyObject *args)
27012766
{
@@ -2823,6 +2888,9 @@ static PyMethodDef PyCurses_methods[] = {
28232888
{"typeahead", (PyCFunction)PyCurses_TypeAhead, METH_VARARGS},
28242889
{"unctrl", (PyCFunction)PyCurses_UnCtrl, METH_VARARGS},
28252890
{"ungetch", (PyCFunction)PyCurses_UngetCh, METH_VARARGS},
2891+
#ifdef HAVE_NCURSESW
2892+
{"unget_wch", (PyCFunction)PyCurses_Unget_Wch, METH_VARARGS},
2893+
#endif
28262894
{"use_env", (PyCFunction)PyCurses_Use_Env, METH_VARARGS},
28272895
#ifndef STRICT_SYSV_CURSES
28282896
{"use_default_colors", (PyCFunction)PyCurses_Use_Default_Colors, METH_NOARGS},

0 commit comments

Comments
 (0)