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 |
|
---|
12 | This may only work on NT or 95...
|
---|
13 |
|
---|
14 | Author: Mark Hammond and Guido van Rossum.
|
---|
15 | Maintenance: Guido van Rossum.
|
---|
16 |
|
---|
17 | ***********************************************************/
|
---|
18 |
|
---|
19 | #include "Python.h"
|
---|
20 | #include "malloc.h"
|
---|
21 | #include <io.h>
|
---|
22 | #include <conio.h>
|
---|
23 | #include <sys/locking.h>
|
---|
24 |
|
---|
25 | // Force the malloc heap to clean itself up, and free unused blocks
|
---|
26 | // back to the OS. (According to the docs, only works on NT.)
|
---|
27 | static PyObject *
|
---|
28 | 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 | Py_INCREF(Py_None);
|
---|
37 | return Py_None;
|
---|
38 | }
|
---|
39 |
|
---|
40 | // Perform locking operations on a C runtime file descriptor.
|
---|
41 | static PyObject *
|
---|
42 | msvcrt_locking(PyObject *self, PyObject *args)
|
---|
43 | {
|
---|
44 | int fd;
|
---|
45 | int mode;
|
---|
46 | long nbytes;
|
---|
47 | int err;
|
---|
48 |
|
---|
49 | if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes))
|
---|
50 | return NULL;
|
---|
51 |
|
---|
52 | Py_BEGIN_ALLOW_THREADS
|
---|
53 | err = _locking(fd, mode, nbytes);
|
---|
54 | Py_END_ALLOW_THREADS
|
---|
55 | if (err != 0)
|
---|
56 | return PyErr_SetFromErrno(PyExc_IOError);
|
---|
57 |
|
---|
58 | Py_INCREF(Py_None);
|
---|
59 | return Py_None;
|
---|
60 | }
|
---|
61 |
|
---|
62 | // Set the file translation mode for a C runtime file descriptor.
|
---|
63 | static PyObject *
|
---|
64 | msvcrt_setmode(PyObject *self, PyObject *args)
|
---|
65 | {
|
---|
66 | int fd;
|
---|
67 | int flags;
|
---|
68 | if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags))
|
---|
69 | return NULL;
|
---|
70 |
|
---|
71 | flags = _setmode(fd, flags);
|
---|
72 | if (flags == -1)
|
---|
73 | return PyErr_SetFromErrno(PyExc_IOError);
|
---|
74 |
|
---|
75 | return PyInt_FromLong(flags);
|
---|
76 | }
|
---|
77 |
|
---|
78 | // Convert an OS file handle to a C runtime file descriptor.
|
---|
79 | static PyObject *
|
---|
80 | msvcrt_open_osfhandle(PyObject *self, PyObject *args)
|
---|
81 | {
|
---|
82 | long handle;
|
---|
83 | int flags;
|
---|
84 | int fd;
|
---|
85 |
|
---|
86 | if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags))
|
---|
87 | return NULL;
|
---|
88 |
|
---|
89 | fd = _open_osfhandle(handle, flags);
|
---|
90 | if (fd == -1)
|
---|
91 | return PyErr_SetFromErrno(PyExc_IOError);
|
---|
92 |
|
---|
93 | return PyInt_FromLong(fd);
|
---|
94 | }
|
---|
95 |
|
---|
96 | // Convert a C runtime file descriptor to an OS file handle.
|
---|
97 | static PyObject *
|
---|
98 | 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 | handle = _get_osfhandle(fd);
|
---|
107 | if (handle == -1)
|
---|
108 | return PyErr_SetFromErrno(PyExc_IOError);
|
---|
109 |
|
---|
110 | /* technically 'handle' is not a pointer, but a integer as
|
---|
111 | large as a pointer, Python's *VoidPtr interface is the
|
---|
112 | most appropriate here */
|
---|
113 | return PyLong_FromVoidPtr((void*)handle);
|
---|
114 | }
|
---|
115 |
|
---|
116 | /* Console I/O */
|
---|
117 |
|
---|
118 | static PyObject *
|
---|
119 | msvcrt_kbhit(PyObject *self, PyObject *args)
|
---|
120 | {
|
---|
121 | int ok;
|
---|
122 |
|
---|
123 | if (!PyArg_ParseTuple(args, ":kbhit"))
|
---|
124 | return NULL;
|
---|
125 |
|
---|
126 | ok = _kbhit();
|
---|
127 | return PyInt_FromLong(ok);
|
---|
128 | }
|
---|
129 |
|
---|
130 | static PyObject *
|
---|
131 | msvcrt_getch(PyObject *self, PyObject *args)
|
---|
132 | {
|
---|
133 | int ch;
|
---|
134 | char s[1];
|
---|
135 |
|
---|
136 | if (!PyArg_ParseTuple(args, ":getch"))
|
---|
137 | return NULL;
|
---|
138 |
|
---|
139 | Py_BEGIN_ALLOW_THREADS
|
---|
140 | ch = _getch();
|
---|
141 | Py_END_ALLOW_THREADS
|
---|
142 | s[0] = ch;
|
---|
143 | return PyString_FromStringAndSize(s, 1);
|
---|
144 | }
|
---|
145 |
|
---|
146 | static PyObject *
|
---|
147 | msvcrt_getche(PyObject *self, PyObject *args)
|
---|
148 | {
|
---|
149 | int ch;
|
---|
150 | char s[1];
|
---|
151 |
|
---|
152 | if (!PyArg_ParseTuple(args, ":getche"))
|
---|
153 | return NULL;
|
---|
154 |
|
---|
155 | Py_BEGIN_ALLOW_THREADS
|
---|
156 | ch = _getche();
|
---|
157 | Py_END_ALLOW_THREADS
|
---|
158 | s[0] = ch;
|
---|
159 | return PyString_FromStringAndSize(s, 1);
|
---|
160 | }
|
---|
161 |
|
---|
162 | static PyObject *
|
---|
163 | msvcrt_putch(PyObject *self, PyObject *args)
|
---|
164 | {
|
---|
165 | char ch;
|
---|
166 |
|
---|
167 | if (!PyArg_ParseTuple(args, "c:putch", &ch))
|
---|
168 | return NULL;
|
---|
169 |
|
---|
170 | _putch(ch);
|
---|
171 | Py_INCREF(Py_None);
|
---|
172 | return Py_None;
|
---|
173 | }
|
---|
174 |
|
---|
175 | static PyObject *
|
---|
176 | msvcrt_ungetch(PyObject *self, PyObject *args)
|
---|
177 | {
|
---|
178 | char ch;
|
---|
179 |
|
---|
180 | if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
|
---|
181 | return NULL;
|
---|
182 |
|
---|
183 | if (_ungetch(ch) == EOF)
|
---|
184 | return PyErr_SetFromErrno(PyExc_IOError);
|
---|
185 | Py_INCREF(Py_None);
|
---|
186 | return Py_None;
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | static void
|
---|
191 | insertint(PyObject *d, char *name, int value)
|
---|
192 | {
|
---|
193 | PyObject *v = PyInt_FromLong((long) value);
|
---|
194 | if (v == NULL) {
|
---|
195 | /* Don't bother reporting this error */
|
---|
196 | PyErr_Clear();
|
---|
197 | }
|
---|
198 | else {
|
---|
199 | PyDict_SetItemString(d, name, v);
|
---|
200 | Py_DECREF(v);
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | /* List of functions exported by this module */
|
---|
206 | static struct PyMethodDef msvcrt_functions[] = {
|
---|
207 | {"heapmin", msvcrt_heapmin, METH_VARARGS},
|
---|
208 | {"locking", msvcrt_locking, METH_VARARGS},
|
---|
209 | {"setmode", msvcrt_setmode, METH_VARARGS},
|
---|
210 | {"open_osfhandle", msvcrt_open_osfhandle, METH_VARARGS},
|
---|
211 | {"get_osfhandle", msvcrt_get_osfhandle, METH_VARARGS},
|
---|
212 | {"kbhit", msvcrt_kbhit, METH_VARARGS},
|
---|
213 | {"getch", msvcrt_getch, METH_VARARGS},
|
---|
214 | {"getche", msvcrt_getche, METH_VARARGS},
|
---|
215 | {"putch", msvcrt_putch, METH_VARARGS},
|
---|
216 | {"ungetch", msvcrt_ungetch, METH_VARARGS},
|
---|
217 | {NULL, NULL}
|
---|
218 | };
|
---|
219 |
|
---|
220 | PyMODINIT_FUNC
|
---|
221 | initmsvcrt(void)
|
---|
222 | {
|
---|
223 | PyObject *d;
|
---|
224 | PyObject *m = Py_InitModule("msvcrt", msvcrt_functions);
|
---|
225 | if (m == NULL)
|
---|
226 | return;
|
---|
227 | d = PyModule_GetDict(m);
|
---|
228 |
|
---|
229 | /* constants for the locking() function's mode argument */
|
---|
230 | insertint(d, "LK_LOCK", _LK_LOCK);
|
---|
231 | insertint(d, "LK_NBLCK", _LK_NBLCK);
|
---|
232 | insertint(d, "LK_NBRLCK", _LK_NBRLCK);
|
---|
233 | insertint(d, "LK_RLCK", _LK_RLCK);
|
---|
234 | insertint(d, "LK_UNLCK", _LK_UNLCK);
|
---|
235 | }
|
---|