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>
28 #if _MSC_VER >= 1500 && _MSC_VER < 1600
33 // Force the malloc heap to clean itself up, and free unused blocks
34 // back to the OS. (According to the docs, only works on NT.)
36 msvcrt_heapmin(PyObject
*self
, PyObject
*args
)
38 if (!PyArg_ParseTuple(args
, ":heapmin"))
42 return PyErr_SetFromErrno(PyExc_IOError
);
48 PyDoc_STRVAR(heapmin_doc
,
51 Force the malloc() heap to clean itself up and return unused blocks\n\
52 to the operating system. On failure, this raises IOError.");
54 // Perform locking operations on a C runtime file descriptor.
56 msvcrt_locking(PyObject
*self
, PyObject
*args
)
63 if (!PyArg_ParseTuple(args
, "iil:locking", &fd
, &mode
, &nbytes
))
66 Py_BEGIN_ALLOW_THREADS
67 err
= _locking(fd
, mode
, nbytes
);
70 return PyErr_SetFromErrno(PyExc_IOError
);
76 PyDoc_STRVAR(locking_doc
,
77 "locking(fd, mode, nbytes) -> None\n\
79 Lock part of a file based on file descriptor fd from the C runtime.\n\
80 Raises IOError on failure. The locked region of the file extends from\n\
81 the current file position for nbytes bytes, and may continue beyond\n\
82 the end of the file. mode must be one of the LK_* constants listed\n\
83 below. Multiple regions in a file may be locked at the same time, but\n\
84 may not overlap. Adjacent regions are not merged; they must be unlocked\n\
87 // Set the file translation mode for a C runtime file descriptor.
89 msvcrt_setmode(PyObject
*self
, PyObject
*args
)
93 if (!PyArg_ParseTuple(args
,"ii:setmode", &fd
, &flags
))
96 flags
= _setmode(fd
, flags
);
98 return PyErr_SetFromErrno(PyExc_IOError
);
100 return PyLong_FromLong(flags
);
103 PyDoc_STRVAR(setmode_doc
,
104 "setmode(fd, mode) -> Previous mode\n\
106 Set the line-end translation mode for the file descriptor fd. To set\n\
107 it to text mode, flags should be os.O_TEXT; for binary, it should be\n\
110 // Convert an OS file handle to a C runtime file descriptor.
112 msvcrt_open_osfhandle(PyObject
*self
, PyObject
*args
)
118 if (!PyArg_ParseTuple(args
, "li:open_osfhandle", &handle
, &flags
))
121 fd
= _open_osfhandle(handle
, flags
);
123 return PyErr_SetFromErrno(PyExc_IOError
);
125 return PyLong_FromLong(fd
);
128 PyDoc_STRVAR(open_osfhandle_doc
,
129 "open_osfhandle(handle, flags) -> file descriptor\n\
131 Create a C runtime file descriptor from the file handle handle. The\n\
132 flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY,\n\
133 and os.O_TEXT. The returned file descriptor may be used as a parameter\n\
134 to os.fdopen() to create a file object.");
136 // Convert a C runtime file descriptor to an OS file handle.
138 msvcrt_get_osfhandle(PyObject
*self
, PyObject
*args
)
143 if (!PyArg_ParseTuple(args
,"i:get_osfhandle", &fd
))
146 handle
= _get_osfhandle(fd
);
148 return PyErr_SetFromErrno(PyExc_IOError
);
150 /* technically 'handle' is not a pointer, but a integer as
151 large as a pointer, Python's *VoidPtr interface is the
152 most appropriate here */
153 return PyLong_FromVoidPtr((void*)handle
);
156 PyDoc_STRVAR(get_osfhandle_doc
,
157 "get_osfhandle(fd) -> file handle\n\
159 Return the file handle for the file descriptor fd. Raises IOError\n\
160 if fd is not recognized.");
165 msvcrt_kbhit(PyObject
*self
, PyObject
*args
)
169 if (!PyArg_ParseTuple(args
, ":kbhit"))
173 return PyLong_FromLong(ok
);
176 PyDoc_STRVAR(kbhit_doc
,
179 Return true if a keypress is waiting to be read.");
182 msvcrt_getch(PyObject
*self
, PyObject
*args
)
187 if (!PyArg_ParseTuple(args
, ":getch"))
190 Py_BEGIN_ALLOW_THREADS
194 return PyBytes_FromStringAndSize(s
, 1);
197 PyDoc_STRVAR(getch_doc
,
198 "getch() -> key character\n\
200 Read a keypress and return the resulting character. Nothing is echoed to\n\
201 the console. This call will block if a keypress is not already\n\
202 available, but will not wait for Enter to be pressed. If the pressed key\n\
203 was a special function key, this will return '\\000' or '\\xe0'; the next\n\
204 call will return the keycode. The Control-C keypress cannot be read with\n\
207 #ifdef _WCONIO_DEFINED
209 msvcrt_getwch(PyObject
*self
, PyObject
*args
)
214 if (!PyArg_ParseTuple(args
, ":getwch"))
217 Py_BEGIN_ALLOW_THREADS
221 return PyUnicode_FromUnicode(u
, 1);
224 PyDoc_STRVAR(getwch_doc
,
225 "getwch() -> Unicode key character\n\
227 Wide char variant of getch(), returning a Unicode value.");
231 msvcrt_getche(PyObject
*self
, PyObject
*args
)
236 if (!PyArg_ParseTuple(args
, ":getche"))
239 Py_BEGIN_ALLOW_THREADS
243 return PyBytes_FromStringAndSize(s
, 1);
246 PyDoc_STRVAR(getche_doc
,
247 "getche() -> key character\n\
249 Similar to getch(), but the keypress will be echoed if it represents\n\
250 a printable character.");
252 #ifdef _WCONIO_DEFINED
254 msvcrt_getwche(PyObject
*self
, PyObject
*args
)
259 if (!PyArg_ParseTuple(args
, ":getwche"))
262 Py_BEGIN_ALLOW_THREADS
266 return PyUnicode_FromUnicode(s
, 1);
269 PyDoc_STRVAR(getwche_doc
,
270 "getwche() -> Unicode key character\n\
272 Wide char variant of getche(), returning a Unicode value.");
276 msvcrt_putch(PyObject
*self
, PyObject
*args
)
280 if (!PyArg_ParseTuple(args
, "c:putch", &ch
))
288 PyDoc_STRVAR(putch_doc
,
289 "putch(char) -> None\n\
291 Print the character char to the console without buffering.");
293 #ifdef _WCONIO_DEFINED
295 msvcrt_putwch(PyObject
*self
, PyObject
*args
)
299 if (!PyArg_ParseTuple(args
, "C:putwch", &ch
))
307 PyDoc_STRVAR(putwch_doc
,
308 "putwch(unicode_char) -> None\n\
310 Wide char variant of putch(), accepting a Unicode value.");
314 msvcrt_ungetch(PyObject
*self
, PyObject
*args
)
318 if (!PyArg_ParseTuple(args
, "c:ungetch", &ch
))
321 if (_ungetch(ch
) == EOF
)
322 return PyErr_SetFromErrno(PyExc_IOError
);
327 PyDoc_STRVAR(ungetch_doc
,
328 "ungetch(char) -> None\n\
330 Cause the character char to be \"pushed back\" into the console buffer;\n\
331 it will be the next character read by getch() or getche().");
333 #ifdef _WCONIO_DEFINED
335 msvcrt_ungetwch(PyObject
*self
, PyObject
*args
)
339 if (!PyArg_ParseTuple(args
, "C:ungetwch", &ch
))
342 if (_ungetwch(ch
) == WEOF
)
343 return PyErr_SetFromErrno(PyExc_IOError
);
348 PyDoc_STRVAR(ungetwch_doc
,
349 "ungetwch(unicode_char) -> None\n\
351 Wide char variant of ungetch(), accepting a Unicode value.");
355 insertint(PyObject
*d
, char *name
, int value
)
357 PyObject
*v
= PyLong_FromLong((long) value
);
359 /* Don't bother reporting this error */
363 PyDict_SetItemString(d
, name
, v
);
371 msvcrt_setreportfile(PyObject
*self
, PyObject
*args
)
376 if (!PyArg_ParseTuple(args
, "ii", &type
, &file
))
378 res
= _CrtSetReportFile(type
, (_HFILE
)file
);
379 return PyLong_FromLong((long)res
);
385 msvcrt_setreportmode(PyObject
*self
, PyObject
*args
)
390 if (!PyArg_ParseTuple(args
, "ii", &type
, &mode
))
392 res
= _CrtSetReportMode(type
, mode
);
394 return PyErr_SetFromErrno(PyExc_IOError
);
395 return PyLong_FromLong(res
);
399 msvcrt_seterrormode(PyObject
*self
, PyObject
*args
)
403 if (!PyArg_ParseTuple(args
, "i", &mode
))
405 res
= _set_error_mode(mode
);
406 return PyLong_FromLong(res
);
412 seterrormode(PyObject
*self
, PyObject
*args
)
414 unsigned int mode
, res
;
416 if (!PyArg_ParseTuple(args
, "I", &mode
))
418 res
= SetErrorMode(mode
);
419 return PyLong_FromUnsignedLong(res
);
423 /* List of functions exported by this module */
424 static struct PyMethodDef msvcrt_functions
[] = {
425 {"heapmin", msvcrt_heapmin
, METH_VARARGS
, heapmin_doc
},
426 {"locking", msvcrt_locking
, METH_VARARGS
, locking_doc
},
427 {"setmode", msvcrt_setmode
, METH_VARARGS
, setmode_doc
},
428 {"open_osfhandle", msvcrt_open_osfhandle
, METH_VARARGS
, open_osfhandle_doc
},
429 {"get_osfhandle", msvcrt_get_osfhandle
, METH_VARARGS
, get_osfhandle_doc
},
430 {"kbhit", msvcrt_kbhit
, METH_VARARGS
, kbhit_doc
},
431 {"getch", msvcrt_getch
, METH_VARARGS
, getch_doc
},
432 {"getche", msvcrt_getche
, METH_VARARGS
, getche_doc
},
433 {"putch", msvcrt_putch
, METH_VARARGS
, putch_doc
},
434 {"ungetch", msvcrt_ungetch
, METH_VARARGS
, ungetch_doc
},
435 {"SetErrorMode", seterrormode
, METH_VARARGS
},
437 {"CrtSetReportFile", msvcrt_setreportfile
, METH_VARARGS
},
438 {"CrtSetReportMode", msvcrt_setreportmode
, METH_VARARGS
},
439 {"set_error_mode", msvcrt_seterrormode
, METH_VARARGS
},
441 #ifdef _WCONIO_DEFINED
442 {"getwch", msvcrt_getwch
, METH_VARARGS
, getwch_doc
},
443 {"getwche", msvcrt_getwche
, METH_VARARGS
, getwche_doc
},
444 {"putwch", msvcrt_putwch
, METH_VARARGS
, putwch_doc
},
445 {"ungetwch", msvcrt_ungetwch
, METH_VARARGS
, ungetwch_doc
},
451 static struct PyModuleDef msvcrtmodule
= {
452 PyModuleDef_HEAD_INIT
,
468 PyObject
*m
= PyModule_Create(&msvcrtmodule
);
471 d
= PyModule_GetDict(m
);
473 /* constants for the locking() function's mode argument */
474 insertint(d
, "LK_LOCK", _LK_LOCK
);
475 insertint(d
, "LK_NBLCK", _LK_NBLCK
);
476 insertint(d
, "LK_NBRLCK", _LK_NBRLCK
);
477 insertint(d
, "LK_RLCK", _LK_RLCK
);
478 insertint(d
, "LK_UNLCK", _LK_UNLCK
);
479 insertint(d
, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS
);
480 insertint(d
, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT
);
481 insertint(d
, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX
);
482 insertint(d
, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX
);
484 insertint(d
, "CRT_WARN", _CRT_WARN
);
485 insertint(d
, "CRT_ERROR", _CRT_ERROR
);
486 insertint(d
, "CRT_ASSERT", _CRT_ASSERT
);
487 insertint(d
, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG
);
488 insertint(d
, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE
);
489 insertint(d
, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW
);
490 insertint(d
, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE
);
491 insertint(d
, "CRTDBG_FILE_STDERR", (int)_CRTDBG_FILE_STDERR
);
492 insertint(d
, "CRTDBG_FILE_STDOUT", (int)_CRTDBG_FILE_STDOUT
);
493 insertint(d
, "CRTDBG_REPORT_FILE", (int)_CRTDBG_REPORT_FILE
);
496 /* constants for the crt versions */
497 #ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
498 st
= PyModule_AddStringConstant(m
, "VC_ASSEMBLY_PUBLICKEYTOKEN",
499 _VC_ASSEMBLY_PUBLICKEYTOKEN
);
500 if (st
< 0) return NULL
;
502 #ifdef _CRT_ASSEMBLY_VERSION
503 st
= PyModule_AddStringConstant(m
, "CRT_ASSEMBLY_VERSION",
504 _CRT_ASSEMBLY_VERSION
);
505 if (st
< 0) return NULL
;
507 #ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
508 st
= PyModule_AddStringConstant(m
, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
509 __LIBRARIES_ASSEMBLY_NAME_PREFIX
);
510 if (st
< 0) return NULL
;