Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 1 | /********************************************************* |
| 2 | |
| 3 | msvcrtmodule.c |
| 4 | |
| 5 | A Python interface to the Microsoft Visual C Runtime |
| 6 | Library, providing access to those non-portable, but |
| 7 | still useful routines. |
| 8 | |
| 9 | Only ever compiled with an MS compiler, so no attempt |
| 10 | has been made to avoid MS language extensions, etc... |
| 11 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 12 | This may only work on NT or 95... |
| 13 | |
| 14 | Author: Mark Hammond and Guido van Rossum. |
| 15 | Maintenance: Guido van Rossum. |
| 16 | |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 17 | ***********************************************************/ |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 19 | #include "Python.h" |
| 20 | #include "malloc.h" |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 22 | // Force the malloc heap to clean itself up, and free unused blocks |
| 23 | // back to the OS. (According to the docs, only works on NT.) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 24 | static PyObject *msvcrt_heapmin(PyObject *self, PyObject *args) |
| 25 | { |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 26 | if (!PyArg_ParseTuple(args, ":heapmin")) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 27 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 28 | |
| 29 | if (_heapmin() != 0) |
| 30 | return PyErr_SetFromErrno(PyExc_IOError); |
| 31 | |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 32 | Py_INCREF(Py_None); |
| 33 | return Py_None; |
| 34 | } |
| 35 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 36 | // Perform locking operations on a C runtime file descriptor. |
| 37 | static PyObject *msvcrt_locking(PyObject *self, PyObject *args) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 38 | { |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 39 | int fd; |
| 40 | int mode; |
| 41 | long nbytes; |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 42 | int err; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 43 | |
| 44 | if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes)) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 45 | return NULL; |
| 46 | |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 47 | Py_BEGIN_ALLOW_THREADS |
| 48 | err = _locking(fd, mode, nbytes); |
| 49 | Py_END_ALLOW_THREADS |
| 50 | if (err != 0) |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 51 | return PyErr_SetFromErrno(PyExc_IOError); |
| 52 | |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 53 | Py_INCREF(Py_None); |
| 54 | return Py_None; |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 55 | } |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 56 | |
| 57 | // Set the file translation mode for a C runtime file descriptor. |
| 58 | static PyObject *msvcrt_setmode(PyObject *self, PyObject *args) |
| 59 | { |
| 60 | int fd; |
| 61 | int flags; |
| 62 | if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags)) |
| 63 | return NULL; |
| 64 | |
| 65 | flags = _setmode(fd, flags); |
| 66 | if (flags == -1) |
| 67 | return PyErr_SetFromErrno(PyExc_IOError); |
| 68 | |
| 69 | return PyInt_FromLong(flags); |
| 70 | } |
| 71 | |
| 72 | // Convert an OS file handle to a C runtime file descriptor. |
| 73 | static PyObject *msvcrt_open_osfhandle(PyObject *self, PyObject *args) |
| 74 | { |
| 75 | long handle; |
| 76 | int flags; |
| 77 | int fd; |
| 78 | |
| 79 | if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags)) |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 80 | return NULL; |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 81 | |
| 82 | fd = _open_osfhandle(handle, flags); |
| 83 | if (fd == -1) |
| 84 | return PyErr_SetFromErrno(PyExc_IOError); |
| 85 | |
| 86 | return PyInt_FromLong(fd); |
| 87 | } |
| 88 | |
| 89 | // Convert a C runtime file descriptor to an OS file handle. |
| 90 | static PyObject *msvcrt_get_osfhandle(PyObject *self, PyObject *args) |
| 91 | { |
| 92 | int fd; |
| 93 | long handle; |
| 94 | |
| 95 | if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd)) |
| 96 | return NULL; |
| 97 | |
| 98 | handle = _get_osfhandle(fd); |
| 99 | if (handle == -1) |
| 100 | return PyErr_SetFromErrno(PyExc_IOError); |
| 101 | |
| 102 | return PyInt_FromLong(handle); |
| 103 | } |
| 104 | |
| 105 | /* Console I/O */ |
| 106 | #include <conio.h> |
| 107 | |
| 108 | static PyObject *msvcrt_kbhit(PyObject *self, PyObject *args) |
| 109 | { |
| 110 | int ok; |
| 111 | |
| 112 | if (!PyArg_ParseTuple(args, ":kbhit")) |
| 113 | return NULL; |
| 114 | |
| 115 | ok = _kbhit(); |
| 116 | return PyInt_FromLong(ok); |
| 117 | } |
| 118 | |
| 119 | static PyObject *msvcrt_getch(PyObject *self, PyObject *args) |
| 120 | { |
| 121 | int ch; |
| 122 | char s[1]; |
| 123 | |
| 124 | if (!PyArg_ParseTuple(args, ":getch")) |
| 125 | return NULL; |
| 126 | |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 127 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 128 | ch = _getch(); |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 129 | Py_END_ALLOW_THREADS |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 130 | s[0] = ch; |
| 131 | return PyString_FromStringAndSize(s, 1); |
| 132 | } |
| 133 | |
| 134 | static PyObject *msvcrt_getche(PyObject *self, PyObject *args) |
| 135 | { |
| 136 | int ch; |
| 137 | char s[1]; |
| 138 | |
| 139 | if (!PyArg_ParseTuple(args, ":getche")) |
| 140 | return NULL; |
| 141 | |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 142 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 143 | ch = _getche(); |
Guido van Rossum | e4e021b | 1998-05-29 01:27:07 +0000 | [diff] [blame] | 144 | Py_END_ALLOW_THREADS |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 145 | s[0] = ch; |
| 146 | return PyString_FromStringAndSize(s, 1); |
| 147 | } |
| 148 | |
| 149 | static PyObject *msvcrt_putch(PyObject *self, PyObject *args) |
| 150 | { |
| 151 | char ch; |
| 152 | |
| 153 | if (!PyArg_ParseTuple(args, "c:putch", &ch)) |
| 154 | return NULL; |
| 155 | |
| 156 | _putch(ch); |
| 157 | Py_INCREF(Py_None); |
| 158 | return Py_None; |
| 159 | } |
| 160 | |
| 161 | static PyObject *msvcrt_ungetch(PyObject *self, PyObject *args) |
| 162 | { |
| 163 | char ch; |
| 164 | |
| 165 | if (!PyArg_ParseTuple(args, "c:ungetch", &ch)) |
| 166 | return NULL; |
| 167 | |
Guido van Rossum | 6543e88 | 1999-02-16 19:40:02 +0000 | [diff] [blame] | 168 | if (_ungetch(ch) == EOF) |
| 169 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 170 | Py_INCREF(Py_None); |
| 171 | return Py_None; |
| 172 | } |
| 173 | |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 174 | |
| 175 | /* List of functions exported by this module */ |
| 176 | static struct PyMethodDef msvcrt_functions[] = { |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 177 | {"heapmin", msvcrt_heapmin, 1}, |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 178 | {"locking", msvcrt_locking, 1}, |
Guido van Rossum | 407a22d | 1997-08-13 19:57:53 +0000 | [diff] [blame] | 179 | {"setmode", msvcrt_setmode, 1}, |
| 180 | {"open_osfhandle", msvcrt_open_osfhandle, 1}, |
| 181 | {"get_osfhandle", msvcrt_get_osfhandle, 1}, |
| 182 | {"kbhit", msvcrt_kbhit, 1}, |
| 183 | {"getch", msvcrt_getch, 1}, |
| 184 | {"getche", msvcrt_getche, 1}, |
| 185 | {"putch", msvcrt_putch, 1}, |
| 186 | {"ungetch", msvcrt_ungetch, 1}, |
Guido van Rossum | 29c1ea5 | 1997-08-07 00:11:34 +0000 | [diff] [blame] | 187 | {NULL, NULL} |
| 188 | }; |
| 189 | |
| 190 | __declspec(dllexport) void |
| 191 | initmsvcrt(void) |
| 192 | { |
| 193 | Py_InitModule("msvcrt", msvcrt_functions); |
| 194 | } |