msvcrtmodule.c 4.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*********************************************************

	msvcrtmodule.c

	A Python interface to the Microsoft Visual C Runtime
	Library, providing access to those non-portable, but
	still useful routines.

	Only ever compiled with an MS compiler, so no attempt
	has been made to avoid MS language extensions, etc...

12 13 14 15 16
	This may only work on NT or 95...

	Author: Mark Hammond and Guido van Rossum.
	Maintenance: Guido van Rossum.

17
***********************************************************/
18

19 20
#include "Python.h"
#include "malloc.h"
21 22 23 24

// Force the malloc heap to clean itself up, and free unused blocks
// back to the OS.  (According to the docs, only works on NT.)
static PyObject *msvcrt_heapmin(PyObject *self, PyObject *args)
25
{
26
	if (!PyArg_ParseTuple(args, ":heapmin"))
27
		return NULL;
28 29

	if (_heapmin() != 0)
30
		return PyErr_SetFromErrno(PyExc_IOError);
31

32 33 34 35
	Py_INCREF(Py_None);
	return Py_None;
}

36 37
// Perform locking operations on a C runtime file descriptor.
static PyObject *msvcrt_locking(PyObject *self, PyObject *args)
38
{
39 40 41
	int fd;
	int mode;
	long nbytes;
42
	int err;
43 44

	if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes))
45
		return NULL;
46

47 48 49 50
	Py_BEGIN_ALLOW_THREADS
	err = _locking(fd, mode, nbytes);
	Py_END_ALLOW_THREADS
	if (err != 0)
51 52
		return PyErr_SetFromErrno(PyExc_IOError);

53 54 55 56
	Py_INCREF(Py_None);
	return Py_None;
}

57 58 59 60 61 62 63
// Set the file translation mode for a C runtime file descriptor.
static PyObject *msvcrt_setmode(PyObject *self, PyObject *args)
{
	int fd;
	int flags;
	if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags))
		return NULL;
64

65 66 67 68 69 70 71 72
	flags = _setmode(fd, flags);
	if (flags == -1)
		return PyErr_SetFromErrno(PyExc_IOError);

	return PyInt_FromLong(flags);
}

// Convert an OS file handle to a C runtime file descriptor.
73 74
static PyObject *msvcrt_open_osfhandle(PyObject *self, PyObject *args)
{
75
	long handle;
76
	int flags;
77 78 79
	int fd;

	if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags))
80
		return NULL;
81 82 83 84 85 86 87 88 89 90 91 92

	fd = _open_osfhandle(handle, flags);
	if (fd == -1)
		return PyErr_SetFromErrno(PyExc_IOError);

	return PyInt_FromLong(fd);
}

// Convert a C runtime file descriptor to an OS file handle.
static PyObject *msvcrt_get_osfhandle(PyObject *self, PyObject *args)
{
	int fd;
93 94
	long handle;

95
	if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
96
		return NULL;
97 98 99

	handle = _get_osfhandle(fd);
	if (handle == -1)
100 101
		return PyErr_SetFromErrno(PyExc_IOError);

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	return PyInt_FromLong(handle);
}

/* Console I/O */
#include <conio.h>

static PyObject *msvcrt_kbhit(PyObject *self, PyObject *args)
{
	int ok;

	if (!PyArg_ParseTuple(args, ":kbhit"))
		return NULL;

	ok = _kbhit();
	return PyInt_FromLong(ok);
}

static PyObject *msvcrt_getch(PyObject *self, PyObject *args)
{
	int ch;
	char s[1];

	if (!PyArg_ParseTuple(args, ":getch"))
		return NULL;

127
	Py_BEGIN_ALLOW_THREADS
128
	ch = _getch();
129
	Py_END_ALLOW_THREADS
130 131 132 133 134 135 136 137 138 139 140
	s[0] = ch;
	return PyString_FromStringAndSize(s, 1);
}

static PyObject *msvcrt_getche(PyObject *self, PyObject *args)
{
	int ch;
	char s[1];

	if (!PyArg_ParseTuple(args, ":getche"))
		return NULL;
141

142
	Py_BEGIN_ALLOW_THREADS
143
	ch = _getche();
144
	Py_END_ALLOW_THREADS
145 146 147
	s[0] = ch;
	return PyString_FromStringAndSize(s, 1);
}
148

149 150 151 152 153 154 155 156 157 158
static PyObject *msvcrt_putch(PyObject *self, PyObject *args)
{
	char ch;

	if (!PyArg_ParseTuple(args, "c:putch", &ch))
		return NULL;

	_putch(ch);
	Py_INCREF(Py_None);
	return Py_None;
159
}
160 161 162 163 164 165 166 167 168 169 170 171 172

static PyObject *msvcrt_ungetch(PyObject *self, PyObject *args)
{
	char ch;

	if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
		return NULL;

	_ungetch(ch);
	Py_INCREF(Py_None);
	return Py_None;
}

173 174 175

/* List of functions exported by this module */
static struct PyMethodDef msvcrt_functions[] = {
176
	{"heapmin",		msvcrt_heapmin, 1},
177
	{"locking",             msvcrt_locking, 1},
178 179 180 181 182 183 184 185
	{"setmode",		msvcrt_setmode, 1},
	{"open_osfhandle",	msvcrt_open_osfhandle, 1},
	{"get_osfhandle",	msvcrt_get_osfhandle, 1},
	{"kbhit",		msvcrt_kbhit, 1},
	{"getch",		msvcrt_getch, 1},
	{"getche",		msvcrt_getche, 1},
	{"putch",		msvcrt_putch, 1},
	{"ungetch",		msvcrt_ungetch, 1},
186 187 188 189 190 191 192 193
	{NULL,			NULL}
};

__declspec(dllexport) void
initmsvcrt(void)
{
	Py_InitModule("msvcrt", msvcrt_functions);
}