[pywin32-checkins] pywin32/win32/src win32consolemodule.cpp,1.2,1.3
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: Roger U. <ru...@us...> - 2005-09-14 03:38:21
|
Update of /cvsroot/pywin32/pywin32/win32/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27457/win32/src Modified Files: win32consolemodule.cpp Log Message: Add GetStdHandle and SetStdHandle Don't automatically close handles obtained from an external source Index: win32consolemodule.cpp =================================================================== RCS file: /cvsroot/pywin32/pywin32/win32/src/win32consolemodule.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** win32consolemodule.cpp 13 Sep 2005 04:14:28 -0000 1.2 --- win32consolemodule.cpp 14 Sep 2005 03:38:13 -0000 1.3 *************** *** 410,423 **** // @object PyConsoleScreenBuffer|Handle to a console screen buffer ! // Create using <om win32console.CreateConsoleScreenBuffer>. // Use PyConsoleScreenBufferType(Handle) to wrap a pre-existing handle as returned by <om win32api.GetStdHandle>. ! // Will also accept a handle created by <om win32file.CreateFile> for CONIN$ or CONOUT$ class PyConsoleScreenBuffer : public PyHANDLE { public: ! PyConsoleScreenBuffer(HANDLE hconsole); ~PyConsoleScreenBuffer(void); ! static void deallocFunc(PyObject *ob); ! static struct PyMemberDef members[]; static struct PyMethodDef methods[]; static PyObject *tp_new(PyTypeObject *tp, PyObject *args, PyObject *kwargs); --- 410,426 ---- // @object PyConsoleScreenBuffer|Handle to a console screen buffer ! // Create using <om win32console.CreateConsoleScreenBuffer> or <om win32console.GetStdHandle> // Use PyConsoleScreenBufferType(Handle) to wrap a pre-existing handle as returned by <om win32api.GetStdHandle>. ! // Will also accept a handle created by <om win32file.CreateFile> for CONIN$ or CONOUT$. ! // Only handles created by CreateConsoleScreenBuffer will be closed when Python object is destroyed class PyConsoleScreenBuffer : public PyHANDLE { public: ! PyConsoleScreenBuffer(HANDLE hconsole, BOOL bautoclose); ~PyConsoleScreenBuffer(void); ! static void tp_dealloc(PyObject *ob); ! const char *GetTypeName() {return "PyConsoleScreenBuffer";} ! BOOL bAutoClose; ! // static struct PyMemberDef members[]; static struct PyMethodDef methods[]; static PyObject *tp_new(PyTypeObject *tp, PyObject *args, PyObject *kwargs); *************** *** 446,449 **** --- 449,453 ---- static PyObject *PyGetConsoleFontSize(PyObject *self, PyObject *args, PyObject *kwargs); static PyObject *PySetConsoleFont(PyObject *self, PyObject *args, PyObject *kwargs); + static PyObject *PySetStdHandle(PyObject *self, PyObject *args, PyObject *kwargs); }; *************** *** 516,522 **** {"ScrollConsoleScreenBuffer", (PyCFunction)PyConsoleScreenBuffer::PyScrollConsoleScreenBuffer, METH_VARARGS|METH_KEYWORDS, "Scrolls a region of the display"}, ! // @pymeth GetCurrentConsoleFont|Returns the font size for the console screen buffer {"GetCurrentConsoleFont", (PyCFunction)PyConsoleScreenBuffer::PyGetCurrentConsoleFont, METH_VARARGS|METH_KEYWORDS, ! "Returns the current font for the console"}, // @pymeth GetConsoleFontSize|Returns size of specified font for the console {"GetConsoleFontSize", (PyCFunction)PyConsoleScreenBuffer::PyGetConsoleFontSize, METH_VARARGS|METH_KEYWORDS, --- 520,526 ---- {"ScrollConsoleScreenBuffer", (PyCFunction)PyConsoleScreenBuffer::PyScrollConsoleScreenBuffer, METH_VARARGS|METH_KEYWORDS, "Scrolls a region of the display"}, ! // @pymeth GetCurrentConsoleFont|Returns the currently displayed font {"GetCurrentConsoleFont", (PyCFunction)PyConsoleScreenBuffer::PyGetCurrentConsoleFont, METH_VARARGS|METH_KEYWORDS, ! "Returns the currently displayed font"}, // @pymeth GetConsoleFontSize|Returns size of specified font for the console {"GetConsoleFontSize", (PyCFunction)PyConsoleScreenBuffer::PyGetConsoleFontSize, METH_VARARGS|METH_KEYWORDS, *************** *** 525,537 **** {"SetConsoleFont", (PyCFunction)PyConsoleScreenBuffer::PySetConsoleFont, METH_VARARGS|METH_KEYWORDS, "Changes the font used by the screen buffer"}, {NULL} }; ! ! struct PyMemberDef PyConsoleScreenBuffer::members[] = { {NULL} }; ! // @pymethod |<o PyConsoleScreenBuffer>|SetConsoleActiveScreenBuffer|Sets this handle as the currently displayed screen buffer PyObject *PyConsoleScreenBuffer::PySetConsoleActiveScreenBuffer(PyObject *self, PyObject *args) { --- 529,550 ---- {"SetConsoleFont", (PyCFunction)PyConsoleScreenBuffer::PySetConsoleFont, METH_VARARGS|METH_KEYWORDS, "Changes the font used by the screen buffer"}, + // @pymeth SetStdHandle|Replaces one of calling process's standard handles with this handle + {"SetStdHandle", (PyCFunction)PyConsoleScreenBuffer::PySetStdHandle, METH_VARARGS|METH_KEYWORDS, + "Replaces one of calling process's standard handles with this handle"}, {NULL} }; ! /* ! ?????? Class members can't be accessed thru normal means due to offsetof failing for derived classes ! which have a base with virtual methods ! Maybe expose AutoClose via Get/Set methods ??????? ! struct PyMemberDef PyConsoleScreenBuffer::members[] = ! { ! {"AutoClose", T_INT, offsetof(PyConsoleScreenBuffer, bAutoClose), 0, "Indicates whether handle should be closed when python object is destroyed"}, {NULL} }; + */ ! // @pymethod |PyConsoleScreenBuffer|SetConsoleActiveScreenBuffer|Sets this handle as the currently displayed screen buffer PyObject *PyConsoleScreenBuffer::PySetConsoleActiveScreenBuffer(PyObject *self, PyObject *args) { *************** *** 950,954 **** } ! // @pymethod (int, <o PyCOORD>)|PyConsoleScreenBuffer|GetCurrentConsoleFont|Returns the font size for the console screen buffer // @rdesc Returns the index of current font and window size // @comm Only exists on XP or later.<nl> --- 963,967 ---- } ! // @pymethod (int, <o PyCOORD>)|PyConsoleScreenBuffer|GetCurrentConsoleFont|Returns currently displayed font // @rdesc Returns the index of current font and window size // @comm Only exists on XP or later.<nl> *************** *** 1002,1005 **** --- 1015,1032 ---- } + // @pymethod |PyConsoleScreenBuffer|SetStdHandle|Replaces one of calling process's standard handles with this handle + PyObject *PyConsoleScreenBuffer::PySetStdHandle(PyObject *self, PyObject *args, PyObject *kwargs) + { + static char *keywords[]={"StdHandle", NULL}; + DWORD StdHandle; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "k:SetStdHandle", keywords, + &StdHandle)) // @pyparm int|StdHandle||Specifies handle to be replaced - STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, or STD_ERROR_HANDLE + return NULL; + if (!SetStdHandle(StdHandle, ((PyConsoleScreenBuffer *)self)->m_handle)) + return PyWin_SetAPIError("SetStdHandle"); + Py_INCREF(Py_None); + return Py_None; + } + PyTypeObject PyConsoleScreenBufferType = *************** *** 1010,1019 **** sizeof(PyConsoleScreenBuffer), 0, ! PyConsoleScreenBuffer::deallocFunc, // tp_dealloc 0, // tp_print 0, // tp_getattr 0, // tp_setattr 0, // tp_compare ! 0, // tp_repr PyHANDLEType.tp_as_number, // tp_as_number 0, // tp_as_sequence --- 1037,1046 ---- sizeof(PyConsoleScreenBuffer), 0, ! PyConsoleScreenBuffer::tp_dealloc, // tp_dealloc 0, // tp_print 0, // tp_getattr 0, // tp_setattr 0, // tp_compare ! PyHANDLE::strFunc, // tp_repr PyHANDLEType.tp_as_number, // tp_as_number 0, // tp_as_sequence *************** *** 1021,1025 **** 0, // tp_hash 0, // tp_call ! 0, // tp_str PyObject_GenericGetAttr, // tp_getattro PyObject_GenericSetAttr, // tp_setattro --- 1048,1052 ---- 0, // tp_hash 0, // tp_call ! PyHANDLE::strFunc, // tp_str PyObject_GenericGetAttr, // tp_getattro PyObject_GenericSetAttr, // tp_setattro *************** *** 1034,1038 **** 0, // tp_iternext PyConsoleScreenBuffer::methods, // tp_methods ! PyConsoleScreenBuffer::members, // tp_members 0, // tp_getset 0, // tp_base --- 1061,1065 ---- 0, // tp_iternext PyConsoleScreenBuffer::methods, // tp_methods ! 0, // PyConsoleScreenBuffer::members, // tp_members 0, // tp_getset 0, // tp_base *************** *** 1055,1074 **** if (!PyArg_ParseTupleAndKeywords(args, kwargs, "l", keywords, &h)) return NULL; ! return new PyConsoleScreenBuffer(h); } ! PyConsoleScreenBuffer::PyConsoleScreenBuffer(HANDLE hconsole) : PyHANDLE(hconsole) { ob_type = &PyConsoleScreenBufferType; } PyConsoleScreenBuffer::~PyConsoleScreenBuffer(void) { ! // don't try to close handle if it's been Detached ! if (m_handle) ! ::CloseHandle(m_handle); } ! void PyConsoleScreenBuffer::deallocFunc(PyObject *ob) { delete (PyConsoleScreenBuffer *)ob; --- 1082,1103 ---- if (!PyArg_ParseTupleAndKeywords(args, kwargs, "l", keywords, &h)) return NULL; ! return new PyConsoleScreenBuffer(h, FALSE); } ! PyConsoleScreenBuffer::PyConsoleScreenBuffer(HANDLE hconsole, BOOL bautoclose) : PyHANDLE(hconsole) { ob_type = &PyConsoleScreenBufferType; + bAutoClose=bautoclose; } PyConsoleScreenBuffer::~PyConsoleScreenBuffer(void) { ! // Only close handles explicitely created by CreateConsoleScreenBuffer ! // Let PyHANDLE's exception catching logic take care of it ! if (this->bAutoClose) ! this->Close(); } ! void PyConsoleScreenBuffer::tp_dealloc(PyObject *ob) { delete (PyConsoleScreenBuffer *)ob; *************** *** 1100,1104 **** if (hconsole==INVALID_HANDLE_VALUE) return PyWin_SetAPIError("CreateConsoleScreenBuffer"); ! return new PyConsoleScreenBuffer(hconsole); } --- 1129,1133 ---- if (hconsole==INVALID_HANDLE_VALUE) return PyWin_SetAPIError("CreateConsoleScreenBuffer"); ! return new PyConsoleScreenBuffer(hconsole, TRUE); } *************** *** 1365,1369 **** return NULL; h=(*pfnGetConsoleWindow)(); ! return PyInt_FromLong((long)h); } --- 1394,1398 ---- return NULL; h=(*pfnGetConsoleWindow)(); ! return PyWinObject_FromHANDLE(h); } *************** *** 1449,1452 **** --- 1478,1502 ---- } + // @pymethod <o PyConsoleScreenBuffer>|win32console|GetStdHandle|Returns one of calling process's standard handles + // @rdesc Returns a <o PyConsoleScreenBuffer> wrapping the handle, or None if specified handle does not exist + PyObject* PyGetStdHandle(PyObject *self, PyObject *args, PyObject *kwargs) + { + static char *keywords[]={"StdHandle", NULL}; + DWORD StdHandle; + HANDLE h; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "k:GetStdHandle", keywords, + &StdHandle)) // @pyparm int|StdHandle||Specifies the handle to return - STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, or STD_ERROR_HANDLE + return NULL; + h=GetStdHandle(StdHandle); + if (h==INVALID_HANDLE_VALUE) + return PyWin_SetAPIError("GetStdHandle"); + if (h==NULL){ + Py_INCREF(Py_None); + return Py_None; + } + return new PyConsoleScreenBuffer(h, FALSE); + } + + // @module win32console|Interface to the Windows Console functions for dealing with character-mode applications static struct PyMethodDef win32console_functions[] = { *************** *** 1483,1493 **** // @pymeth GetNumberOfConsoleFonts|Returns the number of fonts available to the console {"GetNumberOfConsoleFonts", PyGetNumberOfConsoleFonts, METH_VARARGS, "Returns the number of fonts available to the console"}, ! // @pymeth SetConsoleTitle|Sets the title of the console window ! {"SetConsoleTitle", (PyCFunction)PySetConsoleTitle, METH_VARARGS|METH_KEYWORDS, "Sets the title of the console window"}, ! // @pymeth GetConsoleTitle|Returns the title of the console window ! {"GetConsoleTitle", PyGetConsoleTitle, METH_VARARGS, "Returns the title of the console window"}, // @pymeth GenerateConsoleCtrlEvent|Sends a control signal to a group of processes attached to a common console {"GenerateConsoleCtrlEvent", (PyCFunction)PyGenerateConsoleCtrlEvent, METH_VARARGS|METH_KEYWORDS, "Sends a control signal to a group of processes attached to a common console"}, {NULL, NULL} }; --- 1533,1545 ---- // @pymeth GetNumberOfConsoleFonts|Returns the number of fonts available to the console {"GetNumberOfConsoleFonts", PyGetNumberOfConsoleFonts, METH_VARARGS, "Returns the number of fonts available to the console"}, ! // @pymeth SetConsoleTitle|Sets the title of calling process's console ! {"SetConsoleTitle", (PyCFunction)PySetConsoleTitle, METH_VARARGS|METH_KEYWORDS, "Sets the title of calling process's console"}, ! // @pymeth GetConsoleTitle|Returns the title of console to which calling process is attached ! {"GetConsoleTitle", PyGetConsoleTitle, METH_VARARGS, "Returns the title of console to which calling process is attached"}, // @pymeth GenerateConsoleCtrlEvent|Sends a control signal to a group of processes attached to a common console {"GenerateConsoleCtrlEvent", (PyCFunction)PyGenerateConsoleCtrlEvent, METH_VARARGS|METH_KEYWORDS, "Sends a control signal to a group of processes attached to a common console"}, + // @pymeth GetStdHandle|Returns one of calling process's standard handles + {"GetStdHandle", (PyCFunction)PyGetStdHandle, METH_VARARGS|METH_KEYWORDS, "Returns one of calling process's standard handles"}, {NULL, NULL} }; *************** *** 1578,1580 **** PyModule_AddIntConstant(mod, "CTRL_BREAK_EVENT", CTRL_BREAK_EVENT); ! } --- 1630,1636 ---- PyModule_AddIntConstant(mod, "CTRL_BREAK_EVENT", CTRL_BREAK_EVENT); ! // std handles ! PyModule_AddIntConstant(mod, "STD_INPUT_HANDLE", STD_INPUT_HANDLE); ! PyModule_AddIntConstant(mod, "STD_OUTPUT_HANDLE", STD_OUTPUT_HANDLE); ! PyModule_AddIntConstant(mod, "STD_ERROR_HANDLE", STD_ERROR_HANDLE); ! } |