1 /*********************************************************
5 A Python interface to the Microsoft Visual C Runtime
6 Library, providing access to those non-portable, but
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 ***********************************************************/
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"))
30 return PyErr_SetFromErrno(PyExc_IOError
);
36 // Perform locking operations on a C runtime file descriptor.
37 static PyObject
*msvcrt_locking(PyObject
*self
, PyObject
*args
)
44 if (!PyArg_ParseTuple(args
, "iil:locking", &fd
, &mode
, &nbytes
))
47 Py_BEGIN_ALLOW_THREADS
48 err
= _locking(fd
, mode
, nbytes
);
51 return PyErr_SetFromErrno(PyExc_IOError
);
57 // Set the file translation mode for a C runtime file descriptor.
58 static PyObject
*msvcrt_setmode(PyObject
*self
, PyObject
*args
)
62 if (!PyArg_ParseTuple(args
,"ii:setmode", &fd
, &flags
))
65 flags
= _setmode(fd
, flags
);
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
)
79 if (!PyArg_ParseTuple(args
, "li:open_osfhandle", &handle
, &flags
))
82 fd
= _open_osfhandle(handle
, flags
);
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
)
95 if (!PyArg_ParseTuple(args
,"i:get_osfhandle", &fd
))
98 handle
= _get_osfhandle(fd
);
100 return PyErr_SetFromErrno(PyExc_IOError
);
102 return PyInt_FromLong(handle
);
108 static PyObject
*msvcrt_kbhit(PyObject
*self
, PyObject
*args
)
112 if (!PyArg_ParseTuple(args
, ":kbhit"))
116 return PyInt_FromLong(ok
);
119 static PyObject
*msvcrt_getch(PyObject
*self
, PyObject
*args
)
124 if (!PyArg_ParseTuple(args
, ":getch"))
127 Py_BEGIN_ALLOW_THREADS
131 return PyString_FromStringAndSize(s
, 1);
134 static PyObject
*msvcrt_getche(PyObject
*self
, PyObject
*args
)
139 if (!PyArg_ParseTuple(args
, ":getche"))
142 Py_BEGIN_ALLOW_THREADS
146 return PyString_FromStringAndSize(s
, 1);
149 static PyObject
*msvcrt_putch(PyObject
*self
, PyObject
*args
)
153 if (!PyArg_ParseTuple(args
, "c:putch", &ch
))
161 static PyObject
*msvcrt_ungetch(PyObject
*self
, PyObject
*args
)
165 if (!PyArg_ParseTuple(args
, "c:ungetch", &ch
))
168 if (_ungetch(ch
) == EOF
)
169 return PyErr_SetFromErrno(PyExc_IOError
);
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},
190 __declspec(dllexport
) void
193 Py_InitModule("msvcrt", msvcrt_functions
);