blob: 52d4100759280e3bac997f24dea0da739a29d1d2 [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 *
81msvcrt_heapmin_impl(PyModuleDef *module)
82/*[clinic end generated code: output=464f866feb57c436 input=82e1771d21bde2d8]*/
83{
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 *
Zachary Ware45520892015-05-13 01:22:32 -0500108msvcrt_locking_impl(PyModuleDef *module, int fd, int mode, long nbytes)
109/*[clinic end generated code: output=dff41e5e76d544de 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
138msvcrt_setmode_impl(PyModuleDef *module, int fd, int flags)
139/*[clinic end generated code: output=8c84e5b37c586d0d 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
Zachary Ware77772c02015-05-13 10:58:35 -0500163msvcrt_open_osfhandle_impl(PyModuleDef *module, Py_intptr_t handle,
164 int flags)
165/*[clinic end generated code: output=86bce32582c49c06 input=4d8516ed32db8f65]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 int fd;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 fd = _open_osfhandle(handle, flags);
170 if (fd == -1)
Zachary Ware45520892015-05-13 01:22:32 -0500171 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000172
Zachary Ware45520892015-05-13 01:22:32 -0500173 return fd;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000174}
175
Zachary Ware45520892015-05-13 01:22:32 -0500176/*[clinic input]
177msvcrt.get_osfhandle -> handle
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000178
Zachary Ware45520892015-05-13 01:22:32 -0500179 fd: int
180 /
181
182Return the file handle for the file descriptor fd.
183
184Raises IOError if fd is not recognized.
185[clinic start generated code]*/
186
187static Py_intptr_t
188msvcrt_get_osfhandle_impl(PyModuleDef *module, int fd)
189/*[clinic end generated code: output=376bff52586b55a6 input=c7d18d02c8017ec1]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000190{
Zachary Ware45520892015-05-13 01:22:32 -0500191 Py_intptr_t handle = -1;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000192
Zachary Ware45520892015-05-13 01:22:32 -0500193 if (!_PyVerify_fd(fd)) {
194 PyErr_SetFromErrno(PyExc_IOError);
195 }
196 else {
Steve Dower8fc89802015-04-12 00:26:27 -0400197 _Py_BEGIN_SUPPRESS_IPH
Zachary Ware45520892015-05-13 01:22:32 -0500198 handle = _get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -0400199 _Py_END_SUPPRESS_IPH
Zachary Ware45520892015-05-13 01:22:32 -0500200 if (handle == -1)
201 PyErr_SetFromErrno(PyExc_IOError);
202 }
Guido van Rossum407a22d1997-08-13 19:57:53 +0000203
Zachary Ware45520892015-05-13 01:22:32 -0500204 return handle;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000205}
206
207/* Console I/O */
Zachary Ware45520892015-05-13 01:22:32 -0500208/*[clinic input]
209msvcrt.kbhit -> long
Guido van Rossum407a22d1997-08-13 19:57:53 +0000210
Zachary Ware45520892015-05-13 01:22:32 -0500211Return true if a keypress is waiting to be read.
212[clinic start generated code]*/
213
214static long
215msvcrt_kbhit_impl(PyModuleDef *module)
216/*[clinic end generated code: output=2b7293fcbe5cb24e input=e70d678a5c2f6acc]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000217{
Zachary Ware45520892015-05-13 01:22:32 -0500218 return _kbhit();
Guido van Rossum407a22d1997-08-13 19:57:53 +0000219}
220
Zachary Ware45520892015-05-13 01:22:32 -0500221/*[clinic input]
222msvcrt.getch -> byte_char
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000223
Zachary Ware45520892015-05-13 01:22:32 -0500224Read a keypress and return the resulting character as a byte string.
225
226Nothing is echoed to the console. This call will block if a keypress is
227not already available, but will not wait for Enter to be pressed. If the
228pressed key was a special function key, this will return '\000' or
229'\xe0'; the next call will return the keycode. The Control-C keypress
230cannot be read with this function.
231[clinic start generated code]*/
232
233static int
234msvcrt_getch_impl(PyModuleDef *module)
235/*[clinic end generated code: output=199e3d89f49c166a input=37a40cf0ed0d1153]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 int ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 Py_BEGIN_ALLOW_THREADS
240 ch = _getch();
241 Py_END_ALLOW_THREADS
Zachary Ware45520892015-05-13 01:22:32 -0500242 return ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000243}
244
Zachary Ware45520892015-05-13 01:22:32 -0500245/*[clinic input]
246msvcrt.getwch -> wchar_t
247
248Wide char variant of getch(), returning a Unicode value.
249[clinic start generated code]*/
250
251static wchar_t
252msvcrt_getwch_impl(PyModuleDef *module)
253/*[clinic end generated code: output=9d3762861328b1fe input=27b3dec8ad823d7c]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000254{
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100255 wchar_t ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 Py_BEGIN_ALLOW_THREADS
258 ch = _getwch();
259 Py_END_ALLOW_THREADS
Zachary Ware45520892015-05-13 01:22:32 -0500260 return ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000261}
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000262
Zachary Ware45520892015-05-13 01:22:32 -0500263/*[clinic input]
264msvcrt.getche -> byte_char
Christian Heimes2f1019e2007-12-10 16:18:49 +0000265
Zachary Ware45520892015-05-13 01:22:32 -0500266Similar to getch(), but the keypress will be echoed if possible.
267[clinic start generated code]*/
268
269static int
270msvcrt_getche_impl(PyModuleDef *module)
271/*[clinic end generated code: output=8aa369be6550068e input=43311ade9ed4a9c0]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 int ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 Py_BEGIN_ALLOW_THREADS
276 ch = _getche();
277 Py_END_ALLOW_THREADS
Zachary Ware45520892015-05-13 01:22:32 -0500278 return ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000279}
280
Zachary Ware45520892015-05-13 01:22:32 -0500281/*[clinic input]
282msvcrt.getwche -> wchar_t
283
284Wide char variant of getche(), returning a Unicode value.
285[clinic start generated code]*/
286
287static wchar_t
288msvcrt_getwche_impl(PyModuleDef *module)
289/*[clinic end generated code: output=3693cf78e3ea0cf6 input=49337d59d1a591f8]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000290{
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100291 wchar_t ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 Py_BEGIN_ALLOW_THREADS
294 ch = _getwche();
295 Py_END_ALLOW_THREADS
Zachary Ware45520892015-05-13 01:22:32 -0500296 return ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000297}
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000298
Zachary Ware45520892015-05-13 01:22:32 -0500299/*[clinic input]
300msvcrt.putch
301
302 char: char
303 /
304
305Print the byte string char to the console without buffering.
306[clinic start generated code]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000307
308static PyObject *
Zachary Ware45520892015-05-13 01:22:32 -0500309msvcrt_putch_impl(PyModuleDef *module, char char_value)
310/*[clinic end generated code: output=c05548b11554f36f input=ec078dd10cb054d6]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000311{
Zachary Ware45520892015-05-13 01:22:32 -0500312 _putch(char_value);
313 Py_RETURN_NONE;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000314}
315
Zachary Ware45520892015-05-13 01:22:32 -0500316/*[clinic input]
317msvcrt.putwch
318
Zachary Ware77772c02015-05-13 10:58:35 -0500319 unicode_char: int(accept={str})
Zachary Ware45520892015-05-13 01:22:32 -0500320 /
321
322Wide char variant of putch(), accepting a Unicode value.
323[clinic start generated code]*/
324
Christian Heimes2f1019e2007-12-10 16:18:49 +0000325static PyObject *
Zachary Ware45520892015-05-13 01:22:32 -0500326msvcrt_putwch_impl(PyModuleDef *module, int unicode_char)
Zachary Ware77772c02015-05-13 10:58:35 -0500327/*[clinic end generated code: output=c216a73694ca73dd input=996ccd0bbcbac4c3]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000328{
Zachary Ware45520892015-05-13 01:22:32 -0500329 _putwch(unicode_char);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 Py_RETURN_NONE;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000331
332}
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000333
Zachary Ware45520892015-05-13 01:22:32 -0500334/*[clinic input]
335msvcrt.ungetch
336
337 char: char
338 /
339
340Opposite of getch.
341
342Cause the byte string char to be "pushed back" into the
343console buffer; it will be the next character read by
344getch() or getche().
345[clinic start generated code]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000346
Tim Peters5fa0bd62000-12-12 01:58:56 +0000347static PyObject *
Zachary Ware45520892015-05-13 01:22:32 -0500348msvcrt_ungetch_impl(PyModuleDef *module, char char_value)
349/*[clinic end generated code: output=19a4cd3249709ec9 input=22f07ee9001bbf0f]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000350{
Zachary Ware45520892015-05-13 01:22:32 -0500351 if (_ungetch(char_value) == EOF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 return PyErr_SetFromErrno(PyExc_IOError);
Zachary Ware45520892015-05-13 01:22:32 -0500353 Py_RETURN_NONE;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000354}
355
Zachary Ware45520892015-05-13 01:22:32 -0500356/*[clinic input]
357msvcrt.ungetwch
358
Zachary Ware77772c02015-05-13 10:58:35 -0500359 unicode_char: int(accept={str})
Zachary Ware45520892015-05-13 01:22:32 -0500360 /
361
362Wide char variant of ungetch(), accepting a Unicode value.
363[clinic start generated code]*/
364
Christian Heimes2f1019e2007-12-10 16:18:49 +0000365static PyObject *
Zachary Ware45520892015-05-13 01:22:32 -0500366msvcrt_ungetwch_impl(PyModuleDef *module, int unicode_char)
Zachary Ware77772c02015-05-13 10:58:35 -0500367/*[clinic end generated code: output=1ee7674710322bd1 input=83ec0492be04d564]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000368{
Zachary Ware45520892015-05-13 01:22:32 -0500369 if (_ungetwch(unicode_char) == WEOF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 return PyErr_SetFromErrno(PyExc_IOError);
Zachary Ware45520892015-05-13 01:22:32 -0500371 Py_RETURN_NONE;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000372}
Tim Peters5fa0bd62000-12-12 01:58:56 +0000373
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000374#ifdef _DEBUG
Zachary Ware45520892015-05-13 01:22:32 -0500375/*[clinic input]
376msvcrt.CrtSetReportFile -> long
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000377
Zachary Ware45520892015-05-13 01:22:32 -0500378 type: int
379 file: int
380 /
381
382Wrapper around _CrtSetReportFile.
383
384Only available on Debug builds.
385[clinic start generated code]*/
386
387static long
388msvcrt_CrtSetReportFile_impl(PyModuleDef *module, int type, int file)
389/*[clinic end generated code: output=8c3644fb2edfa808 input=bb8f721a604fcc45]*/
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000390{
Zachary Ware45520892015-05-13 01:22:32 -0500391 return (long)_CrtSetReportFile(type, (_HFILE)file);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000392}
393
Zachary Ware45520892015-05-13 01:22:32 -0500394/*[clinic input]
395msvcrt.CrtSetReportMode -> long
396
397 type: int
398 mode: int
399 /
400
401Wrapper around _CrtSetReportMode.
402
403Only available on Debug builds.
404[clinic start generated code]*/
405
406static long
407msvcrt_CrtSetReportMode_impl(PyModuleDef *module, int type, int mode)
408/*[clinic end generated code: output=b407fbf8716a52b9 input=9319d29b4319426b]*/
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 int res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 res = _CrtSetReportMode(type, mode);
413 if (res == -1)
Zachary Ware45520892015-05-13 01:22:32 -0500414 PyErr_SetFromErrno(PyExc_IOError);
415 return res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000416}
417
Zachary Ware45520892015-05-13 01:22:32 -0500418/*[clinic input]
419msvcrt.set_error_mode -> long
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000420
Zachary Ware45520892015-05-13 01:22:32 -0500421 mode: int
422 /
423
424Wrapper around _set_error_mode.
425
426Only available on Debug builds.
427[clinic start generated code]*/
428
429static long
430msvcrt_set_error_mode_impl(PyModuleDef *module, int mode)
431/*[clinic end generated code: output=62148adffa90867d input=046fca59c0f20872]*/
432{
433 return _set_error_mode(mode);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000434}
Zachary Ware45520892015-05-13 01:22:32 -0500435#endif /* _DEBUG */
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000436
Zachary Ware45520892015-05-13 01:22:32 -0500437/*[clinic input]
438msvcrt.SetErrorMode
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000439
Zachary Ware45520892015-05-13 01:22:32 -0500440 mode: unsigned_int(bitwise=True)
441 /
442
443Wrapper around SetErrorMode.
444[clinic start generated code]*/
445
446static PyObject *
447msvcrt_SetErrorMode_impl(PyModuleDef *module, unsigned int mode)
448/*[clinic end generated code: output=544c60b085be79c6 input=d8b167258d32d907]*/
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000449{
Zachary Ware45520892015-05-13 01:22:32 -0500450 unsigned int res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 res = SetErrorMode(mode);
453 return PyLong_FromUnsignedLong(res);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000454}
455
Zachary Ware45520892015-05-13 01:22:32 -0500456/*[clinic input]
457[clinic start generated code]*/
458/*[clinic end generated code: output=da39a3ee5e6b4b0d input=da39a3ee5e6b4b0d]*/
Tim Peters5fa0bd62000-12-12 01:58:56 +0000459
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000460/* List of functions exported by this module */
461static struct PyMethodDef msvcrt_functions[] = {
Zachary Ware45520892015-05-13 01:22:32 -0500462 MSVCRT_HEAPMIN_METHODDEF
463 MSVCRT_LOCKING_METHODDEF
464 MSVCRT_SETMODE_METHODDEF
465 MSVCRT_OPEN_OSFHANDLE_METHODDEF
466 MSVCRT_GET_OSFHANDLE_METHODDEF
467 MSVCRT_KBHIT_METHODDEF
468 MSVCRT_GETCH_METHODDEF
469 MSVCRT_GETCHE_METHODDEF
470 MSVCRT_PUTCH_METHODDEF
471 MSVCRT_UNGETCH_METHODDEF
472 MSVCRT_SETERRORMODE_METHODDEF
473 MSVCRT_CRTSETREPORTFILE_METHODDEF
474 MSVCRT_CRTSETREPORTMODE_METHODDEF
475 MSVCRT_SET_ERROR_MODE_METHODDEF
476 MSVCRT_GETWCH_METHODDEF
477 MSVCRT_GETWCHE_METHODDEF
478 MSVCRT_PUTWCH_METHODDEF
479 MSVCRT_UNGETWCH_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 {NULL, NULL}
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000481};
482
Martin v. Löwis1a214512008-06-11 05:26:20 +0000483
484static struct PyModuleDef msvcrtmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 PyModuleDef_HEAD_INIT,
486 "msvcrt",
487 NULL,
488 -1,
489 msvcrt_functions,
490 NULL,
491 NULL,
492 NULL,
493 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000494};
495
Zachary Ware45520892015-05-13 01:22:32 -0500496static void
497insertint(PyObject *d, char *name, int value)
498{
499 PyObject *v = PyLong_FromLong((long) value);
500 if (v == NULL) {
501 /* Don't bother reporting this error */
502 PyErr_Clear();
503 }
504 else {
505 PyDict_SetItemString(d, name, v);
506 Py_DECREF(v);
507 }
508}
509
Thomas Hellera18331d2004-07-28 20:02:52 +0000510PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000511PyInit_msvcrt(void)
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 int st;
Brian Curtin401f9f32012-05-13 11:19:23 -0500514 PyObject *d, *version;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 PyObject *m = PyModule_Create(&msvcrtmodule);
516 if (m == NULL)
517 return NULL;
518 d = PyModule_GetDict(m);
Tim Peters5fa0bd62000-12-12 01:58:56 +0000519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 /* constants for the locking() function's mode argument */
521 insertint(d, "LK_LOCK", _LK_LOCK);
522 insertint(d, "LK_NBLCK", _LK_NBLCK);
523 insertint(d, "LK_NBRLCK", _LK_NBRLCK);
524 insertint(d, "LK_RLCK", _LK_RLCK);
525 insertint(d, "LK_UNLCK", _LK_UNLCK);
526 insertint(d, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS);
527 insertint(d, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT);
528 insertint(d, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX);
529 insertint(d, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000530#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 insertint(d, "CRT_WARN", _CRT_WARN);
532 insertint(d, "CRT_ERROR", _CRT_ERROR);
533 insertint(d, "CRT_ASSERT", _CRT_ASSERT);
534 insertint(d, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG);
535 insertint(d, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE);
536 insertint(d, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW);
537 insertint(d, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE);
538 insertint(d, "CRTDBG_FILE_STDERR", (int)_CRTDBG_FILE_STDERR);
539 insertint(d, "CRTDBG_FILE_STDOUT", (int)_CRTDBG_FILE_STDOUT);
540 insertint(d, "CRTDBG_REPORT_FILE", (int)_CRTDBG_REPORT_FILE);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000541#endif
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 /* constants for the crt versions */
Brian Curtin401f9f32012-05-13 11:19:23 -0500544 (void)st;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000545#ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
547 _VC_ASSEMBLY_PUBLICKEYTOKEN);
548 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000549#endif
550#ifdef _CRT_ASSEMBLY_VERSION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
552 _CRT_ASSEMBLY_VERSION);
553 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000554#endif
555#ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
557 __LIBRARIES_ASSEMBLY_NAME_PREFIX);
558 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000559#endif
560
Brian Curtin401f9f32012-05-13 11:19:23 -0500561 /* constants for the 2010 crt versions */
562#if defined(_VC_CRT_MAJOR_VERSION) && defined (_VC_CRT_MINOR_VERSION) && defined(_VC_CRT_BUILD_VERSION) && defined(_VC_CRT_RBUILD_VERSION)
563 version = PyUnicode_FromFormat("%d.%d.%d.%d", _VC_CRT_MAJOR_VERSION,
564 _VC_CRT_MINOR_VERSION,
565 _VC_CRT_BUILD_VERSION,
566 _VC_CRT_RBUILD_VERSION);
567 st = PyModule_AddObject(m, "CRT_ASSEMBLY_VERSION", version);
568 if (st < 0) return NULL;
569#endif
570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 return m;
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000572}