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>
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.)
34 msvcrt_heapmin(PyObject
*self
, PyObject
*args
)
36 if (!PyArg_ParseTuple(args
, ":heapmin"))
40 return PyErr_SetFromErrno(PyExc_IOError
);
46 // Perform locking operations on a C runtime file descriptor.
48 msvcrt_locking(PyObject
*self
, PyObject
*args
)
55 if (!PyArg_ParseTuple(args
, "iil:locking", &fd
, &mode
, &nbytes
))
58 Py_BEGIN_ALLOW_THREADS
59 err
= _locking(fd
, mode
, nbytes
);
62 return PyErr_SetFromErrno(PyExc_IOError
);
68 // Set the file translation mode for a C runtime file descriptor.
70 msvcrt_setmode(PyObject
*self
, PyObject
*args
)
74 if (!PyArg_ParseTuple(args
,"ii:setmode", &fd
, &flags
))
77 flags
= _setmode(fd
, flags
);
79 return PyErr_SetFromErrno(PyExc_IOError
);
81 return PyInt_FromLong(flags
);
84 // Convert an OS file handle to a C runtime file descriptor.
86 msvcrt_open_osfhandle(PyObject
*self
, PyObject
*args
)
92 if (!PyArg_ParseTuple(args
, "li:open_osfhandle", &handle
, &flags
))
95 fd
= _open_osfhandle(handle
, flags
);
97 return PyErr_SetFromErrno(PyExc_IOError
);
99 return PyInt_FromLong(fd
);
102 // Convert a C runtime file descriptor to an OS file handle.
104 msvcrt_get_osfhandle(PyObject
*self
, PyObject
*args
)
109 if (!PyArg_ParseTuple(args
,"i:get_osfhandle", &fd
))
112 handle
= _get_osfhandle(fd
);
114 return PyErr_SetFromErrno(PyExc_IOError
);
116 /* technically 'handle' is not a pointer, but a integer as
117 large as a pointer, Python's *VoidPtr interface is the
118 most appropriate here */
119 return PyLong_FromVoidPtr((void*)handle
);
125 msvcrt_kbhit(PyObject
*self
, PyObject
*args
)
129 if (!PyArg_ParseTuple(args
, ":kbhit"))
133 return PyInt_FromLong(ok
);
137 msvcrt_getch(PyObject
*self
, PyObject
*args
)
142 if (!PyArg_ParseTuple(args
, ":getch"))
145 Py_BEGIN_ALLOW_THREADS
149 return PyString_FromStringAndSize(s
, 1);
152 #ifdef _WCONIO_DEFINED
154 msvcrt_getwch(PyObject
*self
, PyObject
*args
)
159 if (!PyArg_ParseTuple(args
, ":getwch"))
162 Py_BEGIN_ALLOW_THREADS
166 return PyUnicode_FromUnicode(u
, 1);
171 msvcrt_getche(PyObject
*self
, PyObject
*args
)
176 if (!PyArg_ParseTuple(args
, ":getche"))
179 Py_BEGIN_ALLOW_THREADS
183 return PyString_FromStringAndSize(s
, 1);
186 #ifdef _WCONIO_DEFINED
188 msvcrt_getwche(PyObject
*self
, PyObject
*args
)
193 if (!PyArg_ParseTuple(args
, ":getwche"))
196 Py_BEGIN_ALLOW_THREADS
200 return PyUnicode_FromUnicode(s
, 1);
205 msvcrt_putch(PyObject
*self
, PyObject
*args
)
209 if (!PyArg_ParseTuple(args
, "c:putch", &ch
))
217 #ifdef _WCONIO_DEFINED
219 msvcrt_putwch(PyObject
*self
, PyObject
*args
)
224 if (!PyArg_ParseTuple(args
, "u#:putwch", &ch
, &size
))
228 PyErr_SetString(PyExc_ValueError
,
229 "Expected unicode string of length 1");
239 msvcrt_ungetch(PyObject
*self
, PyObject
*args
)
243 if (!PyArg_ParseTuple(args
, "c:ungetch", &ch
))
246 if (_ungetch(ch
) == EOF
)
247 return PyErr_SetFromErrno(PyExc_IOError
);
252 #ifdef _WCONIO_DEFINED
254 msvcrt_ungetwch(PyObject
*self
, PyObject
*args
)
258 if (!PyArg_ParseTuple(args
, "u:ungetwch", &ch
))
261 if (_ungetch(ch
) == EOF
)
262 return PyErr_SetFromErrno(PyExc_IOError
);
269 insertint(PyObject
*d
, char *name
, int value
)
271 PyObject
*v
= PyInt_FromLong((long) value
);
273 /* Don't bother reporting this error */
277 PyDict_SetItemString(d
, name
, v
);
283 /* List of functions exported by this module */
284 static struct PyMethodDef msvcrt_functions
[] = {
285 {"heapmin", msvcrt_heapmin
, METH_VARARGS
},
286 {"locking", msvcrt_locking
, METH_VARARGS
},
287 {"setmode", msvcrt_setmode
, METH_VARARGS
},
288 {"open_osfhandle", msvcrt_open_osfhandle
, METH_VARARGS
},
289 {"get_osfhandle", msvcrt_get_osfhandle
, METH_VARARGS
},
290 {"kbhit", msvcrt_kbhit
, METH_VARARGS
},
291 {"getch", msvcrt_getch
, METH_VARARGS
},
292 {"getche", msvcrt_getche
, METH_VARARGS
},
293 {"putch", msvcrt_putch
, METH_VARARGS
},
294 {"ungetch", msvcrt_ungetch
, METH_VARARGS
},
295 #ifdef _WCONIO_DEFINED
296 {"getwch", msvcrt_getwch
, METH_VARARGS
},
297 {"getwche", msvcrt_getwche
, METH_VARARGS
},
298 {"putwch", msvcrt_putwch
, METH_VARARGS
},
299 {"ungetwch", msvcrt_ungetwch
, METH_VARARGS
},
309 PyObject
*m
= Py_InitModule("msvcrt", msvcrt_functions
);
312 d
= PyModule_GetDict(m
);
314 /* constants for the locking() function's mode argument */
315 insertint(d
, "LK_LOCK", _LK_LOCK
);
316 insertint(d
, "LK_NBLCK", _LK_NBLCK
);
317 insertint(d
, "LK_NBRLCK", _LK_NBRLCK
);
318 insertint(d
, "LK_RLCK", _LK_RLCK
);
319 insertint(d
, "LK_UNLCK", _LK_UNLCK
);
321 /* constants for the crt versions */
322 #ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
323 st
= PyModule_AddStringConstant(m
, "VC_ASSEMBLY_PUBLICKEYTOKEN",
324 _VC_ASSEMBLY_PUBLICKEYTOKEN
);
327 #ifdef _CRT_ASSEMBLY_VERSION
328 st
= PyModule_AddStringConstant(m
, "CRT_ASSEMBLY_VERSION",
329 _CRT_ASSEMBLY_VERSION
);
332 #ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
333 st
= PyModule_AddStringConstant(m
, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
334 __LIBRARIES_ASSEMBLY_NAME_PREFIX
);