blob: e110ed8b091c4a13dcb93b7b1c49c272e8b1fcd9 [file] [log] [blame]
Guido van Rossum29c1ea51997-08-07 00:11:34 +00001/*********************************************************
2
3 msvcrtmodule.c
4
5 A Python interface to the Microsoft Visual C Runtime
6 Library, providing access to those non-portable, but
7 still useful routines.
8
9 Only ever compiled with an MS compiler, so no attempt
10 has been made to avoid MS language extensions, etc...
11
Guido van Rossum407a22d1997-08-13 19:57:53 +000012 This may only work on NT or 95...
13
14 Author: Mark Hammond and Guido van Rossum.
15 Maintenance: Guido van Rossum.
16
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
Guido van Rossum407a22d1997-08-13 19:57:53 +000025// Force the malloc heap to clean itself up, and free unused blocks
26// back to the OS. (According to the docs, only works on NT.)
Tim Peters5fa0bd62000-12-12 01:58:56 +000027static PyObject *
28msvcrt_heapmin(PyObject *self, PyObject *args)
Guido van Rossum29c1ea51997-08-07 00:11:34 +000029{
Guido van Rossum407a22d1997-08-13 19:57:53 +000030 if (!PyArg_ParseTuple(args, ":heapmin"))
Guido van Rossum29c1ea51997-08-07 00:11:34 +000031 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +000032
33 if (_heapmin() != 0)
34 return PyErr_SetFromErrno(PyExc_IOError);
35
Guido van Rossum29c1ea51997-08-07 00:11:34 +000036 Py_INCREF(Py_None);
37 return Py_None;
38}
39
Guido van Rossum407a22d1997-08-13 19:57:53 +000040// Perform locking operations on a C runtime file descriptor.
Tim Peters5fa0bd62000-12-12 01:58:56 +000041static PyObject *
42msvcrt_locking(PyObject *self, PyObject *args)
Guido van Rossum29c1ea51997-08-07 00:11:34 +000043{
Guido van Rossum407a22d1997-08-13 19:57:53 +000044 int fd;
45 int mode;
46 long nbytes;
Guido van Rossume4e021b1998-05-29 01:27:07 +000047 int err;
Guido van Rossum407a22d1997-08-13 19:57:53 +000048
49 if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes))
Guido van Rossum29c1ea51997-08-07 00:11:34 +000050 return NULL;
51
Guido van Rossume4e021b1998-05-29 01:27:07 +000052 Py_BEGIN_ALLOW_THREADS
53 err = _locking(fd, mode, nbytes);
54 Py_END_ALLOW_THREADS
55 if (err != 0)
Guido van Rossum29c1ea51997-08-07 00:11:34 +000056 return PyErr_SetFromErrno(PyExc_IOError);
57
Guido van Rossum407a22d1997-08-13 19:57:53 +000058 Py_INCREF(Py_None);
59 return Py_None;
Guido van Rossum29c1ea51997-08-07 00:11:34 +000060}
Guido van Rossum407a22d1997-08-13 19:57:53 +000061
62// Set the file translation mode for a C runtime file descriptor.
Tim Peters5fa0bd62000-12-12 01:58:56 +000063static PyObject *
64msvcrt_setmode(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +000065{
66 int fd;
67 int flags;
68 if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags))
69 return NULL;
70
71 flags = _setmode(fd, flags);
72 if (flags == -1)
73 return PyErr_SetFromErrno(PyExc_IOError);
74
75 return PyInt_FromLong(flags);
76}
77
78// Convert an OS file handle to a C runtime file descriptor.
Tim Peters5fa0bd62000-12-12 01:58:56 +000079static PyObject *
80msvcrt_open_osfhandle(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +000081{
82 long handle;
83 int flags;
84 int fd;
85
86 if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags))
Guido van Rossume4e021b1998-05-29 01:27:07 +000087 return NULL;
Guido van Rossum407a22d1997-08-13 19:57:53 +000088
89 fd = _open_osfhandle(handle, flags);
90 if (fd == -1)
91 return PyErr_SetFromErrno(PyExc_IOError);
92
93 return PyInt_FromLong(fd);
94}
95
96// Convert a C runtime file descriptor to an OS file handle.
Tim Peters5fa0bd62000-12-12 01:58:56 +000097static PyObject *
98msvcrt_get_osfhandle(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +000099{
100 int fd;
Tim Peters79248aa2001-08-29 21:37:10 +0000101 Py_intptr_t handle;
Guido van Rossum407a22d1997-08-13 19:57:53 +0000102
103 if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
104 return NULL;
105
106 handle = _get_osfhandle(fd);
107 if (handle == -1)
108 return PyErr_SetFromErrno(PyExc_IOError);
109
Fred Drake25e17262000-06-30 17:48:51 +0000110 /* technically 'handle' is not a pointer, but a integer as
111 large as a pointer, Python's *VoidPtr interface is the
112 most appropriate here */
113 return PyLong_FromVoidPtr((void*)handle);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000114}
115
116/* Console I/O */
Guido van Rossum407a22d1997-08-13 19:57:53 +0000117
Tim Peters5fa0bd62000-12-12 01:58:56 +0000118static PyObject *
119msvcrt_kbhit(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000120{
121 int ok;
122
123 if (!PyArg_ParseTuple(args, ":kbhit"))
124 return NULL;
125
126 ok = _kbhit();
127 return PyInt_FromLong(ok);
128}
129
Tim Peters5fa0bd62000-12-12 01:58:56 +0000130static PyObject *
131msvcrt_getch(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000132{
133 int ch;
134 char s[1];
135
136 if (!PyArg_ParseTuple(args, ":getch"))
137 return NULL;
138
Guido van Rossume4e021b1998-05-29 01:27:07 +0000139 Py_BEGIN_ALLOW_THREADS
Guido van Rossum407a22d1997-08-13 19:57:53 +0000140 ch = _getch();
Guido van Rossume4e021b1998-05-29 01:27:07 +0000141 Py_END_ALLOW_THREADS
Guido van Rossum407a22d1997-08-13 19:57:53 +0000142 s[0] = ch;
143 return PyString_FromStringAndSize(s, 1);
144}
145
Tim Peters5fa0bd62000-12-12 01:58:56 +0000146static PyObject *
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000147msvcrt_getwch(PyObject *self, PyObject *args)
148{
149 Py_UNICODE ch;
150 Py_UNICODE u[1];
151
152 if (!PyArg_ParseTuple(args, ":getwch"))
153 return NULL;
154
155 Py_BEGIN_ALLOW_THREADS
156 ch = _getwch();
157 Py_END_ALLOW_THREADS
158 u[0] = ch;
159 return PyUnicode_FromUnicode(u, 1);
160}
161
162static PyObject *
Tim Peters5fa0bd62000-12-12 01:58:56 +0000163msvcrt_getche(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000164{
165 int ch;
166 char s[1];
167
168 if (!PyArg_ParseTuple(args, ":getche"))
169 return NULL;
170
Guido van Rossume4e021b1998-05-29 01:27:07 +0000171 Py_BEGIN_ALLOW_THREADS
Guido van Rossum407a22d1997-08-13 19:57:53 +0000172 ch = _getche();
Guido van Rossume4e021b1998-05-29 01:27:07 +0000173 Py_END_ALLOW_THREADS
Guido van Rossum407a22d1997-08-13 19:57:53 +0000174 s[0] = ch;
175 return PyString_FromStringAndSize(s, 1);
176}
177
Tim Peters5fa0bd62000-12-12 01:58:56 +0000178static PyObject *
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000179msvcrt_getwche(PyObject *self, PyObject *args)
180{
181 Py_UNICODE ch;
182 Py_UNICODE s[1];
183
184 if (!PyArg_ParseTuple(args, ":getwche"))
185 return NULL;
186
187 Py_BEGIN_ALLOW_THREADS
188 ch = _getwche();
189 Py_END_ALLOW_THREADS
190 s[0] = ch;
191 return PyUnicode_FromUnicode(s, 1);
192}
193
194static PyObject *
Tim Peters5fa0bd62000-12-12 01:58:56 +0000195msvcrt_putch(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000196{
197 char ch;
198
199 if (!PyArg_ParseTuple(args, "c:putch", &ch))
200 return NULL;
201
202 _putch(ch);
203 Py_INCREF(Py_None);
204 return Py_None;
205}
206
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000207
208static PyObject *
209msvcrt_putwch(PyObject *self, PyObject *args)
210{
211 Py_UNICODE *ch;
212 int size;
213
214 if (!PyArg_ParseTuple(args, "u#:putwch", &ch, &size))
215 return NULL;
216
Christian Heimes61927fc2007-12-10 15:39:09 +0000217 if (size == 0) {
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000218 PyErr_SetString(PyExc_ValueError,
Christian Heimes61927fc2007-12-10 15:39:09 +0000219 "Expected unicode string of length 1");
220 return NULL;
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000221 }
Christian Heimes61927fc2007-12-10 15:39:09 +0000222 _putwch(*ch);
223 Py_RETURN_NONE;
224
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000225}
226
Tim Peters5fa0bd62000-12-12 01:58:56 +0000227static PyObject *
228msvcrt_ungetch(PyObject *self, PyObject *args)
Guido van Rossum407a22d1997-08-13 19:57:53 +0000229{
230 char ch;
231
232 if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
233 return NULL;
234
Guido van Rossum6543e881999-02-16 19:40:02 +0000235 if (_ungetch(ch) == EOF)
236 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum407a22d1997-08-13 19:57:53 +0000237 Py_INCREF(Py_None);
238 return Py_None;
239}
240
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000241static PyObject *
242msvcrt_ungetwch(PyObject *self, PyObject *args)
243{
244 Py_UNICODE ch;
245
246 if (!PyArg_ParseTuple(args, "u:ungetwch", &ch))
247 return NULL;
248
249 if (_ungetch(ch) == EOF)
250 return PyErr_SetFromErrno(PyExc_IOError);
251 Py_INCREF(Py_None);
252 return Py_None;
253}
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000254
Tim Peters5fa0bd62000-12-12 01:58:56 +0000255static void
256insertint(PyObject *d, char *name, int value)
257{
258 PyObject *v = PyInt_FromLong((long) value);
259 if (v == NULL) {
260 /* Don't bother reporting this error */
261 PyErr_Clear();
262 }
263 else {
264 PyDict_SetItemString(d, name, v);
265 Py_DECREF(v);
266 }
267}
268
269
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000270/* List of functions exported by this module */
271static struct PyMethodDef msvcrt_functions[] = {
Neal Norwitz031829d2002-03-31 14:37:44 +0000272 {"heapmin", msvcrt_heapmin, METH_VARARGS},
273 {"locking", msvcrt_locking, METH_VARARGS},
274 {"setmode", msvcrt_setmode, METH_VARARGS},
275 {"open_osfhandle", msvcrt_open_osfhandle, METH_VARARGS},
276 {"get_osfhandle", msvcrt_get_osfhandle, METH_VARARGS},
277 {"kbhit", msvcrt_kbhit, METH_VARARGS},
278 {"getch", msvcrt_getch, METH_VARARGS},
279 {"getche", msvcrt_getche, METH_VARARGS},
280 {"putch", msvcrt_putch, METH_VARARGS},
281 {"ungetch", msvcrt_ungetch, METH_VARARGS},
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000282 {"getwch", msvcrt_getwch, METH_VARARGS},
283 {"getwche", msvcrt_getwche, METH_VARARGS},
284 {"putwch", msvcrt_putwch, METH_VARARGS},
285 {"ungetwch", msvcrt_ungetwch, METH_VARARGS},
286
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000287 {NULL, NULL}
288};
289
Thomas Hellera18331d2004-07-28 20:02:52 +0000290PyMODINIT_FUNC
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000291initmsvcrt(void)
292{
Tim Peters2188bf02006-01-19 15:21:30 +0000293 PyObject *d;
Tim Peters5fa0bd62000-12-12 01:58:56 +0000294 PyObject *m = Py_InitModule("msvcrt", msvcrt_functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000295 if (m == NULL)
296 return;
Tim Peters2188bf02006-01-19 15:21:30 +0000297 d = PyModule_GetDict(m);
Tim Peters5fa0bd62000-12-12 01:58:56 +0000298
299 /* constants for the locking() function's mode argument */
300 insertint(d, "LK_LOCK", _LK_LOCK);
301 insertint(d, "LK_NBLCK", _LK_NBLCK);
302 insertint(d, "LK_NBRLCK", _LK_NBRLCK);
303 insertint(d, "LK_RLCK", _LK_RLCK);
304 insertint(d, "LK_UNLCK", _LK_UNLCK);
Guido van Rossum29c1ea51997-08-07 00:11:34 +0000305}