1997-08-06 21:11:34 -03:00
|
|
|
/*********************************************************
|
|
|
|
|
|
|
|
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...
|
|
|
|
|
1997-08-13 16:57:53 -03:00
|
|
|
This may only work on NT or 95...
|
|
|
|
|
|
|
|
Author: Mark Hammond and Guido van Rossum.
|
|
|
|
Maintenance: Guido van Rossum.
|
|
|
|
|
1997-08-06 21:11:34 -03:00
|
|
|
***********************************************************/
|
1997-08-13 16:57:53 -03:00
|
|
|
|
1997-08-06 21:11:34 -03:00
|
|
|
#include "Python.h"
|
|
|
|
#include "malloc.h"
|
1997-08-13 16:57:53 -03:00
|
|
|
|
|
|
|
// 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)
|
1997-08-06 21:11:34 -03:00
|
|
|
{
|
1997-08-13 16:57:53 -03:00
|
|
|
if (!PyArg_ParseTuple(args, ":heapmin"))
|
1997-08-06 21:11:34 -03:00
|
|
|
return NULL;
|
1997-08-13 16:57:53 -03:00
|
|
|
|
|
|
|
if (_heapmin() != 0)
|
1997-08-06 21:11:34 -03:00
|
|
|
return PyErr_SetFromErrno(PyExc_IOError);
|
1997-08-13 16:57:53 -03:00
|
|
|
|
1997-08-06 21:11:34 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
1997-08-13 16:57:53 -03:00
|
|
|
// Perform locking operations on a C runtime file descriptor.
|
|
|
|
static PyObject *msvcrt_locking(PyObject *self, PyObject *args)
|
1997-08-06 21:11:34 -03:00
|
|
|
{
|
1997-08-13 16:57:53 -03:00
|
|
|
int fd;
|
|
|
|
int mode;
|
|
|
|
long nbytes;
|
1998-05-28 22:27:07 -03:00
|
|
|
int err;
|
1997-08-13 16:57:53 -03:00
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes))
|
1997-08-06 21:11:34 -03:00
|
|
|
return NULL;
|
1997-08-13 16:57:53 -03:00
|
|
|
|
1998-05-28 22:27:07 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
err = _locking(fd, mode, nbytes);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
if (err != 0)
|
1997-08-13 16:57:53 -03:00
|
|
|
return PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
|
1997-08-06 21:11:34 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
1997-08-13 16:57:53 -03:00
|
|
|
// 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;
|
1997-08-06 21:11:34 -03:00
|
|
|
|
1997-08-13 16:57:53 -03:00
|
|
|
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.
|
1997-08-06 21:11:34 -03:00
|
|
|
static PyObject *msvcrt_open_osfhandle(PyObject *self, PyObject *args)
|
|
|
|
{
|
1997-08-13 16:57:53 -03:00
|
|
|
long handle;
|
1997-08-06 21:11:34 -03:00
|
|
|
int flags;
|
1997-08-13 16:57:53 -03:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags))
|
1998-05-28 22:27:07 -03:00
|
|
|
return NULL;
|
1997-08-13 16:57:53 -03:00
|
|
|
|
|
|
|
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;
|
2000-06-30 14:48:51 -03:00
|
|
|
intptr_t handle;
|
1997-08-06 21:11:34 -03:00
|
|
|
|
1997-08-13 16:57:53 -03:00
|
|
|
if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
|
1997-08-06 21:11:34 -03:00
|
|
|
return NULL;
|
1997-08-13 16:57:53 -03:00
|
|
|
|
|
|
|
handle = _get_osfhandle(fd);
|
|
|
|
if (handle == -1)
|
1997-08-06 21:11:34 -03:00
|
|
|
return PyErr_SetFromErrno(PyExc_IOError);
|
|
|
|
|
2000-06-30 14:48:51 -03:00
|
|
|
/* 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);
|
1997-08-13 16:57:53 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
1998-05-28 22:27:07 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1997-08-13 16:57:53 -03:00
|
|
|
ch = _getch();
|
1998-05-28 22:27:07 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
1997-08-13 16:57:53 -03:00
|
|
|
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;
|
1997-08-06 21:11:34 -03:00
|
|
|
|
1998-05-28 22:27:07 -03:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
1997-08-13 16:57:53 -03:00
|
|
|
ch = _getche();
|
1998-05-28 22:27:07 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
1997-08-13 16:57:53 -03:00
|
|
|
s[0] = ch;
|
|
|
|
return PyString_FromStringAndSize(s, 1);
|
|
|
|
}
|
1997-08-06 21:11:34 -03:00
|
|
|
|
1997-08-13 16:57:53 -03:00
|
|
|
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;
|
1997-08-06 21:11:34 -03:00
|
|
|
}
|
1997-08-13 16:57:53 -03:00
|
|
|
|
|
|
|
static PyObject *msvcrt_ungetch(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
|
|
|
|
return NULL;
|
|
|
|
|
1999-02-16 15:40:02 -04:00
|
|
|
if (_ungetch(ch) == EOF)
|
|
|
|
return PyErr_SetFromErrno(PyExc_IOError);
|
1997-08-13 16:57:53 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
1997-08-06 21:11:34 -03:00
|
|
|
|
|
|
|
/* List of functions exported by this module */
|
|
|
|
static struct PyMethodDef msvcrt_functions[] = {
|
1997-08-13 16:57:53 -03:00
|
|
|
{"heapmin", msvcrt_heapmin, 1},
|
1997-08-06 21:11:34 -03:00
|
|
|
{"locking", msvcrt_locking, 1},
|
1997-08-13 16:57:53 -03:00
|
|
|
{"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},
|
1997-08-06 21:11:34 -03:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
__declspec(dllexport) void
|
|
|
|
initmsvcrt(void)
|
|
|
|
{
|
|
|
|
Py_InitModule("msvcrt", msvcrt_functions);
|
|
|
|
}
|