blob: 44c82e4594ff6992ab958449d8b737d3da1b81b9 [file] [log] [blame]
Guido van Rossum29c1ea51997-08-07 00:11:34 +00001/*********************************************************
2
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003 msvcrtmodule.c
Guido van Rossum29c1ea51997-08-07 00:11:34 +00004
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +000012 This may only work on NT or 95...
Guido van Rossum407a22d1997-08-13 19:57:53 +000013
Antoine Pitrouc83ea132010-05-09 14:46:46 +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>
Guido van Rossum29c1ea51997-08-07 00:11:34 +000024
Martin v. Löwisbcb017f2008-11-30 19:28:27 +000025#ifdef _MSC_VER
Martin v. Löwis413fabc2010-02-18 09:22:20 +000026#if _MSC_VER >= 1500 && _MSC_VER < 1600
Martin v. Löwisbcb017f2008-11-30 19:28:27 +000027#include <crtassem.h>
28#endif
29#endif
30
Guido van Rossum407a22d1997-08-13 19:57:53 +000031// Force the malloc heap to clean itself up, and free unused blocks
32// back to the OS. (According to the docs, only works on NT.)
Tim Peters5fa0bd62000-12-12 01:58:56 +000033static PyObject *
34msvcrt_heapmin(PyObject *self, PyObject *args)
Guido van Rossum29c1ea51997-08-07 00:11:34 +000035{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000036 if (!PyArg_ParseTuple(args, ":heapmin"))
37 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +000038
Antoine Pitrouc83ea132010-05-09 14:46:46 +000039 if (_heapmin() != 0)
40 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +000041
Antoine Pitrouc83ea132010-05-09 14:46:46 +000042 Py_INCREF(Py_None);
43 return Py_None;
Guido van Rossum29c1ea51997-08-07 00:11:34 +000044}
45
Amaury Forgeot d'Arcc8a2ce72009-12-29 23:06:17 +000046PyDoc_STRVAR(heapmin_doc,
47"heapmin() -> None\n\
48\n\
49Force the malloc() heap to clean itself up and return unused blocks\n\
Georg Brandlb7953f02009-12-30 19:03:00 +000050to the operating system. On failure, this raises IOError.");
Amaury Forgeot d'Arcc8a2ce72009-12-29 23:06:17 +000051
Guido van Rossum407a22d1997-08-13 19:57:53 +000052// Perform locking operations on a C runtime file descriptor.
Tim Peters5fa0bd62000-12-12 01:58:56 +000053static PyObject *
54msvcrt_locking(PyObject *self, PyObject *args)
Guido van Rossum29c1ea51997-08-07 00:11:34 +000055{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000056 int fd;
57 int mode;
58 long nbytes;
59 int err;
Guido van Rossum407a22d1997-08-13 19:57:53 +000060
Antoine Pitrouc83ea132010-05-09 14:46:46 +000061 if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes))
62 return NULL;
Guido van Rossum29c1ea51997-08-07 00:11:34 +000063
Antoine Pitrouc83ea132010-05-09 14:46:46 +000064 Py_BEGIN_ALLOW_THREADS
65 err = _locking(fd, mode, nbytes);
66 Py_END_ALLOW_THREADS
67 if (err != 0)
68 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum29c1ea51997-08-07 00:11:34 +000069
Antoine Pitrouc83ea132010-05-09 14:46:46 +000070 Py_INCREF(Py_None);
71 return Py_None;
Guido van Rossum29c1ea51997-08-07 00:11:34 +000072}
Guido van Rossum407a22d1997-08-13 19:57:53 +000073
Amaury Forgeot d'Arcc8a2ce72009-12-29 23:06:17 +000074PyDoc_STRVAR(locking_doc,
75"locking(fd, mode, nbytes) -> None\n\
76\n\
77Lock part of a file based on file descriptor fd from the C runtime.\n\
78Raises IOError on failure. The locked region of the file extends from\n\
79the current file position for nbytes bytes, and may continue beyond\n\
80the end of the file. mode must be one of the LK_* constants listed\n\
81below. Multiple regions in a file may be locked at the same time, but\n\
82may not overlap. Adjacent regions are not merged; they must be unlocked\n\
83individually.");
84
Guido van Rossum407a22d1997-08-13 19:57:53 +000085// Set the file translation mode for a C runtime file descriptor.
Tim Peters5fa0bd62000-12-12 01:58:56 +000086static PyObject *
87msvcrt_setmode(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +000088{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000089 int fd;
90 int flags;
91 if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags))
92 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +000093
Antoine Pitrouc83ea132010-05-09 14:46:46 +000094 flags = _setmode(fd, flags);
95 if (flags == -1)
96 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +000097
Antoine Pitrouc83ea132010-05-09 14:46:46 +000098 return PyInt_FromLong(flags);
Guido van Rossum407a22d1997-08-13 19:57:53 +000099}
100
Amaury Forgeot d'Arcc8a2ce72009-12-29 23:06:17 +0000101PyDoc_STRVAR(setmode_doc,
102"setmode(fd, mode) -> Previous mode\n\
103\n\
104Set the line-end translation mode for the file descriptor fd. To set\n\
105it to text mode, flags should be os.O_TEXT; for binary, it should be\n\
106os.O_BINARY.");
107
Guido van Rossum407a22d1997-08-13 19:57:53 +0000108// Convert an OS file handle to a C runtime file descriptor.
Tim Peters5fa0bd62000-12-12 01:58:56 +0000109static PyObject *
110msvcrt_open_osfhandle(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000111{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000112 long handle;
113 int flags;
114 int fd;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000115
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000116 if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags))
117 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000118
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000119 fd = _open_osfhandle(handle, flags);
120 if (fd == -1)
121 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000122
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000123 return PyInt_FromLong(fd);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000124}
125
Amaury Forgeot d'Arcc8a2ce72009-12-29 23:06:17 +0000126PyDoc_STRVAR(open_osfhandle_doc,
127"open_osfhandle(handle, flags) -> file descriptor\n\
128\n\
129Create a C runtime file descriptor from the file handle handle. The\n\
130flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY,\n\
131and os.O_TEXT. The returned file descriptor may be used as a parameter\n\
132to os.fdopen() to create a file object.");
133
Guido van Rossum407a22d1997-08-13 19:57:53 +0000134// Convert a C runtime file descriptor to an OS file handle.
Tim Peters5fa0bd62000-12-12 01:58:56 +0000135static PyObject *
136msvcrt_get_osfhandle(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000137{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000138 int fd;
139 Py_intptr_t handle;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000140
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000141 if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
142 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000143
Antoine Pitroue6ebcda2010-09-04 21:24:42 +0000144 if (!_PyVerify_fd(fd))
145 return PyErr_SetFromErrno(PyExc_IOError);
146
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000147 handle = _get_osfhandle(fd);
148 if (handle == -1)
149 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000150
Martin Panterb362f752015-11-02 03:37:02 +0000151 /* technically 'handle' is not a pointer, but an integer as
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000152 large as a pointer, Python's *VoidPtr interface is the
153 most appropriate here */
154 return PyLong_FromVoidPtr((void*)handle);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000155}
156
Amaury Forgeot d'Arcc8a2ce72009-12-29 23:06:17 +0000157PyDoc_STRVAR(get_osfhandle_doc,
158"get_osfhandle(fd) -> file handle\n\
159\n\
160Return the file handle for the file descriptor fd. Raises IOError\n\
161if fd is not recognized.");
162
Guido van Rossum407a22d1997-08-13 19:57:53 +0000163/* Console I/O */
Guido van Rossum407a22d1997-08-13 19:57:53 +0000164
Tim Peters5fa0bd62000-12-12 01:58:56 +0000165static PyObject *
166msvcrt_kbhit(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000167{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000168 int ok;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000169
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000170 if (!PyArg_ParseTuple(args, ":kbhit"))
171 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000172
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000173 ok = _kbhit();
174 return PyInt_FromLong(ok);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000175}
176
Amaury Forgeot d'Arcc8a2ce72009-12-29 23:06:17 +0000177PyDoc_STRVAR(kbhit_doc,
178"kbhit() -> bool\n\
179\n\
180Return true if a keypress is waiting to be read.");
181
Tim Peters5fa0bd62000-12-12 01:58:56 +0000182static PyObject *
183msvcrt_getch(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000184{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000185 int ch;
186 char s[1];
Guido van Rossum407a22d1997-08-13 19:57:53 +0000187
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000188 if (!PyArg_ParseTuple(args, ":getch"))
189 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000190
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000191 Py_BEGIN_ALLOW_THREADS
192 ch = _getch();
193 Py_END_ALLOW_THREADS
194 s[0] = ch;
195 return PyString_FromStringAndSize(s, 1);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000196}
197
Amaury Forgeot d'Arcc8a2ce72009-12-29 23:06:17 +0000198PyDoc_STRVAR(getch_doc,
199"getch() -> key character\n\
200\n\
201Read a keypress and return the resulting character. Nothing is echoed to\n\
202the console. This call will block if a keypress is not already\n\
203available, but will not wait for Enter to be pressed. If the pressed key\n\
204was a special function key, this will return '\\000' or '\\xe0'; the next\n\
205call will return the keycode. The Control-C keypress cannot be read with\n\
206this function.");
207
Amaury Forgeot d'Arca4dd2e22008-06-13 00:42:22 +0000208#ifdef _WCONIO_DEFINED
Tim Peters5fa0bd62000-12-12 01:58:56 +0000209static PyObject *
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000210msvcrt_getwch(PyObject *self, PyObject *args)
211{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000212 Py_UNICODE ch;
213 Py_UNICODE u[1];
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000214
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000215 if (!PyArg_ParseTuple(args, ":getwch"))
216 return NULL;
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000217
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000218 Py_BEGIN_ALLOW_THREADS
219 ch = _getwch();
220 Py_END_ALLOW_THREADS
221 u[0] = ch;
222 return PyUnicode_FromUnicode(u, 1);
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000223}
Amaury Forgeot d'Arcc8a2ce72009-12-29 23:06:17 +0000224
225PyDoc_STRVAR(getwch_doc,
226"getwch() -> Unicode key character\n\
227\n\
228Wide char variant of getch(), returning a Unicode value.");
Amaury Forgeot d'Arca4dd2e22008-06-13 00:42:22 +0000229#endif
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000230
231static PyObject *
Tim Peters5fa0bd62000-12-12 01:58:56 +0000232msvcrt_getche(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000233{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000234 int ch;
235 char s[1];
Guido van Rossum407a22d1997-08-13 19:57:53 +0000236
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000237 if (!PyArg_ParseTuple(args, ":getche"))
238 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000239
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000240 Py_BEGIN_ALLOW_THREADS
241 ch = _getche();
242 Py_END_ALLOW_THREADS
243 s[0] = ch;
244 return PyString_FromStringAndSize(s, 1);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000245}
246
Amaury Forgeot d'Arcc8a2ce72009-12-29 23:06:17 +0000247PyDoc_STRVAR(getche_doc,
248"getche() -> key character\n\
249\n\
250Similar to getch(), but the keypress will be echoed if it represents\n\
251a printable character.");
252
Amaury Forgeot d'Arca4dd2e22008-06-13 00:42:22 +0000253#ifdef _WCONIO_DEFINED
Tim Peters5fa0bd62000-12-12 01:58:56 +0000254static PyObject *
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000255msvcrt_getwche(PyObject *self, PyObject *args)
256{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000257 Py_UNICODE ch;
258 Py_UNICODE s[1];
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000259
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000260 if (!PyArg_ParseTuple(args, ":getwche"))
261 return NULL;
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000262
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000263 Py_BEGIN_ALLOW_THREADS
264 ch = _getwche();
265 Py_END_ALLOW_THREADS
266 s[0] = ch;
267 return PyUnicode_FromUnicode(s, 1);
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000268}
Amaury Forgeot d'Arcc8a2ce72009-12-29 23:06:17 +0000269
270PyDoc_STRVAR(getwche_doc,
271"getwche() -> Unicode key character\n\
272\n\
273Wide char variant of getche(), returning a Unicode value.");
Amaury Forgeot d'Arca4dd2e22008-06-13 00:42:22 +0000274#endif
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000275
276static PyObject *
Tim Peters5fa0bd62000-12-12 01:58:56 +0000277msvcrt_putch(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000278{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000279 char ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000280
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000281 if (!PyArg_ParseTuple(args, "c:putch", &ch))
282 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000283
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000284 _putch(ch);
285 Py_INCREF(Py_None);
286 return Py_None;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000287}
288
Amaury Forgeot d'Arcc8a2ce72009-12-29 23:06:17 +0000289PyDoc_STRVAR(putch_doc,
290"putch(char) -> None\n\
291\n\
292Print the character char to the console without buffering.");
293
Amaury Forgeot d'Arca4dd2e22008-06-13 00:42:22 +0000294#ifdef _WCONIO_DEFINED
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000295static PyObject *
296msvcrt_putwch(PyObject *self, PyObject *args)
297{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000298 Py_UNICODE *ch;
299 int size;
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000300
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000301 if (!PyArg_ParseTuple(args, "u#:putwch", &ch, &size))
302 return NULL;
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000303
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000304 if (size == 0) {
305 PyErr_SetString(PyExc_ValueError,
306 "Expected unicode string of length 1");
307 return NULL;
308 }
309 _putwch(*ch);
310 Py_RETURN_NONE;
Christian Heimes61927fc2007-12-10 15:39:09 +0000311
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000312}
Amaury Forgeot d'Arcc8a2ce72009-12-29 23:06:17 +0000313
314PyDoc_STRVAR(putwch_doc,
315"putwch(unicode_char) -> None\n\
316\n\
317Wide char variant of putch(), accepting a Unicode value.");
Amaury Forgeot d'Arca4dd2e22008-06-13 00:42:22 +0000318#endif
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000319
Tim Peters5fa0bd62000-12-12 01:58:56 +0000320static PyObject *
321msvcrt_ungetch(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000322{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000323 char ch;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000324
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000325 if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
326 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000327
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000328 if (_ungetch(ch) == EOF)
329 return PyErr_SetFromErrno(PyExc_IOError);
330 Py_INCREF(Py_None);
331 return Py_None;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000332}
333
Amaury Forgeot d'Arcc8a2ce72009-12-29 23:06:17 +0000334PyDoc_STRVAR(ungetch_doc,
335"ungetch(char) -> None\n\
336\n\
337Cause the character char to be \"pushed back\" into the console buffer;\n\
338it will be the next character read by getch() or getche().");
339
Amaury Forgeot d'Arca4dd2e22008-06-13 00:42:22 +0000340#ifdef _WCONIO_DEFINED
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000341static PyObject *
342msvcrt_ungetwch(PyObject *self, PyObject *args)
343{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000344 Py_UNICODE ch;
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000345
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000346 if (!PyArg_ParseTuple(args, "u:ungetwch", &ch))
347 return NULL;
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000348
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000349 if (_ungetch(ch) == EOF)
350 return PyErr_SetFromErrno(PyExc_IOError);
351 Py_INCREF(Py_None);
352 return Py_None;
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000353}
Amaury Forgeot d'Arcc8a2ce72009-12-29 23:06:17 +0000354
355PyDoc_STRVAR(ungetwch_doc,
356"ungetwch(unicode_char) -> None\n\
357\n\
358Wide char variant of ungetch(), accepting a Unicode value.");
Amaury Forgeot d'Arca4dd2e22008-06-13 00:42:22 +0000359#endif
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000360
Tim Peters5fa0bd62000-12-12 01:58:56 +0000361static void
362insertint(PyObject *d, char *name, int value)
363{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000364 PyObject *v = PyInt_FromLong((long) value);
365 if (v == NULL) {
366 /* Don't bother reporting this error */
367 PyErr_Clear();
368 }
369 else {
370 PyDict_SetItemString(d, name, v);
371 Py_DECREF(v);
372 }
Tim Peters5fa0bd62000-12-12 01:58:56 +0000373}
374
375
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000376/* List of functions exported by this module */
377static struct PyMethodDef msvcrt_functions[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000378 {"heapmin", msvcrt_heapmin, METH_VARARGS, heapmin_doc},
379 {"locking", msvcrt_locking, METH_VARARGS, locking_doc},
380 {"setmode", msvcrt_setmode, METH_VARARGS, setmode_doc},
381 {"open_osfhandle", msvcrt_open_osfhandle, METH_VARARGS, open_osfhandle_doc},
382 {"get_osfhandle", msvcrt_get_osfhandle, METH_VARARGS, get_osfhandle_doc},
383 {"kbhit", msvcrt_kbhit, METH_VARARGS, kbhit_doc},
384 {"getch", msvcrt_getch, METH_VARARGS, getch_doc},
385 {"getche", msvcrt_getche, METH_VARARGS, getche_doc},
386 {"putch", msvcrt_putch, METH_VARARGS, putch_doc},
387 {"ungetch", msvcrt_ungetch, METH_VARARGS, ungetch_doc},
Amaury Forgeot d'Arca4dd2e22008-06-13 00:42:22 +0000388#ifdef _WCONIO_DEFINED
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000389 {"getwch", msvcrt_getwch, METH_VARARGS, getwch_doc},
390 {"getwche", msvcrt_getwche, METH_VARARGS, getwche_doc},
391 {"putwch", msvcrt_putwch, METH_VARARGS, putwch_doc},
392 {"ungetwch", msvcrt_ungetwch, METH_VARARGS, ungetwch_doc},
Amaury Forgeot d'Arca4dd2e22008-06-13 00:42:22 +0000393#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000394 {NULL, NULL}
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000395};
396
Thomas Hellera18331d2004-07-28 20:02:52 +0000397PyMODINIT_FUNC
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000398initmsvcrt(void)
399{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000400 int st;
401 PyObject *d;
402 PyObject *m = Py_InitModule("msvcrt", msvcrt_functions);
403 if (m == NULL)
404 return;
405 d = PyModule_GetDict(m);
Tim Peters5fa0bd62000-12-12 01:58:56 +0000406
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000407 /* constants for the locking() function's mode argument */
408 insertint(d, "LK_LOCK", _LK_LOCK);
409 insertint(d, "LK_NBLCK", _LK_NBLCK);
410 insertint(d, "LK_NBRLCK", _LK_NBRLCK);
411 insertint(d, "LK_RLCK", _LK_RLCK);
412 insertint(d, "LK_UNLCK", _LK_UNLCK);
Martin v. Löwisbcb017f2008-11-30 19:28:27 +0000413
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000414 /* constants for the crt versions */
Martin v. Löwisbcb017f2008-11-30 19:28:27 +0000415#ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000416 st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
417 _VC_ASSEMBLY_PUBLICKEYTOKEN);
418 if (st < 0)return;
Martin v. Löwisbcb017f2008-11-30 19:28:27 +0000419#endif
420#ifdef _CRT_ASSEMBLY_VERSION
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000421 st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
422 _CRT_ASSEMBLY_VERSION);
423 if (st < 0)return;
Martin v. Löwisbcb017f2008-11-30 19:28:27 +0000424#endif
425#ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000426 st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
427 __LIBRARIES_ASSEMBLY_NAME_PREFIX);
428 if (st < 0)return;
Martin v. Löwisbcb017f2008-11-30 19:28:27 +0000429#endif
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000430}