blob: a6bd083827b7ecc7f46082bce8a5459ec40dd66d [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
Zachary Ware45520892015-05-13 01:22:32 -050035/*[python input]
36class Py_intptr_t_converter(CConverter):
37 type = 'Py_intptr_t'
38 format_unit = '"_Py_PARSE_INTPTR"'
Guido van Rossum407a22d1997-08-13 19:57:53 +000039
Zachary Ware45520892015-05-13 01:22:32 -050040class handle_return_converter(long_return_converter):
41 type = 'Py_intptr_t'
42 cast = '(void *)'
43 conversion_fn = 'PyLong_FromVoidPtr'
44
45class byte_char_return_converter(CReturnConverter):
46 type = 'int'
47
48 def render(self, function, data):
49 data.declarations.append('char s[1];')
50 data.return_value = 's[0]'
51 data.return_conversion.append(
52 'return_value = PyBytes_FromStringAndSize(s, 1);\n')
53
54class wchar_t_return_converter(CReturnConverter):
55 type = 'wchar_t'
56
57 def render(self, function, data):
58 self.declare(data)
59 data.return_conversion.append(
60 'return_value = PyUnicode_FromOrdinal(_return_value);\n')
61[python start generated code]*/
62/*[python end generated code: output=da39a3ee5e6b4b0d input=6a54fc4e73d0b367]*/
63
64/*[clinic input]
65module msvcrt
66[clinic start generated code]*/
67/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f31a87a783d036cd]*/
68
69#include "clinic/msvcrtmodule.c.h"
70
71/*[clinic input]
72msvcrt.heapmin
73
74Minimize the malloc() heap.
75
76Force the malloc() heap to clean itself up and return unused blocks
77to the operating system. On failure, this raises OSError.
78[clinic start generated code]*/
79
80static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030081msvcrt_heapmin_impl(PyObject *module)
82/*[clinic end generated code: output=1ba00f344782dc19 input=82e1771d21bde2d8]*/
Zachary Ware45520892015-05-13 01:22:32 -050083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 if (_heapmin() != 0)
85 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +000086
Zachary Ware45520892015-05-13 01:22:32 -050087 Py_RETURN_NONE;
Guido van Rossum29c1ea51997-08-07 00:11:34 +000088}
Zachary Ware45520892015-05-13 01:22:32 -050089/*[clinic input]
90msvcrt.locking
Guido van Rossum29c1ea51997-08-07 00:11:34 +000091
Zachary Ware45520892015-05-13 01:22:32 -050092 fd: int
93 mode: int
94 nbytes: long
95 /
Benjamin Peterson1baf4652009-12-31 03:11:23 +000096
Zachary Ware45520892015-05-13 01:22:32 -050097Lock part of a file based on file descriptor fd from the C runtime.
98
99Raises IOError on failure. The locked region of the file extends from
100the current file position for nbytes bytes, and may continue beyond
101the end of the file. mode must be one of the LK_* constants listed
102below. Multiple regions in a file may be locked at the same time, but
103may not overlap. Adjacent regions are not merged; they must be unlocked
104individually.
105[clinic start generated code]*/
106
Tim Peters5fa0bd62000-12-12 01:58:56 +0000107static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300108msvcrt_locking_impl(PyObject *module, int fd, int mode, long nbytes)
109/*[clinic end generated code: output=a4a90deca9785a03 input=d9f13f0f6a713ba7]*/
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 int err;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 Py_BEGIN_ALLOW_THREADS
114 err = _locking(fd, mode, nbytes);
115 Py_END_ALLOW_THREADS
116 if (err != 0)
117 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000118
Zachary Ware45520892015-05-13 01:22:32 -0500119 Py_RETURN_NONE;
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000120}
Guido van Rossum407a22d1997-08-13 19:57:53 +0000121
Zachary Ware45520892015-05-13 01:22:32 -0500122/*[clinic input]
123msvcrt.setmode -> long
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000124
Zachary Ware45520892015-05-13 01:22:32 -0500125 fd: int
126 mode as flags: int
127 /
128
129Set the line-end translation mode for the file descriptor fd.
130
131To set it to text mode, flags should be os.O_TEXT; for binary, it
132should be os.O_BINARY.
133
134Return value is the previous mode.
135[clinic start generated code]*/
136
137static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300138msvcrt_setmode_impl(PyObject *module, int fd, int flags)
139/*[clinic end generated code: output=24a9be5ea07ccb9b input=76e7c01f6b137f75]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000140{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 flags = _setmode(fd, flags);
142 if (flags == -1)
Zachary Ware45520892015-05-13 01:22:32 -0500143 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000144
Zachary Ware45520892015-05-13 01:22:32 -0500145 return flags;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000146}
147
Zachary Ware45520892015-05-13 01:22:32 -0500148/*[clinic input]
149msvcrt.open_osfhandle -> long
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000150
Zachary Ware45520892015-05-13 01:22:32 -0500151 handle: Py_intptr_t
152 flags: int
153 /
154
155Create a C runtime file descriptor from the file handle handle.
156
157The flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY,
158and os.O_TEXT. The returned file descriptor may be used as a parameter
159to os.fdopen() to create a file object.
160[clinic start generated code]*/
161
162static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300163msvcrt_open_osfhandle_impl(PyObject *module, Py_intptr_t handle, int flags)
164/*[clinic end generated code: output=bf65e422243a39f9 input=4d8516ed32db8f65]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 int fd;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 fd = _open_osfhandle(handle, flags);
169 if (fd == -1)
Zachary Ware45520892015-05-13 01:22:32 -0500170 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000171
Zachary Ware45520892015-05-13 01:22:32 -0500172 return fd;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000173}
174
Zachary Ware45520892015-05-13 01:22:32 -0500175/*[clinic input]
176msvcrt.get_osfhandle -> handle
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000177
Zachary Ware45520892015-05-13 01:22:32 -0500178 fd: int
179 /
180
181Return the file handle for the file descriptor fd.
182
183Raises IOError if fd is not recognized.
184[clinic start generated code]*/
185
186static Py_intptr_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300187msvcrt_get_osfhandle_impl(PyObject *module, int fd)
188/*[clinic end generated code: output=eac47643338c0baa input=c7d18d02c8017ec1]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000189{
Zachary Ware45520892015-05-13 01:22:32 -0500190 Py_intptr_t handle = -1;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000191
Zachary Ware45520892015-05-13 01:22:32 -0500192 if (!_PyVerify_fd(fd)) {
193 PyErr_SetFromErrno(PyExc_IOError);
194 }
195 else {
Steve Dower8fc89802015-04-12 00:26:27 -0400196 _Py_BEGIN_SUPPRESS_IPH
Zachary Ware45520892015-05-13 01:22:32 -0500197 handle = _get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -0400198 _Py_END_SUPPRESS_IPH
Zachary Ware45520892015-05-13 01:22:32 -0500199 if (handle == -1)
200 PyErr_SetFromErrno(PyExc_IOError);
201 }
Guido van Rossum407a22d1997-08-13 19:57:53 +0000202
Zachary Ware45520892015-05-13 01:22:32 -0500203 return handle;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000204}
205
206/* Console I/O */
Zachary Ware45520892015-05-13 01:22:32 -0500207/*[clinic input]
208msvcrt.kbhit -> long
Guido van Rossum407a22d1997-08-13 19:57:53 +0000209
Zachary Ware45520892015-05-13 01:22:32 -0500210Return true if a keypress is waiting to be read.
211[clinic start generated code]*/
212
213static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300214msvcrt_kbhit_impl(PyObject *module)
215/*[clinic end generated code: output=940dfce6587c1890 input=e70d678a5c2f6acc]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000216{
Zachary Ware45520892015-05-13 01:22:32 -0500217 return _kbhit();
Guido van Rossum407a22d1997-08-13 19:57:53 +0000218}
219
Zachary Ware45520892015-05-13 01:22:32 -0500220/*[clinic input]
221msvcrt.getch -> byte_char
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000222
Zachary Ware45520892015-05-13 01:22:32 -0500223Read a keypress and return the resulting character as a byte string.
224
225Nothing is echoed to the console. This call will block if a keypress is
226not already available, but will not wait for Enter to be pressed. If the
227pressed key was a special function key, this will return '\000' or
228'\xe0'; the next call will return the keycode. The Control-C keypress
229cannot be read with this function.
230[clinic start generated code]*/
231
232static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300233msvcrt_getch_impl(PyObject *module)
234/*[clinic end generated code: output=a4e51f0565064a7d input=37a40cf0ed0d1153]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 int ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 Py_BEGIN_ALLOW_THREADS
239 ch = _getch();
240 Py_END_ALLOW_THREADS
Zachary Ware45520892015-05-13 01:22:32 -0500241 return ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000242}
243
Zachary Ware45520892015-05-13 01:22:32 -0500244/*[clinic input]
245msvcrt.getwch -> wchar_t
246
247Wide char variant of getch(), returning a Unicode value.
248[clinic start generated code]*/
249
250static wchar_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300251msvcrt_getwch_impl(PyObject *module)
252/*[clinic end generated code: output=be9937494e22f007 input=27b3dec8ad823d7c]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000253{
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100254 wchar_t ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 Py_BEGIN_ALLOW_THREADS
257 ch = _getwch();
258 Py_END_ALLOW_THREADS
Zachary Ware45520892015-05-13 01:22:32 -0500259 return ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000260}
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000261
Zachary Ware45520892015-05-13 01:22:32 -0500262/*[clinic input]
263msvcrt.getche -> byte_char
Christian Heimes2f1019e2007-12-10 16:18:49 +0000264
Zachary Ware45520892015-05-13 01:22:32 -0500265Similar to getch(), but the keypress will be echoed if possible.
266[clinic start generated code]*/
267
268static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300269msvcrt_getche_impl(PyObject *module)
270/*[clinic end generated code: output=d8f7db4fd2990401 input=43311ade9ed4a9c0]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 int ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 Py_BEGIN_ALLOW_THREADS
275 ch = _getche();
276 Py_END_ALLOW_THREADS
Zachary Ware45520892015-05-13 01:22:32 -0500277 return ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000278}
279
Zachary Ware45520892015-05-13 01:22:32 -0500280/*[clinic input]
281msvcrt.getwche -> wchar_t
282
283Wide char variant of getche(), returning a Unicode value.
284[clinic start generated code]*/
285
286static wchar_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300287msvcrt_getwche_impl(PyObject *module)
288/*[clinic end generated code: output=d0dae5ba3829d596 input=49337d59d1a591f8]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000289{
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100290 wchar_t ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 Py_BEGIN_ALLOW_THREADS
293 ch = _getwche();
294 Py_END_ALLOW_THREADS
Zachary Ware45520892015-05-13 01:22:32 -0500295 return ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000296}
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000297
Zachary Ware45520892015-05-13 01:22:32 -0500298/*[clinic input]
299msvcrt.putch
300
301 char: char
302 /
303
304Print the byte string char to the console without buffering.
305[clinic start generated code]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000306
307static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300308msvcrt_putch_impl(PyObject *module, char char_value)
309/*[clinic end generated code: output=92ec9b81012d8f60 input=ec078dd10cb054d6]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000310{
Zachary Ware45520892015-05-13 01:22:32 -0500311 _putch(char_value);
312 Py_RETURN_NONE;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000313}
314
Zachary Ware45520892015-05-13 01:22:32 -0500315/*[clinic input]
316msvcrt.putwch
317
Zachary Ware77772c02015-05-13 10:58:35 -0500318 unicode_char: int(accept={str})
Zachary Ware45520892015-05-13 01:22:32 -0500319 /
320
321Wide char variant of putch(), accepting a Unicode value.
322[clinic start generated code]*/
323
Christian Heimes2f1019e2007-12-10 16:18:49 +0000324static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300325msvcrt_putwch_impl(PyObject *module, int unicode_char)
326/*[clinic end generated code: output=a3bd1a8951d28eee input=996ccd0bbcbac4c3]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000327{
Zachary Ware45520892015-05-13 01:22:32 -0500328 _putwch(unicode_char);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 Py_RETURN_NONE;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000330
331}
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000332
Zachary Ware45520892015-05-13 01:22:32 -0500333/*[clinic input]
334msvcrt.ungetch
335
336 char: char
337 /
338
339Opposite of getch.
340
341Cause the byte string char to be "pushed back" into the
342console buffer; it will be the next character read by
343getch() or getche().
344[clinic start generated code]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000345
Tim Peters5fa0bd62000-12-12 01:58:56 +0000346static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300347msvcrt_ungetch_impl(PyObject *module, char char_value)
348/*[clinic end generated code: output=c6942a0efa119000 input=22f07ee9001bbf0f]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000349{
Zachary Ware45520892015-05-13 01:22:32 -0500350 if (_ungetch(char_value) == EOF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 return PyErr_SetFromErrno(PyExc_IOError);
Zachary Ware45520892015-05-13 01:22:32 -0500352 Py_RETURN_NONE;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000353}
354
Zachary Ware45520892015-05-13 01:22:32 -0500355/*[clinic input]
356msvcrt.ungetwch
357
Zachary Ware77772c02015-05-13 10:58:35 -0500358 unicode_char: int(accept={str})
Zachary Ware45520892015-05-13 01:22:32 -0500359 /
360
361Wide char variant of ungetch(), accepting a Unicode value.
362[clinic start generated code]*/
363
Christian Heimes2f1019e2007-12-10 16:18:49 +0000364static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300365msvcrt_ungetwch_impl(PyObject *module, int unicode_char)
366/*[clinic end generated code: output=e63af05438b8ba3d input=83ec0492be04d564]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000367{
Zachary Ware45520892015-05-13 01:22:32 -0500368 if (_ungetwch(unicode_char) == WEOF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 return PyErr_SetFromErrno(PyExc_IOError);
Zachary Ware45520892015-05-13 01:22:32 -0500370 Py_RETURN_NONE;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000371}
Tim Peters5fa0bd62000-12-12 01:58:56 +0000372
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000373#ifdef _DEBUG
Zachary Ware45520892015-05-13 01:22:32 -0500374/*[clinic input]
375msvcrt.CrtSetReportFile -> long
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000376
Zachary Ware45520892015-05-13 01:22:32 -0500377 type: int
378 file: int
379 /
380
381Wrapper around _CrtSetReportFile.
382
383Only available on Debug builds.
384[clinic start generated code]*/
385
386static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300387msvcrt_CrtSetReportFile_impl(PyObject *module, int type, int file)
388/*[clinic end generated code: output=df291c7fe032eb68 input=bb8f721a604fcc45]*/
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000389{
Zachary Ware45520892015-05-13 01:22:32 -0500390 return (long)_CrtSetReportFile(type, (_HFILE)file);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000391}
392
Zachary Ware45520892015-05-13 01:22:32 -0500393/*[clinic input]
394msvcrt.CrtSetReportMode -> long
395
396 type: int
397 mode: int
398 /
399
400Wrapper around _CrtSetReportMode.
401
402Only available on Debug builds.
403[clinic start generated code]*/
404
405static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300406msvcrt_CrtSetReportMode_impl(PyObject *module, int type, int mode)
407/*[clinic end generated code: output=b2863761523de317 input=9319d29b4319426b]*/
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 int res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 res = _CrtSetReportMode(type, mode);
412 if (res == -1)
Zachary Ware45520892015-05-13 01:22:32 -0500413 PyErr_SetFromErrno(PyExc_IOError);
414 return res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000415}
416
Zachary Ware45520892015-05-13 01:22:32 -0500417/*[clinic input]
418msvcrt.set_error_mode -> long
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000419
Zachary Ware45520892015-05-13 01:22:32 -0500420 mode: int
421 /
422
423Wrapper around _set_error_mode.
424
425Only available on Debug builds.
426[clinic start generated code]*/
427
428static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300429msvcrt_set_error_mode_impl(PyObject *module, int mode)
430/*[clinic end generated code: output=ac4a09040d8ac4e3 input=046fca59c0f20872]*/
Zachary Ware45520892015-05-13 01:22:32 -0500431{
432 return _set_error_mode(mode);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000433}
Zachary Ware45520892015-05-13 01:22:32 -0500434#endif /* _DEBUG */
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000435
Zachary Ware45520892015-05-13 01:22:32 -0500436/*[clinic input]
437msvcrt.SetErrorMode
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000438
Zachary Ware45520892015-05-13 01:22:32 -0500439 mode: unsigned_int(bitwise=True)
440 /
441
442Wrapper around SetErrorMode.
443[clinic start generated code]*/
444
445static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300446msvcrt_SetErrorMode_impl(PyObject *module, unsigned int mode)
447/*[clinic end generated code: output=01d529293f00da8f input=d8b167258d32d907]*/
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000448{
Zachary Ware45520892015-05-13 01:22:32 -0500449 unsigned int res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 res = SetErrorMode(mode);
452 return PyLong_FromUnsignedLong(res);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000453}
454
Zachary Ware45520892015-05-13 01:22:32 -0500455/*[clinic input]
456[clinic start generated code]*/
457/*[clinic end generated code: output=da39a3ee5e6b4b0d input=da39a3ee5e6b4b0d]*/
Tim Peters5fa0bd62000-12-12 01:58:56 +0000458
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000459/* List of functions exported by this module */
460static struct PyMethodDef msvcrt_functions[] = {
Zachary Ware45520892015-05-13 01:22:32 -0500461 MSVCRT_HEAPMIN_METHODDEF
462 MSVCRT_LOCKING_METHODDEF
463 MSVCRT_SETMODE_METHODDEF
464 MSVCRT_OPEN_OSFHANDLE_METHODDEF
465 MSVCRT_GET_OSFHANDLE_METHODDEF
466 MSVCRT_KBHIT_METHODDEF
467 MSVCRT_GETCH_METHODDEF
468 MSVCRT_GETCHE_METHODDEF
469 MSVCRT_PUTCH_METHODDEF
470 MSVCRT_UNGETCH_METHODDEF
471 MSVCRT_SETERRORMODE_METHODDEF
472 MSVCRT_CRTSETREPORTFILE_METHODDEF
473 MSVCRT_CRTSETREPORTMODE_METHODDEF
474 MSVCRT_SET_ERROR_MODE_METHODDEF
475 MSVCRT_GETWCH_METHODDEF
476 MSVCRT_GETWCHE_METHODDEF
477 MSVCRT_PUTWCH_METHODDEF
478 MSVCRT_UNGETWCH_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 {NULL, NULL}
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000480};
481
Martin v. Löwis1a214512008-06-11 05:26:20 +0000482
483static struct PyModuleDef msvcrtmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 PyModuleDef_HEAD_INIT,
485 "msvcrt",
486 NULL,
487 -1,
488 msvcrt_functions,
489 NULL,
490 NULL,
491 NULL,
492 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000493};
494
Zachary Ware45520892015-05-13 01:22:32 -0500495static void
496insertint(PyObject *d, char *name, int value)
497{
498 PyObject *v = PyLong_FromLong((long) value);
499 if (v == NULL) {
500 /* Don't bother reporting this error */
501 PyErr_Clear();
502 }
503 else {
504 PyDict_SetItemString(d, name, v);
505 Py_DECREF(v);
506 }
507}
508
Thomas Hellera18331d2004-07-28 20:02:52 +0000509PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000510PyInit_msvcrt(void)
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 int st;
Brian Curtin401f9f32012-05-13 11:19:23 -0500513 PyObject *d, *version;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 PyObject *m = PyModule_Create(&msvcrtmodule);
515 if (m == NULL)
516 return NULL;
517 d = PyModule_GetDict(m);
Tim Peters5fa0bd62000-12-12 01:58:56 +0000518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 /* constants for the locking() function's mode argument */
520 insertint(d, "LK_LOCK", _LK_LOCK);
521 insertint(d, "LK_NBLCK", _LK_NBLCK);
522 insertint(d, "LK_NBRLCK", _LK_NBRLCK);
523 insertint(d, "LK_RLCK", _LK_RLCK);
524 insertint(d, "LK_UNLCK", _LK_UNLCK);
525 insertint(d, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS);
526 insertint(d, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT);
527 insertint(d, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX);
528 insertint(d, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000529#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 insertint(d, "CRT_WARN", _CRT_WARN);
531 insertint(d, "CRT_ERROR", _CRT_ERROR);
532 insertint(d, "CRT_ASSERT", _CRT_ASSERT);
533 insertint(d, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG);
534 insertint(d, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE);
535 insertint(d, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW);
536 insertint(d, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE);
537 insertint(d, "CRTDBG_FILE_STDERR", (int)_CRTDBG_FILE_STDERR);
538 insertint(d, "CRTDBG_FILE_STDOUT", (int)_CRTDBG_FILE_STDOUT);
539 insertint(d, "CRTDBG_REPORT_FILE", (int)_CRTDBG_REPORT_FILE);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000540#endif
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 /* constants for the crt versions */
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000543#ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
545 _VC_ASSEMBLY_PUBLICKEYTOKEN);
546 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000547#endif
548#ifdef _CRT_ASSEMBLY_VERSION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
550 _CRT_ASSEMBLY_VERSION);
551 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000552#endif
553#ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
555 __LIBRARIES_ASSEMBLY_NAME_PREFIX);
556 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000557#endif
558
Brian Curtin401f9f32012-05-13 11:19:23 -0500559 /* constants for the 2010 crt versions */
560#if defined(_VC_CRT_MAJOR_VERSION) && defined (_VC_CRT_MINOR_VERSION) && defined(_VC_CRT_BUILD_VERSION) && defined(_VC_CRT_RBUILD_VERSION)
561 version = PyUnicode_FromFormat("%d.%d.%d.%d", _VC_CRT_MAJOR_VERSION,
562 _VC_CRT_MINOR_VERSION,
563 _VC_CRT_BUILD_VERSION,
564 _VC_CRT_RBUILD_VERSION);
565 st = PyModule_AddObject(m, "CRT_ASSEMBLY_VERSION", version);
566 if (st < 0) return NULL;
567#endif
Victor Stinnerba452952015-09-21 22:40:28 +0200568 /* make compiler warning quiet if st is unused */
569 (void)st;
Brian Curtin401f9f32012-05-13 11:19:23 -0500570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 return m;
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000572}