Staging
v0.5.1
https://github.com/python/cpython
Revision cc47b05fe5c66bb2f1e520234039070044dad218 authored by Christian Heimes on 25 March 2008, 14:56:36 UTC, committed by Christian Heimes on 25 March 2008, 14:56:36 UTC
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r61834 | raymond.hettinger | 2008-03-24 07:07:49 +0100 (Mon, 24 Mar 2008) | 1 line

  Tighten documentation for Random.triangular.
........
  r61841 | raymond.hettinger | 2008-03-24 09:17:39 +0100 (Mon, 24 Mar 2008) | 1 line

  Issue 2460: Make Ellipsis objects copyable.
........
  r61842 | georg.brandl | 2008-03-24 10:34:34 +0100 (Mon, 24 Mar 2008) | 2 lines

  #1700821: add a note to audioop docs about signedness of sample formats.
........
  r61851 | christian.heimes | 2008-03-24 20:57:42 +0100 (Mon, 24 Mar 2008) | 1 line

  Added quick hack for bzr
........
  r61852 | christian.heimes | 2008-03-24 20:58:17 +0100 (Mon, 24 Mar 2008) | 1 line

  Added quick hack for bzr
........
  r61853 | amaury.forgeotdarc | 2008-03-24 22:04:10 +0100 (Mon, 24 Mar 2008) | 4 lines

  Issue2469: Correct a typo I introduced at r61793: compilation error with UCS4 builds.

  All buildbots compile with UCS2...
........
  r61863 | neal.norwitz | 2008-03-25 05:17:38 +0100 (Tue, 25 Mar 2008) | 2 lines

  Fix a bunch of UnboundLocalErrors when the tests fail.
........
  r61864 | neal.norwitz | 2008-03-25 05:18:18 +0100 (Tue, 25 Mar 2008) | 2 lines

  Try to fix a bunch of compiler warnings on Win64.
........
  r61869 | neal.norwitz | 2008-03-25 07:35:10 +0100 (Tue, 25 Mar 2008) | 3 lines

  Don't try to close a non-open file.
  Don't let file removal cause the test to fail.
........
  r61870 | neal.norwitz | 2008-03-25 08:00:39 +0100 (Tue, 25 Mar 2008) | 7 lines

  Try to get this test to be more stable:
   * disable gc during the test run because we are spawning objects and there
     was an exception when calling Popen.__del__
   * Always set an alarm handler so the process doesn't exit if the test fails
     (should probably add assertions on the value of hndl_called in more places)
   * Using a negative time causes Linux to treat it as zero, so disable that test.
........
  r61874 | gregory.p.smith | 2008-03-25 08:31:28 +0100 (Tue, 25 Mar 2008) | 2 lines

  Use a 32-bit unsigned int here, a long is not needed.
........
  r61889 | georg.brandl | 2008-03-25 12:59:51 +0100 (Tue, 25 Mar 2008) | 2 lines

  Move declarations to block start.
........
1 parent afbc266
Raw File
Tip revision: cc47b05fe5c66bb2f1e520234039070044dad218 authored by Christian Heimes on 25 March 2008, 14:56:36 UTC
Merged revisions 61834,61841-61842,61851-61853,61863-61864,61869-61870,61874,61889 via svnmerge from
Tip revision: cc47b05
msvcrtmodule.c
/*********************************************************

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

	This may only work on NT or 95...

	Author: Mark Hammond and Guido van Rossum.
	Maintenance: Guido van Rossum.

***********************************************************/

#include "Python.h"
#include "malloc.h"
#include <io.h>
#include <conio.h>
#include <sys/locking.h>
#include <crtdbg.h>
#include <windows.h>

// 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)
{
	if (!PyArg_ParseTuple(args, ":heapmin"))
		return NULL;

	if (_heapmin() != 0)
		return PyErr_SetFromErrno(PyExc_IOError);

	Py_INCREF(Py_None);
	return Py_None;
}

// Perform locking operations on a C runtime file descriptor.
static PyObject *
msvcrt_locking(PyObject *self, PyObject *args)
{
	int fd;
	int mode;
	long nbytes;
	int err;

	if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes))
		return NULL;

	Py_BEGIN_ALLOW_THREADS
	err = _locking(fd, mode, nbytes);
	Py_END_ALLOW_THREADS
	if (err != 0)
		return PyErr_SetFromErrno(PyExc_IOError);

	Py_INCREF(Py_None);
	return Py_None;
}

// 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;

	flags = _setmode(fd, flags);
	if (flags == -1)
		return PyErr_SetFromErrno(PyExc_IOError);

	return PyLong_FromLong(flags);
}

// Convert an OS file handle to a C runtime file descriptor.
static PyObject *
msvcrt_open_osfhandle(PyObject *self, PyObject *args)
{
	long handle;
	int flags;
	int fd;

	if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags))
		return NULL;

	fd = _open_osfhandle(handle, flags);
	if (fd == -1)
		return PyErr_SetFromErrno(PyExc_IOError);

	return PyLong_FromLong(fd);
}

// Convert a C runtime file descriptor to an OS file handle.
static PyObject *
msvcrt_get_osfhandle(PyObject *self, PyObject *args)
{
	int fd;
	Py_intptr_t handle;

	if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
		return NULL;

	handle = _get_osfhandle(fd);
	if (handle == -1)
		return PyErr_SetFromErrno(PyExc_IOError);

	/* 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);
}

/* Console I/O */

static PyObject *
msvcrt_kbhit(PyObject *self, PyObject *args)
{
	int ok;

	if (!PyArg_ParseTuple(args, ":kbhit"))
		return NULL;

	ok = _kbhit();
	return PyLong_FromLong(ok);
}

static PyObject *
msvcrt_getch(PyObject *self, PyObject *args)
{
	int ch;
	char s[1];

	if (!PyArg_ParseTuple(args, ":getch"))
		return NULL;

	Py_BEGIN_ALLOW_THREADS
	ch = _getch();
	Py_END_ALLOW_THREADS
	s[0] = ch;
	return PyString_FromStringAndSize(s, 1);
}

#if _MSC_VER >= 1300
static PyObject *
msvcrt_getwch(PyObject *self, PyObject *args)
{
	Py_UNICODE ch;
	Py_UNICODE u[1];

	if (!PyArg_ParseTuple(args, ":getwch"))
		return NULL;

	Py_BEGIN_ALLOW_THREADS
	ch = _getwch();
	Py_END_ALLOW_THREADS
	u[0] = ch;
	return PyUnicode_FromUnicode(u, 1);
}
#endif

static PyObject *
msvcrt_getche(PyObject *self, PyObject *args)
{
	int ch;
	char s[1];

	if (!PyArg_ParseTuple(args, ":getche"))
		return NULL;

	Py_BEGIN_ALLOW_THREADS
	ch = _getche();
	Py_END_ALLOW_THREADS
	s[0] = ch;
	return PyString_FromStringAndSize(s, 1);
}

#if _MSC_VER >= 1300
static PyObject *
msvcrt_getwche(PyObject *self, PyObject *args)
{
	Py_UNICODE ch;
	Py_UNICODE s[1];

	if (!PyArg_ParseTuple(args, ":getwche"))
		return NULL;

	Py_BEGIN_ALLOW_THREADS
	ch = _getwche();
	Py_END_ALLOW_THREADS
	s[0] = ch;
	return PyUnicode_FromUnicode(s, 1);
}
#endif

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;
}


#if _MSC_VER >= 1300
static PyObject *
msvcrt_putwch(PyObject *self, PyObject *args)
{
	Py_UNICODE *ch;
	int size;

	if (!PyArg_ParseTuple(args, "u#:putwch", &ch, &size))
		return NULL;

	if (size == 0) {
		PyErr_SetString(PyExc_ValueError,
			"Expected unicode string of length 1");
		return NULL;
	}
	_putwch(*ch);
	Py_RETURN_NONE;

}
#endif

static PyObject *
msvcrt_ungetch(PyObject *self, PyObject *args)
{
	char ch;

	if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
		return NULL;

	if (_ungetch(ch) == EOF)
		return PyErr_SetFromErrno(PyExc_IOError);
	Py_INCREF(Py_None);
	return Py_None;
}

#if _MSC_VER >= 1300
static PyObject *
msvcrt_ungetwch(PyObject *self, PyObject *args)
{
	Py_UNICODE ch;

	if (!PyArg_ParseTuple(args, "u:ungetwch", &ch))
		return NULL;

	if (_ungetch(ch) == EOF)
		return PyErr_SetFromErrno(PyExc_IOError);
	Py_INCREF(Py_None);
	return Py_None;
}
#endif

static void
insertint(PyObject *d, char *name, int value)
{
	PyObject *v = PyLong_FromLong((long) value);
	if (v == NULL) {
		/* Don't bother reporting this error */
		PyErr_Clear();
	}
	else {
		PyDict_SetItemString(d, name, v);
		Py_DECREF(v);
	}
}

#ifdef _DEBUG

static PyObject*
msvcrt_setreportfile(PyObject *self, PyObject *args)
{
	int type, file;
	_HFILE res;

	if (!PyArg_ParseTuple(args, "ii", &type, &file))
		return NULL;
	res = _CrtSetReportFile(type, (_HFILE)file);
	return PyLong_FromLong((long)res);
	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject*
msvcrt_setreportmode(PyObject *self, PyObject *args)
{
	int type, mode;
	int res;

	if (!PyArg_ParseTuple(args, "ii", &type, &mode))
		return NULL;
	res = _CrtSetReportMode(type, mode);
	if (res == -1)
	    return PyErr_SetFromErrno(PyExc_IOError);
	return PyLong_FromLong(res);
}

static PyObject*
msvcrt_seterrormode(PyObject *self, PyObject *args)
{
	int mode, res;

	if (!PyArg_ParseTuple(args, "i", &mode))
		return NULL;
	res = _set_error_mode(mode);
	return PyLong_FromLong(res);
}

#endif

static PyObject*
seterrormode(PyObject *self, PyObject *args)
{
	unsigned int mode, res;

	if (!PyArg_ParseTuple(args, "I", &mode))
		return NULL;
	res = SetErrorMode(mode);
	return PyLong_FromUnsignedLong(res);
}


/* List of functions exported by this module */
static struct PyMethodDef msvcrt_functions[] = {
	{"heapmin",		msvcrt_heapmin, METH_VARARGS},
	{"locking",             msvcrt_locking, METH_VARARGS},
	{"setmode",		msvcrt_setmode, METH_VARARGS},
	{"open_osfhandle",	msvcrt_open_osfhandle, METH_VARARGS},
	{"get_osfhandle",	msvcrt_get_osfhandle, METH_VARARGS},
	{"kbhit",		msvcrt_kbhit, METH_VARARGS},
	{"getch",		msvcrt_getch, METH_VARARGS},
	{"getche",		msvcrt_getche, METH_VARARGS},
	{"putch",		msvcrt_putch, METH_VARARGS},
	{"ungetch",		msvcrt_ungetch, METH_VARARGS},
	{"SetErrorMode",	seterrormode, METH_VARARGS},
#ifdef _DEBUG
	{"CrtSetReportFile",	msvcrt_setreportfile, METH_VARARGS},
	{"CrtSetReportMode",	msvcrt_setreportmode, METH_VARARGS},
	{"set_error_mode",	msvcrt_seterrormode, METH_VARARGS},
#endif
#if _MSC_VER >= 1300
	{"getwch",		msvcrt_getwch, METH_VARARGS},
	{"getwche",		msvcrt_getwche, METH_VARARGS},
	{"putwch",		msvcrt_putwch, METH_VARARGS},
	{"ungetwch",		msvcrt_ungetwch, METH_VARARGS},
#endif
	{NULL,			NULL}
};

PyMODINIT_FUNC
initmsvcrt(void)
{
	PyObject *d;
	PyObject *m = Py_InitModule("msvcrt", msvcrt_functions);
	if (m == NULL)
		return;
	d = PyModule_GetDict(m);

	/* constants for the locking() function's mode argument */
	insertint(d, "LK_LOCK", _LK_LOCK);
	insertint(d, "LK_NBLCK", _LK_NBLCK);
	insertint(d, "LK_NBRLCK", _LK_NBRLCK);
	insertint(d, "LK_RLCK", _LK_RLCK);
	insertint(d, "LK_UNLCK", _LK_UNLCK);
	insertint(d, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS);
	insertint(d, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT);
	insertint(d, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX);
	insertint(d, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX);
#ifdef _DEBUG
	insertint(d, "CRT_WARN", _CRT_WARN);
	insertint(d, "CRT_ERROR", _CRT_ERROR);
	insertint(d, "CRT_ASSERT", _CRT_ASSERT);
	insertint(d, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG);
	insertint(d, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE);
	insertint(d, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW);
	insertint(d, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE);
	insertint(d, "CRTDBG_FILE_STDERR", (int)_CRTDBG_FILE_STDERR);
	insertint(d, "CRTDBG_FILE_STDOUT", (int)_CRTDBG_FILE_STDOUT);
	insertint(d, "CRTDBG_REPORT_FILE", (int)_CRTDBG_REPORT_FILE);
#endif
}
back to top