blob: c4113e54c2b8fed06163a2f4bfaede5856f95306 [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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 Py_BEGIN_ALLOW_THREADS
Steve Dower21fae032017-02-04 15:05:13 -0800120 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 err = _locking(fd, mode, nbytes);
Steve Dower21fae032017-02-04 15:05:13 -0800122 _Py_END_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 Py_END_ALLOW_THREADS
124 if (err != 0)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300125 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000126
Zachary Ware45520892015-05-13 01:22:32 -0500127 Py_RETURN_NONE;
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000128}
Guido van Rossum407a22d1997-08-13 19:57:53 +0000129
Zachary Ware45520892015-05-13 01:22:32 -0500130/*[clinic input]
131msvcrt.setmode -> long
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000132
Zachary Ware45520892015-05-13 01:22:32 -0500133 fd: int
134 mode as flags: int
135 /
136
137Set the line-end translation mode for the file descriptor fd.
138
139To set it to text mode, flags should be os.O_TEXT; for binary, it
140should be os.O_BINARY.
141
142Return value is the previous mode.
143[clinic start generated code]*/
144
145static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300146msvcrt_setmode_impl(PyObject *module, int fd, int flags)
147/*[clinic end generated code: output=24a9be5ea07ccb9b input=76e7c01f6b137f75]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000148{
Steve Dower21fae032017-02-04 15:05:13 -0800149 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 flags = _setmode(fd, flags);
Steve Dower21fae032017-02-04 15:05:13 -0800151 _Py_END_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 if (flags == -1)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300153 PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000154
Zachary Ware45520892015-05-13 01:22:32 -0500155 return flags;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000156}
157
Zachary Ware45520892015-05-13 01:22:32 -0500158/*[clinic input]
159msvcrt.open_osfhandle -> long
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000160
Segev Finer679b5662017-07-27 01:17:57 +0300161 handle: HANDLE
Zachary Ware45520892015-05-13 01:22:32 -0500162 flags: int
163 /
164
165Create a C runtime file descriptor from the file handle handle.
166
167The flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY,
168and os.O_TEXT. The returned file descriptor may be used as a parameter
169to os.fdopen() to create a file object.
170[clinic start generated code]*/
171
172static long
Segev Finer679b5662017-07-27 01:17:57 +0300173msvcrt_open_osfhandle_impl(PyObject *module, void *handle, int flags)
174/*[clinic end generated code: output=b2fb97c4b515e4e6 input=d5db190a307cf4bb]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 int fd;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000177
Steve Dower21fae032017-02-04 15:05:13 -0800178 _Py_BEGIN_SUPPRESS_IPH
Segev Finer679b5662017-07-27 01:17:57 +0300179 fd = _open_osfhandle((intptr_t)handle, flags);
Steve Dower21fae032017-02-04 15:05:13 -0800180 _Py_END_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 if (fd == -1)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300182 PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000183
Zachary Ware45520892015-05-13 01:22:32 -0500184 return fd;
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{
Benjamin Petersonca470632016-09-06 13:47:26 -0700202 intptr_t handle = -1;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000203
Steve Dower8fc89802015-04-12 00:26:27 -0400204 _Py_BEGIN_SUPPRESS_IPH
Steve Dower940f33a2016-09-08 11:21:54 -0700205 handle = _get_osfhandle(fd);
Steve Dower8fc89802015-04-12 00:26:27 -0400206 _Py_END_SUPPRESS_IPH
Steve Dower940f33a2016-09-08 11:21:54 -0700207 if (handle == -1)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300208 PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000209
Segev Finer679b5662017-07-27 01:17:57 +0300210 return (HANDLE)handle;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000211}
212
213/* Console I/O */
Zachary Ware45520892015-05-13 01:22:32 -0500214/*[clinic input]
215msvcrt.kbhit -> long
Guido van Rossum407a22d1997-08-13 19:57:53 +0000216
Zachary Ware45520892015-05-13 01:22:32 -0500217Return true if a keypress is waiting to be read.
218[clinic start generated code]*/
219
220static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300221msvcrt_kbhit_impl(PyObject *module)
222/*[clinic end generated code: output=940dfce6587c1890 input=e70d678a5c2f6acc]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000223{
Zachary Ware45520892015-05-13 01:22:32 -0500224 return _kbhit();
Guido van Rossum407a22d1997-08-13 19:57:53 +0000225}
226
Zachary Ware45520892015-05-13 01:22:32 -0500227/*[clinic input]
228msvcrt.getch -> byte_char
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000229
Zachary Ware45520892015-05-13 01:22:32 -0500230Read a keypress and return the resulting character as a byte string.
231
232Nothing is echoed to the console. This call will block if a keypress is
233not already available, but will not wait for Enter to be pressed. If the
234pressed key was a special function key, this will return '\000' or
235'\xe0'; the next call will return the keycode. The Control-C keypress
236cannot be read with this function.
237[clinic start generated code]*/
238
239static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300240msvcrt_getch_impl(PyObject *module)
241/*[clinic end generated code: output=a4e51f0565064a7d input=37a40cf0ed0d1153]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 int ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 Py_BEGIN_ALLOW_THREADS
246 ch = _getch();
247 Py_END_ALLOW_THREADS
Zachary Ware45520892015-05-13 01:22:32 -0500248 return ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000249}
250
Zachary Ware45520892015-05-13 01:22:32 -0500251/*[clinic input]
252msvcrt.getwch -> wchar_t
253
254Wide char variant of getch(), returning a Unicode value.
255[clinic start generated code]*/
256
257static wchar_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300258msvcrt_getwch_impl(PyObject *module)
259/*[clinic end generated code: output=be9937494e22f007 input=27b3dec8ad823d7c]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000260{
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100261 wchar_t ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 Py_BEGIN_ALLOW_THREADS
264 ch = _getwch();
265 Py_END_ALLOW_THREADS
Zachary Ware45520892015-05-13 01:22:32 -0500266 return ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000267}
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000268
Zachary Ware45520892015-05-13 01:22:32 -0500269/*[clinic input]
270msvcrt.getche -> byte_char
Christian Heimes2f1019e2007-12-10 16:18:49 +0000271
Zachary Ware45520892015-05-13 01:22:32 -0500272Similar to getch(), but the keypress will be echoed if possible.
273[clinic start generated code]*/
274
275static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300276msvcrt_getche_impl(PyObject *module)
277/*[clinic end generated code: output=d8f7db4fd2990401 input=43311ade9ed4a9c0]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000278{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 int ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 Py_BEGIN_ALLOW_THREADS
282 ch = _getche();
283 Py_END_ALLOW_THREADS
Zachary Ware45520892015-05-13 01:22:32 -0500284 return ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000285}
286
Zachary Ware45520892015-05-13 01:22:32 -0500287/*[clinic input]
288msvcrt.getwche -> wchar_t
289
290Wide char variant of getche(), returning a Unicode value.
291[clinic start generated code]*/
292
293static wchar_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300294msvcrt_getwche_impl(PyObject *module)
295/*[clinic end generated code: output=d0dae5ba3829d596 input=49337d59d1a591f8]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000296{
Victor Stinner9d3b93b2011-11-22 02:27:30 +0100297 wchar_t ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 Py_BEGIN_ALLOW_THREADS
300 ch = _getwche();
301 Py_END_ALLOW_THREADS
Zachary Ware45520892015-05-13 01:22:32 -0500302 return ch;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000303}
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000304
Zachary Ware45520892015-05-13 01:22:32 -0500305/*[clinic input]
306msvcrt.putch
307
308 char: char
309 /
310
311Print the byte string char to the console without buffering.
312[clinic start generated code]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000313
314static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300315msvcrt_putch_impl(PyObject *module, char char_value)
316/*[clinic end generated code: output=92ec9b81012d8f60 input=ec078dd10cb054d6]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000317{
Steve Dower21fae032017-02-04 15:05:13 -0800318 _Py_BEGIN_SUPPRESS_IPH
Zachary Ware45520892015-05-13 01:22:32 -0500319 _putch(char_value);
Steve Dower21fae032017-02-04 15:05:13 -0800320 _Py_END_SUPPRESS_IPH
Zachary Ware45520892015-05-13 01:22:32 -0500321 Py_RETURN_NONE;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000322}
323
Zachary Ware45520892015-05-13 01:22:32 -0500324/*[clinic input]
325msvcrt.putwch
326
Zachary Ware77772c02015-05-13 10:58:35 -0500327 unicode_char: int(accept={str})
Zachary Ware45520892015-05-13 01:22:32 -0500328 /
329
330Wide char variant of putch(), accepting a Unicode value.
331[clinic start generated code]*/
332
Christian Heimes2f1019e2007-12-10 16:18:49 +0000333static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300334msvcrt_putwch_impl(PyObject *module, int unicode_char)
335/*[clinic end generated code: output=a3bd1a8951d28eee input=996ccd0bbcbac4c3]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000336{
Steve Dower21fae032017-02-04 15:05:13 -0800337 _Py_BEGIN_SUPPRESS_IPH
Zachary Ware45520892015-05-13 01:22:32 -0500338 _putwch(unicode_char);
Steve Dower21fae032017-02-04 15:05:13 -0800339 _Py_END_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 Py_RETURN_NONE;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000341
342}
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000343
Zachary Ware45520892015-05-13 01:22:32 -0500344/*[clinic input]
345msvcrt.ungetch
346
347 char: char
348 /
349
350Opposite of getch.
351
352Cause the byte string char to be "pushed back" into the
353console buffer; it will be the next character read by
354getch() or getche().
355[clinic start generated code]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000356
Tim Peters5fa0bd62000-12-12 01:58:56 +0000357static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300358msvcrt_ungetch_impl(PyObject *module, char char_value)
359/*[clinic end generated code: output=c6942a0efa119000 input=22f07ee9001bbf0f]*/
Guido van Rossum407a22d1997-08-13 19:57:53 +0000360{
Steve Dower21fae032017-02-04 15:05:13 -0800361 int res;
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +0300362
Steve Dower21fae032017-02-04 15:05:13 -0800363 _Py_BEGIN_SUPPRESS_IPH
364 res = _ungetch(char_value);
365 _Py_END_SUPPRESS_IPH
366
367 if (res == EOF)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300368 return PyErr_SetFromErrno(PyExc_OSError);
Zachary Ware45520892015-05-13 01:22:32 -0500369 Py_RETURN_NONE;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000370}
371
Zachary Ware45520892015-05-13 01:22:32 -0500372/*[clinic input]
373msvcrt.ungetwch
374
Zachary Ware77772c02015-05-13 10:58:35 -0500375 unicode_char: int(accept={str})
Zachary Ware45520892015-05-13 01:22:32 -0500376 /
377
378Wide char variant of ungetch(), accepting a Unicode value.
379[clinic start generated code]*/
380
Christian Heimes2f1019e2007-12-10 16:18:49 +0000381static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300382msvcrt_ungetwch_impl(PyObject *module, int unicode_char)
383/*[clinic end generated code: output=e63af05438b8ba3d input=83ec0492be04d564]*/
Christian Heimes2f1019e2007-12-10 16:18:49 +0000384{
Steve Dower21fae032017-02-04 15:05:13 -0800385 int res;
386
387 _Py_BEGIN_SUPPRESS_IPH
388 res = _ungetwch(unicode_char);
389 _Py_END_SUPPRESS_IPH
390
391 if (res == WEOF)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300392 return PyErr_SetFromErrno(PyExc_OSError);
Zachary Ware45520892015-05-13 01:22:32 -0500393 Py_RETURN_NONE;
Christian Heimes2f1019e2007-12-10 16:18:49 +0000394}
Tim Peters5fa0bd62000-12-12 01:58:56 +0000395
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000396#ifdef _DEBUG
Zachary Ware45520892015-05-13 01:22:32 -0500397/*[clinic input]
Segev Finer679b5662017-07-27 01:17:57 +0300398msvcrt.CrtSetReportFile -> HANDLE
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000399
Zachary Ware45520892015-05-13 01:22:32 -0500400 type: int
Segev Finer679b5662017-07-27 01:17:57 +0300401 file: HANDLE
Zachary Ware45520892015-05-13 01:22:32 -0500402 /
403
404Wrapper around _CrtSetReportFile.
405
406Only available on Debug builds.
407[clinic start generated code]*/
408
Segev Finer679b5662017-07-27 01:17:57 +0300409static void *
410msvcrt_CrtSetReportFile_impl(PyObject *module, int type, void *file)
411/*[clinic end generated code: output=9393e8c77088bbe9 input=290809b5f19e65b9]*/
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000412{
Segev Finer679b5662017-07-27 01:17:57 +0300413 HANDLE res;
Steve Dower21fae032017-02-04 15:05:13 -0800414
415 _Py_BEGIN_SUPPRESS_IPH
Segev Finer679b5662017-07-27 01:17:57 +0300416 res = _CrtSetReportFile(type, file);
Steve Dower21fae032017-02-04 15:05:13 -0800417 _Py_END_SUPPRESS_IPH
418
419 return res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000420}
421
Zachary Ware45520892015-05-13 01:22:32 -0500422/*[clinic input]
423msvcrt.CrtSetReportMode -> long
424
425 type: int
426 mode: int
427 /
428
429Wrapper around _CrtSetReportMode.
430
431Only available on Debug builds.
432[clinic start generated code]*/
433
434static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300435msvcrt_CrtSetReportMode_impl(PyObject *module, int type, int mode)
436/*[clinic end generated code: output=b2863761523de317 input=9319d29b4319426b]*/
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 int res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000439
Steve Dower21fae032017-02-04 15:05:13 -0800440 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 res = _CrtSetReportMode(type, mode);
Steve Dower21fae032017-02-04 15:05:13 -0800442 _Py_END_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (res == -1)
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300444 PyErr_SetFromErrno(PyExc_OSError);
Zachary Ware45520892015-05-13 01:22:32 -0500445 return res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000446}
447
Zachary Ware45520892015-05-13 01:22:32 -0500448/*[clinic input]
449msvcrt.set_error_mode -> long
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000450
Zachary Ware45520892015-05-13 01:22:32 -0500451 mode: int
452 /
453
454Wrapper around _set_error_mode.
455
456Only available on Debug builds.
457[clinic start generated code]*/
458
459static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300460msvcrt_set_error_mode_impl(PyObject *module, int mode)
461/*[clinic end generated code: output=ac4a09040d8ac4e3 input=046fca59c0f20872]*/
Zachary Ware45520892015-05-13 01:22:32 -0500462{
Steve Dower21fae032017-02-04 15:05:13 -0800463 long res;
464
465 _Py_BEGIN_SUPPRESS_IPH
466 res = _set_error_mode(mode);
467 _Py_END_SUPPRESS_IPH
468
469 return res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000470}
Zachary Ware45520892015-05-13 01:22:32 -0500471#endif /* _DEBUG */
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000472
Zachary Ware45520892015-05-13 01:22:32 -0500473/*[clinic input]
474msvcrt.SetErrorMode
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000475
Zachary Ware45520892015-05-13 01:22:32 -0500476 mode: unsigned_int(bitwise=True)
477 /
478
479Wrapper around SetErrorMode.
480[clinic start generated code]*/
481
482static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300483msvcrt_SetErrorMode_impl(PyObject *module, unsigned int mode)
484/*[clinic end generated code: output=01d529293f00da8f input=d8b167258d32d907]*/
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000485{
Zachary Ware45520892015-05-13 01:22:32 -0500486 unsigned int res;
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000487
Steve Dower21fae032017-02-04 15:05:13 -0800488 _Py_BEGIN_SUPPRESS_IPH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 res = SetErrorMode(mode);
Steve Dower21fae032017-02-04 15:05:13 -0800490 _Py_END_SUPPRESS_IPH
491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 return PyLong_FromUnsignedLong(res);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000493}
494
Zachary Ware45520892015-05-13 01:22:32 -0500495/*[clinic input]
496[clinic start generated code]*/
497/*[clinic end generated code: output=da39a3ee5e6b4b0d input=da39a3ee5e6b4b0d]*/
Tim Peters5fa0bd62000-12-12 01:58:56 +0000498
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000499/* List of functions exported by this module */
500static struct PyMethodDef msvcrt_functions[] = {
Zachary Ware45520892015-05-13 01:22:32 -0500501 MSVCRT_HEAPMIN_METHODDEF
502 MSVCRT_LOCKING_METHODDEF
503 MSVCRT_SETMODE_METHODDEF
504 MSVCRT_OPEN_OSFHANDLE_METHODDEF
505 MSVCRT_GET_OSFHANDLE_METHODDEF
506 MSVCRT_KBHIT_METHODDEF
507 MSVCRT_GETCH_METHODDEF
508 MSVCRT_GETCHE_METHODDEF
509 MSVCRT_PUTCH_METHODDEF
510 MSVCRT_UNGETCH_METHODDEF
511 MSVCRT_SETERRORMODE_METHODDEF
512 MSVCRT_CRTSETREPORTFILE_METHODDEF
513 MSVCRT_CRTSETREPORTMODE_METHODDEF
514 MSVCRT_SET_ERROR_MODE_METHODDEF
515 MSVCRT_GETWCH_METHODDEF
516 MSVCRT_GETWCHE_METHODDEF
517 MSVCRT_PUTWCH_METHODDEF
518 MSVCRT_UNGETWCH_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 {NULL, NULL}
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000520};
521
Martin v. Löwis1a214512008-06-11 05:26:20 +0000522
523static struct PyModuleDef msvcrtmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 PyModuleDef_HEAD_INIT,
525 "msvcrt",
526 NULL,
527 -1,
528 msvcrt_functions,
529 NULL,
530 NULL,
531 NULL,
532 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000533};
534
Zachary Ware45520892015-05-13 01:22:32 -0500535static void
536insertint(PyObject *d, char *name, int value)
537{
538 PyObject *v = PyLong_FromLong((long) value);
539 if (v == NULL) {
540 /* Don't bother reporting this error */
541 PyErr_Clear();
542 }
543 else {
544 PyDict_SetItemString(d, name, v);
545 Py_DECREF(v);
546 }
547}
548
Segev Finer679b5662017-07-27 01:17:57 +0300549static void
550insertptr(PyObject *d, char *name, void *value)
551{
552 PyObject *v = PyLong_FromVoidPtr(value);
553 if (v == NULL) {
554 /* Don't bother reporting this error */
555 PyErr_Clear();
556 }
557 else {
558 PyDict_SetItemString(d, name, v);
559 Py_DECREF(v);
560 }
561}
562
Thomas Hellera18331d2004-07-28 20:02:52 +0000563PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000564PyInit_msvcrt(void)
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 int st;
Brian Curtin401f9f32012-05-13 11:19:23 -0500567 PyObject *d, *version;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 PyObject *m = PyModule_Create(&msvcrtmodule);
569 if (m == NULL)
570 return NULL;
571 d = PyModule_GetDict(m);
Tim Peters5fa0bd62000-12-12 01:58:56 +0000572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 /* constants for the locking() function's mode argument */
574 insertint(d, "LK_LOCK", _LK_LOCK);
575 insertint(d, "LK_NBLCK", _LK_NBLCK);
576 insertint(d, "LK_NBRLCK", _LK_NBRLCK);
577 insertint(d, "LK_RLCK", _LK_RLCK);
578 insertint(d, "LK_UNLCK", _LK_UNLCK);
579 insertint(d, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS);
580 insertint(d, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT);
581 insertint(d, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX);
582 insertint(d, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000583#ifdef _DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 insertint(d, "CRT_WARN", _CRT_WARN);
585 insertint(d, "CRT_ERROR", _CRT_ERROR);
586 insertint(d, "CRT_ASSERT", _CRT_ASSERT);
587 insertint(d, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG);
588 insertint(d, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE);
589 insertint(d, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW);
590 insertint(d, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE);
Segev Finer679b5662017-07-27 01:17:57 +0300591 insertptr(d, "CRTDBG_FILE_STDERR", _CRTDBG_FILE_STDERR);
592 insertptr(d, "CRTDBG_FILE_STDOUT", _CRTDBG_FILE_STDOUT);
593 insertptr(d, "CRTDBG_REPORT_FILE", _CRTDBG_REPORT_FILE);
Martin v. Löwis3dc33d12007-08-31 07:58:36 +0000594#endif
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 /* constants for the crt versions */
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000597#ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
599 _VC_ASSEMBLY_PUBLICKEYTOKEN);
600 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000601#endif
602#ifdef _CRT_ASSEMBLY_VERSION
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
604 _CRT_ASSEMBLY_VERSION);
605 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000606#endif
607#ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
609 __LIBRARIES_ASSEMBLY_NAME_PREFIX);
610 if (st < 0) return NULL;
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000611#endif
612
Brian Curtin401f9f32012-05-13 11:19:23 -0500613 /* constants for the 2010 crt versions */
614#if defined(_VC_CRT_MAJOR_VERSION) && defined (_VC_CRT_MINOR_VERSION) && defined(_VC_CRT_BUILD_VERSION) && defined(_VC_CRT_RBUILD_VERSION)
615 version = PyUnicode_FromFormat("%d.%d.%d.%d", _VC_CRT_MAJOR_VERSION,
616 _VC_CRT_MINOR_VERSION,
617 _VC_CRT_BUILD_VERSION,
618 _VC_CRT_RBUILD_VERSION);
619 st = PyModule_AddObject(m, "CRT_ASSEMBLY_VERSION", version);
620 if (st < 0) return NULL;
621#endif
Victor Stinnerba452952015-09-21 22:40:28 +0200622 /* make compiler warning quiet if st is unused */
623 (void)st;
Brian Curtin401f9f32012-05-13 11:19:23 -0500624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 return m;
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000626}