blob: 37631a7792358f884e5422776e91204788691312 [file] [log] [blame]
Guido van Rossum29c1ea51997-08-07 00:11:34 +00001/*********************************************************
2
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003 msvcrtmodule.c
Guido van Rossum29c1ea51997-08-07 00:11:34 +00004
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00005 A Python interface to the Microsoft Visual C Runtime
6 Library, providing access to those non-portable, but
7 still useful routines.
Guido van Rossum29c1ea51997-08-07 00:11:34 +00008
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00009 Only ever compiled with an MS compiler, so no attempt
10 has been made to avoid MS language extensions, etc...
Guido van Rossum29c1ea51997-08-07 00:11:34 +000011
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000012 This may only work on NT or 95...
Guido van Rossum407a22d1997-08-13 19:57:53 +000013
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000014 Author: Mark Hammond and Guido van Rossum.
15 Maintenance: Guido van Rossum.
Guido van Rossum407a22d1997-08-13 19:57:53 +000016
Guido van Rossum29c1ea51997-08-07 00:11:34 +000017***********************************************************/
Guido van Rossum407a22d1997-08-13 19:57:53 +000018
Guido van Rossum29c1ea51997-08-07 00:11:34 +000019#include "Python.h"
20#include "malloc.h"
Tim Peters5fa0bd62000-12-12 01:58:56 +000021#include <io.h>
22#include <conio.h>
23#include <sys/locking.h>
Martin v. Löwis3dc33d12007-08-31 07:58:36 +000024#include <crtdbg.h>
25#include <windows.h>
Guido van Rossum29c1ea51997-08-07 00:11:34 +000026
Benjamin Peterson4469d0c2008-11-30 22:46:23 +000027#ifdef _MSC_VER
Martin v. Löwis31e8af92010-02-18 16:29:17 +000028#if _MSC_VER >= 1500 && _MSC_VER < 1600
Benjamin Peterson4469d0c2008-11-30 22:46:23 +000029#include <crtassem.h>
30#endif
31#endif
32
Guido van Rossum407a22d1997-08-13 19:57:53 +000033// 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 Peters5fa0bd62000-12-12 01:58:56 +000035static PyObject *
36msvcrt_heapmin(PyObject *self, PyObject *args)
Guido van Rossum29c1ea51997-08-07 00:11:34 +000037{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000038 if (!PyArg_ParseTuple(args, ":heapmin"))
39 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +000040
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000041 if (_heapmin() != 0)
42 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +000043
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000044 Py_INCREF(Py_None);
45 return Py_None;
Guido van Rossum29c1ea51997-08-07 00:11:34 +000046}
47
Benjamin Peterson68dbebc2009-12-31 03:30:26 +000048PyDoc_STRVAR(heapmin_doc,
49"heapmin() -> None\n\
50\n\
51Force the malloc() heap to clean itself up and return unused blocks\n\
52to the operating system. On failure, this raises IOError.");
53
Guido van Rossum407a22d1997-08-13 19:57:53 +000054// Perform locking operations on a C runtime file descriptor.
Tim Peters5fa0bd62000-12-12 01:58:56 +000055static PyObject *
56msvcrt_locking(PyObject *self, PyObject *args)
Guido van Rossum29c1ea51997-08-07 00:11:34 +000057{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000058 int fd;
59 int mode;
60 long nbytes;
61 int err;
Guido van Rossum407a22d1997-08-13 19:57:53 +000062
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000063 if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes))
64 return NULL;
Guido van Rossum29c1ea51997-08-07 00:11:34 +000065
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000066 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 Rossum29c1ea51997-08-07 00:11:34 +000071
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000072 Py_INCREF(Py_None);
73 return Py_None;
Guido van Rossum29c1ea51997-08-07 00:11:34 +000074}
Guido van Rossum407a22d1997-08-13 19:57:53 +000075
Benjamin Peterson68dbebc2009-12-31 03:30:26 +000076PyDoc_STRVAR(locking_doc,
77"locking(fd, mode, nbytes) -> None\n\
78\n\
79Lock part of a file based on file descriptor fd from the C runtime.\n\
80Raises IOError on failure. The locked region of the file extends from\n\
81the current file position for nbytes bytes, and may continue beyond\n\
82the end of the file. mode must be one of the LK_* constants listed\n\
83below. Multiple regions in a file may be locked at the same time, but\n\
84may not overlap. Adjacent regions are not merged; they must be unlocked\n\
85individually.");
86
Guido van Rossum407a22d1997-08-13 19:57:53 +000087// Set the file translation mode for a C runtime file descriptor.
Tim Peters5fa0bd62000-12-12 01:58:56 +000088static PyObject *
89msvcrt_setmode(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +000090{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000091 int fd;
92 int flags;
93 if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags))
94 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +000095
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000096 flags = _setmode(fd, flags);
97 if (flags == -1)
98 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +000099
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000100 return PyLong_FromLong(flags);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000101}
102
Benjamin Peterson68dbebc2009-12-31 03:30:26 +0000103PyDoc_STRVAR(setmode_doc,
104"setmode(fd, mode) -> Previous mode\n\
105\n\
106Set the line-end translation mode for the file descriptor fd. To set\n\
107it to text mode, flags should be os.O_TEXT; for binary, it should be\n\
108os.O_BINARY.");
109
Guido van Rossum407a22d1997-08-13 19:57:53 +0000110// Convert an OS file handle to a C runtime file descriptor.
Tim Peters5fa0bd62000-12-12 01:58:56 +0000111static PyObject *
112msvcrt_open_osfhandle(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000113{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000114 long handle;
115 int flags;
116 int fd;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000117
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000118 if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags))
119 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000120
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000121 fd = _open_osfhandle(handle, flags);
122 if (fd == -1)
123 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000124
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000125 return PyLong_FromLong(fd);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000126}
127
Benjamin Peterson68dbebc2009-12-31 03:30:26 +0000128PyDoc_STRVAR(open_osfhandle_doc,
129"open_osfhandle(handle, flags) -> file descriptor\n\
130\n\
131Create a C runtime file descriptor from the file handle handle. The\n\
132flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY,\n\
133and os.O_TEXT. The returned file descriptor may be used as a parameter\n\
134to os.fdopen() to create a file object.");
135
Guido van Rossum407a22d1997-08-13 19:57:53 +0000136// Convert a C runtime file descriptor to an OS file handle.
Tim Peters5fa0bd62000-12-12 01:58:56 +0000137static PyObject *
138msvcrt_get_osfhandle(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000139{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000140 int fd;
141 Py_intptr_t handle;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000142
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000143 if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
144 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000145
Antoine Pitrou0bb502d2010-09-04 21:02:41 +0000146 if (!_PyVerify_fd(fd))
147 return PyErr_SetFromErrno(PyExc_IOError);
148
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000149 handle = _get_osfhandle(fd);
150 if (handle == -1)
151 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000152
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000153 /* 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 Rossum407a22d1997-08-13 19:57:53 +0000157}
158
Benjamin Peterson68dbebc2009-12-31 03:30:26 +0000159PyDoc_STRVAR(get_osfhandle_doc,
160"get_osfhandle(fd) -> file handle\n\
161\n\
162Return the file handle for the file descriptor fd. Raises IOError\n\
163if fd is not recognized.");
164
Guido van Rossum407a22d1997-08-13 19:57:53 +0000165/* Console I/O */
Guido van Rossum407a22d1997-08-13 19:57:53 +0000166
Tim Peters5fa0bd62000-12-12 01:58:56 +0000167static PyObject *
168msvcrt_kbhit(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000169{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000170 int ok;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000171
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000172 if (!PyArg_ParseTuple(args, ":kbhit"))
173 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000174
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000175 ok = _kbhit();
176 return PyLong_FromLong(ok);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000177}
178
Benjamin Peterson68dbebc2009-12-31 03:30:26 +0000179PyDoc_STRVAR(kbhit_doc,
180"kbhit() -> bool\n\
181\n\
182Return true if a keypress is waiting to be read.");
183
Tim Peters5fa0bd62000-12-12 01:58:56 +0000184static PyObject *
185msvcrt_getch(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000186{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000187 int ch;
188 char s[1];
Guido van Rossum407a22d1997-08-13 19:57:53 +0000189
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000190 if (!PyArg_ParseTuple(args, ":getch"))
191 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000192
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000193 Py_BEGIN_ALLOW_THREADS
194 ch = _getch();
195 Py_END_ALLOW_THREADS
196 s[0] = ch;
197 return PyBytes_FromStringAndSize(s, 1);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000198}
199
Benjamin Peterson68dbebc2009-12-31 03:30:26 +0000200PyDoc_STRVAR(getch_doc,
201"getch() -> key character\n\
202\n\
203Read a keypress and return the resulting character. Nothing is echoed to\n\
204the console. This call will block if a keypress is not already\n\
205available, but will not wait for Enter to be pressed. If the pressed key\n\
206was a special function key, this will return '\\000' or '\\xe0'; the next\n\
207call will return the keycode. The Control-C keypress cannot be read with\n\
208this function.");
209
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +0000210#ifdef _WCONIO_DEFINED
Tim Peters5fa0bd62000-12-12 01:58:56 +0000211static PyObject *
Christian Heimes2f1019e2007-12-10 16:18:49 +0000212msvcrt_getwch(PyObject *self, PyObject *args)
213{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000214 Py_UNICODE ch;
215 Py_UNICODE u[1];
Christian Heimes2f1019e2007-12-10 16:18:49 +0000216
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000217 if (!PyArg_ParseTuple(args, ":getwch"))
218 return NULL;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000219
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000220 Py_BEGIN_ALLOW_THREADS
221 ch = _getwch();
222 Py_END_ALLOW_THREADS
223 u[0] = ch;
224 return PyUnicode_FromUnicode(u, 1);
Christian Heimes2f1019e2007-12-10 16:18:49 +0000225}
Benjamin Peterson68dbebc2009-12-31 03:30:26 +0000226
227PyDoc_STRVAR(getwch_doc,
228"getwch() -> Unicode key character\n\
229\n\
230Wide char variant of getch(), returning a Unicode value.");
Christian Heimesc36625b2008-01-04 13:33:00 +0000231#endif
Christian Heimes2f1019e2007-12-10 16:18:49 +0000232
233static PyObject *
Tim Peters5fa0bd62000-12-12 01:58:56 +0000234msvcrt_getche(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000235{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000236 int ch;
237 char s[1];
Guido van Rossum407a22d1997-08-13 19:57:53 +0000238
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000239 if (!PyArg_ParseTuple(args, ":getche"))
240 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000241
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000242 Py_BEGIN_ALLOW_THREADS
243 ch = _getche();
244 Py_END_ALLOW_THREADS
245 s[0] = ch;
246 return PyBytes_FromStringAndSize(s, 1);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000247}
248
Benjamin Peterson68dbebc2009-12-31 03:30:26 +0000249PyDoc_STRVAR(getche_doc,
250"getche() -> key character\n\
251\n\
252Similar to getch(), but the keypress will be echoed if it represents\n\
253a printable character.");
254
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +0000255#ifdef _WCONIO_DEFINED
Tim Peters5fa0bd62000-12-12 01:58:56 +0000256static PyObject *
Christian Heimes2f1019e2007-12-10 16:18:49 +0000257msvcrt_getwche(PyObject *self, PyObject *args)
258{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000259 Py_UNICODE ch;
260 Py_UNICODE s[1];
Christian Heimes2f1019e2007-12-10 16:18:49 +0000261
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000262 if (!PyArg_ParseTuple(args, ":getwche"))
263 return NULL;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000264
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000265 Py_BEGIN_ALLOW_THREADS
266 ch = _getwche();
267 Py_END_ALLOW_THREADS
268 s[0] = ch;
269 return PyUnicode_FromUnicode(s, 1);
Christian Heimes2f1019e2007-12-10 16:18:49 +0000270}
Benjamin Peterson68dbebc2009-12-31 03:30:26 +0000271
272PyDoc_STRVAR(getwche_doc,
273"getwche() -> Unicode key character\n\
274\n\
275Wide char variant of getche(), returning a Unicode value.");
Christian Heimesc36625b2008-01-04 13:33:00 +0000276#endif
Christian Heimes2f1019e2007-12-10 16:18:49 +0000277
278static PyObject *
Tim Peters5fa0bd62000-12-12 01:58:56 +0000279msvcrt_putch(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000280{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000281 char ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000282
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000283 if (!PyArg_ParseTuple(args, "c:putch", &ch))
284 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000285
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000286 _putch(ch);
287 Py_INCREF(Py_None);
288 return Py_None;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000289}
290
Benjamin Peterson68dbebc2009-12-31 03:30:26 +0000291PyDoc_STRVAR(putch_doc,
292"putch(char) -> None\n\
293\n\
294Print the character char to the console without buffering.");
295
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +0000296#ifdef _WCONIO_DEFINED
Christian Heimes2f1019e2007-12-10 16:18:49 +0000297static PyObject *
298msvcrt_putwch(PyObject *self, PyObject *args)
299{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000300 int ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000301
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000302 if (!PyArg_ParseTuple(args, "C:putwch", &ch))
303 return NULL;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000304
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000305 _putwch(ch);
306 Py_RETURN_NONE;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000307
308}
Benjamin Peterson68dbebc2009-12-31 03:30:26 +0000309
310PyDoc_STRVAR(putwch_doc,
311"putwch(unicode_char) -> None\n\
312\n\
313Wide char variant of putch(), accepting a Unicode value.");
Christian Heimesc36625b2008-01-04 13:33:00 +0000314#endif
Christian Heimes2f1019e2007-12-10 16:18:49 +0000315
Tim Peters5fa0bd62000-12-12 01:58:56 +0000316static PyObject *
317msvcrt_ungetch(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000318{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000319 char ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000320
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000321 if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
322 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000323
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000324 if (_ungetch(ch) == EOF)
325 return PyErr_SetFromErrno(PyExc_IOError);
326 Py_INCREF(Py_None);
327 return Py_None;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000328}
329
Benjamin Peterson68dbebc2009-12-31 03:30:26 +0000330PyDoc_STRVAR(ungetch_doc,
331"ungetch(char) -> None\n\
332\n\
333Cause the character char to be \"pushed back\" into the console buffer;\n\
334it will be the next character read by getch() or getche().");
335
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +0000336#ifdef _WCONIO_DEFINED
Christian Heimes2f1019e2007-12-10 16:18:49 +0000337static PyObject *
338msvcrt_ungetwch(PyObject *self, PyObject *args)
339{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000340 int ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000341
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000342 if (!PyArg_ParseTuple(args, "C:ungetwch", &ch))
343 return NULL;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000344
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000345 if (_ungetwch(ch) == WEOF)
346 return PyErr_SetFromErrno(PyExc_IOError);
347 Py_INCREF(Py_None);
348 return Py_None;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000349}
Benjamin Peterson68dbebc2009-12-31 03:30:26 +0000350
351PyDoc_STRVAR(ungetwch_doc,
352"ungetwch(unicode_char) -> None\n\
353\n\
354Wide char variant of ungetch(), accepting a Unicode value.");
Christian Heimesc36625b2008-01-04 13:33:00 +0000355#endif
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000356
Tim Peters5fa0bd62000-12-12 01:58:56 +0000357static void
358insertint(PyObject *d, char *name, int value)
359{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000360 PyObject *v = PyLong_FromLong((long) value);
361 if (v == NULL) {
362 /* Don't bother reporting this error */
363 PyErr_Clear();
364 }
365 else {
366 PyDict_SetItemString(d, name, v);
367 Py_DECREF(v);
368 }
Tim Peters5fa0bd62000-12-12 01:58:56 +0000369}
370
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000371#ifdef _DEBUG
372
373static PyObject*
374msvcrt_setreportfile(PyObject *self, PyObject *args)
375{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000376 int type, file;
377 _HFILE res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000378
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000379 if (!PyArg_ParseTuple(args, "ii", &type, &file))
380 return NULL;
381 res = _CrtSetReportFile(type, (_HFILE)file);
382 return PyLong_FromLong((long)res);
383 Py_INCREF(Py_None);
384 return Py_None;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000385}
386
387static PyObject*
388msvcrt_setreportmode(PyObject *self, PyObject *args)
389{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000390 int type, mode;
391 int res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000392
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000393 if (!PyArg_ParseTuple(args, "ii", &type, &mode))
394 return NULL;
395 res = _CrtSetReportMode(type, mode);
396 if (res == -1)
397 return PyErr_SetFromErrno(PyExc_IOError);
398 return PyLong_FromLong(res);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000399}
400
401static PyObject*
402msvcrt_seterrormode(PyObject *self, PyObject *args)
403{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000404 int mode, res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000405
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000406 if (!PyArg_ParseTuple(args, "i", &mode))
407 return NULL;
408 res = _set_error_mode(mode);
409 return PyLong_FromLong(res);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000410}
411
412#endif
413
414static PyObject*
415seterrormode(PyObject *self, PyObject *args)
416{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000417 unsigned int mode, res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000418
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000419 if (!PyArg_ParseTuple(args, "I", &mode))
420 return NULL;
421 res = SetErrorMode(mode);
422 return PyLong_FromUnsignedLong(res);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000423}
424
Tim Peters5fa0bd62000-12-12 01:58:56 +0000425
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000426/* List of functions exported by this module */
427static struct PyMethodDef msvcrt_functions[] = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000428 {"heapmin", msvcrt_heapmin, METH_VARARGS, heapmin_doc},
429 {"locking", msvcrt_locking, METH_VARARGS, locking_doc},
430 {"setmode", msvcrt_setmode, METH_VARARGS, setmode_doc},
431 {"open_osfhandle", msvcrt_open_osfhandle, METH_VARARGS, open_osfhandle_doc},
432 {"get_osfhandle", msvcrt_get_osfhandle, METH_VARARGS, get_osfhandle_doc},
433 {"kbhit", msvcrt_kbhit, METH_VARARGS, kbhit_doc},
434 {"getch", msvcrt_getch, METH_VARARGS, getch_doc},
435 {"getche", msvcrt_getche, METH_VARARGS, getche_doc},
436 {"putch", msvcrt_putch, METH_VARARGS, putch_doc},
437 {"ungetch", msvcrt_ungetch, METH_VARARGS, ungetch_doc},
438 {"SetErrorMode", seterrormode, METH_VARARGS},
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000439#ifdef _DEBUG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000440 {"CrtSetReportFile", msvcrt_setreportfile, METH_VARARGS},
441 {"CrtSetReportMode", msvcrt_setreportmode, METH_VARARGS},
442 {"set_error_mode", msvcrt_seterrormode, METH_VARARGS},
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000443#endif
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +0000444#ifdef _WCONIO_DEFINED
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000445 {"getwch", msvcrt_getwch, METH_VARARGS, getwch_doc},
446 {"getwche", msvcrt_getwche, METH_VARARGS, getwche_doc},
447 {"putwch", msvcrt_putwch, METH_VARARGS, putwch_doc},
448 {"ungetwch", msvcrt_ungetwch, METH_VARARGS, ungetwch_doc},
Christian Heimesc36625b2008-01-04 13:33:00 +0000449#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000450 {NULL, NULL}
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000451};
452
Martin v. Löwis1a214512008-06-11 05:26:20 +0000453
454static struct PyModuleDef msvcrtmodule = {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000455 PyModuleDef_HEAD_INIT,
456 "msvcrt",
457 NULL,
458 -1,
459 msvcrt_functions,
460 NULL,
461 NULL,
462 NULL,
463 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000464};
465
Thomas Hellera18331d2004-07-28 20:02:52 +0000466PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000467PyInit_msvcrt(void)
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000468{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000469 int st;
470 PyObject *d;
471 PyObject *m = PyModule_Create(&msvcrtmodule);
472 if (m == NULL)
473 return NULL;
474 d = PyModule_GetDict(m);
Tim Peters5fa0bd62000-12-12 01:58:56 +0000475
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000476 /* constants for the locking() function's mode argument */
477 insertint(d, "LK_LOCK", _LK_LOCK);
478 insertint(d, "LK_NBLCK", _LK_NBLCK);
479 insertint(d, "LK_NBRLCK", _LK_NBRLCK);
480 insertint(d, "LK_RLCK", _LK_RLCK);
481 insertint(d, "LK_UNLCK", _LK_UNLCK);
482 insertint(d, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS);
483 insertint(d, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT);
484 insertint(d, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX);
485 insertint(d, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000486#ifdef _DEBUG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000487 insertint(d, "CRT_WARN", _CRT_WARN);
488 insertint(d, "CRT_ERROR", _CRT_ERROR);
489 insertint(d, "CRT_ASSERT", _CRT_ASSERT);
490 insertint(d, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG);
491 insertint(d, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE);
492 insertint(d, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW);
493 insertint(d, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE);
494 insertint(d, "CRTDBG_FILE_STDERR", (int)_CRTDBG_FILE_STDERR);
495 insertint(d, "CRTDBG_FILE_STDOUT", (int)_CRTDBG_FILE_STDOUT);
496 insertint(d, "CRTDBG_REPORT_FILE", (int)_CRTDBG_REPORT_FILE);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000497#endif
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000498
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000499 /* constants for the crt versions */
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000500#ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000501 st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
502 _VC_ASSEMBLY_PUBLICKEYTOKEN);
503 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000504#endif
505#ifdef _CRT_ASSEMBLY_VERSION
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000506 st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
507 _CRT_ASSEMBLY_VERSION);
508 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000509#endif
510#ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000511 st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
512 __LIBRARIES_ASSEMBLY_NAME_PREFIX);
513 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000514#endif
515
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000516 return m;
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000517}