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>
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 // Perform locking operations on a C runtime file descriptor.
50 msvcrt_locking(PyObject
*self
, PyObject
*args
)
57 if (!PyArg_ParseTuple(args
, "iil:locking", &fd
, &mode
, &nbytes
))
60 Py_BEGIN_ALLOW_THREADS
61 err
= _locking(fd
, mode
, nbytes
);
64 return PyErr_SetFromErrno(PyExc_IOError
);
70 // Set the file translation mode for a C runtime file descriptor.
72 msvcrt_setmode(PyObject
*self
, PyObject
*args
)
76 if (!PyArg_ParseTuple(args
,"ii:setmode", &fd
, &flags
))
79 flags
= _setmode(fd
, flags
);
81 return PyErr_SetFromErrno(PyExc_IOError
);
83 return PyLong_FromLong(flags
);
86 // Convert an OS file handle to a C runtime file descriptor.
88 msvcrt_open_osfhandle(PyObject
*self
, PyObject
*args
)
94 if (!PyArg_ParseTuple(args
, "li:open_osfhandle", &handle
, &flags
))
97 fd
= _open_osfhandle(handle
, flags
);
99 return PyErr_SetFromErrno(PyExc_IOError
);
101 return PyLong_FromLong(fd
);
104 // Convert a C runtime file descriptor to an OS file handle.
106 msvcrt_get_osfhandle(PyObject
*self
, PyObject
*args
)
111 if (!PyArg_ParseTuple(args
,"i:get_osfhandle", &fd
))
114 handle
= _get_osfhandle(fd
);
116 return PyErr_SetFromErrno(PyExc_IOError
);
118 /* technically 'handle' is not a pointer, but a integer as
119 large as a pointer, Python's *VoidPtr interface is the
120 most appropriate here */
121 return PyLong_FromVoidPtr((void*)handle
);
127 msvcrt_kbhit(PyObject
*self
, PyObject
*args
)
131 if (!PyArg_ParseTuple(args
, ":kbhit"))
135 return PyLong_FromLong(ok
);
139 msvcrt_getch(PyObject
*self
, PyObject
*args
)
144 if (!PyArg_ParseTuple(args
, ":getch"))
147 Py_BEGIN_ALLOW_THREADS
151 return PyBytes_FromStringAndSize(s
, 1);
154 #ifdef _WCONIO_DEFINED
156 msvcrt_getwch(PyObject
*self
, PyObject
*args
)
161 if (!PyArg_ParseTuple(args
, ":getwch"))
164 Py_BEGIN_ALLOW_THREADS
168 return PyUnicode_FromUnicode(u
, 1);
173 msvcrt_getche(PyObject
*self
, PyObject
*args
)
178 if (!PyArg_ParseTuple(args
, ":getche"))
181 Py_BEGIN_ALLOW_THREADS
185 return PyBytes_FromStringAndSize(s
, 1);
188 #ifdef _WCONIO_DEFINED
190 msvcrt_getwche(PyObject
*self
, PyObject
*args
)
195 if (!PyArg_ParseTuple(args
, ":getwche"))
198 Py_BEGIN_ALLOW_THREADS
202 return PyUnicode_FromUnicode(s
, 1);
207 msvcrt_putch(PyObject
*self
, PyObject
*args
)
211 if (!PyArg_ParseTuple(args
, "c:putch", &ch
))
219 #ifdef _WCONIO_DEFINED
221 msvcrt_putwch(PyObject
*self
, PyObject
*args
)
226 if (!PyArg_ParseTuple(args
, "u#:putwch", &ch
, &size
))
230 PyErr_SetString(PyExc_ValueError
,
231 "Expected unicode string of length 1");
241 msvcrt_ungetch(PyObject
*self
, PyObject
*args
)
245 if (!PyArg_ParseTuple(args
, "c:ungetch", &ch
))
248 if (_ungetch(ch
) == EOF
)
249 return PyErr_SetFromErrno(PyExc_IOError
);
254 #ifdef _WCONIO_DEFINED
256 msvcrt_ungetwch(PyObject
*self
, PyObject
*args
)
260 if (!PyArg_ParseTuple(args
, "u:ungetwch", &ch
))
263 if (_ungetch(ch
) == EOF
)
264 return PyErr_SetFromErrno(PyExc_IOError
);
271 insertint(PyObject
*d
, char *name
, int value
)
273 PyObject
*v
= PyLong_FromLong((long) value
);
275 /* Don't bother reporting this error */
279 PyDict_SetItemString(d
, name
, v
);
287 msvcrt_setreportfile(PyObject
*self
, PyObject
*args
)
292 if (!PyArg_ParseTuple(args
, "ii", &type
, &file
))
294 res
= _CrtSetReportFile(type
, (_HFILE
)file
);
295 return PyLong_FromLong((long)res
);
301 msvcrt_setreportmode(PyObject
*self
, PyObject
*args
)
306 if (!PyArg_ParseTuple(args
, "ii", &type
, &mode
))
308 res
= _CrtSetReportMode(type
, mode
);
310 return PyErr_SetFromErrno(PyExc_IOError
);
311 return PyLong_FromLong(res
);
315 msvcrt_seterrormode(PyObject
*self
, PyObject
*args
)
319 if (!PyArg_ParseTuple(args
, "i", &mode
))
321 res
= _set_error_mode(mode
);
322 return PyLong_FromLong(res
);
328 seterrormode(PyObject
*self
, PyObject
*args
)
330 unsigned int mode
, res
;
332 if (!PyArg_ParseTuple(args
, "I", &mode
))
334 res
= SetErrorMode(mode
);
335 return PyLong_FromUnsignedLong(res
);
339 /* List of functions exported by this module */
340 static struct PyMethodDef msvcrt_functions
[] = {
341 {"heapmin", msvcrt_heapmin
, METH_VARARGS
},
342 {"locking", msvcrt_locking
, METH_VARARGS
},
343 {"setmode", msvcrt_setmode
, METH_VARARGS
},
344 {"open_osfhandle", msvcrt_open_osfhandle
, METH_VARARGS
},
345 {"get_osfhandle", msvcrt_get_osfhandle
, METH_VARARGS
},
346 {"kbhit", msvcrt_kbhit
, METH_VARARGS
},
347 {"getch", msvcrt_getch
, METH_VARARGS
},
348 {"getche", msvcrt_getche
, METH_VARARGS
},
349 {"putch", msvcrt_putch
, METH_VARARGS
},
350 {"ungetch", msvcrt_ungetch
, METH_VARARGS
},
351 {"SetErrorMode", seterrormode
, METH_VARARGS
},
353 {"CrtSetReportFile", msvcrt_setreportfile
, METH_VARARGS
},
354 {"CrtSetReportMode", msvcrt_setreportmode
, METH_VARARGS
},
355 {"set_error_mode", msvcrt_seterrormode
, METH_VARARGS
},
357 #ifdef _WCONIO_DEFINED
358 {"getwch", msvcrt_getwch
, METH_VARARGS
},
359 {"getwche", msvcrt_getwche
, METH_VARARGS
},
360 {"putwch", msvcrt_putwch
, METH_VARARGS
},
361 {"ungetwch", msvcrt_ungetwch
, METH_VARARGS
},
367 static struct PyModuleDef msvcrtmodule
= {
368 PyModuleDef_HEAD_INIT
,
384 PyObject
*m
= PyModule_Create(&msvcrtmodule
);
387 d
= PyModule_GetDict(m
);
389 /* constants for the locking() function's mode argument */
390 insertint(d
, "LK_LOCK", _LK_LOCK
);
391 insertint(d
, "LK_NBLCK", _LK_NBLCK
);
392 insertint(d
, "LK_NBRLCK", _LK_NBRLCK
);
393 insertint(d
, "LK_RLCK", _LK_RLCK
);
394 insertint(d
, "LK_UNLCK", _LK_UNLCK
);
395 insertint(d
, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS
);
396 insertint(d
, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT
);
397 insertint(d
, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX
);
398 insertint(d
, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX
);
400 insertint(d
, "CRT_WARN", _CRT_WARN
);
401 insertint(d
, "CRT_ERROR", _CRT_ERROR
);
402 insertint(d
, "CRT_ASSERT", _CRT_ASSERT
);
403 insertint(d
, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG
);
404 insertint(d
, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE
);
405 insertint(d
, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW
);
406 insertint(d
, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE
);
407 insertint(d
, "CRTDBG_FILE_STDERR", (int)_CRTDBG_FILE_STDERR
);
408 insertint(d
, "CRTDBG_FILE_STDOUT", (int)_CRTDBG_FILE_STDOUT
);
409 insertint(d
, "CRTDBG_REPORT_FILE", (int)_CRTDBG_REPORT_FILE
);
412 /* constants for the crt versions */
413 #ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
414 st
= PyModule_AddStringConstant(m
, "VC_ASSEMBLY_PUBLICKEYTOKEN",
415 _VC_ASSEMBLY_PUBLICKEYTOKEN
);
416 if (st
< 0) return NULL
;
418 #ifdef _CRT_ASSEMBLY_VERSION
419 st
= PyModule_AddStringConstant(m
, "CRT_ASSEMBLY_VERSION",
420 _CRT_ASSEMBLY_VERSION
);
421 if (st
< 0) return NULL
;
423 #ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
424 st
= PyModule_AddStringConstant(m
, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
425 __LIBRARIES_ASSEMBLY_NAME_PREFIX
);
426 if (st
< 0) return NULL
;