Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 1 | /********************************************************* |
| 2 | |
| 3 | msvcrtmodule.c |
| 4 | |
| 5 | A Python interface to the Microsoft Visual C Runtime |
| 6 | Library, providing access to those non-portable, but |
| 7 | still useful routines. |
| 8 | |
| 9 | Only ever compiled with an MS compiler, so no attempt |
| 10 | has been made to avoid MS language extensions, etc... |
| 11 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 12 | This may only work on NT or 95... |
| 13 | |
| 14 | Author: Mark Hammond and Guido van Rossum. |
| 15 | Maintenance: Guido van Rossum. |
| 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 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 25 | // Force the malloc heap to clean itself up, and free unused blocks |
| 26 | // back to the OS. (According to the docs, only works on NT.) |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 27 | static PyObject * |
| 28 | msvcrt_heapmin(PyObject *self, PyObject *args) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 29 | { |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 30 | if (!PyArg_ParseTuple(args, ":heapmin")) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 31 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 32 | |
| 33 | if (_heapmin() != 0) |
| 34 | return PyErr_SetFromErrno(PyExc_IOError); |
| 35 | |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 36 | Py_INCREF(Py_None); |
| 37 | return Py_None; |
| 38 | } |
| 39 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 40 | // Perform locking operations on a C runtime file descriptor. |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 41 | static PyObject * |
| 42 | msvcrt_locking(PyObject *self, PyObject *args) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 43 | { |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 44 | int fd; |
| 45 | int mode; |
| 46 | long nbytes; |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 47 | int err; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 48 | |
| 49 | if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes)) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 50 | return NULL; |
| 51 | |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 52 | Py_BEGIN_ALLOW_THREADS |
| 53 | err = _locking(fd, mode, nbytes); |
| 54 | Py_END_ALLOW_THREADS |
| 55 | if (err != 0) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 56 | return PyErr_SetFromErrno(PyExc_IOError); |
| 57 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 58 | Py_INCREF(Py_None); |
| 59 | return Py_None; |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 60 | } |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 61 | |
| 62 | // Set the file translation mode for a C runtime file descriptor. |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 63 | static PyObject * |
| 64 | msvcrt_setmode(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 65 | { |
| 66 | int fd; |
| 67 | int flags; |
| 68 | if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags)) |
| 69 | return NULL; |
| 70 | |
| 71 | flags = _setmode(fd, flags); |
| 72 | if (flags == -1) |
| 73 | return PyErr_SetFromErrno(PyExc_IOError); |
| 74 | |
| 75 | return PyInt_FromLong(flags); |
| 76 | } |
| 77 | |
| 78 | // Convert an OS file handle to a C runtime file descriptor. |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 79 | static PyObject * |
| 80 | msvcrt_open_osfhandle(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 81 | { |
| 82 | long handle; |
| 83 | int flags; |
| 84 | int fd; |
| 85 | |
| 86 | if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags)) |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 87 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 88 | |
| 89 | fd = _open_osfhandle(handle, flags); |
| 90 | if (fd == -1) |
| 91 | return PyErr_SetFromErrno(PyExc_IOError); |
| 92 | |
| 93 | return PyInt_FromLong(fd); |
| 94 | } |
| 95 | |
| 96 | // Convert a C runtime file descriptor to an OS file handle. |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 97 | static PyObject * |
| 98 | msvcrt_get_osfhandle(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 99 | { |
| 100 | int fd; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 101 | Py_intptr_t handle; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 102 | |
| 103 | if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd)) |
| 104 | return NULL; |
| 105 | |
| 106 | handle = _get_osfhandle(fd); |
| 107 | if (handle == -1) |
| 108 | return PyErr_SetFromErrno(PyExc_IOError); |
| 109 | |
Fred Drake | 25e1726 | 2000-06-30 17:48:51 +0000 | [diff] [blame] | 110 | /* technically 'handle' is not a pointer, but a integer as |
| 111 | large as a pointer, Python's *VoidPtr interface is the |
| 112 | most appropriate here */ |
| 113 | return PyLong_FromVoidPtr((void*)handle); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* Console I/O */ |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 117 | |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 118 | static PyObject * |
| 119 | msvcrt_kbhit(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 120 | { |
| 121 | int ok; |
| 122 | |
| 123 | if (!PyArg_ParseTuple(args, ":kbhit")) |
| 124 | return NULL; |
| 125 | |
| 126 | ok = _kbhit(); |
| 127 | return PyInt_FromLong(ok); |
| 128 | } |
| 129 | |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 130 | static PyObject * |
| 131 | msvcrt_getch(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 132 | { |
| 133 | int ch; |
| 134 | char s[1]; |
| 135 | |
| 136 | if (!PyArg_ParseTuple(args, ":getch")) |
| 137 | return NULL; |
| 138 | |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 139 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 140 | ch = _getch(); |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 141 | Py_END_ALLOW_THREADS |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 142 | s[0] = ch; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 143 | return PyString_FromStringAndSize(s, 1); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 146 | #ifdef _WCONIO_DEFINED |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 147 | static PyObject * |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 148 | msvcrt_getwch(PyObject *self, PyObject *args) |
| 149 | { |
| 150 | Py_UNICODE ch; |
| 151 | Py_UNICODE u[1]; |
| 152 | |
| 153 | if (!PyArg_ParseTuple(args, ":getwch")) |
| 154 | return NULL; |
| 155 | |
| 156 | Py_BEGIN_ALLOW_THREADS |
| 157 | ch = _getwch(); |
| 158 | Py_END_ALLOW_THREADS |
| 159 | u[0] = ch; |
| 160 | return PyUnicode_FromUnicode(u, 1); |
| 161 | } |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 162 | #endif |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 163 | |
| 164 | static PyObject * |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 165 | msvcrt_getche(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 166 | { |
| 167 | int ch; |
| 168 | char s[1]; |
| 169 | |
| 170 | if (!PyArg_ParseTuple(args, ":getche")) |
| 171 | return NULL; |
| 172 | |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 173 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 174 | ch = _getche(); |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 175 | Py_END_ALLOW_THREADS |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 176 | s[0] = ch; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 177 | return PyString_FromStringAndSize(s, 1); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 180 | #ifdef _WCONIO_DEFINED |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 181 | static PyObject * |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 182 | msvcrt_getwche(PyObject *self, PyObject *args) |
| 183 | { |
| 184 | Py_UNICODE ch; |
| 185 | Py_UNICODE s[1]; |
| 186 | |
| 187 | if (!PyArg_ParseTuple(args, ":getwche")) |
| 188 | return NULL; |
| 189 | |
| 190 | Py_BEGIN_ALLOW_THREADS |
| 191 | ch = _getwche(); |
| 192 | Py_END_ALLOW_THREADS |
| 193 | s[0] = ch; |
| 194 | return PyUnicode_FromUnicode(s, 1); |
| 195 | } |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 196 | #endif |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 197 | |
| 198 | static PyObject * |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 199 | msvcrt_putch(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 200 | { |
| 201 | char ch; |
| 202 | |
| 203 | if (!PyArg_ParseTuple(args, "c:putch", &ch)) |
| 204 | return NULL; |
| 205 | |
| 206 | _putch(ch); |
| 207 | Py_INCREF(Py_None); |
| 208 | return Py_None; |
| 209 | } |
| 210 | |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 211 | #ifdef _WCONIO_DEFINED |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 212 | static PyObject * |
| 213 | msvcrt_putwch(PyObject *self, PyObject *args) |
| 214 | { |
| 215 | Py_UNICODE *ch; |
| 216 | int size; |
| 217 | |
| 218 | if (!PyArg_ParseTuple(args, "u#:putwch", &ch, &size)) |
| 219 | return NULL; |
| 220 | |
Christian Heimes | 61927fc | 2007-12-10 15:39:09 +0000 | [diff] [blame] | 221 | if (size == 0) { |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 222 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 61927fc | 2007-12-10 15:39:09 +0000 | [diff] [blame] | 223 | "Expected unicode string of length 1"); |
| 224 | return NULL; |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 225 | } |
Christian Heimes | 61927fc | 2007-12-10 15:39:09 +0000 | [diff] [blame] | 226 | _putwch(*ch); |
| 227 | Py_RETURN_NONE; |
| 228 | |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 229 | } |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 230 | #endif |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 231 | |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 232 | static PyObject * |
| 233 | msvcrt_ungetch(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 234 | { |
| 235 | char ch; |
| 236 | |
| 237 | if (!PyArg_ParseTuple(args, "c:ungetch", &ch)) |
| 238 | return NULL; |
| 239 | |
Guido van Rossum | 6543e88 | 1999-02-16 19:40:02 +0000 | [diff] [blame] | 240 | if (_ungetch(ch) == EOF) |
| 241 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 242 | Py_INCREF(Py_None); |
| 243 | return Py_None; |
| 244 | } |
| 245 | |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 246 | #ifdef _WCONIO_DEFINED |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 247 | static PyObject * |
| 248 | msvcrt_ungetwch(PyObject *self, PyObject *args) |
| 249 | { |
| 250 | Py_UNICODE ch; |
| 251 | |
| 252 | if (!PyArg_ParseTuple(args, "u:ungetwch", &ch)) |
| 253 | return NULL; |
| 254 | |
| 255 | if (_ungetch(ch) == EOF) |
| 256 | return PyErr_SetFromErrno(PyExc_IOError); |
| 257 | Py_INCREF(Py_None); |
| 258 | return Py_None; |
| 259 | } |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 260 | #endif |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 261 | |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 262 | static void |
| 263 | insertint(PyObject *d, char *name, int value) |
| 264 | { |
| 265 | PyObject *v = PyInt_FromLong((long) value); |
| 266 | if (v == NULL) { |
| 267 | /* Don't bother reporting this error */ |
| 268 | PyErr_Clear(); |
| 269 | } |
| 270 | else { |
| 271 | PyDict_SetItemString(d, name, v); |
| 272 | Py_DECREF(v); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 277 | /* List of functions exported by this module */ |
| 278 | static struct PyMethodDef msvcrt_functions[] = { |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 279 | {"heapmin", msvcrt_heapmin, METH_VARARGS}, |
| 280 | {"locking", msvcrt_locking, METH_VARARGS}, |
| 281 | {"setmode", msvcrt_setmode, METH_VARARGS}, |
| 282 | {"open_osfhandle", msvcrt_open_osfhandle, METH_VARARGS}, |
| 283 | {"get_osfhandle", msvcrt_get_osfhandle, METH_VARARGS}, |
| 284 | {"kbhit", msvcrt_kbhit, METH_VARARGS}, |
| 285 | {"getch", msvcrt_getch, METH_VARARGS}, |
| 286 | {"getche", msvcrt_getche, METH_VARARGS}, |
| 287 | {"putch", msvcrt_putch, METH_VARARGS}, |
| 288 | {"ungetch", msvcrt_ungetch, METH_VARARGS}, |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 289 | #ifdef _WCONIO_DEFINED |
Christian Heimes | 7c7f6af | 2007-12-10 15:12:41 +0000 | [diff] [blame] | 290 | {"getwch", msvcrt_getwch, METH_VARARGS}, |
| 291 | {"getwche", msvcrt_getwche, METH_VARARGS}, |
| 292 | {"putwch", msvcrt_putwch, METH_VARARGS}, |
| 293 | {"ungetwch", msvcrt_ungetwch, METH_VARARGS}, |
Amaury Forgeot d'Arc | a4dd2e2 | 2008-06-13 00:42:22 +0000 | [diff] [blame] | 294 | #endif |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 295 | {NULL, NULL} |
| 296 | }; |
| 297 | |
Thomas Heller | a18331d | 2004-07-28 20:02:52 +0000 | [diff] [blame] | 298 | PyMODINIT_FUNC |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 299 | initmsvcrt(void) |
| 300 | { |
Tim Peters | 2188bf0 | 2006-01-19 15:21:30 +0000 | [diff] [blame] | 301 | PyObject *d; |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 302 | PyObject *m = Py_InitModule("msvcrt", msvcrt_functions); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 303 | if (m == NULL) |
| 304 | return; |
Tim Peters | 2188bf0 | 2006-01-19 15:21:30 +0000 | [diff] [blame] | 305 | d = PyModule_GetDict(m); |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 306 | |
| 307 | /* constants for the locking() function's mode argument */ |
| 308 | insertint(d, "LK_LOCK", _LK_LOCK); |
| 309 | insertint(d, "LK_NBLCK", _LK_NBLCK); |
| 310 | insertint(d, "LK_NBRLCK", _LK_NBRLCK); |
| 311 | insertint(d, "LK_RLCK", _LK_RLCK); |
| 312 | insertint(d, "LK_UNLCK", _LK_UNLCK); |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 313 | } |