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 | |
Benjamin Peterson | 4469d0c | 2008-11-30 22:46:23 +0000 | [diff] [blame] | 27 | #ifdef _MSC_VER |
| 28 | #if _MSC_VER >= 1500 |
| 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 | { |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 38 | if (!PyArg_ParseTuple(args, ":heapmin")) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 39 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 40 | |
| 41 | if (_heapmin() != 0) |
| 42 | return PyErr_SetFromErrno(PyExc_IOError); |
| 43 | |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 44 | Py_INCREF(Py_None); |
| 45 | return Py_None; |
| 46 | } |
| 47 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 48 | // Perform locking operations on a C runtime file descriptor. |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 49 | static PyObject * |
| 50 | msvcrt_locking(PyObject *self, PyObject *args) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 51 | { |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 52 | int fd; |
| 53 | int mode; |
| 54 | long nbytes; |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 55 | int err; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 56 | |
| 57 | if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes)) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 58 | return NULL; |
| 59 | |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 60 | Py_BEGIN_ALLOW_THREADS |
| 61 | err = _locking(fd, mode, nbytes); |
| 62 | Py_END_ALLOW_THREADS |
| 63 | if (err != 0) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 64 | return PyErr_SetFromErrno(PyExc_IOError); |
| 65 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 66 | Py_INCREF(Py_None); |
| 67 | return Py_None; |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 68 | } |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 69 | |
| 70 | // Set the file translation mode for a C runtime file descriptor. |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 71 | static PyObject * |
| 72 | msvcrt_setmode(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 73 | { |
| 74 | int fd; |
| 75 | int flags; |
| 76 | if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags)) |
| 77 | return NULL; |
| 78 | |
| 79 | flags = _setmode(fd, flags); |
| 80 | if (flags == -1) |
| 81 | return PyErr_SetFromErrno(PyExc_IOError); |
| 82 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 83 | return PyLong_FromLong(flags); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // Convert an OS file handle to a C runtime file descriptor. |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 87 | static PyObject * |
| 88 | msvcrt_open_osfhandle(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 89 | { |
| 90 | long handle; |
| 91 | int flags; |
| 92 | int fd; |
| 93 | |
| 94 | if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags)) |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 95 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 96 | |
| 97 | fd = _open_osfhandle(handle, flags); |
| 98 | if (fd == -1) |
| 99 | return PyErr_SetFromErrno(PyExc_IOError); |
| 100 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 101 | return PyLong_FromLong(fd); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Convert a C runtime file descriptor to an OS file handle. |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 105 | static PyObject * |
| 106 | msvcrt_get_osfhandle(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 107 | { |
| 108 | int fd; |
Tim Peters | 79248aa | 2001-08-29 21:37:10 +0000 | [diff] [blame] | 109 | Py_intptr_t handle; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 110 | |
| 111 | if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd)) |
| 112 | return NULL; |
| 113 | |
| 114 | handle = _get_osfhandle(fd); |
| 115 | if (handle == -1) |
| 116 | return PyErr_SetFromErrno(PyExc_IOError); |
| 117 | |
Fred Drake | 25e1726 | 2000-06-30 17:48:51 +0000 | [diff] [blame] | 118 | /* technically 'handle' is not a pointer, but a integer as |
| 119 | large as a pointer, Python's *VoidPtr interface is the |
| 120 | most appropriate here */ |
| 121 | return PyLong_FromVoidPtr((void*)handle); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* Console I/O */ |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 125 | |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 126 | static PyObject * |
| 127 | msvcrt_kbhit(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 128 | { |
| 129 | int ok; |
| 130 | |
| 131 | if (!PyArg_ParseTuple(args, ":kbhit")) |
| 132 | return NULL; |
| 133 | |
| 134 | ok = _kbhit(); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 135 | return PyLong_FromLong(ok); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 138 | static PyObject * |
| 139 | msvcrt_getch(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 140 | { |
| 141 | int ch; |
| 142 | char s[1]; |
| 143 | |
| 144 | if (!PyArg_ParseTuple(args, ":getch")) |
| 145 | return NULL; |
| 146 | |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 147 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 148 | ch = _getch(); |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 149 | Py_END_ALLOW_THREADS |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 150 | s[0] = ch; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 151 | return PyBytes_FromStringAndSize(s, 1); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 154 | #ifdef _WCONIO_DEFINED |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 155 | static PyObject * |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 156 | msvcrt_getwch(PyObject *self, PyObject *args) |
| 157 | { |
| 158 | Py_UNICODE ch; |
| 159 | Py_UNICODE u[1]; |
| 160 | |
| 161 | if (!PyArg_ParseTuple(args, ":getwch")) |
| 162 | return NULL; |
| 163 | |
| 164 | Py_BEGIN_ALLOW_THREADS |
| 165 | ch = _getwch(); |
| 166 | Py_END_ALLOW_THREADS |
| 167 | u[0] = ch; |
| 168 | return PyUnicode_FromUnicode(u, 1); |
| 169 | } |
Christian Heimes | c36625b | 2008-01-04 13:33:00 +0000 | [diff] [blame] | 170 | #endif |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 171 | |
| 172 | static PyObject * |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 173 | msvcrt_getche(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 174 | { |
| 175 | int ch; |
| 176 | char s[1]; |
| 177 | |
| 178 | if (!PyArg_ParseTuple(args, ":getche")) |
| 179 | return NULL; |
| 180 | |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 181 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 182 | ch = _getche(); |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 183 | Py_END_ALLOW_THREADS |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 184 | s[0] = ch; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 185 | return PyBytes_FromStringAndSize(s, 1); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 188 | #ifdef _WCONIO_DEFINED |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 189 | static PyObject * |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 190 | msvcrt_getwche(PyObject *self, PyObject *args) |
| 191 | { |
| 192 | Py_UNICODE ch; |
| 193 | Py_UNICODE s[1]; |
| 194 | |
| 195 | if (!PyArg_ParseTuple(args, ":getwche")) |
| 196 | return NULL; |
| 197 | |
| 198 | Py_BEGIN_ALLOW_THREADS |
| 199 | ch = _getwche(); |
| 200 | Py_END_ALLOW_THREADS |
| 201 | s[0] = ch; |
| 202 | return PyUnicode_FromUnicode(s, 1); |
| 203 | } |
Christian Heimes | c36625b | 2008-01-04 13:33:00 +0000 | [diff] [blame] | 204 | #endif |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 205 | |
| 206 | static PyObject * |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 207 | msvcrt_putch(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 208 | { |
| 209 | char ch; |
| 210 | |
| 211 | if (!PyArg_ParseTuple(args, "c:putch", &ch)) |
| 212 | return NULL; |
| 213 | |
| 214 | _putch(ch); |
| 215 | Py_INCREF(Py_None); |
| 216 | return Py_None; |
| 217 | } |
| 218 | |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 219 | #ifdef _WCONIO_DEFINED |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 220 | static PyObject * |
| 221 | msvcrt_putwch(PyObject *self, PyObject *args) |
| 222 | { |
Benjamin Peterson | 435b0f2 | 2009-05-01 21:42:23 +0000 | [diff] [blame] | 223 | int ch; |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 224 | |
Benjamin Peterson | 435b0f2 | 2009-05-01 21:42:23 +0000 | [diff] [blame] | 225 | if (!PyArg_ParseTuple(args, "C:putwch", &ch)) |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 226 | return NULL; |
| 227 | |
Benjamin Peterson | 435b0f2 | 2009-05-01 21:42:23 +0000 | [diff] [blame] | 228 | _putwch(ch); |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 229 | Py_RETURN_NONE; |
| 230 | |
| 231 | } |
Christian Heimes | c36625b | 2008-01-04 13:33:00 +0000 | [diff] [blame] | 232 | #endif |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 233 | |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 234 | static PyObject * |
| 235 | msvcrt_ungetch(PyObject *self, PyObject *args) |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 236 | { |
| 237 | char ch; |
| 238 | |
| 239 | if (!PyArg_ParseTuple(args, "c:ungetch", &ch)) |
| 240 | return NULL; |
| 241 | |
Guido van Rossum | 6543e88 | 1999-02-16 19:40:02 +0000 | [diff] [blame] | 242 | if (_ungetch(ch) == EOF) |
| 243 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 244 | Py_INCREF(Py_None); |
| 245 | return Py_None; |
| 246 | } |
| 247 | |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 248 | #ifdef _WCONIO_DEFINED |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 249 | static PyObject * |
| 250 | msvcrt_ungetwch(PyObject *self, PyObject *args) |
| 251 | { |
Benjamin Peterson | 435b0f2 | 2009-05-01 21:42:23 +0000 | [diff] [blame] | 252 | int ch; |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 253 | |
Benjamin Peterson | 435b0f2 | 2009-05-01 21:42:23 +0000 | [diff] [blame] | 254 | if (!PyArg_ParseTuple(args, "C:ungetwch", &ch)) |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 255 | return NULL; |
| 256 | |
Benjamin Peterson | 435b0f2 | 2009-05-01 21:42:23 +0000 | [diff] [blame] | 257 | if (_ungetwch(ch) == WEOF) |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 258 | return PyErr_SetFromErrno(PyExc_IOError); |
| 259 | Py_INCREF(Py_None); |
| 260 | return Py_None; |
| 261 | } |
Christian Heimes | c36625b | 2008-01-04 13:33:00 +0000 | [diff] [blame] | 262 | #endif |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 263 | |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 264 | static void |
| 265 | insertint(PyObject *d, char *name, int value) |
| 266 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 267 | PyObject *v = PyLong_FromLong((long) value); |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 268 | if (v == NULL) { |
| 269 | /* Don't bother reporting this error */ |
| 270 | PyErr_Clear(); |
| 271 | } |
| 272 | else { |
| 273 | PyDict_SetItemString(d, name, v); |
| 274 | Py_DECREF(v); |
| 275 | } |
| 276 | } |
| 277 | |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 278 | #ifdef _DEBUG |
| 279 | |
| 280 | static PyObject* |
| 281 | msvcrt_setreportfile(PyObject *self, PyObject *args) |
| 282 | { |
| 283 | int type, file; |
| 284 | _HFILE res; |
| 285 | |
| 286 | if (!PyArg_ParseTuple(args, "ii", &type, &file)) |
| 287 | return NULL; |
| 288 | res = _CrtSetReportFile(type, (_HFILE)file); |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 289 | return PyLong_FromLong((long)res); |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 290 | Py_INCREF(Py_None); |
| 291 | return Py_None; |
| 292 | } |
| 293 | |
| 294 | static PyObject* |
| 295 | msvcrt_setreportmode(PyObject *self, PyObject *args) |
| 296 | { |
| 297 | int type, mode; |
| 298 | int res; |
| 299 | |
| 300 | if (!PyArg_ParseTuple(args, "ii", &type, &mode)) |
| 301 | return NULL; |
| 302 | res = _CrtSetReportMode(type, mode); |
| 303 | if (res == -1) |
| 304 | return PyErr_SetFromErrno(PyExc_IOError); |
| 305 | return PyLong_FromLong(res); |
| 306 | } |
| 307 | |
| 308 | static PyObject* |
| 309 | msvcrt_seterrormode(PyObject *self, PyObject *args) |
| 310 | { |
| 311 | int mode, res; |
| 312 | |
| 313 | if (!PyArg_ParseTuple(args, "i", &mode)) |
| 314 | return NULL; |
| 315 | res = _set_error_mode(mode); |
| 316 | return PyLong_FromLong(res); |
| 317 | } |
| 318 | |
| 319 | #endif |
| 320 | |
| 321 | static PyObject* |
| 322 | seterrormode(PyObject *self, PyObject *args) |
| 323 | { |
| 324 | unsigned int mode, res; |
| 325 | |
| 326 | if (!PyArg_ParseTuple(args, "I", &mode)) |
| 327 | return NULL; |
| 328 | res = SetErrorMode(mode); |
| 329 | return PyLong_FromUnsignedLong(res); |
| 330 | } |
| 331 | |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 332 | |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 333 | /* List of functions exported by this module */ |
| 334 | static struct PyMethodDef msvcrt_functions[] = { |
Neal Norwitz | 031829d | 2002-03-31 14:37:44 +0000 | [diff] [blame] | 335 | {"heapmin", msvcrt_heapmin, METH_VARARGS}, |
| 336 | {"locking", msvcrt_locking, METH_VARARGS}, |
| 337 | {"setmode", msvcrt_setmode, METH_VARARGS}, |
| 338 | {"open_osfhandle", msvcrt_open_osfhandle, METH_VARARGS}, |
| 339 | {"get_osfhandle", msvcrt_get_osfhandle, METH_VARARGS}, |
| 340 | {"kbhit", msvcrt_kbhit, METH_VARARGS}, |
| 341 | {"getch", msvcrt_getch, METH_VARARGS}, |
| 342 | {"getche", msvcrt_getche, METH_VARARGS}, |
| 343 | {"putch", msvcrt_putch, METH_VARARGS}, |
| 344 | {"ungetch", msvcrt_ungetch, METH_VARARGS}, |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 345 | {"SetErrorMode", seterrormode, METH_VARARGS}, |
| 346 | #ifdef _DEBUG |
| 347 | {"CrtSetReportFile", msvcrt_setreportfile, METH_VARARGS}, |
| 348 | {"CrtSetReportMode", msvcrt_setreportmode, METH_VARARGS}, |
| 349 | {"set_error_mode", msvcrt_seterrormode, METH_VARARGS}, |
| 350 | #endif |
Amaury Forgeot d'Arc | 3d17a5c | 2008-06-13 01:09:34 +0000 | [diff] [blame] | 351 | #ifdef _WCONIO_DEFINED |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 352 | {"getwch", msvcrt_getwch, METH_VARARGS}, |
| 353 | {"getwche", msvcrt_getwche, METH_VARARGS}, |
| 354 | {"putwch", msvcrt_putwch, METH_VARARGS}, |
| 355 | {"ungetwch", msvcrt_ungetwch, METH_VARARGS}, |
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 | {NULL, NULL} |
| 358 | }; |
| 359 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 360 | |
| 361 | static struct PyModuleDef msvcrtmodule = { |
| 362 | PyModuleDef_HEAD_INIT, |
| 363 | "msvcrt", |
| 364 | NULL, |
| 365 | -1, |
| 366 | msvcrt_functions, |
| 367 | NULL, |
| 368 | NULL, |
| 369 | NULL, |
| 370 | NULL |
| 371 | }; |
| 372 | |
Thomas Heller | a18331d | 2004-07-28 20:02:52 +0000 | [diff] [blame] | 373 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 374 | PyInit_msvcrt(void) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 375 | { |
Benjamin Peterson | 4469d0c | 2008-11-30 22:46:23 +0000 | [diff] [blame] | 376 | int st; |
Tim Peters | 2188bf0 | 2006-01-19 15:21:30 +0000 | [diff] [blame] | 377 | PyObject *d; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 378 | PyObject *m = PyModule_Create(&msvcrtmodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 379 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 380 | return NULL; |
Tim Peters | 2188bf0 | 2006-01-19 15:21:30 +0000 | [diff] [blame] | 381 | d = PyModule_GetDict(m); |
Tim Peters | 5fa0bd6 | 2000-12-12 01:58:56 +0000 | [diff] [blame] | 382 | |
| 383 | /* constants for the locking() function's mode argument */ |
| 384 | insertint(d, "LK_LOCK", _LK_LOCK); |
| 385 | insertint(d, "LK_NBLCK", _LK_NBLCK); |
| 386 | insertint(d, "LK_NBRLCK", _LK_NBRLCK); |
| 387 | insertint(d, "LK_RLCK", _LK_RLCK); |
| 388 | insertint(d, "LK_UNLCK", _LK_UNLCK); |
Martin v. Löwis | 3dc33d1 | 2007-08-31 07:58:36 +0000 | [diff] [blame] | 389 | insertint(d, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS); |
| 390 | insertint(d, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT); |
| 391 | insertint(d, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX); |
| 392 | insertint(d, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX); |
| 393 | #ifdef _DEBUG |
| 394 | insertint(d, "CRT_WARN", _CRT_WARN); |
| 395 | insertint(d, "CRT_ERROR", _CRT_ERROR); |
| 396 | insertint(d, "CRT_ASSERT", _CRT_ASSERT); |
| 397 | insertint(d, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG); |
| 398 | insertint(d, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE); |
| 399 | insertint(d, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW); |
| 400 | insertint(d, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE); |
| 401 | insertint(d, "CRTDBG_FILE_STDERR", (int)_CRTDBG_FILE_STDERR); |
| 402 | insertint(d, "CRTDBG_FILE_STDOUT", (int)_CRTDBG_FILE_STDOUT); |
| 403 | insertint(d, "CRTDBG_REPORT_FILE", (int)_CRTDBG_REPORT_FILE); |
| 404 | #endif |
Benjamin Peterson | 4469d0c | 2008-11-30 22:46:23 +0000 | [diff] [blame] | 405 | |
| 406 | /* constants for the crt versions */ |
| 407 | #ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN |
| 408 | st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN", |
| 409 | _VC_ASSEMBLY_PUBLICKEYTOKEN); |
| 410 | if (st < 0) return NULL; |
| 411 | #endif |
| 412 | #ifdef _CRT_ASSEMBLY_VERSION |
| 413 | st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION", |
| 414 | _CRT_ASSEMBLY_VERSION); |
| 415 | if (st < 0) return NULL; |
| 416 | #endif |
| 417 | #ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX |
| 418 | st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX", |
| 419 | __LIBRARIES_ASSEMBLY_NAME_PREFIX); |
| 420 | if (st < 0) return NULL; |
| 421 | #endif |
| 422 | |
| 423 | return m; |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 424 | } |