summaryrefslogtreecommitdiffstats
path: root/PC/msvcrtmodule.c
blob: dec628516acf7ddb708c9128351e006ef15bb7d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*********************************************************

	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...

***********************************************************/
#include "Python.h"
#include "malloc.h"
// Perform locking operations on a file.
static PyObject *msvcrt_locking(PyObject *self, PyObject *args)
{
	int mode;
	long nBytes;
	PyObject *obFile;
	FILE *pFile;
	if (!PyArg_ParseTuple(args,"O!il:locking", &obFile, PyFile_Type, &mode, &nBytes))
		return NULL;
	if (NULL==(pFile = PyFile_AsFile(obFile)))
		return NULL;
	if (0 != _locking(_fileno(pFile), mode, nBytes))
		return PyErr_SetFromErrno(PyExc_IOError);
	Py_INCREF(Py_None);
	return Py_None;
}

// Forces the malloc heap to clean itself up, and free unused blocks
// back to the OS.
static PyObject *msvcrt_heapmin(PyObject *self, PyObject *args)
{
	if (!PyArg_ParseTuple(args,":heapmin"))
		return NULL;
	if (_heapmin()!=0)
		return PyErr_SetFromErrno(PyExc_MemoryError); // Is this the correct error???
	Py_INCREF(Py_None);
	return Py_None;
}

/*******
Left this out for now...

// Convert an OS file handle to a Python file object (yay!).
// This may only work on NT
static PyObject *msvcrt_open_osfhandle(PyObject *self, PyObject *args)
{
	// Note that we get the underlying handle using the long
	// "abstract" interface.  This will allow either a native integer
	// or else a Win32 extension PyHANDLE object, which implements an 
	// int() converter.
	PyObject *obHandle;
	PyObject *obInt;
	int flags;
	long handle;
	if (!PyArg_ParseTuple(args,"Oi:open_osfhandle", &obHandle, &flags))
		return NULL;

	if (NULL==(obInt = PyNumber_Int(obHandle))) {
		PyErr_Clear();
		PyErr_SetString(PyExc_TypeError, "The handle param must be an integer, =
or an object able to be converted to an integer");
		return NULL;
	}
	handle = PyInt_AsLong(obInt);
	Py_DECREF(obInt);
	rtHandle = _open_osfhandle(handle, flags);
	if (rtHandle==-1)
		return PyErr_SetFromErrno(PyExc_IOError);

  what mode?  Should I just return here, and expose _fdopen 
  and setvbuf?

	f1=_fdopen(fd1, "w");
	setvbuf(f1, NULL, _IONBF, 0);
	f=PyFile_FromFile(f1, cmdstring, "w", fclose);

}
*****/

/* List of functions exported by this module */
static struct PyMethodDef msvcrt_functions[] = {
	{"locking",             msvcrt_locking, 1},
	{"heapmin",				msvcrt_heapmin, 1},
	{NULL,			NULL}
};

__declspec(dllexport) void
initmsvcrt(void)
{
	Py_InitModule("msvcrt", msvcrt_functions);
}