blob: 18dec6dbe864d74ebd1ca2e13d3576efa2b56fa3 [file] [log] [blame]
Guido van Rossum29c1ea51997-08-07 00:11:34 +00001/*********************************************************
2
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003 msvcrtmodule.c
Guido van Rossum29c1ea51997-08-07 00:11:34 +00004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +000012 This may only work on NT or 95...
Guido van Rossum407a22d1997-08-13 19:57:53 +000013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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öwis3afc62e2010-02-18 16:27:43 +000028#if _MSC_VER >= 1500 && _MSC_VER < 1600
Benjamin Peterson4469d0c2008-11-30 22:46:23 +000029#include <crtassem.h>
Brian Curtin401f9f32012-05-13 11:19:23 -050030#elif _MSC_VER >= 1600
31#include <crtversion.h>
Benjamin Peterson4469d0c2008-11-30 22:46:23 +000032#endif
33#endif
34
Guido van Rossum407a22d1997-08-13 19:57:53 +000035// Force the malloc heap to clean itself up, and free unused blocks
36// back to the OS. (According to the docs, only works on NT.)
Tim Peters5fa0bd62000-12-12 01:58:56 +000037static PyObject *
38msvcrt_heapmin(PyObject *self, PyObject *args)
Guido van Rossum29c1ea51997-08-07 00:11:34 +000039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 if (!PyArg_ParseTuple(args, ":heapmin"))
41 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +000042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 if (_heapmin() != 0)
44 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +000045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 Py_INCREF(Py_None);
47 return Py_None;
Guido van Rossum29c1ea51997-08-07 00:11:34 +000048}
49
Benjamin Peterson1baf4652009-12-31 03:11:23 +000050PyDoc_STRVAR(heapmin_doc,
51"heapmin() -> None\n\
52\n\
53Force the malloc() heap to clean itself up and return unused blocks\n\
54to the operating system. On failure, this raises IOError.");
55
Guido van Rossum407a22d1997-08-13 19:57:53 +000056// Perform locking operations on a C runtime file descriptor.
Tim Peters5fa0bd62000-12-12 01:58:56 +000057static PyObject *
58msvcrt_locking(PyObject *self, PyObject *args)
Guido van Rossum29c1ea51997-08-07 00:11:34 +000059{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 int fd;
61 int mode;
62 long nbytes;
63 int err;
Guido van Rossum407a22d1997-08-13 19:57:53 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes))
66 return NULL;
Guido van Rossum29c1ea51997-08-07 00:11:34 +000067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 Py_BEGIN_ALLOW_THREADS
69 err = _locking(fd, mode, nbytes);
70 Py_END_ALLOW_THREADS
71 if (err != 0)
72 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum29c1ea51997-08-07 00:11:34 +000073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 Py_INCREF(Py_None);
75 return Py_None;
Guido van Rossum29c1ea51997-08-07 00:11:34 +000076}
Guido van Rossum407a22d1997-08-13 19:57:53 +000077
Benjamin Peterson1baf4652009-12-31 03:11:23 +000078PyDoc_STRVAR(locking_doc,
79"locking(fd, mode, nbytes) -> None\n\
80\n\
81Lock part of a file based on file descriptor fd from the C runtime.\n\
82Raises IOError on failure. The locked region of the file extends from\n\
83the current file position for nbytes bytes, and may continue beyond\n\
84the end of the file. mode must be one of the LK_* constants listed\n\
85below. Multiple regions in a file may be locked at the same time, but\n\
86may not overlap. Adjacent regions are not merged; they must be unlocked\n\
87individually.");
88
Guido van Rossum407a22d1997-08-13 19:57:53 +000089// Set the file translation mode for a C runtime file descriptor.
Tim Peters5fa0bd62000-12-12 01:58:56 +000090static PyObject *
91msvcrt_setmode(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +000092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 int fd;
94 int flags;
95 if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags))
96 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +000097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 flags = _setmode(fd, flags);
99 if (flags == -1)
100 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 return PyLong_FromLong(flags);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000103}
104
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000105PyDoc_STRVAR(setmode_doc,
106"setmode(fd, mode) -> Previous mode\n\
107\n\
108Set the line-end translation mode for the file descriptor fd. To set\n\
109it to text mode, flags should be os.O_TEXT; for binary, it should be\n\
110os.O_BINARY.");
111
Guido van Rossum407a22d1997-08-13 19:57:53 +0000112// Convert an OS file handle to a C runtime file descriptor.
Tim Peters5fa0bd62000-12-12 01:58:56 +0000113static PyObject *
114msvcrt_open_osfhandle(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000115{
Richard Oudkerkac0ad882013-06-05 23:29:30 +0100116 Py_intptr_t handle;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 int flags;
118 int fd;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000119
Richard Oudkerkac0ad882013-06-05 23:29:30 +0100120 if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR "i:open_osfhandle",
121 &handle, &flags))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 fd = _open_osfhandle(handle, flags);
125 if (fd == -1)
126 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 return PyLong_FromLong(fd);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000129}
130
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000131PyDoc_STRVAR(open_osfhandle_doc,
132"open_osfhandle(handle, flags) -> file descriptor\n\
133\n\
134Create a C runtime file descriptor from the file handle handle. The\n\
135flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY,\n\
136and os.O_TEXT. The returned file descriptor may be used as a parameter\n\
137to os.fdopen() to create a file object.");
138
Guido van Rossum407a22d1997-08-13 19:57:53 +0000139// Convert a C runtime file descriptor to an OS file handle.
Tim Peters5fa0bd62000-12-12 01:58:56 +0000140static PyObject *
141msvcrt_get_osfhandle(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 int fd;
144 Py_intptr_t handle;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
147 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000148
Antoine Pitrou00492492010-09-04 20:53:29 +0000149 if (!_PyVerify_fd(fd))
150 return PyErr_SetFromErrno(PyExc_IOError);
151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 handle = _get_osfhandle(fd);
153 if (handle == -1)
154 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 /* technically 'handle' is not a pointer, but a integer as
157 large as a pointer, Python's *VoidPtr interface is the
158 most appropriate here */
159 return PyLong_FromVoidPtr((void*)handle);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000160}
161
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000162PyDoc_STRVAR(get_osfhandle_doc,
163"get_osfhandle(fd) -> file handle\n\
164\n\
165Return the file handle for the file descriptor fd. Raises IOError\n\
166if fd is not recognized.");
167
Guido van Rossum407a22d1997-08-13 19:57:53 +0000168/* Console I/O */
Guido van Rossum407a22d1997-08-13 19:57:53 +0000169
Tim Peters5fa0bd62000-12-12 01:58:56 +0000170static PyObject *
171msvcrt_kbhit(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 int ok;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 if (!PyArg_ParseTuple(args, ":kbhit"))
176 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 ok = _kbhit();
179 return PyLong_FromLong(ok);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000180}
181
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000182PyDoc_STRVAR(kbhit_doc,
183"kbhit() -> bool\n\
184\n\
185Return true if a keypress is waiting to be read.");
186
Tim Peters5fa0bd62000-12-12 01:58:56 +0000187static PyObject *
188msvcrt_getch(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 int ch;
191 char s[1];
Guido van Rossum407a22d1997-08-13 19:57:53 +0000192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 if (!PyArg_ParseTuple(args, ":getch"))
194 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 Py_BEGIN_ALLOW_THREADS
197 ch = _getch();
198 Py_END_ALLOW_THREADS
199 s[0] = ch;
200 return PyBytes_FromStringAndSize(s, 1);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000201}
202
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000203PyDoc_STRVAR(getch_doc,
204"getch() -> key character\n\
205\n\
Brian Curtin8790a072010-08-24 05:20:30 +0000206Read a keypress and return the resulting character as a byte string.\n\
207Nothing is echoed to the console. This call will block if a keypress is\n\
208not already available, but will not wait for Enter to be pressed. If the\n\
209pressed key was a special function key, this will return '\\000' or\n\
210'\\xe0'; the next call will return the keycode. The Control-C keypress\n\
211cannot be read with this function.");
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000212
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +0000213#ifdef _WCONIO_DEFINED
Tim Peters5fa0bd62000-12-12 01:58:56 +0000214static PyObject *
Christian Heimes2f1019e2007-12-10 16:18:49 +0000215msvcrt_getwch(PyObject *self, PyObject *args)
216{
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100217 wchar_t ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 if (!PyArg_ParseTuple(args, ":getwch"))
220 return NULL;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 Py_BEGIN_ALLOW_THREADS
223 ch = _getwch();
224 Py_END_ALLOW_THREADS
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200225 return PyUnicode_FromOrdinal(ch);
Christian Heimes2f1019e2007-12-10 16:18:49 +0000226}
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000227
228PyDoc_STRVAR(getwch_doc,
229"getwch() -> Unicode key character\n\
230\n\
231Wide char variant of getch(), returning a Unicode value.");
Christian Heimesc36625b2008-01-04 13:33:00 +0000232#endif
Christian Heimes2f1019e2007-12-10 16:18:49 +0000233
234static PyObject *
Tim Peters5fa0bd62000-12-12 01:58:56 +0000235msvcrt_getche(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 int ch;
238 char s[1];
Guido van Rossum407a22d1997-08-13 19:57:53 +0000239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 if (!PyArg_ParseTuple(args, ":getche"))
241 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 Py_BEGIN_ALLOW_THREADS
244 ch = _getche();
245 Py_END_ALLOW_THREADS
246 s[0] = ch;
247 return PyBytes_FromStringAndSize(s, 1);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000248}
249
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000250PyDoc_STRVAR(getche_doc,
251"getche() -> key character\n\
252\n\
253Similar to getch(), but the keypress will be echoed if it represents\n\
254a printable character.");
255
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +0000256#ifdef _WCONIO_DEFINED
Tim Peters5fa0bd62000-12-12 01:58:56 +0000257static PyObject *
Christian Heimes2f1019e2007-12-10 16:18:49 +0000258msvcrt_getwche(PyObject *self, PyObject *args)
259{
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100260 wchar_t ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 if (!PyArg_ParseTuple(args, ":getwche"))
263 return NULL;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 Py_BEGIN_ALLOW_THREADS
266 ch = _getwche();
267 Py_END_ALLOW_THREADS
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200268 return PyUnicode_FromOrdinal(ch);
Christian Heimes2f1019e2007-12-10 16:18:49 +0000269}
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000270
271PyDoc_STRVAR(getwche_doc,
272"getwche() -> Unicode key character\n\
273\n\
274Wide char variant of getche(), returning a Unicode value.");
Christian Heimesc36625b2008-01-04 13:33:00 +0000275#endif
Christian Heimes2f1019e2007-12-10 16:18:49 +0000276
277static PyObject *
Tim Peters5fa0bd62000-12-12 01:58:56 +0000278msvcrt_putch(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 char ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 if (!PyArg_ParseTuple(args, "c:putch", &ch))
283 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 _putch(ch);
286 Py_INCREF(Py_None);
287 return Py_None;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000288}
289
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000290PyDoc_STRVAR(putch_doc,
291"putch(char) -> None\n\
292\n\
Brian Curtin8790a072010-08-24 05:20:30 +0000293Print the byte string char to the console without buffering.");
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000294
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +0000295#ifdef _WCONIO_DEFINED
Christian Heimes2f1019e2007-12-10 16:18:49 +0000296static PyObject *
297msvcrt_putwch(PyObject *self, PyObject *args)
298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 int ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 if (!PyArg_ParseTuple(args, "C:putwch", &ch))
302 return NULL;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 _putwch(ch);
305 Py_RETURN_NONE;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000306
307}
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000308
309PyDoc_STRVAR(putwch_doc,
310"putwch(unicode_char) -> None\n\
311\n\
312Wide char variant of putch(), accepting a Unicode value.");
Christian Heimesc36625b2008-01-04 13:33:00 +0000313#endif
Christian Heimes2f1019e2007-12-10 16:18:49 +0000314
Tim Peters5fa0bd62000-12-12 01:58:56 +0000315static PyObject *
316msvcrt_ungetch(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 char ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
321 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 if (_ungetch(ch) == EOF)
324 return PyErr_SetFromErrno(PyExc_IOError);
325 Py_INCREF(Py_None);
326 return Py_None;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000327}
328
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000329PyDoc_STRVAR(ungetch_doc,
330"ungetch(char) -> None\n\
331\n\
Brian Curtin8790a072010-08-24 05:20:30 +0000332Cause the byte string char to be \"pushed back\" into the\n\
333console buffer; it will be the next character read by\n\
334getch() or getche().");
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000335
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 Pitrouf95a1b32010-05-09 15:52:27 +0000340 int ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 if (!PyArg_ParseTuple(args, "C:ungetwch", &ch))
343 return NULL;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Peterson1baf4652009-12-31 03:11:23 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000376 int type, file;
377 _HFILE res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000390 int type, mode;
391 int res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000404 int mode, res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000417 unsigned int mode, res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000469 int st;
Brian Curtin401f9f32012-05-13 11:19:23 -0500470 PyObject *d, *version;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +0000499 /* constants for the crt versions */
Brian Curtin401f9f32012-05-13 11:19:23 -0500500 (void)st;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000501#ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
503 _VC_ASSEMBLY_PUBLICKEYTOKEN);
504 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000505#endif
506#ifdef _CRT_ASSEMBLY_VERSION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
508 _CRT_ASSEMBLY_VERSION);
509 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000510#endif
511#ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
513 __LIBRARIES_ASSEMBLY_NAME_PREFIX);
514 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000515#endif
516
Brian Curtin401f9f32012-05-13 11:19:23 -0500517 /* constants for the 2010 crt versions */
518#if defined(_VC_CRT_MAJOR_VERSION) && defined (_VC_CRT_MINOR_VERSION) && defined(_VC_CRT_BUILD_VERSION) && defined(_VC_CRT_RBUILD_VERSION)
519 version = PyUnicode_FromFormat("%d.%d.%d.%d", _VC_CRT_MAJOR_VERSION,
520 _VC_CRT_MINOR_VERSION,
521 _VC_CRT_BUILD_VERSION,
522 _VC_CRT_RBUILD_VERSION);
523 st = PyModule_AddObject(m, "CRT_ASSEMBLY_VERSION", version);
524 if (st < 0) return NULL;
525#endif
526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 return m;
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000528}