summaryrefslogtreecommitdiffstats
path: root/Modules/_cursesmodule.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@haypocalc.com>2011-09-05 23:53:03 (GMT)
committerVictor Stinner <victor.stinner@haypocalc.com>2011-09-05 23:53:03 (GMT)
commit71e44cb97ffaaa3a777477aa6dcb0b0f29c9eda4 (patch)
treec0d3cf142d5142d2f9a374512af64287add19353 /Modules/_cursesmodule.c
parentd4d8ae5a42ac859c95bdcb2a608c1b972c56efac (diff)
downloadcpython-71e44cb97ffaaa3a777477aa6dcb0b0f29c9eda4.zip
cpython-71e44cb97ffaaa3a777477aa6dcb0b0f29c9eda4.tar.gz
cpython-71e44cb97ffaaa3a777477aa6dcb0b0f29c9eda4.tar.bz2
Issue #12567: Add curses.unget_wch() function
Push a character so the next get_wch() will return it.
Diffstat (limited to 'Modules/_cursesmodule.c')
-rw-r--r--Modules/_cursesmodule.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c
index 6d72024..ef0a66c 100644
--- a/Modules/_cursesmodule.c
+++ b/Modules/_cursesmodule.c
@@ -2696,6 +2696,71 @@ PyCurses_UngetCh(PyObject *self, PyObject *args)
return PyCursesCheckERR(ungetch(ch), "ungetch");
}
+#ifdef HAVE_NCURSESW
+/* Convert an object to a character (wchar_t):
+
+ - int
+ - str of length 1
+
+ Return 1 on success, 0 on error. */
+static int
+PyCurses_ConvertToWchar_t(PyObject *obj,
+ wchar_t *wch)
+{
+ if (PyUnicode_Check(obj)) {
+ wchar_t buffer[2];
+ if (PyUnicode_AsWideChar(obj, buffer, 2) != 1) {
+ PyErr_Format(PyExc_TypeError,
+ "expect bytes or str of length 1, or int, "
+ "got a str of length %zi",
+ PyUnicode_GET_SIZE(obj));
+ return 0;
+ }
+ *wch = buffer[0];
+ return 2;
+ }
+ else if (PyLong_CheckExact(obj)) {
+ long value;
+ int overflow;
+ value = PyLong_AsLongAndOverflow(obj, &overflow);
+ if (overflow) {
+ PyErr_SetString(PyExc_OverflowError,
+ "int doesn't fit in long");
+ return 0;
+ }
+ *wch = (wchar_t)value;
+ if ((long)*wch != value) {
+ PyErr_Format(PyExc_OverflowError,
+ "character doesn't fit in wchar_t");
+ return 0;
+ }
+ return 1;
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "expect bytes or str of length 1, or int, got %s",
+ Py_TYPE(obj)->tp_name);
+ return 0;
+ }
+}
+
+static PyObject *
+PyCurses_Unget_Wch(PyObject *self, PyObject *args)
+{
+ PyObject *obj;
+ wchar_t wch;
+
+ PyCursesInitialised;
+
+ if (!PyArg_ParseTuple(args,"O", &obj))
+ return NULL;
+
+ if (!PyCurses_ConvertToWchar_t(obj, &wch))
+ return NULL;
+ return PyCursesCheckERR(unget_wch(wch), "unget_wch");
+}
+#endif
+
static PyObject *
PyCurses_Use_Env(PyObject *self, PyObject *args)
{
@@ -2823,6 +2888,9 @@ static PyMethodDef PyCurses_methods[] = {
{"typeahead", (PyCFunction)PyCurses_TypeAhead, METH_VARARGS},
{"unctrl", (PyCFunction)PyCurses_UnCtrl, METH_VARARGS},
{"ungetch", (PyCFunction)PyCurses_UngetCh, METH_VARARGS},
+#ifdef HAVE_NCURSESW
+ {"unget_wch", (PyCFunction)PyCurses_Unget_Wch, METH_VARARGS},
+#endif
{"use_env", (PyCFunction)PyCurses_Use_Env, METH_VARARGS},
#ifndef STRICT_SYSV_CURSES
{"use_default_colors", (PyCFunction)PyCurses_Use_Default_Colors, METH_NOARGS},