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 PyDoc_STRVAR(heapmin_doc
,
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.
55 msvcrt_locking(PyObject
*self
, PyObject
*args
)
62 if (!PyArg_ParseTuple(args
, "iil:locking", &fd
, &mode
, &nbytes
))
65 Py_BEGIN_ALLOW_THREADS
66 err
= _locking(fd
, mode
, nbytes
);
69 return PyErr_SetFromErrno(PyExc_IOError
);
75 PyDoc_STRVAR(locking_doc
,
76 "locking(fd, mode, nbytes) -> None\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\
86 // Set the file translation mode for a C runtime file descriptor.
88 msvcrt_setmode(PyObject
*self
, PyObject
*args
)
92 if (!PyArg_ParseTuple(args
,"ii:setmode", &fd
, &flags
))
95 flags
= _setmode(fd
, flags
);
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\
109 // Convert an OS file handle to a C runtime file descriptor.
111 msvcrt_open_osfhandle(PyObject
*self
, PyObject
*args
)
117 if (!PyArg_ParseTuple(args
, "li:open_osfhandle", &handle
, &flags
))
120 fd
= _open_osfhandle(handle
, flags
);
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.
137 msvcrt_get_osfhandle(PyObject
*self
, PyObject
*args
)
142 if (!PyArg_ParseTuple(args
,"i:get_osfhandle", &fd
))
145 handle
= _get_osfhandle(fd
);
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.");
164 msvcrt_kbhit(PyObject
*self
, PyObject
*args
)
168 if (!PyArg_ParseTuple(args
, ":kbhit"))
172 return PyInt_FromLong(ok
);
175 PyDoc_STRVAR(kbhit_doc
,
178 Return true if a keypress is waiting to be read.");
181 msvcrt_getch(PyObject
*self
, PyObject
*args
)
186 if (!PyArg_ParseTuple(args
, ":getch"))
189 Py_BEGIN_ALLOW_THREADS
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\
206 #ifdef _WCONIO_DEFINED
208 msvcrt_getwch(PyObject
*self
, PyObject
*args
)
213 if (!PyArg_ParseTuple(args
, ":getwch"))
216 Py_BEGIN_ALLOW_THREADS
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.");
230 msvcrt_getche(PyObject
*self
, PyObject
*args
)
235 if (!PyArg_ParseTuple(args
, ":getche"))
238 Py_BEGIN_ALLOW_THREADS
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
253 msvcrt_getwche(PyObject
*self
, PyObject
*args
)
258 if (!PyArg_ParseTuple(args
, ":getwche"))
261 Py_BEGIN_ALLOW_THREADS
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.");
275 msvcrt_putch(PyObject
*self
, PyObject
*args
)
279 if (!PyArg_ParseTuple(args
, "c:putch", &ch
))
287 PyDoc_STRVAR(putch_doc
,
288 "putch(char) -> None\n\
290 Print the character char to the console without buffering.");
292 #ifdef _WCONIO_DEFINED
294 msvcrt_putwch(PyObject
*self
, PyObject
*args
)
299 if (!PyArg_ParseTuple(args
, "u#:putwch", &ch
, &size
))
303 PyErr_SetString(PyExc_ValueError
,
304 "Expected unicode string of length 1");
312 PyDoc_STRVAR(putwch_doc
,
313 "putwch(unicode_char) -> None\n\
315 Wide char variant of putch(), accepting a Unicode value.");
319 msvcrt_ungetch(PyObject
*self
, PyObject
*args
)
323 if (!PyArg_ParseTuple(args
, "c:ungetch", &ch
))
326 if (_ungetch(ch
) == EOF
)
327 return PyErr_SetFromErrno(PyExc_IOError
);
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
340 msvcrt_ungetwch(PyObject
*self
, PyObject
*args
)
344 if (!PyArg_ParseTuple(args
, "u:ungetwch", &ch
))
347 if (_ungetch(ch
) == EOF
)
348 return PyErr_SetFromErrno(PyExc_IOError
);
353 PyDoc_STRVAR(ungetwch_doc
,
354 "ungetwch(unicode_char) -> None\n\
356 Wide char variant of ungetch(), accepting a Unicode value.");
360 insertint(PyObject
*d
, char *name
, int value
)
362 PyObject
*v
= PyInt_FromLong((long) value
);
364 /* Don't bother reporting this error */
368 PyDict_SetItemString(d
, name
, 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
},
400 PyObject
*m
= Py_InitModule("msvcrt", msvcrt_functions
);
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
);
418 #ifdef _CRT_ASSEMBLY_VERSION
419 st
= PyModule_AddStringConstant(m
, "CRT_ASSEMBLY_VERSION",
420 _CRT_ASSEMBLY_VERSION
);
423 #ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
424 st
= PyModule_AddStringConstant(m
, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
425 __LIBRARIES_ASSEMBLY_NAME_PREFIX
);