blob: 0591497871cadc29039dbafd4c5dec3f7755858b [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]
Segev Finer679b5662017-07-27 01:17:57 +030036class HANDLE_converter(CConverter):
37 type = 'void *'
Segev Finera80e9852017-07-27 06:15:18 +030038 format_unit = '"_Py_PARSE_UINTPTR"'
Guido van Rossum407a22d1997-08-13 19:57:53 +000039
Segev Finer679b5662017-07-27 01:17:57 +030040class HANDLE_return_converter(CReturnConverter):
41 type = 'void *'
42
43 def render(self, function, data):
44 self.declare(data)
45 self.err_occurred_if(
46 "_return_value == NULL || _return_value == INVALID_HANDLE_VALUE",
47 data)
48 data.return_conversion.append(
49 'return_value = PyLong_FromVoidPtr(_return_value);\n')
Zachary Ware45520892015-05-13 01:22:32 -050050
51class byte_char_return_converter(CReturnConverter):
52 type = 'int'
53
54 def render(self, function, data):
55 data.declarations.append('char s[1];')
56 data.return_value = 's[0]'
57 data.return_conversion.append(
58 'return_value = PyBytes_FromStringAndSize(s, 1);\n')
59
60class wchar_t_return_converter(CReturnConverter):
61 type = 'wchar_t'
62
63 def render(self, function, data):
64 self.declare(data)
65 data.return_conversion.append(
66 'return_value = PyUnicode_FromOrdinal(_return_value);\n')
67[python start generated code]*/
Segev Finera80e9852017-07-27 06:15:18 +030068/*[python end generated code: output=da39a3ee5e6b4b0d input=d102511df3cda2eb]*/
Zachary Ware45520892015-05-13 01:22:32 -050069
70/*[clinic input]
71module msvcrt
72[clinic start generated code]*/
73/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f31a87a783d036cd]*/
74
75#include "clinic/msvcrtmodule.c.h"
76
77/*[clinic input]
78msvcrt.heapmin
79
80Minimize the malloc() heap.
81
82Force the malloc() heap to clean itself up and return unused blocks
83to the operating system. On failure, this raises OSError.
84[clinic start generated code]*/
85
86static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030087msvcrt_heapmin_impl(PyObject *module)
88/*[clinic end generated code: output=1ba00f344782dc19 input=82e1771d21bde2d8]*/
Zachary Ware45520892015-05-13 01:22:32 -050089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 if (_heapmin() != 0)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +030091 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum407a22d1997-08-13 19:57:53 +000092
Zachary Ware45520892015-05-13 01:22:32 -050093 Py_RETURN_NONE;
Guido van Rossum29c1ea51997-08-07 00:11:34 +000094}
Zachary Ware45520892015-05-13 01:22:32 -050095/*[clinic input]
96msvcrt.locking
Guido van Rossum29c1ea51997-08-07 00:11:34 +000097
Zachary Ware45520892015-05-13 01:22:32 -050098 fd: int
99 mode: int
100 nbytes: long
101 /
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000102
Zachary Ware45520892015-05-13 01:22:32 -0500103Lock part of a file based on file descriptor fd from the C runtime.
104
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300105Raises OSError on failure. The locked region of the file extends from
Zachary Ware45520892015-05-13 01:22:32 -0500106the current file position for nbytes bytes, and may continue beyond
107the end of the file. mode must be one of the LK_* constants listed
108below. Multiple regions in a file may be locked at the same time, but
109may not overlap. Adjacent regions are not merged; they must be unlocked
110individually.
111[clinic start generated code]*/
112
Tim Peters5fa0bd62000-12-12 01:58:56 +0000113static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300114msvcrt_locking_impl(PyObject *module, int fd, int mode, long nbytes)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300115/*[clinic end generated code: output=a4a90deca9785a03 input=e97bd15fc4a04fef]*/
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 int err;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000118
Saiyang Gou7514f4f2020-02-12 23:47:42 -0800119 if (PySys_Audit("msvcrt.locking", "iil", fd, mode, nbytes) < 0) {
120 return NULL;
121 }
122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 Py_BEGIN_ALLOW_THREADS
Steve Dower21fae032017-02-04 15:05:13 -0800124 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 err = _locking(fd, mode, nbytes);
Steve Dower21fae032017-02-04 15:05:13 -0800126 _Py_END_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 Py_END_ALLOW_THREADS
128 if (err != 0)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300129 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000130
Zachary Ware45520892015-05-13 01:22:32 -0500131 Py_RETURN_NONE;
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000132}
Guido van Rossum407a22d1997-08-13 19:57:53 +0000133
Zachary Ware45520892015-05-13 01:22:32 -0500134/*[clinic input]
135msvcrt.setmode -> long
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000136
Zachary Ware45520892015-05-13 01:22:32 -0500137 fd: int
138 mode as flags: int
139 /
140
141Set the line-end translation mode for the file descriptor fd.
142
143To set it to text mode, flags should be os.O_TEXT; for binary, it
144should be os.O_BINARY.
145
146Return value is the previous mode.
147[clinic start generated code]*/
148
149static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300150msvcrt_setmode_impl(PyObject *module, int fd, int flags)
151/*[clinic end generated code: output=24a9be5ea07ccb9b input=76e7c01f6b137f75]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000152{
Steve Dower21fae032017-02-04 15:05:13 -0800153 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 flags = _setmode(fd, flags);
Steve Dower21fae032017-02-04 15:05:13 -0800155 _Py_END_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 if (flags == -1)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300157 PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000158
Zachary Ware45520892015-05-13 01:22:32 -0500159 return flags;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000160}
161
Zachary Ware45520892015-05-13 01:22:32 -0500162/*[clinic input]
163msvcrt.open_osfhandle -> long
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000164
Segev Finer679b5662017-07-27 01:17:57 +0300165 handle: HANDLE
Zachary Ware45520892015-05-13 01:22:32 -0500166 flags: int
167 /
168
169Create a C runtime file descriptor from the file handle handle.
170
171The flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY,
172and os.O_TEXT. The returned file descriptor may be used as a parameter
173to os.fdopen() to create a file object.
174[clinic start generated code]*/
175
176static long
Segev Finer679b5662017-07-27 01:17:57 +0300177msvcrt_open_osfhandle_impl(PyObject *module, void *handle, int flags)
178/*[clinic end generated code: output=b2fb97c4b515e4e6 input=d5db190a307cf4bb]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000179{
Saiyang Gou7514f4f2020-02-12 23:47:42 -0800180 if (PySys_Audit("msvcrt.open_osfhandle", "Ki", handle, flags) < 0) {
Steve Dower6c444d02020-02-20 22:24:44 +0000181 return -1;
Saiyang Gou7514f4f2020-02-12 23:47:42 -0800182 }
183
Segev Finer5e437fb2021-04-24 01:00:27 +0300184 return _Py_open_osfhandle(handle, flags);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000185}
186
Zachary Ware45520892015-05-13 01:22:32 -0500187/*[clinic input]
Segev Finer679b5662017-07-27 01:17:57 +0300188msvcrt.get_osfhandle -> HANDLE
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000189
Zachary Ware45520892015-05-13 01:22:32 -0500190 fd: int
191 /
192
193Return the file handle for the file descriptor fd.
194
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300195Raises OSError if fd is not recognized.
Zachary Ware45520892015-05-13 01:22:32 -0500196[clinic start generated code]*/
197
Segev Finer679b5662017-07-27 01:17:57 +0300198static void *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300199msvcrt_get_osfhandle_impl(PyObject *module, int fd)
Segev Finer679b5662017-07-27 01:17:57 +0300200/*[clinic end generated code: output=aca01dfe24637374 input=5fcfde9b17136aa2]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000201{
Saiyang Gou7514f4f2020-02-12 23:47:42 -0800202 if (PySys_Audit("msvcrt.get_osfhandle", "(i)", fd) < 0) {
203 return NULL;
204 }
205
Segev Finer5e437fb2021-04-24 01:00:27 +0300206 return _Py_get_osfhandle(fd);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000207}
208
209/* Console I/O */
Zachary Ware45520892015-05-13 01:22:32 -0500210/*[clinic input]
211msvcrt.kbhit -> long
Guido van Rossum407a22d1997-08-13 19:57:53 +0000212
Zachary Ware45520892015-05-13 01:22:32 -0500213Return true if a keypress is waiting to be read.
214[clinic start generated code]*/
215
216static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300217msvcrt_kbhit_impl(PyObject *module)
218/*[clinic end generated code: output=940dfce6587c1890 input=e70d678a5c2f6acc]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000219{
Zachary Ware45520892015-05-13 01:22:32 -0500220 return _kbhit();
Guido van Rossum407a22d1997-08-13 19:57:53 +0000221}
222
Zachary Ware45520892015-05-13 01:22:32 -0500223/*[clinic input]
224msvcrt.getch -> byte_char
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000225
Zachary Ware45520892015-05-13 01:22:32 -0500226Read a keypress and return the resulting character as a byte string.
227
228Nothing is echoed to the console. This call will block if a keypress is
229not already available, but will not wait for Enter to be pressed. If the
230pressed key was a special function key, this will return '\000' or
231'\xe0'; the next call will return the keycode. The Control-C keypress
232cannot be read with this function.
233[clinic start generated code]*/
234
235static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300236msvcrt_getch_impl(PyObject *module)
237/*[clinic end generated code: output=a4e51f0565064a7d input=37a40cf0ed0d1153]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 int ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 Py_BEGIN_ALLOW_THREADS
242 ch = _getch();
243 Py_END_ALLOW_THREADS
Zachary Ware45520892015-05-13 01:22:32 -0500244 return ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000245}
246
Zachary Ware45520892015-05-13 01:22:32 -0500247/*[clinic input]
248msvcrt.getwch -> wchar_t
249
250Wide char variant of getch(), returning a Unicode value.
251[clinic start generated code]*/
252
253static wchar_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300254msvcrt_getwch_impl(PyObject *module)
255/*[clinic end generated code: output=be9937494e22f007 input=27b3dec8ad823d7c]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000256{
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100257 wchar_t ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 Py_BEGIN_ALLOW_THREADS
260 ch = _getwch();
261 Py_END_ALLOW_THREADS
Zachary Ware45520892015-05-13 01:22:32 -0500262 return ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000263}
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000264
Zachary Ware45520892015-05-13 01:22:32 -0500265/*[clinic input]
266msvcrt.getche -> byte_char
Christian Heimes2f1019e2007-12-10 16:18:49 +0000267
Zachary Ware45520892015-05-13 01:22:32 -0500268Similar to getch(), but the keypress will be echoed if possible.
269[clinic start generated code]*/
270
271static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300272msvcrt_getche_impl(PyObject *module)
273/*[clinic end generated code: output=d8f7db4fd2990401 input=43311ade9ed4a9c0]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 int ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 Py_BEGIN_ALLOW_THREADS
278 ch = _getche();
279 Py_END_ALLOW_THREADS
Zachary Ware45520892015-05-13 01:22:32 -0500280 return ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000281}
282
Zachary Ware45520892015-05-13 01:22:32 -0500283/*[clinic input]
284msvcrt.getwche -> wchar_t
285
286Wide char variant of getche(), returning a Unicode value.
287[clinic start generated code]*/
288
289static wchar_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300290msvcrt_getwche_impl(PyObject *module)
291/*[clinic end generated code: output=d0dae5ba3829d596 input=49337d59d1a591f8]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000292{
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100293 wchar_t ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 Py_BEGIN_ALLOW_THREADS
296 ch = _getwche();
297 Py_END_ALLOW_THREADS
Zachary Ware45520892015-05-13 01:22:32 -0500298 return ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000299}
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000300
Zachary Ware45520892015-05-13 01:22:32 -0500301/*[clinic input]
302msvcrt.putch
303
304 char: char
305 /
306
307Print the byte string char to the console without buffering.
308[clinic start generated code]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000309
310static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300311msvcrt_putch_impl(PyObject *module, char char_value)
312/*[clinic end generated code: output=92ec9b81012d8f60 input=ec078dd10cb054d6]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000313{
Steve Dower21fae032017-02-04 15:05:13 -0800314 _Py_BEGIN_SUPPRESS_IPH
Zachary Ware45520892015-05-13 01:22:32 -0500315 _putch(char_value);
Steve Dower21fae032017-02-04 15:05:13 -0800316 _Py_END_SUPPRESS_IPH
Zachary Ware45520892015-05-13 01:22:32 -0500317 Py_RETURN_NONE;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000318}
319
Zachary Ware45520892015-05-13 01:22:32 -0500320/*[clinic input]
321msvcrt.putwch
322
Zachary Ware77772c02015-05-13 10:58:35 -0500323 unicode_char: int(accept={str})
Zachary Ware45520892015-05-13 01:22:32 -0500324 /
325
326Wide char variant of putch(), accepting a Unicode value.
327[clinic start generated code]*/
328
Christian Heimes2f1019e2007-12-10 16:18:49 +0000329static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300330msvcrt_putwch_impl(PyObject *module, int unicode_char)
331/*[clinic end generated code: output=a3bd1a8951d28eee input=996ccd0bbcbac4c3]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000332{
Steve Dower21fae032017-02-04 15:05:13 -0800333 _Py_BEGIN_SUPPRESS_IPH
Zachary Ware45520892015-05-13 01:22:32 -0500334 _putwch(unicode_char);
Steve Dower21fae032017-02-04 15:05:13 -0800335 _Py_END_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 Py_RETURN_NONE;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000337
338}
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000339
Zachary Ware45520892015-05-13 01:22:32 -0500340/*[clinic input]
341msvcrt.ungetch
342
343 char: char
344 /
345
346Opposite of getch.
347
348Cause the byte string char to be "pushed back" into the
349console buffer; it will be the next character read by
350getch() or getche().
351[clinic start generated code]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000352
Tim Peters5fa0bd62000-12-12 01:58:56 +0000353static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300354msvcrt_ungetch_impl(PyObject *module, char char_value)
355/*[clinic end generated code: output=c6942a0efa119000 input=22f07ee9001bbf0f]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000356{
Steve Dower21fae032017-02-04 15:05:13 -0800357 int res;
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +0300358
Steve Dower21fae032017-02-04 15:05:13 -0800359 _Py_BEGIN_SUPPRESS_IPH
360 res = _ungetch(char_value);
361 _Py_END_SUPPRESS_IPH
362
363 if (res == EOF)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300364 return PyErr_SetFromErrno(PyExc_OSError);
Zachary Ware45520892015-05-13 01:22:32 -0500365 Py_RETURN_NONE;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000366}
367
Zachary Ware45520892015-05-13 01:22:32 -0500368/*[clinic input]
369msvcrt.ungetwch
370
Zachary Ware77772c02015-05-13 10:58:35 -0500371 unicode_char: int(accept={str})
Zachary Ware45520892015-05-13 01:22:32 -0500372 /
373
374Wide char variant of ungetch(), accepting a Unicode value.
375[clinic start generated code]*/
376
Christian Heimes2f1019e2007-12-10 16:18:49 +0000377static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300378msvcrt_ungetwch_impl(PyObject *module, int unicode_char)
379/*[clinic end generated code: output=e63af05438b8ba3d input=83ec0492be04d564]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000380{
Steve Dower21fae032017-02-04 15:05:13 -0800381 int res;
382
383 _Py_BEGIN_SUPPRESS_IPH
384 res = _ungetwch(unicode_char);
385 _Py_END_SUPPRESS_IPH
386
387 if (res == WEOF)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300388 return PyErr_SetFromErrno(PyExc_OSError);
Zachary Ware45520892015-05-13 01:22:32 -0500389 Py_RETURN_NONE;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000390}
Tim Peters5fa0bd62000-12-12 01:58:56 +0000391
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000392#ifdef _DEBUG
Zachary Ware45520892015-05-13 01:22:32 -0500393/*[clinic input]
Segev Finer679b5662017-07-27 01:17:57 +0300394msvcrt.CrtSetReportFile -> HANDLE
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000395
Zachary Ware45520892015-05-13 01:22:32 -0500396 type: int
Segev Finer679b5662017-07-27 01:17:57 +0300397 file: HANDLE
Zachary Ware45520892015-05-13 01:22:32 -0500398 /
399
400Wrapper around _CrtSetReportFile.
401
402Only available on Debug builds.
403[clinic start generated code]*/
404
Segev Finer679b5662017-07-27 01:17:57 +0300405static void *
406msvcrt_CrtSetReportFile_impl(PyObject *module, int type, void *file)
407/*[clinic end generated code: output=9393e8c77088bbe9 input=290809b5f19e65b9]*/
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000408{
Segev Finer679b5662017-07-27 01:17:57 +0300409 HANDLE res;
Steve Dower21fae032017-02-04 15:05:13 -0800410
411 _Py_BEGIN_SUPPRESS_IPH
Segev Finer679b5662017-07-27 01:17:57 +0300412 res = _CrtSetReportFile(type, file);
Steve Dower21fae032017-02-04 15:05:13 -0800413 _Py_END_SUPPRESS_IPH
414
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.CrtSetReportMode -> long
420
421 type: int
422 mode: int
423 /
424
425Wrapper around _CrtSetReportMode.
426
427Only available on Debug builds.
428[clinic start generated code]*/
429
430static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300431msvcrt_CrtSetReportMode_impl(PyObject *module, int type, int mode)
432/*[clinic end generated code: output=b2863761523de317 input=9319d29b4319426b]*/
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 int res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000435
Steve Dower21fae032017-02-04 15:05:13 -0800436 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 res = _CrtSetReportMode(type, mode);
Steve Dower21fae032017-02-04 15:05:13 -0800438 _Py_END_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 if (res == -1)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300440 PyErr_SetFromErrno(PyExc_OSError);
Zachary Ware45520892015-05-13 01:22:32 -0500441 return res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000442}
443
Zachary Ware45520892015-05-13 01:22:32 -0500444/*[clinic input]
445msvcrt.set_error_mode -> long
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000446
Zachary Ware45520892015-05-13 01:22:32 -0500447 mode: int
448 /
449
450Wrapper around _set_error_mode.
451
452Only available on Debug builds.
453[clinic start generated code]*/
454
455static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300456msvcrt_set_error_mode_impl(PyObject *module, int mode)
457/*[clinic end generated code: output=ac4a09040d8ac4e3 input=046fca59c0f20872]*/
Zachary Ware45520892015-05-13 01:22:32 -0500458{
Steve Dower21fae032017-02-04 15:05:13 -0800459 long res;
460
461 _Py_BEGIN_SUPPRESS_IPH
462 res = _set_error_mode(mode);
463 _Py_END_SUPPRESS_IPH
464
465 return res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000466}
Zachary Ware45520892015-05-13 01:22:32 -0500467#endif /* _DEBUG */
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000468
Zachary Ware45520892015-05-13 01:22:32 -0500469/*[clinic input]
Victor Stinnerf6e58ae2020-06-10 18:49:23 +0200470msvcrt.GetErrorMode
471
472Wrapper around GetErrorMode.
473[clinic start generated code]*/
474
475static PyObject *
476msvcrt_GetErrorMode_impl(PyObject *module)
477/*[clinic end generated code: output=3103fc6145913591 input=5a7fb083b6dd71fd]*/
478{
479 unsigned int res;
480
481 _Py_BEGIN_SUPPRESS_IPH
482 res = GetErrorMode();
483 _Py_END_SUPPRESS_IPH
484
485 return PyLong_FromUnsignedLong(res);
486}
487
488/*[clinic input]
Zachary Ware45520892015-05-13 01:22:32 -0500489msvcrt.SetErrorMode
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000490
Zachary Ware45520892015-05-13 01:22:32 -0500491 mode: unsigned_int(bitwise=True)
492 /
493
494Wrapper around SetErrorMode.
495[clinic start generated code]*/
496
497static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300498msvcrt_SetErrorMode_impl(PyObject *module, unsigned int mode)
499/*[clinic end generated code: output=01d529293f00da8f input=d8b167258d32d907]*/
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000500{
Zachary Ware45520892015-05-13 01:22:32 -0500501 unsigned int res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000502
Steve Dower21fae032017-02-04 15:05:13 -0800503 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 res = SetErrorMode(mode);
Steve Dower21fae032017-02-04 15:05:13 -0800505 _Py_END_SUPPRESS_IPH
506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 return PyLong_FromUnsignedLong(res);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000508}
509
Zachary Ware45520892015-05-13 01:22:32 -0500510/*[clinic input]
511[clinic start generated code]*/
512/*[clinic end generated code: output=da39a3ee5e6b4b0d input=da39a3ee5e6b4b0d]*/
Tim Peters5fa0bd62000-12-12 01:58:56 +0000513
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000514/* List of functions exported by this module */
515static struct PyMethodDef msvcrt_functions[] = {
Zachary Ware45520892015-05-13 01:22:32 -0500516 MSVCRT_HEAPMIN_METHODDEF
517 MSVCRT_LOCKING_METHODDEF
518 MSVCRT_SETMODE_METHODDEF
519 MSVCRT_OPEN_OSFHANDLE_METHODDEF
520 MSVCRT_GET_OSFHANDLE_METHODDEF
521 MSVCRT_KBHIT_METHODDEF
522 MSVCRT_GETCH_METHODDEF
523 MSVCRT_GETCHE_METHODDEF
524 MSVCRT_PUTCH_METHODDEF
525 MSVCRT_UNGETCH_METHODDEF
Victor Stinnerf6e58ae2020-06-10 18:49:23 +0200526 MSVCRT_GETERRORMODE_METHODDEF
Zachary Ware45520892015-05-13 01:22:32 -0500527 MSVCRT_SETERRORMODE_METHODDEF
528 MSVCRT_CRTSETREPORTFILE_METHODDEF
529 MSVCRT_CRTSETREPORTMODE_METHODDEF
530 MSVCRT_SET_ERROR_MODE_METHODDEF
531 MSVCRT_GETWCH_METHODDEF
532 MSVCRT_GETWCHE_METHODDEF
533 MSVCRT_PUTWCH_METHODDEF
534 MSVCRT_UNGETWCH_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 {NULL, NULL}
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000536};
537
Martin v. Löwis1a214512008-06-11 05:26:20 +0000538
539static struct PyModuleDef msvcrtmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 PyModuleDef_HEAD_INIT,
541 "msvcrt",
542 NULL,
543 -1,
544 msvcrt_functions,
545 NULL,
546 NULL,
547 NULL,
548 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000549};
550
Zachary Ware45520892015-05-13 01:22:32 -0500551static void
552insertint(PyObject *d, char *name, int value)
553{
554 PyObject *v = PyLong_FromLong((long) value);
555 if (v == NULL) {
556 /* Don't bother reporting this error */
557 PyErr_Clear();
558 }
559 else {
560 PyDict_SetItemString(d, name, v);
561 Py_DECREF(v);
562 }
563}
564
Segev Finer679b5662017-07-27 01:17:57 +0300565static void
566insertptr(PyObject *d, char *name, void *value)
567{
568 PyObject *v = PyLong_FromVoidPtr(value);
569 if (v == NULL) {
570 /* Don't bother reporting this error */
571 PyErr_Clear();
572 }
573 else {
574 PyDict_SetItemString(d, name, v);
575 Py_DECREF(v);
576 }
577}
578
Thomas Hellera18331d2004-07-28 20:02:52 +0000579PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000580PyInit_msvcrt(void)
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000581{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 int st;
Brian Curtin401f9f32012-05-13 11:19:23 -0500583 PyObject *d, *version;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 PyObject *m = PyModule_Create(&msvcrtmodule);
585 if (m == NULL)
586 return NULL;
587 d = PyModule_GetDict(m);
Tim Peters5fa0bd62000-12-12 01:58:56 +0000588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 /* constants for the locking() function's mode argument */
590 insertint(d, "LK_LOCK", _LK_LOCK);
591 insertint(d, "LK_NBLCK", _LK_NBLCK);
592 insertint(d, "LK_NBRLCK", _LK_NBRLCK);
593 insertint(d, "LK_RLCK", _LK_RLCK);
594 insertint(d, "LK_UNLCK", _LK_UNLCK);
595 insertint(d, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS);
596 insertint(d, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT);
597 insertint(d, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX);
598 insertint(d, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000599#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 insertint(d, "CRT_WARN", _CRT_WARN);
601 insertint(d, "CRT_ERROR", _CRT_ERROR);
602 insertint(d, "CRT_ASSERT", _CRT_ASSERT);
603 insertint(d, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG);
604 insertint(d, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE);
605 insertint(d, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW);
606 insertint(d, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE);
Segev Finer679b5662017-07-27 01:17:57 +0300607 insertptr(d, "CRTDBG_FILE_STDERR", _CRTDBG_FILE_STDERR);
608 insertptr(d, "CRTDBG_FILE_STDOUT", _CRTDBG_FILE_STDOUT);
609 insertptr(d, "CRTDBG_REPORT_FILE", _CRTDBG_REPORT_FILE);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000610#endif
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 /* constants for the crt versions */
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000613#ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
615 _VC_ASSEMBLY_PUBLICKEYTOKEN);
616 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000617#endif
618#ifdef _CRT_ASSEMBLY_VERSION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
620 _CRT_ASSEMBLY_VERSION);
621 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000622#endif
623#ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
625 __LIBRARIES_ASSEMBLY_NAME_PREFIX);
626 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000627#endif
628
Brian Curtin401f9f32012-05-13 11:19:23 -0500629 /* constants for the 2010 crt versions */
630#if defined(_VC_CRT_MAJOR_VERSION) && defined (_VC_CRT_MINOR_VERSION) && defined(_VC_CRT_BUILD_VERSION) && defined(_VC_CRT_RBUILD_VERSION)
631 version = PyUnicode_FromFormat("%d.%d.%d.%d", _VC_CRT_MAJOR_VERSION,
632 _VC_CRT_MINOR_VERSION,
633 _VC_CRT_BUILD_VERSION,
634 _VC_CRT_RBUILD_VERSION);
635 st = PyModule_AddObject(m, "CRT_ASSEMBLY_VERSION", version);
636 if (st < 0) return NULL;
637#endif
Victor Stinnerba452952015-09-21 22:40:28 +0200638 /* make compiler warning quiet if st is unused */
639 (void)st;
Brian Curtin401f9f32012-05-13 11:19:23 -0500640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 return m;
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000642}