Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 1 | /********************************************************* |
| 2 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3 | msvcrtmodule.c |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 4 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 5 | A Python interface to the Microsoft Visual C Runtime |
| 6 | Library, providing access to those non-portable, but |
| 7 | still useful routines. |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 8 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 9 | Only ever compiled with an MS compiler, so no attempt |
| 10 | has been made to avoid MS language extensions, etc... |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 11 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 12 | This may only work on NT or 95... |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 13 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 14 | Author: Mark Hammond and Guido van Rossum. |
| 15 | Maintenance: Guido van Rossum. |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 17 | ***********************************************************/ |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 19 | #include "Python.h" |
| 20 | #include "malloc.h" |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 21 | #include <io.h> |
| 22 | #include <conio.h> |
| 23 | #include <sys/locking.h> |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 24 | |
Martin v. Löwis | bcb017f | 2008-11-30 19:28:27 +0000 | [diff] [blame] | 25 | #ifdef _MSC_VER |
Martin v. Löwis | 413fabc | 2010-02-18 09:22:20 +0000 | [diff] [blame] | 26 | #if _MSC_VER >= 1500 && _MSC_VER < 1600 |
Martin v. Löwis | bcb017f | 2008-11-30 19:28:27 +0000 | [diff] [blame] | 27 | #include <crtassem.h> |
| 28 | #endif |
| 29 | #endif |
| 30 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 31 | // Force the malloc heap to clean itself up, and free unused blocks |
| 32 | // back to the OS. (According to the docs, only works on NT.) |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 33 | static PyObject * |
| 34 | msvcrt_heapmin(PyObject *self, PyObject *args) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 35 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 36 | if (!PyArg_ParseTuple(args, ":heapmin")) |
| 37 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 38 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 39 | if (_heapmin() != 0) |
| 40 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 41 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 42 | Py_INCREF(Py_None); |
| 43 | return Py_None; |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Amaury Forgeot d'Arc | c8a2ce7 | 2009-12-29 23:06:17 +0000 | [diff] [blame] | 46 | PyDoc_STRVAR(heapmin_doc, |
| 47 | "heapmin() -> None\n\ |
| 48 | \n\ |
| 49 | Force the malloc() heap to clean itself up and return unused blocks\n\ |
Georg Brandl | b7953f0 | 2009-12-30 19:03:00 +0000 | [diff] [blame] | 50 | to the operating system. On failure, this raises IOError."); |
Amaury Forgeot d'Arc | c8a2ce7 | 2009-12-29 23:06:17 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 52 | // Perform locking operations on a C runtime file descriptor. |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 53 | static PyObject * |
| 54 | msvcrt_locking(PyObject *self, PyObject *args) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 55 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 56 | int fd; |
| 57 | int mode; |
| 58 | long nbytes; |
| 59 | int err; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 60 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 61 | if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes)) |
| 62 | return NULL; |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 63 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 64 | Py_BEGIN_ALLOW_THREADS |
| 65 | err = _locking(fd, mode, nbytes); |
| 66 | Py_END_ALLOW_THREADS |
| 67 | if (err != 0) |
| 68 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 69 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 70 | Py_INCREF(Py_None); |
| 71 | return Py_None; |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 72 | } |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 73 | |
Amaury Forgeot d'Arc | c8a2ce7 | 2009-12-29 23:06:17 +0000 | [diff] [blame] | 74 | PyDoc_STRVAR(locking_doc, |
| 75 | "locking(fd, mode, nbytes) -> None\n\ |
| 76 | \n\ |
| 77 | Lock part of a file based on file descriptor fd from the C runtime.\n\ |
| 78 | Raises IOError on failure. The locked region of the file extends from\n\ |
| 79 | the current file position for nbytes bytes, and may continue beyond\n\ |
| 80 | the end of the file. mode must be one of the LK_* constants listed\n\ |
| 81 | below. Multiple regions in a file may be locked at the same time, but\n\ |
| 82 | may not overlap. Adjacent regions are not merged; they must be unlocked\n\ |
| 83 | individually."); |
| 84 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 85 | // Set the file translation mode for a C runtime file descriptor. |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 86 | static PyObject * |
| 87 | msvcrt_setmode(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 88 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 89 | int fd; |
| 90 | int flags; |
| 91 | if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags)) |
| 92 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 93 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 94 | flags = _setmode(fd, flags); |
| 95 | if (flags == -1) |
| 96 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 97 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 98 | return PyInt_FromLong(flags); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Amaury Forgeot d'Arc | c8a2ce7 | 2009-12-29 23:06:17 +0000 | [diff] [blame] | 101 | PyDoc_STRVAR(setmode_doc, |
| 102 | "setmode(fd, mode) -> Previous mode\n\ |
| 103 | \n\ |
| 104 | Set the line-end translation mode for the file descriptor fd. To set\n\ |
| 105 | it to text mode, flags should be os.O_TEXT; for binary, it should be\n\ |
| 106 | os.O_BINARY."); |
| 107 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 108 | // Convert an OS file handle to a C runtime file descriptor. |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 109 | static PyObject * |
| 110 | msvcrt_open_osfhandle(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 111 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 112 | long handle; |
| 113 | int flags; |
| 114 | int fd; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 115 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 116 | if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags)) |
| 117 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 118 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 119 | fd = _open_osfhandle(handle, flags); |
| 120 | if (fd == -1) |
| 121 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 122 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 123 | return PyInt_FromLong(fd); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Amaury Forgeot d'Arc | c8a2ce7 | 2009-12-29 23:06:17 +0000 | [diff] [blame] | 126 | PyDoc_STRVAR(open_osfhandle_doc, |
| 127 | "open_osfhandle(handle, flags) -> file descriptor\n\ |
| 128 | \n\ |
| 129 | Create a C runtime file descriptor from the file handle handle. The\n\ |
| 130 | flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY,\n\ |
| 131 | and os.O_TEXT. The returned file descriptor may be used as a parameter\n\ |
| 132 | to os.fdopen() to create a file object."); |
| 133 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 134 | // Convert a C runtime file descriptor to an OS file handle. |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 135 | static PyObject * |
| 136 | msvcrt_get_osfhandle(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 137 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 138 | int fd; |
| 139 | Py_intptr_t handle; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 140 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 141 | if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd)) |
| 142 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 143 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 144 | handle = _get_osfhandle(fd); |
| 145 | if (handle == -1) |
| 146 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 147 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 148 | /* technically 'handle' is not a pointer, but a integer as |
| 149 | large as a pointer, Python's *VoidPtr interface is the |
| 150 | most appropriate here */ |
| 151 | return PyLong_FromVoidPtr((void*)handle); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Amaury Forgeot d'Arc | c8a2ce7 | 2009-12-29 23:06:17 +0000 | [diff] [blame] | 154 | PyDoc_STRVAR(get_osfhandle_doc, |
| 155 | "get_osfhandle(fd) -> file handle\n\ |
| 156 | \n\ |
| 157 | Return the file handle for the file descriptor fd. Raises IOError\n\ |
| 158 | if fd is not recognized."); |
| 159 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 160 | /* Console I/O */ |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 161 | |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 162 | static PyObject * |
| 163 | msvcrt_kbhit(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 164 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 165 | int ok; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 166 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 167 | if (!PyArg_ParseTuple(args, ":kbhit")) |
| 168 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 169 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 170 | ok = _kbhit(); |
| 171 | return PyInt_FromLong(ok); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Amaury Forgeot d'Arc | c8a2ce7 | 2009-12-29 23:06:17 +0000 | [diff] [blame] | 174 | PyDoc_STRVAR(kbhit_doc, |
| 175 | "kbhit() -> bool\n\ |
| 176 | \n\ |
| 177 | Return true if a keypress is waiting to be read."); |
| 178 | |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 179 | static PyObject * |
| 180 | msvcrt_getch(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 181 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 182 | int ch; |
| 183 | char s[1]; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 184 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 185 | if (!PyArg_ParseTuple(args, ":getch")) |
| 186 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 187 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 188 | Py_BEGIN_ALLOW_THREADS |
| 189 | ch = _getch(); |
| 190 | Py_END_ALLOW_THREADS |
| 191 | s[0] = ch; |
| 192 | return PyString_FromStringAndSize(s, 1); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Amaury Forgeot d'Arc | c8a2ce7 | 2009-12-29 23:06:17 +0000 | [diff] [blame] | 195 | PyDoc_STRVAR(getch_doc, |
| 196 | "getch() -> key character\n\ |
| 197 | \n\ |
| 198 | Read a keypress and return the resulting character. Nothing is echoed to\n\ |
| 199 | the console. This call will block if a keypress is not already\n\ |
| 200 | available, but will not wait for Enter to be pressed. If the pressed key\n\ |
| 201 | was a special function key, this will return '\\000' or '\\xe0'; the next\n\ |
| 202 | call will return the keycode. The Control-C keypress cannot be read with\n\ |
| 203 | this function."); |
| 204 | |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 205 | #ifdef _WCONIO_DEFINED |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 206 | static PyObject * |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 207 | msvcrt_getwch(PyObject *self, PyObject *args) |
| 208 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 209 | Py_UNICODE ch; |
| 210 | Py_UNICODE u[1]; |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 211 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 212 | if (!PyArg_ParseTuple(args, ":getwch")) |
| 213 | return NULL; |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 214 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 215 | Py_BEGIN_ALLOW_THREADS |
| 216 | ch = _getwch(); |
| 217 | Py_END_ALLOW_THREADS |
| 218 | u[0] = ch; |
| 219 | return PyUnicode_FromUnicode(u, 1); |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 220 | } |
Amaury Forgeot d'Arc | c8a2ce7 | 2009-12-29 23:06:17 +0000 | [diff] [blame] | 221 | |
| 222 | PyDoc_STRVAR(getwch_doc, |
| 223 | "getwch() -> Unicode key character\n\ |
| 224 | \n\ |
| 225 | Wide char variant of getch(), returning a Unicode value."); |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 226 | #endif |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 227 | |
| 228 | static PyObject * |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 229 | msvcrt_getche(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 230 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 231 | int ch; |
| 232 | char s[1]; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 233 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 234 | if (!PyArg_ParseTuple(args, ":getche")) |
| 235 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 236 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 237 | Py_BEGIN_ALLOW_THREADS |
| 238 | ch = _getche(); |
| 239 | Py_END_ALLOW_THREADS |
| 240 | s[0] = ch; |
| 241 | return PyString_FromStringAndSize(s, 1); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Amaury Forgeot d'Arc | c8a2ce7 | 2009-12-29 23:06:17 +0000 | [diff] [blame] | 244 | PyDoc_STRVAR(getche_doc, |
| 245 | "getche() -> key character\n\ |
| 246 | \n\ |
| 247 | Similar to getch(), but the keypress will be echoed if it represents\n\ |
| 248 | a printable character."); |
| 249 | |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 250 | #ifdef _WCONIO_DEFINED |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 251 | static PyObject * |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 252 | msvcrt_getwche(PyObject *self, PyObject *args) |
| 253 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 254 | Py_UNICODE ch; |
| 255 | Py_UNICODE s[1]; |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 256 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 257 | if (!PyArg_ParseTuple(args, ":getwche")) |
| 258 | return NULL; |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 259 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 260 | Py_BEGIN_ALLOW_THREADS |
| 261 | ch = _getwche(); |
| 262 | Py_END_ALLOW_THREADS |
| 263 | s[0] = ch; |
| 264 | return PyUnicode_FromUnicode(s, 1); |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 265 | } |
Amaury Forgeot d'Arc | c8a2ce7 | 2009-12-29 23:06:17 +0000 | [diff] [blame] | 266 | |
| 267 | PyDoc_STRVAR(getwche_doc, |
| 268 | "getwche() -> Unicode key character\n\ |
| 269 | \n\ |
| 270 | Wide char variant of getche(), returning a Unicode value."); |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 271 | #endif |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 272 | |
| 273 | static PyObject * |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 274 | msvcrt_putch(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 275 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 276 | char ch; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 277 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 278 | if (!PyArg_ParseTuple(args, "c:putch", &ch)) |
| 279 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 280 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 281 | _putch(ch); |
| 282 | Py_INCREF(Py_None); |
| 283 | return Py_None; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Amaury Forgeot d'Arc | c8a2ce7 | 2009-12-29 23:06:17 +0000 | [diff] [blame] | 286 | PyDoc_STRVAR(putch_doc, |
| 287 | "putch(char) -> None\n\ |
| 288 | \n\ |
| 289 | Print the character char to the console without buffering."); |
| 290 | |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 291 | #ifdef _WCONIO_DEFINED |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 292 | static PyObject * |
| 293 | msvcrt_putwch(PyObject *self, PyObject *args) |
| 294 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 295 | Py_UNICODE *ch; |
| 296 | int size; |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 297 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 298 | if (!PyArg_ParseTuple(args, "u#:putwch", &ch, &size)) |
| 299 | return NULL; |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 300 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 301 | if (size == 0) { |
| 302 | PyErr_SetString(PyExc_ValueError, |
| 303 | "Expected unicode string of length 1"); |
| 304 | return NULL; |
| 305 | } |
| 306 | _putwch(*ch); |
| 307 | Py_RETURN_NONE; |
Christian Heimes | 61927fc | 2007-12-10 15:39:09 +0000 | [diff] [blame] | 308 | |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 309 | } |
Amaury Forgeot d'Arc | c8a2ce7 | 2009-12-29 23:06:17 +0000 | [diff] [blame] | 310 | |
| 311 | PyDoc_STRVAR(putwch_doc, |
| 312 | "putwch(unicode_char) -> None\n\ |
| 313 | \n\ |
| 314 | Wide char variant of putch(), accepting a Unicode value."); |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 315 | #endif |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 316 | |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 317 | static PyObject * |
| 318 | msvcrt_ungetch(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 319 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 320 | char ch; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 321 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 322 | if (!PyArg_ParseTuple(args, "c:ungetch", &ch)) |
| 323 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 324 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 325 | if (_ungetch(ch) == EOF) |
| 326 | return PyErr_SetFromErrno(PyExc_IOError); |
| 327 | Py_INCREF(Py_None); |
| 328 | return Py_None; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Amaury Forgeot d'Arc | c8a2ce7 | 2009-12-29 23:06:17 +0000 | [diff] [blame] | 331 | PyDoc_STRVAR(ungetch_doc, |
| 332 | "ungetch(char) -> None\n\ |
| 333 | \n\ |
| 334 | Cause the character char to be \"pushed back\" into the console buffer;\n\ |
| 335 | it will be the next character read by getch() or getche()."); |
| 336 | |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 337 | #ifdef _WCONIO_DEFINED |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 338 | static PyObject * |
| 339 | msvcrt_ungetwch(PyObject *self, PyObject *args) |
| 340 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 341 | Py_UNICODE ch; |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 342 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 343 | if (!PyArg_ParseTuple(args, "u:ungetwch", &ch)) |
| 344 | return NULL; |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 345 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 346 | if (_ungetch(ch) == EOF) |
| 347 | return PyErr_SetFromErrno(PyExc_IOError); |
| 348 | Py_INCREF(Py_None); |
| 349 | return Py_None; |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 350 | } |
Amaury Forgeot d'Arc | c8a2ce7 | 2009-12-29 23:06:17 +0000 | [diff] [blame] | 351 | |
| 352 | PyDoc_STRVAR(ungetwch_doc, |
| 353 | "ungetwch(unicode_char) -> None\n\ |
| 354 | \n\ |
| 355 | Wide char variant of ungetch(), accepting a Unicode value."); |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 356 | #endif |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 357 | |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 358 | static void |
| 359 | insertint(PyObject *d, char *name, int value) |
| 360 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 361 | PyObject *v = PyInt_FromLong((long) value); |
| 362 | if (v == NULL) { |
| 363 | /* Don't bother reporting this error */ |
| 364 | PyErr_Clear(); |
| 365 | } |
| 366 | else { |
| 367 | PyDict_SetItemString(d, name, v); |
| 368 | Py_DECREF(v); |
| 369 | } |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 373 | /* List of functions exported by this module */ |
| 374 | static struct PyMethodDef msvcrt_functions[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 375 | {"heapmin", msvcrt_heapmin, METH_VARARGS, heapmin_doc}, |
| 376 | {"locking", msvcrt_locking, METH_VARARGS, locking_doc}, |
| 377 | {"setmode", msvcrt_setmode, METH_VARARGS, setmode_doc}, |
| 378 | {"open_osfhandle", msvcrt_open_osfhandle, METH_VARARGS, open_osfhandle_doc}, |
| 379 | {"get_osfhandle", msvcrt_get_osfhandle, METH_VARARGS, get_osfhandle_doc}, |
| 380 | {"kbhit", msvcrt_kbhit, METH_VARARGS, kbhit_doc}, |
| 381 | {"getch", msvcrt_getch, METH_VARARGS, getch_doc}, |
| 382 | {"getche", msvcrt_getche, METH_VARARGS, getche_doc}, |
| 383 | {"putch", msvcrt_putch, METH_VARARGS, putch_doc}, |
| 384 | {"ungetch", msvcrt_ungetch, METH_VARARGS, ungetch_doc}, |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 385 | #ifdef _WCONIO_DEFINED |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 386 | {"getwch", msvcrt_getwch, METH_VARARGS, getwch_doc}, |
| 387 | {"getwche", msvcrt_getwche, METH_VARARGS, getwche_doc}, |
| 388 | {"putwch", msvcrt_putwch, METH_VARARGS, putwch_doc}, |
| 389 | {"ungetwch", msvcrt_ungetwch, METH_VARARGS, ungetwch_doc}, |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 390 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 391 | {NULL, NULL} |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 392 | }; |
| 393 | |
Thomas Heller | a18331d | 2004-07-28 20:02:52 +0000 | [diff] [blame] | 394 | PyMODINIT_FUNC |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 395 | initmsvcrt(void) |
| 396 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 397 | int st; |
| 398 | PyObject *d; |
| 399 | PyObject *m = Py_InitModule("msvcrt", msvcrt_functions); |
| 400 | if (m == NULL) |
| 401 | return; |
| 402 | d = PyModule_GetDict(m); |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 403 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 404 | /* constants for the locking() function's mode argument */ |
| 405 | insertint(d, "LK_LOCK", _LK_LOCK); |
| 406 | insertint(d, "LK_NBLCK", _LK_NBLCK); |
| 407 | insertint(d, "LK_NBRLCK", _LK_NBRLCK); |
| 408 | insertint(d, "LK_RLCK", _LK_RLCK); |
| 409 | insertint(d, "LK_UNLCK", _LK_UNLCK); |
Martin v. Löwis | bcb017f | 2008-11-30 19:28:27 +0000 | [diff] [blame] | 410 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 411 | /* constants for the crt versions */ |
Martin v. Löwis | bcb017f | 2008-11-30 19:28:27 +0000 | [diff] [blame] | 412 | #ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 413 | st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN", |
| 414 | _VC_ASSEMBLY_PUBLICKEYTOKEN); |
| 415 | if (st < 0)return; |
Martin v. Löwis | bcb017f | 2008-11-30 19:28:27 +0000 | [diff] [blame] | 416 | #endif |
| 417 | #ifdef _CRT_ASSEMBLY_VERSION |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 418 | st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION", |
| 419 | _CRT_ASSEMBLY_VERSION); |
| 420 | if (st < 0)return; |
Martin v. Löwis | bcb017f | 2008-11-30 19:28:27 +0000 | [diff] [blame] | 421 | #endif |
| 422 | #ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 423 | st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX", |
| 424 | __LIBRARIES_ASSEMBLY_NAME_PREFIX); |
| 425 | if (st < 0)return; |
Martin v. Löwis | bcb017f | 2008-11-30 19:28:27 +0000 | [diff] [blame] | 426 | #endif |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 427 | } |