Commit 7c7f6afa authored by Christian Heimes's avatar Christian Heimes

Added wide char api variants of getch and putch to msvcrt module. The wide...

Added wide char api variants of getch and putch to msvcrt module. The wide char methods are required to fix #1578 in py3k. I figured out that they might be useful in 2.6, too.
parent e6daafb4
...@@ -16,6 +16,10 @@ this in the implementation of the :func:`getpass` function. ...@@ -16,6 +16,10 @@ this in the implementation of the :func:`getpass` function.
Further documentation on these functions can be found in the Platform API Further documentation on these functions can be found in the Platform API
documentation. documentation.
The module implements both the normal and wide char variants of the console I/O
api. The normal API deals only with ASCII characters and is of limited use
for internationalized applications. The wide char API should be used where
ever possible
.. _msvcrt-files: .. _msvcrt-files:
...@@ -95,23 +99,51 @@ Console I/O ...@@ -95,23 +99,51 @@ Console I/O
function. function.
.. function:: getwch()
Wide char variant of `func:getch`, returns unicode.
..versionadded:: 2.6
.. function:: getche() .. function:: getche()
Similar to :func:`getch`, but the keypress will be echoed if it represents a Similar to :func:`getch`, but the keypress will be echoed if it represents a
printable character. printable character.
.. function:: getwche()
Wide char variant of `func:getche`, returns unicode.
..versionadded:: 2.6
.. function:: putch(char) .. function:: putch(char)
Print the character *char* to the console without buffering. Print the character *char* to the console without buffering.
.. function:: putwch(unicode_char)
Wide char variant of `func:putch`, accepts unicode.
..versionadded:: 2.6
.. function:: ungetch(char) .. function:: ungetch(char)
Cause the character *char* to be "pushed back" into the console buffer; it will Cause the character *char* to be "pushed back" into the console buffer; it will
be the next character read by :func:`getch` or :func:`getche`. be the next character read by :func:`getch` or :func:`getche`.
.. function:: ungetwch(unicode_char)
Wide char variant of `func:ungetch`, accepts unicode.
..versionadded:: 2.6
.. _msvcrt-other: .. _msvcrt-other:
Other Functions Other Functions
......
...@@ -877,6 +877,9 @@ Library ...@@ -877,6 +877,9 @@ Library
Extension Modules Extension Modules
----------------- -----------------
- Added wide char functions to msvcrt module: getwch, getwche, putwch and
ungetwch. The functions accept or return unicode.
- os.access now returns True on Windows for any existing directory. - os.access now returns True on Windows for any existing directory.
- Added warnpy3k function to the warnings module. - Added warnpy3k function to the warnings module.
......
...@@ -143,6 +143,22 @@ msvcrt_getch(PyObject *self, PyObject *args) ...@@ -143,6 +143,22 @@ msvcrt_getch(PyObject *self, PyObject *args)
return PyString_FromStringAndSize(s, 1); return PyString_FromStringAndSize(s, 1);
} }
static PyObject *
msvcrt_getwch(PyObject *self, PyObject *args)
{
Py_UNICODE ch;
Py_UNICODE u[1];
if (!PyArg_ParseTuple(args, ":getwch"))
return NULL;
Py_BEGIN_ALLOW_THREADS
ch = _getwch();
Py_END_ALLOW_THREADS
u[0] = ch;
return PyUnicode_FromUnicode(u, 1);
}
static PyObject * static PyObject *
msvcrt_getche(PyObject *self, PyObject *args) msvcrt_getche(PyObject *self, PyObject *args)
{ {
...@@ -159,6 +175,22 @@ msvcrt_getche(PyObject *self, PyObject *args) ...@@ -159,6 +175,22 @@ msvcrt_getche(PyObject *self, PyObject *args)
return PyString_FromStringAndSize(s, 1); return PyString_FromStringAndSize(s, 1);
} }
static PyObject *
msvcrt_getwche(PyObject *self, PyObject *args)
{
Py_UNICODE ch;
Py_UNICODE s[1];
if (!PyArg_ParseTuple(args, ":getwche"))
return NULL;
Py_BEGIN_ALLOW_THREADS
ch = _getwche();
Py_END_ALLOW_THREADS
s[0] = ch;
return PyUnicode_FromUnicode(s, 1);
}
static PyObject * static PyObject *
msvcrt_putch(PyObject *self, PyObject *args) msvcrt_putch(PyObject *self, PyObject *args)
{ {
...@@ -172,6 +204,25 @@ msvcrt_putch(PyObject *self, PyObject *args) ...@@ -172,6 +204,25 @@ msvcrt_putch(PyObject *self, PyObject *args)
return Py_None; return Py_None;
} }
static PyObject *
msvcrt_putwch(PyObject *self, PyObject *args)
{
Py_UNICODE *ch;
int size;
if (!PyArg_ParseTuple(args, "u#:putwch", &ch, &size))
return NULL;
if (size == 1)
_putwch(*ch);
Py_RETURN_NONE;
else {
PyErr_SetString(PyExc_ValueError,
"Expected unicode of length 1");
}
}
static PyObject * static PyObject *
msvcrt_ungetch(PyObject *self, PyObject *args) msvcrt_ungetch(PyObject *self, PyObject *args)
{ {
...@@ -186,6 +237,19 @@ msvcrt_ungetch(PyObject *self, PyObject *args) ...@@ -186,6 +237,19 @@ msvcrt_ungetch(PyObject *self, PyObject *args)
return Py_None; return Py_None;
} }
static PyObject *
msvcrt_ungetwch(PyObject *self, PyObject *args)
{
Py_UNICODE ch;
if (!PyArg_ParseTuple(args, "u:ungetwch", &ch))
return NULL;
if (_ungetch(ch) == EOF)
return PyErr_SetFromErrno(PyExc_IOError);
Py_INCREF(Py_None);
return Py_None;
}
static void static void
insertint(PyObject *d, char *name, int value) insertint(PyObject *d, char *name, int value)
...@@ -214,6 +278,11 @@ static struct PyMethodDef msvcrt_functions[] = { ...@@ -214,6 +278,11 @@ static struct PyMethodDef msvcrt_functions[] = {
{"getche", msvcrt_getche, METH_VARARGS}, {"getche", msvcrt_getche, METH_VARARGS},
{"putch", msvcrt_putch, METH_VARARGS}, {"putch", msvcrt_putch, METH_VARARGS},
{"ungetch", msvcrt_ungetch, METH_VARARGS}, {"ungetch", msvcrt_ungetch, METH_VARARGS},
{"getwch", msvcrt_getwch, METH_VARARGS},
{"getwche", msvcrt_getwche, METH_VARARGS},
{"putwch", msvcrt_putwch, METH_VARARGS},
{"ungetwch", msvcrt_ungetwch, METH_VARARGS},
{NULL, NULL} {NULL, NULL}
}; };
......
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