Added all documentation.
[python/dscho.git] / PC / msvcrtmodule.c
blob21be21a5c9f2cb1b6791dc19a935ad2841749739
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"
22 // Force the malloc heap to clean itself up, and free unused blocks
23 // back to the OS. (According to the docs, only works on NT.)
24 static PyObject *msvcrt_heapmin(PyObject *self, PyObject *args)
26 if (!PyArg_ParseTuple(args, ":heapmin"))
27 return NULL;
29 if (_heapmin() != 0)
30 return PyErr_SetFromErrno(PyExc_IOError);
32 Py_INCREF(Py_None);
33 return Py_None;
36 // Perform locking operations on a C runtime file descriptor.
37 static PyObject *msvcrt_locking(PyObject *self, PyObject *args)
39 int fd;
40 int mode;
41 long nbytes;
42 int err;
44 if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes))
45 return NULL;
47 Py_BEGIN_ALLOW_THREADS
48 err = _locking(fd, mode, nbytes);
49 Py_END_ALLOW_THREADS
50 if (err != 0)
51 return PyErr_SetFromErrno(PyExc_IOError);
53 Py_INCREF(Py_None);
54 return Py_None;
57 // Set the file translation mode for a C runtime file descriptor.
58 static PyObject *msvcrt_setmode(PyObject *self, PyObject *args)
60 int fd;
61 int flags;
62 if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags))
63 return NULL;
65 flags = _setmode(fd, flags);
66 if (flags == -1)
67 return PyErr_SetFromErrno(PyExc_IOError);
69 return PyInt_FromLong(flags);
72 // Convert an OS file handle to a C runtime file descriptor.
73 static PyObject *msvcrt_open_osfhandle(PyObject *self, PyObject *args)
75 long handle;
76 int flags;
77 int fd;
79 if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags))
80 return NULL;
82 fd = _open_osfhandle(handle, flags);
83 if (fd == -1)
84 return PyErr_SetFromErrno(PyExc_IOError);
86 return PyInt_FromLong(fd);
89 // Convert a C runtime file descriptor to an OS file handle.
90 static PyObject *msvcrt_get_osfhandle(PyObject *self, PyObject *args)
92 int fd;
93 long handle;
95 if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
96 return NULL;
98 handle = _get_osfhandle(fd);
99 if (handle == -1)
100 return PyErr_SetFromErrno(PyExc_IOError);
102 return PyInt_FromLong(handle);
105 /* Console I/O */
106 #include <conio.h>
108 static PyObject *msvcrt_kbhit(PyObject *self, PyObject *args)
110 int ok;
112 if (!PyArg_ParseTuple(args, ":kbhit"))
113 return NULL;
115 ok = _kbhit();
116 return PyInt_FromLong(ok);
119 static PyObject *msvcrt_getch(PyObject *self, PyObject *args)
121 int ch;
122 char s[1];
124 if (!PyArg_ParseTuple(args, ":getch"))
125 return NULL;
127 Py_BEGIN_ALLOW_THREADS
128 ch = _getch();
129 Py_END_ALLOW_THREADS
130 s[0] = ch;
131 return PyString_FromStringAndSize(s, 1);
134 static PyObject *msvcrt_getche(PyObject *self, PyObject *args)
136 int ch;
137 char s[1];
139 if (!PyArg_ParseTuple(args, ":getche"))
140 return NULL;
142 Py_BEGIN_ALLOW_THREADS
143 ch = _getche();
144 Py_END_ALLOW_THREADS
145 s[0] = ch;
146 return PyString_FromStringAndSize(s, 1);
149 static PyObject *msvcrt_putch(PyObject *self, PyObject *args)
151 char ch;
153 if (!PyArg_ParseTuple(args, "c:putch", &ch))
154 return NULL;
156 _putch(ch);
157 Py_INCREF(Py_None);
158 return Py_None;
161 static PyObject *msvcrt_ungetch(PyObject *self, PyObject *args)
163 char ch;
165 if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
166 return NULL;
168 if (_ungetch(ch) == EOF)
169 return PyErr_SetFromErrno(PyExc_IOError);
170 Py_INCREF(Py_None);
171 return Py_None;
175 /* List of functions exported by this module */
176 static struct PyMethodDef msvcrt_functions[] = {
177 {"heapmin", msvcrt_heapmin, 1},
178 {"locking", msvcrt_locking, 1},
179 {"setmode", msvcrt_setmode, 1},
180 {"open_osfhandle", msvcrt_open_osfhandle, 1},
181 {"get_osfhandle", msvcrt_get_osfhandle, 1},
182 {"kbhit", msvcrt_kbhit, 1},
183 {"getch", msvcrt_getch, 1},
184 {"getche", msvcrt_getche, 1},
185 {"putch", msvcrt_putch, 1},
186 {"ungetch", msvcrt_ungetch, 1},
187 {NULL, NULL}
190 __declspec(dllexport) void
191 initmsvcrt(void)
193 Py_InitModule("msvcrt", msvcrt_functions);