Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 1 | /********************************************************* |
| 2 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3 | msvcrtmodule.c |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 4 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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 | f95a1b3 | 2010-05-09 15:52:27 +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> |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 24 | #include <crtdbg.h> |
| 25 | #include <windows.h> |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 26 | |
Benjamin Peterson | 4469d0c | 2008-11-30 22:46:23 +0000 | [diff] [blame] | 27 | #ifdef _MSC_VER |
Martin v. Löwis | 3afc62e | 2010-02-18 16:27:43 +0000 | [diff] [blame] | 28 | #if _MSC_VER >= 1500 && _MSC_VER < 1600 |
Benjamin Peterson | 4469d0c | 2008-11-30 22:46:23 +0000 | [diff] [blame] | 29 | #include <crtassem.h> |
| 30 | #endif |
| 31 | #endif |
| 32 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 33 | // Force the malloc heap to clean itself up, and free unused blocks |
| 34 | // back to the OS. (According to the docs, only works on NT.) |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 35 | static PyObject * |
| 36 | msvcrt_heapmin(PyObject *self, PyObject *args) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 37 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 38 | if (!PyArg_ParseTuple(args, ":heapmin")) |
| 39 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 40 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 41 | if (_heapmin() != 0) |
| 42 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 43 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 44 | Py_INCREF(Py_None); |
| 45 | return Py_None; |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 48 | PyDoc_STRVAR(heapmin_doc, |
| 49 | "heapmin() -> None\n\ |
| 50 | \n\ |
| 51 | Force the malloc() heap to clean itself up and return unused blocks\n\ |
| 52 | to the operating system. On failure, this raises IOError."); |
| 53 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 54 | // Perform locking operations on a C runtime file descriptor. |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 55 | static PyObject * |
| 56 | msvcrt_locking(PyObject *self, PyObject *args) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 57 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | int fd; |
| 59 | int mode; |
| 60 | long nbytes; |
| 61 | int err; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 62 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 63 | if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes)) |
| 64 | return NULL; |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 65 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | Py_BEGIN_ALLOW_THREADS |
| 67 | err = _locking(fd, mode, nbytes); |
| 68 | Py_END_ALLOW_THREADS |
| 69 | if (err != 0) |
| 70 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 71 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | Py_INCREF(Py_None); |
| 73 | return Py_None; |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 74 | } |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 75 | |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 76 | PyDoc_STRVAR(locking_doc, |
| 77 | "locking(fd, mode, nbytes) -> None\n\ |
| 78 | \n\ |
| 79 | Lock part of a file based on file descriptor fd from the C runtime.\n\ |
| 80 | Raises IOError on failure. The locked region of the file extends from\n\ |
| 81 | the current file position for nbytes bytes, and may continue beyond\n\ |
| 82 | the end of the file. mode must be one of the LK_* constants listed\n\ |
| 83 | below. Multiple regions in a file may be locked at the same time, but\n\ |
| 84 | may not overlap. Adjacent regions are not merged; they must be unlocked\n\ |
| 85 | individually."); |
| 86 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 87 | // Set the file translation mode for a C runtime file descriptor. |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 88 | static PyObject * |
| 89 | msvcrt_setmode(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 90 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | int fd; |
| 92 | int flags; |
| 93 | if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags)) |
| 94 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 95 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | flags = _setmode(fd, flags); |
| 97 | if (flags == -1) |
| 98 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 99 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | return PyLong_FromLong(flags); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 103 | PyDoc_STRVAR(setmode_doc, |
| 104 | "setmode(fd, mode) -> Previous mode\n\ |
| 105 | \n\ |
| 106 | Set the line-end translation mode for the file descriptor fd. To set\n\ |
| 107 | it to text mode, flags should be os.O_TEXT; for binary, it should be\n\ |
| 108 | os.O_BINARY."); |
| 109 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 110 | // Convert an OS file handle to a C runtime file descriptor. |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 111 | static PyObject * |
| 112 | msvcrt_open_osfhandle(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 113 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | long handle; |
| 115 | int flags; |
| 116 | int fd; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags)) |
| 119 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | fd = _open_osfhandle(handle, flags); |
| 122 | if (fd == -1) |
| 123 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 124 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 125 | return PyLong_FromLong(fd); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 128 | PyDoc_STRVAR(open_osfhandle_doc, |
| 129 | "open_osfhandle(handle, flags) -> file descriptor\n\ |
| 130 | \n\ |
| 131 | Create a C runtime file descriptor from the file handle handle. The\n\ |
| 132 | flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY,\n\ |
| 133 | and os.O_TEXT. The returned file descriptor may be used as a parameter\n\ |
| 134 | to os.fdopen() to create a file object."); |
| 135 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 136 | // Convert a C runtime file descriptor to an OS file handle. |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 137 | static PyObject * |
| 138 | msvcrt_get_osfhandle(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 139 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | int fd; |
| 141 | Py_intptr_t handle; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd)) |
| 144 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 145 | |
Antoine Pitrou | 0049249 | 2010-09-04 20:53:29 +0000 | [diff] [blame^] | 146 | if (!_PyVerify_fd(fd)) |
| 147 | return PyErr_SetFromErrno(PyExc_IOError); |
| 148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | handle = _get_osfhandle(fd); |
| 150 | if (handle == -1) |
| 151 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | /* technically 'handle' is not a pointer, but a integer as |
| 154 | large as a pointer, Python's *VoidPtr interface is the |
| 155 | most appropriate here */ |
| 156 | return PyLong_FromVoidPtr((void*)handle); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 159 | PyDoc_STRVAR(get_osfhandle_doc, |
| 160 | "get_osfhandle(fd) -> file handle\n\ |
| 161 | \n\ |
| 162 | Return the file handle for the file descriptor fd. Raises IOError\n\ |
| 163 | if fd is not recognized."); |
| 164 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 165 | /* Console I/O */ |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 166 | |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 167 | static PyObject * |
| 168 | msvcrt_kbhit(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 169 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | int ok; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | if (!PyArg_ParseTuple(args, ":kbhit")) |
| 173 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | ok = _kbhit(); |
| 176 | return PyLong_FromLong(ok); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 179 | PyDoc_STRVAR(kbhit_doc, |
| 180 | "kbhit() -> bool\n\ |
| 181 | \n\ |
| 182 | Return true if a keypress is waiting to be read."); |
| 183 | |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 184 | static PyObject * |
| 185 | msvcrt_getch(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 186 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 187 | int ch; |
| 188 | char s[1]; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | if (!PyArg_ParseTuple(args, ":getch")) |
| 191 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | Py_BEGIN_ALLOW_THREADS |
| 194 | ch = _getch(); |
| 195 | Py_END_ALLOW_THREADS |
| 196 | s[0] = ch; |
| 197 | return PyBytes_FromStringAndSize(s, 1); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 200 | PyDoc_STRVAR(getch_doc, |
| 201 | "getch() -> key character\n\ |
| 202 | \n\ |
Brian Curtin | 8790a07 | 2010-08-24 05:20:30 +0000 | [diff] [blame] | 203 | Read a keypress and return the resulting character as a byte string.\n\ |
| 204 | Nothing is echoed to the console. This call will block if a keypress is\n\ |
| 205 | not already available, but will not wait for Enter to be pressed. If the\n\ |
| 206 | pressed key was a special function key, this will return '\\000' or\n\ |
| 207 | '\\xe0'; the next call will return the keycode. The Control-C keypress\n\ |
| 208 | cannot be read with this function."); |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 209 | |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 210 | #ifdef _WCONIO_DEFINED |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 211 | static PyObject * |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 212 | msvcrt_getwch(PyObject *self, PyObject *args) |
| 213 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | Py_UNICODE ch; |
| 215 | Py_UNICODE u[1]; |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 217 | if (!PyArg_ParseTuple(args, ":getwch")) |
| 218 | return NULL; |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | Py_BEGIN_ALLOW_THREADS |
| 221 | ch = _getwch(); |
| 222 | Py_END_ALLOW_THREADS |
| 223 | u[0] = ch; |
| 224 | return PyUnicode_FromUnicode(u, 1); |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 225 | } |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 226 | |
| 227 | PyDoc_STRVAR(getwch_doc, |
| 228 | "getwch() -> Unicode key character\n\ |
| 229 | \n\ |
| 230 | Wide char variant of getch(), returning a Unicode value."); |
Christian Heimes | c36625b | 2008-01-04 13:33:00 +0000 | [diff] [blame] | 231 | #endif |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 232 | |
| 233 | static PyObject * |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 234 | msvcrt_getche(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 235 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | int ch; |
| 237 | char s[1]; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | if (!PyArg_ParseTuple(args, ":getche")) |
| 240 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | Py_BEGIN_ALLOW_THREADS |
| 243 | ch = _getche(); |
| 244 | Py_END_ALLOW_THREADS |
| 245 | s[0] = ch; |
| 246 | return PyBytes_FromStringAndSize(s, 1); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 249 | PyDoc_STRVAR(getche_doc, |
| 250 | "getche() -> key character\n\ |
| 251 | \n\ |
| 252 | Similar to getch(), but the keypress will be echoed if it represents\n\ |
| 253 | a printable character."); |
| 254 | |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 255 | #ifdef _WCONIO_DEFINED |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 256 | static PyObject * |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 257 | msvcrt_getwche(PyObject *self, PyObject *args) |
| 258 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | Py_UNICODE ch; |
| 260 | Py_UNICODE s[1]; |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 261 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 262 | if (!PyArg_ParseTuple(args, ":getwche")) |
| 263 | return NULL; |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | Py_BEGIN_ALLOW_THREADS |
| 266 | ch = _getwche(); |
| 267 | Py_END_ALLOW_THREADS |
| 268 | s[0] = ch; |
| 269 | return PyUnicode_FromUnicode(s, 1); |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 270 | } |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 271 | |
| 272 | PyDoc_STRVAR(getwche_doc, |
| 273 | "getwche() -> Unicode key character\n\ |
| 274 | \n\ |
| 275 | Wide char variant of getche(), returning a Unicode value."); |
Christian Heimes | c36625b | 2008-01-04 13:33:00 +0000 | [diff] [blame] | 276 | #endif |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 277 | |
| 278 | static PyObject * |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 279 | msvcrt_putch(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 280 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 281 | char ch; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | if (!PyArg_ParseTuple(args, "c:putch", &ch)) |
| 284 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | _putch(ch); |
| 287 | Py_INCREF(Py_None); |
| 288 | return Py_None; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 291 | PyDoc_STRVAR(putch_doc, |
| 292 | "putch(char) -> None\n\ |
| 293 | \n\ |
Brian Curtin | 8790a07 | 2010-08-24 05:20:30 +0000 | [diff] [blame] | 294 | Print the byte string char to the console without buffering."); |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 295 | |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 296 | #ifdef _WCONIO_DEFINED |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 297 | static PyObject * |
| 298 | msvcrt_putwch(PyObject *self, PyObject *args) |
| 299 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 300 | int ch; |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | if (!PyArg_ParseTuple(args, "C:putwch", &ch)) |
| 303 | return NULL; |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 305 | _putwch(ch); |
| 306 | Py_RETURN_NONE; |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 307 | |
| 308 | } |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 309 | |
| 310 | PyDoc_STRVAR(putwch_doc, |
| 311 | "putwch(unicode_char) -> None\n\ |
| 312 | \n\ |
| 313 | Wide char variant of putch(), accepting a Unicode value."); |
Christian Heimes | c36625b | 2008-01-04 13:33:00 +0000 | [diff] [blame] | 314 | #endif |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 315 | |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 316 | static PyObject * |
| 317 | msvcrt_ungetch(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 318 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | char ch; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | if (!PyArg_ParseTuple(args, "c:ungetch", &ch)) |
| 322 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | if (_ungetch(ch) == EOF) |
| 325 | return PyErr_SetFromErrno(PyExc_IOError); |
| 326 | Py_INCREF(Py_None); |
| 327 | return Py_None; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 330 | PyDoc_STRVAR(ungetch_doc, |
| 331 | "ungetch(char) -> None\n\ |
| 332 | \n\ |
Brian Curtin | 8790a07 | 2010-08-24 05:20:30 +0000 | [diff] [blame] | 333 | Cause the byte string char to be \"pushed back\" into the\n\ |
| 334 | console buffer; it will be the next character read by\n\ |
| 335 | getch() or getche()."); |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 336 | |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 337 | #ifdef _WCONIO_DEFINED |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 338 | static PyObject * |
| 339 | msvcrt_ungetwch(PyObject *self, PyObject *args) |
| 340 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | int ch; |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | if (!PyArg_ParseTuple(args, "C:ungetwch", &ch)) |
| 344 | return NULL; |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 346 | if (_ungetwch(ch) == WEOF) |
| 347 | return PyErr_SetFromErrno(PyExc_IOError); |
| 348 | Py_INCREF(Py_None); |
| 349 | return Py_None; |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 350 | } |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +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."); |
Christian Heimes | c36625b | 2008-01-04 13:33:00 +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 | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | PyObject *v = PyLong_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 | |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 372 | #ifdef _DEBUG |
| 373 | |
| 374 | static PyObject* |
| 375 | msvcrt_setreportfile(PyObject *self, PyObject *args) |
| 376 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | int type, file; |
| 378 | _HFILE res; |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | if (!PyArg_ParseTuple(args, "ii", &type, &file)) |
| 381 | return NULL; |
| 382 | res = _CrtSetReportFile(type, (_HFILE)file); |
| 383 | return PyLong_FromLong((long)res); |
| 384 | Py_INCREF(Py_None); |
| 385 | return Py_None; |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | static PyObject* |
| 389 | msvcrt_setreportmode(PyObject *self, PyObject *args) |
| 390 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 391 | int type, mode; |
| 392 | int res; |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 394 | if (!PyArg_ParseTuple(args, "ii", &type, &mode)) |
| 395 | return NULL; |
| 396 | res = _CrtSetReportMode(type, mode); |
| 397 | if (res == -1) |
| 398 | return PyErr_SetFromErrno(PyExc_IOError); |
| 399 | return PyLong_FromLong(res); |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static PyObject* |
| 403 | msvcrt_seterrormode(PyObject *self, PyObject *args) |
| 404 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 405 | int mode, res; |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 406 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 407 | if (!PyArg_ParseTuple(args, "i", &mode)) |
| 408 | return NULL; |
| 409 | res = _set_error_mode(mode); |
| 410 | return PyLong_FromLong(res); |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | #endif |
| 414 | |
| 415 | static PyObject* |
| 416 | seterrormode(PyObject *self, PyObject *args) |
| 417 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | unsigned int mode, res; |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | if (!PyArg_ParseTuple(args, "I", &mode)) |
| 421 | return NULL; |
| 422 | res = SetErrorMode(mode); |
| 423 | return PyLong_FromUnsignedLong(res); |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 426 | |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 427 | /* List of functions exported by this module */ |
| 428 | static struct PyMethodDef msvcrt_functions[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 429 | {"heapmin", msvcrt_heapmin, METH_VARARGS, heapmin_doc}, |
| 430 | {"locking", msvcrt_locking, METH_VARARGS, locking_doc}, |
| 431 | {"setmode", msvcrt_setmode, METH_VARARGS, setmode_doc}, |
| 432 | {"open_osfhandle", msvcrt_open_osfhandle, METH_VARARGS, open_osfhandle_doc}, |
| 433 | {"get_osfhandle", msvcrt_get_osfhandle, METH_VARARGS, get_osfhandle_doc}, |
| 434 | {"kbhit", msvcrt_kbhit, METH_VARARGS, kbhit_doc}, |
| 435 | {"getch", msvcrt_getch, METH_VARARGS, getch_doc}, |
| 436 | {"getche", msvcrt_getche, METH_VARARGS, getche_doc}, |
| 437 | {"putch", msvcrt_putch, METH_VARARGS, putch_doc}, |
| 438 | {"ungetch", msvcrt_ungetch, METH_VARARGS, ungetch_doc}, |
| 439 | {"SetErrorMode", seterrormode, METH_VARARGS}, |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 440 | #ifdef _DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 441 | {"CrtSetReportFile", msvcrt_setreportfile, METH_VARARGS}, |
| 442 | {"CrtSetReportMode", msvcrt_setreportmode, METH_VARARGS}, |
| 443 | {"set_error_mode", msvcrt_seterrormode, METH_VARARGS}, |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 444 | #endif |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 445 | #ifdef _WCONIO_DEFINED |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | {"getwch", msvcrt_getwch, METH_VARARGS, getwch_doc}, |
| 447 | {"getwche", msvcrt_getwche, METH_VARARGS, getwche_doc}, |
| 448 | {"putwch", msvcrt_putwch, METH_VARARGS, putwch_doc}, |
| 449 | {"ungetwch", msvcrt_ungetwch, METH_VARARGS, ungetwch_doc}, |
Christian Heimes | c36625b | 2008-01-04 13:33:00 +0000 | [diff] [blame] | 450 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 451 | {NULL, NULL} |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 452 | }; |
| 453 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 454 | |
| 455 | static struct PyModuleDef msvcrtmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | PyModuleDef_HEAD_INIT, |
| 457 | "msvcrt", |
| 458 | NULL, |
| 459 | -1, |
| 460 | msvcrt_functions, |
| 461 | NULL, |
| 462 | NULL, |
| 463 | NULL, |
| 464 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 465 | }; |
| 466 | |
Thomas Heller | a18331d | 2004-07-28 20:02:52 +0000 | [diff] [blame] | 467 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 468 | PyInit_msvcrt(void) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 469 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | int st; |
| 471 | PyObject *d; |
| 472 | PyObject *m = PyModule_Create(&msvcrtmodule); |
| 473 | if (m == NULL) |
| 474 | return NULL; |
| 475 | d = PyModule_GetDict(m); |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 476 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | /* constants for the locking() function's mode argument */ |
| 478 | insertint(d, "LK_LOCK", _LK_LOCK); |
| 479 | insertint(d, "LK_NBLCK", _LK_NBLCK); |
| 480 | insertint(d, "LK_NBRLCK", _LK_NBRLCK); |
| 481 | insertint(d, "LK_RLCK", _LK_RLCK); |
| 482 | insertint(d, "LK_UNLCK", _LK_UNLCK); |
| 483 | insertint(d, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS); |
| 484 | insertint(d, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT); |
| 485 | insertint(d, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX); |
| 486 | insertint(d, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX); |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 487 | #ifdef _DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | insertint(d, "CRT_WARN", _CRT_WARN); |
| 489 | insertint(d, "CRT_ERROR", _CRT_ERROR); |
| 490 | insertint(d, "CRT_ASSERT", _CRT_ASSERT); |
| 491 | insertint(d, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG); |
| 492 | insertint(d, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE); |
| 493 | insertint(d, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW); |
| 494 | insertint(d, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE); |
| 495 | insertint(d, "CRTDBG_FILE_STDERR", (int)_CRTDBG_FILE_STDERR); |
| 496 | insertint(d, "CRTDBG_FILE_STDOUT", (int)_CRTDBG_FILE_STDOUT); |
| 497 | insertint(d, "CRTDBG_REPORT_FILE", (int)_CRTDBG_REPORT_FILE); |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 498 | #endif |
Benjamin Peterson | 4469d0c | 2008-11-30 22:46:23 +0000 | [diff] [blame] | 499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | /* constants for the crt versions */ |
Benjamin Peterson | 4469d0c | 2008-11-30 22:46:23 +0000 | [diff] [blame] | 501 | #ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN", |
| 503 | _VC_ASSEMBLY_PUBLICKEYTOKEN); |
| 504 | if (st < 0) return NULL; |
Benjamin Peterson | 4469d0c | 2008-11-30 22:46:23 +0000 | [diff] [blame] | 505 | #endif |
| 506 | #ifdef _CRT_ASSEMBLY_VERSION |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION", |
| 508 | _CRT_ASSEMBLY_VERSION); |
| 509 | if (st < 0) return NULL; |
Benjamin Peterson | 4469d0c | 2008-11-30 22:46:23 +0000 | [diff] [blame] | 510 | #endif |
| 511 | #ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX", |
| 513 | __LIBRARIES_ASSEMBLY_NAME_PREFIX); |
| 514 | if (st < 0) return NULL; |
Benjamin Peterson | 4469d0c | 2008-11-30 22:46:23 +0000 | [diff] [blame] | 515 | #endif |
| 516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | return m; |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 518 | } |