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 | |
| 12 | ***********************************************************/ |
| 13 | #include "Python.h" |
| 14 | #include "malloc.h" |
| 15 | // Perform locking operations on a file. |
| 16 | static PyObject *msvcrt_locking(PyObject *self, PyObject *args) |
| 17 | { |
| 18 | int mode; |
| 19 | long nBytes; |
| 20 | PyObject *obFile; |
| 21 | FILE *pFile; |
| 22 | if (!PyArg_ParseTuple(args,"O!il:locking", &obFile, PyFile_Type, &mode, &nBytes)) |
| 23 | return NULL; |
| 24 | if (NULL==(pFile = PyFile_AsFile(obFile))) |
| 25 | return NULL; |
| 26 | if (0 != _locking(_fileno(pFile), mode, nBytes)) |
| 27 | return PyErr_SetFromErrno(PyExc_IOError); |
| 28 | Py_INCREF(Py_None); |
| 29 | return Py_None; |
| 30 | } |
| 31 | |
| 32 | // Forces the malloc heap to clean itself up, and free unused blocks |
| 33 | // back to the OS. |
| 34 | static PyObject *msvcrt_heapmin(PyObject *self, PyObject *args) |
| 35 | { |
| 36 | if (!PyArg_ParseTuple(args,":heapmin")) |
| 37 | return NULL; |
| 38 | if (_heapmin()!=0) |
| 39 | return PyErr_SetFromErrno(PyExc_MemoryError); // Is this the correct error??? |
| 40 | Py_INCREF(Py_None); |
| 41 | return Py_None; |
| 42 | } |
| 43 | |
| 44 | /******* |
| 45 | Left this out for now... |
| 46 | |
| 47 | // Convert an OS file handle to a Python file object (yay!). |
| 48 | // This may only work on NT |
| 49 | static PyObject *msvcrt_open_osfhandle(PyObject *self, PyObject *args) |
| 50 | { |
| 51 | // Note that we get the underlying handle using the long |
| 52 | // "abstract" interface. This will allow either a native integer |
| 53 | // or else a Win32 extension PyHANDLE object, which implements an |
| 54 | // int() converter. |
| 55 | PyObject *obHandle; |
| 56 | PyObject *obInt; |
| 57 | int flags; |
| 58 | long handle; |
| 59 | if (!PyArg_ParseTuple(args,"Oi:open_osfhandle", &obHandle, &flags)) |
| 60 | return NULL; |
| 61 | |
| 62 | if (NULL==(obInt = PyNumber_Int(obHandle))) { |
| 63 | PyErr_Clear(); |
| 64 | PyErr_SetString(PyExc_TypeError, "The handle param must be an integer, = |
| 65 | or an object able to be converted to an integer"); |
| 66 | return NULL; |
| 67 | } |
| 68 | handle = PyInt_AsLong(obInt); |
| 69 | Py_DECREF(obInt); |
| 70 | rtHandle = _open_osfhandle(handle, flags); |
| 71 | if (rtHandle==-1) |
| 72 | return PyErr_SetFromErrno(PyExc_IOError); |
| 73 | |
| 74 | what mode? Should I just return here, and expose _fdopen |
| 75 | and setvbuf? |
| 76 | |
| 77 | f1=_fdopen(fd1, "w"); |
| 78 | setvbuf(f1, NULL, _IONBF, 0); |
| 79 | f=PyFile_FromFile(f1, cmdstring, "w", fclose); |
| 80 | |
| 81 | } |
| 82 | *****/ |
| 83 | |
| 84 | /* List of functions exported by this module */ |
| 85 | static struct PyMethodDef msvcrt_functions[] = { |
| 86 | {"locking", msvcrt_locking, 1}, |
| 87 | {"heapmin", msvcrt_heapmin, 1}, |
| 88 | {NULL, NULL} |
| 89 | }; |
| 90 | |
| 91 | __declspec(dllexport) void |
| 92 | initmsvcrt(void) |
| 93 | { |
| 94 | Py_InitModule("msvcrt", msvcrt_functions); |
| 95 | } |