msvcrtmodule.c 4.93 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
#include <io.h>
#include <conio.h>
#include <sys/locking.h>
24 25 26

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

	if (_heapmin() != 0)
34
		return PyErr_SetFromErrno(PyExc_IOError);
35

36 37 38 39
	Py_INCREF(Py_None);
	return Py_None;
}

40
// Perform locking operations on a C runtime file descriptor.
41 42
static PyObject *
msvcrt_locking(PyObject *self, PyObject *args)
43
{
44 45 46
	int fd;
	int mode;
	long nbytes;
47
	int err;
48 49

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

52 53 54 55
	Py_BEGIN_ALLOW_THREADS
	err = _locking(fd, mode, nbytes);
	Py_END_ALLOW_THREADS
	if (err != 0)
56 57
		return PyErr_SetFromErrno(PyExc_IOError);

58 59 60 61
	Py_INCREF(Py_None);
	return Py_None;
}

62
// Set the file translation mode for a C runtime file descriptor.
63 64
static PyObject *
msvcrt_setmode(PyObject *self, PyObject *args)
65 66 67 68 69
{
	int fd;
	int flags;
	if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags))
		return NULL;
70

71 72 73 74 75 76 77 78
	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.
79 80
static PyObject *
msvcrt_open_osfhandle(PyObject *self, PyObject *args)
81
{
82
	long handle;
83
	int flags;
84 85 86
	int fd;

	if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags))
87
		return NULL;
88 89 90 91 92 93 94 95 96

	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.
97 98
static PyObject *
msvcrt_get_osfhandle(PyObject *self, PyObject *args)
99 100
{
	int fd;
101
	Py_intptr_t handle;
102

103
	if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
104
		return NULL;
105 106 107

	handle = _get_osfhandle(fd);
	if (handle == -1)
108 109
		return PyErr_SetFromErrno(PyExc_IOError);

110 111 112 113
	/* technically 'handle' is not a pointer, but a integer as
	   large as a pointer, Python's *VoidPtr interface is the
	   most appropriate here */
	return PyLong_FromVoidPtr((void*)handle);
114 115 116 117
}

/* Console I/O */

118 119
static PyObject *
msvcrt_kbhit(PyObject *self, PyObject *args)
120 121 122 123 124 125 126 127 128 129
{
	int ok;

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

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

130 131
static PyObject *
msvcrt_getch(PyObject *self, PyObject *args)
132 133 134 135 136 137 138
{
	int ch;
	char s[1];

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

139
	Py_BEGIN_ALLOW_THREADS
140
	ch = _getch();
141
	Py_END_ALLOW_THREADS
142 143 144 145
	s[0] = ch;
	return PyString_FromStringAndSize(s, 1);
}

146 147
static PyObject *
msvcrt_getche(PyObject *self, PyObject *args)
148 149 150 151 152 153
{
	int ch;
	char s[1];

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

155
	Py_BEGIN_ALLOW_THREADS
156
	ch = _getche();
157
	Py_END_ALLOW_THREADS
158 159 160
	s[0] = ch;
	return PyString_FromStringAndSize(s, 1);
}
161

162 163
static PyObject *
msvcrt_putch(PyObject *self, PyObject *args)
164 165 166 167 168 169 170 171 172
{
	char ch;

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

	_putch(ch);
	Py_INCREF(Py_None);
	return Py_None;
173
}
174

175 176
static PyObject *
msvcrt_ungetch(PyObject *self, PyObject *args)
177 178 179 180 181 182
{
	char ch;

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

183 184
	if (_ungetch(ch) == EOF)
		return PyErr_SetFromErrno(PyExc_IOError);
185 186 187 188
	Py_INCREF(Py_None);
	return Py_None;
}

189

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
static void
insertint(PyObject *d, char *name, int value)
{
	PyObject *v = PyInt_FromLong((long) value);
	if (v == NULL) {
		/* Don't bother reporting this error */
		PyErr_Clear();
	}
	else {
		PyDict_SetItemString(d, name, v);
		Py_DECREF(v);
	}
}


205 206
/* List of functions exported by this module */
static struct PyMethodDef msvcrt_functions[] = {
207 208 209 210 211 212 213 214 215 216
	{"heapmin",		msvcrt_heapmin, METH_VARARGS},
	{"locking",             msvcrt_locking, METH_VARARGS},
	{"setmode",		msvcrt_setmode, METH_VARARGS},
	{"open_osfhandle",	msvcrt_open_osfhandle, METH_VARARGS},
	{"get_osfhandle",	msvcrt_get_osfhandle, METH_VARARGS},
	{"kbhit",		msvcrt_kbhit, METH_VARARGS},
	{"getch",		msvcrt_getch, METH_VARARGS},
	{"getche",		msvcrt_getche, METH_VARARGS},
	{"putch",		msvcrt_putch, METH_VARARGS},
	{"ungetch",		msvcrt_ungetch, METH_VARARGS},
217 218 219
	{NULL,			NULL}
};

Thomas Heller's avatar
Thomas Heller committed
220
PyMODINIT_FUNC
221 222
initmsvcrt(void)
{
223 224 225 226 227 228 229 230 231
	PyObject *m = Py_InitModule("msvcrt", msvcrt_functions);
	PyObject *d = PyModule_GetDict(m);

	/* constants for the locking() function's mode argument */
	insertint(d, "LK_LOCK", _LK_LOCK);
	insertint(d, "LK_NBLCK", _LK_NBLCK);
	insertint(d, "LK_NBRLCK", _LK_NBRLCK);
	insertint(d, "LK_RLCK", _LK_RLCK);
	insertint(d, "LK_UNLCK", _LK_UNLCK);
232
}