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