Add Marcos Donolo for work on issue 7534 patch.
[python.git] / PC / msvcrtmodule.c
blob24e0cd521240b7a2d6ed3fdb38d66e2fb6892123
1 /*********************************************************
3 msvcrtmodule.c
5 A Python interface to the Microsoft Visual C Runtime
6 Library, providing access to those non-portable, but
7 still useful routines.
9 Only ever compiled with an MS compiler, so no attempt
10 has been made to avoid MS language extensions, etc...
12 This may only work on NT or 95...
14 Author: Mark Hammond and Guido van Rossum.
15 Maintenance: Guido van Rossum.
17 ***********************************************************/
19 #include "Python.h"
20 #include "malloc.h"
21 #include <io.h>
22 #include <conio.h>
23 #include <sys/locking.h>
25 #ifdef _MSC_VER
26 #if _MSC_VER >= 1500
27 #include <crtassem.h>
28 #endif
29 #endif
31 // 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.)
33 static PyObject *
34 msvcrt_heapmin(PyObject *self, PyObject *args)
36 if (!PyArg_ParseTuple(args, ":heapmin"))
37 return NULL;
39 if (_heapmin() != 0)
40 return PyErr_SetFromErrno(PyExc_IOError);
42 Py_INCREF(Py_None);
43 return Py_None;
46 PyDoc_STRVAR(heapmin_doc,
47 "heapmin() -> None\n\
48 \n\
49 Force the malloc() heap to clean itself up and return unused blocks\n\
50 to the operating system. This only works on Windows NT. On failure,\n\
51 this raises IOError.");
53 // Perform locking operations on a C runtime file descriptor.
54 static PyObject *
55 msvcrt_locking(PyObject *self, PyObject *args)
57 int fd;
58 int mode;
59 long nbytes;
60 int err;
62 if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes))
63 return NULL;
65 Py_BEGIN_ALLOW_THREADS
66 err = _locking(fd, mode, nbytes);
67 Py_END_ALLOW_THREADS
68 if (err != 0)
69 return PyErr_SetFromErrno(PyExc_IOError);
71 Py_INCREF(Py_None);
72 return Py_None;
75 PyDoc_STRVAR(locking_doc,
76 "locking(fd, mode, nbytes) -> None\n\
77 \n\
78 Lock part of a file based on file descriptor fd from the C runtime.\n\
79 Raises IOError on failure. The locked region of the file extends from\n\
80 the current file position for nbytes bytes, and may continue beyond\n\
81 the end of the file. mode must be one of the LK_* constants listed\n\
82 below. Multiple regions in a file may be locked at the same time, but\n\
83 may not overlap. Adjacent regions are not merged; they must be unlocked\n\
84 individually.");
86 // Set the file translation mode for a C runtime file descriptor.
87 static PyObject *
88 msvcrt_setmode(PyObject *self, PyObject *args)
90 int fd;
91 int flags;
92 if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags))
93 return NULL;
95 flags = _setmode(fd, flags);
96 if (flags == -1)
97 return PyErr_SetFromErrno(PyExc_IOError);
99 return PyInt_FromLong(flags);
102 PyDoc_STRVAR(setmode_doc,
103 "setmode(fd, mode) -> Previous mode\n\
105 Set the line-end translation mode for the file descriptor fd. To set\n\
106 it to text mode, flags should be os.O_TEXT; for binary, it should be\n\
107 os.O_BINARY.");
109 // Convert an OS file handle to a C runtime file descriptor.
110 static PyObject *
111 msvcrt_open_osfhandle(PyObject *self, PyObject *args)
113 long handle;
114 int flags;
115 int fd;
117 if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags))
118 return NULL;
120 fd = _open_osfhandle(handle, flags);
121 if (fd == -1)
122 return PyErr_SetFromErrno(PyExc_IOError);
124 return PyInt_FromLong(fd);
127 PyDoc_STRVAR(open_osfhandle_doc,
128 "open_osfhandle(handle, flags) -> file descriptor\n\
130 Create a C runtime file descriptor from the file handle handle. The\n\
131 flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY,\n\
132 and os.O_TEXT. The returned file descriptor may be used as a parameter\n\
133 to os.fdopen() to create a file object.");
135 // Convert a C runtime file descriptor to an OS file handle.
136 static PyObject *
137 msvcrt_get_osfhandle(PyObject *self, PyObject *args)
139 int fd;
140 Py_intptr_t handle;
142 if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
143 return NULL;
145 handle = _get_osfhandle(fd);
146 if (handle == -1)
147 return PyErr_SetFromErrno(PyExc_IOError);
149 /* technically 'handle' is not a pointer, but a integer as
150 large as a pointer, Python's *VoidPtr interface is the
151 most appropriate here */
152 return PyLong_FromVoidPtr((void*)handle);
155 PyDoc_STRVAR(get_osfhandle_doc,
156 "get_osfhandle(fd) -> file handle\n\
158 Return the file handle for the file descriptor fd. Raises IOError\n\
159 if fd is not recognized.");
161 /* Console I/O */
163 static PyObject *
164 msvcrt_kbhit(PyObject *self, PyObject *args)
166 int ok;
168 if (!PyArg_ParseTuple(args, ":kbhit"))
169 return NULL;
171 ok = _kbhit();
172 return PyInt_FromLong(ok);
175 PyDoc_STRVAR(kbhit_doc,
176 "kbhit() -> bool\n\
178 Return true if a keypress is waiting to be read.");
180 static PyObject *
181 msvcrt_getch(PyObject *self, PyObject *args)
183 int ch;
184 char s[1];
186 if (!PyArg_ParseTuple(args, ":getch"))
187 return NULL;
189 Py_BEGIN_ALLOW_THREADS
190 ch = _getch();
191 Py_END_ALLOW_THREADS
192 s[0] = ch;
193 return PyString_FromStringAndSize(s, 1);
196 PyDoc_STRVAR(getch_doc,
197 "getch() -> key character\n\
199 Read a keypress and return the resulting character. Nothing is echoed to\n\
200 the console. This call will block if a keypress is not already\n\
201 available, but will not wait for Enter to be pressed. If the pressed key\n\
202 was a special function key, this will return '\\000' or '\\xe0'; the next\n\
203 call will return the keycode. The Control-C keypress cannot be read with\n\
204 this function.");
206 #ifdef _WCONIO_DEFINED
207 static PyObject *
208 msvcrt_getwch(PyObject *self, PyObject *args)
210 Py_UNICODE ch;
211 Py_UNICODE u[1];
213 if (!PyArg_ParseTuple(args, ":getwch"))
214 return NULL;
216 Py_BEGIN_ALLOW_THREADS
217 ch = _getwch();
218 Py_END_ALLOW_THREADS
219 u[0] = ch;
220 return PyUnicode_FromUnicode(u, 1);
223 PyDoc_STRVAR(getwch_doc,
224 "getwch() -> Unicode key character\n\
226 Wide char variant of getch(), returning a Unicode value.");
227 #endif
229 static PyObject *
230 msvcrt_getche(PyObject *self, PyObject *args)
232 int ch;
233 char s[1];
235 if (!PyArg_ParseTuple(args, ":getche"))
236 return NULL;
238 Py_BEGIN_ALLOW_THREADS
239 ch = _getche();
240 Py_END_ALLOW_THREADS
241 s[0] = ch;
242 return PyString_FromStringAndSize(s, 1);
245 PyDoc_STRVAR(getche_doc,
246 "getche() -> key character\n\
248 Similar to getch(), but the keypress will be echoed if it represents\n\
249 a printable character.");
251 #ifdef _WCONIO_DEFINED
252 static PyObject *
253 msvcrt_getwche(PyObject *self, PyObject *args)
255 Py_UNICODE ch;
256 Py_UNICODE s[1];
258 if (!PyArg_ParseTuple(args, ":getwche"))
259 return NULL;
261 Py_BEGIN_ALLOW_THREADS
262 ch = _getwche();
263 Py_END_ALLOW_THREADS
264 s[0] = ch;
265 return PyUnicode_FromUnicode(s, 1);
268 PyDoc_STRVAR(getwche_doc,
269 "getwche() -> Unicode key character\n\
271 Wide char variant of getche(), returning a Unicode value.");
272 #endif
274 static PyObject *
275 msvcrt_putch(PyObject *self, PyObject *args)
277 char ch;
279 if (!PyArg_ParseTuple(args, "c:putch", &ch))
280 return NULL;
282 _putch(ch);
283 Py_INCREF(Py_None);
284 return Py_None;
287 PyDoc_STRVAR(putch_doc,
288 "putch(char) -> None\n\
290 Print the character char to the console without buffering.");
292 #ifdef _WCONIO_DEFINED
293 static PyObject *
294 msvcrt_putwch(PyObject *self, PyObject *args)
296 Py_UNICODE *ch;
297 int size;
299 if (!PyArg_ParseTuple(args, "u#:putwch", &ch, &size))
300 return NULL;
302 if (size == 0) {
303 PyErr_SetString(PyExc_ValueError,
304 "Expected unicode string of length 1");
305 return NULL;
307 _putwch(*ch);
308 Py_RETURN_NONE;
312 PyDoc_STRVAR(putwch_doc,
313 "putwch(unicode_char) -> None\n\
315 Wide char variant of putch(), accepting a Unicode value.");
316 #endif
318 static PyObject *
319 msvcrt_ungetch(PyObject *self, PyObject *args)
321 char ch;
323 if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
324 return NULL;
326 if (_ungetch(ch) == EOF)
327 return PyErr_SetFromErrno(PyExc_IOError);
328 Py_INCREF(Py_None);
329 return Py_None;
332 PyDoc_STRVAR(ungetch_doc,
333 "ungetch(char) -> None\n\
335 Cause the character char to be \"pushed back\" into the console buffer;\n\
336 it will be the next character read by getch() or getche().");
338 #ifdef _WCONIO_DEFINED
339 static PyObject *
340 msvcrt_ungetwch(PyObject *self, PyObject *args)
342 Py_UNICODE ch;
344 if (!PyArg_ParseTuple(args, "u:ungetwch", &ch))
345 return NULL;
347 if (_ungetch(ch) == EOF)
348 return PyErr_SetFromErrno(PyExc_IOError);
349 Py_INCREF(Py_None);
350 return Py_None;
353 PyDoc_STRVAR(ungetwch_doc,
354 "ungetwch(unicode_char) -> None\n\
356 Wide char variant of ungetch(), accepting a Unicode value.");
357 #endif
359 static void
360 insertint(PyObject *d, char *name, int value)
362 PyObject *v = PyInt_FromLong((long) value);
363 if (v == NULL) {
364 /* Don't bother reporting this error */
365 PyErr_Clear();
367 else {
368 PyDict_SetItemString(d, name, v);
369 Py_DECREF(v);
374 /* List of functions exported by this module */
375 static struct PyMethodDef msvcrt_functions[] = {
376 {"heapmin", msvcrt_heapmin, METH_VARARGS, heapmin_doc},
377 {"locking", msvcrt_locking, METH_VARARGS, locking_doc},
378 {"setmode", msvcrt_setmode, METH_VARARGS, setmode_doc},
379 {"open_osfhandle", msvcrt_open_osfhandle, METH_VARARGS, open_osfhandle_doc},
380 {"get_osfhandle", msvcrt_get_osfhandle, METH_VARARGS, get_osfhandle_doc},
381 {"kbhit", msvcrt_kbhit, METH_VARARGS, kbhit_doc},
382 {"getch", msvcrt_getch, METH_VARARGS, getch_doc},
383 {"getche", msvcrt_getche, METH_VARARGS, getche_doc},
384 {"putch", msvcrt_putch, METH_VARARGS, putch_doc},
385 {"ungetch", msvcrt_ungetch, METH_VARARGS, ungetch_doc},
386 #ifdef _WCONIO_DEFINED
387 {"getwch", msvcrt_getwch, METH_VARARGS, getwch_doc},
388 {"getwche", msvcrt_getwche, METH_VARARGS, getwche_doc},
389 {"putwch", msvcrt_putwch, METH_VARARGS, putwch_doc},
390 {"ungetwch", msvcrt_ungetwch, METH_VARARGS, ungetwch_doc},
391 #endif
392 {NULL, NULL}
395 PyMODINIT_FUNC
396 initmsvcrt(void)
398 int st;
399 PyObject *d;
400 PyObject *m = Py_InitModule("msvcrt", msvcrt_functions);
401 if (m == NULL)
402 return;
403 d = PyModule_GetDict(m);
405 /* constants for the locking() function's mode argument */
406 insertint(d, "LK_LOCK", _LK_LOCK);
407 insertint(d, "LK_NBLCK", _LK_NBLCK);
408 insertint(d, "LK_NBRLCK", _LK_NBRLCK);
409 insertint(d, "LK_RLCK", _LK_RLCK);
410 insertint(d, "LK_UNLCK", _LK_UNLCK);
412 /* constants for the crt versions */
413 #ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
414 st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
415 _VC_ASSEMBLY_PUBLICKEYTOKEN);
416 if (st < 0)return;
417 #endif
418 #ifdef _CRT_ASSEMBLY_VERSION
419 st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
420 _CRT_ASSEMBLY_VERSION);
421 if (st < 0)return;
422 #endif
423 #ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
424 st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
425 __LIBRARIES_ASSEMBLY_NAME_PREFIX);
426 if (st < 0)return;
427 #endif