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 ***********************************************************/
23 #include <sys/locking.h>
25 // 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.)
28 msvcrt_heapmin(PyObject
*self
, PyObject
*args
)
30 if (!PyArg_ParseTuple(args
, ":heapmin"))
34 return PyErr_SetFromErrno(PyExc_IOError
);
40 // Perform locking operations on a C runtime file descriptor.
42 msvcrt_locking(PyObject
*self
, PyObject
*args
)
49 if (!PyArg_ParseTuple(args
, "iil:locking", &fd
, &mode
, &nbytes
))
52 Py_BEGIN_ALLOW_THREADS
53 err
= _locking(fd
, mode
, nbytes
);
56 return PyErr_SetFromErrno(PyExc_IOError
);
62 // Set the file translation mode for a C runtime file descriptor.
64 msvcrt_setmode(PyObject
*self
, PyObject
*args
)
68 if (!PyArg_ParseTuple(args
,"ii:setmode", &fd
, &flags
))
71 flags
= _setmode(fd
, flags
);
73 return PyErr_SetFromErrno(PyExc_IOError
);
75 return PyInt_FromLong(flags
);
78 // Convert an OS file handle to a C runtime file descriptor.
80 msvcrt_open_osfhandle(PyObject
*self
, PyObject
*args
)
86 if (!PyArg_ParseTuple(args
, "li:open_osfhandle", &handle
, &flags
))
89 fd
= _open_osfhandle(handle
, flags
);
91 return PyErr_SetFromErrno(PyExc_IOError
);
93 return PyInt_FromLong(fd
);
96 // Convert a C runtime file descriptor to an OS file handle.
98 msvcrt_get_osfhandle(PyObject
*self
, PyObject
*args
)
103 if (!PyArg_ParseTuple(args
,"i:get_osfhandle", &fd
))
106 handle
= _get_osfhandle(fd
);
108 return PyErr_SetFromErrno(PyExc_IOError
);
110 /* 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
);
119 msvcrt_kbhit(PyObject
*self
, PyObject
*args
)
123 if (!PyArg_ParseTuple(args
, ":kbhit"))
127 return PyInt_FromLong(ok
);
131 msvcrt_getch(PyObject
*self
, PyObject
*args
)
136 if (!PyArg_ParseTuple(args
, ":getch"))
139 Py_BEGIN_ALLOW_THREADS
143 return PyString_FromStringAndSize(s
, 1);
146 #ifdef _WCONIO_DEFINED
148 msvcrt_getwch(PyObject
*self
, PyObject
*args
)
153 if (!PyArg_ParseTuple(args
, ":getwch"))
156 Py_BEGIN_ALLOW_THREADS
160 return PyUnicode_FromUnicode(u
, 1);
165 msvcrt_getche(PyObject
*self
, PyObject
*args
)
170 if (!PyArg_ParseTuple(args
, ":getche"))
173 Py_BEGIN_ALLOW_THREADS
177 return PyString_FromStringAndSize(s
, 1);
180 #ifdef _WCONIO_DEFINED
182 msvcrt_getwche(PyObject
*self
, PyObject
*args
)
187 if (!PyArg_ParseTuple(args
, ":getwche"))
190 Py_BEGIN_ALLOW_THREADS
194 return PyUnicode_FromUnicode(s
, 1);
199 msvcrt_putch(PyObject
*self
, PyObject
*args
)
203 if (!PyArg_ParseTuple(args
, "c:putch", &ch
))
211 #ifdef _WCONIO_DEFINED
213 msvcrt_putwch(PyObject
*self
, PyObject
*args
)
218 if (!PyArg_ParseTuple(args
, "u#:putwch", &ch
, &size
))
222 PyErr_SetString(PyExc_ValueError
,
223 "Expected unicode string of length 1");
233 msvcrt_ungetch(PyObject
*self
, PyObject
*args
)
237 if (!PyArg_ParseTuple(args
, "c:ungetch", &ch
))
240 if (_ungetch(ch
) == EOF
)
241 return PyErr_SetFromErrno(PyExc_IOError
);
246 #ifdef _WCONIO_DEFINED
248 msvcrt_ungetwch(PyObject
*self
, PyObject
*args
)
252 if (!PyArg_ParseTuple(args
, "u:ungetwch", &ch
))
255 if (_ungetch(ch
) == EOF
)
256 return PyErr_SetFromErrno(PyExc_IOError
);
263 insertint(PyObject
*d
, char *name
, int value
)
265 PyObject
*v
= PyInt_FromLong((long) value
);
267 /* Don't bother reporting this error */
271 PyDict_SetItemString(d
, name
, v
);
277 /* List of functions exported by this module */
278 static struct PyMethodDef msvcrt_functions
[] = {
279 {"heapmin", msvcrt_heapmin
, METH_VARARGS
},
280 {"locking", msvcrt_locking
, METH_VARARGS
},
281 {"setmode", msvcrt_setmode
, METH_VARARGS
},
282 {"open_osfhandle", msvcrt_open_osfhandle
, METH_VARARGS
},
283 {"get_osfhandle", msvcrt_get_osfhandle
, METH_VARARGS
},
284 {"kbhit", msvcrt_kbhit
, METH_VARARGS
},
285 {"getch", msvcrt_getch
, METH_VARARGS
},
286 {"getche", msvcrt_getche
, METH_VARARGS
},
287 {"putch", msvcrt_putch
, METH_VARARGS
},
288 {"ungetch", msvcrt_ungetch
, METH_VARARGS
},
289 #ifdef _WCONIO_DEFINED
290 {"getwch", msvcrt_getwch
, METH_VARARGS
},
291 {"getwche", msvcrt_getwche
, METH_VARARGS
},
292 {"putwch", msvcrt_putwch
, METH_VARARGS
},
293 {"ungetwch", msvcrt_ungetwch
, METH_VARARGS
},
302 PyObject
*m
= Py_InitModule("msvcrt", msvcrt_functions
);
305 d
= PyModule_GetDict(m
);
307 /* constants for the locking() function's mode argument */
308 insertint(d
, "LK_LOCK", _LK_LOCK
);
309 insertint(d
, "LK_NBLCK", _LK_NBLCK
);
310 insertint(d
, "LK_NBRLCK", _LK_NBRLCK
);
311 insertint(d
, "LK_RLCK", _LK_RLCK
);
312 insertint(d
, "LK_UNLCK", _LK_UNLCK
);